Use the power operator ( ** ) to raise the left value to the power of the second value in Python. Python has three ways to get the power of 2 or any other number:
- The
**
operator. To program 25 we do2 ** 5
. - The built-in
pow()
function. 23 coded becomespow(2, 3)
. - The
math.pow()
function. To calculate 35, we domath.pow(3, 5)
.
Python power of 2 example
Simple example code.
import math
print("2 ** 5 = ", 2 ** 5)
print("pow: ", pow(2, 3))
print("Math pow:", math.pow(3, 5))
Output:
Do comment if you have any doubts or suggestions on this Python power 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.