Why Are += And =+ Different?


rather than += to find out the value of the rainfall why can’y i just =+ instead?
+= vs =+
it’s not the same but why?

FAQ: Learn Python - Python Syntax - Updating Variables

Hi @intergrain,

They have different meanings by design. the plus equals operator (+=) takes the variable and adds the amount given to the variable, like this:

foo = 3
foo += 5


# output:

The “equals plus” operator you’re asking about though, just means “set the variable to this positive integer”:

foo = 3
foo =+ 5 # the same as: foo = 5


# output:

Note that this works in reverse as well. -= and =- will subtract the amount from the variable’s current value or give the variable a new value equal to a negative integer, depending on which one you use.


Thanks it’s been about 2 days since I posted and I get it now but I’m still sad it doesn’t work the way I want it to :sob: lol


This topic is solved.