We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Python Default Parameters: How They Work

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

In Python you can specify a default value for a function parameter. It's useful when a function has parameters that are "optional." You can specify a default value in case the caller doesn't provide one.

All the content from our Boot.dev courses are available for free here on the blog. This one is from the "Functions" chapter of Learn Python for Beginners. If you want to try the far more immersive version of the course, do check it out!

How to Set a Default Parameter

A default value is created by using the assignment (=) operator in the function signature.

def get_greeting(email, name="there"):
    print("Hello", name + ", welcome! You've registered your email:", email)

You can provide the optional argument normally:

get_greeting("[email protected]", "Lane")
# Hello Lane, welcome! You've registered your email: [email protected]

Or leave it out:

get_greeting("[email protected]")
# Hello there, welcome! You've registered your email: [email protected]

If the second parameter is omitted, the default "there" value will be used in its place.

Optional Parameters Come After Required Parameters

For this structure to work, optional parameters (the ones with defaults) must come after all required parameters. In the example above, email is required and comes first. name has a default, so it comes after email. The Python functions guide covers the rest of the syntax for defining and calling functions.

Frequently Asked Questions

How do you set a default parameter in Python?

Use the assignment operator in the function signature, such as def greet(name="there"). Python uses that value when the caller omits the argument.

Do default parameters have to come last in Python?

Parameters with defaults must come after all required positional parameters. Additional optional parameters can follow them.

What happens when you omit an optional argument?

Python uses the default value from the function signature in its place.

Can you override a default parameter?

Yes. Pass another value for that parameter when you call the function.