TeachingBee

5 Quick Ways to Use Exponents in Python

exponents in python

Introduction

In this article, we’ll be looking into how to calculate the exponents in Python by using various methods. However, before starting first we need to learn about mathematical definition of exponents.

You might have noticed exponential value in maths which can be described as Px. It is a simple concept of multiplying P, x number of times. P” here is known as a base number and x is referred to as the value of an exponent.

For eg. 25 = 2 * 2 * 2 * 2 * 2 = 32

In this case, 2 is the base value as well as 2 an exponent, therefore we multiplied 2 five times.

There are many ways of finding the exponential value in Python. Let’s look at them step by step.

Python Loops

The first way to calculate exponents in Python is by using loops. Loops can help us run the code block over and over, to gain its benefits for finding the exponential value of Python.

base = 2
exponent = 5

output = base
 
if exponent != 0:
    for i in range(exponent - 1):
         output = output * base
else:
    output == 1
 
print("Exponent using python loops is : ", output) # 32

In the above code, we have taken the base value of 2 and the exponent was 5. We used the variable result and then initialised the base value.

Here , the range of the for loop is set to between 0 and 5 to repeat the loop 4 times.

The time complexity for the above scenario is O(n) and the space complexity is O(1).

In this case, we repeat the loop numerous times in order to determine the final value. However, we can use simpler methods to calculate the exponential value in Python.

In Python, we have an exponentiation function that is one method to determine how much exponential is base and exponent values.

Exponent Operator In Python

We use the (**) double asterisk/exponentiation operator in between the base value and exponent value

In the previous example we used base 2 and the exponent of 5. Then, 2 is multiplied by 5 times. This is the easiest method of calculating the value of the exponential function in Python.

base = 2
exponent = 5
 
print("Exponential value using Operator is : ", base ** exponent) #32

We can also use floating-point numbers when calculating exponential value. When we apply any operand to represent a floating point number, the result is in the floating point number as illustrated in the code fragment.

print(5.1 ** 2.3) #42.404447108809826

ZeroDivisionError in Python

If we set 0 to negative power, we will get the error below. ZeroDivisionError will occur as 0 cannot be increased to a negative value

Pow Function Python

Python provides wide variety of built-in functions and pow() is one of them that aids us calculate an exponents in python

With the pow() function you can input the base value as well as the exponent value. These values may be of different types of data, such as integers, floating and complex.

pow(base, exponent) does the same work as exponent operator in python.

base = 2
exponent = 5
 
output = pow(base, exponent)
 
print("Exponential value using built in function : ", output)

In the pow() function, three parameters are allowed. The first two arguments are exponent and base value, however we can also provide the third argument which is optional, that calculates the modulus  of the exponential value.

The time complexity for computing the exponential value using squares the value is O(Log(exponent)).

Let’s look at some examples.

It is suggested to make use of pow(5,3,2) in place of pow(5,3)%2 since the efficiency is higher in calculating the exponential modulo value.

print("Pow method using 3 arguments:", pow(10, 5, 6)) # Example 3 : 4

These operations can be performed with negative numbers, too:

print(pow(-2, 3)) # -8
print(pow(-5, -3)) # -0.008

When making use of a negative value within the pow function python, we must be aware of certain things when using a negative number. Because the pow function converts its argument to floating point and then calculates its amount of power it calculates, you can observe some distinctions in return types.

Math Module’s Pow Function Python

In addition to using the pow built in() function, we also are also able to use also the math.pow() function, which is part of the math module in Python that includes a variety of mathematical functions.

This function could also be utilised to calculate the value of exponential in Python.

Math.pow() illustrates O(1) time complexity because of the floating-point exponentiation, which is superior to using the pow built into() functions. however due to this, it loses the accuracy of the output result.

Math.pow() function accepts just two arguments as opposed to the three arguments used in built-in pow function python.

Math.pow() method always produces a float number, whereas with the built-in pow() function, we receive integer values the majority times.

import math

print(math.pow(5, 2)) #25.0

Math Module’s Exp Function Python

There is a function known as exp() in the math module, which utilizes the value of e as a base. The value of e is about 2.71828.

This value of e can be considered to be the basic value. The value of the exponent is provided in the form of an argument to the function

import math
 
exponent = 3
 
# The value of e is taken as the base value
output = math.exp(exponent)
 
# output
print("Exponential value using exp function: ", output) #20.085536923187668

Calculating Exponents in Python for Complex Numbers

To calculate the python exponent for a complex number , we need to import Python’s cmath module. This exp() method in the cmath module uses complex numbers in its argument, and then returns an exponential number for numbers.

import cmath
 
print (cmath.exp(1 + 5j)) #(0.7710737641656674-2.6066264306850795j)

Let’s Compare

TechniqueTime Complexity
Python LoopsO(n)
Exponent Operator Python (**)O(Log(n))
Built-In Pow Function PythonO(Log(n))
Math.Pow Function PythonO(1)
*n represents exponent value

Why is time complexity O(1) for Pow Function?

  • Built-In Pow Function and Exponent Operator perform Integer Exponentiation when the arguments are both integers. This takes O(log(b)) as exponentiation is done via squaring.The time complexity is majorly dependent on the specifics of the algorithm used for multiplication.The algorithm that Python does employ still requires O(log(b)) multiplications.
  • Math.Pow() on contrary is a distinct. It is always a floating point exponentiation, and it is O(1). The floating point exponentiation is less accurate. In cases in which the non-constant complexity of the integer exponentiation is relevant, math.pow will provide less precise results, or even throw the overflow error.

Conclusion

The article discussed how exponential numbers work, and ways to determine them by using various methods in Python.When using the looping technique you have to write lots of code which is not time efficient for bigger values. The exponent operator in python, is among the most straightforward methods to determine the value of exponential.

Built-in Pow function python accepts 2 required and third argument as optional. It assists us in determining the exponential value if 2 arguments are provided and, when we add the third argument, then the exponential value’s modulus is calculated. Math Module’s pow function python is the quickest method to calculate the exponential value using the time complexity O(1).

Exp() function calculates the exponential value using the base e as well as the exponent that we have provided to the argument.

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Complex Data Type In Python

Complex Data Type In Python

The complex() function is an essential building block in Python for working with complex numbers across scientific computing, signal processing, physics, engineering, and more fields. This article provides a comprehensive

list function python

Comprehensive Guide To List Function Python

Introduction The list() function in Python is used to create new lists from existing iterables like strings, tuples, dictionaries etc. The list type is one of the most commonly used

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In