Skip to main content
Practice

How to Assign Default Values to Function Parameters

In Python, you can set default values for function parameters, which are known as Default Parameters.

Default parameters use pre-defined values when no argument is passed during a function call.

To define default parameters, assign a default value to the parameter using the = operator in the function definition.

Example of Default Parameters
# Set the default value of the name parameter to "CodeFriend"
def greet(name="CodeFriend"):
return f"Hello, {name}!"

# "Hello, CodeFriend!"
print(greet())

In the code above, the greet function has a name parameter, with the default value set to "CodeFriend" using name="CodeFriend".


Example of Using Default Parameters

With default parameters, you do not need to provide a value for those parameters during the function call.

Example of Using Default Parameters
def greet(name="CodeFriend"):
return f"Hello, {name}!"

# "Hello, CodeFriend!"
print(greet())

# "Hello, Python!"
print(greet("Python"))

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.