Python exponent operator is the arithmetic operator. Raising a number to the second power is not easy to compare with normal multiplication.
m ** n
The exponentiation operator uses the (**) double asterisk/exponentiation operator between the base and exponent values.
Python exponent operator example
A simple example code exponent operator raises its second variable to the power of its first variable.
2**5
translates to 2*2*2*2*2
= 32
m = 2
n = 5
p = m ** n
print("The exponent:", p)
Output:
You can use loops to calculate exponential but its time complexity becomes O(n) and space complexity is O(1).
The Exponent operator **
works in the same way as the pow(a, b)
function.
base = 2 exponent = 8 # pow() function res = pow(base, exponent) print("Exponential value is:", res)
Output: Exponential value is: 256
As the pow() function first converts its argument into float and then calculates the power, we see some return type differences.
Base | Exponent | Return Value |
---|---|---|
Non-Negative | Non-Negative | Integer |
Non-Negative | Negative | Float |
Negative | Non-Negative | Integer |
Negative | Negative | Float |
Comment if you have any doubts or suggestions on this Python operator topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.