2. Numerical data

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

Integers are whole numbers, positive or negative without decimals. For example: 1, -2 and 100
Floating point
Floating point numbers are numbers with decimals. For example: 1.5, 3.14 and -0.75


In Python, decimals are separated with a period. In Spanish it is not the correct way to separate decimals, since the comma is used, but decimals in Python are not translated from the Anglo-Saxon standard to the Spanish standard because this would lead to errors in the code.

In Python it is easy to work with these types of numerical data using common mathematical operators such as addition, subtraction, multiplication, division or power. It is also possible to perform more advanced operations such as module calculation, rounding, truncation, trigonometric functions, etc. using special functions.

Operations with whole numbers

Whole numbers 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 to 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 precision figures:

>>> 123 ** 456

Operations with floating point numbers

Floating point numbers are numbers with decimals.

Floating point numbers lose the high precision that integers have and store only 16 digits of precision. The maximum size of a floating point number is also smaller (10^307) than the maximum size of an integer (10^4300).

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 to 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 (made up of zeros and ones) and not as decimal numbers. The representation of these numbers on the screen is done by converting binary numbers into numbers in decimal format. This causes errors 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 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 integers, even if we store them in floating-point format:

>>> 1.0 + 2.0

Type conversion

There are several cases in which data can be converted from integers to floating point or vice versa.

Division of two integers

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

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

Any operation between an integer 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 n to an integer by removing the decimal part.
  • float(n) Converts 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 many of advanced mathematical operations.

Before using the 'math' library it is necessary to import it into our program. The import keyword is used to import any Python library, which will give access to many extra functions.

This is an example:

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

In this example, the math.gcd() function has been used, which returns the greatest common divisor of several numbers.

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


  1. Calculate the sum of the following distances: 100m, 15m, 50m, 80m

  2. Calculate the square of 55 knowing that the square of a number is equivalent to raising that number to 2.

    The result should be 3025

  3. Calculate the average of these grades: 6, 7, 6, 8

    All the numbers must be added and the result divided by four. Use parentheses so the operation works well. (6 + 7 + 6 + 8) / 4

    The result should be 6.75

  4. Round the previous result to a single decimal.

    The result should be 6.8

  5. Calculate the remainder of dividing 53 by 7.

    The result should be 4

  6. Calculate the next number to 80 that divided by 7 gives the remainder zero.

    You must use the remainder operator of a division % 7 with each of the numbers 80, 81, 82, 83, etc. until the remainder is worth zero.