Parameters & Arguments in Python
Parameters aur arguments ka use function ko data provide karne ke liye kiya jata hai. Isse hum same function ko different values ke saath multiple times use kar sakte hain.
1. What is a Parameter?
Parameter ek variable hota hai jo function definition ke andar input receive karne ke liye use hota hai.
def greet(name):
print("Hello", name)
Is example mein name ek parameter hai.
2. What is an Argument?
Function ko call karte waqt jo actual value pass ki jaati hai, usse argument kehte hain.
def greet(name):
print("Hello", name)
greet("Rahul")
Yahan name parameter hai aur "Rahul" argument hai.
Output:
Hello Rahul
3. Parameter vs Argument
| Parameter | Argument |
|---|---|
| Function definition mein hota hai | Function call mein hota hai |
| Input receive karta hai | Actual value provide karta hai |
Example: name |
Example: "Rahul" |
4. Single Parameter
def square(number):
return number * number
result = square(5)
print(result)
Output:
25
5. Multiple Parameters
Function mein multiple parameters bhi define kar sakte hain.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
6. Positional Arguments
Positional arguments mein values ka order parameters ke order ke according match hota hai.
def student(name, course):
print("Name:", name)
print("Course:", course)
student("Amit", "Python")
Output:
Name: Amit
Course: Python
7. Keyword Arguments
Keyword arguments mein hum parameter ka naam explicitly specify karte hain.
def student(name, course):
print("Name:", name)
print("Course:", course)
student(
course="Python",
name="Amit"
)
Output:
Name: Amit
Course: Python
Keyword arguments ka benefit ye hai ki values ka order change karne par bhi correct parameter ko value milti hai.
8. Default Parameters
Parameter ko default value di ja sakti hai. Agar function call ke time value provide nahi ki gayi, toh default value use hoti hai.
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 aur keyword arguments ko ek saath use kiya ja sakta hai.
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)
Jab hume pata nahi ho ki function ko kitne arguments milenge, tab *args use kar sakte hain.
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)
Multiple keyword arguments accept karne ke liye **kwargs use kiya ja sakta hai.
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 |
| Tuple ke form mein receive hote hain | Dictionary ke form mein receive hote hain |
Example: add(10, 20, 30) |
Example: student(name="Amit") |
14. CIIT Example 👀🤓
CIIT mein student enrollment ke liye function create karte hain.
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)
Yahan b ke liye value missing hai, isliye error aayega.
# Correct
def add(a, b):
return a + b
add(10, 20)
17. Important Rules
- Function parameters ko meaningful naam do.
- Positional arguments ka order important hota hai.
- Keyword arguments parameter name ke saath pass kiye jaate hain.
- Default parameter optional input provide karta hai.
- *args multiple positional arguments handle karta hai.
- **kwargs multiple keyword arguments handle karta hai.
CIIT Learning Point
Parameters aur arguments functions ko flexible aur reusable banate hain. Positional, keyword, default arguments ke saath *args aur **kwargs ka use real-world Python applications mein bahut useful hota hai.
CIIT Practice Tasks
-
Student Function:
student(name, course)function create karo jo student details print kare. -
Calculator:
calculate(a, b, operation)function create karo jo addition, subtraction, multiplication aur division perform kare. -
Default City:
student(name, city="Pune")function create karo. City provide na karne par Pune display hona chahiye. -
*args Practice:
find_total(*numbers)function create karo jo kisi bhi number of values ka total return kare. -
**kwargs Practice:
display_student(**details)function create karo jo student ki multiple details display kare. -
CIIT Fee Calculator:
calculate_fee(fee, discount)function create karo jo discount apply karke final course fee return kare.
Summary :
Parameters function definition mein input receive karte hain, jabki arguments function call ke waqt actual values hoti hain. Python mein positional arguments, keyword arguments, default parameters, *args aur **kwargs ka use karke flexible aur reusable functions create kiye ja sakte hain.