Mathew K Analytics

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.…

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Data 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')
Pass
score = 55
if score >= 70:
    print('Pass')
else:
    print('Fail')
Fail
score = 75
if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
else:
    grade = 'D'
print(grade)
C

Part 2: for Loops#

prices = [19.99, 45.50, 12.75, 88.00]
for price in prices:
    print(price)
19.99
45.5
12.75
88.0
for i in range(5):
    print(i)
0
1
2
3
4
total = 0
prices = [19.99, 45.50, 12.75, 88.00]
for price in prices:
    total = total + price
print(f'Total: ${total:.2f}')
Total: $166.24
prices = [19.99, 45.50, 12.75, 88.00]
for index, price in enumerate(prices):
    print(f'Item {index}: ${price}')
Item 0: $19.99
Item 1: $45.5
Item 2: $12.75
Item 3: $88.0

Part 3: while Loops#

count = 0
while count < 5:
    print(count)
    count = count + 1
0
1
2
3
4

break and continue#

for number in range(10):
    if number == 5:
        break
    print(number)
0
1
2
3
4
for number in range(10):
    if number % 2 != 0:
        continue
    print(number)
0
2
4
6
8

Part 4: List Comprehensions#

prices = [19.99, 45.50, 12.75, 88.00]
discounted = [price * 0.9 for price in prices]
print(discounted)
[17.991, 40.95, 11.475, 79.2]
prices = [19.99, 45.50, 12.75, 88.00]
expensive = [price for price in prices if price > 20]
print(expensive)
[45.5, 88.0]

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.

  1. 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)
    
  2. What does list(range(5)) produce?

  3. What happens in a while count < 5: loop if you forget to update count inside it?

  4. What is the difference between break and continue?

  5. What does this output?

    prices = [19.99, 45.50, 12.75, 88.00]
    print([p for p in prices if p > 20])
    

💻 Practice exercises

Try each one in the notebook or in your own editor before opening the solution.

  1. 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}')
    
  2. Write code that takes score = 82 and 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)
    
  3. 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 > 20 and round(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.