Types of Arguments in Python
In Python, when we call a function, we pass values (called arguments) to it. These arguments can be passed in different ways depending on the situation.
There are mainly 4 types of arguments in Python:
- Positional Arguments
- Keyword Arguments
- Default Arguments
- Variable-Length Arguments (*args, **kwargs)
1. Positional Arguments
In positional arguments, the values are passed to the function in the **same order** as the parameters are defined. This is the most common way of passing data into functions.
The position matters — the **first value** goes to the **first parameter**, the **second value** to the **second parameter**, and so on.
def student_info(name, grade):
print("Student Name:", name)
print("Grade:", grade)
student_info("Angel", "A")
Output:
Student Name: Angel
Grade: A
Explanation:
- The function
student_info()has two parameters:nameandgrade. - When we call it as
student_info("Angel", "A"), the first argument"Ananya"is assigned toname. - The second argument
"A"is assigned tograde. - This method depends on the correct order. If you change the order, the meaning changes.
2. Keyword Arguments
In keyword arguments, we pass values by **mentioning the parameter names explicitly**. Here, the **order doesn’t matter** because Python matches values using the parameter names.
student_info(grade="A", name="Angel") # calling the previous function
Output:
Student Name: Angel
Grade: A
Explanation:
- Even though the order is reversed, Python knows that
grade="A"should go to thegradeparameter andname="Angel"toname. - This makes the code more **readable** and **less error-prone**, especially when a function has many parameters.
- Also helpful when we only want to pass a few parameters and skip the rest (if they have default values).
3. Default Arguments
When we provide a default value to a parameter, we can skip it while calling.
For detailed examples and explanation, Click here to continue reading.
def greet(name, age=18):
print("Name:", name)
print("Age:", age)
greet("Peehu")
Output:
Name: Peehu
Age: 18
- If
ageis not provided, it takes default value18.
4. Variable-Length Arguments
*args → Non-keyword variable-length arguments
For detailed examples and explanation, Click here to continue reading.
def add(*numbers):
total = 0
for n in numbers:
total += n
print("Sum:", total)
add(5, 10, 15)
Output:
Sum: 30
**kwargs → Keyword variable-length arguments
def show_details(**info):
for key, value in info.items():
print(key + ":", value)
show_details(name="Devanshi", age=20)
Output:
name: Devanshi
age: 20
Explanation:
*argscollects extra positional arguments in a tuple.**kwargscollects extra keyword arguments in a dictionary.
Summary Table:
| Type | Description | Example |
|---|---|---|
| Positional | Based on order of arguments | greet("A", 20) |
| Keyword | Based on parameter names | greet(age=20, name="A") |
| Default | Function has default values | def f(a, b=10) |
| *args | Many positional values | add(1, 2, 3) |
| **kwargs | Many keyword values | show(name="A") |