Python Double Asterisk (**) Explained: Understanding Keyword Arguments in Functions π
Ever Wondered What ** in pythonΒ Does? π€**
If youβve been coding in Python for a while, youβve probably encountered the python double asterisk (**) in function arguments. Maybe youβve seen **kwargs and thought, What is this magic? Or perhaps, you tried using it and got an error. Trust me, Iβve been there! Today, weβre diving deep into the Python double asterisk, breaking it down with real-life examples and a sprinkle of fun! π
Table Of Content
- Ever Wondered What ** in pythonΒ Does? π€**
- π Key Highlights
- What is Python Double Asterisk (**)?
- Quick Example
- Why Use Double Asterisk (**)? π€·ββοΈ
- Double Asterisk in Function Arguments ποΈ
- Example 1: Using **kwargs to Accept Dynamic Arguments
- Unpacking Dictionaries Using ** π
- Example 2: Unpacking a Dictionary into Function Arguments
- Mixing *args and **kwargs in the Same Function
- Example 3: Combining *args and **kwargs
- Common Mistakes with **kwargs β
- 1. Mixing Positional Arguments Incorrectly
- 2. Forgetting to Unpack a Dictionary
- Final Thoughts: Why Python **(double asterisk) is a Game-Changer π―
- Recap
π Key Highlights
- Python double asterisk (
**) handles keyword arguments in functions. - It collects multiple keyword arguments into a dictionary.
- You can unpack dictionaries using
**when passing arguments. - Itβs super handy for flexible function definitions!
- Weβll cover everything with easy-to-follow examples!
What is Python Double Asterisk (**)?

Python’s double asterisk (**) is a powerful tool used in function arguments. Unlike a single asterisk (*args), which gathers positional arguments into a tuple, **kwargs collects keyword arguments into a dictionary.
Quick Example:
def greet(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
greet(name="Alice", age=25, city="New York")
Output:
name: Alice
age: 25
city: New York
Boom! π Python automatically converts the arguments into a dictionary!
Why Use Double Asterisk (**)? π€·ββοΈ
Using **kwargs in functions makes your code more flexible and future-proof. Hereβs why you should love it:
- Allows multiple keyword arguments without knowing them beforehand.
- Prevents errors when adding new parameters in functions.
- Helps with function overloading, making one function handle various cases.
- Improves code readability by clearly defining keyword-based parameters.
Double Asterisk in Function Arguments ποΈ
Letβs go deeper! When defining a function, ** lets us accept any number of keyword arguments.
Example 1: Using **kwargs to Accept Dynamic Arguments
def profile(**info):
return info
user = profile(name="John", age=30, profession="Developer")
print(user)
Output:
{'name': 'John', 'age': 30, 'profession': 'Developer'}
β‘οΈ Here, **info captures all keyword arguments and stores them in a dictionary!
Unpacking Dictionaries Using ** π
Not only can we collect keyword arguments using **kwargs, but we can also unpack dictionaries when passing arguments! π€―
Example 2: Unpacking a Dictionary into Function Arguments
def introduce(name, age, city):
print(f"Hello, I'm {name}, {age} years old from {city}.")
data = {"name": "Emma", "age": 22, "city": "Boston"}
introduce(**data)
Output:
Hello, I'm Emma, 22 years old from Boston.
β‘οΈ Here, **data unpacks the dictionary, passing values as keyword arguments.
Mixing *args and **kwargs in the Same Function
Yes, you can use both! This allows a function to handle both positional and keyword arguments.
Example 3: Combining *args and **kwargs
def order_food(order_type, *items, **extras):
print(f"Order Type: {order_type}")
print("Items:", items)
print("Extras:", extras)
order_food("Takeaway", "Burger", "Fries", sauce="Ketchup", drink="Coke")
Output:
Order Type: Takeaway
Items: ('Burger', 'Fries')
Extras: {'sauce': 'Ketchup', 'drink': 'Coke'}
β‘οΈ *items collects positional arguments into a tuple, while **extras stores keyword arguments in a dictionary. Super useful, right? π
Common Mistakes with **kwargs β
1. Mixing Positional Arguments Incorrectly
def example(**kwargs, name): # β Incorrect
print(name)
β This will raise a SyntaxError because **kwargs must always be the last parameter.
β Corrected version:
def example(name, **kwargs): # β
Correct
print(name)
2. Forgetting to Unpack a Dictionary
def calculate_price(price, tax):
return price + (price * tax)
data = {"price": 100, "tax": 0.08}
print(calculate_price(**data)) # β
Correct
β‘οΈ Without **data, youβd need to manually pass each key-value pair.
Final Thoughts: Why Python **(double asterisk) is a Game-Changer π―
Mastering the Python double asterisk (**) can make your code cleaner, more readable, and flexible. Whether youβre handling dynamic arguments or unpacking dictionaries, **kwargs is an essential tool in a Python developerβs arsenal! π‘
Recap:
β
**kwargs collects multiple keyword arguments as a dictionary.
β
You can unpack dictionaries into function arguments using **.
β
*args and **kwargs can be used together for flexibility.
β
Always place **kwargs at the end of function parameters.
π Now go try it out in your Python projects! If you have any questions, drop them in the comments. Letβs discuss! π¬π₯

