Lesson 2 · Data analytics zero to hero
Python Control Flow: If, For & While Loops | Data Analytics #2
Making decisions and repeating work with if/else and loops. If you haven't seen video one on variables and data types, that's the natural starting point.…
- CourseData analytics zero to hero
- Lesson2 of 30
- Video11 min
- FormatJupyter notebook · 12 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 2: Control Flow#
- Video two of the 30-part series: making decisions and repeating work with if/else and loops.
- If you haven't seen video one on variables and data types, that's the natural starting point.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
Part 1: if, elif, and else#
score = 82
if score >= 70:
print('Pass')
score = 55
if score >= 70:
print('Pass')
else:
print('Fail')
score = 75
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
else:
grade = 'D'
print(grade)
Part 2: for Loops#
prices = [19.99, 45.50, 12.75, 88.00]
for price in prices:
print(price)
for i in range(5):
print(i)
total = 0
prices = [19.99, 45.50, 12.75, 88.00]
for price in prices:
total = total + price
print(f'Total: ${total:.2f}')
prices = [19.99, 45.50, 12.75, 88.00]
for index, price in enumerate(prices):
print(f'Item {index}: ${price}')
Part 3: while Loops#
count = 0
while count < 5:
print(count)
count = count + 1
break and continue#
for number in range(10):
if number == 5:
break
print(number)
for number in range(10):
if number % 2 != 0:
continue
print(number)
Part 4: List Comprehensions#
prices = [19.99, 45.50, 12.75, 88.00]
discounted = [price * 0.9 for price in prices]
print(discounted)
prices = [19.99, 45.50, 12.75, 88.00]
expensive = [price for price in prices if price > 20]
print(expensive)
Wrap-Up: What You Learned#
- if, elif, and else for branching logic based on conditions.
- for loops for iterating over a sequence, plus enumerate for tracking position.
- while loops for repeating until a condition changes, and why you must update that condition yourself.
- break to exit a loop early, and continue to skip to the next pass.
- List comprehensions for building and filtering lists in a single compact line.
- Control flow is how your code makes decisions and does repeated work; video three covers functions and modules, so you can package this logic up and reuse it. Subscribe so it lands automatically see you there.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does this code print?
score = 75 if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' else: grade = 'D' print(grade)
Python checks top to bottom and runs the first true branch. 75 fails >= 90 and >= 80 but passes >= 70, so the grade is C.
What does
list(range(5))produce?range(5) starts at 0 and stops before 5, so it gives 0 through 4.
What happens in a
while count < 5:loop if you forget to updatecountinside it?A while loop repeats as long as the condition is true. If nothing changes count, the condition never becomes false.
What is the difference between
breakandcontinue?break stops the loop completely, while continue skips the rest of the current pass and goes on to the next item.
What does this output?
prices = [19.99, 45.50, 12.75, 88.00] print([p for p in prices if p > 20])
The comprehension keeps only prices greater than 20, which are 45.5 and 88.0.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Given
prices = [19.99, 45.50, 12.75, 88.00], use a for loop to add them up and print the total with 2 decimals.💡 Hint
Start with total = 0 and add each price inside the loop.
✅ Show solution
prices = [19.99, 45.50, 12.75, 88.00] total = 0 for price in prices: total = total + price print(f'Total: ${total:.2f}')
Write code that takes
score = 82and prints a grade: A for 90+, B for 80-89, C for 70-79, otherwise D. Then test it by looping over the scores [95, 82, 71, 40] and printing each score with its grade.💡 Hint
Use if / elif / else inside a for loop.
✅ Show solution
scores = [95, 82, 71, 40] for score in scores: if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' else: grade = 'D' print(score, grade)
Using
prices = [19.99, 45.50, 12.75, 88.00], build a list of 10% discounted prices only for items costing more than 20, rounded to 2 decimals, using a list comprehension. Print the list and, with enumerate, print each index and value.💡 Hint
Combine
if price > 20andround(price * 0.9, 2)in one comprehension.✅ Show solution
prices = [19.99, 45.50, 12.75, 88.00] discounted = [round(p * 0.9, 2) for p in prices if p > 20] print(discounted) for index, value in enumerate(discounted): print(index, value)
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



