Python Basics — Day 16 Advanced Functions in Python
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
01. Default Parameters
You can assign default values to parameters.
If no argument is passed, the default value is used.
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Hello, Guest!
greet("Sabin") # Hello, Sabin!
02. Variable-Length Arguments (*args)
By adding
*before a parameter, you can pass multiple arguments.Inside the function, they are received as a tuple.
def add_all(*numbers):
total = 0
for n in numbers:
total += n
return total
print(add_all(1, 2, 3)) # 6
print(add_all(10, 20, 30, 40)) # 100
03. Keyword Variable Arguments (**kwargs)
By adding
**before a parameter, you can pass multiple values inkey=valueform.Inside the function, they are received as a dictionary.
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Sabin", age=30, hobby="Coding")
# name: Sabin
# age: 30
# hobby: Coding
04. Mixing Parameters
You can use **default parameters + *args + kwargs together.
Order: normal parameters → args → *kwargs.
def show_info(title, *args, **kwargs):
print("Title:", title)
print("args:", args)
print("kwargs:", kwargs)
show_info("Student Info", "Tom", "Anna", grade="A", age=20)
# Title: Student Info
# args: ('Tom', 'Anna')
# kwargs: {'grade': 'A', 'age': 20}
05. Practice Examples
Example 1: Default Parameters
def power(base, exp=2):
return base ** exp
print(power(3)) # 9 (square)
print(power(3, 3)) # 27 (cube)
Example 2: Using *args
def multiply_all(*nums):
result = 1
for n in nums:
result *= n
return result
print(multiply_all(2, 3, 4)) # 24
Example 3: Using **kwargs
def introduce(**person):
print(f"{person['name']} is {person['age']} years old.")
introduce(name="Sabin", age=30)
06. Mini Challenges ✨
Create a function
average(*numbers)that returns the average of multiple numbers.Create a function
profile(**info)that prints out a person’s name, age, and hobby.Create a function
calculator(op, *nums)that:If
op = "add"→ returns the sum of all numbersIf
op = "mul"→ returns the product of all numbers