**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses trial

to watch this video

Let's explore numbers and some basic math operations

### Update

At 3:22 of the video it is stated "5.5 raised to the negative 17th power". That should actually be: "5.5 times 10 raised to the negative 17th power".

#### Learn More

[MUSIC]
0:00

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

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

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

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

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

is with the term data.
0:19

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

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

rather, numeric data.
0:28

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

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

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

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

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

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

So we can add 1 + 2.
0:54

And integers are also called ints for
short, I-N-T, int.
0:57

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

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

Now, integers can also be negative.
1:11

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

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

So 4 * 2 is 8.
1:20

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

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

of each one of these statements.
1:31

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

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

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

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

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

Now note, that this returned 5.5.
1:55

This result is not an integer.
2:00

Integers are whole numbers.
2:03

This has a decimal point.
2:05

This is a different data type.
2:07

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

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

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

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

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

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

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

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

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

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

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

You can keep on adding.
2:53

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

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

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

Cuz that should be 0.
3:09

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

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

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

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

Which is more or less 0 right?
3:31

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

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

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

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

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

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

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

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

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

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

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

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

about computer programmers.
4:23

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

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

And then the amazing Richard Pryor,
4:31

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

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

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

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

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

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

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

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

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

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

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

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

The operations work in that order.
5:22

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

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

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

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

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

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

right?
5:51

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

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

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

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

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

Awesome.
6:07

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Integers and floats working
together is totally convenient
7:13

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

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

just don't get each other?
7:23

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

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

We're not gonna break anything.
7:32

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

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

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

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

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

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

That's also a TypeError.
8:00

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

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

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

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

There is a handy built-in
function that lets you change or
8:17

coerce into a different type.
8:21

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

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

So our string 11.
8:30

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

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

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

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

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

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

Just straight 11 and a number.
8:48

That's an integer, right?
8:49

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

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

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

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

It would round up.
9:05

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

everything after the decimal point.
9:09

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

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

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

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

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

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

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

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

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

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

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

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

So, that's remainder math.
10:02

More on the teacher's notes.
10:04

That was a lot, right?
10:09

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

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

Now, don't fear though,
10:14

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

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

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

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

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

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

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

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

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

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

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

You got this.
10:50

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

take a look at our next type, strings.
10:55

You need to sign up for Treehouse in order to download course files.

Sign up