Programming:Python Flow control - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:Python Flow control
 

Programming:Python Flow control

Previous: Operators Index Next: Functions

As with most imperative languages, there are three main categories of program flow control:

  • loops
  • branches
  • function calls

Function calls are covered in a later section.

Generators might arguably be considered an advanced form of program flow control, but they are not covered here.

Table of contents

Loops

In Python, there are two kinds of loops, 'for' loops and 'while' loops.

For loops

A for loop iterates over elements of a sequence (tuple or list). A variable is created to represent the object in the sequence. For example,

l = [1,2,3,4,5]
for i in l:
    print i

This will output

1
2
3
4
5

The for loop loops over each of the elements of a list or iterator, assigning the current element to the variable name given. In the first example above, each of the elements in l is assigned to i.

A builtin function called range exists to make creating sequential lists such as the one above easier. The loop above is equivalent to either:

l = range(1,6)
for i in l:
    print i

or

for i in range(10,0,-1):
    print i

This will output

10 
9
8
7
6
5
4
3
2
1

or

for i in range(10,0,-2):
    print i

This will output

10 
8
6
4
2

or

for i in range(10,0,-1):
    print i,

This will output

10 9 8 7 6 5 4 3 2 1

or

for i in range(1,6):
    print i

for loops can have names for each element of a tuple, if it loops over a sequence of tuples. For instance

l = [(1,1), (2,4), (3,9), (4,16), (5,25)]
for x,xsquared in l:
    print x,':',xsquared

will output

1 : 1
2 : 4
3 : 9
4 : 16
5 : 25

While loops

A while loop repeats a sequence of statements until some condition becomes false. For example:

x = 5
while x > 0:
    print x
    x = x - 1

will output

5
4
3
2
1

Python's while loops can also have an 'else' clause, which is a block of statements that is executed (once) when the statement starts out false. For example:

x = 5
y = x
while y > 0:
    print y
    y = y - 1
else:
    print x

this will output

5
4
3
2
1
5

Unlike some languages, there is no postcondition loop.

Breaking, continuing and the else clause of loops

Python includes statements to exit a loop (either a for loop or a while loop) prematurely. To exit a loop, use the break statement

x = 5
while x > 0:
   print x
   break
   x = x - 1
   print x

this will output

5

The statement to begin the next iteration of the loop without waiting for the end of the current loop is 'continue'.

l = [5,6,7]
for x in l:
    continue
    print x

This will not produce any output.

The else clause of loops will be executed if no break statements are met in the loop.

l = range(1,100)
for x in l:
    if x == 100: 
        print x
        break
else:
    print "100 not found in range"

Branches

There is basically only one kind of branch in Python, the 'if' statement. The simplest form of the if statement simple executes a block of code only if a given predicate is true, and skips over it if the predicate is false

For instance,

>>> x = 10
>>> if x > 0:
...    print "Positive"
... 
Positive
>>> if x < 0:
...    print "Negative"
... 

You can also add "elif" (short for "else if") branches onto the if statement. If the predicate on the first “if” is false, it will test the predicate on the first elif, and run that branch if it’s true. If the first elif is false, it tries the second one, and so on. Note, however, that it will stop checking branches as soon as it finds a true predicate, and skip the rest of the if statement. You can also end your if statements with an "else" branch. If none of the other branches are executed, then python will run this branch.

>>> x = -6
>>> if x > 0:
...    print "Positive"
... elif x == 0:
...    print "Zero"
... else:
...    print "Negative"
...
'Negative'

Conclusion

Any of these loops, branches, and function calls can be nested in any way desired. A loop can loop over a loop, a branch can branch again, and a function can call other functions, or even call itself.

Previous: Operators Index Next: Functions


Also helps finding: ProgrammingPythonFlowcontrol, ProgrammingPython, PythonFlow, Flowcontrol, programing, clow, controll, programmin, floe, contro, progamming, fow, contol, prgramming, flw

   
 
  
Add to bookmarks
Related Articles
 
Programming:Python control
Programming:Python Functions
Programming:Python Operators
Programming:Python
Computer programming
Top Articles
 
Ada Programming/Keywords/end
Ada Programming/Keywords/package
Ada Programming/Keywords/type
Ada Programming/Keywords/use
Cookbook:Dessert
Cookbook:Onion
Cookbook:Recipes
Cookbook:Salt
Cookbook:Vegetable
Final Fantasy
GAT: A Glossary of Astronomical Terms
General Biology
Puzzles
SA CUR Info
SA NCS:Agricultural Science
SA NCS:Consumer Studies
SA NCS:First Additional Language
SA NCS:History
SA NCS:Mathematics
SA NCS:Tourism
Wikiversity
Search LiveJournal blogs for Programming:Python Flow control
 

Capital One Credit Card  •  Valentine's Day Gifts  •  Credit Consolidation  •  Insurance Quotes •  Credit Consolidation

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.