Basic Syntax
for <temporary_variable> in <collection>:
<action>
# Example:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
range()
range(n)
generates numbers from 0 to n-1.
for i in range(6):
print(i) # Prints numbers 0 to 5
You can also use:
range(start, stop, step) for example, range(2, 10, 2) → 2, 4, 6, 8
Ignoring the Loop Variable
Use _ when the variable won't be used inside the loop.
for _ in range(3):
print("Do this 3 times")