Why Are += And =+ Different?


#1

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
#2

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

print(foo)

# output:
8

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

print(foo)

# output:
5

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.


#3

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


#4

This topic is solved.