OUTDATED, DO NOT USE: This tutorial was written in 1999 and was based on Python 1.5.2, which today is a really old version. I would not recommend using this tutorial to learn modern Python as the language has changed significantly. This page is left here in it's mostly-original form for historic reference only. Please consult the Python website for current documentation.

if-then conditionals

  • The interior block must be indented. The condition expression must end with a colon (:),
    if age > 21 and country == "US":
       print "You are eligble"
       apply_for_credit()
    
  • You can provide an optional else clause and any number of elif conditions. Note that each condition must end with a colon.
    if shape in ['circle', 'elipse']:
       print 'it is round'
    elif shape in ['line','arc']:
       print 'it is hollow'
    elif shape in ['square','triangle']:
       print 'it has corners'
    else:
       print "I don't know about the %s shape" % shape
    
  • Using multiple elif conditions is the typical way you simulate C's switch statement.
  • "If" statements can arbitrarily nest. The indentation level is used to match "else" clauses to the "if" statement.

for loops

  • For loops iterate over each element of a sequence in order.
    for name in ['John','Mike','Amy']:
        print 'Hello' + name
    
  • You can escape out of a for loop early with the break statement, or cause the loop to immediately start again at the next value with a continue statement.
    for item_number in inventory:
       if item_number < 0:
          break      # stop the loop early
       elif item_number == 0:
          continue   # skip this item
    
  • An optional "else" clause may be added. Its body will be executed only if the loop was not exited with a break statement
    for name in store:
       if name == "baked beans":
          break
    else:
       print "No baked beans were found"
    
  • The iteration variable ("name" in above example) is still available for examination after the loop finishes.
  • It is common to use the builtin functions range or xrange to loop over a number sequence. Comparing python and C for loops,
    PythonC/C++
    for i in range(10):
       print i
    
    int i;
    for( i=0; i<10; ++i ) {
       printf("%d\n",i)
    }
    
    for i in range(200,100,-5):
       print i
    
    int i;
    for( i=200; i>100; i-=5 ) {
       printf("%d\n",i)
    }
    

while loops

  • while loops are like for loops only there is no sequence, only a conditional test. The body of the loop is executed as long as the test is true.
    while cash > 0:
        thing = find_random_item()
        thing.purchase()
        cash = cash - thing.cost()
    
  • Like the for loop, an optional else clause may be used. It is only executed if the while loop was not terminated with a break statement.
    while len(filename) > 0:
       filename = get_next_file()
       if filename == "virus.exe":
          break
    else:
       print "Whew, virus not found"
    

The pass statement

Occassionally you may need to write a statement which should do nothing. The pass statement does just this. It is most useful when dealing with object-oriented programs using classes, but it can be used anyplace.

if i > 0:
   pass
else:
   print "i is zero"