Trusted answers to developer questions

Calculating the exponential value in Python

Checkout some of our related courses
Learn Intermediate Python 3
Beginner
Full Speed Python
Beginner
Coderust: Hacking the Coding Interview
Beginner

In Mathematics, the exponential value of a number is equivalent to the number being multiplied by itself a particular set of times.

The number to be multiplied by itself is called the base and the number of times it is to be multiplied is the exponent.

svg viewer

How to calculate the exponential value of a number

Python allows users to calculate the exponential value of a number in multiple ways. Let’s look at each of them in detail!

1. ** operator

The double asterisk, ** operator is a shortcut to calculate the exponential value. Let’s take a look at how this can be used in the following code:

base = 3
exponent = 4
print "Exponential Value is: ", base ** exponent

2. pow( )

In addition to the ** operator, Python has included a built-in pow() function which allows users to calculate the exponential value.

The function takes as input the base and exponent and returns the corresponding value. The general syntax of the function is:

pow(base, exponent)

Look at the coding example to see how it works:

base = 3
exponent = 4
print "Exponential Value is: ", pow(base, exponent)

3. exp( )

The exp() function in Python allows users to calculate the exponential value with the base set to e.

Note:

  1. e is a Mathematical constant, with a value approximately equal to 2.71828.
  2. The math library must be imported for this function to be executed.

The function takes as input the exponent value. The general syntax of the function is:

math.exp(exponent)

Execute the following piece of code to see the result!

import math
exponent = 4
print "Exponential Value is: ", math.exp(exponent)

RELATED TAGS

python
exponent
exp
pow
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?