This course will be retired on June 22, 2018. We recommend "Python Basics" for uptodate content.
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
Numbers are one of the most common bits you'll encounter in programming. They're used for everything from money to votes to ages and more.
The two builtin types of numbers in Python are int
and float
. Ints are whole numbers and floats are decimal numbers.
Both number types support addition (+
), subtraction (
), multiplication (*
), division (/
) and more.
Division always results in a float, even if the result would be a whole number (e.g. 10 / 2
will give you 5.0
).
round()
is a builtin function that will round a float to the nearest whole number.
Dividing by zero will result in a ZeroDivisionError
exception.
You can create integers using the int()
function. You can create floats using the float()
function. Using int()
on a float
is not the same as using round()
on one.

0:00
We've been playing with numbers as we've learned about variables and

0:03
other bits, but let's talk about them a bit more seriously.

0:07
In the real world, we all deal with numbers all the time.

0:10
The score on the big game, how many days of vacation we have left, or

0:14
how many cookies our favorite recipe will make when we make four and a half batches.

0:18
Also, in the real world,

0:19
we often don't think about the types of numbers that we're using.

0:23
We happily add and subtract, multiply and divide whole numbers and

0:26
partial numbers all the time, at least with the help of a calculator, right?

0:30
Well, Python doesn't care what kinds of numbers that we use, either.

0:34
Numbers are numbers are numbers.

0:36
The only possible exception is when we're dealing with floats.

0:38
They're not exactly like the decimal numbers we all learned about in

0:41
elementary school.

0:42
But they're pretty close.

0:44
Let's hop over to workspaces and

0:45
look at some examples of working with numbers in Python.

0:49
We call whole numbers ints or integers.

0:52
Int is more common because that's the class name in Python.

0:56
They work just like you're used to numbers working when you do math or

1:00
anything else numeric, right.

1:01
You can do 5 + 5 and get 10.

1:04
2 2 and get 0.

1:06
10 and then we use the star as times, so 10 * 18 is 180.

1:12
And then we use the forward slash,

1:14
or slash, whatever you want to think of that one as, for division.

1:18
And 8 / 2 gives us back 4.0.

1:21
Let's talk about that one, 4.0.

1:23
We call numbers with decimals, or points in them, floats.

1:27
Notice that we got back a float when we did division.

1:30
Addition, subtraction,

1:31
and multiplication always give us back ints, unless we're working with floats.

1:36
But division always gives a float.

1:39
Now this is a difference between Python 2 and Python 3.

1:41
Python 2 would always give back ints if you used nothing but ints.

1:45
Python 3 fixed that by giving back floats when appropriate.

1:49
Actually always, because they're always appropriate.

1:52
Oh, and one other thing to watch out for is division by 0.

1:56
Dividing by 0 gives us back a new error, which we have to watch out for,

1:59
which is the zero division error.

2:01
You try to divide by 0, and you can't do that.

2:05
So we'll need to be careful with that, we don't wanna create any black holes or

2:08
anything like that.

2:10
So, let's play with floats a little bit.

2:11
If we do 5.0 + 2.5, we get back 7.5.

2:16
And 1.2 + 0.3, we get back 1.5.

2:22
Now floats work mostly like ints do, but they can sometimes be tricky.

2:28
This is due to them actually being a human understandable representation

2:32
of a more mathematically pure number.

2:35
They can represent a huge range of numbers with a very specific amount of precision.

2:41
This, though, sometimes makes them do weird things.

2:46
Let's do 0.1 + 0.1 + 0.1 0.3.

2:53
Now that should be 0, right?

2:56
Instead we get this number, and that looks way bigger than 0,

3:00
cuz that's got 5.5, but it's actually not.

3:03
Notice at the end that there is this e17.

3:07
If you haven't seen this notation before, or this way of writing numbers,

3:12
it actually means that the number is raised.

3:15
e stands for exponent to the power of 17.

3:18
That means there's basically 17 zeros between the decimal point and

3:23
this infinitesimal value.

3:27
So let's use the round function.

3:28
Let's copy this.

3:32
Highlight that.

3:33
Copy, round,

3:37
and then paste.

3:41
We'll use the round function, which rounds a float to the nearest whole number.

3:46
And if we do that, we see that it's 0.

3:51
It's effectively 0.

3:53
Now if we were doing some serious banking math with this,

3:56
we'd have to be more careful, and

3:57
we'd probably want to be using a more money appropriate number type anyway.

4:02
There are some of those in Python, but we're not gonna get to them.

4:05
Round gives us the integer that's closest to the float that we have.

4:11
But what about if we need to make an int from a float?

4:14
And we don't care about rounding, we just want the int value?

4:18
We can use the int function to do this.

4:20
We can do int(5.2), and we get back 5.

4:24
And if we do int(5.9), if it was rounded, we would get 6.

4:28
But we just get 5 because that's the int value.

4:32
Int just chops off the float portion and just turns it into ints.

4:37
Int also lets us turn strings into ints,

4:40
that's assuming that the string is just an int.

4:45
We couldn't do int('a').

4:47
That gives us a value error because there's no such thing as the number a in

4:51
base ten.

4:54
We can do the same thing with floats.

4:55
We can do float and a number and that'll give us the float value of that number.

5:00
Or float and a string, and it will give us the float version of that string.

5:08
And just like you did math back in school, we can control the order of operations.

5:13
Now Python understands the standard order of operations on its own.

5:17
If you were to do 5 + 5 * 2, we get back 15.

5:22
If you didn't do order of operations,

5:24
which says multiplication comes before addition, or if Python didn't do it.

5:28
Python just did the math from left to right, we would get back 20 instead of 15.

5:33
5 + 5 being 10, * 2 being 20.

5:37
If we want that though, we can use parentheses to control the order.

5:41
So we can do (5 + 5) * 2, and we get 20.

5:46
Again, just like math class.

5:49
We can also use the plus equals or

5:51
the minus equals when we have numbers in a variable.

5:54
So like let's say we have age = 34.

5:58
We can say age += 7, and this is the same.

6:03
We'll be able to get age.

6:04
Age is 41.

6:04
This would be the same as us doing age = age + 7, which we're not going to do.

6:10
We can also do this the other way around and do age =, say, 5.

6:16
And if we look at age, it's now 36.

6:18
And again, this would be the same idea of us doing age = age 5.

6:25
We're not gonna do that.

6:27
So the next time that you have to take a math test, take some Python with you.

6:32
Numbers are usually a pretty common part of programming.

6:35
We end up coding a lot of things that have to do with money or

6:38
quantities or all sorts of other things that we have to deal with numerically.

6:42
The int and float functions are really most useful when we're dealing with input

6:46
from users that we can't guarantee the type of.

6:49
We'll be doing some of that at the end of this course so be sure to stick around.

6:52
Now though let's see about expanding our cast of characters and

6:56
take a look at strings.
You need to sign up for Treehouse in order to download course files.
Sign up