Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Feature»Python Power Function
    Feature

    Python Power Function

    March 23, 2023No Comments4 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Introduction to Python Power Function

    Python’s Power function makes exponentiation operations very straightforward. Similarly known as the technique of exponentiation in mathematics. Let us first examine the mathematical understanding behind the Exponentiation.

    method.

    Methods in Exponentiation

    Exponentiation is a mathematical process involving the two integers x and y, where x represents the base and y represents the exponent or power. With various values of y, the meaning of exponentiation varies.


    • y>0:

      When y is positive, then the result of exponentiation would be repeated multiplication of the base. Example: – 2

      4

      = 2*2*2*2 = 16 (the base i.e 2 multiplied repeatedly exponent i.e. 4 number of times)

    • y=0:

      When y is 0, then the result of the exponentiation would be 1. Example: – 2

      0

      = 1

    • y<0:

      When y is negative, then the result of the exponentiation would be the repeated division of the base. Example: – 2

      -2

      = ¼

    Now that we’ve examined the mathematical idea behind the Power Function, let’s move on to its programming side.

    it.

    Power Function in Python

    Programming as it exists now has evolved since its conception. When the complexity of programming logic increases over time, it becomes vital to understand all calculating techniques. In our programmes, we often need to determine the power of a number, and it is in these instances that knowledge of this function comes in helpful.

    Consider a Python example of a user-defined power function. Code:

    base = int(input("Enter base:"))
    power = int(input("Enter power:"))
    n = 1
    for i in range(1,power+1):
    n=base*n
    print ("The value of",base,"**",power," is",n)
    Output:

    Python Power Function Description:

    • First Iteration i.e. n=1: n = base*n = 2*1 = 2
    • Second Iteration i.e. n=2: n = base * n = 2*2 = 4
    • Third Iteration i.e. n=3: N = base * n = 2*4 = 8


    X


    Y

    Z


    Return Value


    Non-negative Integer
    Non-negative Integer N/A
    Integer

    Non-negative Integer
    Negative Integer N/A
    Float

    Negative Integer
    Non-negative Integer N/A
    Integer

    Negative Integer
    Negative Integer N/A
    Integer

    Negative/Non-negative Integer
    Non-negative Integer Negative/Non-negative Integer
    Integer

    With Python’s Exponent Arithmetic Operator, the above line of code may be simplified. The Exponent Arithmetic Operator (**) facilitates the Exponentiation procedure. Example: – 2**3 = 8

    However the power function was invented to provide the exponentiation process greater flexibility. The syntax for the power function is shown below. Syntax:

    pow(x, y[, z])

    Parameters

    Let us discuss the power’s specifications.

    function:


    • x:

      x denotes the base number

    • y:

      y denotes the exponent value

    • z:

      z is an optional variable and is used to derive the modulus of the power of x and y.

    Examples to Implement Power Function

    The third parameter distinguishes and strengthens the pow function. The return values for the various combinations of x,y, and z values that are permitted by the pow function are listed below.

    types:

    Example #1

    Nonnegative x and y integers, except z Code.

    base = 3
    power = 4
    result = pow(base,power)
    print(base,"raised to the power of",power,"gives",result)
    Output:

    Python Power Function

    Example #2

    x is positive and y is negative, but there is no z. Code:

    base = 3
    power = -4
    result = pow(base,power)
    print(base,"raised to the power of",power,"gives",result)
    Output:

    Python Power Function

    Example #3

    Negative x and positive y, but no z Code:

    base = -3
    power = 4
    result = pow(base,power)
    print(base,"raised to the power of",power,"gives",result)
    Output:

    Python Power Function

    Example #4

    Without z, Negative x and Negative y. Code:

    base = -3
    power = -4
    result = pow(base,power)
    print(base,"raised to the power of",power,"gives",result)
    Output:

    Python Power Function

    Example #5

    x and y are nonnegative when combined with z. Code:

    base = 5
    power = 3
    modulus = 10
    result = pow(base,power,modulus)
    print(base,"raised to the power of",power,"and thereby applying the modulus on",modulus,"gives",result)
    Output:

    Python Power Function

    Other Power Functions in Python

    There are three other techniques for calculating the power, in addition to the one stated above.

    function.

    1. Using the pow function of Math Package

    Code:

    import math
    base = 2
    power = 5
    print(math.pow(base,power))
    Output:

    Python Power Function

    2. Using the power function of the Numpy Package

    Code:

    import numpy as np
    base = 2
    power = 5
    print(np.power(base,power))
    Output:

    Python Power Function

    3. Using the power function of the Scipy Package

    Code:

    import scipy
    base = 2
    power = 5
    print(scipy.power(base,power))
    Output:

    0

    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language Python Power Function
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleDifference between Property and Attributes in Python
    Next Article Versions of Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.