Table of Content
- What are the Asterisks or Star Symbols * and ** in Python?
- 5 Different Ways to Use the Asterisks or Star Symbols '*' and '**' in Python
- 1. Perform calculations on numeric data types
- 2. Unpack an arbitrary number of arguments in a function parameter
- 3. Force all arguments passed to a function to be keyword-only arguments
- 4. Unpack Elements of an iterable into a new iterable
- 5. Repeat a string multiple times
- Wrap Off
When you've been exposed to Python codes, you would have come across one or more of the asterisk symbols '*' or '**'.
As confusing and almost irrelevant as they might look, they are actually a really important part of Python programming.
Good knowledge of them might help you write efficient and more generic solutions when solving problems using Python language. Think of the DRY principle.
The asterisk (star) operator is used in Python with more than one meaning attached to it.
After going through this article, you will gain a solid understanding of the various ways you can use the asterisk or star * symbol and the counterpart double-asterisk or double-star * symbol in Python programming.
What are the Asterisks or Star Symbols * and ** in Python?
The single asterisk or star (*) and the double-asterisk or double-star (**) symbols are both used primarily for numeric multiplication and exponential calculation respectively in Python.
They also hold the same conventional use case in other programming languages.
5 Different Ways to Use the Asterisks or Star Symbols '*' and '**' in Python
In Python programming language, the single asterisk or star (*) and the double-asterisk or double-star (**) symbols can be used for other functionality that is different from the conventional use case.
Here are five different ways you can use them in Python programming:
1. Perform calculations on numeric data types
This is more conventional use in programming.
In Python programming language, as with most programming languages, the single asterisk or star symbol (*) is used as a multiplication operator to get the product of two numeric values or variables.
The double asterisk or star symbol (**) is used as an exponential operator and is used to raise the value or variable on the left to the power of the value or variable on the right.
2. Unpack an arbitrary number of arguments in a function parameter
There is a chance you should have come across the *args and **kwargs in a function argument in Python codes.
These symbols specify that the function could receive an unspecified and a possibly unlimited number of arguments or keyword arguments.
If you are not sure how many arguments and keyword arguments will be passed, you can use a single asterisk or star (*) and the double-asterisk or double-star (**) symbols, respectively.
For arguments, the values are received as a tuple and all properties of a tuple can be performed on it:
You can also pass the arguments as a list, tuple, or set. For a dictionary, to pass the keys only, use
*, to pass the values only, use
For keyword arguments, the values are received as a dictionary, and all properties of a dictionary can be performed on it:
Passing as dictionary vs passing as keyword arguments for
3. Force all arguments passed to a function to be keyword-only arguments
Another way we can use an asterisk in a function is to ensure that a function can only receive keyword arguments.
To do that, simply pass a single asterisk or star
* in the parameter list and the following arguments must be passed as keyword arguments.
Also, we can restrict a few arguments to be keyword-only by just putting the positional arguments before the asterisk.
4. Unpack Elements of an iterable into a new iterable
One way we can use asterisks to make our programs clear and elegant is when we need to combine different iterable such as lists, tuples, and sets into a new list.
An obvious solution for this is to use for-loops to iterate all items and add them to a new list one after the other.
Even though the code block above accomplishes our mission, the code looks so long and is not very “Pythonic”.
We could use list comprehensions to make the code a lot better.
Using list comprehension takes us a step closer to achieving a great solution, but we can do even better than that by using asterisks.
This method works really well for iterable such as lists, tuples, and sets.
However, it's different for dictionaries.
If we use a single asterisk
* as a prefix to unpack
dict, its keys will be unpacked. And if we use double asterisks
** as a prefix, its values will be unpacked.
But, we need the keys of a dictionary to receive the unpacked values, which makes unpacking a dictionary to be inconvenient and uncommon.
Extended unpacking of Iterable
In Python 3, it is possible to use `*l` on the left side of an assignment as an Extended Iterable Unpacking proposed in PEP 3132.
However, the variable with the '*' becomes a list instead of a tuple in this context:
5. Repeat a string multiple times
The single asterisk (*) can be used to repeat values of variables in sequences such as string, list, and tuple.
So far, we've covered the Asterisk(*) of Python. It was interesting to be able to do various operations with one operator, and most of those above are the basics for writing Pythonic code.
We. hope you've gained a solid understanding of the various ways you can use the asterisk or star * symbol and the counterpart double-asterisk or double-star * symbol in Python programming.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.
Please share this post and for more insightful posts on business, technology, engineering, history, and marketing, subscribe to our newsletter.