Skip to main content
how to do exponents in python

How To Do Exponents in Python

This python tutorial help to exponent the number. The exponent is referred to the number of times a number is multiplied by itself.

Exponent Example – 5^3

The exponent will be of above – 5 * 5 * 5 = 125

There are several methods for calculating a number’s exponents. I’ll go over each one with an example of code.

Checkout other python tutorials:

Option 1: Using ** for calculating exponent in Python

The easy and common way of calculating exponent in python3 is double asterisk (**) exponentiation operator.

Let’s take simple example to calculate exponents –

#test.py
exp1 = 2**3

exp2 = 3**4

exp3 = 5**3

print("The exponent of 2**3 = ",exp1)
 
print("The exponent of 3**4 = ",exp2)
 
print("The exponent of 5**3 = ",exp3)

The Results :

The exponent of 2**3 = 8
The exponent of 3**4 = 81
The exponent of 5**3 = 125

The pow() function to calculate Exonenent

The python also has an inbuilt method pow() that can be used to calculate exponent. You don’t need to include any module like math to access this method. The 3^2 is also called “3 to the power 2” and is used to the exponent of a number.

The syntax for using the pow() function is:

pow(x, y[, z])

The third parameter is the modulo.

The example of pow() function

We’ll compute the exponent of an int, a floating number, and a negative number.

pow1 = pow(3, 4)
 
pow2 = pow(2.5, 3)
 
pow3 = pow(-3, 4)
 
pow4 = pow(3, -4)
 
print("The result of pow(3, 4) = " ,pow1)
 
print("The result of pow(2.5, 3) = " ,pow2)
 
print("The result of pow(-3, 4) = " ,pow3)
 
print("The result of pow(3, -4) = " ,pow4)

The Results :

The result of pow(3, 4) = 81
The result of pow(2.5, 3) = 15.625
The result of pow(-3, 4) = 81
The result of pow(3, -4) = 0.012345679012345678

Math pow() function

The math module also includes a function called pow() that can be used to calculate a number’s exponent. The math pow() function converts both of its arguments to the float type.

The syntax for pow() function is:

math.pow(x, y)

It returns x raised to power y.

import math

m_pow1 = math.pow(5, 3)
 
m_pow2 = math.pow(4.5, 3)
 
print("The result ofpow(5,3) = " ,m_pow1)
 
print("The result of pow(4.5, 3) = " ,m_pow2)

The Results –

The result ofpow(5,3) = 125.0
The result of pow(4.5, 3) = 91.125

Leave a Reply

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