Skip to content

Python exponent operator

  • by

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:

Python exponent operator

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.

BaseExponentReturn Value
Non-NegativeNon-NegativeInteger
Non-NegativeNegativeFloat
NegativeNon-NegativeInteger
NegativeNegativeFloat

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading