0
0
Pythonprogramming~5 mins

Positional arguments in Python

Choose your learning style9 modes available
Introduction

Positional arguments let you send information to a function in order. The function uses these values to work.

When you want to give a function specific values to use in order.
When the order of information matters, like giving a name then an age.
When calling simple functions that expect a fixed number of inputs.
When you want to keep your code easy to read by following the function's expected order.
Syntax
Python
def function_name(arg1, arg2, arg3):
    # use arg1, arg2, arg3 inside

function_name(value1, value2, value3)

Arguments are matched to parameters by their position, first to first, second to second, and so on.

If you change the order of values when calling, the function will get different data.

Examples
This sends "Alice" as the first argument and 30 as the second. The function prints a greeting.
Python
def greet(name, age):
    print(f"Hello {name}, you are {age} years old.")

greet("Alice", 30)
The function adds two numbers given in order and returns the sum.
Python
def add(x, y):
    return x + y

result = add(5, 3)
print(result)
Here, "dog" is the first argument and "Buddy" is the second, matching the parameters.
Python
def describe_pet(animal, name):
    print(f"I have a {animal} named {name}.")

describe_pet("dog", "Buddy")
Sample Program

This program defines a function that takes three positional arguments and prints a sentence using them.

Python
def introduce(first_name, last_name, age):
    print(f"My name is {first_name} {last_name} and I am {age} years old.")

introduce("John", "Doe", 25)
OutputSuccess
Important Notes

Make sure to give the arguments in the exact order the function expects.

If you give fewer or more arguments than the function needs, Python will show an error.

You can mix positional arguments with keyword arguments, but positional ones must come first.

Summary

Positional arguments send values to a function in the order the function expects.

The order of arguments is important and changes how the function works.

They are simple and common for passing data to functions.