# Power Functions

**00:00**
Python’s `math`

module is well known to equip you with powerful functions. At the end of this lesson, you’ll see why.

**00:09**
So, let’s talk about power functions. A power function in mathematics is a function of the form *f*(*x*) equals *x* to the power of *n*. The meaning of *x* to the power of *n* depends a lot on what this number *n* is. Here, *n* is a fixed number, it’s called the power, and *x* is called the base.

**00:30**
When *n* is a positive integer, the notation *x* to the power of *n* means that we take *x* and we multiply it by itself *n* times. If the power *n* is 0, then *x* to the 0 is defined to be 1, but only if *x* is non-zero. When *x* is zero, so in other words, 0 to the power of 0—that’s usually left undefined in mathematics.

**00:56**
Now let’s talk about negative integer powers. *x* to the power of *-m*, if *m* is a positive integer—so we have a negative power—that means 1 divided by *x* to the power of *m*. And *x* to the power of *m* means we take *x* and we multiply it by itself *m* times. Now, if in *f*(*x*) = *x* to the *n*, if the power *n* is not an integer and *x* is negative, then things get a bit tricky. In fact, they get *complex*, meaning that you’ll have to deal with complex numbers.

**01:32**
We’ll talk about complex numbers in a future lesson, but with regards to the power function, things are just a little bit more mathematical that fall outside the scope of this video course.

**01:44**
The `math`

module defines a `pow()`

function, or power function, which computes `x`

raised to the power of `y`

.

**01:53**
So it takes two positional arguments, two numbers, and it’s computing `x`

raised to the power of `y`

. Here, `y`

can be a non-integer but then `x`

must be positive.

**02:06**
And so an edge case is that, okay, if both `x`

and `y`

are finite and `x`

is indeed negative and `y`

is a non-integer, then the power function is going to raise a `ValueError`

.

**02:18**
And lastly, one last thing to note with the power function in the `math`

module is that `0`

raised to the power of `0`

as computed by the `pow()`

function, it’s going to return `1.0`

. So even though in math we usually don’t define 0 raised to the power of 0—we leave it undefined—in the `math`

module, it returns `1.0`

Let’s try some examples with the power function.

**02:45**
Let’s say `2`

to the power of `8`

. You see here that even though the inputs are both integers, the power function from the `math`

module is always going to return a float.

**02:56**
Let’s just verify that. Even though we see it, why don’t we ask for the type? We get a `float`

. Now, if you use the built-in power operator, or the double star operator (`**`

), `2`

to the power of `8`

, here, you’re going to get an integer.

**03:14**
And if you use the built-in power function,

**03:18**
you’re also going to get an integer. Now, negative exponents are okay, so if we try the same thing but `2`

to the power of `-8`

, we saw that this is going to be computing, really, 1 over 2 to the power of 8, and we can also use non-integer powers. So, for example, `2`

to the power of `3.4`

. And same thing, we can have a negative exponent, so `-3.4`

.

**03:49**
And again, this is pretty much going to be equal to `math.pow()`

, `2`

to the power of `3.4`

, but divide. Now, we may not get exact values here because of rounding or truncation, but let’s just give it a try. And we get `True`

. So, again, when we’re raising a base to a negative exponent, it’s really 1 over the base to the positive exponent.

**04:16**
One of the edge cases with `pow()`

from the `math`

module is that when the base is negative, if we’ve got integer powers, then everything’s okay. So again, this is really just `-3`

multiplied by itself `4`

times.

**04:32**
And the same thing if we have a negative exponent. This is going to be 1 divided by -3 to the power of 4, which is 1 over 81. But if the exponent is a non-integer and we have a negative base—so, for example, `0.5`

—then power is going to raise a `ValueError`

saying that `math domain error`

.

**04:58**
Now let’s do a quick test of the computational speed of the power function in the `math`

module compared to the built-in `pow()`

and the double star or exponentiation operator (`**`

).

**05:10**
And let’s compute 10 to the power of 308 using all three different methods—so using the exponentiation operator, using the built-in `pow()`

, and then using the `pow()`

function from the `math`

module.

**05:23**
So I went ahead and wrote some of this up already. I’m going to call it `with_star`

, the computation of computing 10 to the power of 300 using the exponentiation operator.

**05:37**
And then with the built-in `pow()`

function, `pow()`

, `10`

, to the power of `300`

.

**05:46**
Now let’s do this with the power function from the `math`

module.

**05:53**
And then let’s take a look at all of these, so `with_star`

, and `with_pow`

, and `with_math_pow`

.

**06:01**
It looks like if we compare how much faster, say, `with_math_pow`

is relative to the built-in power function—so about 7 times faster. And `with_star`

, it’s going to be pretty close, the same, rounding off to 7.

**06:19**
All right, so that is the power function. Up next, we’ll take a look at exponential functions.

Become a Member to join the conversation.