Learning Machines

Taught by Patrick Hebron at ITP, Fall 2015


Getting Started in Python:


Documentation:

Hello, World:

print( "Hello, World!" )

Variables:

myInt = 2
myReal = 2.34
myString = "hello"

print( myInt )
print( myReal )
print( myString )

Arithmetic Operators:

print( 1 + 2 + 3 + 4 )
print( 10 - 2 )
print( 2 * 3.14 )
print( 8 / 3 )
print( 8 / 3.0 )

String Operators:

myString = "hello"
print( len( myString ) ) # prints string length
print( myString[0] )     # prints "h"
print( myString[0:2] )   # prints "he"
myString = myString.upper() # converts string to uppercase
myString = myString.lower() # converts string to lowercase

if "hello" == "hello":
    print( "strings equal" )

if "hello" in "hello world":
    print( "'hello' found in 'hello world'" )

Lists:

myList = [ "Drake", "Derp", "Derek", "Dominique" ]
print myList                # prints all elements
print myList[0]             # print first element
print myList[1]             # prints second element
myList.append("Victoria")   # add element
myList.remove("Derp")       # remove element
myList.sort()               # sorts the list in alphabetical order
myList.reverse()            # reverses order

Loops:

for i in range( 1, 10 ):
    print i

myList = [ "Drake", "Derp", "Derek", "Dominique" ]
for item in myList:
    print item

Conditionals:

x = 3

if x < 10:
   print 'x smaller than 10'
else:
   print 'x is bigger than 10 or equal'

if x > 10 and x < 20:
    print "In range"
else:
    print "Out of range"

Functions:

def myFunc(x):
    return x*x

print myFunc( 3 )