Python Simplified

Python Parameters And Arguments Demystified

Python Parameters & Arguments resized

Introduction

What are parameters and arguments? Most of the time we use both the terms interchangeably. And that should be fine. However, it’s important that you understand the difference between the two. In this article, let’s explore both in detail. Along the way, you will learn all you need to know about parameters & arguments in Python. Let’s jump right in.

Parameters & Arguments

Parameters

Parameters are the names that appear in the function definition. In the below example, name, age, and skill are the parameters as they appear in the function definition. 

				
					def my_func(name, age, skill):
     pass
				
			
parameters-arguments-1

Arguments

Arguments are the names that appear in the function call. In the above example, ‘Chetan’, 33, and ‘Python’ are the arguments as they appear when making a call to the function my_func.

				
					my_func('Chetan', 33, 'Python')
				
			

Types of arguments

There are two types of arguments. Let’s explore the syntax now and then in the next section you will understand how it works with examples.

parameters-arguments2

Keyword Arguments

Keyword argument is preceded by identifier/variable in the function call. In the above example, “Chetan” and 33 are the keyword arguments; as you can see they are preceded by identifiers name and age. Keyword arguments follow key=value pairs syntax. It is also called named arguments

				
					def my_func(name, age):
    pass

my_func(name='Chetan', age=33)
				
			

Positional Arguments

Any argument that is not a keyword argument or that doesn’t precede with an identifier is a positional argument. In the below example, chetan and 33 are the positional arguments as they don’t precede by any identifier.

				
					def my_func(name, age):
    pass

my_func('Chetan', 33)
				
			

Types of Parameters

There are 5 different types of parameters. Let’s explore the syntax and understand how it works with examples. 

Positional or keyword

Indicates arguments that can be passed to a function either positionally or as a keyword argument. The positional parameters come first followed by keyword parameters. In the below syntax, pos1 is positional and key1 is the keyword parameter.

				
					def func(pos1, key1=None):
     pass 
				
			
Example

Python’s built-in function print() makes use of positional or keyword parameter as you can see from the below function definition.

parameters-arguments-3

Positional-only

Positional-only parameters indicate that arguments can be passed to function only by position. You can create positional-only parameters using / symbol. All the parameters that come before / are strictly positional-only parameters.

In the below syntax, pos_only1 and pos_only2 are positional-only meaning during the function call we won’t be able to use them as keyword arguments like pos_only1=10, etc. The function call would like to func(10, 20, 30) where 10, 20 correspond to pos_only1 & pos_only2 and 30 can be either positional and keyword parameter.

				
					def func(pos_only1, pos_only2, /, positional_or_keyword):
     pass
				
			
Example

Python’s built-in function len() makes use of positional-only parameter as you can see the function definition in the below screenshot.

parameters-arguments-4

Keyword-only

Keyword-only parameters indicate that arguments can only be passed to function by keywords. You can create keyword-only parameters using * symbol. All the parameters that come after * are strictly keyword-only parameters. 

In the below syntax, pos_only1 & pos_only2 are positional parameters and key_only & key_only2 are keyword-only parameters. This means that both key_only1 & key_only2 can only be used as keyword arguments in the function call.

				
					def func(pos_only1, pos_only2, *, key_only1, key_only2): 
     pass
				
			
Example

Python’s built-in function sorted() makes use of both keyword-only and positional-only parameters as you can see the function definition below:

parameters-arguments-5

Var-positional

This parameter is useful when your application requires an arbitrary number of positional parameters. This is done by appending * to the parameter. The *args holds an arbitrary number of remaining positional arguments. Note that the use of the variable args is not mandatory. You could use any variable name but args has become the defacto. The args return its values as a tuple. 

				
					def func(*args): 
     pass
				
			
Example

Python’s built-in function max() makes use of Var-positional parameter  *args as you can see from the function definition.

parameters-arguments-6

Var-keyword

This parameter is useful when your application requires an arbitrary number of keyword parameters. This is done by appending ** to the parameter. The *kwargs hold an arbitrary number of remaining keyword arguments. Similar to args, the name of the variable kwargs is also not mandatory. The **kwargs returns a dictionary containing parameters and their arguments.

				
					def func(**kwargs): 
     pass
				
			

Example

Python’s built-in function dict() is an example Var-keyword parameter. Refer to the function definition below.

parameters-arguments-7

The below two examples pretty much sum up everything we learned so far about parameters.

parameters-arguments-9

Summary

  • Parameters appear in the function definition and arguments appear in the function call.
  • There are two types of arguments (positional and keyword arguments) and five types of parameters (positional or keyword, positional-only, keyword-only, Var-positional, and Var-keyword).
  • Positional parameters can also have default values which can be specified using keywords.
  • All the parameters that precede / in the function definition are strictly positional (positional-only). 
  • All the parameters that follow * in the function definition are strictly keyword (keyword-only).
  • The *args holds an arbitrary number of remaining positional arguments. 
  • The **kwargs hold an arbitrary number of remaining keyword arguments.
  • Positional arguments must not follow keyword arguments in the function call.
  • No parameter comes after **kwargs in the function definition. It’s the end of all the parameters.

References

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on email
Chetan Ambi

Chetan Ambi

A Software Engineer & Team Lead with over 10+ years of IT experience, a Technical Blogger with a passion for cutting edge technology. Currently working in the field of Python, Machine Learning & Data Science. Chetan Ambi holds a Bachelor of Engineering Degree in Computer Science.
Scroll to Top