Lesson 5 of 41
Iteration Mastery: While Loops, For Loops, enumerate() & zip()
Overview & Learning Objectives
Conditional branching directs data flow based on analytical business rules. Python relies on clean indentation (4 spaces) rather than curly braces. Data analysts must master truthy and falsy evaluation to handle empty DataFrames, empty lists, `None`, and zero-value outliers safely without crashing production pipelines.
- Structure clean, readable multi-branch conditional logic with `if`, `elif`, and `else`.
- Understand truthy and falsy rules across native Python types (`0`, `""`, `[]`, `{}`, `None`).
- Leverage short-circuit boolean evaluation (`and`, `or`) and concise ternary expressions.
Industry Standard
Standard: Production standards at Deloitte and Swiggy require guarding against empty input data collections using Python truthiness (`if not df.empty:`) before executing heavy analytical transformations.
Real-World Example: ScoopCraft Dynamic Delivery Fee Tiering
ScoopCraft calculates delivery fees based on order cart value and franchise VIP membership status.
| Cart Value | VIP Member | Calculated Delivery Fee | Logic Rule |
|---|---|---|---|
| ₹850.00 | True | ₹0.00 (Free) | VIP Member cart >= ₹500 |
| ₹620.00 | False | ₹25.00 | Standard cart >= ₹500 |
| ₹320.00 | False | ₹50.00 | Standard cart < ₹500 |
| ₹0.00 (Cancelled) | False | ₹0.00 | Falsy order validation |
Syntax & Core Code Implementation
# Multi-branch business condition
cart_total = 650.0
is_vip = False
if cart_total >= 1000 or is_vip:
delivery_fee = 0.0
elif cart_total >= 500:
delivery_fee = 25.0
else:
delivery_fee = 50.0
# Concise Python Ternary Expression
status = "Eligible" if cart_total >= 500 else "Standard"
# Truthy / Falsy Validation
order_items = []
if not order_items:
print("Warning: Cart is empty, skipping payment gateway.")
Parameters & Arguments Breakdown
if / elif / else: Sequential decision statements; evaluates condition from top to bottom, executing only the first True block.short-circuit: In `a or b`, if `a` is True, `b` is never evaluated. In `a and b`, if `a` is False, `b` is never evaluated.ternary operator: Inline expression: `x if condition else y`.Falsy values: Objects evaluating to False in boolean context: `False`, `None`, `0`, `0.0`, `""`, `[]`, `()`, `{}`.
Sample Walkthrough (Q&A)
Question: What is the output of `"Gold" if 0 else "Silver"` and why?
Code Pattern: "Gold" if 0 else "Silver"
Result: "Silver"
Explanation: Integer `0` is inherently falsy in Python; therefore, the ternary expression branches to the `else` expression "Silver".
Common Mistakes & Limitations
- Using `if x == True:` or `if len(items) > 0:` instead of idiomatic Python truthy checks (`if x:` and `if items:`).
- Confusing identity operator `is` (checks memory address) with equality operator `==` (checks value).
Keyboard Shortcuts (Jupyter & VS Code)
| Action | Windows | Mac |
|---|---|---|
| Indent Code Block in VS Code | Tab | Tab |
| Unindent Code Block in VS Code | Shift + Tab | Shift + Tab |
Pro Tips & Modern Best Practices
- Use Python 3.10+ `match / case` structural pattern matching for cleaner multi-way branching over long `elif` chains.
- Use dictionary dispatch tables for mapping static keys to calculation functions.