Jeremy Jeremy - 19 days ago 6
Python Question

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

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

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

Comments