for — Easy Examples
Starts a loop that iterates over a sequence or iterable
Iterate over a list
Using for to process each item in a sequence.
python
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Iterate over a range for i in range(5): print(i, end=" ") print() # Iterate over a string for char in "Python": print(char, end="-") print()
Expected Output
apple banana cherry 0 1 2 3 4 P-y-t-h-o-n-
for iterates over any iterable: lists, ranges, strings, dicts, files, and more.
For with enumerate and zip
Getting indices and combining sequences.
python
colors = ["red", "green", "blue"] for i, color in enumerate(colors): print(f"{i}: {color}") names = ["Alice", "Bob", "Charlie"] ages = [25, 30, 35] for name, age in zip(names, ages): print(f"{name} is {age}")
Expected Output
0: red 1: green 2: blue Alice is 25 Bob is 30 Charlie is 35
enumerate gives you (index, value) pairs. zip combines multiple iterables element by element.
Want to try these examples interactively?
Open Easy Playground