Introduction
Arguments allow you to send information to a function. They make functions more flexible because the same function can work with different values every time it is called.
Positional Arguments
The values are matched to parameters according to their order.
def greet(name, age):
print(name, "is", age, "years old")
greet("Alice", 25)
Output:
Alice is 25 years old
Keyword Arguments
You can specify which parameter receives each value by using the parameter name.
def greet(name, age):
print(name, "is", age, "years old")
greet(age=30, name="John")
Default Parameter Values
A parameter can have a default value that is used when no argument is provided.
def greet(name="Guest"):
print("Welcome", name)
greet()
greet("Alice")
Multiple Default Parameters
def calculate(price, quantity=1):
print(price * quantity)
calculate(50)
calculate(50, 4)
Using *args
The *args syntax allows a function to receive any number of positional arguments.
def total(*numbers):
print(sum(numbers))
total(2, 4)
total(2, 4, 6, 8)
Using **kwargs
The **kwargs syntax accepts any number of keyword arguments.
def person(**details):
print(details["name"])
print(details["country"])
person(name="Alice", country="Mauritius")
Argument Unpacking
Lists and dictionaries can be unpacked when calling functions.
def add(a, b):
print(a + b)
numbers = [5, 7]
add(*numbers)
def person(name, age):
print(name, age)
data = {
"name": "John",
"age": 40
}
person(**data)
Best Practices
- Use meaningful parameter names.
- Keep the number of parameters reasonable.
- Use keyword arguments for readability.
- Use default values when appropriate.
- Use
*argsand**kwargsonly when flexibility is needed.
Real-World Example
def order(product, quantity=1, discount=0):
total = product * quantity
total -= discount
return total
print(order(100))
print(order(100, 3))
print(order(100, 3, 20))
Summary
Function arguments make Python functions powerful and reusable. Whether using positional arguments, keyword arguments, default values, or advanced techniques like *args and **kwargs, understanding arguments will help you write flexible and professional Python programs.
Examples
The following examples help reinforce the concepts explained in this lesson.
<!DOCTYPE html>
<html>
<head>
<title>My First Page</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
💡 Pro Tip
Practice every concept immediately after reading it. Learning by doing is the fastest way to master HTML.
⚠ Common Mistake
Do not simply copy code examples. Type them yourself and experiment with small changes.
Best Practices
- Write clean and readable HTML.
- Indent your code consistently.
- Use semantic HTML elements.
- Validate your HTML regularly.
- Test your pages in multiple browsers.
Frequently Asked Questions
Why should I learn HTML first?
HTML is the foundation of every website. Once you understand HTML, learning CSS and JavaScript becomes much easier.
Is HTML difficult?
No. HTML is considered one of the easiest web technologies to learn, making it an excellent starting point for beginners.