Lecture
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"))
Lessons in this chapter · Functions: Code Blocks for Specific Tasks
- 1. Creating Reusable Code Blocks with Functions
- 2. The Difference Between Parameters and Arguments
- 3. Handling TypeError for Function Parameters
- 4. Multiple-choice quiz
- 5. Understanding and Using Variable-Length Arguments
- 6. How to Assign Default Values to Function Parameters
- 7. Using Keyword Arguments
- 8. Various Function Return Methods
- 9. Coding Quiz - Output Formatting
- 10. Multiple-choice quiz
- 11. Fill-in-the-blank quiz
Quiz
0 / 1
Which of the following 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.
+
*
**
=
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help