Skip to content
    Hack The FutureHack The FuturePython Notes

    UNIT 03 · TOPIC 06

    Variable-Length Arguments

    *args collects extra positional arguments and **kwargs collects extra named arguments.

    The idea

    Variable-Length Arguments, simply explained

    *args collects extra positional arguments and **kwargs collects extra named arguments.

    01

    See the big picture

    How the idea flows

    Input or goalVariable-Length Arg…Python result
    Understand the problemApply Variable-Length ArgumentsRun the codeCheck the output

    Read the diagram as a simple path: begin with a problem, use the Python idea, then verify the result.

    02

    Everyday connection

    Real-life example

    ↗

    A café bill function can total any number of ordered items.

    03

    Learn by doing

    Python code example

    PYTHONExample
    def total(*prices):
        return sum(prices)
    print(total(30, 45, 25))
    Expected result100
    Try it: Change one value in the code, predict what will happen, then run it and compare your answer.
    04

    Keep this in mind

    Key takeaways

    • *args collects extra positional arguments and **kwargs collects extra named arguments.
    • Think of a real problem where variable-length arguments could save time or organize information.
    • Test small changes and read the result carefully before moving to a bigger program.

    Quick self-check

    Can you explain it in your own words?

    What is the main idea of Variable-Length Arguments?

    *args collects extra positional arguments and **kwargs collects extra named arguments.

    Where could you see this idea in everyday life?

    A café bill function can total any number of ordered items.

    Keep learning · Hack The Future