Learn Python Tutorial - Loops



Python Basics

(Learn Python Programming in 20 Minutes)


 

3 Python commands



3.7 Loops 

Python provides two looping commands: for and while. These are compound commands.


3.7.1 for loop



The syntax of a for loop is

for item in l i s t : 
     a c t i o n

As usual, the action consists of one or more statements, all at the same indentation level. These statements are also known as the body of the loop. The item is a variable name, and list is a list.

Execution of the for loop works by setting the variable successively to each item in the list, and then executing the body each time. Here is a simple example (the comma at the end of the print makes all printing occur on the same line):

for i in [2, 4, 6, 0]: 

    print  i ,
 
This produces the output
2 4 6 0



3.7.2 while loop

The syntax of the while loop is

while c o n d i t i o n : 

     a c t i o n
 
Of course, the action may consist of one or more statements all at the same indentation level. The statements in the action are known as the body of the loop. Execution of the loop works as follows. First the condition is evaluated. If True, the body is executed and the condition evaluated again, and this repeats until the condition evaluates to False. Here is a simple example:


n = 0 

while n < 10: 
     print n, 
     n = n + 3

This produces the following output
0 3 6 9


Note that the body of a while loop is never executed if the condition evaluates to False the first time. Also, if the body does not change the subsequent evaluations of the condition, an infinite loop may occur. For example


while True: 

     print "Hello",

will print Hellos endlessly. To interrupt the execution of an infinite loop, use CTRL-C.



3.7.3 else in loops

A loop may have an optional else which is executed when the loop finishes. For example, the loop

for n in [10,9,8,7,6,5,4,3,2,1]: 

    print n, 
else: 
    print "blastoff"

results in the output
10 9 8 7 6 5 4 3 2 1 blastoff

and the loop


n=10 

while n > 0: 
     print n, 
     n = n - 1 
else: 
     print "blastoff"

has the same effect (it produces identical output).


3.7.4 break, continue, and pass

The break statement, like in C, breaks out of the smallest enclosing for or while loop. The continue statement, also borrowed from C, continues with the next iteration of the loop. The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. 
Here is an example of the use of a break statement and an else clause in a loop.

for n in range(2, 10): 

      for x in range(2, n): 
            if n % x == 0: 
                  print n, ’equals’, x, ’*’, n/x break 
      else: 
            # loop fell through without finding a factor 
            print n, ’is a prime number’

The above code searches for prime numbers between 2 and 10, and produces the following output.


2 is a prime number 

3 is a prime number 
4 equals 2 * 2 
5 is a prime number 
6 equals 2 * 3 
7 is a prime number 
8 equals 2 * 4 
9 equals 3 * 3
 

 


 

 

 

 

 

No comments:

Post a Comment

Your feedback is highly appreciated and will help us to improve our content.