Table of Contents

## Introduction to Python pow() function

The python `pow()`

function can be used to derive the power of variable x to the variable y. If in a particular situation, the user includes a third variable that is z into the equation, then the math power function returns x to the power of y, modulus of z.

In this tutorial, we will learn about the python `pow()`

function from scratch to an advanced level. We will take different cases with examples and see how we can use the python power function in various different cases. Moreover, we will also cover some of the alternative ways that we can use instead of the math `power()`

function. Finally, we will also take some examples of the python power function with different formatted and complex numbers.

## Getting started with python pow() function

The Python `pow()`

function is a built-in function that returns `x`

to the power of `y`

(for parameters `x`

and `y`

). If the third argument (let say `z`

) is given, it returns `x`

to the power of `y`

modulus z, i.e., `pow(x, y) % z`

. In this section, we will see the syntax of the python `pow()`

function and will solve some real examples.

### Syntax of python pow() function

The python `pow()`

function is a built-in function that comes with python installation. Here is a simple syntax of power function in Python.

`pow(a, b, c)`

It can take up to three-parameter ( c as an optional parameter).

`a`

is the number in which power will be calculated. It can be a negative or positive.`b`

is the value raised to compute power.`c`

is a mod where the result can be taken as a mod. The m parameter is optional.

### Check type of python pow() function

The python `pow()`

method returns an integer value if the integer is given as an argument and it will return float data type if a float number is given. See the example below which confirm it.

```
# python pow() function
num = pow(2, 3)
# python data type
print("data type is: {}".format(type(num)))
```

Output:

`data type is: <class 'int'>`

Notice that the return type is `int`

because we had given integer as an argument. Now see the example below, which returns float data type.

```
# python pow() function
num = pow(2.4, 3)
# python data type
print("data type is: {}".format(type(num)))
```

Output:

`data type is: <class 'float'>`

Notice that this time we get float data type as an output. Let us now take real examples and find the power of a number by using the python power function.

```
# python pow() function
num = pow(2, 3)
# printing the answer
print("Power is: {}".format(num))
```

Output:

`Power is: 8`

Now let us take an example of a float number and see what we get when we apply the python `pow()`

function to the floating number.

```
# python pow() function
num = pow(2.4, 3)
# printing the answer
print("Power is: {}".format(num))
```

Output:

`Power is: 13.823999999999998`

Notice that this time we get a floating number as an output.

### Calculate power and mod using python pow() function

So far we have calculated the power of a number using the power function. We also know that the python `pow()`

function takes an optional parameter as well, which returns the mod of the numbers. Let us now take an example and use that optional parameter to find the mod.

See the following example.

```
# python pow() function
num = pow(2, 3)
# printing the answer
print("Power is: {}".format(num))
# now apply mode
num1 = pow(2, 3, 2)
# printing mod
print("mod of the given number is: {}".format(num1))
```

Output:

```
Power is: 8
mod of the given number is: 0
```

Notice that the mod is `zero`

because we know that `8`

mod `2`

is `zero`

. Now let us find the mod with any other number. See the example below:

```
# python pow() function
num = pow(2, 3)
# printing the answer
print("Power is: {}".format(num))
# now apply mode
num1 = pow(2, 3, 3)
# printing mod
print("mod of the given number is: {}".format(num1))
```

Output:

```
Power is: 8
mod of the given number is: 2
```

This time we get 2 as a mod because 8 mod 3 is 2. Now note one thing that we cannot apply the mod to floating numbers. If we will try to find a mod of floating numbers, we will get an error. See the following example.

```
# python pow() function
num = pow(2.34, 3)
# printing the answer
print("Power is: {}".format(num))
# now apply mode
num1 = pow(2.34, 3, 3)
# printing mod
print("mod of the given number is: {}".format(num1))
```

Output:

Notice that we can only provide the third argument if the other arguments as of int data type.

## Alternatives of Python power function

Python provides a number of ways to find the power of a number. We can use any of the ways to find the power. In this section, we will discuss some of the alternatives that we can use to find the power of a number, rather than using the built-in python pow() function.

### Using a double asterisk to find the power

