Asterisks (*) in Python

Recently, someone asked me how to use the asterisks (*) before variables in Python. I will give you examples of how to use asterisks (*) before variables in Python.

In Python, a single asterisk (*) before a variable name in a function definition, commonly denoted as *args, allows the function to accept an arbitrary number of non-keyword arguments. This means you can pass multiple arguments to the function, and they will be accessible as a tuple within the function. For example, def greet(*args): enables the function to handle any number of names passed to it, allowing for flexible and dynamic argument handling.

What Do Asterisks (*) Mean in Python?

In Python, the asterisk (*) is a powerful operator that can be used in several contexts, including function definitions, function calls, and unpacking collections. So, you should understand how and when to use the asterisk in Python programming.

Asterisks (*) in Python

Now, let me show you how to use asterisks in Python or how to use the asterisk operator in Python.

1. Asterisks in Function Definitions

When defining functions in Python, asterisks can be used to accept an arbitrary number of arguments. This is particularly useful when you don’t know beforehand how many arguments will be passed to the function.

Single Asterisk (*args)

A single asterisk before a variable name (commonly *args) allows you to pass a variable number of non-keyword arguments to a function. Here is an example.

def greet(*args):
    for name in args:
        print(f"Hello, {name}!")

greet("Alice", "Bob", "Charlie")

In this example, the greet function can accept any number of arguments, and it will greet each person individually.

I executed the above Python code, and you can see the exact output in the screenshot below:

asterisk operator in python

Double Asterisk (**kwargs)

A double asterisk in Python before a variable name (commonly **kwargs) allows you to pass a variable number of keyword arguments to a function in Python.

Here is an example.

def display_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

display_info(name="Alice", age=30, city="New York")

Here, the display_info function can accept any number of keyword arguments and print each key-value pair.

Here is the output you can see in the screenshot below:

Asterisks in Python Variables

Check out Call a Variable from Another Function in Python

2. Asterisks in Function Calls

Asterisks in Python can also be used when calling functions to unpack arguments from a list or dictionary.

Unpacking with Single Asterisk (*)

You can use a single asterisk to unpack elements from a list or tuple and pass them as positional arguments to a function.

def add(a, b, c):
    return a + b + c

numbers = [1, 2, 3]
result = add(*numbers)
print(result)  # Output: 6

You can see the output in the screenshot below:

Asterisks in Function Calls in Python

Unpacking with Double Asterisk (**)

You can use a double asterisk to unpack elements from a dictionary and pass them as keyword arguments to a function.

def introduce(name, age, city):
    print(f"Name: {name}, Age: {age}, City: {city}")

person = {"name": "Alice", "age": 30, "city": "New York"}
introduce(**person)

Read Static Variables in Functions in Python

3. Asterisks for Collection Unpacking

Asterisks can also be used to unpack collections in assignments and loops in Python

Unpacking in Assignments

You can use a single asterisk to unpack elements from a list or tuple into variables in Python.

Here is an example.

numbers = [1, 2, 3, 4, 5]
a, *b, c = numbers
print(a)  # Output: 1
print(b)  # Output: [2, 3, 4]
print(c)  # Output: 5

Unpacking in Loops

Asterisks can be used to unpack elements from a list or tuple in loops in Python.

Here is an example.

pairs = [(1, 2), (3, 4), (5, 6)]
for a, b in pairs:
    print(a, b)

Conclusion

The asterisk (*) in Python can be used in various contexts, such as defining functions that accept a variable number of arguments, calling functions with unpacked arguments, or unpacking collections. In this tutorial, I have explained how to use asterisks (*) in Python with various examples. Still have questions? Leave a comment below.

You may also like the following tutorials:

Leave a Comment