SyntaxError: positional argument follows keyword argument

In the above 3 cases, we have seen how python can interpret the argument values that are being passed during a function call. Now, let us consider the below example which leads to a SyntaxError.

Python




# function definition
def foo(a, b, c=10):
    print('a =', a)
    print('b =', b)
    print('c =', c)
  
    # call the function
print("Function Call 4")
foo(a=2, c=3, 9)


Output:

File "<ipython-input-40-982df054f26b>", line 7
    foo(a=2, c=3, 9)
                 ^
SyntaxError: positional argument follows keyword argument

Explanation:

In this example, the error occurred because of the way we have passed the arguments during the function call. The error positional argument follows keyword argument means that the if any keyword argument is used in the function call then it should always be followed by keyword arguments. Positional arguments can be written in the beginning before any keyword argument is passed. Here, a=2 and c=3 are keyword argument. The 3rd argument 9 is a positional argument. This can not be interpreted by the python as to which key holds what value. The way python works in this regards is that, it will first map the positional argument and then any keyword argument if present.

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




# function
def calculate_square(num):
    return num * num
  
  
# call the function
result = calculate_square(10)
print(result)


Output

100

The calculate_square() function takes in an argument num which is an integer or decimal input, calculates the square of the number and returns the value.

Similar Reads

Keyword and Positional Arguments in Python

...

SyntaxError: positional argument follows keyword argument

There are two kind of arguments, namely, keyword and positional. As the name suggests, the keyword argument is identified by a function based on some key whereas the positional argument is identified based on its position in the function definition. Let us have a look at this with an example....

How to avoid the error – Conclusion

...