For Loops in Python
For Loops in Python
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated programming
languages.
With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
# if number is divisble by 2
# then it's even
if i%2==0:
for i in range(given_range):
# if i is odd, add it
# to the sum variable
if i%2!=0:::
sum+=ii
for i in range(11):
print (given_number," x",i," =",5*i)
Copy code
Output
5x0=0
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Example 6: Python program to display numbers from a list using a for loop.
# if the below list is given
list = [1,2,4,6,88,125]
for i in list:
print(i)
Copy code
Output
1
2
4
6
88
125