Keywords Flashcards
and
Logical and.
True and False == False
as
Part of the with-as statement.
with X as Y: pass
assert
Assert (ensure) that something is true.
assert False, “Error!”
break
Stop this loop right now.
while True: break
class
Define a class.
class Person(object)
continue
Don’t process more of the loop, do it again.
while True: continue
def
Define a function.
def X(): pass
del
Delete from dictionary.
del X[Y]
except
If an exception happens, do this.
except ValueError, e: print e
exec
Run a string as Python.
exec ‘print “hello”’
finally
Exceptions or not, finally do this no matter what.
finally: pass
from
Importing specific parts of a module.
from x import Y
global
Declare that you want a global variable.
global X
in
Part of for-loops. Also a test of X in Y.
for X in Y: pass also 1 in [1] == True
is
Like == to test equality.
1 is 1 == True
lambda
Create a short anonymous function.
s = lambda y: y ** y; s(3)
not
Logical not.
not True == False
or
Logical or.
True or False == True
raise
Raise an exception when things go wrong.
raise ValueError(“No”)
with
With an expression as a variable do.
with X as Y: pass
yield
Pause here and return to caller.
def X(): yield Y; X().next()
dicts
Stores a key=value mapping of things.
e = {‘x’: 1, ‘y’: 2}
lists
Stores a list of things.
j = [1,2,3,4]