Conditional Statements and Loops
# CHAPTER 5
Conditional Statements and Loops
1. Chapter Introduction
Programs are useless if they can only run in a straight line. Data science requires logic: *If* a customer is VIP, *then* apply a discount. Furthermore, you don't want to write the same line of code 10,000 times to process 10,000 rows of data. This chapter covers Control Flow—using conditionalif statements to make decisions, and for loops to automate repetitive tasks.
2. Conditional Statements (if, elif, else)
The if statement evaluates a condition (using the comparison operators from Chapter 4). If the condition evaluates to True, the indented block of code runs.
For more complex logic, we add elif (Else If) and else.
*How it works:* Python evaluates from top to bottom. The moment it finds a True condition, it executes that block and skips the rest.
3. The for Loop
A for loop iterates over a sequence (like a list, string, or a range of numbers) and executes a block of code for each item. This is how you automate repetitive data processing.
1. Looping over a List:
2. Looping over a Range of Numbers:
The range(start, stop) function generates a sequence of numbers. (Note: It stops *before* the stop number).
4. The while Loop
A while loop continues to execute *as long as* a specific condition remains True.
*Warning:* If you forget to update the condition (like forgetting count += 1), the loop will run forever (an Infinite Loop) and crash your program!
5. Loop Control: break and continue
Sometimes you need to interrupt a loop before it finishes naturally.
break: Stops the entire loop immediately.
continue: Skips the rest of the *current* iteration and moves to the next item.
6. Mini Project: Student Grade Calculator
Let's build a program that processes a list of student scores, calculates their letter grade, and counts how many students passed.
7. Common Mistakes
-
Forgetting the colon
:Everyif,elif,else,for, andwhilestatement MUST end with a colon.
- Indentation Errors: The code inside the loop or if-statement MUST be indented. Code that is not indented is considered outside the loop.
-
Infinite While Loops: Creating a
while True:loop without abreakstatement inside it will freeze your computer.
8. MCQs
What keyword is used to check multiple alternative conditions in Python?
In a Python for loop, what determines which code belongs inside the loop?
What does range(3) generate?
Which loop runs indefinitely as long as a condition remains true?
What does the break statement do?
What does the continue statement do?
Every if and for statement line must end with which character?
If x = 10, which block will execute? if x > 5: print("A"); elif x > 8: print("B")?
How do you increment a counter variable by 1?
What happens if a while loop condition never evaluates to False?
9. Interview Questions
-
Q: Explain the difference between
breakandcontinuein a loop. Give a data-cleaning scenario where you would use each.
-
Q: Why is the order of
if / elifstatements important? What happens if you checkif score > 50before checkingif score > 90?
11. Summary
Control flow makes code intelligent.if/elif/else blocks allow the program to make decisions based on changing data. for loops are essential for iterating through datasets or lists of files, while while loops execute until a condition changes. Use break to exit loops early, and continue to skip specific items (like skipping invalid rows in a dataset).