Python Chapter#5: Loops

Ankur Kulhari

To execute statement/s multiple times, loops are used. Python loops:

  • Python While Loop
  • Python For Loop
  • Python Nested Loops
While Loop
  • Condition is tested before executing the loop body.
  • The condition may be any expression, and any non-zero value is considered as true
  • The loop iterates while the condition is true
  • When the condition becomes false, program control passes to the line immediately next to the loop
Syntax
while expression:
   statement/s

Ex:

counter = 5
while (counter > 0):
   print("The counter value is: ", counter)
   counter = counter - 1
print("Tata")

OUTPUT:

The counter value is:  5
The counter value is:  4
The counter value is:  3
The counter value is:  2
The counter value is:  1
Tata

In the above program the condition is true until counter value becomes 1, so it enters into the loop body until counter value is > 0. Once counter=0 it executes statement next to the loop body i.e. print(“Tata”).

For Loop

Executes statement/s for fixed number of times.

  • If a sequence contains an expression list, it is evaluated first
  • Then, the first item in the sequence is assigned to the iterating variable (iter_var: please refer Syntax below)
  • Then, the statement/s are executed.
  • Each item in the list is assigned to iterating variable (iter_var), and the statement/s are executed until the entire sequence is exhausted
Syntax
for iter_var in sequence:
   statements/s

Here, sequence can be:

  • String
  • List
  • List of List
  • Range

Ex:

print("From String as sequence Loop")
for character in "Python":     # String as sequence
   print("Current character in string is: ", character)

list1 = ["Jonny", "Bablu", "Raju"]
print("From List as sequence Loop")
for boy in list1:             # List as sequence
   print(" Current list item is: ", boy)

list2 = ["Jonny", "Bablu", "Raju"]
print("From Iterating by sequence index")
for index in range(len(list2)):            # Iterating by sequence index
   print(" Current list item is: ", list2[index])

list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("From List of Lists as sequence Loop")
for list1 in list_of_lists:    # List of Lists as sequence
    for item1 in list1:        # Nested for loop
        print(item1)
         
print("From range as sequence Loop")
for num in range(0, 3):          # range as sequence
    print("Hello world %d", num)

print("Tata")

OUTPUT:

From String as sequence Loop
Current character in string is:  P
Current character in string is:  y
Current character in string is:  t
Current character in string is:  h
Current character in string is:  o
Current character in string is:  n
From List as sequence Loop
Current list item is:  Jonny
Current list item is:  Bablu
Current list item is:  Raju
From Iterating by sequence index
Current list item is:  Jonny
Current list item is:  Bablu
Current list item is:  Raju
From List of Lists as sequence Loop
1
2
3
4
5
6
7
8
9
From range as sequence Loop
Hello world %d 0
Hello world %d 1
Hello world %d 2
Tata
Nested Loops

Python allows to use nested loops. One or more loop can be used inside any another while or for loop.

  • Nested For Loop
    Syntax:

    for iter_var in sequence:
       for iter_var in sequence:
          statement/s
       statement/s
    

    Ex:

    list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
    for list1 in list_of_lists:    
        for item1 in list1:        # Nested for loop
            print(item1)
    
  • Nested While Loop
    Syntax:

    while expression:
       while expression:
          statement/s
       statement/s
    
  • Nested For and While Loop
    Syntax:

    while expression:
          statement/s
          for iter_var in sequence:
             statement/s
          statement/s
    statement/s
    
  • Nested While and For Loop
    Syntax:

    for iter_var in sequence:
         statement/s
         while expression:
            statement/s
         statement/s 
    statement/s
    

What do you think about the article?

This site uses Akismet to reduce spam. Learn how your comment data is processed.