The for loop in Python is used to repeat a block of code multiple times. When used with the range() function, it is especially useful for numeric iteration.
This approach is commonly used when:
for variable in range(start, stop, step): # code blockrange()The range() function generates a sequence of numbers.
range():range(stop)range(start, stop)range(start, stop, step)for i in range(5): print(i)Output
01234Explanation
for i in range(1, 6): print(i)Output
12345for i in range(1, 10, 2): print(i)Output
13579Explanation
for i in range(5, 0, -1): print(i)Output
54321Explanation
num = 5 for i in range(1, 11): print(num * i)total = 0 for i in range(1, 6): total += i print("Sum =", total)Output
Sum = 15for Loop with range()for i in range(1, 4): for j in range(1, 4): print(i, j)range() does not include the stop valuefor loop with range() is used for numeric iteration
range() generates a sequence of numbers
Forms:
range(stop)range(start, stop)range(start, stop, step)Supports: