For loops in Python, just like any other language are used to repeat a block of code for a fixed number of times. For loop is yet another control flow statement since the control of the program is continuously transferred to the beginning of the for loop to execute the body of for loop for a fixed number of times. While other languages contain conditions and increment expression in the syntax of for loop, in python, the iteration and incrementing value is controlled by generating a sequence. In this module, we will learn about the for loops in Python. Following is the list of all the topics that we will cover this module, in case you need to jump to a specific one.
Learn Python in 16 hrs from experts
For loops in Python are used for sequential iterations for a certain number of times, that is, the length of the sequence. Iterations on sequences in Python is called traversal.
Syntax of the for loop in Python:
The following flowchart explains the working of for loop in Python.
As depicted by the flowchart, the loop will continue until the last item in the sequence is reached. The body of the for loop, like the body of the while loop, is indented from the rest of the code in the program.
In the above example, we are calculating the squares of all the elements present in the list of numbers that we have created, named numbers_list, using a for loop. The variable i iterates from the beginning to the end of the list and when the program enters the body of the loop, square of each element is calculated using the variable i and stored in a variable named square.
In python, we can specify a particular range using an inbuilt function named range(), to iterate the loop a specified number of times through that range.
Note that the range here does not start from 1 to 10, the range starts from 0 to 9 (10 numbers).
Specifying the start and stop points in range() function.
Here in this example, the starting point is 3 and the ending point is 7, So the list will be generated from 3 to 6 ( 4 numbers).
Using for loops with range() function
We can simply use for loop with range() function as shown in the example below
By default the increment in range() function when used with loops is set to 1, however we can change or specify a particular increment by including a third parameter in range() function, as illustrated in the following example:
1. Break statement
Just as in while loops, for loops can also be prematurely terminated using the break statement. The break statement will immediately terminate the execution of the loop and transfer the control of the program to the end of the loop.
2. Continue Statement
Just as in while loop, continue statement can also be used with for loops to terminate the ongoing iteration and transfer the control to the beginning of the loop to continue the next iteration.
Note that the fifth iteration was interrupted as we have used continue statement when the value of i is 5. Therefore, the control was passed to the beginning of the loop and program starting executing sixth iteration and did not print the value of i when it was 5.
For loop in Python can have an optional else block. The else block will be executed only when all the iterations are completed. When break is used in for loop to terminate the loop before all the iterations are completed, the else block is ignored.
As the name suggests, Nested loops are the loops that are nested inside an existing loop. That means that the nested loops are the body of another loop.
In the above example, we have used nested loops to print a number pyramid pattern. The outer loop is used to handle the number of rows in the pattern and the inner loop, or the nested loop is used to handle the number of columns in pattern. Note that we have also used the third parameter in range() function and set the increment to two, that is why the number in every column in incremented by two.
With this we come to an end of this module, the next module will highlight Python functions. See you there!
Download Interview Questions asked by top MNCs in 2019?