# What is the need of float()?

#1

Link : Computing the average
What is the need of `float()` in finding the length of the list?
Your answers will be appreciated.
THANK YOU

#2

In Python 2, the `/` operator will perform integer division when both operands are `int`s. Converting one of the operands to `float` prior to the division will cause the operator to perform `float` division so that any fractional component of the result will not be discarded.

The `grades` list does contain a `float`. However, if we were to be given a list of all `int`s instead, the conversion would make a difference.

#3

Iâ€™ve always contended that the author threw in a float just to skirt this discussion which might derail the train of thought within the lessons. If the instructions are followed using the provided data, a float would be generated in the first function. From there it would remain a float. No problem (or smoke and mirrors).

It also demonstrated less than obvious mechanics. My contention was not an objection, so to speak.

#4

In this exercise, part of the hint reads â€¦

Remember that integer division in Python will always result in an integer. We convert `len(grades_input)` is a float so that the average is a float.

I actually donâ€™t like that they asked us to convert the length of the list to a `float`. The denominator is a count of the number of items being averaged, which is best regarded as an integer. If they wish us to have the `grades_average` function explicitly convert one of the operands to a `float` as a defensive measure, it should be the numerator, as follows â€¦

``````  return float(grades_sum(grades_input)) / len(grades_input)
``````

Note that the grammar is a bit off, and perhaps the typography as well, where they should have had â€¦

â€¦ convert `len(grades_input)` to a `float` â€¦

â€¦ instead of â€¦

â€¦ convert `len(grades_input)` is a float â€¦

#5

Glad Iâ€™m not the only one preaching this point. I too refute the idea of casting a counting number to float when the other number might be well considered a float.

In general, speaking to the topic, not as a reply,

``````grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
``````

The first function to operate on this list is `grades_sum`, which, because 50.5 is a float, will return a float as a result. From there it carries forward through the other functions as a float. There is no need to convert anything (for this exercise).

However, were we to submit a different list that contained no floats, then I would suggest casting to float the output of `grades_sum` and not doing it somewhere along the chain of functions. Do it in the first link.

``````def grades_sum(grades):
total = 0.0
for i in range(len(grades)):
total += grades[i]
return total
``````

or,

``````def grades_sum(grades):
return float(sum(grades))
``````

Donâ€™t use the above unless the lesson allows it. At his stage the naive approach, iteration and augmented assignment may be all that is at our disposal, and the `sum()` function may not have been introduced yet.

#6

In Exercise 4: The sum of scores, the instructions for creating the `grades_sum` function include this â€¦

I know what youâ€™re thinking, â€śletâ€™s just use the built-in `sum` function!â€ť The built-in function would work beautifully, but it would be too easy.

The author probably wants us to avoid using the `sum` function for the `grades_average` function as well. After all, â€śit would be too easy.â€ť