8  Loops

Loops are one of the most powerful and basic concepts in programming. A loop can contain a set of statements that keeps on executing until a specific condition is reached.

Programming languages provide us the concept of loops which helps us in executing some task n number of times where n can be any whole number. They are pretty useful and can be applied to various use cases.

There are two types of loops in Python.

8.1 The “for” loop

For loop iterate over a given sequence (range, list, tuple, string etc.) or other iterable objects. Iterating over a sequence is called traversal. Flow chart of for loop is:

for-loop
Figure 8.1: ?(caption)

8.2 Looping through String

Even strings are iterable objects, they contain a sequence of characters:

s
t
r
i
n
g

8.3 Looping through a List

ABL
MCB
NBP

8.4 How to Use Index

0 ABL
1 MCB
2 NBP

8.5 Use of Enumerate for Indexing

0 red
1 yellow
2 green
3 purple
4 blue

We can change the elements in a list:

Before square  0 is red
After square  0 is white
Before square  1 is yellow
After square  1 is white
Before square  2 is green
After square  2 is white
Before square  3 is purple
After square  3 is white
Before square  4 is blue
After square  4 is white

Quick Quiz: Print the following pattern using for loop.

+++

++

+++++

+++

+++++++++

+++
++
+++++
+++
+++++++++

8.6 Iterating over tuples

We can use for loops to iterate on a tuple. The for loop will keep on iterating until the elements in the tuples are exhausted.

SBP
NIBAF
BSC

8.7 The range ( ) function

When using for loops in Python, the range() function is pretty useful to specify the number of times the loop is executed. It yields a sequence of numbers within a specified range.

syntax: range (start, stop, steps)

  • The first argument is the starting value. It is zero by default.

  • The second argument is the ending value of the range.

  • The third argument is the number of steps to take after each yield.

8.7.1 Iterating over range object

2
5
8

8.7.2 an other for loop with else

S
B
P
loop ended

8.8 Nested Loop

A nested loop is a loop inside a loop. The “inner loop” will be executed once for each iteration of the “outer loop” i.e., in each iteration of the outer loop, inner loop execute all its iteration. For each iteration of an outer loop, the inner loop re-start and completes its execution before the outer loop can continue to its next iteration.

(Rizwan, JD)
(Rizwan, OG-4)
(Rizwan, SBP)
(Adil, JD)
(Adil, OG-4)
(Adil, SBP)
(Ahmad, JD)
(Ahmad, OG-4)
(Ahmad, SBP)

Quick Quiz: Create Co-ordinate (0,0) ,(0,1) ,(0,2) , (1,0) ,(1,1) ,(1,2) ,(2,0) ,(2,1) ,(2,2) ,(3,0) ,(3,1) ,(3,2)

(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
(2,0)
(2,1)
(2,2)
(3,0)
(3,1)
(3,2)

8.9 The “while” loop

The while loop in Python executes a block of code until the specified condition becomes False.

Flow chart of while loop is :

while Loop Flow chart
Figure 8.2: ?(caption)
0
2
4
6
8

In the example, the while statement checks if count is less than 10.

Initially, count is zero so the statement is true and it executes the body of while. Then the count gets incremented by 2. Again we check the condition and this goes on till the condition becomes false.

Here, when our code checks 10<10, the statement returns False and so the code in while block is not executed.

8.9.1 Example

1982
1980
It took  2 repetitions to get out of loop.

8.10 Infinite loop

A loop is called an infinite loop when the loop will never reach its end.

Usually, when a condition is always True in a while loop, the loop will become an infinite loop. So we should be careful when writing conditions and while updating variables used in the loop.

In Python shell, we can stop/terminate the program on an infinite loop by using CTRL + C. In jupyter notebook press i twice.

Quick Quiz: Write a program to print 1 to 50 using a while loop.

Quick Quiz: Write a program to print the contents of a list=['C', 'C++', 'java', 'fortran','python'] using while loop.

8.11 Loop control statements in Python

1
2
3
4

In this loop, we are incrementing the value of num variable and then printing it. When the num value becomes 5 the break statement is executed which terminates the loop and therefore loop is not executed further.

1
2
3
4
6
7
8
9
10

Here, we see that when the num variable is equal to 5, the continue statement is executed. It then doesn’t execute the lines after the continue statement and the control is sent to the next iteration.

8.12 Common Mistakes

  1. Mostly people forgte to initialize the variable.

  2. There is second issue, we might face if we forget to initialize the variables with the right value. we might have already used the variable in our program. if we reuse a variable without setting the correct value from the start, it will still have the value from before.

45

Whenever you are writing a loop, check that you are initializing all the variables you want to use before you use them.

362880

8.13 Exercise

  1. A list of groceries is given below, print all items with its index.

    groceries = ["bananas","butter","cheese","toothpaste"]
  2. Make a combination of all the letters of ‘SBP’ and ‘NIBAF’

  3. Write a program to say “hello” to all persons in a list which starts with M. List = ['Ahmad', 'Muhammad', 'Essa', 'Mossa']

  4. Print odd numbers in first 10 numbers using while loop.

  5. Also find the sum of all numbers in above question.

  6. Write a program to print the multiplication table of a number entered by the user, using ‘for’ loop.

  7. Attempt above problem using while loop.

  8. Find the total of [10,20,30] using for loop.

  9. Write a program to guess a secrete number in 3 attempts using while loop.

  10. Solve above problem while generating secrete number randomly.

  11. Provide a hint to make problem easy, if guess is larger, then print ‘its larger’, otherwise ‘its smaller’

  12. Write a program whether a given number is prime or not.

  13. Find the prime numbers in a given range.

  14. store the numbers obtained in above question as a list.

  15. Find the sum of all the numbers in the list above in question 14.

  16. Challenge: calculate the time required to run the above code 500 times.

  17. Draw the following pattern using nested loop.

  18. Make the co-ordinates (0,0,0) …(2,2,2) using nested for loop.

  19. Write a for loop the prints out all the element between -5 and 5 using the range function.

  20. Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings. If the score is less than 6, exit the loop. The list PlayListRatings is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]

  21. Write a program to sum first n natural numbers, take input from user.

  22. Write program to find the factorial of a number n.

  23. Write a program to print the following pattern.

    *
    * *
    * * *
    * * * *
    * * * * *
  24. Write a Python program to concatenate all elements in a list into a string and return it

  25. Write a nested for loop program to print multiplication table in Python from 2 to 10.

  26. Write a Python program to print all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence.

  27. Write a program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included) and print the result in the form of a list.