Why is the += sign necessary here?


#1

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 + ?


#2
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

#4

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)


#5

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