In this tutorial, we will learn how to use Python for loops.Python frequently uses the loop to iterate over iterable objects like lists, tuples and strings
Introduction to for Loop in Python: for loops are used when a section of code needs to be repeated a certain number of times. In Python, the for statement runs the code block each time it traverses a series of elements. On the other hand, the "while" loop is used when a condition needs to be verified after each repeatition or when a piece of code needs to be repeated indefinitely.
Syntax of for Loop
for value in sequence: {loop body} |
---|
Example of Python for loop:
#Python program to add the square of each element of the list #very first create a sequence, like here i create a list of numbers numbers=[2, 8, 6, 7, 6, 4, 9, 8, 13, 25] #now create a variable to store the sum of squares of the numbers sum_=0 #now use for loop to iterate over the list for num in numbers: sum_= sum_+ num**2 print("The sum of squares of each element of the list is: ", sum_) |
---|
Output:
The sum of square of each element of the list is: 1144 |
---|
The range() Function: It is a built in Python function that is used to provide a series for the for expression to run over by following a particular pattern.
Better understandable by the following codes:
my_list=[2, 8, 6, 7] for iter_var in range(len(my_list)): my_list.append(my_list[iter_var]+2) print(my_list) |
---|
Output:
[2, 8, 6, 7, 4, 10, 8, 9] |
---|
Iterating by Using Index of Sequence: Another method of iterating through every item is to use an index offset within the sequence. Here is a simple example to understand it better:
#Python program to add the square of each element of the list #very first create a sequence, like here i create a list of numbers numbers=[2, 8, 6, 7, 6, 4, 9, 8, 13, 25] #now create a variable to store the sum of squares of the numbers sum_=0 #now use for loop to iterate over the list for num in range(len(numbers)): sum_= sum_+ numbers[num]**2 print("The sum of squares of each element of the list is: ", sum_) |
---|
Output:
The sum of square of each element of the list is: 1144 |
---|
Iterating by Using Index of Sequence: Another method of iterating through every item of a list is to use an index offset within the sequence. Here's a simple illustration:
#Python program to add the square of each element of the list #very first create a sequence, like here i create a list of numbers numbers=[2, 8, 6, 7, 6, 4, 11, 24] #now create a variable to store the sum of squares of the numbers sum_=0 #now use for loop to iterate over the list for num in range(len(numbers)): sum_= sum_+ numbers[num]**2 print("The sum of squares of each element of the list is: ", sum_) |
---|
Output:
The sum of square of each element of the list is: 902 |
---|
The len() worked in a technique that profits the complete number of things in the rundown or tuple, and implicit capability range(), which returns the specific grouping to emphasize over, proved helpful here.
Using else Statement with for Loop
A loop expression and an else expression can be connected in Python.
After the circuit has finished iterating over the list, the else statement is combined with for loop.
Following is the demonstration to show how to extract students' marks from the record by combining for expression with an else statement.
#Python program to find and print odd numbers from the given list list_1=(3, 4, 8, 7, 5, 6) #Creating for loop for value in list_1: if value%2!=0: print(value) else: print("These are odd numbers in the list") |
---|
Output:
3 7 5 These are odd numbers in the list |
---|
Nested Loops: Nested loops means loop inside a loop. It may be for loop inside the while loop or for loop inside the for loop etc.
Thr "inner loop" will be executed one time for each iteration of the "outer loop".
Syntax of Python nested loop:
Outer_loop Expression: Inner_loop Expression: Statement inside inner_loop |
---|
Example1: Print each adjective given in adj list for each fruit given in fruits list:
adj = ["Sweet", "Red", "Big"] fruits = ["apple", "cherry", "strawberry"] for x in adj: for y in fruits: print (x, y) |
---|
Output:
Sweet apple Sweet cherry Sweet strawberry Red apple Red cherry Red strawberry Big apple Big cherry Big strawberry |
---|
Example2: Print tables of 2 and 3 using range() function in nested loop.
for x in range(2, 4): for y in range(1, 11): print (x, "*", y, "=", x*y) |
---|
Output:
2*1 = 2 2*2 = 4 2*3 = 6 2*4 = 8 2*5 = 10 2*6 = 12 2*7 = 14 2*8 = 16 2*9 = 18 2*10 = 20 3*1 = 3 3*2 = 6 3*3 = 9 3*4 = 12 3*5 = 15 3*6 = 18 3*7 = 21 3*8 = 24 3*9 = 27 3*10 = 30 |
---|