Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's explore numbers and some basic math operations
Learn More

0:00
[MUSIC]

0:04
We've seen how Python can work with different types of information, right?

0:08
Like, for instance, so far we saw strings, and

0:10
we also saw how we could work a bit with numbers.

0:12
Now, those are definitely different types of information.

0:15
The more common way you'll hear this information referred to,

0:19
is with the term data.

0:20
So, these different types of data are called data types.

0:25
Earlier, I showed off a little bit of how to work with numbers, or

0:28
rather, numeric data.

0:30
Let's pop open the Python Shell and dive a little deeper into our exploration.

0:35
So let's go ahead and open up that REPL, so we do that by typing python.

0:40
I'm gonna scroll this up so we get some more space.

0:43
Okay, so whole numbers like 1 are called integers.

0:50
And they're whole because they aren't fractional,

0:52
meaning they don't have a decimal point.

0:54
So we can add 1 + 2.

0:57
And integers are also called ints for short, INT, int.

1:03
And we can subtract using the minus key.

1:07
So you do 3 2, and we'll see that we get 1.

1:11
Now, integers can also be negative.

1:13
So we can say 5 7, and that will give us a 2.

1:17
And we can multiply by using the asterisk.

1:20
So 4 * 2 is 8.

1:23
A handy trick that I'd like to show while we're here is that there's a magic

1:26
variable in the REPL that always gets set to the result

1:31
of each one of these statements.

1:33
It's stored in the underscore, so, for instance, this 8 that was just returned,

1:37
it's actually in the variable underscore, 8.

1:41
You'll thank me later when you need that.

1:43
So let's take a look at what happens when we use a decimal point now.

1:48
So, let's add 4and we'll do one and a half, so 1.5.

1:55
Now note, that this returned 5.5.

2:00
This result is not an integer.

2:03
Integers are whole numbers.

2:05
This has a decimal point.

2:07
This is a different data type.

2:08
It's known as a floatingpoint number, or float for short.

2:13
Now, we always will get a float when we do division,

2:16
we'll always be returned to float.

2:18
So watch, this is 16 / 4, and you'll see that we got 4.0.

2:25
Floats are required when we need more precision,

2:28
that precision however comes with a need for some caution.

2:32
As you'll see here, you can do math with floats just like we were doing with ints.

2:40
So we can say 0.1 + 0.1, and we get 0.2.

2:42
But you need to be careful, here watch this example.

2:46
So Ill use the history we'll get back 0.1 + 0.1,

2:49
I'm gonna add one more, so you can chain values like that.

2:53
You can keep on adding.

2:54
So we get 0.1 + 0.1 + 0.1.

2:57
So that should be 0.3, and if we subtract 0.3, watch what happens.

3:04
And we get back 5 and some change, which is weird, right?

3:09
Cuz that should be 0.

3:10
But really, if you look at the end here, this is exponential, right here.

3:16
This is an exponent of 17, so this is actually

3:21
5.5 raised to the negative 17th power.

3:25
So, this is really 0 point and then 17 0s, and then a 5.

3:31
Which is more or less 0 right?

3:33
If you wanna bring a floating point number back to an integer, you can

3:36
round it to the closest integer using quite intuitively a function named round.

3:42
So we'll say round, kind of want to type that.

3:45
I can use the underscore, so we'll say that, so

3:48
that will be rounding of the 5.55, blah, blah, blah.

3:51
So here we go, 0, awesome.

3:55
So, I'm gonna do a Ctrl+L here, that's gonna bring us back up to the top.

4:00
Okay so here we go, so in case you've forgotten about rounding,

4:03
if the fraction portion is more than half, it will roll up.

4:07
So we'll say round (4.6), so that should round to 5, and it does.

4:12
And if its below half, so let's say 4.2 that will round down, awesome.

4:19
Now, these rounding errors have been a plot point in several movies

4:23
about computer programmers.

4:24
My favorites include Office Space, where the rounding errors were

4:27
sending fractions of a set to Samir and Michael Bolton's bank accounts.

4:31
And then the amazing Richard Pryor,

4:33
prior to that pulled a similarly clever stunt in Superman III.

4:37
Now this points out that floats in their precisions make them

4:40
not the most ideal type to deal with currency.

4:43
There are Python data types and various methods that help to make sure that we

4:46
don't let this movie plot disaster happen to the software you create.

4:50
There's more in the theacher's notes.

4:52
And you might remember from your math class, whenever that might have been, but

4:57
there is an order of operations, and that holds true here too.

5:00
So, we've got this PEMDAS, or Please Excuse My Dear Aunt Sally,

5:07
that's what my junior high maths teacher used to say.

5:12
So, shout out to Mr. Beetle,

5:15
that one's been stuck in my head since seventh grade.

5:22
The operations work in that order.

5:23
Now first it's any P which is parenthesis, then it's E for

5:28
exponents, M for multiplication, then division, addition, subtraction.

5:33
Now there's more in the teacher's notes if you feel like you need a refresher.

5:36
Let's just go ahead and create some random equation.

5:39
Let's do 10 3 * 5 + 8.

