Lesson 29 of 4073%
(Progress persistence is disabled until Phase 9)
Python
Intermediate
Default and Keyword Arguments
Make function calls more flexible.
Default & Keyword Arguments
What You’ll Learn
You’ll learn how to set default values for parameters and how to call functions using argument names.
Default Parameter Value
If we call the function without argument, it uses the default value.
def greet(country="Norway"):
print(f"I am from {country}")
greet("Sweden")
greet() # Uses "Norway"
Expected Output:
I am from Sweden
I am from Norway
Keyword Arguments
You can also send arguments with the key = value syntax. This way the order of the arguments does not matter.
def build_profile(first, last, age):
print(f"{first} {last}, age {age}")
# Order doesn't matter when using keyword arguments
build_profile(age=30, last="Smith", first="John")