Python
Python Basics
- Introduction to Python and Its History
- Python Syntax and Indentation
- Python Variables and Data Types
- Dynamic and Strong Typing
- Comments and Docstrings
- Taking User Input (input())
- Printing Output (print())
- Python Operators (Arithmetic, Logical, Comparison)
- Type Conversion and Casting
- Escape Characters and Raw Strings
Data Structures in Python
- Lists
- Dictionaries
- Dictionary Comprehensions
- Strings and String Manipulation
- Tuples
- Python Sets: Unordered Collections
- List Comprehensions and Generator Expressions
- Set Comprehensions
- String Formatting
- Indexing and Slicing
Control Flow and Loops
- Conditional Statements: if, elif, and else
- Loops and Iteration
- While Loops
- Nested Loops
- Loop Control Statements
- Iterators and Iterables
- List, Dictionary, and Set Iterations
Functions and Scope
- Defining and Calling Functions (`def`)
- Function Arguments (`*args`, `**kwargs`)
- Default Arguments and Keyword Arguments
- Lambda Functions
- Global and Local Scope
- Function Return Values
- Recursion in Python
Object-Oriented Programming (OOP)
- Object-Oriented Programming
- Classes and Objects
- the `__init__()` Constructor
- Instance Variables and Methods
- Class Variables and `@classmethod`
- Encapsulation and Data Hiding
- Inheritance and Subclasses
- Method Overriding and super()
- Polymorphism
- Magic Methods and Operator Overloading
- Static Methods
- Abstract Classes and Interfaces
Python Programs
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions
- Program: Calculate Pi in Python
- String Formatting in Python
Understanding *args
and **kwargs
in Python
When learning Python, most beginners start by defining functions with a fixed number of arguments. But what happens when you don’t know how many inputs you’ll receive? That’s where *args
and **kwargs
come into play. These special symbols give Python developers the power of flexibility and dynamic input handling. Whether you’re building simple utilities or complex APIs, mastering *args
and **kwargs
is essential.
In this article, we’ll explore what these keywords mean, how and when to use them, and see real-world examples that demonstrate their practicality.
📚 Why Are *args
and **kwargs
Important?
Before we dive into the syntax, it’s important to understand the “why”:
- 🔁 Flexibility: Accept a variable number of arguments.
- 🔧 Dynamic Input: Useful for writing reusable, general-purpose functions.
- 🧹 Clean Code: Helps avoid long parameter lists.
- 🤝 Compatibility: Great for integrating with existing or third-party functions.
✅ Prerequisites
To fully benefit from this guide, you should have:
- A basic understanding of functions (
def
, return statements, and calling functions). - Familiarity with Python data types like lists, tuples, and dictionaries.
- Some practice writing and calling Python functions.
🌟 Understanding *args
(Non-keyword Arguments)
What is *args
?
*args
allows you to pass a variable number of non-keyword arguments to a function. These arguments are received as a tuple inside the function.
Syntax:
def function_name(*args):
for arg in args:
print(arg)
Example:
def print_names(*names):
for name in names:
print("Hello", name)
print_names("Alice", "Bob", "Charlie")
Output:
Hello Alice
Hello Bob
Hello Charlie
You can pass zero or more arguments, and Python will handle it gracefully.
🌟 Understanding **kwargs
(Keyword Arguments)
What is **kwargs
?
**kwargs
allows you to handle a variable number of keyword arguments — arguments passed in as name=value pairs. Inside the function, they’re accessible as a dictionary.
Syntax:
def function_name(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
Example:
def print_user_info(**info):
for key, value in info.items():
print(f"{key}: {value}")
print_user_info(name="Alice", age=30, location="NYC")
Output:
name: Alice
age: 30
location: NYC
🔄 Using *args
and **kwargs
Together
You can use both in the same function to accept a mix of arguments.
Example:
def mixed_function(*args, **kwargs):
print("Positional arguments:", args)
print("Keyword arguments:", kwargs)
mixed_function(1, 2, 3, name="Alice", age=25)
Output:
Positional arguments: (1, 2, 3)
Keyword arguments: {'name': 'Alice', 'age': 25}
Important: The order in your function definition must always be:
def function(arg1, *args, kwarg1=value, **kwargs):
🧪 Real-World Use Cases
1. Logging Function Calls
def logger(*args, **kwargs):
print("Function called with:")
for arg in args:
print("ARG:", arg)
for key, val in kwargs.items():
print(f"{key.upper()}: {val}")
logger("test", user="admin", action="login")
2. Handling Configurations
def setup_app(**config):
print("App configured with:")
for k, v in config.items():
print(f"{k} = {v}")
setup_app(debug=True, theme="dark", version="1.0")
3. Creating Flexible Math Functions
def multiply_all(*numbers):
result = 1
for num in numbers:
result *= num
return result
print(multiply_all(2, 3, 4))
4. Function Wrappers (Decorators)
def decorator(func):
def wrapper(*args, **kwargs):
print("Before function")
result = func(*args, **kwargs)
print("After function")
return result
return wrapper
@decorator
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
5. APIs or Forms with Dynamic Fields
def save_form_data(**form_fields):
print("Saving data:")
for field, value in form_fields.items():
print(f"{field}: {value}")
save_form_data(name="Alice", email="alice@example.com", subscribed=True)
🚫 Common Mistakes to Avoid
-
Wrong Order of Arguments
Always follow the correct order:def func(normal_arg, *args, default_arg="value", **kwargs):
-
Using
args
andkwargs
as Variable Names
You can renameargs
andkwargs
to anything, but the*
and**
are what matter. -
Forgetting to Unpack
When calling another function with*args
or**kwargs
, remember to unpack:def show(a, b): print(a, b) args = (1, 2) show(*args) # Correct
💡 Tips and Best Practices
- Use
*args
when you’re unsure about the number of positional arguments. - Use
**kwargs
when you’re unsure about the number of named parameters. - Combine them wisely to build generic and extensible functions.
- Avoid overusing them if the function doesn’t actually need flexibility—it can make code harder to read.
Understanding how to use *args
and **kwargs
is essential for writing flexible, clean, and reusable Python functions. They unlock the ability to build more dynamic and robust applications. Whether you’re writing utility functions, decorators, or handling user input, these tools are invaluable.
Now that you’ve mastered the basics of *args
and **kwargs
, start applying them in your projects. The more you use them, the more natural they’ll become in your coding toolkit.