Statement | Comment | Examples |
Expression | Any expression. The results are discarded. Often used for their side effects. | (1 * 2) ** 3 / 4
"string"[2:] == "ring"
someFunction(1, 2, 3)
|
|
Assignment | Assigns an expression to a target. | x = 3 x = f(1,32,3) * 10 + x
list[1:3] = [7, 8, 9 , 10]
|
|
Augmented Assignment | Updates a target with an expression. | |
Unpacking Assignment | Assigns elements of a sequence to multiple targets; very convenient for access to tuple and list members. Note that the expression 1,2,3 is a short form of (1,2,3) . | |
Multiple Assignment | Assigns the same expression to multiple targets. | |
Pass | No operation. | |
If If/Else If/Elif/Else | Conditional processing. | if x < 0: x = -x
if x == 3: print "It's three!"
if x == 1: ... elif x == 2: ... elif x == 3: ... else: print "Bad value " + x
|
|
While | Looping over a condition. | x = 10 while x > 0: print x x -= 1
|
|
For | Iterates over a sequence. Use range to iterate over a numeric sequence. | for i in range(10): print i
for c in "A string": print c
for i in (10, "Hello", 1e6): print func(i)
|
|
Continue | Advances to the next loop (while/for) iteration. | for i in range(10): if not testOk(i): continue :
|
|
Break | Exits a loop (while/for) | for i in range(10): if testBad(i): break :
|
|
Delete | Removes a variable or sequence element(s) or class attribute. | del x
del list[3:5]
del x.attr1
|
|
Global | Declares a reference to a global value; used in functions. | x,y,z = 0,1,2 def f(a,b,c): global x,y,z :
|
|
Print | Prints expression(s) to a stream. | print "Hello World!" print "Hello","World!" print "Hello" + ' ' + "World!"
msg = "Name: %(last)s, (first)s" data = {'last':"Feigenbaum", 'first':"Barry"} print >>out, msg % data
|
|
Assert | Asserts a condition is true. | def process(v): assert v > 0, "Illegal value %i" % v :
|
|
Import | Imports all or part of a module. | import sys from sys import argv from javax import swing from java.util import Map
|
|
Execute | Executes a string/file as a subprogram. There is also a related function, exec , that executes a constructed string and returns its result. This support allows you to dynamically create programs. | globals = {'x':1, 'y':2, 'z':3} locals = {'list':(1,2,3)} code = "print x, y, z, list" exec code in globals, locals
|
|
Try/Except | Executes code within an exception handler. | try: x = int(string) except ValueError, e: x = 0
|
|
Try/Finally | Executes code with a cleanup routine. | f = open("test.dat") try: lines = f.readlines() finally: f.close()
|
|
Raise | Creates and throws an exception. | def factorial(x): raise ValueError, "x must be > 0" :
|
|
Define | Defines a function; arguments may be optional and/or keywords; Functions are generic which allows for very flexible programming. | def f(x,y=2,z=10): return x+y+z
q = f(1,2) + f(3) + f(4,5,6) s = f("a","b","c")
|
|
Return | Returns from a function with an optional value. | |
Class | Defines a class (a container for attributes and methods) . | class X: pass
class MyClass(SomeClass): pass
|
|
No comments:
Post a Comment