Python's for loop iterates over a sequence. Use range() to generate number sequences:
for i in range(5):
print(i)
# Output: 0, 1, 2, 3, 4
for i in range(1, 6):
print(i)
# Output: 1, 2, 3, 4, 5
for i in range(0, 10, 2):
print(i)
# Output: 0, 2, 4, 6, 8 (step size 2)
colors = ["red", "green", "blue"]
for color in colors:
print(color)
total = 0
for i in range(1, 6):
total += i # total = total + i
print(total) # 15
Write a function factorial(n) that computes n!
n! = 1 × 2 × 3 × ... × n
Example: 5! = 1 × 2 × 3 × 4 × 5 = 120
Click "Run" to execute your code.