How to Calculate Exponent in Python
To calculate an exponent in Python, use the exponentiation operator which is
** (two asterisks.) Pass the number to evaluate on the left and to the power of on the right.
Let's try this out by getting the value of
4^³ (four to the power of three) the expression of which is 4*4*4 = 64.
exp = 4**3 print(exp)
Floating Point Exponent in Python
It is also possible to use floating-point numbers for both the exponent and power of using the Python
Exponent as a float:
exp = 4.5**3 print(exp)
Power as a float:
exp = 4**3.5 print(exp)
Both values as floats:
exp = 4.5**3.5 print(exp)
You can also use a negative power with the Python exponentiation operator like this:
exp = 4**-3 print(exp)
The Python pow() Function
Another way to calculate an exponent in Python is with the
pow() function. Pass the exponent as the first argument and the power as the second.
exp = pow(4,3) print(exp)
Like, the Python
pow() also accepts any combination of floats and integers and negative power.
exp = pow(4.1,-3.1) print(exp)
The math.pow() Function
If you are using the math package, there is
pow() function available in it, which might be a preferable option for you – its default behaviour is to return floats.
import math exp = math.pow(4,3) print(exp)