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

Note

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
    8
>>> 5 - 9
    -4
>>> 11 // 3
    3
>>> 11 % 3
    2
>>> 5 ** 2
    25

Integers in Python can be very large and have many precision figures:

>>> 123 ** 456
    992500687720988567008314620574696326372959408198869005198162
    988813828671047493990779211286614261446380554242369362718724
    928003527416499021181438196726015699981001207904967595176364
    654458956257416098662099005001984071532446047789680169630280
    503102614176159144687299182406854878786176459769390634643579
    861657117309763994785076492286863414669671679101266533421349
    427448514638999274870924866109771461127635671016726459531321
    964814393398730170881404146612711985003332557130961423351514
    146306516830655187840812036784877030028020820912366035190262
    568806244996817813872275740354848312715156831237421490955692
    604636096559777009388445806119312464951662086955403136981400
    116380273225662526897808381363518287953142721621112222311709
    017156123557013475523715300136938553798348656670600146433024
    591004297836539669137830022907842834556282833554705299329560
    514844771293338811599302127586876027950885792304316616960102
    32187390436601614145603241902386663442520160735566561

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
    8.0
>>> 5.0 - 9.0
    -4.0
>>> 11 / 3
    3.6666666666666665
>>> 11.0 % 3.5
    0.5
>>> 5.0 ** 3.2
    172.4662076826519
>>> round(3.1415926, 4)
    3.1416

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.1
>>> 0.2
    0.2
>>> 0.1 + 0.2
    0.30000000000000004

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
    3.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
    1.6666666666666667
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
    10.0
>>> 5 + 2.0
    7.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)
    5
>>> float(8)
    8.0

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)
    6

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.

Exercises

  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.