Parameters & Arguments in Python
Parameters and arguments are used to provide data to a function. This allows us to use the same function multiple times with different values.
1. What is a Parameter?
A parameter is a variable used inside the function definition to receive input.
def greet(name):
print("Hello", name)
In this example, name is a parameter.
2. What is an Argument?
The actual value passed when calling a function is called an argument.
def greet(name):
print("Hello", name)
greet("Rahul")
Here, name is the parameter and "Rahul" is the argument.
Output:
Hello Rahul
3. Parameter vs Argument
| Parameter | Argument |
|---|---|
| Present in the function definition | Present in the function call |
| Receives input | Provides the actual value |
Example: name |
Example: "Rahul" |
4. Single Parameter
def square(number):
return number * number
result = square(5)
print(result)
Output:
25
5. Multiple Parameters
You can also define multiple parameters in a function.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
6. Positional Arguments
In positional arguments, the order of values matches the order of the parameters.
def student(name, course):
print("Name:", name)
print("Course:", course)
student("Amit", "Python")
Output:
Name: Amit
Course: Python
7. Keyword Arguments
In keyword arguments, we explicitly specify the parameter name.
def student(name, course):
print("Name:", name)
print("Course:", course)
student(
course="Python",
name="Amit"
)
Output:
Name: Amit
Course: Python
The benefit of keyword arguments is that even if the order of values changes, the correct value is assigned to the corresponding parameter.
8. Default Parameters
A parameter can be given a default value. If a value is not provided when calling the function, the default value is used.
def greet(name="Student"):
print("Hello", name)
greet()
greet("Rahul")
Output:
Hello Student
Hello Rahul
9. Multiple Default Parameters
def student_info(
name="Student",
course="Python"
):
print("Name:", name)
print("Course:", course)
student_info()
student_info(
"Amit",
"Python Full Stack"
)
Output:
Name: Student
Course: Python
Name: Amit
Course: Python Full Stack
10. Positional + Keyword Arguments
Positional and keyword arguments can be used together.
def student(name, course, city):
print(name)
print(course)
print(city)
student(
"Rahul",
course="Python",
city="Pune"
)
Output:
Rahul
Python
Pune
11. Arbitrary Positional Arguments (*args)
When we do not know how many arguments a function will receive, we can use *args.
def total(*numbers):
total = 0
for number in numbers:
total += number
return total
print(total(10, 20))
print(total(10, 20, 30, 40))
Output:
30
100
12. Arbitrary Keyword Arguments (**kwargs)
**kwargs can be used to accept multiple keyword arguments.
def student_info(**details):
for key, value in details.items():
print(key, ":", value)
student_info(
name="Amit",
course="Python",
city="Pune"
)
Output:
name : Amit
course : Python
city : Pune
13. *args vs **kwargs
| *args | **kwargs |
|---|---|
| Multiple positional arguments | Multiple keyword arguments |
| Received in the form of a tuple | Received in the form of a dictionary |
Example: add(10, 20, 30) |
Example: student(name="Amit") |
14. CIIT Example 👀🤓
We create a function for student enrollment at CIIT.
def enroll_student(name, course, city="Pune"):
print("Student:", name)
print("Course:", course)
print("City:", city)
enroll_student(
"Sneha",
"Python Full Stack"
)
Output:
Student: Sneha
Course: Python Full Stack
City: Pune
15. Calculation Example 👀🤓
def calculate_fee(fee, discount):
final_fee = fee - (fee * discount / 100)
return final_fee
amount = calculate_fee(35000, 10)
print("Final Fee:", amount)
Output:
Final Fee: 31500.0
16. Common Mistakes
# Wrong
def add(a, b):
return a + b
add(10)
Here, the value for b is missing, so an error will occur.
# Correct
def add(a, b):
return a + b
add(10, 20)
17. Important Rules
- Give function parameters meaningful names.
- The order of positional arguments is important.
- Keyword arguments are passed with the parameter name.
- A default parameter provides optional input.
- *args handles multiple positional arguments.
- **kwargs handles multiple keyword arguments.
CIIT Learning Point
Parameters and arguments make functions flexible and reusable. Positional, keyword, and default arguments, along with *args and **kwargs, are very useful in real-world Python applications.
CIIT Practice Tasks
-
Student Function:
Create astudent(name, course)function that prints student details. -
Calculator:
Create acalculate(a, b, operation)function that performs addition, subtraction, multiplication, and division. -
Default City:
Create astudent(name, city="Pune")function. If the city is not provided, it should display Pune. -
*args Practice:
Create afind_total(*numbers)function that returns the total of any number of values. -
**kwargs Practice:
Create adisplay_student(**details)function that displays multiple student details. -
CIIT Fee Calculator:
Create acalculate_fee(fee, discount)function that applies the discount and returns the final course fee.
Summary :
Parameters receive input in the function definition, while arguments are the actual values passed during a function call. In Python, flexible and reusable functions can be created using positional arguments, keyword arguments, default parameters, *args, and **kwargs.