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.
Note
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
8
>>> 5 - 9
-4
>>> 11 // 3
3
>>> 11 % 3
2
>>> 5 ** 2
25
Integers in Python can be very large and have many, many digits of precision:
>>> 123 ** 456
9925006877209885670083146205746963263729594081988690051981629888138
2867104749399077921128661426144638055424236936271872492800352741649
9021181438196726015699981001207904967595176364654458956257416098662
0990050019840715324460477896801696302805031026141761591446872991824
0685487878617645976939063464357986165711730976399478507649228686341
4669671679101266533421349427448514638999274870924866109771461127635
6710167264595313219648143933987301708814041466127119850033325571309
6142335151414630651683065518784081203678487703002802082091236603519
0262568806244996817813872275740354848312715156831237421490955692604
6360965597770093884458061193124649516620869554031369814001163802732
2566252689780838136351828795314272162111222231170901715612355701347
5523715300136938553798348656670600146433024591004297836539669137830
0229078428345562828335547052993295605148447712933388115993021275868
7602795088579230431661696010232187390436601614145603241902386663442
520160735566561
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
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, 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.1
>>> 0.2
0.2
>>> 0.1 + 0.2
0.30000000000000004
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
3.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 1.6666666666666667
- 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 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 the number n to an integerfloat(n)
Converts the number 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 a multitude of advanced mathematical operations.
Some examples are the following:
>>> import math >>> math.gcd(24, 18) 6
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.