# 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)}")
$ python3 junior_functions.py
Total: 59.97, Fact(5): 120
Execution completed successfully.
import unittest
class TestJuniorFunctions(unittest.TestCase):
def test_calculate_total(self):
self.assertAlmostEqual(calculate_total(10.5, 4), 42.0)
def test_factorial(self):
self.assertEqual(factorial(5), 120)
if __name__ == "__main__":
unittest.main()