-2

I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:

a = 5
print(a++)

But print(++a) works fine. Can anyone help me understand the difference? Btw, it seems that print(a+=1) also doesn't work. Thanks!

Wei He
  • 1
  • And just for added information about why there is no support for `++` and `--`, read [here](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python) – idjaw Feb 11 '18 at 00:49

1 Answers1

7
  • ++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
  • a++ is not valid python syntax (unlike other languages).
  • a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment
TerryA
  • 58,805
  • 11
  • 114
  • 143