Why must we set a variable = 0 in functions?


In the example below, I see that we set result = 0. Is it because when we make a variable, we have to give it a value?

n = [3, 5, 7]

def total(numbers):
    result = 0
    for i in range(len(numbers)):
        result = sum(numbers)
    return result


Exactly. If result = 0 it is 0 and nothing else. Of course you can later assign something else to it and it can be greater than 0, but it will be always a number.


Perfect! It was such a basic concept, I can't believe I overlooked that.

And if we want an empty list we would do this:
example = []

or an empty string:

example = ""

I see now, thanks!!


It's nice to be useful, hope you got it! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.