How To Fix: SyntaxError: Positional Argument follows Keyword Argument in Python

When a function call is made, the values are passed as arguments in the function and the order of the parameters called is same as in the function name. This is called positional arguments. The positional arguments are followed by the keyword arguments.

Let’s write a code to see the positional argument.

Code:

def add(a,b,c):

return (a+b+c)

Explanation:

The above code is used to add three and the parameters a, b and c are the positional arguments. The values in the function are passed by their parameters such as 10 is assigned to a, 20 is assigned to b and 30 is assigned to c.

Consider another example:

Code:

def info(name, age):

    print(f"Hi, my name is {name}. I am  {age * 365.25} days old.")

The above code gives the information of a person and age. The name and the age are the two positional arguments. Let’s call the above function in two ways:

Code:

info("Alice", 23.0)

Output:

Hi, my name is Alice. I am  8400.75 days old.

In the above code, "Alice" is the name and her age 23.0 is called in the function with the same position as the function. But what if the arguments changed.

Code:

info(23.0, "Alice")

Output:

---------------------------------------------------------------------------


TypeError                                 Traceback (most recent call last)


Cell In[7], line 1


----> 1 info(23.0, "Alice")


 


Cell In[6], line 2, in info(name, age)


      1 def info(name, age):


----> 2     print(f"Hi, my name is {name}. I am  {age * 365.25} days old.")


 


TypeError: can't multiply sequence by non-int of type 'float'

Explanation:

In the above code, the name is given as 23.0 and the age is taken as "Alice" by Python. So we get an error, the argument should be passed in the position.

The positional argument is used in Python because the order of the arguments is considered by Python.

An error in the syntax of a coding language that is done by a program is called a syntax error. The syntax error is checked by the software program called compiler and this error should be fixed before the program is compiled.

In Python programming, when working on positional and keyword parameters together in one function, an error might occur SyntaxError: Positional Argument Follows Keyword Arguments. To solve this error, we need to place the argument and variables in their correct place. Argument helps us to provide input values to a program or function that returns output based on the arguments that are passed to the function. In Python, arguments are commonly used in calling functions and methods of the class. A method of the class is simply a function that defines a part of a class. The parameter and argument used interchangeably in the code. A parameter is a variable that is defined in the heading of the function and the argument is a variable that is passed with the help of a function call, which is mapped to a parameter.

The error in Python SyntaxError: positional argument follows keyword argument raises when a positional argument is specified after a keyword argument in a function call.

Code:

>> def fun(num1, num2):

...     print(f"Num 1: {num1}, Num 2: {num2}")

...

>>> fun(num1 = 1,2)

Output:

File "<stdin>", line 1

    fun(num1 = 1,2)

                  ^

SyntaxError: positional argument follows keyword argument

Explanation:

In the above code, a SyntaxError is raised due to the keyword argument num1 =1 followed by the positional argument 2.

This error is raised due to the order for Python to map all the argument variables that are passed to the named parameters.

Let’s See How To Fix This Error:

There are many ways to fix this SyntaxError: positional argument follows keywords. Let’s see how to fix this error.

Using Positional Arguments Followed by Keyword Arguments:

One method is to just specify positional arguments before keyword arguments.

Code:

def fun(num1, num2):

    print(f"Num 1: {num1}, Num 2: {num2}")

fun(1, num2 = 3)

Output:

Num 1: 1, Num 2: 3

Explanation:

In the above code, a function fun is made and the two parameters num1 and num2 are passed into the function and the numbers num1 and num2 are printed. Outside the function, the function is called by passing the argument as 1 and num2 = 3, which means the position is defined for the numbers and the error is resolved. All the keyword arguments should come after the positional arguments.

When working with function, the use of arguments can be beneficial that require positional arguments, and any additional optional arguments can be keyword arguments. Sometimes, more optional arguments are passed and can be impractical without using keyword arguments.

Using Only Positional Arguments:

The use only positional arguments can be used to resolve positional argument syntax errors in the function call.

Code:

def fun(num1, num2):

print(f"Num 1: {num1}, Num 2: {num2}")

fun(1, 3)

Output:

Num 1: 1, Num 2: 3

Explanation:

In the above code, a function fun is made and the num1 and num2 are passed as position arguments and both the numbers are printed and after the function, the function is called by passing the arguments as 1 and 3 only.

Using Only Keyword Arguments:

The error can also be solved by using the keyword arguments only:

Code:

def fun(num1, num2):

    print(f"Num 1: {num1}, Num 2: {num2}")

fun(num1 = 1, num2 = 3)

fun(num2 = 3, num1 = 1)

Output:

Num 1: 1, Num 2: 3


Num 1: 1, Num 2: 3

Explanation:

In the above code a function fun is made and the num1 and num2 is passed as position arguments and both the numbers are printed and after the function, the function is called by passing the number as only keyword argument. In the first function calling the num1 is called first and then the num2 which is the keyword argument and in the second function calling the num2 keyword positional argument is called first and the num1 keyword argument is called after the num1. By doing this the syntax error is resolved in Python.

*args and **kwargs Parameter in Python:

The syntax error can also be resolved by passing and defining the arguments in Python as *args and **kwargs. The asterisk * or double asterisk ** syntax before a parameter name in a function heading helps pass a number of non-keyword or keyword arguments in a function call. The *args stand for argument and **kwargs stands for keyword arguments.

The *args parameter accepts a number of positional arguments, which are input as a tuple object, with each argument value as an element in the tuple. The **kwargs argument accepts any number of keyword arguments that can be input as a dictionary object. The keyword argument name is included as a key in the dictionary.

Code:

def f_args(*args):

    print(args)

f_args(1,2,3,4,5)

Output:

(1, 2, 3, 4, 5)

Explanation:

In the above code, a function f_args is made and the *args argument is passed as input in the function and the args is printed and after the function the function is called with the values and the output of the function as the tuple object.

It can be seen that variable names arg and kwargs are replaced with another variable name; the * and ** are important to define the argument input.

Let's see how to use the *args argument:

Code:

def my_sum(*nums):

     total = 0

     for num in nums:

         total += num

     return total

print(my_sum(1,2,3,4,5))

print(my_sum(3,39,2,30,2))

Output:

15


76

Explanation:

In the above code, the function my_sum is made and *num is passed as input in the function that tells that many numbers can be passed into the my_sum function and the sum of all the numbers is calculated and the total of the number is returned. After the function, the function my_sum is called with the values, and the total number of values is printed.

Code:

def f(*args, **kwargs):

    print(args, kwargs)

f(1, 2, a=3)

Output:

(1, 2) {'a': 3}

Explanation:

In the above code, the function f is made and in the function both the *args and *kwargs are passed into the function as input and both the arguments are printed. After the function, the function is called with the value 1, 2, a = 3. The values 1 and 2 are printed in a tuple object since these values are treated as *args and the value a = 3 is printed as a dictionary since the value is assigned to a and is treated as *kwargs.

Conclusion:

In this article, you have seen how to fix the Python error 'SyntaxError: Positional Arguments Follow Keyword Argument" with the help of various methods, and you have also seen the different types of arguments in Python.