What is the need of float()?


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


In Python 2, the / operator will perform integer division when both operands are ints. 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 ints instead, the conversion would make a difference.


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.


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 …


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


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.


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.” :wink: