Numeric Data Types

Every value in Python has a data type that describes what kind of information is represented by the value. The data type for a value also determines the ways that the value can be used in operations.

In this chapter, we will dicussion data types used to represent numerical values. Python has three numerical variable types: int, float, and complex. We will discuss only the int and float data types in this chapter.

  • An integer, or int, is a value that represents a whole number. For example: 7, 42, and 1337.

  • A float is a value that represents a decimal number. For example, 0.33, 3.14, and 25.1.

Every variable also has a data type determined by the value stored inside it. We can use the type() function to check the type of a variable. This is demonstrated below. termine a variable’s type using the type() function.

x = 17
y = 4.3

print('The type of x is:', type(x))
print('The type of y is:', type(y))
The type of x is: <class 'int'>
The type of y is: <class 'float'>

Python infers whether a numeric variable should be stored as an int or a float based on the value of that variable. Occasionally we might wish to assign a whole number value to a variable, but have it stored as a float. We can accomplish this by adding “.0” to the end of the number.

z = 14.0
print(type(z))
<class 'float'>

Operations Involving Floats and Ints

The data type for a value determines the ways that the value can be used in operations. We can use integers, floats, or a combination of the two in most numerical calculations. When performing an artihmetic operation that involves an integer and a float, the result will always be represented as a float.

x = 7
y = 5.1

total = x + y

print(total)
print(type(total))
12.1
<class 'float'>

This holds true even when the result of the calculation is a whole number.

x = 4.2
y = 5

product = x * y

print(product)
print(type(product))
21.0
<class 'float'>

The ratio of two integers is always represented as a float.

a = 7
b = 5

q = a / b

print(q)
print(type(q))
1.4
<class 'float'>

Again, this holds true even if the resulting value is a whole number.

a = 20
b = 5

q = a / b

print(q)
print(type(q))
4.0
<class 'float'>

Rounding Values

Python provides us a round() function that can be used to round a floating-point value to a specified number of decimal digits. Suppose that we wish to round the value number to a number of decimal places indicated by digits. The syntax for to accomplish this is shown below.

round(number, digits)

Some examples of the round() function are provided below.

pi = 3.141592653589793

print(round(pi, 0))
print(round(pi, 1))
print(round(pi, 2))
print(round(pi, 3))
print(round(pi, 4))
3.0
3.1
3.14
3.142
3.1416

If we do not provide round() with the desired number of digits, writing only round(number), then Python will round the provided number to the nearest integer.

print(round(12.3))
print(round(12.7))
12
13

Type Coercion

There are occasions when we would like to convert a variable from one data type to another. This is referred to as type coercion. We can coerce a variable to another date type by passing it to a function whose name is identical to the desired data type. For instance, if we want to convert a variable x to an integer, we would use the command int(x). If we want to convert a variable y to a float, we would wuse float(y).

We will discuss coercion in more detail in a later chapter. For now, we will consider some examples of converting between integers and floats.

In the cell below, we create an integer with a value of 19 and coerce it into a float. Notice that resulting float is displayed as 19.0 rather than 19.

x_int = 19
x_float = float(x_int)

print(x_float)
print(type(x_float))
19.0
<class 'float'>

We will now explore coercing a floating-point value into an integer.

y_float = 6.8
y_int = int(y_float)

print(y_int)
print(type(y_int))
6
<class 'int'>

Notice that we we coerce a float to an int, Python does not round the float to the nearest integer. Instead, it return the integer portion of the floating-point value discarding any digits to the right of the decimal point.