Kodeclik Blog

# Python pow()

Python’s pow() is a handy function for exponentiation operations. For instance, to compute 2^4, you can do:

`print(pow(2,4))`

Note that the first argument is the base and the second is the exponent. The output is:

`16`

To see how rapidly powers grow, try this program:

```
for i in range(0,25):
print(pow(2,i))
```

The output is:

```
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
```

## Usage of pow()

pow()’s two arguments can be any integers, floats, or even negative numbers. See for instance:

```
print(pow(2,1))
print(pow(2,-1))
print(pow(2,0.5))
print(pow(2,-0.5))
print(pow(-1,2))
```

The output is:

```
2
0.5
1.4142135623730951
0.7071067811865476
1
```

Let us dissect what is happening. The first line simply computes 2^1 which is just 2. The second line is raising 2 to the negative power of 1, which is really shorthand for ½, or 0.5. The third line is using 0.5 as the exponent which means it is really computing the square root of the base, i.e., square root of 2. The fourth line is similar to the third line but because it is a negative exponent, it is the reciprocal of the previous value. Finally, the fifth line is raising a negative number to an even exponent, so the result is positive.

## pow() with a third parameter

Consider the program:

```
for i in range(1,10):
print(pow(10,1,i))
```

Here the first two arguments are used to compute 10^1 or just 10. We are then applying the modulus operator repeatedly on 10 with a range of arguments, from 1 to 9. The output is:

```
0
0
1
2
0
4
3
2
1
```

To decipher what all these numbers mean, here is a handy print statement you can substitute in place of the earlier one:

```
for i in range(1,10):
print("10 when divided by "+str(i)+" gives a remainder of "+str(pow(10,1,i)))
```

The output is:

```
10 when divided by 1 gives a remainder of 0
10 when divided by 2 gives a remainder of 0
10 when divided by 3 gives a remainder of 1
10 when divided by 4 gives a remainder of 2
10 when divided by 5 gives a remainder of 0
10 when divided by 6 gives a remainder of 4
10 when divided by 7 gives a remainder of 3
10 when divided by 8 gives a remainder of 2
10 when divided by 9 gives a remainder of 1
```

This is now self-explanatory. For the numbers that are factors of 10, i.e., 1,2,5 we get a remainder of 0 when this number is used as the third argument of pow(). For others, you can verify that the function is working as intended.

## pow() with complex numbers

Incidentally, depending on the arguments given to pow() the result could be a complex number. Consider for instance finding the square root of negative 1, which is the imaginary number “i” (denoted by “j” in electrical engineering and in Python circles). Here is a program to compute it:

`print(pow(-1,0.5))`

The output is:

`(6.123233995736766e-17+1j)`

Wow - what does this mean? Note that the result is a complex number which has a real part and an imaginary part. The real part is 6.123233995736766e-17, an exceedingly small number that can be taken to be zero. The imaginary part is the “+1j” part which here denotes square root of -1.

Similarly if we try:

`print(pow(-4,0.5))`

We will get the numerical approximation to “2j”, like so:

`(1.2246467991473532e-16+2j)`

If you liked pow(), you will also enjoy learning

about Python's double slash operator.

Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.