• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells
  • Difficulty Rating

Loading Runtime

In Python there are two main ways of representing numbers: integers, and floats.

Integers

Integers are any values that represent whole numbers (positive or negative) that do not include a decimal place. If fact, we've already been storing integers to our variables up to this point in the course.

Some examples are: 30, 9001, 1729, -27, 0, etc.

You get the idea. They're the counting numbers just like you learned when you were in elementary.

Floats

In contrast to this, Floats are numbers (again, positive or negative) that do include a decimal portion.

Some examples are: 2.5, 3.141529, -.03, etc. Notice, each of these includes a decimal point.

Whole numbers that do include a decimal place are still classified as floats: 0.0, 5.0, 100.0, etc.

Why is it called a "Float"?

Float is short for "Floating Point Number". This refers to the fact that the decimal point is allowed to seemingly move around and take up any position within the number.

To demonstrate this, let's start with the more or less arbitrarily chosen fraction 3/17 represented in its decimal format.

Notice that when displayed by Python, the decimal point comes right after this leading zero telling us that this fraction is smaller than one.

0.17647058823529413

But what if I multiply this fraction by, say, 1,000?

176.47058823529412

or 1,000,000?

1764705.8823529412

or 1,000,000,000,000?

176470588235.29413

You'll notice that in each of these cases, the digits stay the same, but that the decimal point moves (aka) "floats" to its new position within the number. Hence the name "Floating Point Number" but we'll just call these kinds of numbers "floats" for short.

So to reiterate, in Python numbers that don't include a decimal point are integers, and numbers that do include a decimal point are floats.

What is a "data type"?

The category of value that we store to a variable is referred to as the variable's "data type." Or, more commonly, we just say "type" –for short.

What is the "type" of the following variable?

If you said, "integer" or "int" you would be correct. ("int" –again– is short for "integer." Programmers like to abbreviate their terminology sometimes.)

Okay, how about this next one. What is this variable's "type"?

If you said "float" you are correct. You'll notice that you can turn any integer into a float by just adding .0 to the end of the value.

The type() Function

Whenever we want to know the type of value that is stored within a variable, we can use the type() function to tell us exactly that.

Remember our two silly variables from before favorite_number and still_favorite? Which one of them had an integer assigned to it, and which one had a float assigned to it?

Maybe you have a good memory and you're pretty sure which one is which, but there's a couple of ways that we can be absolutely sure which one holds which data type.

First off you could use the print() function and look to see which one's number includes a decimal point or not. But with this method our brains still have to do a little bit of work to look through the digits searching for the presence or lack of a decimal point to know which is which.

66.0

Let's remove our fallible brains from the equation by using the type() function instead.

Just like when we used the print() function we will use the type() function by writing the variable's name inside of the parentheses. When we run the code cell, the variable's data type will be displayed for us.

<class 'int'>
<class 'float'>

In most notebooks you can use the type() function all by itself. For reasons I don't want to get into right now, in Temzee lessons you'll need to put the type() inside of a print() function in order to see the output displayed -just a side note.

Built-in Functions

The print() function and the type() function are both examples of what we call "built-in" functions. This means that they come pre-packaged as part of the Python programming language. We don't have to do anything special to get access to them, and we can use them absolutely anywhere in our code. They just work.

As of right now, Python has 71 different built-in functions (new ones do get added now and again as the language is updated). I don't want you be stressed out about knowing what the different built-in functions are or what they do. We'll introduce them little by little when they're relevant to what we're doing -in fact, a good chunk of them I've never even had occasion to use before.

However, I do want you to be able to recognize the terminology of "built-in function", know that the print() and type() functions are two examples, and to know that that's why the print() and type() functions just work out of the box -they're part of Python.

Soon we'll learn how to write our own custom functions and then this topic will become even more clear to us.

Reiterating Floats and Integers

This has been a bit of a tangent way from our original topic of floats and integers, but I hope that the difference between these two data types is straightforward enough that I can get away throwing in a couple of extra topics here without making things too heavy.

Remember, that integers are whole numbers without a decimal portion, while floats do have a decimal portion.