Datatypes - Integers and Floats

Datatypes define what and how operators can be applied to each datatype, as well as how much memory should be allocated for each. In this lesson we will cover integers and floats, and cover additional datatypes and data structures in the following lessons. Let's again give some definitions.

A datatype informs the Python interpreter what sort of operations can be performed on it, as well as how much memory should be allocated for each type of data.
A data structure is an object or format that is used to organize and store data in specific ways.

We can check the datatype of a variable with the built in function type.

Again, we will go into what specifically each of these datatypes mean, however for now, we can see there are a variety of types. We see that x is an int (short for integer), while y is a float.

Integers

Let's start with integers.

Integers are whole numbers, that is to say positive and negative numbers, including zero, without any decimal or fraction components.

Example integer values: -1, 2, or 3.

Python has some built in functions to cast one datatype to another. The int function can be used to cast its input to type int.

Floats

A float is a real numbers with a fractional component. It's used to represent numbers that are not integers.

A float is a number with a decimal point representation, such as: 1.0, 2/5, or 3.14159.

Similarly, the float function can be used to cast its input to type float.

Interactions and Conversions

When we perform operations on two numbers, such as addition, the interpreter strives to maintain the same datatype for the result. So, when adding two integers, the result will be an integer, and when adding two floats, it will be a float. However, if an integer and a float are added together, the more inclusive datatype will be chosen as the return type. In this case, since integers are a subset of floats, the result will be of the float datatype.

In this code editor, when we add the integer 2 to the float 3.1, the result is a float. In the same way, if we cast the integer 3 to float, in this addition, the result will also be a float.

However, if we cast the datatype of the float to an integer, it will round down to the nearest integer, in this case, to 3, resulting in an integer instead.

In addition to how int rounded down the float to an integer, there are additional functions in the math package. We will learn packages later, for now, we can think of it as a toolbox that we can access by prefixing the function name with the package name, to assist in our programming. The math package comes pre-installed with Python. And we have imported this package behind the scenes as well. Let's look at a few functions from here.

math.floor will round down a float to an integer, no matter how large the remainder is.

math.ceil will round up a float to an integer, no matter how small the remainder is.

round will round a number based on the size of the remainder.

Integers and floats are our bread and butter for the numbers we will use in this Python course. Let's go over booleans next.