'+=' sign


#1



Can anyone please explain what the '+=' sign signifies in Python? Thanks :)


#2

A short example:

cost = 20
cost = cost + 20
print cost
# 40

You can replace cost = cost + 20 with cost += 20.

cost = 20
cost += 20
print cost
# 40

There are similar operators for subtraction, dividing, multiplying and exponentation : -=, /=, *=, **=


#3

Hi @hrithiktheboss ,

+= is one of Python's augmented assignment operators. Like other augmented assignment operators, it evaluates the expression to the right of the operator, performs the binary operation specific to the type of assignment on the variable to the left of the operator and that expression, and finally assigns the result to the variable.

For example, this will result in x having the value, 7 ...

x = 4
x += 3

Some other examples of augmented assignment operators include -=, *=, /=, and //=.

See 6.3.1 Augmented Assignment statements.


#4

Thank you, @arkussss @appylpye! By the way, if you guys are interested, check out this book on Python:
Dive Into Python by Mark Pilgrim


#5

I honestly don't think that they need it :slight_smile:


#6

+= appends
= sets
++before add after
after++ add before
== compare
=== compare types