Basic Python for Data Engineers
By the end of this lesson, you’ll be able to:
Use
ifstatements to make decisions in your code.Loop through lists and files using
forandwhileloops.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
| Concept | What it does | Example |
|---|---|---|
if | Run code only if condition is true | if x > 10: |
for | Loop over items | for row in data: |
while | Loop while a condition is true | while count < 5: |
break | Stop loop early | if error: break |
continue | Skip to next item | if empty: continue |
range() | Numbers from 0 to N-1 | range(5) → 0,1,2,3,4 |
enumerate() | Loop with index | for i, val in enumerate() |
Finish Course Early?
You have not completed all required lessons and assessments.