Why is the += sign necessary here?


n = [3, 5, 7]

def total(numbers):
result = 0
for i in range(0,len(numbers)):
result += numbers[i]
return result

Why can’t it just be + ?

a = a + n

is the longhand way to write this new assignment. But we can perform the same operation with a single operator…

a += n


As mtf said, (a = a + n) is the same as a += n. a + n does the computation on that line and doesn’t save it anywhere unless you assign a = a + n. a += n is shorthand that you’ll see used in other peoples programs, personally, I use whatever i’m in the mood for, generally shorthand (a += n)


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