Hackforge Academy

Category: python

Understanding *args

Published on 27 Oct 2026

Explanation

*args collects extra positional arguments into a tuple.

Code:

def add(*args): return sum(args)
print(add(10, 20, 30))

Explanation

**kwargs collects extra keyword arguments into a dictionary.

Code:

def show(**kwargs): print(kwargs)
show(name='Arun', age=25)

Explanation

A function can accept extra positional and keyword arguments in one signature.

Code:

def display(*args, **kwargs):
    print(args)
    print(kwargs)
display(1, 2, name='Arun')

Explanation

Use * to unpack an iterable into positional arguments and ** to unpack a mapping into keyword arguments.

Code:

def greet(name, message): print(message, name)
greet(*['Arun', 'Hello'])
greet(**{'name': 'Priya', 'message': 'Welcome'})

Explanation

Flexible arguments are useful for wrappers and utility functions that forward arguments to another callable.

Code:

def log(level, *messages, **meta):
    print(level, ' '.join(messages), meta)
log('INFO', 'User', 'logged in', user_id=101)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community