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.

Â