Functions in Python
Functions in Python
Section titled “Functions in Python”Introduction
Section titled “Introduction”A function is a reusable block of code that performs a specific task. Functions help you organize code, avoid repetition, and improve readability.
Defining a Function
Section titled “Defining a Function”def greet(name): """Display a greeting.""" print(f"Hello, {name}!")Calling a Function
Section titled “Calling a Function”greet("Alice") # Hello, Alice!Return Values
Section titled “Return Values”# Single return valuedef add(a, b): return a + b
result = add(3, 5) # 8
# Multiple return values (tuple)def min_max(numbers): return min(numbers), max(numbers)
low, high = min_max([3, 1, 4, 1, 5])print(low, high) # 1 5
# Early returndef divide(a, b): if b == 0: return "Cannot divide by zero" return a / bDocstrings
Section titled “Docstrings”def calculate_bmi(weight: float, height: float) -> float: """Calculate Body Mass Index (BMI).
Args: weight: Weight in kilograms height: Height in meters
Returns: BMI value as a float
Example: >>> calculate_bmi(70, 1.75) 22.86 """ return weight / (height ** 2)Type Hints
Section titled “Type Hints”def process_data(name: str, age: int, scores: list[float]) -> dict[str, float]: """Process user data and return statistics.""" return { "name": name, "average": sum(scores) / len(scores), "age_category": "adult" if age >= 18 else "minor" }Best Practices
Section titled “Best Practices”- Functions should do one thing — single responsibility principle
- Use descriptive names —
calculate_total()notcalc() - Keep functions short — ideally under 20-30 lines
- Always include docstrings for non-trivial functions
- Use type hints to make code self-documenting
- Avoid side effects — prefer returning values over modifying globals
Practice Exercises
Section titled “Practice Exercises”Exercise 1: Write a function is_palindrome(s) that checks if a string is a palindrome.
Exercise 2: Create a function that takes a list of numbers and returns the second largest number.
Exercise 3: Build a simple calculator with functions for each operation.