For Loop 😉:
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.
For example:
when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration.
some program of for loop :
Program 1
# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
output:
The sum is 48
Program 2
#Program for Print fruit in which have (a) in it
#list of fruits
fruits = ["apple" ,"Orange" ,"Mouse" ,"Keyboard"]
#Empty list
newlist = []
#Apply For loop
for x in fruits: if "a" in x: newlist.append(x)
#Print the newlist
print(newlist)
output:
['apple', 'Orange', 'Keyboard']
program 3
#Tuple of items
numbers = (2,3,5,7)
print ('These are the first four prime numbers ')
#Iterating over the tuple
for item in numbers:
print (item)
output:
These are the first four prime numbers
2
3
5
7
Nested For Loop 😏:
Loops can be nested in Python, as they can with other programming languages. The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion
Some program of Nested for loop :
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)