''' Variables ''' # Python provides dynamic typing of its variables. # You do not have to define a type of the variable. Python will take care of this for you. # This is a text s = "abc" s = 'abc' # This is an integer x = 1 y = 2 z = x + y ''' Loops and if clauses ''' # The body of the loops and if clauses is indented. # Each line within a block must be indented by the same amount x = int(raw_input("Please enter an integer: ")) # input if x < 0: x = 0 print 'Negative changed to zero' # output elif x == 0: print 'Zero' elif x == 1: print 'Single' else: print 'More' a, b = 0, 1 # multiple assignment while b < 10: print b a, b = b, a+b a = ['cat', 'window', 'defenestrate'] for x in a: print x, len(x) ''' Methods ''' # Python allows to define methods via the keyword def. # As the language is interpreted, the methods need to be defined before using it. def add(a, b): return a + b print add(1, 2) ''' Lists ''' # List items need not all have the same type. A = ['a', "b", 123] # List Comprehensions S = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] V = [2**i for i in range(13)] # [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] M = [x for x in S if x % 2 == 0] # [0, 4, 16, 36, 64] # Print the first list element print(A[0]) # List indices start at 0 # Print the last element print(A[-1]) # Negative values starts the list from the end # Sublist print(A[0:2]) # first and second element # Add elements to the list A.append("x") # Print the content of the list for element in A: print(element) # or a = ['Mary', 'had', 'a', 'little', 'lamb'] for i in range(len(a)): # range(10) generates a list of 10 values (0, 1, ..., 9) print a[i] ''' break and continue Statements, and else Clauses on Loops ''' # The break statement, like in C, breaks out of the smallest enclosing for or while loop. # The continue statement, also borrowed from C, continues with the next iteration of the loop. # Loop statements may have an else clause; # it is executed when the loop terminates through exhaustion of the list (with for) # or when the condition becomes false (with while), # but not when the loop is terminated by a break statement. for n in range(2, 10): for x in range(2, n): # range(2, n): 2, 3, ..., n-1 if n % x == 0: print n, 'equals', x, '*', n/x break else: # loop fell through without finding a factor print n, 'is a prime number' ''' The range() Function ''' range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # It is possible to let the range start at another number, # or to specify a different increment (even negative) range(5, 10) # [5, 6, 7, 8, 9] range(0, 10, 3) # [0, 3, 6, 9] range(-10, -100, -30) # [-10, -40, -70] ''' The timeit() Function ''' # The timeit() function runs the code many times (default one million) # and takes an average of the timings import timeit def function(x): return x # no import needed if the method is a Python built-in function t = timeit.Timer('function(123)', 'from __main__ import function') # time function 1000 times, divide result by 1000 # then multiply by 1000000 to get microseconds # measuredTime = (1000000 * t.timeit(number=1000)/1000) = (1000 * t.timeit(number=1000)) measuredTime = (1000 * t.timeit(number=1000)) print "function(123) takes %0.3f micro-seconds" % measuredTime