Python6 min read
Python Loops
Repeat code using for and while loops.
Emily Davis
December 18, 2025
0.0k0
Repeat code efficiently.
For Loop
```python # Loop through list cities = ["Boston", "Portland", "Atlanta"]
for city in cities: print(city)
Loop through range for i in range(5): print(i) # 0, 1, 2, 3, 4 ```
While Loop
```python count = 0
while count < 5: print(count) count += 1 # Same as: count = count + 1 ```
Break and Continue
```python # Break - stop loop for i in range(10): if i == 5: break print(i) # 0, 1, 2, 3, 4
Continue - skip iteration for i in range(5): if i == 2: continue print(i) # 0, 1, 3, 4 ```
Loop with Index
```python names = ["Tom", "Sarah", "Mike"]
for index, name in enumerate(names): print(f"{index}: {name}") # 0: Tom # 1: Sarah # 2: Mike ```
Remember
- for loop for known iterations - while loop for unknown iterations - Use break to exit early
#Python#Beginner#Loops