0:06

Numbers are the most basic type of element in pretty much any programming language.

That's true in Python just like it's true everywhere else.

So today we're going to look at

the different types of numbers that you have available in Python.

There are a few limitations when I'm using them

in a program and we'll talk about those as well.

So let's see what we got.

All right, we have a simple program here in Python that's going to print a bunch of

numbers and this will allow us to see what we type in

the program for the number and what we get in the output pane,

so what Python thinks of as the number.

All right, the most basic type in Python and

pretty much any programming language is an integer or in Python it's called an int.

These are regular whole numbers that you're familiar with from

math and so let's just run this program and print a few of them.

Okay, so you can see we have the numbers zero here.

We just type a zero, we get a zero, right?

We can do that with one.

We can do that with negative numbers, minus 3,

or we can do that with relatively big numbers.

See, I can type a bunch of digits here and I get that large number that begins with a 7.

Okay, I'm too lazy to figure out what it actually is or say it for you.

Okay, great, now, we also have a notion of real numbers.

Okay, and in Python these are called floats, all right,

short for floating point,

where the decimal point floats around.

Okay, so I can move the decimal point around and have different kinds of numbers.

All right, so let's print out a few floats.

Okay, you can see we can print out zero as well.

Now, notice that we have to actually

type the decimal point if we want a number to be a float.

So zero point zero is a different number than zero.

When I type zero I got the integer zero.

When I type zero point zero,

I get the float zero, okay?

And you can see that Python prints that out as zero point zero,

indicating to you that it is a float.

All right, we also can have 7.35.

We can have negative floating point numbers,

negative 43.2 and so on.

Now, floats are not really mathematical real numbers, okay?

So there are some limitations here and one of these is the precision, okay?

The computer cannot hold an infinite number of digits in a floating point number,

it actually holds it in a fixed size and it's complicated how

it represents that form and so it's tough for me

to tell you exactly what it's going to look like,

but if I print out these numbers here

you'll see that it prints out far fewer digits in the output pane.

Now first off that's just Python being nice to you when it prints things.

The internal number that it's using actually has

more digits than this but I think this makes the point.

It does not keep every single digit you type.

So if I typed a whole lot of digits you're going to lose some and the number is

going to be an approximation of the real number that you typed and when it prints it,

it further approximates it and keeps it to a smaller number of digits so that

it's nice for you to look at.

Now I don't have to type floating point numbers with all of their digits like this.

Instead I can use scientific notation and Python will use

scientific notation for you when it's printing numbers as well, that get too big.

All right, so you can see here we have 5e32,

and what that means is five times ten to the thirty-second power and when I print it out,

Python's going to give it back to me,

except it added a plus sign so it said 5e plus 32.

That's telling me that it's five times ten to the positive thirty-second power.

I can represent very small numbers too with negative power so I could have five

times ten to the negative 32 as well and

it's making it clear that that's a positive number.

Notice also that I typed a whole bunch of nines here and I got back 1e39.

All right, that doesn't look like the right number.

Well, as I said before,

it's keeping these numbers in sort of a finite amount of space.

It does not keep all those digits,

so it took that number and it rounded it to the number one with

39 zeros after it and it's now printing it for me as one times ten to the 39th power.

Okay? This does not go on forever.

It doesn't just keep rounding for me.

Eventually you run out of space entirely and it becomes infinity.

All right, and so Python has the notion of infinity.

If I type 1e500 or 1 times ten to the 500th power,

I am not going to get that back.

Instead you can see over here that Python prints inf,

and that's its abbreviation for infinity.

So when you see inf, it means infinity.

Notice that we could also have negative infinity so negative 1e500 or

negative one times ten to five hundredth power is negative inf, or negative infinity.

All right, and you can use these as numbers but when you

add something to infinity you're going to get infinity back.

All right?

Finally I can convert back and forth

between floating point numbers and integers and you can see what happens here.

Basically when I type this,

I type float of three.

I'm trying to say, hey, take the integer three,

and turn it into a float, all right?

And you see what it does. It prints out 3.0.

It doesn't just print out three.

It took that three, made it a float, printed out 3.0.

Similarly I took an integer with a whole bunch of nines in it and said, hey,

make this a float and I get back 1e38.

So in scientific notation,

one times ten to the thirty eighth power which is a floating point number.

Right, not an integer.

I can do the opposite conversion.

I can type int 3.0 here and it says take the floating point number

3.0 and turn it into an int for me and you see it printed out three in the output pane.

Okay, so it did that for me.

Now I've got a problem here. I can't just take

an arbitrary floating point number and say, hey,

make it an integer for me right, 3.7,

there is no equivalent int.

All right, so what Python does is it just throws away the fractional part.

So int of 3.7 is just 3 and you can see it prints out three.

Similarly int of negative 3.7 is just negative 3 so it prints out negative 3.

So there are some basics of using numbers and hopefully you get

a feel for you know what you can type into a program.

You can type the number six if you need it and there you go, you have it,

and what you're going to see in the output pane -

Python's going to represent that six to the sixth.

If you want a number that's a real number 6.37,

just type it in and that's what you get.

Then you can convert back and forth between them relatively easily.

Biggest thing here is to be aware of the fact that floats are

not exactly reals and so there's a little bit of a limitation here.

They have a fixed precision so you're going to lose some digits some times

and eventually they turn into infinity instead of going infinitely large.

That was a quick tour through and using numbers in Python.

Hopefully you found it fairly straightforward and for the most part it is.

In general you type a number in your program and

Python uses that number exactly as you'd expect.

There are a couple of limitations,

but in most programs it doesn't matter at all.

But it is important to be aware of those limitations on

floating point numbers so that if you do see

something that doesn't behave exactly as you expect,

you understand what's happening.

Well, now you're ready to use numbers in your programs.