Python statements are executed sequentially in any code, however, sometimes we wish to iterate a particular piece of code repeatedly. To attain this we use, Loops in Python. The Loops in Python will allow us to execute a group of statements repetitively.
Loops are always accompanied by a condition, with the help of this condition we run the loop whenever the condition is true and break the loop or come out the loop whenever the condition is false. This acts as an important component in designing the loops according to our purpose.
The flow chart of Loops in Python is shown below:
The main motive of this condition is to bring a definitive end to our loop, i.e. to come out the loop after repeating n number of times. If the condition is always true, we end up with an infinite loop that goes on running continuously.
Types of Loops in Python
We have two primitive types of Loops in Python:
- For loop
- While loop
Let’s look into each type.
The For loop is used to iterate a particular block of code several times. It is mainly used for traversing over sequences or data structures like list, tuple, dictionary, etc.
for iterating_var in sequence:
Here, iterating_var is the iterating variable that selects the first item in the sequence, executes the given statements in the loop, then moves on to the next item in the sequence. This goes on until the last item is selected and the statements of the loop are executed. Thereafter, we come out of the loop and the remaining statements of the code are executed.
It is better to go with for loop when we know the number of iterations in advance.
Let’s have a look at a few examples.
#Example of for loop num = range(10) for i in num: print(i) print("Out of the loop")
Here, we have used the range() function which gives us the sequence of numbers starting from 0 by default. The argument 10 in the function means that we want 10 numbers as output and therefore we get values from 0 to 9.
Therefore, with the help of this range()function we created a sequence named num and our iterating variable here is i.
colors = ['red','blue','green','yellow','white','black'] #A list of colors for col in colors: print(col)
Here, we can see that we come out of the loop after the list is exhaustive.
The While loop in Python is used to execute a group of statements within the loop until the given condition is satisfied. When the condition becomes false, we come out of the while loop and the rest of the lines in the code are executed.
It is adequate to use while loop when we are not aware of the number of iterations in advance.
Let’s have a look at a an example.
#Example of while loop n = 7 while n>0: print(n) n = n-1 print("Out of the loop")
Therefore, here we can see that we came out of the loop when the condition of n>0 was not satisfied.
There are no predefined rules stating that a particular situation can be addressed by for loop or by while loop only.
Suppose we wish to find the sum of first n natural numbers. Let’s see how we do it using for loop and while loop both.
Sum of first 10 numbers is 55
Sum of first 10 numbers is 55
Here, we can see that the output that we receive is the same but we need to make small number of changes according to the requirements and format of each loop.
Thus, loops provide us the advantage of code re-usability which enables us to execute the same set of statements as many times as we want.
This article is shared and published by Jinal Shah.