Integers & Floats
Integers versus Floats
Integers or int are whole number i.e.: do not have a fractional value. On the other hand, float are floating-point numbers that have a fractional part. The reason why we make this distinction is because int and float are not stored in the same way within a computer’s memory.
Scientific notation
Python supports scientific notation if you are dealing with large numbers:
large_num = 5e6This value corresponds to five million (5 000 000).
Underscores between digits
The following corresponds to eleven million one hundred eleven thousand one hundred eleven. (11 111 111). Since we cannot separate the digits with spaces, there are two valid ways of writing this number in Python:
number = 11111111
number2 = 11_111_111 #has the same value, but is clearer to readPython 3.6 has introduced underscores in numeric literals, allowing for the placement of single underscores tween digits and after base specifiers for improved readability. This feature is not available in older versions of Python. [1]
Negative numbers
To create a negative integer, you can simply add a minus sign:
my_negatiive_num = -14568Converting from one type to another
int \(\rightarrow\) float
You can convert an integer into a float by using the functionfloat()
float(10)Output: 10.0
float \(\leftarrow\) int
Likewise, you can convert a float:
`python int(3.14) This will truncate the decimal part of the float number, the resulting int is 3. Therefore, you must be careful when performing those conversion because this might result in loosing precision or loosing information.
This will become more relevant when performing arithmetic operations between the different types.
Reference
- Cisco skills for all. (n.d.). https://skillsforall.com/launch?id=da0847b7-e6fc-4597-bc31-38ddd6b07a2e&tab=curriculum&view=7f5e478b-86ed-59e1-a590-7c95a41832d5]