Python pow() Function: Use, Syntax, and Examples

Python pow() function: In this tutorial, we will learn about the pow() function in Python with its use, syntax, parameters, returns type, and examples. By Yash Khandelwal Last updated : June 24, 2023

Python pow() function

The pow() function is a library function in Python, it is used to get the x to the power of y, where x is the base and y is the power – in other words we can say that pow() function calculates the power i.e. x**y – it means x raised to the power y.

pow() function with two parameters

When the pow() function is called with the two parameters (x, y), the result is equivalent to x**y.

pow() function with three parameters

When the pow() function is called with the three parameters (x, y, z), the result is equivalent to (x**y)%z.

Consider the below example with sample input/output values:

Input:
x = 2   # base
y = 3   # power
z = 3   # value for modulus

print(pow(x, y))
print(pow(x, y, z))
    
Output:
8
2

Syntax

The following is the syntax of pow() function:

pow(x, y [,z])

Parameter(s):

The following are the parameter(s):

  • x – A base number which is to be powered
  • y – A value of the power
  • z – It's an optional parameter, it is used to define/fine the modules of the result of (x**y).

Return Value

The return type of pow() function is <type 'int'>, it returns the result of the calculation.

The pow() function results with different types of the values, consider the below given table,

X Y Z
Negative or Non Negative Integer Non-negative Integer May or may not be present
Negative or Non Negative Integer Negative Integer Should not be present

Python pow() Function: Example 1

# python code to demonstrate example of 
# pow() function 

x = 2   # base
y = 3   # power
z = 3   # value for modulus

# calcilating power with two arguments
result1 = pow(x, y)
# calcilating power & modulus with three arguments
result2 = pow(x, y, z)

# printing the values
print("result1: ", result1)
print("result2: ", result2)

Output

result1:  8
result2:  2

Note: pow() can return integer and float both values depend on the condition/ values.

Python pow() Function: Example 2

# python code to demonstrate example of 
# pow() function 

x = 4   # base
y = 3   # power
z = 6   # value for modulus

print("With 2 args:", pow(x,y))     #first taking 2 args only
print("With 3 args:", pow(x,y,z))   #then all the 3 args

print("Return float values:", pow(2,-3))

print('Random numbers power:' , pow(5,5,6))

Output

With 2 args: 64
With 3 args: 4
Return float values: 0.125
Random numbers power: 5 

Different approaches to calculate the power of any number

  1. By using simple approach: (x**y)
  2. By using pow() function: pow(x,y)
  3. By using math.pow() function – which is a function of "math" library

Note: pow() function takes three arguments (the last one is optional), where math.pow() takes only two arguments. In this, there is no third parameter present else all the functionality is the same as simple pow(). But the math.pow() always returns float values. So if you, for some reason, want to make sure you get float as a result back, then math.pow() will provide that benefit to the user.

Example 1: Calculating X to the power Y using different approaches

# python code to demonstrate different 
# approaches to calculate x to the power y

import math 

x = 2   # base 
y = 3   # power

result1 = x**y
result2 = pow(x,y)
result3 = math.pow(x,y)

print("result1 (normal approach): ", result1)
print("result2 (using pow() function): ", result2)
print("result3 (using math.pow() function): ", result3)

Output

result1 (normal approach):  8
result2 (using pow() function):  8
result3 (using math.pow() function):  8.0

Example 2: pow() vs math.pow() with third parameter

# python code to demonstrate different 
# approaches to calculate x to the power y

import math 

x = 2   # base 
y = 3   # power
z = 3   # for moduls

print("pow(x,y,z): ", pow(x,y,z))

# following statement will throw an error
print("math.pow(x,y,z): ", math.pow(x,y,z))

Output

pow(x,y,z):  2
Traceback (most recent call last):
  File "/home/main.py", line 12, in <module>
    print("math.pow(x,y,z): ", math.pow(x,y,z))
TypeError: pow expected 2 arguments, got 3


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.