Open In App

Keyword and Positional Argument in Python

Last Updated : 14 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python provides different ways of passing the arguments during the function call from which we will explore keyword-only argument means passing the argument by using the parameter names during the function call.is 

Types of arguments

  • Keyword-only argument
  • Positional-only argument

Difference between the Keyword and Positional Argument

Keyword-Only Argument

Positional-Only Argument

Parameter Names are used to pass the argument during the function call.Arguments are passed in the order of parameters. The order defined in the order function declaration.

Order of parameter Names can be changed to 

pass the argument(or values).

Order of values cannot be changed to avoid 

the unexpected output.

Syntax : – FunctionName(paramName = value, ...)Syntax :-FunctionName(value1, value2, value3,....)

Keyword-Only Arguments

Keyword-only arguments mean whenever we pass the arguments(or value) by their parameter names at the time of calling the function in Python in which if you change the position of arguments then there will be no change in the output.

Benefits of using Keyword arguments over positional arguments

  • On using keyword arguments you will get the correct output because the order of argument doesn’t matter provided the logic of your code is correct. But in the case of positional arguments, you will get more than one output on changing the order of the arguments.

Let’s see the example for the keyword-only argument as explained below:

Example

So now, we will call the function by using the keyword-only arguments(or by using the parameter names) in two ways and In both cases, we will be getting the correct output but not necessarily in the positional argument. Here we have used argument naming to declare the value. This is called the keyword-only argument. Let’s assume we have a function that tells the name and age of the individual(or person) as defined below:

Python3

def nameAge(name, age):
    print("Hi, I am", name)
    print("My age is ", age)
 
nameAge(name="Prince", age=20)
 
nameAge(age=20, name="Prince")

                    

Output
Hi, I am Prince
My age is  20
Hi, I am Prince
My age is  20

Positional-Only Arguments

Position-only arguments mean whenever we pass the arguments in the order we have defined function parameters in which if you change the argument position then you may get the unexpected output. We should use positional Arguments whenever we know the order of argument to be passed. So now, we will call the function by using the position-only arguments in two ways and In both cases, we will be getting different outputs from which one will be correct and another one will be incorrect.

Let’s see the examples for the positional-only Arguments as explained below : –

Example 1

During the function call, we have used the Position such that the first argument(or value) will be assigned to name and the second argument(or value) will be assigned to age. By changing the position or in case you forgot the order of the position then the values can be used at the wrong places as we can see in the below example of Case-2 that 20 has been assigned to the name and Prince has been assigned to age.

Python3

def nameAge(name, age):
    print("Hi, I am", name)
    print("My age is ", age)
 
# You will get correct output because argument is given in order
print("Case-1:")
nameAge("Prince", 20)
# You will get incorrect output because argument is not in order
print("\nCase-2:")
nameAge(20, "Prince")

                    

Output
Case-1:
Hi, I am Prince
My age is  20

Case-2:
Hi, I am 20
My age is  Prince

Example 2

During the calling of the function, we have used the position to pass the argument(or value) such that first value will be assigned to a and second value will be assigned to b. But in the result2 you will getting the incorrect output because in the function declaration b should be subtracted from the a but in result2 a is been subtracted from b because of the swapped position.

Python3

def minus(a, b):
    return a - b
 
 
a, b = 20, 10
result1 = minus(a, b)
print("Used Positional arguments:", result1)
 
# you will get incorrect output because
# expected was (a-b) but you will be getting (b-a)
# because of swapped position of value a and b
 
result2 = minus(b, a)
print("Used Positional arguments:", result2)

                    

Output
Used Positional arguments: 10
Used Positional arguments: -10

Note: We should use the positional argument if we know the order of arguments or before using read the function you use and know the order of arguments to be passed over there otherwise you can get the wrong output as you can see in above-explained example for positional Argument.



Similar Reads

How to Fix: SyntaxError: positional argument follows keyword argument in Python
In this article, we will discuss how to fix the syntax error that is positional argument follows keyword argument in Python An argument is a value provided to a function when you call that function. For example, look at the below program - Python Code # function def calculate_square(num): return num * num # call the function result = calculate_squa
3 min read
Python | Positional Index
This article talks about building an inverted index for an information retrieval (IR) system. However, in a real-life IR system, we not only encounter single-word queries (such as "dog", "computer", or "alex") but also phrasal queries (such as "winter is coming", "new york", or "where is kevin"). To handle such queries, using an inverted index is n
5 min read
Python | Get positional characters from String
Sometimes, while working with Python strings, we can have a problem in which we need to create a substring by joining the particular index elements from a string. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute method in which this task can be performed. In this, we run a loop over the indices l
5 min read
Python - Remove positional rows
Given a Matrix, the task is to write a Python program to remove rows that have certain positions. Example: Input: test_list = [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]; idx_lis = [1, 2, 5] Output: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]] Explanation: 1st, 2nd and 5th rows are removed. Input: test_list = [[3,
7 min read
Positional-only Parameter in Python3.8
Python introduces the new function syntax in Python3.8.2 Version, Where we can introduce the / forward slash to compare the positional only parameter which comes before the / slash and parameters that comes after * is keyword only arguments. Rest of the arguments that are come between / and * can be either positional or keyword type of argument. Th
2 min read
How to Fix – Indexerror: Single Positional Indexer Is Out-Of-Bounds
While working with Python, many errors occur in Python. IndexError: Single Positional Indexer is Out-Of-Bounds occurs when we are trying to access the elements and that element is not present inside the Pandas DataFrame. In this article, we will see how we can fix IndexError: single positional indexer is out-of-bounds error in Python. What is Index
2 min read
Least Astonishment and the Mutable Default Argument in Python
The principle of Least Astonishment (also known as least surprise) is a design guideline that states that a system should behave the way it is intended to behave. It should not change its behavior in an unexpected manner to astonish the user. By following this principle, designers can help to create user interfaces that are more user-friendly and l
3 min read
Command-Line Option and Argument Parsing using argparse in Python
Command line arguments are those values that are passed during the calling of the program along with the calling statement. Usually, python uses sys.argv array to deal with such arguments but here we describe how it can be made more resourceful and user-friendly by employing argparse module. Python Argparse ModuleThe 'argparse' module in Python hel
5 min read
Pass list as command line argument in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module. sys Module A module is a file containing Python definitions and statements. The sys module provides functi
3 min read
Passing function as an argument in Python
A function can take multiple arguments, these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg