🌱 Junior Track 08 of 08

8. Python Functions

Master modular function abstractions: `def`, `Parameters & Arguments`, `Return Values`, `Default & Keyword Args`, `*args` & `**kwargs`, `Positional-only (/)` & `Keyword-only (*)`, `LEGB Scope Rules`, `Nested Closures`, `Recursion`, `Docstrings`, and `Type Hints`.

👁️ Views: 0
# JUNIOR PYTHON TRACK 08 — FUNCTIONS MASTER IMPLEMENTATION

from typing import Dict, Any, Callable

# 1. Core Function Definition & Type Hints
def calculate_total(price: float, quantity: int) -> float:
    """Calculate total cost given unit price and quantity."""
    return price * quantity

# 2. Variable *args & **kwargs
def calculate_stats(*numbers: float, **metadata: Any) -> Dict[str, Any]:
    total = sum(numbers)
    count = len(numbers)
    avg = total / count if count > 0 else 0.0
    return {"total": total, "count": count, "average": avg, "meta": metadata}

# 3. Positional-only (/) & Keyword-only (*)
def configure_service(endpoint: str, /, timeout: int = 30, *, debug: bool = False) -> Dict[str, Any]:
    return {"endpoint": endpoint, "timeout": timeout, "debug": debug}

# 4. Closures & Recursion
def make_multiplier(factor: int) -> Callable[[int], int]:
    def multiplier(x: int) -> int:
        return x * factor
    return multiplier

def factorial(n: int) -> int:
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(f"Total: {calculate_total(19.99, 3)}, Fact(5): {factorial(5)}")

⭐ Rate Track 08: Functions & Scope (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

★ ★ ★ ★ ★
← 7. Comprehensions 🌱 Junior Hub 9. *args & **kwargs →