Use
for loops to repeat code a specific number of times.
for i in range(5):
print(i)
# Prints: 0, 1, 2, 3, 4
range(5) # 0, 1, 2, 3, 4
range(1, 6) # 1, 2, 3, 4, 5
range(0, 10, 2) # 0, 2, 4, 6, 8
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}")
Calculate sum of 1 to 100:
total = 0
for num in range(1, 101):
total += num
print(total) # 5050
Interactive Visualization