# pow() function with example in Python

Python pow() function: Here, we are going to learn about the pow() function in Python with example.
Submitted by Yash Khandelwal, on April 02, 2019

## Python pow() function

pow() function is a library function in Python, 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.

Syntax:

```    pow(x, y [,z])
```

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: numer – returns result.

Example:

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

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

Output:
8
2
```

Note:

• pow() function with two arguments (x,y) – it is equivalent to x**y
• pow() function with three arguments (x,y,z) – it is equivalent to (x**y) % z

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

Example1:

```# 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.

Example2:

```# 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
```

Example2: 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
print("math.pow(x,y,z): ", math.pow(x,y,z))
TypeError: pow expected 2 arguments, got 3
```

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates