2. Numerical data

In Python, there are different numeric data types that can be used to store and work with numbers. The main types of numeric data are integers (int), floating point numbers (float) and complex numbers (complex), which we will not cover for now.

Integers are whole numbers, positive or negative with no decimal places. For example, 1, -2, and 100 are whole numbers.

Floating point numbers are numbers with decimal places. For example, 1.5, 3.14, and -0.75 are floating point numbers.


In Python, decimals are separated by a period. In Spanish it is not the correct way to separate the decimals, because the comma must be used, but the decimals in Python are not translated to the Spanish form because this would lead to errors when programming the same code in different countries.

It's easy to work with these numeric data types in Python using common math operators like addition, subtraction, multiplication, division, or power. It is also possible to perform more advanced operations such as modulus calculation, rounding, truncation, trigonometric functions, etc. using special functions.

Operations with integers

Integers are numbers without decimals. The basic operations that can be applied to integers are the following:

Symbol Operation
+ Addition
- Subtraction
* Multiplication
// Integer division (without calculating decimals)
% Remainder of a division
** Power (raised to a number)

Copy the following examples into the IDLE environment to verify that they work correctly:

>>> 2 + 6
>>> 5 - 9
>>> 11 // 3
>>> 11 % 3
>>> 5 ** 2

Integers in Python can be very large and have many, many digits of precision:

>>> 123 ** 456

Operations with floating point numbers

Floating point numbers are numbers with decimal places.

Integers can also be stored as floating point numbers, but in this case they lose the high precision of integers to store only 16 digits of precision and numbers up to 10^307 in maximum size. The floating point integer 2 will look like 2.0, with a zero added after the decimal point.

The basic operations that can be applied to floating point numbers are the following:

Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division with decimals
% Remainder of a division
** Power (raised to a number)
round(number, digits) Rounds a floating point number to decimal digits.

Copy the following examples into the IDLE environment to verify that they work correctly:

>>> 2.0 + 6.0
>>> 5.0 - 9.0
>>> 11 / 3
>>> 11.0 % 3.5
>>> 5.0 ** 3.2
>>> round(3.1415926, 4)

Floating point decimal errors

Floating point numbers are stored within the computer as binary numbers, not decimal numbers. When representing the number on the screen, the binary number is converted into decimal. This causes errors to occur when storing and performing calculations with decimals. A practical example is the following sum:

>>> 0.1
>>> 0.2
>>> 0.1 + 0.2

Decimal numbers are stored with a small error that is not visible when displayed on the screen, however the sum of the two decimal numbers results in a number with a small error that is visible in decimal number 17.

These rounding errors occur only with the decimal part of floating-point numbers and do not occur with whole numbers, even if they are stored in floating-point format:

>>> 1.0 + 2.0

Type conversion

There are several cases in which you can convert data types from integers to floating point or vice versa.

Division of two integers

The division with decimals operation / applied to integers can result in a number with decimals, which will be stored as a floating point number.

>>> 5 / 3
Mixed operations between integers and floats

Any operation between an integer number and a floating point number will result in a floating point number.

>>> 5 * 2.0
>>> 5 + 2.0
Force type conversion

You can force a number to be converted from one type to another type with the following functions:

  • int(n) Converts the number n to an integer
  • float(n) Converts the number n to a floating point number

Examples of conversions (also called cast):

>>> int(5.6)
>>> float(8)

Other mathematical operations

To perform more mathematical operations with integers or floating point numbers, it is necessary to import the math library which gives access to a multitude of advanced mathematical operations.

Some examples are the following:

>>> import math
>>> math.gcd(24, 18)

The import keyword is used to import a Python library, which will give access to many extra functions. In this example we have used the function math.gcd() which returns the greatest common divisor of several numbers.

You can see all the functions of the math library in the official Python language documentation.