Data engineering is all about processing streams of data. To do that, you need to make decisions (conditionals) and repeat actions (loops). This lesson covers the logic you'll use in almost every script.
if/elif/else)Conditionals let your code make decisions based on data.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or lower")
Python allows you to check if lists, strings, or numbers are "empty" or "zero" directly:
# Check if a list has items
users = []
if not users:
print("No users found!")
# Check if a string is missing
name = None
if not name:
print("Name is missing")
# Check if a number is non-zero
count = 0
if count:
print(f"Count is {count}")
else:
print("Count is zero")
for LoopsUse for loops when you want to iterate over a collection (like a list of files or rows in a CSV).
# Loop over a list
files = ["data1.csv", "data2.csv", "data3.csv"]
for filename in files:
print(f"Processing {filename}...")
# Loop with an index using enumerate()
for i, filename in enumerate(files):
print(f"File {i+1}: {filename}")
# Loop over a dictionary
user = {"name": "Alice", "role": "Engineer"}
for key, value in user.items():
print(f"{key}: {value}")
while LoopsUse while loops when you don't know how many times to repeat, but you have a condition to stop (e.g., waiting for a file to appear, or retrying a network request).
import time
retries = 3
while retries > 0:
print(f"Connecting to database... ({retries} retries left)")
# simulate connection attempt
success = False
if success:
print("Connected!")
break # Exit the loop immediately
retries -= 1
time.sleep(1) # Wait 1 second before retrying
if retries == 0:
print("Failed to connect.")
break and continuebreak: Exits the loop entirely.continue: Skips the rest of the current iteration and jumps to the next one.records = [10, 20, -1, 30, -5, 40]
valid_records = []
for record in records:
if record < 0:
print(f"Skipping invalid record: {record}")
continue # Skip negative numbers
if record > 50:
print("Limit reached, stopping.")
break # Stop processing if value is too high
valid_records.append(record)
List comprehensions are a concise, "Pythonic" way to create lists. They are extremely popular in data engineering for simple transformations.
# [expression for item in iterable if condition]
1. Transform a list (Map)
# Old way
numbers = [1, 2, 3, 4]
squared = []
for num in numbers:
squared.append(num * 2)
# List comprehension way
squared = [num * 2 for num in numbers]
# Result: [2, 4, 6, 8]
2. Filter a list
# Get only even numbers
evens = [num for num in numbers if num % 2 == 0]
3. Clean data strings
raw_names = [" Alice ", "Bob", " Charlie "]
clean_names = [name.strip().lower() for name in raw_names]
# Result: ['alice', 'bob', 'charlie']
⚠️ Pro Tip: If your list comprehension is getting too complex (e.g., nested loops or multiple distinct conditions), switch back to a regular for loop for readability.
Sometimes you need to loop loop inside a loop.
departments = {
"Engineering": ["Alice", "Bob"],
"Sales": ["Charlie", "David"]
}
for dept, employees in departments.items():
print(f"--- {dept} ---")
for employee in employees:
print(f" - {employee}")
Output:
--- Engineering ---
- Alice
- Bob
--- Sales ---
- Charlie
- David
This pattern is useful when processing grouped data (e.g., records by department, transactions by customer).
Test your understanding of loops and logic with this interactive exercise:
[Judge0 Interactive Check]
🚀 Open the Judge0 Widget: asse.be/simple-judge0-widget
Challenge: The code in the widget loops through a list but has logic errors. Use break and continue to filter the data correctly as per the instructions in the widget.
[] in Python?while loop instead of a for loop?squared = []; for x in range(5): squared.append(x**2)?
```python
if x > 10:
pass
elif x == 5:
pass
else:
pass
## For loop
for item in items:
# do something
## With index
for index, item in enumerate(items):
# do something
## List Comprehension
new_list = [transform(x) for x in old_list if condition(x)]
## Dictionary Iteration
for key, value in my_dict.items():
# do something
break: stop loopcontinue: skip to next iterationpass: do nothing (placeholder)Next lesson: Functions and Modules

Found a mistake or have a suggestion? Let us know in the feedback form.