Python for Loop
The for loop in Python is used to iterate over sequences like lists, tuples, strings, or ranges. It follows a clean for variable in sequence syntax.
Basic for Loop Over a List
Looping through each item in a list.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Try it Yourself โ
Looping with range()
range(start, stop, step) generates a sequence of numbers.
for i in range(1, 6):
print(f"Number {i}")
Try it Yourself โ
Using enumerate()
enumerate() gives you both the index and the value during iteration.
colors = ["red", "green", "blue"]
for idx, color in enumerate(colors):
print(f"{idx}: {color}")
Try it Yourself โ
Nested Loops
A loop inside another loop. Runs the inner loop fully for each outer iteration.
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i * j}")
print("---")
Try it Yourself โ