The for loop in Python is used to iterate the statements or a part of the application countless times. It is frequently used to traverse the records buildings like list, tuple, or dictionary.
The syntax of for loop in python is given below.
for iterating_var in sequence: statement(s)
The for loop flowchart
For loop Using Sequence
Example-1: Iterating string using for loop
str = "Python" for i in str: print(i)
P y t h o n
Example- 2: Program to print the table of the given quantity .
list = [1,2,3,4,5,6,7,8,9,10] n = 5 for i in list: c = n*i print(c)
5 10 15 20 25 30 35 40 45 50s
Example-4: Program to print the sum of the given list.
list = [10,30,23,43,65,12] sum = 0 for i in list: sum = sum+i print("The sum is:",sum)
The sum is: 183
For loop Using range() function
The range() function
The range() feature is used to generate the sequence of the numbers. If we ignore the range(10), it will generate the numbers from zero to 9. The syntax of the range() function is given below.
The start represents the beginning of the iteration.
The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
Consider the following examples:
Example-1: Program to print numbers in sequence.
for i in range(10): print(i,end = ' ')
0 1 2 3 4 5 6 7 8 9
Example – 2: Program to print table of given number.
n = int(input("Enter the number ")) for i in range(1,11): c = n*i print(n,"*",i,"=",c)
Enter the number 10 10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100
Example-3: Program to print even quantity using step size in range().
n = int(input("Enter the number ")) for i in range(2,n,2): print(i)
Enter the number 20 2 4 6 8 10 12 14 16 18
We can additionally use the range() feature with sequence of numbers. The len() function is blended with range() characteristic which iterate via a sequence the use of indexing. Consider the following example.
list = ['Peter','Joseph','Ricky','Devansh'] for i in range(len(list)): print("Hello",list[i])
Hello Peter Hello Joseph Hello Ricky Hello Devansh
Nested for loop in python
Python permits us to nest any number of for loops inside a for loop. The inner loop is achieved n number of instances for every new release of the outer loop. The syntax is given below.
for iterating_var1 in sequence: #outer loop for iterating_var2 in sequence: #inner loop #block of statements #Other statements
Example- 1: Nested for loop
# User input for number of rows rows = int(input("Enter the rows:")) # Outer loop will print number of rows for i in range(0,rows+1): # Inner loop will print number of Astrisk for j in range(i): print("*",end = '') print()
Enter the rows:5 * ** *** **** *****
Example-2: Program to number pyramid.
rows = int(input("Enter the rows")) for i in range(0,rows+1): for j in range(i): print(i,end = '') print()
1 22 333 4444 55555
Using else statement with for loop
Unlike other languages like C, C++, or Java, Python permits us to use the else declaration with the for loop which can be accomplished only when all the iterations are exhausted. Here, we should be aware that if the loop incorporates any of the break declaration then the else statement will not be executed.
for i in range(0,5): print(i) else: print("for loop completely exhausted, since there is no break.")
0 1 2 3 4 for loop completely exhausted, since there is no break.
The for loop totally exhausted, on the grounds that there is no break.
for i in range(0,5): print(i) break; else:print("for loop is exhausted"); print("The loop is broken due to break statement...came out of the loop")
In the above example, the loop is damaged due to the destroy statement; therefore, the else statement will no longer be executed. The announcement existing instantaneous next to else block will be executed.
The loop is broken due to the wreck statement…came out of the loop. We will study more about the damage assertion in subsequent tutorial.