• Save
  • Run All Cells
  • Clear All Output
  • Runtime
  • Download
  • 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 school.

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"?

The name integer makes sense. But why call numbers that include a decimal place a "float"? Kind of seems like a funny name... Well, "float" is short for "Floating Point Number". This refers to the fact that the decimal point is allowed to seemingly move around (or "float" around) and take up any position within the number.

To demonstrate this, let's create a number that has a lot of numbers after the decimal place and see what happens when we force the decimal place to move.

Let's use... 3/17 represented in its decimal format.

Notice how three seventeenths 3/17 is smaller than one, and Python still puts a leading zero on the left side of the number. There aren't any huge implications from this, just that this is how Python chooses to display it.

0.17647058823529413

Now, I want to contrast this number with a similar one but where the decimal place is in a different spot. So what if I multiply this fraction by, say, 1,000? Well then the decimal place moves to the right.

176.47058823529412

Now, what if I multiply it by 1,000,000?

1764705.8823529412

or by 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.

Sometimes you'll see people when they move a decimal place draw these arrows. Whenever I see those they look like waves in an ocean and I imagine the decimal place bobbing on the waves over to its new position.

This is just what I imagine in my head I don't think it's what was intended when this kind of number was named a "float" but hopefully some of these mental images will help your brain remember this term that is potentially (maybe, for you) a new term for a number that includes a decimal point.

This is a lot of talking just to say that, 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"?

So generally, we have two kinds of numbers in Python, that is to say, of all the numbers we could ever create, we could sort then into one of two "categories" floats, or integers.

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? Can you remember?

Well, you don't have to remember, because there are 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'>

It might feel like using the type() function is overkill right now, because the differences between integers and floats are so obvious and visual, but soon we'll be storing all kinds of different things to variables –a lot more than just numbers– and as we learn about more categories of values that we can work with, we'll have greater and greater motivation to be aware of what "type" of data we're working with.

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 as 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 of it, and to know that that's why the print() and type() functions just work right out of the box -they're just 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.

Recap

To recap, we can represent numbers in two different ways: integers and floats. Integers being whole numbers and floats being numbers that contain a decimal point. These are the first two "data types" that we've learned about so far, and we can use the built-in type() function to check a variable or value's "type".

In the next lesson we're going to take a break from Python specifics to introduce Machine Learning generally and motivate the importance of numeric data types in Data Science.