Python functions become powerful when they can accept data from outside. This is done using function arguments. In this lecture, we will clearly understand every type of argument with simple explanations and examples.
1. What Are Function Arguments?
Functions in Python can receive information from the caller. This information is passed as arguments inside the parentheses () when calling a function.
Why arguments are important?
- They make functions reusable
- They allow functions to work with different data
- They make programs dynamic instead of fixed
Example
def my_function(fname):
print(fname + " Thapa")
my_function("Subin")
my_function("Ayush")
my_function("Amor")
my_function("Yubraj")
my_function("Saubhagya")
Explanation:
fname→ parameter (defined inside the function)- Values like
"Subin","Ayush"→ arguments (passed while calling)
2. Parameters vs Arguments
Many beginners get confused between parameters and arguments.
| Term | Meaning |
|---|---|
| Parameter | Variable written inside the function definition |
| Argument | Actual value sent to the function when calling it |
Example
def my_function(name): # parameter
print("Hello", name)
my_function("Nikilesh") # argument
3. Number of Arguments
A function must be called with the exact number of arguments it expects.
Example
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Subin", "Thapa") # Correct
my_function("Subin") # Error: missing argument
4. Default Parameter Values
Python allows parameters to have default values.
- If no argument is passed → default value is used
- If argument is passed → default value is overridden
Example 1
def my_function(name="friend"):
print("Hello", name)
my_function("Babina")
my_function()
Example 2
def my_function(district="Dhading"):
print("I am from", district)
my_function("Kathmandu")
my_function("Pokhara")
my_function()
my_function("Chitwan")
Important Rule:
Default parameters must always come after non-default parameters.
5. Keyword Arguments
Arguments can be passed using key = value format.
Benefits
- Order does not matter
- Code becomes more readable
Example
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function(animal="dog", name="Buddy")
my_function(name="Buddy", animal="dog")
6. Positional Arguments
When arguments are passed without keywords, Python assigns them based on position.
Example
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function("dog", "Buddy")
⚠️ Order matters
my_function("Buddy", "dog") # Wrong meaning
7. Mixing Positional and Keyword Arguments
You can mix both types, but:
Positional arguments must come first
Correct Example
def my_function(animal, name, age):
print("I have a", age, "year old", animal, "named", name)
my_function("dog", name="Buddy", age=5)
Incorrect Example
my_function(name="Buddy", "dog", age=5) # Error
8. Passing Different Data Types as Arguments
Python functions can accept any data type:
- string
- integer
- float
- list
- tuple
- dictionary
Example: Passing a List
def my_function(fruits):
for fruit in fruits:
print(fruit)
my_fruits = ["apple", "banana", "cherry"]
my_function(my_fruits)
Example: Passing a Dictionary
def my_function(person):
print("Name:", person["name"])
print("Age:", person["age"])
my_person = {"name": "Emil", "age": 25}
my_function(my_person)
9. Return Values
A function can send data back using the return keyword.
Example
def my_function(x, y):
return x + y
result = my_function(5, 3)
print(result)
10. Returning Different Data Types
Functions can return any type of data.
Returning a List
def my_function():
return ["apple", "banana", "cherry"]
fruits = my_function()
print(fruits)
Returning a Tuple
def my_function():
return (10, 20)
x, y = my_function()
print(x, y)
11. Positional-Only Arguments (Python 3.8+)
Use / to force parameters to be positional-only.
Example
def my_function(name, /):
print("Hello", name)
my_function("Emil") # Valid
my_function(name="Emil") # Error
12. Keyword-Only Arguments
Use * to force parameters to be keyword-only.
Example
def my_function(*, name):
print("Hello", name)
my_function(name="Babina")
my_function("Babina") # Error
13. Combining Positional-Only and Keyword-Only Arguments
You can combine both / and * in a single function.
Example
def my_function(a, b, /, *, c, d):
return a + b + c + d
result = my_function(5, 10, c=15, d=20)
print(result)
Explanation:
a,b→ positional-onlyc,d→ keyword-only
Challenges: Day 2
Challenge 1
Create a function that accepts name and age (default = 18).
def person_info(name, age=18):
print(f"Name: {name}, Age: {age}")
person_info("Subin")
person_info("Subin", 20)
Challenge 2
def student_report(name, marks, grade="A"):
print("Name:", name)
print("Marks:", marks)
print("Grade:", grade)
student_report("Subin", 85, "A+")
student_report("Subin", 85)
Challenge 3
def employee_info(name, /, *, department, salary=30000):
print("Employee:", name)
print("Department:", department)
print("Salary:", salary)
employee_info("Subin", department="IT")
employee_info("Subin", department="Finance", salary=50000)
For more lectures like this, visit my website and don’t forget to subscribe to my YouTube channel: Coding with Subin