Python Parameters
Learn how to make Python functions flexible and reusable by passing information through parameters and arguments.
What You'll Learn
- Understand parameters and arguments.
- Use positional arguments.
- Use keyword arguments.
- Create functions with multiple parameters.
- Use default parameter values.
- Understand parameter order and rules.
- Pass variables, expressions, lists, and dictionaries.
- Build flexible real-world functions.
What Are Parameters?
A parameter is a variable defined inside a function's parentheses. Parameters allow a function to receive information from the code that calls it.
Without parameters, a function may perform exactly the same operation every time. Parameters make functions flexible.
def greet(name):
print(f"Hello, {name}!")
greet("Ali")
greet("Sara")
Hello, Ali! Hello, Sara!
The function is the same, but the result changes because different values are supplied.
Parameters vs Arguments
These two terms are closely related, but they describe different things.
Parameter
A variable written in the function definition.
Argument
The actual value supplied when the function is called.
def greet(name):
print(f"Hello, {name}!")
greet("Adnan")
Here, name is the parameter and
"Adnan" is the argument.
Positional Arguments
With positional arguments, values are assigned to parameters according to their position.
def introduce(name, age):
print(f"Name: {name}")
print(f"Age: {age}")
introduce("Ali", 25)
Name: Ali Age: 25
Python matches "Ali" with name and
25 with age.
If you reverse the arguments, the values will be assigned to the wrong parameters.
Keyword Arguments
A keyword argument explicitly specifies which parameter should receive a value.
def introduce(name, age):
print(f"Name: {name}")
print(f"Age: {age}")
introduce(age=25, name="Ali")
Name: Ali Age: 25
Notice that the arguments were supplied in a different order, but Python still assigned them correctly because their parameter names were explicitly specified.
Using Multiple Parameters
A function can have several parameters.
def calculate_bill(item, price, quantity):
total = price * quantity
print(f"Item: {item}")
print(f"Total: Rs. {total}")
calculate_bill("Laptop Bag", 2500, 2)
Item: Laptop Bag Total: Rs. 5000
Each parameter represents a piece of information required by the function.
Default Parameters
A parameter can have a default value. If the caller does not provide a value, Python uses the default.
def greet(name="Student"):
print(f"Hello, {name}!")
greet()
greet("Ahmed")
Hello, Student! Hello, Ahmed!
Default parameters are useful when a value is optional or when there is a sensible standard value.
def create_profile(name, country="Pakistan"):
print(f"Name: {name}")
print(f"Country: {country}")
create_profile("Ali")
create_profile("Sara", "Canada")
Required Parameters
A parameter without a default value is normally required.
def add(a, b):
return a + b
print(add(10, 20))
Both a and b must receive values.
Calling add(10) causes Python to raise a
TypeError because b has not
received a value.
Understanding Parameter Order
Python follows specific rules when parameters are defined.
In a simple function, required parameters normally come before parameters that have default values.
def create_user(name, country="Pakistan"):
print(name, country)
A parameter with a default value cannot normally be placed before a required positional parameter in the same parameter list.
def create_user(country="Pakistan", name):
print(name, country)
Python will raise a SyntaxError for this
definition.
Combining Positional and Keyword Arguments
Python allows positional and keyword arguments to be used in the same function call, but positional arguments must come before keyword arguments.
def student(name, age, city):
print(name, age, city)
student("Ali", age=22, city="Multan")
Ali 22 Multan
Once you start supplying arguments by keyword, you cannot place another positional argument after them.
Expressions as Arguments
An argument does not have to be a literal value. You can pass an expression.
def double(number):
return number * 2
result = double(10 + 5)
print(result)
30
Python evaluates 10 + 5 first and then passes
the resulting value to the function.
Passing Variables as Arguments
Variables can be passed directly to functions.
def calculate_total(price, quantity):
return price * quantity
price = 1500
quantity = 4
total = calculate_total(price, quantity)
print(total)
6000
This pattern is extremely common in real applications because data usually comes from variables, user input, databases, or APIs.
Passing Lists and Dictionaries
Functions can receive complex data structures such as lists and dictionaries.
Passing a list
def show_students(students):
for student in students:
print(student)
names = ["Ali", "Sara", "Ahmed"]
show_students(names)
Ali Sara Ahmed
Passing a dictionary
def show_profile(profile):
print(f"Name: {profile['name']}")
print(f"Age: {profile['age']}")
user = {
"name": "Ali",
"age": 25
}
show_profile(user)
Functions can therefore operate on complete collections of data rather than only individual values.
Using None as a Default
Sometimes you need a parameter whose default value represents "no value supplied."
Python commonly uses None for this purpose.
def welcome(name=None):
if name is None:
print("Welcome, Guest!")
else:
print(f"Welcome, {name}!")
welcome()
welcome("Adnan")
Welcome, Guest! Welcome, Adnan!
Good Practice
When checking specifically for the absence of a value,
use is None rather than
== None.
Parameters + Return Values
Parameters become especially powerful when combined with return values.
def calculate_discount(price, percentage):
discount = price * percentage / 100
final_price = price - discount
return final_price
total = calculate_discount(5000, 10)
print(f"Final Price: Rs. {total}")
Final Price: Rs. 4500.0
The parameters provide the input, the function performs the
processing, and return provides the result.
Arguments → Parameters → Processing → Return Value
Real-World Examples
Example 1 — Ecommerce Order
def calculate_order(product, price, quantity=1):
total = price * quantity
return {
"product": product,
"quantity": quantity,
"total": total
}
order = calculate_order("Shoes", 4500, 2)
print(order)
{'product': 'Shoes', 'quantity': 2, 'total': 9000}
Example 2 — Employee Salary
def calculate_salary(basic, allowance=5000):
return basic + allowance
salary = calculate_salary(50000)
print(f"Salary: Rs. {salary}")
Salary: Rs. 55000
Example 3 — Temperature Converter
def celsius_to_fahrenheit(celsius):
return (celsius * 9 / 5) + 32
temperature = celsius_to_fahrenheit(30)
print(f"Temperature: {temperature}°F")
Temperature: 86.0°F
Common Parameter Mistakes
1. Providing too few arguments
def add(a, b):
return a + b
add(10)
Python raises a TypeError because
b is missing.
2. Providing too many arguments
def add(a, b):
return a + b
add(10, 20, 30)
The function accepts two parameters, but three arguments were supplied.
3. Positional argument after keyword argument
def student(name, age):
print(name, age)
student(name="Ali", 25)
This produces a SyntaxError. Positional
arguments must come before keyword arguments.
4. Repeating the same argument
def student(name, age):
print(name, age)
student("Ali", name="Ahmed", age=25)
The parameter name receives more than one value,
which causes a TypeError.
5. Confusing parameter names
Use clear parameter names such as
price, quantity,
student_name, or
customer_email.
Practice Exercises
Exercise 1 — Greeting
Create a function that accepts a name and prints a personalized greeting.
Exercise 2 — Addition
Create a function that accepts two numbers and returns their sum.
Exercise 3 — Rectangle
Create a function with length and
width parameters that returns the area.
Exercise 4 — Default Country
Create a function with country="Pakistan" as
a default parameter.
Exercise 5 — Discount
Create a function that accepts price and discount percentage and returns the final price.
Exercise 6 — Student Profile
Create a function that accepts a student's name, age, and city and displays the information.
Exercise 7 — List Average
Create a function that receives a list of numbers and returns the average.
Mini Challenge — Sales Invoice Generator
Build a small reusable sales invoice system using functions and parameters.
🎯 Your Mission
Create a function that receives:
- Product name
- Price
- Quantity
- Discount percentage
The function should calculate the subtotal, discount, and final amount.
def create_invoice(product, price, quantity=1, discount=0):
subtotal = price * quantity
discount_amount = subtotal * discount / 100
final_amount = subtotal - discount_amount
print("----- INVOICE -----")
print(f"Product: {product}")
print(f"Price: Rs. {price}")
print(f"Quantity: {quantity}")
print(f"Subtotal: Rs. {subtotal}")
print(f"Discount: Rs. {discount_amount}")
print(f"Final Amount: Rs. {final_amount}")
create_invoice(
"Python Course",
5000,
quantity=2,
discount=10
)
----- INVOICE ----- Product: Python Course Price: Rs. 5000 Quantity: 2 Subtotal: Rs. 10000 Discount: Rs. 1000.0 Final Amount: Rs. 9000.0
Add a tax parameter and calculate the final amount after both discount and tax.
Quick Knowledge Check
1. What is a parameter?
2. What is an argument?
3. Which type of argument uses parameter names?
4. What happens if a required argument is missing?
5. Which is a valid default parameter?
6. Which argument order is valid?
7. Which value commonly represents "no value"?
8. Which is the best check for None?
Lesson Summary
Parameters make functions flexible by allowing them to receive information from the code that calls them.
Key Takeaways
- Parameters are variables defined by a function.
- Arguments are values supplied during a function call.
- Positional arguments are matched according to order.
- Keyword arguments explicitly identify parameters.
- Functions can have multiple parameters.
- Default parameters provide fallback values.
- Required parameters must receive values.
- Positional arguments must come before keyword arguments.
- Lists and dictionaries can be passed to functions.
-
Noneis commonly used to represent the absence of a value.
You now have a much stronger understanding of how information moves into functions. In the next lesson, we will focus specifically on one of the most important parts of functions: return values.
Ready for Lesson 30?
Mark this lesson complete when you are comfortable using positional arguments, keyword arguments, and default parameters.