Control Flow Statements – For Loop in Python 3
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 are controlled by generating a sequence. In this module, we will learn about for loops in Python.
Become a professional Python Programmer with this complete Python Training in Singapore!
Following is the list of all topics that we will cover this module.
How to Use For Loop in Python 3
For loops in Python are used for sequential iterations for a certain number of times, that is, the length of the sequence. Iterations on the sequences in Python are called traversals.
Syntax of for loop in Python 3
Let us see the Python Syntax of For Loop with examples:
for a in sequence: body of for
The following flowchart explains the working of for loops in Python:
As depicted by the flowchart, the loop will continue to execute until the last item in the sequence is reached. The body of the for loop, like the body of the Python while loop, is indented from the rest of the code in the program.
Go for this in-depth job-oriented Python Training in Hyderabad now!
Let us take a look at the Python for loop example for better understanding.
square = 1 numbers_list = [1,2,3,4,5,6,7] for i in numbers_list: square = i*i print(“The square of”, i, “is”, square) Output: The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49
In the above example, we are calculating the squares of all elements present in the list of numbers that we have created, named ‘numbers_list’, using a Python for loop. The variable i iterates from the beginning to the end of the Python List, and when the program enters the body of the loop, the square of each element is calculated using the variable i and stored in a variable named ‘square’.
Now that we understood how to write for loop in Python let us go ahead and take a step further and learn what range function in for loop.
The range() Function In Python For Loop
We can specify a particular range using an inbuilt Python function, named range(), to iterate the loop a specified number of times through that range.
Note: The range here is not from 1 to 10 but from 0 to 9 (10 numbers).
Specifying start and stop points in the range() function:
Note: 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 Python for loops with the range() function:
We can simply use Python for loop with the range() function as shown in the example below.
For i in range(2,10): print(i) Output: 2 3 4 5 6 7 8 9
By default, the increment in the 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 the range() function, as illustrated in the following example:
For i in range(2,10,2) print(i) Output: 2 4 6 8
We have the perfect professional Python Course in Bangalore for you!
Loop Interruptions in Python For Loop
- 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.
# creating a list of numbers number_list = [2,3,4,5,6,7,8] for i in number_list: print(i) if i == 5: break Output: 2 3 4 5
- Continue Statement
Just as with while loops, the continue statement can also be used in Python for loops to terminate the ongoing iteration and transfer the control to the beginning of the loop to continue the next iteration.
number_list = [2,3,4,5,6,7,8] for i in number_list: if i == 5: continue print(i) Output: 2 3 4 6 7 8
Note that the fifth iteration was interrupted as we have used the continue statement when the value of i is 5. Therefore, the control was passed to the beginning of the loop, and the program started executing the 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 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.
for i in range(1,6): print(i) else: print(” All iterations completed”) Output: 1 2 3 4 5 All iterations completed
As the name suggests, nested loops are the loops that are nested inside an existing loop, that is, nested loops are the body of another loop.
for i in range(1,9,2): for j in range(i): print( i, end = ‘ ‘) print() Output: 1 3 3 3 5 5 5 5 5 7 7 7 7 7 7 7
Looking for Python for Data Science Course All-in-1 Combo Training? Enroll now!
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 the pattern. Note that we have also used the third parameter in the range() function and have set the increment to 2, that is why the number in every column is incremented by 2.
With this, we come to an end of this module in Python Tutorial. Now, if you want to know how you can use python for data science, you can go through this blog on Python for Data Science