Python Exponent

Posted in /   /  

Python Exponent
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    Exponential is a mathematical operation in which a number is multiplied by itself up to a certain time. It is also known as the power of something. Let’s discuss how we can use the Python math operations and inbuilt function and method to find the exponent of a number.

    How to Calculate Exponents of a Number in Python Programming?

    An exponent of a number is defined by multiplying the number itself up to a certain time. In mathematics, we represent exponent like this - a n , where a will be multiplied by itself n times. In Python, we can use the following three methods to find the exponential value of a number:

    • By using the ** operator.
    • Python built-in pow() method.
    • math.pow() method.

    1. Calculate the exponent value of a number using the Python ** operator

    In Python, ** symbol represents the exponential operator or Power operator. This operator needs two values to operate. The value on the left side represents the number whose exponential value we need to find out, and the value on the right side represents the power of the value.

    Syntax

    a ** n

    Python exponent operator examples

    print(3**3) # this is equal to 3 *3 *3
    print(2**3) # this is equal to 2 * 2 *2
    print(5**2) # this is equal to 5*5

    Output

    27
    8
    25

    2. Calculate the exponent value of a number using the Python pow() method

    Python provides an inbuilt pow() method to find out the exponential value of a number. The pow(a, n) method accepts two values as parameters a and n , and it returns a numeric value by raising the value a to the power n .

    Syntax

    pow(a,n)

    The above syntax represents a n

    Python pow() method examples

    print(pow(2,3)) # 2*2*2
    print(pow(3,4))  # 3*3*3*3
    print(pow(4, 2))  #4*4

    Output

    8
    81
    16

    3. Calculate the exponent value of a number using Python math.pow() method

    math is a Python inbuilt module that is inherited from the C programming language. The math module also contains a pow() method that works similar to that of the Python pow() method. However, the only difference is that the math.pow() method always returns a float value whereas the pow() method can return integers as well as floating-point values, it depends on the passed parameters.

    Syntax

    math.pow(a,n)

    The above syntax represents a n

    Python math.pow() method examples

    import math
    print(math.pow(2,3)) # 2.0*2.0*2.0
    print(math.pow(3,4))  # 3.0*3.0*3.0*3.0
    print(math.pow(4, 2))  #4.0*4.0

    Output

    8.0
    81.0
    16.0

    Calculate the Exponential Value of a List

    If you have a list of numbers and you want to find the square or cube value for every number present in the list,  you can do that by using the map() and pow() or math.pow() methods.

    Calculating the exponential value of a list using the Python pow() method

    my_list = [2,4,23, 67,34,84,47]
    #square every number
    sq_list = list(map(lambda a:pow(a,2), my_list))
    print(sq_list)

    Output

    [4, 16, 529, 4489, 1156, 7056, 2209]

    Calculating the exponential value of a list using Python math.pow() method

    import math
    my_list = [2,4,23, 67,34,84,47]
    sq_list = list(map(lambda a:math.pow(a,2), my_list))
    print(sq_list)

    Output

    [4.0, 16.0, 529.0, 4489.0, 1156.0, 7056.0, 2209.0]

    Difference Between Python pow() and math.pow() Methods

    While both the methods are similar, the only difference is that the pow() method returns an integer value if the output is supposed to be an integer. But the math.pow() method will always return a Floating point number.

    Python pow() Python math.pow()
    >>> pow(2,3)
    8
    pow() returned an integer value 8
    >>> math.pow(2,3)
    8.0
    math.pow() returned a floating value 8.0
    >>> pow(2, 0.5)
    1.4142135623730951
    >>> math.pow(2, 0.5)
    1.4142135623730951

    Conclusion

    In this article, we have discussed the different Python methods to find out the exponent of a number. The exponent operator ** and pow() methods are the same and return the same value. On the other hand, the math.pow() function always returns a floating-point number. Choosing a method to find the exponent of a number entirely depends on your preferences.

    People are also reading:

    Leave a Comment on this Post

    0 Comments