Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Python Power Function?
3.
Parameters of the Python Power Function
4.
Return Values for Python Power Function
5.
How can we do it without using the pow() function
5.1.
Using loop
5.2.
Using Recursion
5.3.
Using bit masking
6.
Frequently Asked Questions
6.1.
Can python power take 3 arguments?
6.2.
Why do we use the python power function?
6.3.
How do you write powers in Python?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python Pow() Function

Introduction

Hey Ninjas!! Welcome to an article on python power.

Python is, without a doubt, one of the most well-known and widely used programming languages available today. Python includes various functions, each created to increase the interface's adaptability. The Power operator in Python, often known as pow(), is a component of the Python ecosystem. 

The article explains the details of the python power function. Let's get started.

python power

Also see, Intersection in Python, and Convert String to List Python

What is Python Power Function?

When calculating the power of a variable to another, one can use the python power function. The Python Pow() function returns u to the power of v, modulus of z if the user adds the third variable z to the equation in a certain circumstance. This has a mathematical representation that is similar to pow(u, v)% z.

The syntax for the python power function:

pow(u, v[, z])


If you calculate the pow (u,v), the Output will be u**v.

Parameters of the Python Power Function

Now that you are familiar with the fundamentals of the Python Pow() Function let's explore the many parameters that go into its development.

Three factors are considered while applying the python power approach:

  • U: The number to be powered is denoted by the letter u.
     
  • V: v denotes the number that u will power.
     
  • Z: The power modulus for u and v is derived using the optional variable z.
     

Parameter Cases for python power function:

  • Whenever u is used, it may be either a positive or a negative integer.
     
  • When v is employed in the equation, it can also be a non-negative or a negative integer.
     
  • z is typically an optional variable that may or may not exist.
     

You can also read about, Fibonacci Series in Python

Return Values for Python Power Function

The Power operator in Python returns a number of distinct variables depending on the circumstance it is used in. The following list includes some of the more significant ones.

Return Values for Python Power Function

Example - 1

print(pow(4,2))

 

Output

16

 

Example - 2

# positive u, positive v (u**v)
print(pow(3, 3))
 
# negative u, positive v
print(pow(-3, 3))
 
# positive u, negative v (u**-v)
print(pow(2, -2))
 
# negative u, negative v
print(pow(-2, -2))

 

Output

27
-27
0.25
0.25

 

Example - 3

print("The value of (5**4) % 10 is : ", end="")
# Returns 625%10
print(pow(5, 4, 10))

 

Output

The value of (5**4) % 10 is : 5

 

Must read: Python Square Root

How can we do it without using the pow() function

We have three methods to find power without using the Power operator in Python.

  1. Using loop
  2. Using Recursion
  3. Using bit masking

Using loop

Power can be calculated by repeatedly adding. For instance, to calculate 5^4.

Example 

# Works only if both u and v>=0.
def pow(u,v):
    if(v==0):
        return 1

    result=u
    increment=u

    for i in range(1,v):
        for j in range (1,u):
            result+=increment
        increment=result

    return result
print(pow(5,4))

 

Output

625

Using Recursion

To find the product of two numbers, recursively add u. To raise to the power of v, multiply recursively.

Example 

def pow(u,v):
    if(v):
        return multiply(u, pow(u, v-1));
    else:
        return 1;

#Recursive function.
def multiply(x, y):
    if (y):
        return (x + multiply(x, y-1));
    else:
        return 0;

print(pow(5, 4));

 

Output

625

Using bit masking

Example 

def pow(u, v):
    result = 1
    while(v > 0):
        # Determine the last bit of v.
        last_bit = v&1

        if(last_bit):
            result = result*u
    
        # Every subsequent bit was squared.
        u = u*u
        v = v >> 1
    return result

print(pow(5, 4))

 

Output

625


You can compile it with online python compiler.

Check out this article - Quicksort Python

Frequently Asked Questions

Can python power take 3 arguments?

Base, exponent, and modulus are the inputs the built-in pow() function accepts; modulus is the third argument and is optional.

Why do we use the python power function?

The pow() function determines a number's power for every given value.

How do you write powers in Python?

In Python, you can write the power as 2**5 which means 2^5 and you can also use the pow function which takes 2 parameters, the base and the exponent that is pow(2, 5).

Conclusion

In this article, we have discussed the details of the python power function and Methods Power operator in Python.

We hope that the blog has helped you enhance your knowledge regarding the python power function.

Recommended Readings:


You can also consider our paid courses such as DSA in Python to give your career an edge over others! 

Do upvote our python power function blog to help other ninjas grow. 

Happy Coding!

Live masterclass