OBSERVATIONS - Example 1
We used the function int() which took the text string input by the user as an argument.
It converted it into a signed integer value that was stored in the variable x,
and similarly for y. This time, the result is mathematically correct,
which is what we wanted – the sum of the numbers.
Note. Since we imposed the int() data type for the two variables,
x and y,
if we try to input the text Star (or "Star" or 'Star') as the value for the first variable,
we will now get an error:
ValueError: invalid literal for int() with base 10: 'Star' on line 1
x can only hold signed integer numbers because we used the int() function.
Note. In Python 3, the long type does not exist. Depending on the internal memory available,
you can enter even a billion billion plus 1 😜:
1000000000000000001
OBSERVATIONS - Example 2
A real number (float),
added to an integer (int), yields a
real result (float)!
For 5 and 6, we get 11.0.
For 5.23 and 2, we get 7.23.
For 2.128 and 4, we get 6.128.
We observe that the result is implicitly rounded to the number of decimal places of the most detailed real number.
If we input an integer, a single decimal place, 0, is used to indicate the data type
– float.
OBSERVATIONS - Example 3
Let's assume we want to divide 10 by 3. The known result is 3.(3), read as 3 repeating 3.
The result implicitly displayed by Python is:
3.333333333333333
If we use the function format(value,format) as above, we can display the result with 20 decimal places
(the f stands for fractional part) and see that the variable rez actually holds:
3.33333333333333348136
Computers store real values with many decimal places, and the displayed value may differ from the actual stored value.
What is displayed is not always what is stored!
We can use, for example, the function round(number[, decimals]) which
is used to round a number.
The default value of the parameter decimals is 0, so the function returns the nearest
integer if it is not specified.
Examples:
round(12.234567,3) results in 12.234 round(12.234567,1) results in 12.2 round(12.234567) results in 12 round(6.7543,1) results in 6.8 round(6.7543) results in 7
Read more about Python's limitations in the official documentation
[here].