11/20
Control Flow & Loops · Page 2 of 2

For & While Loops

Loops

for Loops

Python's for loop iterates over items in an iterable (lists, strings, ranges):

for i in range(5):    # 0, 1, 2, 3, 4
for char in "Data":   # D, a, t, a

while Loops

Run until a condition is false:

while x < 10:
    x += 1

Loop Control

  • break: Exit the loop immediately
  • continue: Skip the rest of the current iteration

Enumerate & Zip (Essential for Data Science)

for i, val in enumerate(['a', 'b']):
    # i is index, val is value
    
for name, age in zip(names, ages):
    # iterate over two lists at once
main.py
Loading...
OUTPUT
Click "Run Code" to execute…