The difference between '+=' and '=+'?

faceless picture faceless · Nov 17, 2016 · Viewed 32.3k times · Source

So I have a simple piece of code that prints out the integers 1-10:

i = 0
while i < 10:
        i += 1
        print(i)

Then if you just change one operator around on line 3, it prints out an infinite amount of 1 integers(which i understand why it does that). Why isn't a syntax error occurring when running this second program? Wouldn't it call a syntax error in the event of an assignment operator being followed by an addition operator??

i = 0
while i < 10:
        i =+ 1
        print(i)

Answer

Kittsil picture Kittsil · Nov 17, 2016

i+=1 is the same as i=i+1, whereas i=+1 just means i=(+1).