What is the Asterisk Operator in Python?
Explanation
In Python, the symbols * and ** are used frequently. Even intermediate-level Python programmers frequently have trouble understanding the asterisk (*) character there in Python.
Asterisks are mostly used as power and multiplication operators, but they can also perform a wide variety of operations in Python when used as a prefix operator. You will have a clear understanding of Python's asterisk (*) operator after going through this article.
Syntax
Use an asterisk(*) according to the operation to be performed.
Example
Output
In the given code above a variable number of parameters are passed to the sumto function and then this *nums works as an *args keyword which stores that series of numbers and then the sum function is performed over that series of numbers.
What are the Uses of Asterisk in Python?
In Python, asterisks have a wide variety of applications. The operators like multiplication and power are generally known to us that can be done using this asterisk(*). In different circumstances, it can carry out additional operations like unpacking and a variable number of arguments passing, etc. Let's look at how asterisks are generally used first.
1. Multiplication
When multiplying two numbers, we use the asterisk (*) operator as the infix operator.
Output
2. Exponentiation
We can determine an exponential value of the integer using the two (**) asterisk operators.
Output
3. List Multiplication
The symbol "*" allows us to multiply list items and reduces the code to a single line.
Output
4. Unpacking a Function Using the Positional Argument
This technique is extremely helpful when we have to print information in a raw format that is without any brackets and commas. This prefix asterisk can help you in unpacking them because many programmers attempt to remove the bracket and comma by using a convolution of functions.
Output
5. Passing a Function Using an Arbitrary Number of Positional Arguments
Here, *args also uses a single asterisk (*). It is used to pass the variable number of parameters to the function, typically non-key arguments and argument lists of varying lengths. There are many applications for it, and one of them is shown in the example below. We create an additional function that can add any number of parameters using the *args keyword.
Output
6. Passing a Function Using an Arbitrary Number of Keyword Arguments
While passing a dictionary as an argument to the function, the ** symbol is used before an argument. This syntax is used to execute the code correctly when we don't know how many keyword arguments will be provided to the function.
Output
7. List Packing up
In some situations, we can use asterisks for both packs and unpack lists of items. For instance, the syntax given below can be used to get three separate items from a list by using 3 variables.
Output
Let's say that in the scenario mentioned above, it doesn't concern who my friends are, all that matters is that my name comes last and the list of names stays the same. In this instance, we can pack up all of the other elements using an asterisk.
Output
More Examples of Different Applications of Asterisk in Python
Unpacking Dictionary
A double asterisk must be placed before the dictionary variable to allow it to be unpacked. As for Python 3.5, asterisks also can unpack the dictionaries. Combining several dictionaries would be one of the most popular applications. Let's say we want to combine the following two dictionaries. We could unpack them both and place them within a different, empty dictionary.
Output
Unpack Range
A Python range is one of the rare applications for utilizing asterisks for unpacking. To specify how many times a for-loop should loop, we typically use range(). It can also be used to generate the following sequence of integers.
Output
Conclusion
- Whenever Python is updated(when the new version is released), developers add new functionality.
- Special symbols like asterisks shorten various sections of the code.
- Without asterisk(*), we are unable to accept multiple positional arguments, keyword-only arguments also require asterisk(*).
- Asterisk(*) may function as an operator or even an indicator for passing varying numbers of parameters. In addition to packing them up, it is also able to unpack dictionaries, lists, and range objects.