Python Tutorial 0/52 lessons ~6 min read Lesson 11
Functions
Functions are first-class objects in Python — pass them as arguments, return them, store them in collections.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Functions are first-class objects in Python — pass them as arguments, return them, store them in collections. They support default args, keyword args, *args, **kwargs.
Understanding the topic
Core concepts to understand:
def name(args):- Default values:
def f(x=10): - Variadic:
*args(tuple),**kwargs(dict). - Return tuples for multiple values.
Syntax reference
Visual flow / code:
python
def greet(name: str, greeting: str = "Hello") -> str:return f"{greeting}, {name}!"# Keyword argsgreet(name="Alice", greeting="Hi")# Variadicdef total(*nums: int, factor: int = 1) -> int:return sum(nums) * factortotal(1, 2, 3, factor=2) # 12# **kwargsdef config(**opts):print(opts)config(host="localhost", port=8080)
Execution workflow
1Functions Workflow
1 / 4Step 1
def name(args):
Apply this step while implementing functions in real code.
Real-world use
Functions with type hints + docstrings form the API contract of any Python codebase. Tools like Sphinx and mkdocs auto-generate docs from them.
Best practices
- Always add type hints.
- Default args must be immutable (avoid
=[]). - Keep functions small (~20 lines).
Common mistakes
def f(x=[]):— mutable default arg shared across calls!
Hands-on exercise
Interview preparation — practice these questions:
- What's the mutable default arg trap?
- *args vs **kwargs?
- Can functions return functions?
Summary
In summary: Functions are first-class. Never use mutable defaults.
Ready to mark this lesson complete?Track your journey across the entire course.