Search code examples
pythonoperators

What is the difference between '+=' and '=+'?


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)

Solution

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