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"))
Mission
0 / 1
What is the most appropriate operator to fill in the blank?
In Python, you can set default values for function parameters, and use the operator to set default parameters.
+
*
**
=
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Generate
Execution Result