Download Python 2.7 Quick Reference Sheet and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity! Python 2.7 Quick Reference Sheet ver 2.01 110105 (sjd) Interactive Help in Python Shell help() Invoke interactive help help(m) Display help for module m help(f) Display help for function f dir(m) Display names in module m Small Operator Precedence Table func_name(args, ) Function call x[index : index] Slicing x[index] Indexing x.attribute Attribute reference ** Exponentiation *, /, % Multiply, divide, mod +, -‐ Add, subtract >, <, <=, >=, !=, == Comparison in, not in Membership tests not, and, or Boolean operators NOT, AND, OR Module Import import module_name from module_name import name , from module_name import * Common Data Types Type Description Literal Ex int long float complex bool str tuple list dict 32-‐bit Integer Integer > 32 bits Floating point number Complex number Boolean Character sequence Immutable sequence Mutable sequence Mapping 3, -‐4 101L 3.0, -‐6.55 1.2J True, False Python (2, 4, 7) [2, x, 3.1] { x:2, y:5 } Common Syntax Structures Assignment Statement var = exp Console Input/Output var = input( [prompt] ) var = raw_input( [prompt] ) print exp[,] Selection if (boolean_exp): stmt [elif (boolean_exp): stmt ] [else: stmt ] Repetition while (boolean_exp): stmt Traversal for var in traversable_object: stmt Function Definition def function_name( parmameters ): stmt Function Call function_name( arguments ) Class Definition class Class_name [ (super_class) ]: [ class variables ] def method_name( self, parameters ): stmt Object Instantiation obj_ref = Class_name( arguments ) Method Invocation obj_ref.method_name( arguments ) Exception Handling try: stmt except [exception_type] [, var]: stmt Common Built-‐in Functions Function Returns abs(x) Absolute value of x dict() Empty dictionary, eg: d = dict() float(x) int or string x as float id(obj) memory addr of obj int (x) float or string x as int len(s) Number of items in sequence s list() Empty list, eg: m = list() max(s) Maximum value of items in s min(s) Minimum value of items in s open(f) Open filename f for input ord(c) ASCII code of c pow(x,y) x ** y range(x) A list of x ints 0 to x -‐ 1 round(x,n) float x rounded to n places str(obj) str representation of obj sum(s) Sum of numeric sequence s tuple(items) tuple of items type(obj) Data type of obj Common Math Module Functions Function Returns (all float) ceil(x) Smallest whole nbr >= x cos(x) Cosine of x radians degrees(x) x radians in degrees radians(x) x degrees in radians exp(x) e ** x floor(x) Largest whole nbr <= x hypot(x, y) sqrt(x * x + y * y) log(x [, base]) Log of x to base or natural log if base not given pow(x, y) x ** y sin(x) Sine of x radians sqrt(x) Positive square root of x tan(x) Tangent of x radians pi Math constant pi to 15 sig figs e Math constant e to 15 sig figs