1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. exercises
  7. pyMPI tutorial
  8. Calculating PI, Part I
  9. Calculating PI, Part II
  10. Calculating PI, Part III
  11. Poogle - Web Search
  12. Mandelbrot Sets
  13. Mandelbrot, The Code
  14. Mandelbrot, The Images
  15. Conway's Life, Part I
  16. Life Code Listing
  17. Conway's Life, Part II
  18. MPI Life Code Listing

How to Program, Part II

Normally python executes each line of your program in sequence, from the first line to the last line. Sometimes this is not flexible enough, and for this reason we introduce "control flow."

Control flow allows us to cause the program to do something conditionally, or to do it multiple times.

Here is how we make python count:

file: count.py
1 for i in range(4):
2     print i
> python count.py
0
1
2
3

This is called a "for loop" because the program loops over the same piece of code (line 2). The print statement executes 4 times, taking on each of the values from zero to 3. You could make it start at 1 if you wanted, all you'd have to do is this:

file: count2.py
1 for i in range(0,4):
2     print 1+i
> python count2.py
1
2
3
4

or this:

file: count3.py
1 for i in range(1,4):
2     print i
> python count3.py
1
2
3

Question: How do you make Python count by 5's?

What if we want to make python add the numbers up as it counts?

file: addup.py
1 s = 0
2 for i in range(4):
3     s = s + i
4 print "sum=",s
> python addup.py
sum= 6

Something important to notice here is what happened on line 1. Here we "create" or "define" the variable s. Without this line, we would not be able to add to the variable s later.

file: naddup.py
1 for i in range(4):
2     s = s + i
3 print "sum=",s
> python naddup.py
Traceback (most recent call last):
  File "naddup.py", line 2, in ?
    s = s + i
NameError: name 's' is not defined

Note that there are spaces in front of the print statement in the programs above. This is important. The spaces tell python that the print command is inside the for loop. Here is another program with more print statements. This should clarify what isinside the loop and what is not.

file: spacing.py
1 print "before loop"
2 for i in range(4):
3     print i
4     print "in loop"
5 print "after loop"
> python spacing.py
before loop
0
in loop
1
in loop
2
in loop
3
in loop
after loop

When you run this program you will see that the "in loop" message gets printed several times, but the "before loop" and "after loop" each get printed only once.

Another kind of control flow is the "if" statement. It works like this:

file: if.py
1 x = 5
2 if x < 3:
3    print "x is less than 3"
> python if.py

This program will do nothing at all. With a slight change:

file: if2.py
1 x = 1
2 if x < 3:
3     print "x is less than 3"
> python if2.py
x is less than 3
Alternatively, we could have created some output by doing this:
file: if3.py
1 x = 5
2 if x < 3:
3     print "x is less than 3"
4 else:
5     print "x is more than 2"
> python if3.py
x is more than 2

Now it will print out "x is more than 2".

But there is yet another thing we can do:

file: if4.py
1 x = 7
2 if x == 6:
3     print "x is six"
4 elif x <= 7:
5     print "x is less than or equal to seven"
6 elif x >= 3:
7     print "x is greater than or equal to three"
8 else:
9     print "x is",x # can never happen!
> python if4.py
x is less than or equal to seven

In the above code "elif" is a kind of short-hand for "else if". If the initial "if" is false, then the program checks the first "elif". If that is true, it finishes evaluating the sequence of if/elif's. If the result of the first elif is false, the program proceeds to the next elif, and so on. If all of the elif's fail, the "else" is executed.

This program will produce a number of different outputs depending on what value is assinged to x on line 1. If x is 6, then it prints out "x is six". If x is 2, it prints out "x is less than or equal to three". If x is 5, it prints out "x is 5".

You can combine these control flow structures in arbitrary ways. Consider this program:

file: mix.py
1 for i in range(1,5):
2      if i == 1:
3         print "I see",i,"duck."
4      elif i==4:
5         print "I see",i,"ducks!"
6      else:
7         print "I see",i,"ducks."
> python mix.py
I see 1 duck.
I see 2 ducks.
I see 3 ducks.
I see 4 ducks!

This program enumerates the number of ducks, and uses the correct ending in each case. It gets really excited if it sees four of them!

Something that may be helpful here, is that you can use "pass" to have no action take place.

file: pass.py
1 for i in range(5):
2     if i >= 2 and i <= 3:
3         pass
4     else:
5         print "i=",i
> python pass.py
i= 0
i= 1
i= 4

Here I've thrown another curve your way. Notice the "and" between the two conditions "i >= 2" and "i <= 3". This if block is only executed if both conditions are true. Alternatively, I could use "or" which will work if either condition is true.

file: or.py
1 for i in range(8):
2     if i <= 2 or i >= 5:
3         pass
4     else:
5         print "i=",i
> python or.py
i= 3
i= 4

Another kind of loop is the while loop. You can count using the while loop, but it requires a bit more typing.

file: wh.py
1 i=0
2 while i<4:
3     print i
4     i = i + 1
> python wh.py
0
1
2
3

This produces the exact same result as count.py above.