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
    Interview Q&A included

    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 args
    greet(name="Alice", greeting="Hi")
    # Variadic
    def total(*nums: int, factor: int = 1) -> int:
    return sum(nums) * factor
    total(1, 2, 3, factor=2) # 12
    # **kwargs
    def config(**opts):
    print(opts)
    config(host="localhost", port=8080)

    Execution workflow

    1Functions Workflow
    1 / 4

    def name(args):

    def name(args):

    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!

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhat's the mutable default arg trap?+

    Answer

    Default argument values are evaluated once, when the def statement runs, not on each call. So a default like items=[] is one shared list that persists across calls and accumulates changes. The fix is to use None as the default and create the list inside: if items is None: items = [].

    Follow-up

    Where is the default value stored on the function object?
    2BeginnerQuestion*args vs **kwargs?+

    Answer

    *args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dict. The same symbols at a call site unpack an iterable or mapping into arguments. They are commonly used for wrappers, decorators, and forwarding arguments, and the names args and kwargs are only convention.

    Follow-up

    What is the order of parameter kinds in a signature?
    3BeginnerQuestionCan functions return functions?+

    Answer

    Yes, functions are first-class objects, so they can be passed, stored, and returned. Returning an inner function that captures variables from the enclosing scope creates a closure, which is the basis for decorators and function factories. For example def make_adder(n): return lambda x: x + n.

    Follow-up

    How does this relate to decorators?

    Summary

    In summary: Functions are first-class. Never use mutable defaults.

    Ready to mark this lesson complete?Track your journey across the entire course.