?
How can you make a parameter optional in a Python function?
Answer & Solution
Correct Answer:
Option
B
In Python, you can make a parameter optional by assigning it a default value in the function definition.
When a default value is specified, the parameter becomes optional, and the function can be called without explicitly passing a value for it.
For example:
Here, the parameter name is optional because it has a default value of "Guest".
Note: If no value is provided when the function is called, the default value will be used.
When a default value is specified, the parameter becomes optional, and the function can be called without explicitly passing a value for it.
For example:
def greet(name="Guest"):
print(f"Hello, {name}!")
Here, the parameter name is optional because it has a default value of "Guest".
Note: If no value is provided when the function is called, the default value will be used.
Join the Discussion
Login to post a comment or share your explanation.
Login