Python for Data Analytics & AutomationLesson 5 of 41
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 ValueVIP MemberCalculated Delivery FeeLogic Rule
₹850.00True₹0.00 (Free)VIP Member cart >= ₹500
₹620.00False₹25.00Standard cart >= ₹500
₹320.00False₹50.00Standard cart < ₹500
₹0.00 (Cancelled)False₹0.00Falsy 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)

ActionWindowsMac
Indent Code Block in VS CodeTabTab
Unindent Code Block in VS CodeShift + TabShift + 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.