Python 101Free
FUNCTIONS

Arguments

All the ways to pass values into a function.

SECTION 01

Positional vs keyword

By default, arguments bind to parameters by position. greet("Ada", "hi") matches the first parameter to "Ada" and the second to "hi". The order matters and Python does not check whether the values look right for their slots.

Keyword arguments bind by name. greet(greeting="hi", name="Ada") ignores the order and assigns each value to the matching parameter. This is more verbose but lets the call document itself, which is worth a lot in functions with several parameters.

A call can mix the two, but positional arguments must come before keyword ones. The general rule is: positional for the obvious values, keyword for anything where order is not self-evident or where you want to skip past defaults.

python
def greet(name, greeting="hi"):
    return f"{greeting}, {name}"

greet("Ada")                       # by position
greet(name="Ada", greeting="yo")   # by name, any order
2 more sections

Create a free account to access all sections and animations.

Create free accountAlready have an account? Sign in