In some Python code, you might spot double stars standing next to each other. What do those two stars mean, and what is their job? We will answer all the questions in this article.
Introduction
Double star or double asterisk (**) in Python code can mean two different things based on the position in the code. First, if a double asterisk stands in the equation, it is part of it and is considered a Power operator. Otherwise, double-asterisk can occur in function and method argument input, and in such a case, it should be followed by kwargs shortcut (keyword arguments).
Power Operator
What is double start operator in equation?
Double star (or double-asterisk) in mathematical equation is one of the arithmetic operator (like +, -, *, **, /, //, %) in Python programming language. Double stars are also known as Power operator.
What Power Operator does?
For numerical data types Power operator works as an Exponential operator – anything on the right is the power of the left side.
Rather than talking about it, let’s demonstrate the Exponential operator on example:
two = 2
three = 3
# We can read the following notation as two to the power of the three
result = two ** three
# Result is according to the math laws, and it is equal to 2 * 2 * 2
print(result)
# Example of using double asterisk operator.
result_2 = 10 * (4 ** 2) - (2 ** 2) * 20
print(result_2)
Output
8
80
What is the order of arithmetic operators?
Arithmetic operators in Python follow the same precedence rule as in mathematics. The order is as follows: first performs exponential operator, then multiplication and division; as last executed are addition and subtraction, retrospectively.
Arithmetic operators ordered in decreasing order by priority:
() -> ** -> * -> / -> // -> % -> + -> -
** as variable argument length in functions and methods
Interestingly, in Python, two starts fulfil also a different role. In Python’s function and method definition, the double-asterisk is also known for its role in defining variable argument length. Double asterisk concatenate with kwargs word and form **kwargs. So every time you see it, it means that the method or function it contains has a variable-length argument dictionary in the place of **kwargs occurrence. So when we use **kwargs as a parameter, we don’t need to know how many arguments we will need to pass to a function/method.
Btw. kwargs is not a Python reserved word and can be used in Python code independently.
**kwargs examples
Let’s write a simple function with **kwargs, taking various length arguments and passing them to a function.
def function(**kwargs):
for key, value in kwargs.items():
print("key: {} - value: {}".format(key, value))
function(pos1="John", pos2="Thomas", pos3="Paul")
Output
key: pos1 - value: John
key: pos2 - value: Thomas
key: pos3 - value: Paul
And now, let’s use the same function with additional arguments. Then, we will see how the function with **kwargs will accept a different number of arguments:
def function(**kwargs):
for key, value in kwargs.items():
print("key: {} - value: {}".format(key, value))
function(pos1="John", pos2="Thomas", pos3="Paul", pos4="Isabella", pos5="Carry", pos6="Angela")
Output
key: pos1 - value: John
key: pos2 - value: Thomas
key: pos3 - value: Paul
key: pos4 - value: Isabella
key: pos5 - value: Carry
key: pos6 - value: Angela
Note: It is a good idea to create functions that accept **kwargs where you expect the number of inputs within the argument list to remain relatively small.
Conclusion
This article has shown two different ways how the double asterisk is used. First, we have seen the order of arithmetic operators in Python and how a double asterisk denotes a mathematical Power operator. The second usage is variable argument length in Python’s functions and class methods. Usage of **kwargs provides us flexibility to use keyword arguments in our program.
Did you find the usage of double stars in Python confusing? Do you have any tricks or know another way how to use double stars in Python? Let us know in the comments below the article. We would like to hear your ideas and stories.