Python Summary Primary sources: http://www.greenteapress.com/thinkpython/html/ http://www.python.org Chapter 1: The Way of the Program print "Hello World!" can be typed in interpreter or stored in hello.py in "Python shell": restart shell, import hello Chapter 2: Variables, Expressions and Statements ------------------------------------------------ type() returns the type of the expression print , ..., print several expressions = assign value to variable operators: + - * / normal arithmetic operators operators: ** % ** is exponentiation, % is mod () > ** > * / % > + - precedence (otherwise mostly left-to-right) = raw_input() read a text line of input = input() read a numerical line of input # text comment Chapter 3: Functions -------------------- int() convert to int str() convert to string import math make math functions available math.sin, math.cos, math.sqrt examples of math functions help('math') list of all math functions (q to quit help) defining functions: def (, ..., ): Chapter 4: Conditionals and Recursion ------------------------------------- type boolean (logical tests): either True or False x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y x and y # x and y are both true x or y # x or y (or both) are true not x # x is not true if/else statements: if : if : else: if : elif : else: return statement (immediately exits function, returning a value): return recursion: base case(s) and recursive case(s) usually in if/else Chapter 5: Fruitful functions ----------------------------- examples of functions that return values boolean function returns True or False adding temporary print statements is helpful while developing/debugging Chapter 7: Strings ------------------ s[index] return character at index (0 based) len(s) length of string strings are immutable construct new string for a change s[-1], s[-2], s[-3] last character, 2nd to last, 3rd to last s[i:j] slice of string: i through (j - 1) s[:j] slice from beginning through (j - 1) s[i:] slice from i to end of string + concatenate two strings help('str') show string functions (q to quit help) s.lower() returns lowercase version of s str.isalpha is alphabetic character (boolean function) foreach loop over string: for in : Chapter 8: Lists ---------------- [, ..., ] a list same use of [], len lists are very similar to strings range(n) list of integers 0 through (n - 1) range(i, j) list of integers i through (j - 1) [] empty list in test for list membership (boolean) + append two lists together to form new list list1 = list(list2) make a copy of list2, store in list1 = list() convert string to list .append() append value to end of list .remove() remove given value from list del lst[i] remove value at index i foreach loop over list: for in : Chapter 11: Files and Exceptions -------------------------------- = open(, 'r') open a file for reading .readLine() return next line of file as a string .readLines() return all lines of file as list of strings Higher-Order Functions ---------------------- map(, ) returns list obtained by applying function to each value in list filter(, ) returns list of values that return True for the given boolean function reduce(, ) returns the value obtained by reducing the list with the given binary function int.__add__ built-in addition function str.__add__ built-in concatenation function list.__add__ built-in append function Anonymous function: lambda , ..., : example: filter(lambda x: x in 'aeiou', 'how are you doing, Joe?') which returns ['o', 'a', 'e', 'o', 'u', 'o', 'i', 'o', 'e']