By the end of this lesson, you’ll be able to:

  • Use if statements to make decisions in your code.

  • Loop through lists and files using for and while loops.

  • Filter data from CSV files using loops and conditions.

  • Understand how these tools apply in real-world data engineering tasks.

Conditions in Python (if, elif, else)

Conditions let you control what your code does depending on the data.

  • Syntax
if condition:
    # code to run if condition is True
elif another_condition:
    # code to run if the first is False but this one is True
else:
    # code to run if none of the above are True

Use case in data engineering:

You might want to label rows based on status:

status = 503
if status >= 500:
    print("Server error")
elif status >= 400:
    print("Client error")
else:
    print("OK")

Loops in Python (for, while)

Loops let you repeat actions — very useful for processing files, rows, logs, etc.

for loop

Used to go through each item in a list, file, or other collection.

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Used to go through each item in a list, file, or other collection.

Use range() with for:

for i in range(5):  # goes from 0 to 4
    print(i)

Data example: looping through CSV rows

import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

while loop

Repeats as long as a condition is True.

count = 0
while count < 3:
    print("Loop number", count)
    count += 1

Extra tools you’ll use often

enumerate() — get index while looping

names = ["Anna", "Ben", "Cara"]
for i, name in enumerate(names):
    print(f"{i + 1}. {name}")

break and continue

# break stops the loop early
for i in range(10):
    if i == 5:
        break
    print(i)

# continue skips one loop
for i in range(5):
    if i == 2:
        continue
    print(i)

Summary

ConceptWhat it doesExample
ifRun code only if condition is trueif x > 10:
forLoop over itemsfor row in data:
whileLoop while a condition is truewhile count < 5:
breakStop loop earlyif error: break
continueSkip to next itemif empty: continue
range()Numbers from 0 to N-1range(5) → 0,1,2,3,4
enumerate()Loop with indexfor i, val in enumerate()