Functions and Error Handling
A function is a named, reusable block of code, you define it once and call it as many times as you need.
Defining a function
def divide(a, b): starts a function definition, a and b are parameters, placeholders for whatever values get passed in when the function is called. return sends a value back to whoever called the function, once return runs, the function stops immediately.
Type hints
a: float, b: float) -> float are type hints, they document what types a function expects and returns. Python doesn't enforce them at runtime (you could still pass in the wrong type), but they help tools, editors, and other developers understand your code, and catch mistakes early.
Handling errors
Sometimes something goes wrong that a function can't recover from itself, dividing by zero, a missing file, invalid input. Python handles this with exceptions:
raise ValueError("...")signals "something is wrong" and stops normal execution.try:wraps code that might fail.except ValueError as err:catches that specific kind of error if it happens, letting your program respond gracefully instead of crashing.
Without the try/except, calling divide(10, 0) would crash the whole program the moment the ValueError was raised.