Codzy

Parameters and arguments

Parameters are the names in the function definition, and arguments are the values you pass when calling it. You can pass them in order, or by name: introduce(age=25, name="Ali"). A parameter can have a default value, like city="Paris".

With *args, a function accepts as many arguments as you like: they arrive in a tuple. With **kwargs, it accepts as many named arguments as you like: they arrive in a dictionary.

def introduce(name, age, city="Paris"):
    print(f"{name}, {age}, {city}")

introduce("Sara", 20)
introduce(age=25, name="Ali", city="Lyon")

def total(*numbers):
    return sum(numbers)

print(total(1, 2, 3, 4))

def profile(**info):
    for key, value in info.items():
        print(key, "=", value)

profile(job="dev", level="junior")

Start the lesson