How do you define an optional parameter with a default value in Python?
Experience Level: Junior
Tags: Python
Answer
To define an optional parameter with a default value, add the default value prefixed with an equal sign after the parameter name and type in the method signature.
Example:
def do_something(name: str="Anonymous"):
print("Name: " + name)
Related Python job interview questions
How do you detect if the optional parameter value was set?
Python JuniorHow do you define an optional parameter with no value in Python?
Python JuniorWhat is a data type timedelta good for in Python?
Python JuniorWhat is the datetime data type good for in Python?
Python JuniorWhat is bool data type good for in Python?
Python Junior