8 ways to calculate Exponent in Python

Python

In this post, we are going to learn multiple ways to calculate Exponents in Python. By using 8 ways to calculate the Exponent in Python or the finding power of a number in Python for example.

The exponential of the number in math terms means how many times a number multiplies to itself for a specified set of times. The number is known as base and the number of times multiplies is called the exponent(power).

8 ways to calculate Exponent in Python


  • Using Exponentiation ** operator
  • Using Python built in pow() function
  • Using Math module pow() function.
  • Using math.Exp() function
  • Find exponential of a list elements
  • Find exponential of complex number .
  • For loop to Find exponential of list elements
  • Find exponent of list or array using NumPy.exp()

1. Python Exponentiation operator (**)


To calculate Exponent in Python exponentiation operator(**) or power operator is used to calculate exponentiate. This operator is placed between two numbers(base and exponent) to calculate exponentiate.

**operator to calculate positive and float exponent

In this example, we have used the Exponentiation operator with positive and float exponent and printing exponent resulting using print() method.

power = 2
num1 = 3**power
num2 = 4**power
print('expontial of num1 :',num1)
print('expontial of num2 :', num2)


# Exponentiation operator with float exponent

floatnum1 = 3**2.5
floatnum2 = 4**3.5

print('\n Exponentiation operator with float exponent')
print('expontial of floatnum1 :',floatnum1 )
print('expontial of floatnum2 :', floatnum2 )



Output

Eexpontial of num1 : 9
expontial of num2 : 16
expontial of num1 : 25

Exponentiation operator with float exponent
expontial of num1 : 15.588457268119896
expontial of num2 : 128.0

Exponentiation operator with Python negative exponent


In this example, we are using the Python exponentiation operator(**) with a negative exponent.

num1 = 3**-2
num2 = 4**-3

print('expontial of num1 :',num1)
print('expontial of num2 :', num2)



Output

expontial of num1 : 0.1111111111111111
expontial of num2 : 0.015625

2. Python pow() function to calculate Exponent


The Python built-in pow() function is the return power of a number. It takes three arguments(base, exponent, modulus).

Syntax

pow(num,exponent,mod)

Parameters

  • Num: It is the base number that power we are calculating.
  • exponent: It is an exponent number. raise to a number.
  • Mod: This is an optional number by which modulus is calculated.

Python pow() function negative exponents


In this example, we are using the python built-in pow() function to calculate exponents in Python.

we are calculating the exponents in Python using the pow() function with negative exponent and printing the exponents result using the Python print() method.

#python3 program to calculate exponents
Exp1= pow(3,2)
Exp2 = pow(6,2)


print('expontial of 3^2 :',Exp1)
print('expontial of 6^2 :', Exp2)



#Python pow() function negative exponents

floatExp1= pow(3,-5)
flaotExp2 = pow(6,-6)

print('pow() negative exponent')
print('expontial of 3^-5 :',floatExp1)
print('expontial of 6^-6 :', floatExp2)

Output

expontial of 3^2 : 9
expontial of 6^2 : 36

pow() negative exponent
expontial of 3^-5 : 0.00411522633744856
expontial of 6^-6 : 2.143347050754458e-05

Python float exponent with Pow()


In this example, we are calculating the exponents in Python using the pow() function with float exponent and printing the exponents result using the Python print() method.

Exp1= pow(3,5.3)
Exp2 = pow(6,5.6)

print('expontial of 3,5.3 :',Exp1)
print('expontial of 6,5.6 :', Exp2)


Output

expontial of 3,5.3 : 337.8645683867659
expontial of 6,5.6 : 22784.89345711345

Python pow() to Calculate Modulus of exponent


In this example, we are using the pow() built function third argument that is used to calculate the modulus of exponents result.

Mod1= pow(3,2,3)   #it will be 9%5  
Mod2 = pow(6,2,3)  #it will be 36%3


print('The mode of (3,2,3) :',Mod1)
print('The mode of (6,2,3) :', Mod2)

Output

The mode of (3,2,3) : 0
The mode of (6,2,3) : 0

3. Python Math.pow() to calculate expoentent


The math module pow() function allows us to calculate the exponent of numbers in Python. It takes two arguments base and exponent.

Syntax

math.pow(base,exponent)

Parameters

  • base : This is Number which exponent we have to find.
  • exponent : This is exponent number.It is exponent of base.

Important

  • The difference between inbuilt pow() and Math.pow().The math.pow() converts the both agruments to float and returns float resut.
  • The pow() built takes only three agruments whereas math.pow() two agruments.

Program to exponentiation of a number

In this example, we are going to use the Math module pow() function to calculate the exponent in Python

#python 3 program to 
exponentiation of a number

import math

exp1 = math.pow(3,3)
exp2 = math.pow(6,3)
exp3 = math.pow(7,2)

print('The exponent of (3,2,3) :',exp1)
print('The exponent of (6,3) :', exp2)
print('The exponent of (7,2) :',exp3)

Output

The exponent of (3,2,3) : 27.0
The exponent of (6,3) : 216.0
The exponent of (7,2) : 49.0

4. Math.exp() calculate exponent of Number


The math module math.exp() method returns exponential float value of EX. It takes one required argument that is the exponent.

import math


print('int number: ',math.exp(6))
print('float negative num:',math.exp(-1.9))
print('float num:',math.exp(1.9))

Output

int number:  403.4287934927351
float negative num: 0.14956861922263506
float num: 6.6858944422792685

5. Python calculate exponent of complex number


To find the exponential of a complex number we are using the Python cmath module. The exp() method of cmath module takes a complex number as an argument and returns the exponential value of numbers.

import cmath

print (cmath.exp(1 + 5j))

Output

(0.7710737641656674-2.6066264306850795j)

6. Calculate exponent of list elements


Using list comprehension we can easily raise power to list values. In this example, we will use pow() method with each element of the list.

list_values = [
    18,19.5, -17.5, 0.16,
    90, 80, -78, 56,78
]

list_exponents = [pow(ele, 2) for ele in list_values]

print("Original list:", list_values)
print("List element Raised to the power 2:\n", list_exponents)

Output

Original list: [18, 19.5, -17.5, 0.16, 90, 80, -78, 56, 78]
List element Raised to the power 2:
 [324, 380.25, 306.25, 0.0256, 8100, 6400, 6084, 3136, 6084]

7. For loop Calculate exponent of list elements


In this example, we are using the for loop to finding the exponential of list elements with pow() function.

list_values = [
    8,9.5, -1.5, 0.6,
    4, 2, 3,11
]

exponent = [2,3,5,1,4,2,5,2]

result_list = []


for index, element in enumerate(list_values):
 result_list.append(pow(element ,exponent[index]))

print("List element Raised to the power 2:\n", result_list)


Output

List element Raised to the power 2:
 [64, 857.375, -7.59375, 0.6, 256, 4, 243, 121]

8.Numpy.exp() to Calculate exponent of list or array


In this example, we are using NumPy module Exp() method to find the Exponent of the list or array element.To use this module,First we need to import numpy using import numpy as np

import numpy as np
np_array = np.array([8,9.5, -1.5, 0.6,
    4, 2, 3,11])
result = np.exp(np_array)

print("exponential of array:\n", result)

Output

exponential of array:
 [2.98095799e+03 1.33597268e+04 2.23130160e-01 1.82211880e+00
 5.45981500e+01 7.38905610e+00 2.00855369e+01 5.98741417e+04]

Summary

In this post, we have explored 8 ways to calculate Exponent in Python using a built-in function.