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}

Updated on: 24-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements