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. How to Program, Part VI
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Dividing Work
  13. More MPI
  14. Poogle - Web Search
  15. Mandelbrot Sets
  16. Mandelbrot, The Code
  17. Mandelbrot, The Images
  18. Mandelbrot In CUDA
  19. Conway's Life, Part I
  20. Life Code Listing
  21. Conway's Life, Part II
  22. 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:

count.py
1for 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 0 to 3. You could make it start at 1 if you wanted, all you'd have to do is this:

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

or this:

count3.py
1for i in range(1,5):
2    print i
$ python ./count3.py
1
2
3
4

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

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

addup.py
1s = 0
2for i in range(4):
3    s = s + i
4print "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.

naddup.py
1for i in range(4):
2    s = s + i
3print "sum=",s
$ python ./naddup.py
Traceback (most recent call last):
  File "./naddup.py", line 2, in <module>
    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 is inside the loop and what is not.

spacing.py
1print "before loop"
2for i in range(4):
3    print i
4    print "in loop"
5print "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:

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

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

if2.py
1x = 1
2if 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:
if3.py
1x = 5
2if x < 3:
3    print "x is less than 3"
4else:
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:

if4.py
1x = 7
2if x == 6:
3    print "x is six"
4elif x <= 7:
5    print "x is less than or equal to seven"
6elif x >= 3:
7    print "x is greater than or equal to three"
8else:
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 can produce a number of different outputs depending on what value is assigned 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:

mix.py
1for i in range(1,5):
2     if i == 1:
3        print "I see",i,"duck."
4     elif i % 2 == 0: # The % means modulus, or remainder
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. Note that "i%2" means the remainder after dividing i by 2. If the remainder after dividing by two is zero, i must be an even number. The above program gets really excited if it sees an even number of them!

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

pass.py
1for 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.

or.py
1for 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

Up until now, the loop variable for all the loops we've looked at increased by 1 at each step. It's possible to ask it to increase by two, three, (or even by -1).

or2.py
1for i in range(0,16,2):
2    if i <= 4 or i >= 9:
3        pass
4    else:
5        print "i=",i
$ python ./or2.py
i= 6
i= 8

Range starts with the first number, goes two the second number, and increments by the third number. Thus, the above loop starts with 0, stops before 16, and counts by 2.

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

wh.py
1i=0
2while 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.


Problems

  1. What is wrong with this program?
    wrong.py
    1s = 0
    2for i in range(4):
    3s = s + i
    4print s
  2. What is wrong with this program?
    wrong2.py
    1s = 0
    2for i in range(4)
    3  s = s + i
    4print s
  3. What is wrong with this program?
    wrong3.py
    1s = 0
    2For i in Range(4)
    3  s = s + i
    4print s
  4. What does this program do? Reason it out, don't just run it.
    sumit.py
    1s = 0
    2for i in range(4):
    3  s = s + 2*i
    4print s
  5. What does this program do? Reason it out, don't just run it.
    sumit2.py
    1s = 0
    2for i in range(0,10,3):
    3  s = s + i
    4print s
  6. What does this program do? Reason it out, don't just run it.
    sumit3.py
    1s = 0
    2for i in range(0,14,3):
    3  if i % 2 == 0:
    4    s = s + i
    5print s
  7. What does this program do? Reason it out, don't just run it.
    sumit4.py
    1s = 0
    2for i in range(0,4):
    3  if i % 2 == 0:
    4    s = s + i
    5  s += 1
    6print s
  8. What does this program do? Reason it out, don't just run it.
    sumit5.py
    1s = 0
    2for i in range(0,4):
    3  if i % 2 == 0:
    4    s = s + i
    5    s += 1
    6print s
  9. What does this program do? Reason it out, don't just run it.
    sumit6.py
    1s = 0
    2for i in range(0,4):
    3  if i % 2 == 0:
    4    s = s + i
    5s += 1
    6print s
  10. Write a program to take the sum of all integers from 2 to 17.
  11. Write a program to take the sum of all even integers from 2 to 17.
  12. Write a program to take the sum of all integers from 2 to 17 excluding 13.