Introduction 🌟
Function arguments are the values you pass into a function when calling it. Python provides several flexible ways to pass data, making functions powerful and reusable.
Note
1. Positional Arguments 📌
These arguments are passed **in order**, and the position matters.
positional_args.py
def add(a, b):
print(a + b)
add(10, 20) # 302. Keyword Arguments 🧩
Arguments passed using the format key=value. Order does NOT matter.
keyword_args.py
def student(name, age):
print(name, age)
student(age=21, name="Sathish")Note
3. Default Arguments 🧃
Arguments with default values used when no value is provided.
default_args.py
def greet(name="Guest"):
print("Hello", name)
greet() # Hello Guest
greet("Kumar") # Hello KumarNote
4. Arbitrary Positional Arguments (*args) 🔢
Use *args when you don't know how many arguments will be passed. Arguments are received as a tuple.
args_example.py
def add(*nums):
print(nums)
print("Sum =", sum(nums))
add(1, 2, 3, 4)5. Arbitrary Keyword Arguments (**kwargs) 🧰
Use **kwargs to accept multiple named arguments. They are stored in a dictionary.
kwargs_example.py
def display(**info):
print(info)
display(name="Sathish", age=25, country="India")6. Keyword-Only Arguments 🛑➡️🧩
Use * to force certain parameters to be keyword-only.
keyword_only.py
def order(item, *, quantity=1, price=0):
print(item, quantity, price)
order("Book", quantity=2, price=500)
# order("Book", 2, 500) ❌ ErrorNote
7. Positional-Only Arguments ( / ) 📐
Introduced in Python 3.8, / forces arguments to be positional-only.
positional_only.py
def divide(a, b, /):
return a / b
print(divide(10, 2))
# divide(a=10, b=2) ❌ Error8. Combining All Argument Types 🔥
Python allows mixing all styles in a single function.
combined_args.py
def func(a, b=10, *args, c=20, **kwargs):
print(a, b, args, c, kwargs)
func(1, 2, 3, 4, 5, c=30, x=100, y=200)Note
9. Passing Lists, Tuples, Dicts as Arguments 📦
List
list_arg.py
def total(nums):
print(sum(nums))
total([1, 2, 3, 4])Tuple
tuple_arg.py
def show(items):
for i in items:
print(i)
show((10, 20, 30))Dictionary
dict_arg.py
def print_info(info):
for k, v in info.items():
print(k, "=", v)
print_info({"name": "Sathish", "age": 25})10. Argument Unpacking Using * and ** 🎁
unpack_args.py
def add(a, b, c):
print(a + b + c)
nums = [1, 2, 3]
add(*nums) # unpack list
info = {"a": 5, "b": 10, "c": 15}
add(**info) # unpack dictionary11. Real-World Examples 🌍
Flexible Logging Function
logging.py
def log(message, **data):
print("LOG:", message)
for k, v in data.items():
print(f"{k}: {v}")
log("User login", user="Sathish", status="success")Billing System
billing.py
def bill(*items):
total = sum(items)
return total
print(bill(100, 200, 350))Dynamic Settings Loader
settings.py
def configure(**settings):
return settings
print(configure(theme="dark", font="Arial", size=14))Conclusion 🎉
You now clearly understand all types of Python function arguments! Want the next topic? Try Lambda Functions, Recursion, Modules, or OOP (Classes & Objects). Just tell me! 😊