Default parameters in a Python Function
In this lesson, you will learn about the default parameters in python, their usage and how to pass multiple default parameters to a function in python.
Arguments of a Function
Arguments or parameters are values passed to a function inside the parenthesis. A function can have multiple arguments in it, separated by a comma.
Types of Arguments
The following are the types of arguments that can be passed in function.
- Default arguments
- Keyword arguments
Default Parameters or Arguments in Python
The default parameters or arguments are default values that indicate the function will take that particular value when no argument or parameter is passed during the function call.
Using the assignment operator (=), you can assign a default value to an argument. A function can have an infinite number of default arguments but once we hit a default argument, all the arguments to their right should have default values.
Example:
def default_parameters(firstname, lastname ='Academy.', learning ='Python Programming'): print('Welcome to',firstname, lastname, 'You are learning',learning) default_parameters(firstname='Developer Publish')
Keyword Parameters or Arguments in Python
Here, the arguments are denoted by the keywords. The order of the keyword arguments can vary in unction. A function can have multiple keyword arguments. Just prefix the keyword with (*) inside the function parenthesis.
Example
def function_name(*kwargs): statements
How to pass multiple arguments or parameters to a function in python?
Arguments or parameters are values passed to a function in python. A function can have multiple arguments in it separated by a comma (,). You need to specify the arguments in the function by mentioning it inside the parenthesis. If you do not know the solid count of arguments, you can add a ‘*’ before the arguments.
Syntax:
def function_namen(*args): statements
Example
Input:
[gamipress_button type=”submit” label=”Run Code Snippet” onclick=”window.open(‘https://coderseditor.com/?id=1395′,’_blank’)”]
def multiple_arguments(*names): for name in names: print("Howdy", name,"Welcome to Developer Publish Academy") multiple_arguments("Raj", "Shyam", "Jim", "Nathan")
Output:
Howdy Raj Welcome to Developer Publish Academy Howdy Shyam Welcome to Developer Publish Academy Howdy Jim Welcome to Developer Publish Academy Howdy Nathan Welcome to Developer Publish Academy