AppDividend
Latest Code Tutorials

# Python pow: How to Calculate Power in Python

Python pow() function computes a**b. The pow() function first converts its arguments into float and then computes the power. So, if you want to compute power operation then pow() function is your solution.

## Python pow() Example

Python pow() is an inbuilt function that returns x to the power of y. If the third argument (z) is given, it returns x to the power of y modulus z, i.e., pow(x, y) % z. The pow() function returns a value of x to the power of y (xy). If the third parameter is present, it returns x to the power of y, modulus z.

### Syntax

The syntax of the pow() method is the following.

`pow(a, b, c)`
ParameterDescription
aA number, the base
bA number, the exponent
cOptional. A number, the modulus

1. If only two arguments are provided, then a to the power of b is returned. In this case, the arguments can be integers, floats, and complex numbers. The two-argument form of pow(a, b) is equivalent to using the power operator: a**b.
2. If three arguments are provided, then a to the power b, modulo c is returned. It’s computed more efficiently than using pow(a, b) % c.
3. If c is present, a and b must be of integer types, and b must be non-negative.

See the following example.

```# app.py

data = pow(4, 3)
print(data)```

See the output.

```➜  pyt python3 app.py
64
➜  pyt```

Let’s pass the third parameter and see the output.

```# app.py

data = pow(4, 3, 10)
print(data)```

So, what is does is first 4*4*4, which is 64 and then 64%10, which is 4. That is why it will give us output 4.

```➜  pyt python3 app.py
4
➜  pyt```

## #Python pow() with floats

We can also calculate the power of float values using the pow() method.

See the following code example of floats.

```# app.py

print(pow(11, 2.0))

print(pow(21.0, 2))```

See the output.

```➜  pyt python3 app.py
121.0
441.0
➜  pyt```

## #Python pow() with different format integers

We can calculate the power of different format integers using the pow() method.

See the following code example.

```# app.py

print(pow(0b11, 2))
print(pow(0b11, 2, 2))

print(pow(0o11, 2))
print(pow(0o11, 3, 2))

print(pow(0xF, 2))
print(pow(0xF, 3, 2))```

See the following output.

```➜  pyt python3 app.py
9
1
81
1
225
1
➜  pyt```

## #pow() with complex numbers

We can calculate the power of complex numbers using the pow() method.

```# app.py

print(pow(11 + 21j, 2))```

See the output.

```➜  pyt python3 app.py
(-320+462j)
➜  pyt```

## #pow() vs math.pow()

Python math module also has the pow() function, but the built-in function is more powerful because we can perform the modulo operation too after power.

Also, we don’t need to import the math module for a single functionality.

So, it is preferable to use Python built-in pow() function and not the math.pow() function.

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.

Finally, Python pow() Example Tutorial is over.