Python – for Loop

for loop:

  • “for” is a keyword.
  • for loop executes a block repeatedly as long as the condition is valid.
  • for loop uses range() function for loop repetition.

range():

  • The range() function in Python generates a sequence of numbers within a given range.
  • It takes up to three arguments: start (optional), stop (required), and step (optional).
  • The sequence generated by the range() function is commonly used in for loops.
Print the numbers from 0 to 9:
for i in range(10):
            print(i)
 
Print the numbers from 3 to 8:
for i in range(3,9):
            print(i)
 
Print the numbers from 10 to 1:
for i in range(10, 0, -1):
            print(i)
 
Print every third number from 0 to 20:
for i in range(0, 21, 3):
            print(i)
 
Print the even numbers from 0 to 10:
for i in range(0, 11, 2):
            print(i)
 
Print the odd numbers from 1 to 9:
for i in range(1, 10, 2):
            print(i)
 
Print the even numbers from 10 to 0:
for i in range(10, -1, -2):
            print(i)
Scroll to Top