13

Possible Duplicate:
Behaviour of increment and decrement operators in Python

I'm new to Python, I'm confused about ++ python. I've tried to ++num but num's value is not changed:

>>> a = 1
>>> ++a
1
>>> print a
1
>>> print(++a)
1

Could somebody explain this? If Python support ++, why num has not changed. If it doesn't why can I use ++?

Community
  • 1
  • 1
sunkehappy
  • 8,970
  • 5
  • 44
  • 65

3 Answers3

13

No:

In [1]: a=1

In [2]: a++
------------------------------------------------------------
   File "<ipython console>", line 1
     a++
        ^
SyntaxError: invalid syntax

But you can:

In [3]: a+=1

In [4]: a
Out[4]: 2
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
  • " There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. " as stated in the [Zen of Python](https://en.wikipedia.org/wiki/Zen_of_Python) – Kristof Gilicze Apr 08 '21 at 11:49
1

It should look like

a = 6
a += 1
print a
>>> 7
alexvassel
  • 10,600
  • 2
  • 29
  • 31
0

There should be one and preferably only one obvious way to do it

>>> a = 1
>>> a += 1
>>> a
2
Rag Sagar
  • 2,314
  • 1
  • 18
  • 21