# Python pow() function Explained [Easy Examples]

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

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)

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)

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)

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)

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 sum 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.

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.
{"ft_lang_label":"__label__en","ft_lang_prob":0.695248,"math_prob":0.99269485,"size":9400,"snap":"2021-43-2021-49","text_gpt3_token_len":2280,"char_repetition_ratio":0.20072371,"word_repetition_ratio":0.22696766,"special_character_ratio":0.25680852,"punctuation_ratio":0.12162877,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998196,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-18T07:43:30Z\",\"WARC-Record-ID\":\"<urn:uuid:fb1649a0-ebb5-47a6-a45d-6e917c7132e4>\",\"Content-Length\":\"173322\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1eb23eb5-5cd4-4719-9664-46b15f4c545e>\",\"WARC-Concurrent-To\":\"<urn:uuid:2d1c2a0b-d149-4b7d-b1ce-26b23433ecf4>\",\"WARC-IP-Address\":\"104.19.154.92\",\"WARC-Target-URI\":\"https://www.golinuxcloud.com/python-pow-function-examples/\",\"WARC-Payload-Digest\":\"sha1:TLKLUFSW5UYV4RPMF3TZEUEZV7XFD42K\",\"WARC-Block-Digest\":\"sha1:UU5OTOJRTSZFNSWM2SK3MYYYTCIBG2BG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585199.76_warc_CC-MAIN-20211018062819-20211018092819-00491.warc.gz\"}"}