Skip to content

Instantly share code, notes, and snippets.

View kaydell's full-sized avatar

Kaydell Leavitt kaydell

  • Self
  • Layton, Utah, USA
View GitHub Profile
@kaydell
kaydell / PythonTypes.py
Last active December 27, 2015 14:28
A Python Script That Demonstrates int and float literals and how to get the type of any value.
#!/usr/bin/env python3
"""
Script version: Python 3 (may not work in Python 2)
This script demonstrates setting a variable to an int and to a float.
A float is sometimes called a "real" and an int is also known as a
"whole number".
The point of this demo:
@kaydell
kaydell / loop_once_or_more.py
Created October 9, 2013 18:43
This is a demo of doing a loop that happens one or more times in Python 3
while True:
# read number from user
# if the number is in the range to exit, then break out of this loop
if n >= 1 and n <= 10:
break
@kaydell
kaydell / ask_int.py
Created October 9, 2013 17:41
A Python 3 Demo of Asking the User To Input An Int
# This script is a demonstration of asking the user to enter an
# int in Python 3
# ask the user to enter an int
string = input("Please enter an int: ")
# the function input() returns a str we need to convert it to be an int
n = int(string)
# echo the int back to the user
@kaydell
kaydell / python3_gui_template.py
Created October 9, 2013 15:23
Python 3 GUI Starting Point
#!/usr/bin/env python3
def main():
# require Python 3
import sys
if sys.version_info.major < 3:
print("This program requires Python 3 or later")
return
@kaydell
kaydell / gist:6826708
Created October 4, 2013 14:20
This bash script demonstrates how to specify that you want the script to use Python 3 rather than Python 2 which comes with Mac OS X.
#!/usr/bin/env python3
import sys
print(sys.version_info)
@kaydell
kaydell / gist:6826647
Created October 4, 2013 14:16
Demonstration #1 of Using the Version of Python That Comes With Mac OS X
#!/usr/bin/env python
import sys
print(sys.version_info)
@kaydell
kaydell / gist:6826385
Last active December 24, 2015 16:09
Configure Python 3 Without Losing Python 2 On Mac OS X
# This code needs to be placed within the .bash_profile file of the user's home directory
# You can use a text editor such as Text Wrangler.app downloaded from the following
# link to create or edit the file .bash_profile, which is an invisible file because it begins
# with a dot.
# TextWrangler: http://www.barebones.com/products/textwrangler/download.html
# set the path to our Python Library
# require that '$HOME' be set to a dir
if [ -d "$HOME" ]; then
# set the path to our PYTHON_LIB
@kaydell
kaydell / python_or_java.py
Last active December 24, 2015 08:49
This gist is a sample of Python code that demonstrates a simpler, easier syntax for beginning programming students to learn more easily. Consider teaching Python for introductory courses, and teaching Java to more advanced students in a more advanced course.
"""
This Python source file demonstrates how Python for-loops
and the main() method are easier for students to understand
than Java is.
About Python: http://www.python.org/about/
Free Python Download: http://www.python.org/download/
It is worth considering teaching beginning students Python
and teach Java in more advanced courses.
@kaydell
kaydell / SwingGUIDemo.java
Created September 25, 2013 20:20
Using Multiple Threads With a Swing GUI
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
* This class is an example of how to correctly startup a GUI interface
*
* From Oracle's Java Tutorial:
* http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
*
@kaydell
kaydell / Matrix.java
Last active December 23, 2015 22:29
Example Java Code for a Matrix which is a two-dimensional array.
/**
* This class demonstrates how to write code that can process a matrix (i.e. a two-dimensional array)
* Note that the method printMatrix will print any two-dimensional array of ints
*
* @author kaydell
*
* Copyright, 2013, Kaydell Leavitt, All Rights Reserved
*
* {link http://simple.wikipedia.org/wiki/Matrix_%28mathematics%29}
*