7. Function Annotations
These examples demonstrate how function annotations can be used to improve code readability, documentation, and static analysis. Although Python itself doesn’t enforce type annotations at runtime, too
def greet(name: str) -> str:
return f"Hello, {name}!"
print(greet("Alice")) # Output: Hello, Alice!def add(x: int, y: int) -> int:
return x + y
print(add(10, 20)) # Output: 30def power(base: int, exponent: int = 2) -> int:
return base ** exponent
print(power(5)) # Output: 25
print(power(5, 3)) # Output: 125Last updated