# If most numbers can be represented as a float value, why would we need to use integers?

### Question

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?

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.

30 Likes

Hi,

both of the below lines throw error:
print(release_year,\n,runtime,rating)
print(release_year+"\n"+runtime)

Can anyone tell me what is the valid syntax for inserting \n in middle of multiple variables?

2 Likes

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.

print(str(release_year)+"\n"+str(runtime))

14 Likes

Thank you for the explanation!

print (release_year,’ \ n’, runtime, rating)
hi. can we do it like this?

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.

2 Likes

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!

4 Likes

Thank you for your explanation @Jephos249.

.

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
>>>
``````
12 Likes

I coded example.py

``````release_year = 1987
runtime = 1988
print(str(release_year)+"\n"+str(runtime))
``````

and it works like a charm. However, I thought that `1987` and `1988` is number, so it can be integer. Then I change them like this

``````release_year = 1987
runtime = 1988
print(int(release_year)+"\n"+int(runtime))
``````

and run agains.

``````~ > 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.

`int` cannot be concatenated with `str`.

It is pointless to recast an integer to an integer. Your earlier example is valid and sensible.

2 Likes

You do not have to change the type of the variables if you use ‘format’:
‘{}\n{} {}’.format(release_year, runtime, rating)
https://docs.python.org/3/library/functions.html#format

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?

Hi, you can try this
print(f"{release_year} \n {runtime} {rating}")

1 Like