Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How to Implement Power Function in Python

Last updated on Nov 27,2024 24.3K Views


In today’s modern times, Python is undoubtedly one of the most prominent and popular programming languages out there. Python comes with a host of different functions each built specifically to add more versatility to the interface than before. One such function of the Python ecosystem is the power function, also represented as the pow(). In this article, we will discuss the Power function in Python.

    Introduction to the Power Function in Python?

    The power function in Python can be used when one needs to derive the power of variable x to the variable y. If in a particular situation, the user includes a third variable that is z into the equation, then the pow() function returns x to the power of y, modulus of z. This in mathematical terms, looks something like this, pow(x, y) % z.

    The syntax for Power function:

    pow(x, y[, z])

    If you are computing the pow (x,y) then the output will be, x**y.

     

    Parameters of the Power Function

    Now that you are accustomed to the basics of the Power Function in Python, let us explore the various parameters that are involved in the creation of the same. 

    When using the power method three parameters are taken into account. 

    1. X: x denotes the number which is to be powered. 

    2. Y: y denotes the number which is to be powered with x.

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

    Parameter Cases for Power method 

    1. X: X can either be a non-negative integer or a negative integer whenever it is being used. 

    2. Y: Y can also be a non-negative integer or a negative integer when used in the equation. 

    3. Z: In most cases, z is an optional variable and may or may not be present.

     

    Return Values for Pow()

    Dependent upon the situation it is used at, the power method returns several different variables. Some of the most significant ones are as mentioned below.

    XYZReturn Value
    Non-negative Integer
    Non-negative IntegerN/AInteger
    Non-negative IntegerNegative IntegerN/AFloat
    Negative IntegerNon-negative IntegerN/AInteger
    Negative IntegerNegative IntegerN/AInteger
    Negative/Non-negative IntegerNon-negative IntegerNegative/Non-negative IntegerInteger

     

    Example Code

    • Example 1:
    # positive x, positive y (x**y)
    print(pow(2, 2))
    
    # negative x, positive y
    print(pow(-2, 2))
    
    # positive x, negative y (x**-y)
    print(pow(2, -2))
    
    # negative x, negative y
    print(pow(-2, -2))

    Output:

    Power-Function-in-Python-Output

     

    • Example 2:
    x = 7
    y = 2
    z = 5
    
    print(pow(x, y, z))

    Output:

    4

     

    • Example 3:
    # Python code to demonstrate naive method 
    # to compute power 
    n = 1
    for i in range(1,5): 
    	n=3*n 
    
    print ("The value of 3**4 is : ",end="") 
    print (n)

    Output:

    The value of 3**4 is : 81

     

    • Example 4:
    # Python code to demonstrate pow() 
    # version 1 
    
    print ("The value of 3**4 is : ",end="") 
    
    # Returns 81 
    print (pow(3,4))

    Output:

    The value of 3**4 is : 81.0

     

    • Example 5:
    # Python code to demonstrate pow() 
    # version 2 
    
    print ("The value of (3**4) % 10 is : ",end="") 
    
    # Returns 81%10 
    # Returns 1 
    print (pow(3,4,10))

    Output:

    Output-Power

     

    The power function in Python, when used correctly can eliminate a lot of stress and confusion. We hope that from this article you have learned how to use the power function in Python correctly and thus will make use of the same in your day to day programming.

    With this, we come to an end of this Power Function in Python article. To get in-depth knowledge on Python along with its various applications, you can enroll now for live Python course training with 24/7 support and lifetime access.

    Got a question for us? Mention them in the comments section of this article and we will get back to you.

    Upcoming Batches For Python Programming Certification Course
    Course NameDateDetails
    Python Programming Certification Course

    Class Starts on 28th December,2024

    28th December

    SAT&SUN (Weekend Batch)
    View Details
    Python Programming Certification Course

    Class Starts on 25th January,2025

    25th January

    SAT&SUN (Weekend Batch)
    View Details
    Comments
    0 Comments

    Join the discussion

    Browse Categories

    webinar REGISTER FOR FREE WEBINAR
    REGISTER NOW
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

    Subscribe to our Newsletter, and get personalized recommendations.

    image not found!
    image not found!

    How to Implement Power Function in Python

    edureka.co