+= operator reassignment rule

I did the the “Receipts for Lovely Loveseats” project on the “hello world” section of the CS101 course.

In the project you need to update integer variables and also do some calculations.

As I understood, the += will reassign the value throughout the code. so any past calculation using the variable will need to be considered.
I realized while doing the project that the += will only reassign the (updated) value for future use. is that correct?

customer_one_tax = customer_one_total * sales_tax
customer_one_final_total = customer_one_total + customer_one_tax
print (customer_one_final_total)

will equal

customer_one_tax = customer_one_total * sales_tax
customer_one_total += customer_one_tax


Yes that’s correct, just like reassigning the variable any other way, using += changes the value for future use in the rest of the code and (aside from the different variable name) those two code snippets are equal :slight_smile: .

Moreover this carries to many other operations such as subtraction, multiplication, bit-shifting, logical operations.

x -= 4 #subtract by 4
x *= 20 #multiply by 20
x <<= 2 #left-shift by 2
1 Like