This is a draft cheat sheet. It is a work in progress and is not finished yet.
Keywords
Keyword |
Description |
Example |
|
Logical and |
|
|
Part of the with-as
statement |
|
|
Assert that something is true. |
`assert False, "Error!" |
|
Stop this loop right now. |
|
|
Define a class. |
|
|
Don't process more of the loop, do it again. |
|
|
Define a function. |
|
|
Delete from dictionary. |
|
|
Else if |
|
|
Else condition |
|
|
If an exception happens, do this.` |
except ValueError, e: print e
|
|
Run a string as Python |
|
|
Exceptions or not, finally do this no matter what. |
|
|
Loop over a collection of things. |
|
|
Importing specific parts of a module. |
|
|
Declares a global variable |
|
|
If condition |
|
|
Import a module into this one to use. |
|
|
Part of for-loops
. Also a test of X in Y. |
for X in Y: pass
also 1 in [1] == True
|
|
|
1 is 1 == True |
|
Create a short anonymous function |
s = lambda y: y ** y; s(3)
|
|
Logical not |
|
|
Logical or |
|
|
This block is empty. |
|
|
Print this string. |
|
|
Raise an exception when things go wrong. |
raise ValueError("No")
|
|
Exit the function with a return value. |
|
|
Try this block, and if exception, go to except
. |
|
|
While loop |
|
|
With an expression as a variable do. |
|
|
Pause here and return to caller. |
def x(): yield Y; X().next()
|
|
|
Data Types
Type |
Description |
Example |
|
True boolean value |
|
|
False boolean value. |
|
|
Represents "nothing" or "no value". |
x = None |
|
Stores textual info |
|
|
Stores integers |
|
|
Stores decimals |
|
|
Stores a list of things |
|
|
Stores a key=value mapping of things |
e = {'x': 1, 'y': 2} |
String Formats
Escape |
Description |
Example |
|
Decimal int |
|
|
Octal number |
`%o % 1000 == '1750' |
|
Unsigned decimal |
|
|
Hexadecimal lowercase |
|
|
Hexadecimal uppercase. |
|
|
Exponential notation, lowercase 'e'. |
"%e" % 1000 == '1.000000e+03'
|
|
Exponential notation, uppercase 'E'. |
"%E" % 1000 == '1.000000E+03'
|
|
Floating point real number. |
"%f" % 10.34 == '10.340000'
|
|
Same as %f. |
"%F" % 10.34 == '10.340000'
|
|
Either %f or %e, whichever is shorter.` |
|
|
Same as %g but uppercase. |
|
|
Character format. |
|
|
Repr format (debugging format). |
"%r" % int == "<type 'int'>"
|
|
String format. |
"%s there" % 'hi' == 'hi there'
|
|
A percent sign. |
"%g%%" % 10.34 == '10.34%'
|
|
|
String Escape Sequences
\\ |
Backslash |
|
Single-quote |
|
Double-quote |
|
Bell |
|
Backspace |
|
Formfeed |
|
|
|
Carriage |
|
Tab |
|
Vertical tab |
|