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} |
|---|
| #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_) |
|---|
| The sum of square of each element of the list is: 1144 |
|---|
| 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) |
|---|
| [2, 8, 6, 7, 4, 10, 8, 9] |
|---|
| #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_) |
|---|
| The sum of square of each element of the list is: 1144 |
|---|
| #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_) |
|---|
| 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.
| #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") |
|---|
| 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.
| 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) |
|---|
| Sweet apple Sweet cherry Sweet strawberry Red apple Red cherry Red strawberry Big apple Big cherry Big strawberry |
|---|
| for x in range(2, 4): for y in range(1, 11): print (x, "*", y, "=", x*y) |
|---|
| 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 |
|---|
No comments:
Post a Comment