Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 16 Advanced Functions in Python

Updated
2 min read
S

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 in key=value form.

  • 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 ✨

  1. Create a function average(*numbers) that returns the average of multiple numbers.

  2. Create a function profile(**info) that prints out a person’s name, age, and hobby.

  3. Create a function calculator(op, *nums) that:

    • If op = "add" → returns the sum of all numbers

    • If op = "mul" → returns the product of all numbers

Python Basics

Part 16 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 17 Local vs Global Variables & Scope

01. Local Variables Declared inside a function Exist only while the function is running def show_number(): x = 10 # Local variable print("Inside function:", x) show_number() # print(x) # Error! (x is not defined outside the function)...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.