Why x - = x + 4 returns -4 instead of 4

new for python and trying to deal with exact points of assignment operators. Here is my code and then the question.

x = 5 print(x) x -= x + 4 print(x) 

the above code returns 5 for the first time, but another -4 for the second print. In my head, I feel that the number should be 4, as I read it as x = x - x +4. However, I know this is wrong, since python returns -4. I would be happy if someone could explain to me (in simple terms, as I am new), as I really banged my head on the table on this.

+5
source share
1 answer

x -= x + 4 can be written as:

 x = x - (x + 4) = x - x - 4 = -4 
+14
source

Source: https://habr.com/ru/post/1233748/


All Articles