Why does += add the sum int. of one list to another and not just +?

Nathaniel Mayberg :

I have the following code:

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0

I originally wrote:

for location in sales_data:
  print(location)
  for element in location: 
    scoops_sold + element 
    print(scoops_sold) 

After messing around I saw I was just missing an = after the + sign.

Why does += add the sum int. of one list to another and not just +?

The Big Kahuna :

+= is short for scoops_sold = scoops_sold + element. Having scoops_sold + element is computing the result but not storing it to any variable where as scoops_sold = scoops_sold + element is computing the result and assigning it to scoops_sold.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=408154&siteId=1