If most integer values can be represented as a float by adding a decimal and a 0 after it, and float values have a much wider range of values that includes decimal values, why would we ever need to use integers over floats?
Answer
Although technically we can produce an equivalent value for most whole numbers by adding a decimal followed by a 0 after the number (1 and 1.0, 2 and 2.0, …), integer values are sometimes preferred, or necessary for certain situations.
For instance, it would be probably make more sense to use integer values to count things that do not have fractional values, like counting how many students are in a class, or the length of a string in Python, which is always a whole number (You will never have a string that is 2.5 characters long).
Another reason is that float values sometimes behave in unexpected ways due to how they are stored in computer memory, especially when using very large float values. So, if consistency is key for your program, integers might be a better choice.
Furthermore, certain data types in Python, such as lists, can only access elements by whole number indexes, and the range() function, which is a very useful function in Python, will only take whole numbers as input to generate a range of values.
When you are doing the command in question, you can’t mix different variable types. In this case, since you want to output a string, you need to change the release_year and runtime variables into strings in order to concatenate the whole thing.
Hi jephos249. What you are saying makes total sense. I will use integer values most of the time and add float only if needed. I believe it makes life much easier.
as a programmer is always important to think about the EU sometimes. Do you think the end user wants to see 2.0 apples or 2 apples. which one makes more sense? Sure you can choose the data type for number of apple to be float data type, but if the end user wants it to display a whole number, you will have to do some more work to make it display that way. Situations like this is the reason why we would need to use integer data type values. Plus it make more sense if you are going to be using whole numbers only then just use integers. That is my 2 cents. You got this!
Generally speaking, you might want to restrict floats for times when your data set contains the same and you are then processing through some a loop or algorithm
The advantage of int over float is computational speed. Integers are represented in memory as a fixed value. Floats, on the other hand are stored as a mathematical construct, mantissa and exponent so there is computation involved just in assessing the value. Floating point arithmetic and integer arithmetic are two different processes.
Even in simple arithmetic, floats lose some of their precision whereas integers do not.
>>> a = 101.01
>>> b = 393.03
>>> a + b
494.03999999999996
>>> (int(a * 100) + int(b * 100)) / 100
494.04
>>>
~ > python3 code.py took 48s at 08:05:39
Traceback (most recent call last):
File "/Users/tien.phan/code.py", line 4, in <module>
print(int(release_year)+"\n"+int(runtime))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Can someone tell me why? Thank you.
In my mind, release_year will be delivered an integer 1987, and runtime is 1988. So they are integer.
How does the use of integers vs. floats impact the accuracy of mathematical operations? Can you provide an example where using integers may lead to more accurate results than using floats, or vice versa?