Introduction to Python ---------------------- Reference: http://www.ibiblio.org/obp/thinkCSpy print statements variables and assignment statements data: numbers, booleans, strings arithmetic operations: + - * / ** % ( ) comparisons: == != < > <= >= logical operators: and or not if/elif/else statements while loops input statements lists - indexing (zero based) - len function ------------------------------------------------------ Example: printing out numbers from 1 to 10 Pseudocode: set COUNT to 1 while COUNT <= 10 do print COUNT add 1 to COUNT (end of loop) print "all done!" Equivalent Python code: count = 1 while count <= 10: print count count = count + 1 print "all done!" --------------------------------------------------------------------------------- Example: finding the largest value in a list Pseudocode: set BIGGEST to 0 set POSITION to 1 input a list of values V1 ... Vn set i to 1 while i <= n do if Vi > BIGGEST then set BIGGEST to Vi set POSITION to i (end of loop) output BIGGEST and POSITION Equivalent Python code (notice that first value in list is at position 0, not 1): biggest = 0 position = 0 values = input("Enter a list of values: ") i = 0 while i < n: if values[i] > biggest: biggest = values[i] position = i print 'Biggest is', biggest, 'at position', position