How does `+=` work?

Hey I’m just wondering. I didn’t know how to complete the last part of this exercise because I think it’s the first time I see the “+=” operator. Maybe I’m wrong?

First time, but not the last. If we query assignment operators in search we find this to be among them.

It happens in two steps. The addition operation is performed where we take the current value and add the new value to it arriving at a new current value once it is assigned back to the variable.

new_current_value = old_current_value + new_value

a = a + b

Both statements above express the same thing.

a += b

expresses it in more succinct terms.

32 Likes

2 posts were split to a new topic: Why += and not append?

2 posts were split to a new topic: Need explanation of output

10 posts were split to a new topic: Why did the pace of the exercises started to go so fast?

I find myself easily switching up =+ and += by mistake. As we’ve been taught what += is for in the course, I was surprised to see that =+ apparently has an alternate task.

Am I mistaking or is there a place and time for both of these operators - if so, could somebody please elaborate?

Example:

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

scoops_sold = 0

for location in sales_data:
  for scoops in location:
    scoops_sold += scoops
  print(location)

I get either 13 or 96 as a final value for scoops_sold, depending on the operator-formation I use…

Thanks for any replies in advance! :slight_smile:

1 Like

There is no such operator that puts the equal sign first. The operation is always before the assignment.

1 Like

Hi,
say for instance you are doing following -
x = x + y , (y can be anything a number , variable , but compatible with the datatype of x)
the alternate way to write this is -
+= , where we first mention ‘+’ operator for the operation to be performed and then ‘=’ to assign the result to the variable on the left hand side.

we usually do this , when we are adding something to a variable and want to store the result in the same variable. Other similar operators are -

‘-=’ - for ,say we want do x = x - 2
‘*=’ for, say we want to do x = x * 2

2 Likes

thanks that is good to know!

1 Like

Thank you for the succinct explanation.

Signed n00b

Thanks a lot for the explanation!

I have also printed the single outputs of each for condition and made me question myself about the += too:

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
    print(location)
    for element in location:
      print(element)
      scoops_sold += element
print(scoops_sold)

Specifically to the += how does automatically sum all single elements to get to 96? is += automatically detect all for values as int and sum them together? what if those are strings instead? Is it just applicable to int and float numbers?

The addition is one value at a time. The first loop iterates over the outer list, the second iterates over the inner lists. The += is an addition assignment that accumulates on the scoops_sold variable. There is nothing automatic going on here. Your code is declarative around that variable.

The sum will go like this,

12
29
51
53
63
66
71
83
96
2 Likes

but what would happen if we use =+ instead of +=? would we get a syntax error?

Yes, =+ will raise an error since the assignment is coming before the augmentation. These compound operators all have the operation first, then the assignment.

# operation
#      \
        +=
#         \
#      assignment

I saw this one recently and it’s trickier than that. Unfortunately python doesn’t consider this a compound operator and treats x =+ 3 like assignment and an operator-

x = (+3)
# Similar mistaken syntax does this too

So it can be sneaky little bug in code because it doesn’t throw a proper error should anyone mistype the correct operator.

1 Like

Sneaky, is right. I hadn’t tested that to see if it would raise an error. My bad. Thanks for clarifying.

1 Like

means you add the left operand to the right operand and that becomes you new var

1 Like

Just curious but when would anyone ever do +3 inside ()?

I included brackets there just to highlight the order of operation. So despite having =+ next to each other they are separate operations. That’s all.

1 Like

bc we might be doing some higher BODMAS problems with different integers…we do need +3 or -3 in ().