For loop in Python
Published in
1 min readApr 4, 2024
Lesson#15
Python has very powerful for loop as compared to other programming languages and comes in different flavors. Lets’ discuss them one by one.
for loop for a range of numbers
The sytax of for loop for a given range of numbers is:
for variable in range(value):
statement
Examples:
# print numbers from 0 to 4
print("-----Loop # 1---------")
for x in range(5):
print(x)
# print numbers from 1 to 4
print("-----Loop # 2---------")
for x in range(1,5):
print(x)
# print numbers from 1 to 4
print("-----Loop # 3---------")
for x in range(1,10,3):
print(x)
Output of the program:
— — -Loop # 1 — — — — -
0
1
2
3
4
— — -Loop # 2 — — — — -
1
2
3
4
— — -Loop # 3 — — — — -
1
4
7
range(1,10,3) means: start with 1, increment with 3, and end with 10–1.