Functions
Functions package reusable behavior behind a name.
Functions package reusable behavior behind a name.
def normalize_username(value: str) -> str:
return value.strip().lower()
user = normalize_username(" Student ")
print(user)
Parameters and return values
Parameters are inputs. return sends a result back to the caller. A function that only prints is harder to test than one that returns a value.
def is_valid_port(port: int) -> bool:
return 1 <= port <= 65535
Good function design
- Give the function one clear responsibility.
- Use descriptive names.
- Validate inputs at trust boundaries.
- Avoid hidden global state.
- Raise or return meaningful errors.
- Add a short docstring when behavior is not obvious.
Small functions make automation scripts easier to review and safer to modify.