The pow python function is a built-in function used to calculate the power of the given numbers. We use the built-in function to find out x to the power of y modulus z.
pow() Parameters
The pow python function takes three parameters as input from the user. The parameters taken from the user are x, y, and z. The first two parameters, x & y, must be given. The first parameter, ‘x,’ is an integer, which is the base. The second parameter, ‘y,’ is an integer, which is an exponent. The third parameter is optional. It is an integer used for the modulus calculation.
For example:
Consider the example used below. It returns the value of 4 to the power of 7, modulus 5 (same as (4 * 4 * 4 *4 * 4 * 4* 4) % 5):
Let us see more examples using the pow():
z= pow(4,7,5)
print (z)
Output:
4
Example 1: Python pow()
# x is a positive number, y is a positive number(x**y)
print(pow(3, 3)) # 27
# y is a positive number, while x is a negative number.
print(pow(-3, 3)) # -27
# y is a negative number, while x is a positive number.
print(pow(3, -3)) #0.037037037037037035
# x and y are negative
print(pow(-3, -3)) #-0.037037037037037035
Output
27
-27
0.037037037037037035
-0.037037037037037035
Example 2: pow() with three arguments (x**y) % z
Source Code:
ax = 7
by = 8
cz = 5
print(pow(ax, by, cz)) # 1
Output
1
What is pow() in Python?
The pow() is used to calculate the power of a number. It uses three parameters x,y, and z. With the help of these three parameters, the users can calculate the power of any number.
Traditionally, loops were used for calculating the power of numbers.
For example:
In the program given below the user has used the for loop to calculate the power of n.
Source Code:
a = 1
for i in range(1,5):
a=3*a
print ("The output of 3**4 is")
print (a)
Output:
The output of 3**4 is:81
How do you use POW in Python?
The pow python function is used to calculate the powers of the numbers. Based on the number of parameters, taken by the user this function is used to return different values.
For Example:
- float pow(x,y)- The parameters are taken by the user is first converted into floating-point numbers. After this, the pow() is used to compute the value x**y.
Example:
Source Code:
print ("The output of 5**4 is: ",end="")
# Returns 625
print (pow(5,4))
Output:
The output of 5**4 is: 625.0
- float pow(x,y, mod): After converting the parameters taken by the user into floating-point numbers the pow() is used to compute the value of (x**y) % z.
Example:
Source Code:
print ("The output (5**4) equals: ",end="")
# Returns 625 %10
# Returns 5
print (pow(5,4,10))
Output:
The output (5**4) equals: : 1
What does math Pow do in Python?
The math. pow() takes two numbers as the input from the user x and y. The parameter x given by the user is raised to the power of y.
Example:
import math
#The output will be 9 raised to the power of 3
print(math.pow(9, 3))
Output
729.0
How do you type to the power of 2 in Python?
In python, we can raise a number to the power of another number by using exponents.
For example:
If you want to raise 10 to the power of 2, you can write:
10**2.
Another method of typing any number to the power of 2 is using the pow():
Example:
x = pow(4, 2)
print(x)
Leave a Reply