In python, we can use a double asterisk to find the power of numbers. It is popular and comes under python mathematical operators. The simple syntax looks like this:

`a** b`

where a and be are any numbers. Now let us take a real example and find the power of a number. See the following example.

```
# Alternative of python pow() function
num = 2**3
# printing
print(num)
```

Output:

`8`

Using this method, we can also find the power of floating numbers as well. See the example below:

```
# Alternative of python pow() function
num = 2.3**3
# printing
print(num)
```

Output:

`12.166999999999998`

Notice that we get a floating number as an output.

### NumPy power method

The NumPy is a popular 3rd party Python library that also provides the `pow()`

method. We can install the NumPy library by using the pip command;

pip install numpy

Once the NumPy is installed successfully, we can access the `pow()`

method. The following is the simple syntax of the python NumPy `pow()`

method.

`numpy.power(a, b)`

Now let us take a real example and find the power of numbers using the NumPy `pow()`

method. See the example below:

```
# importing numpy
import numpy
# Alternative of python pow() function
num = numpy.power(2, 3)
# printing
print("power is: {}".format(num))
```

Output:

`power is: 8`

we can also find the power of a floating number using this method as well. See the example below:

```
# importing numpy
import numpy
# Alternative of python pow() function
num = numpy.power(2.3, 3)
# printing
print("power is: {}".format(num))
```

Output:

`power is: 12.166999999999998`

### Scipy library power() method

Scipy is another popular Python library that provides science-related modules where `power()`

the method is also provided. We can install the scipy library using pip command as shown below:

pip install scipy

Once we install scipy library successfully, we can then access to `power()`

function. Here is the simple syntax of the `power()`

function of scipy linrary.

`scipy.power(a, b)`

Now let us solve a real example using the scipy `power()`

function. See the example below:

```
# importing scipy
import scipy
# Alternative of python pow() function
num = scipy.power(2, 3)
# printing
print("power is: {}".format(num))
```

Output:

`power is: 8`

In a similar way, we can use float numbers and print out their power. See the following example.

```
# importing scipy
import scipy
# Alternative of python pow() function
num = scipy.power(2.3, 3)
# printing
print("power is: {}".format(num))
```

Output:

`power is: 12.166999999999998`

## Python pow() function with hexadecimal formatted integers

So far we have seen examples of floating and integer data types using the python pow() function, in this section we will see some more examples which will use hexadecimal formatted integers. For example, see the program below which uses hexadecimal formatted numbers to find the power, but notice that the return type is again a decimal integer.

```
# python pow() function
num = pow(0xF, 3)
# printing
print("the power is: {}".format(num))
```

Output:

`the power is: 3375`

In a similar way, we can provide the third argument and find out the mod as well. See the example below:

```
# python pow() function
num = pow(0xF, 3, 2)
# printing
print("the mode is: {}".format(num))
```

Output:

`the mod is: 1`

## Python pow() function with complex numbers

We already applied the python `pow()`

function on different formatted integer numbers, now let us apply the same method on complex numbers. A complex number is the** s**um of a real number and an imaginary number. It is expressed in standard form when written `a + bi`

where a is the real part and bi is the imaginary part. Let us take a complex number and find the power. See the example below:

```
# pow() function with complex number
num = pow(3 + 2j, 2)
# printing
print("the power is: {}".format(num))
```

Output:

`the power is: (5+12j)`

Notice that this time the output is also a complex number. We can apply python type method to find out the data type. See the example below:

```
# pow function with complex numbers
num = pow(3 + 2j, 2)
# printing
print("the datatype is: {}".format(type(num)))
```

Output:

`the datatype is: <class 'complex'>`

Notice that the data type is complex.

## Summary

Calculating the power of a number is a common mathematical operation. In python, we have the power function which calculates the power of a number. In this tutorial, we learned about the math `pow()`

function in detail. We found the power of differently formatted numbers, floats, and complex numbers through various examples. Moreover, we also learned some alternative ways to find power in python using various libraries. In a nutshell, this tutorial contains everything that you need to know about the python power function.

## Further Reading

Python pow() function

math module in python

Python mathematical operation