Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is ** in Python

Understanding the Double Asterisk (**) in Python

When you're starting out in the world of programming, encountering symbols like ** in Python can seem like stumbling upon a mysterious hieroglyph. But fear not! This symbol is a tool that, once understood, will empower you to write more flexible and powerful code. So let's demystify it together.

The Power Operator: Exponentiation

At its most basic level, the double asterisk ** is used in Python to represent the exponentiation operator. This means it's a quick and easy way to raise a number to the power of another number.

Example: Basic Exponentiation

# Raising 2 to the power of 3
result = 2 ** 3
print(result)  # Output: 8

# Raising 5 to the power of 2 (which is essentially squaring 5)
square_of_five = 5 ** 2
print(square_of_five)  # Output: 25

Think of ** as a shorthand for saying "multiplied by itself so many times." If you remember your math classes, you know that 2 to the power of 3 (2^3) is the same as saying 2 * 2 * 2. That's exactly what the ** operator is doing in Python.

Unpacking with Double Asterisk: Keyword Arguments

Beyond exponentiation, ** has another superpower in Python. It's used to unpack dictionaries into keyword arguments when calling a function. This might sound a bit complex, but let's break it down with an analogy.

Imagine you have a box full of ingredients for a recipe (this is your dictionary). When you're ready to cook, you need to take each ingredient out of the box and use it as instructed by the recipe (this is your function). The ** operator is like having a kitchen assistant who knows exactly how to unpack the ingredients for you and hand them over as you need them.

Example: Unpacking a Dictionary

def greet(first_name, last_name):
    print(f"Hello, {first_name} {last_name}!")

# A dictionary with keys that match the parameter names of the 'greet' function
person = {'first_name': 'John', 'last_name': 'Doe'}

# Using ** to unpack the dictionary into the function's keyword arguments
greet(**person)  # Output: Hello, John Doe!

In this example, **person takes the dictionary called person and unpacks it. The keys of the dictionary match the parameter names of the greet function, so first_name and last_name are automatically filled with the corresponding values from the dictionary.

Using ** for Arbitrary Keyword Arguments

Sometimes when you're defining a function, you might not know in advance all the keyword arguments that will be passed to it. Python allows you to use ** to collect those arguments into a dictionary so that you can work with them inside the function.

Example: Collecting Arbitrary Keyword Arguments

def build_profile(**user_info):
    print("User Profile:")
    for key, value in user_info.items():
        print(f"{key}: {value}")

# Calling 'build_profile' with arbitrary keyword arguments
build_profile(name='Jane', age=28, occupation='Engineer')

In the build_profile function, **user_info collects all the keyword arguments passed to the function into a dictionary called user_info. This dictionary can then be used within the function, regardless of how many arguments or what kind of arguments are passed to it.

When to Use Double Asterisk: Practical Scenarios

Now that you understand the basics of **, it's important to know when to use it. Here are a couple of scenarios where ** can be particularly useful:

  • Working with APIs: When interacting with APIs (Application Programming Interfaces, which are sets of rules that allow different pieces of software to communicate), you might need to pass a variable set of parameters to a function. ** makes this easy and clean.
  • Creating Configurations: If you have a function that takes a lot of configuration options, using ** allows you to pass a dictionary of configurations, making your code more readable and flexible.

Common Pitfalls and How to Avoid Them

While ** is incredibly useful, there are a couple of pitfalls to watch out for:

  • Key Collisions: If you're unpacking a dictionary into a function, and the dictionary contains keys that don't match the function's parameter names, you'll get a TypeError.
  • Overwriting Values: If you have a dictionary with repeated keys (which is not standard and should be avoided), only the last value will be used when unpacking with **.

To avoid these issues, always ensure that the keys in your dictionary match the parameter names of the functions you're calling, and avoid using dictionaries with duplicate keys.

Conclusion: Embracing the Double Asterisk

The double asterisk ** in Python is more than just a symbol for math enthusiasts. It's a versatile tool that can simplify your code and make it more adaptable. Whether you're performing mathematical operations or passing a flexible number of arguments to a function, understanding ** will open up new possibilities in your programming journey.

As you continue to learn and grow as a programmer, remember that each symbol and concept you master is like adding a new spice to your coding pantry. And the double asterisk? It's the secret ingredient that can turn a good code recipe into a great one. So sprinkle it in where it makes sense, and watch your programs come to life with newfound efficiency and elegance.