Python Functions — A Practical Beginner's Guide
Learn how to write Python functions, use default arguments, type hints, and avoid the mutable-default-argument trap. With a runnable code editor.
Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.
A function in Python is a reusable block of code with a name. It's the single most important building block once your scripts grow past 20 lines.
def greet(name):
return f"Hello, {name}!"
message = greet("Alice")
print(message)
The def keyword defines a function. The colon and indent mark its body. return sends a value back to the caller. A function without return returns None.
Naming and calling
Names use snake_case by convention. Call a function by writing its name with parentheses, even if it takes no arguments: greet(), not just greet.
Parameters vs arguments
These two words are often confused but mean different things:
- Parameters are the names in the definition:
def greet(name):—nameis a parameter. - Arguments are the values you pass:
greet("Alice")—"Alice"is an argument.
Default arguments
def discount(price, pct=10):
return price * (1 - pct/100)
discount(100) # 90.0 — uses the default
discount(100, 25) # 75.0 — overrides the default
Keyword arguments
When a function has more than 2 or 3 parameters, calling by keyword is clearer:
send_email(to="alice@example.com", subject="Hi", body="...", attachments=[])
Self-documenting at the call site.
Type hints (modern Python — use them)
Type hints don't change runtime behavior, but they help editors, linters, and other humans:
def add(a: int, b: int) -> int:
return a + b
Add them once you're past the throwaway-script stage. They pay off immensely.
Docstrings
A triple-quoted string right after def becomes the function's documentation:
def discount(price, pct=10):
"""Return price after applying pct% discount.
pct is an integer percentage. Use 0 for no discount.
"""
return price * (1 - pct/100)
help(discount) prints the docstring. So does your IDE's hover.
The mutable default argument trap
The single most-asked Python interview question.
def add_to_basket(item, basket=[]): # WRONG
basket.append(item)
return basket
The default list is created once when the function is defined, then reused across every call. Two callers share the same list. Catastrophic bug.
Fix:
def add_to_basket(item, basket=None):
if basket is None:
basket = []
basket.append(item)
return basket
Rule: never use a mutable default (list, dict, set). Use None and check.
Where this fits in the 7-Day Python Sprint
Functions are introduced on Day 3 — alongside *args, **kwargs, lambdas, and map / filter / sorted. By the end of Day 3 your CLI calculator from Day 2 gets refactored into a clean function library.
Practice
Auto-graded — run in your browser, no signup.
Exercise 1: Define a function `area(width, height=1)` that returns width times height. Then call it twice to compute `a1 = area(5, 4)` and `a2 = area(7)`.
Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.
Like this tutorial? Get the full Python Sprint.
7 days, daily videos, auto-graded labs, AI tutor, mini-project. 1-year content access.
See the Python Sprint