Skip to content

Positional Arguments in Python

  • by

Positional arguments in Python are a type of function arguments that are passed to a function based on their position or order, rather than by explicitly specifying their names. They are the most common type of arguments used in function definitions and function calls.

When defining a function that accepts positional arguments, you declare them inside the parentheses of the function’s signature, separated by commas. Here’s the syntax:

def function_name(arg1, arg2, arg3, ...):
    # Function body
    # Code that uses the arguments

When calling the function, you provide the values for the positional arguments in the same order as they are defined. Here’s the syntax for calling a function with positional arguments:

function_name(value1, value2, value3, ...)
  • function_name is the name of the function you are defining.
  • arg1, arg2, arg3, ... (value1, value2, value, …) are the names you choose for the positional arguments. You can have any number of positional arguments, separated by commas.

They are called “positional” because their values are determined by their position or order in the function’s definition and call.

Positional arguments in Python example

Here’s an example that demonstrates the use of positional arguments in Python:

def greet(name, age):
    print("Hello, " + name + "! You are " + str(age) + " years old.")

greet("Alice", 25)
greet("Bob", 30)


Positional Arguments in Python

Here’s an example with a default value for the age argument:

def greet(name, age=30):
    print("Hello, " + name + "! You are " + str(age) + " years old.")

greet("Bob")  # Hello, Bob! You are 30 years old.
greet("Charlie", 40)  # Hello, Charlie! You are 40 years old.

Last example

def add_numbers(a, b, c):
    result = a + b + c
    return result

sum_result = add_numbers(1, 2, 3)
print(sum_result)  # Output: 6

Do comment if you have any doubts or suggestions on this Python Function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *