In this session of the Python tutorial, you’ll learn in detail about for loop in Python 3. you’ll also know how to use the range() function for loops. You’ll even know how to use else statements with loop, nested for loop, and loop.
We’ll be covering the following topics in this tutorial:
For-Loop Control Flow Statements in Python 3
Python for Loop Statements is another control flow statement. The program’s control is always moved to the start of the for-loop to perform specific blocks of statements for a definite time, which control through an iterable expression. After a while, the condition becomes false, the ‘for’ loop suspends.
Though other languages include conditions and increment expression in the syntax of both for-loop, the iteration, and incrementing value commanded by creating a Python sequence, in this module, we will learn about for-loops in Python.
What’s iteration inside a Python for loop?
Repetitive implementation of the identical block of code over and over is known as iteration.
There are two types of iteration:
• Definite iteration: where the number of repetitions is given explicitly in advance.
• Indefinite iteration: where the code block executes until a condition fulfill.
In Python, indefinite iteration did use a while loop.
Use of For Loop in Python 3
For loops in Python are called iterators. Python for-loop can iterate over the sequences (such as string, array, list, tuples, dictionary) is called traversal.
Python for loop syntax
Let’s see the Python Syntax of for-loop with examples:
for val in sequence: statements(s)
If a sequence includes an expression list, it evaluates first. Then, the very first item in the series will assign to the iterating variable Val. Then, the statements block executed. Each item in the list assigned to Val, and the statement(s) block executed until the whole sequence using up.
The below flowchart demonstrates the working of for loop:
According to the flowchart, the loop will continue to do before the previous item in the order is reached. The whole body of the for loop, like the entire body of this Python while-loop, is indented from the rest of the code from the program.
Let’s take a look at Python for-loop examples for better understanding.
Sq_root = 1 count_list = [1,2,3,4,5,6,7] for val in count_list: Sq_root = val*val print("The Square of", val, "is", Sq_root) 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
The range() Function In Python For Loop
Python range() function: We can specify the sequence of numbers within a given range, starting from 0 and increments by 1.
Note: The range is not out of 1 to 100 but from 0 to 99 (100 numbers). Specifying Start and stop points from the range() function:
Note: In this example, the Beginning point is 1 and Also the End point is 5.
So, the list will be created from 1 to 4 (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 Val in range(10,15): print(Val) Output: 10 11 12 13 14
By default, the increment from the range() function when used with loops set to 1; Nonetheless, we Could Alter or Define a Specific increment by Adding the third parameter from the range() function, as illustrated in the next example:
For i in range(10,20,2) print(i) Output: 10 12 14 16 18
The break and continue Statements
Break and continue work precisely the identical manner with for loops as with while loops. Break terminates the loop entirely and proceeds into the first statement after the loop:
Like while loops, for loops, may also be terminated with the break statement, the break will immediately terminate the execution of this loop and then move the control of the program towards the end of the loop.
# creating a list of numbers count_list = [2,3,4,5,6,7,8] for Val in count_list: print(Val) if Val == 6: break Output:2 3 4 5 6
Like while loops, continue statement can be used in Python for loops to complete the ongoing iteration and transfer the control to the start of the loop to keep another iteration.
count_list = [2,3,4,5,6,7,8] for Val in count_list: if Val == 5: continue print(Val) Output: 2 3 4 6 7 8
Note that the fifth iteration was interrupted as we’ve used the continue statement when the value of Val is 5. Consequently, the control passed to the start of the loop, and the program began executing the sixth iteration and did not print the value of Val when it was 5.
Else in Python For Loop
For loop in Python can have an optional else block. The else block will be executed when all iterations completed. When the break used for loop to terminate the loop before all the iterations completed, the else block ignored.
for Val in range(1,6): print(Val) else: print("All iterations complete") Output:1 2 3 4 5
All iterations completed
Nested For Loop in Python
The nested loops would be the loops that nested inside an existing loop; that is, nested loops would be the body of another loop.
for V1 in range(1,9,2): for V2 in range(V1): print( V1, end = ' ') print() Output: 1 3 3 3 5 5 5 5 5 7 7 7 7 7 7 7
From the preceding example, we’ve used nested loops to publish some pyramid design. The outer loop utilizes the number of rows from the design, and the inner loop and the nested loop used to control the number of columns from the design. Note that we used the third parameter in range() function and set the increment to 2. That’s why the number in each column is incremented by 2.