Flow Control

Python has all the basic flow control structures:
Conditional
if 5 > 3:
     print "good"
elif 3 == 3:
     print "hrm"
else:
     print "okay..."
	
Iteration
for i in 1,2,3,15,6,3,5.4,'blah':
     print i
	
outputs:
1
2
3
15
6
3
5.4
blah
	  
Miscellaneous
Python also has break, continue and else keywords defined for loops, but I won't go into detail here.
Pass
The pass statement is the 'do nothing' statement, in Python you must use this statement if you want nothing to happen, you cannot use a blank line.
Functions
Functions are defined using the def keyword:
def function():
     print "the body of the function"
	

You do not define what a function returns, this allows functions to return different things for different situations. If a function doesn't explicitly return anything, it returns the special value 'None'.

Python supports a rich set of argument definition and usage:
Default values:
def function(argument="initial value"):
	    
means that if the caller of the function does not supply a value for argument, the value "initial value" is automatically inserted.
Positional and Named parameter passing:
If a function is defined as:
	   
def function(a, b):
	    
then it can be called in two ways with the same results:
  1. function("a value", "b value")
    		
  2. function(b="b value", a="a value")
    		
Arbitrary number of arguments:
Python has very good support for this, but I haven't covered the data structures needed to explain this, so...

Prev Contents Next
Clinton Roy