How do you define an optional parameter with no value in Python?

Experience Level: Junior
Tags: Python

Answer

To define an optional parameter with a no value, use None as the default value of the parameter in the method signature.

Example:

def do_something(name: str=None):
    print("Name: " + name)

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself