
Python pow and math.pow: how to calculate exponentiation
In Python, double asterisks means power operator.
a = 3 ** 2
b = 3 ** 3
c = 3 ** 4
print(a) # 9
print(b) # 27
print(c) # 81
3 ** 4
is 3 * 3 * 3 * 3
. Python
pow
pow
is a Python built-in function calculating exponentiation.
a = pow(2, 2)
b = pow(2, 3)
c = pow(2, 4)
print(a) # 4
print(b) # 8
print(c) # 16
The first argument is base and the second is power.
math.pow
pow
in Python math module is different from built-in function pow
explained in above. math.pow also returns exponentiation but the type is float even if the value is int.
import math
a = math.pow(2, 2)
b = math.pow(2, 3)
c = math.pow(2, 4)
print(a) # 4.0
print(b) # 8.0
print(c) # 16.0
math.pow(2, 4) means $2^4$ but it returns 16.0, a float value.
- pow basically returns int
- math.pow basically returns float
Comments
Powered by Markdown