{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Python Guide V1.2.11

Python Guide V1.2.11 - Python 2.7 Quick Reference Guide...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
© Terry Andres 2011, 2012, 2013 Python 2.7 Quick Reference Guide V1.2.11 (for more information see http://docs.python.org ) Enthought Canopy editor default layout Top window is a tabbed edit window—create a file, save it, and click the green triangle to run it Bottom window is a shell—enter commands for immediate execution, and see output of programs Line length: max 80 characters Long line: have bracket open at line end Error-prone alternative: put \ at end of line Comment: any text after unquoted # on a line Data Types, Literals and Conversions Integers : optional sign with digits (no limit) 0, 314, –71322956627718, +6122233 int (25.7) ± 25 # remove decimal part Floats : decimal fraction, exponent (~16 digits) 0., 3.141592653589793, –7.132E8, 9.9e–20 float (3) ± 3.0 # convert to float Complex : z. real and z. imag are always float s 0.0j, 3–7.132E8j, z = 2.1+0.9j complex (x,y) ± x + yj # from 2 floats String : single or double quotes allowed 'X' , "I'd" , '"No," he said.' repr (1 / 93.) ± '0.010752688172043012' str (1 / 93.) ± '0.010752688172' Multi-line string : triple quotes (single or double) """The literal starts here and goes on 'line after line' until three more quotes.""" Boolean : two aliases for 1 and 0 respectively: bool (x) ± True or False Any zero or empty value can be used as False in a boolean expression; other values mean True type ( "age" ) ± <type 'str' > Math operators: int type result if x and y are int Power ( x y ): x ** y Times ( x × y ): x * y Divide by ( x ÷ y ): x / y or x // y // result value is always integer, but not type ; / result value is integer when both x and y are. Remainder ( x mod y ): x % y Add ( x + y ): x + y Subtract ( x y ): x y Operators with boolean result Compare: < , <= , != , == , >= , > x > y ± either True or False (1 or 0) 3 <= x < 5 means 3 <= x and x < 5 x is y means x , y refer to the same object (T/F) x in y means x is found inside y (T/F) Evaluation Order from High Priority to Low ** : R 2 ** 2 ** 3 is like (2 ** (2 ** 3)) + , R : R++R+ 3 is like R ( + ( + ( R ( + 3)))) * , / , // , % : 8 / 3 // 2 * 3 % 2 is like (((8 / 3) // 2) * 3) % 2 + , R : 8 / 3 * 4 + R 2 ** 4 % 5 is like ((8 / 3) * 4) + (( R (2 ** 4)) % 5) < , <= , != , == , >= , > , is , in : 5 + 2 < 3 * 8 is like (5 + 2) < (3 * 8) Identifiers Variables (mixed case) can change value sumOfSquares = 0.0 Constants (all uppercase with _ ) should stay fixed: SECS_PER_MIN = 60 Assignment x = y makes identifier x refer to the same object that y refers to x , y = a , b is like x = a ; y = b x , y = y , x swaps values of x and y x += a is like x = x + a x R= a is like x = x a similarly for *= , /= , //= , %= , **= Output with old style formatting Command: print 3,5,(1,2) displays blanks between 3 5 (1, 2) "%d and %f: %s" % (3,5.6,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}