What Is a Positional Argument in Python

Positional arguments in Python

A positional argument in Python means the argument’s position matters in a function call.

You have likely used positional arguments without noticing.

For instance:

# This function takes two positional arguments 'name' and 'age'.
# These arguments are expected to be given in this order.
def info(name, age):
    print(f"Hi, my name is {name}. I am  {age * 365.25} days old.")

# This works
info("Alice", 23.0)

# This doesn't
info(23.0, "Alice")

What Are the Argument Types in Python

Python supports 5 types of arguments:

  1. Positional arguments.
  2. Keyword arguments.
  3. Any number of positional arguments.
  4. Any number of keyword arguments.
  5. Default arguments.

In this guide, we are taking a look at positional arguments and keywords arguments.

  • Positional argument means that the argument must be provided in a correct position in a function call.
  • With keyword arguments, the position does not matter, because a keyword is labeled with a keyword.

Positional Arguments in Python

A positional argument in Python is an argument whose position matters in a function call.

call_func(arg1, arg2, arg3)

Let’s define a function that shows info about a person given the name and age:

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

Now let’s call this function with two positional arguments name and age.

info("Alice", 23.0)

Result:

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

Why positional? Because the order of the arguments matters.

If you call this function with the same arguments in the wrong order, you get an error:

info(23.0, "Alice")

Output:

Traceback (most recent call last):
  File "example.py", line 5, in <module>
    info(23.0, "Alice")
  File "example.py", line 3, in info
    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'

Now it treats age as a name and the name as age.

Keyword Arguments in Python

A keyword argument in Python means that a function argument has a name label.

call_func(arg_name=arg1)

Let’s go back to the example of the previous section.

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

As you saw, this function expects two positional arguments name and age.

This means the order matters.

But you can also pass the arguments in as keyword arguments.

This means you name the arguments when you call the function. Let me show you what this means:

info(age=23.0, name="Alice")

Output:

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

As you can see, the argument order does not matter anymore. This is because you gave the arguments as keyword arguments. This way the interpreter knows which one of the arguments is age and which one the name.

Conclusion

Today you learned what is a positional argument in Python.

A positional argument means its position matters in a function call.

A keyword argument is a function argument with a name label. Passing arguments as keyword arguments means order does not matter.

Thanks for reading.

Happy coding!

Further Reading

  • Any number of positional arguments
  • Any number of keyword arguments.
  • Share on facebook
    Share on twitter
    Share on linkedin

    Leave a Comment

    Your email address will not be published.