5:46
So here, using the order of operations, the multiplication would happen first,

5:51
right?

5:51
So there's no parentheses, there's no exponents.

5:54
There is an M, so that would happen first.

5:56
And then division, and then addition, and subtraction.

5:59
So, what we have here is we have 15, and

6:03
we have 10 15, 5, + 8 is 3.

6:07
Awesome.

6:08
But that's probably not that obvious if you don't remember the rules.

6:13
So it's best to just be explicit in what you want and Python lets you do that.

6:17
So, you can just use parenthesis to drip your ordering.

6:21
Like let's say that we actually wanted to have 10 3 first, so

6:25
I can put that in paren's, and then I wanted to multiply 5 plus 8.

6:30
That way we don't need to, I think that's more clear, right?

6:33
So we have (10 3), we 7 times (5 + 8), is 13.

6:39
I'm gonna go ahead and let that do it, it should be somewhere around 91.

6:43
There we go, and you can see how that reads more clear, right?

6:46
Especially the more time that you spent away not doing math like this.

6:50
Like for instance, over the weekend, I was talking with my dad's sister and

6:55
she was saying how she forgot a bunch of this math knowledge.

6:58
So I think that she's a really great example of how it would help her if you

7:01
were just more explicit with your mathematical statements.

7:04
I guess what I'm trying to say here is Please Excuse My Dear Aunt Sally,

7:08
it's been a while for her.

7:10
Bam, math teacher joke, Mr. Beattle be so proud.

7:13
Integers and floats working together is totally convenient

7:17
because the types understand how to work together.

7:20
But what happens if you try to do math operations with data types that

7:23
just don't get each other?

7:25
Well, we're here in the REPL, the place built for exploration.

7:28
So let's just quench our thirst for knowledge by giving it a try.

7:32
We're not gonna break anything.

7:33
So, let's take the string apple.

7:37
And say "apple, and I wanna, that's not good.

7:43
We want "apple", and then I want to add 2 to that.

7:48
So we get a TypeError, and it must be a string, not an int.

7:53
And that's true, even if the string looks like a number.

7:56
So if we have "11" + 2.

8:00
That's also a TypeError.

8:02
Now if you remember, our input function always returns a string.

8:07
I sense that that's gonna be a problem, because I'm sure eventually we're gonna

8:10
want to prompt a user for a number, but we're gonna get a string back.

8:15
Gonna CTRL+L to get back up there.

8:17
There is a handy builtin function that lets you change or

8:21
coerce into a different type.

8:23
So if we want an integer, we just pass it our string like this.

8:27
So we say int, and we pass in our string.

8:30
So our string 11.

8:31
We'll come back, and you'll see now it's an int.

8:35
And there's also one for floats.

8:36
So if we could take a float and we can give it a string of 11,

8:39
and you'll see it comes back with 11.0.

8:42
Awesome, and you can also convert an existing integer to a float.

8:46
So we'll just give it an 11 there.

8:48
Just straight 11 and a number.

8:49
That's an integer, right?

8:51
And now it becomes 11.0, and, of course, there's the reverse.

8:54
So if I say int(11.9), you'll see that we get back 11.

9:01
Now note, this didn't round.

9:03
If it did round, it would be 12 right?

9:05
It would round up.

9:06
What this type of coercion does is it just forgets

9:09
everything after the decimal point.

9:11
Which might be handy if you only want to work with integers.

9:15
You can do something similar with what's known as the division shortcut.

9:19
Remember, division was a single forward slash.

9:22
So, if we said 23 / 3, and also we get 7.6 and some change, right?

9:28
That's a float, but if we use the double forward slashes.

9:31
So, if we say 23 // 3, we'll get just the integer portion.

9:37
So, we'll get back 7, and if you wanted to get the remainder in integer form,

9:42
you can do that too using what is known as the modulus.

9:46
So we'll say 23 % 3.

9:51
So, 3 goes in to 23 7 times,

9:56
and 3 times 7 is 21, right?

10:00
So, 23 minus 21 is our answer too.

10:02
So, that's remainder math.

10:04
More on the teacher's notes.

10:09
That was a lot, right?

10:10
Especially if you're like my aunt Sally and

10:12
it's been a while since your last math session.

10:14
Now, don't fear though,

10:15
a common misconception is that there is a ton of math in all of programming.

10:19
No, don't get me wrong,

10:20
there are defintely heavy calculations requiring a few business sectors.

10:24
But you'll ramp up to those calculations as you need them.

10:28
Now if you're working on a shopping cart application, maybe you'll need some basic

10:32
arithmetic like quantity multiplied by the price of the product, and

10:35
then calculate tax and add a shipping charge.

10:38
But those formulas, they're already established and

10:40
you just need to make the code run through the proper equations.

10:44
If this all feels overwhelming, don't worry.

10:47
I'm gonna be walking you through some practical math examples in this course.

10:50
You got this.

10:52
Why don't we take a well deserved break and then swing back and

10:55
take a look at our next type, strings.
You need to sign up for Treehouse in order to download course files.
Sign up