For loops

A for loop repeats a block of instructions, usually a specific number of times.

This example takes each item in little_list one at a time and prints it.

>>>

little list = ['the', 'quick', 'brown',

'fox']

>>>

for the item in little list:

print the item, "*",

the

* quick * brown * fox *

Check out Chapter 10 for the lowdown on for loops. While loops

A while loop repeats an instruction as long as a particular condition is true.

The following example code prints a number as long as the countdown value is greater than zero. It also subtracts 1 from the countdown value each time around the loop.

countdown = 10 while countdown:

print countdown, countdown -= 1 print "blastoff!"

Here's what it prints:

Was this article helpful?

0 0

Post a comment