
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to pass keyword parameters to a function in Python?
In Python, functions are usually called by passing none, one or more arguments to it. There are two types of arguments that can be used −
Positional arguments
Keyword arguments.
Positional arguments depend on the order they are passed to a function; they are the more common type of arguments used in programming languages. Whereas, keyword arguments are passed as key-value pairs so that the arguments are mapped to a key using the “=” operator which allows the Python interpreter to ignore the positions.
In this article, we will be discussing about keyword parameters/arguments and how to pass them to a function in Python.
Keyword Arguments
Keyword arguments are a special type of arguments that are passed to a function in the form of (key, value) pairs. They are also known as named arguments. When these arguments are passed to a function, the order or position of their occurrence does not matter.
They work similar to the dictionaries in python.
Example
Let us see a small example program which demonstrates how keyword arguments are used in functions. Here, we are defining a function “team” and passing two arguments “name” and “company”.
def team(name, company): print(name, "is working at", company) team(company = "Google", name = 'John')
Output
The output for the program above is given as follows −
John is working at Google
Example
Now, let us interchange the positions of the arguments while calling the function and check whether it affects the output.
def team(name, company): print(name, "is working at", company) team(name = 'John' , company = "Google",)
Output
On executing the program, the output displayed will exactly be the same as the output for the previous program. Hence, the position of these arguments does not matter.
John is working at Google
Passing Multiple Arguments
As we already know, a Python function can accept any number of arguments as long as the corresponding parameters are defined for them.
But, it is not always possible to define the number of parameters in the function based on the values handed in. We typically pass the function a large number of data values as arguments. It is impossible to define 100 variables in a function if we input 100 values.
In such situations, we use Python's predefined special symbols known as *args and **kwargs. By args, we mean positional arguments that are provided to the function, while by kwargs, we mean keyword arguments.
**kwargs
**kwargs allows us to pass the function a flexible number of keyword arguments. For that, we simply use a double asterisk (**) before the parameter or argument name during the function definition and call respectively.
Like args, kwargs is an idiom; we can use any other name as long as we put the (**) symbol before it.
Example
Let’s see an example for **kwargs below. We are creating a function called “person” with all their details passed as a **kwargs argument to it.
def Person(**kwargs): for key, value in kwargs.items(): print(key,"->", value) Person(Name = 'Rahul', Sex = 'Male', Age = 38, City = 'Hyderabad', Mobile = 123456789)
Output
The output for the program above is displayed below. We can see more clearly the data we have printed by using keywords Name, Sex, Age, City, and Mobile in the above program. As a result, the users are not confused and coding time is saved. kwargs are therefore keywords associated with parameter values.
Name -> Rahul Sex -> Male Age -> 38 City -> Hyderabad Mobile -> 123456789
Example
Another example to demonstrate the usage of the **kwargs arguments is given below. Here, we are displayed the output in the dictionary format.
def print_kwargs(**kwargs): print(kwargs) print_kwargs(kwargs_1="Whale", kwargs_2=5, kwargs_3= False, kwargs_4=2.1)
Output
On executing the program above, the output is displayed as follows −
{'kwargs_1': 'Whale', 'kwargs_2': 5, 'kwargs_3': False, 'kwargs_4': 2.1}
- Related Articles
- How to pass optional parameters to a function in Python?
- How can I pass optional or keyword parameters from one function to another in Python?
- How to pass the parameters in the PowerShell function?
- How to pass parameters to a method in C#?
- How to pass reference parameters PHP?
- How to pass Python function as a function argument?
- How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
- How to pass arguments by reference in a Python function?
- How to pass a dictionary as argument in Python function?
- How to pass pointers as parameters to methods in C#?
- How to pass parameters using param array in a C# method?
- How to pass arguments by value in Python function?
- How to pass arguments by reference in Python function?
- How to pass a json object as a parameter to a python function?
- How to pass a Slice to Function in Golang?
