To learn more about constants, check out Python Constants: Improve Your Code’s Maintainability.

**Hint:**You can adjust the default video playback speed in your account settings.

**Hint:**You can set the default subtitles language in your account settings.

**Sorry!**Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

# Constants of the math Module

**00:00**
We’ll start by taking a look at the constants defined in the `math`

module.

**00:05**
The `math`

module defines five constants. Perhaps the most famous constant in mathematics is pi. The symbol that’s given to the numerical value of pi is the Greek letter π.

**00:18**
The value of pi is around 3.141592. It’s an irrational number, and so the number of digits continue on forever with no predictable pattern. The value of pi is a sort of universal constant in the sense that if you take any circle and you take its circumference and divide it by the diameter, you’re always going to get the value of pi. Now, in Python 3.6, the constant tau was introduced, and this is the value of 2 times pi. Perhaps the next most famous constant in mathematics is Euler’s number.

**00:56**
This is usually given the letter *e*, and *e* is around 2.718281. This is also an irrational number, so the number of decimal digits continues on forever with no predictable pattern.

**01:10**
*e* comes up a lot when you’re working with problems that involve exponential growth and decay, and it comes up also a lot in calculus and in compound interest.

**01:20**
We’ll take a look at an example involving decay in a future lesson. Then there are another two constants in the `math`

module that are not technically numerical values, but more of conceptual values.

**01:32**
The first one is the infinity constant, which is denoted by `inf`

. The `inf`

constant is there to encapsulate the mathematical concept of something that’s boundless or never-ending. Sometimes in an algorithm, what you’ll want to do is compare a given value to some sort of absolute maximum or minimum, and this is where the `inf`

comes in.

**01:55**
The other conceptual type of constant is `nan`

, and `nan`

is there to represent the idea of *Not a Number*. This comes up sometimes when you’re doing a numerical computation and maybe your data gets corrupted in some way, or you do an invalid mathematical computation—like, say, dividing by zero—then a lot of programming languages will return a value of `nan`

, or *Not a Number*.

**02:20**
All right, let’s take a look at these constants.

**02:24**
First things first, go ahead and import the `math`

module. Let’s take a look at the `pi`

constant, and we’ve also got Euler’s number, and we’ve got `tau`

,

**02:44**
Let’s work through an example showing why you would want to use the constants in the `math`

module. We’ll do this for `pi`

, but similar ideas will apply to the other constants.

**02:54**
Let’s suppose you have a sheet of material that has a radius of 5 feet. Then, you know back from grade school that the area of the sheet would then be pi times the radius squared.

**03:11**
So the area is around 78.53. Let’s suppose instead that you computed the area, say, just using 3.14.

**03:24**
So the area using 3.14—about 78.5, which is just this value rounded to the first decimal place. Now let’s suppose that you needed to buy a lot of these sheets, and the price that you need to pay per square foot of the sheet is, say, 39 dollars and 49 cents.

**03:46**
Let’s define the cost per square foot of the sheet, 39 dollars and 49 cents. So if you compute the cost of the sheet using the cost per square feet times the area of the sheet obtained by computing the area using the built-in `pi`

constant from the `math`

module, and if you compute the cost using, again, the cost per square feet—but this time using the area computed by using just `3.14`

—the difference now is 1 dollar and 57 cents. Not a huge difference, but already we see that there’s a non-trivial difference when using an approximation to pi of 3.14 instead of using the value that comes in with the `math`

module. Now, things will get worse if, for example, you have a lot of sheets to buy.

**04:44**
If you needed to buy 10,000 of these sheets, then the cost that you’re underestimating in what you’d have to pay to buy all 10,000 sheets is $15,723. So this is just a quick example of why you would want to use the built-in constant `pi`

in the `math`

module if you were going to do any type of computations involving pi.

**05:10**
Let’s now take a look at `inf`

. I’ll let you explore `nan`

on your own time. So, `inf`

, again, is `inf`

. The `inf`

constant in the `math`

module was introduced as an equivalent to the `float('inf')`

value. So in the `float`

constructor, if you pass in a value of `'inf'`

—the string `'inf'`

—this is going to be equivalent to the `math.inf`

constant.

**05:40**
Whether you’re using the `float('inf')`

value or the `math.inf`

value provided in the `math`

module, they both represent the concept of infinity.

**05:49**
So, for example, if you compare the `math.inf`

value with, say, the maximum size of a floating-point number, which is 1 times 10 to the power of 308, you’re going to get `True`

.

**06:03**
If you multiply the `math.inf`

value by -1, then you’re going to get the concept of negative infinity, and this is less than the largest negative float value that you can store. And so in this case, again, you’re going to get `True`

.

**06:22**
Now, to make it clear that this `math.inf`

constant is not really a numerical value but more like a concept, if you add to `math.inf`

a value, say, of `1`

, and you ask whether that is greater than `math.inf`

itself, you’re going to get `False`

. So again, the idea there is that `math.inf`

is not there to really represent a numerical value, but more like a concept of boundlessness, or “without end.”

**06:51**
Those are the constants that are contained in the `math`

module. Coming up next, we’re going to take a look at some of the built-in arithmetic functions that are in the `math`

module.

Become a Member to join the conversation.