codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

25 / 68 topics
24Python Functions25Function Arguments (*args, **kwargs)26Python Lambda Functions27Python Namespace & Scope (Global/Local)28Python Closures29Python Recursion
Tutorials/Python Programming/Function Arguments (*args, **kwargs)
🐍Python Programming

Function Arguments (*args, **kwargs)

Updated 2026-05-15
15 min read

Function Arguments (*args, **kwargs)

In the previous tutorial, we explored the basics of defining and using functions in Python. Functions are essential building blocks for writing modular and reusable code. However, sometimes you need to write functions that can accept a varying number of arguments. This is where *args and **kwargs come into play.

Introduction

*args and **kwargs allow functions to handle a variable number of positional and keyword arguments respectively. Understanding how to use these features will make your functions more flexible and adaptable to different scenarios.

In this tutorial, we'll cover:

  • Positional arguments
  • Keyword arguments
  • Default parameter values
  • *args for handling multiple positional arguments
  • **kwargs for handling multiple keyword arguments

Core Content

1. Positional Arguments

Positional arguments are the most common type of function arguments. They are passed to functions in the order they are defined.

positional_args.py
1def greet(name, age):
2 print(f"Hello {name}, you are {age} years old.")
3
4greet("Alice", 30)
Output
Hello Alice, you are 30 years old.

2. Keyword Arguments

Keyword arguments allow you to specify the name of the parameter when passing an argument to a function. This makes the code more readable and flexible.

keyword_args.py
1def greet(name, age):
2 print(f"Hello {name}, you are {age} years old.")
3
4greet(age=30, name="Alice")
Output
Hello Alice, you are 30 years old.

3. Default Parameter Values

Default parameter values allow a function to be called with fewer arguments than it is defined to accept. If an argument is not provided when the function is called, the default value is used.

default_args.py
1def greet(name, age=25):
2 print(f"Hello {name}, you are {age} years old.")
3
4greet("Alice")
5greet("Bob", 30)
Output
Hello Alice, you are 25 years old.
Hello Bob, you are 30 years old.

4. *args for Variable Positional Arguments

*args allows a function to accept any number of positional arguments as a tuple.

args.py
1def add(*args):
2 return sum(args)
3
4result = add(1, 2, 3, 4)
5print(result)
Output
10

5. **kwargs for Variable Keyword Arguments

**kwargs allows a function to accept any number of keyword arguments as a dictionary.

kwargs.py
1def display_info(**kwargs):
2 for key, value in kwargs.items():
3 print(f"{key}: {value}")
4
5display_info(name="Alice", age=30, city="New York")
Output
name: Alice
age: 30
city: New York

Practical Example

Let's create a practical example that combines the use of positional arguments, default values, *args, and **kwargs.

practical_example.py
1def report(name, age=25, *hobbies, **details):
2 print(f"Name: {name}")
3 print(f"Age: {age}")
4 if hobbies:
5 print("Hobbies:", ", ".join(hobbies))
6 for key, value in details.items():
7 print(f"{key}: {value}")
8
9report("Alice", 30, "reading", "hiking", country="USA", occupation="Engineer")
Output
Name: Alice
Age: 30
Hobbies: reading, hiking
country: USA
occupation: Engineer

Summary

  • Positional Arguments: Passed in the order they are defined.
  • Keyword Arguments: Passed by specifying the parameter name.
  • Default Parameter Values: Provide a default value for parameters that can be omitted when calling the function.
  • *args: Accepts any number of positional arguments as a tuple.
  • **kwargs: Accepts any number of keyword arguments as a dictionary.

What's Next?

In the next tutorial, we'll explore Python Lambda Functions. These are small anonymous functions defined with the lambda keyword. They are useful for short, throwaway functions and can be used wherever function objects are required.

Stay tuned!


PreviousPython FunctionsNext Python Lambda Functions

Recommended Gear

Python FunctionsPython Lambda Functions