Python Exponent | Calculate exponent in Python

Exponential is a mathematical operation where the number gets multiplied to a definite set of times to itself. For Example, 2 to the 3rd (written like this: 23) means: 2 x 2 x 2 = 8. 

How To Calculate Exponent In Python?

In Python exponent is calculated using the following approaches.

  1. Using ** Operator 
  2. Using pow() method
  3. Using math.pow() method
  4. Using numpy.np() method

Using ** operator to calculate exponent in Python

In mathematics, we use the caret symbol ^ to define the exponent value 23 is read as 2 to the power of 3. However, in programming languages, we use the caret ^ symbol is reserved for the bitwise xor operator. 

The exponent operator is defined using the two consecutive asterisks ** between the base and the exponent number in Python. In simple terms ** operator is called an exponent operator in Python.

Like the regular multiplication, the exponent operator ** works between 2 numbers, the base and the exponent number.

If we need to calculate the exponential of 23 in Python, we can do it using the ** operator as shown below.

print(2**3)
# returns 8

Example Exponentiation in Python using ** operator

print(2**3)
print(542**22)
print(-32**3)
print(22**-3)
print(0.25**0.1)
print(0.77**-2)
print(-9**-9)

Output

8
1405996708053103199772546738510358672795198151494953456369664
-32768
9.391435011269723e-05
0.8705505632961241
1.6866250632484399
-2.581174791713197e-09
Note: The ** operator raises ZeroDivisionError if we raise 0.0 to a negative power. Similarly, when we raise a negative number to a fractional power, it returns a complex number.

Example ZeroDivisionError 

print(0.0 ** -10) 

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 3, in <module>
    print(0.0 ** -10) 
ZeroDivisionError: 0.0 cannot be raised to a negative power

Using pow() function to calculate exponent in Python

We can also calculate exponential in Python using the built-in pow() function. The pow() method takes two main parameters and returns the power of the number as output.

pow() Parameters

The pow() function takes three parameters:

  • x – a number, the base
  • y – a number, the exponent
  • z (optional) – a number used for modulus

Hence,

  • pow(x, y) is equal to xy
  • pow(x, y, z) is equal to xy % z

Example – Calculate exponential in Python using pow() function

print(pow(2,3))
print(pow(542,22))
print(pow(-32,3))
print(pow(22,-3))
print(pow(0.25,0.1))
print(pow(0.77,-2))
print(pow(-9,-9))
print(pow(7,2,5))

Output

8
1405996708053103199772546738510358672795198151494953456369664
-32768
9.391435011269723e-05
0.8705505632961241
1.6866250632484399
-2.581174791713197e-09
4

Using math.pow() function to calculate exponent in Python

Like the pow() method, we can leverage the math module in Python to calculate the exponent. The main difference between pow() and math.pow() is math.pow() always returns a float value even if the whole number is passed as arguments, while pow() will only return float if at least one float argument.

The math.pow() function takes two required parameters, x and y and returns the power of the number as a floating-point value.

Syntax

math.pow(xy)
Note: If x is negative and y is not an integer, it returns a ValueError.

Example: Calculate Exponentiation in Python using math.pow() function

import math

print(math.pow(2,3))
print(math.pow(542,22))
print(math.pow(-32,3))
print(math.pow(22,-3))
print(math.pow(0.25,0.1))
print(math.pow(0.77,-2))
print(math.pow(-9,-9))

Output

8.0
1.4059967080531033e+60
-32768.0
9.391435011269723e-05
0.8705505632961241
1.6866250632484399
-2.581174791713197e-09

Using numpy.power() function to calculate exponential in Python

You can also use the NumPy module, which has its own function power() to calculate the exponent.

The NumPy module needs to be installed first before importing, as shown below.

# Python 2
pip install numpy
# Python 3
pip3 install numpy

Example: Calculate Exponentiation in Python using numpy.power() function

import numpy as np

print(np.power(2,3))
print(np.power(542,22))
print(np.power(-32,3))
print(np.power(0.25,0.1))

Output

8
-398458880
-32768
0.8705505632961241
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Comment Block / Python Multiline Comment

Python Comment Block

Table of Contents Hide Introduction to Python Comment BlockTypes of comments in PythonSingle-line commentsInline commentsMultiline commentsUsing Multiple Hashtags (#)Python docstringsSingle line docstringsMulti-line docstrings Comments are a piece of text in…
View Post