Advanced Code Example — Variables, Expressions & Data Types#
This example extends the basic module code into a more complete customer analytics workflow. It demonstrates type inspection, formatted output, boolean logic, and derived calculations — all using only variables, expressions, and data types.
Business Scenario#
You are an analyst at a regional retail chain. Given a customer’s transaction history, you need to:
- Store and organize customer data using appropriate data types
- Calculate key metrics: total spend, average purchase, and spend category
- Format the results for a business-readable report
- Apply boolean logic to determine eligibility flags
Code#
# ── Customer Profile ────────────────────────────────────────────────
customer_name: str = 'Alice Johnson'
customer_id: int = 1001
customer_region: str = 'Northwest'
is_loyalty_member: bool = True
# ── Transaction Data (individual floats) ────────────────────────────
purchase_jan: float = 250.50
purchase_feb: float = 180.75
purchase_mar: float = 420.25
purchase_apr: float = 315.00
# ── Derived Metrics ─────────────────────────────────────────────────
total_spent: float = purchase_jan + purchase_feb + purchase_mar + purchase_apr
num_purchases: int = 4
average_purchase: float = total_spent / num_purchases
# ── Business Rules via Boolean Expressions ──────────────────────────
qualifies_for_gold: bool = total_spent >= 500
qualifies_for_platinum: bool = total_spent >= 1000 and num_purchases >= 4
qualifies_for_free_shipping: bool = is_loyalty_member and total_spent > 300
# ── Tier Assignment using Conditional Expression ────────────────────
customer_tier: str = (
'Platinum' if qualifies_for_platinum
else 'Gold' if qualifies_for_gold
else 'Standard'
)
# ── String Formatting for Report Output ─────────────────────────────
separator: str = '-' * 45
print(separator)
print(f" CUSTOMER ANALYTICS REPORT")
print(separator)
print(f" Name : {customer_name}")
print(f" ID : {customer_id}")
print(f" Region : {customer_region}")
print(f" Member : {'Yes' if is_loyalty_member else 'No'}")
print(separator)
print(f" Total Spent : ${total_spent:,.2f}")
print(f" Avg Purchase : ${average_purchase:,.2f}")
print(f" Num Purchases : {num_purchases}")
print(separator)
print(f" Tier : {customer_tier}")
print(f" Free Shipping : {'Yes' if qualifies_for_free_shipping else 'No'}")
print(separator)
# ── Data Type Inspection ─────────────────────────────────────────────
print("\n DATA TYPE SUMMARY:")
print(f" customer_name -> {type(customer_name).__name__}")
print(f" customer_id -> {type(customer_id).__name__}")
print(f" total_spent -> {type(total_spent).__name__}")
print(f" is_loyalty_member-> {type(is_loyalty_member).__name__}")
print(f" customer_tier -> {type(customer_tier).__name__}")Expected Output#
---------------------------------------------
CUSTOMER ANALYTICS REPORT
---------------------------------------------
Name : Alice Johnson
ID : 1001
Region : Northwest
Member : Yes
---------------------------------------------
Total Spent : $1,166.50
Avg Purchase : $291.63
Num Purchases : 4
---------------------------------------------
Tier : Platinum
Free Shipping : Yes
---------------------------------------------
DATA TYPE SUMMARY:
customer_name -> str
customer_id -> int
total_spent -> float
is_loyalty_member-> bool
customer_tier -> strKey Concepts Demonstrated#
| Concept | Where in Code |
|---|---|
| Type annotations | customer_name: str = 'Alice Johnson' |
| Float arithmetic | total_spent / num_purchases |
| Compound boolean | total_spent >= 1000 and num_purchases >= 4 |
| Conditional expression | 'Platinum' if ... else 'Gold' if ... else 'Standard' |
| f-string formatting | f"${total_spent:,.2f}" |
| Type inspection | type(customer_name).__name__ |
What to Notice#
Type annotations (the : str, : float hints) don’t change how Python works — they’re documentation that makes code more readable and understandable for colleagues and tools.
The conditional expression on the tier assignment is a compact way to assign a value based on conditions. It reads like natural language: “Platinum if platinum conditions are met, else Gold if gold conditions, else Standard.”
f-string formatting with {total_spent:,.2f} applies two formatting rules: , adds thousands separators and .2f rounds to 2 decimal places — essential for any currency display in business reporting.
Next: Jupyter Notebook →