Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python support ++? [duplicate]

Tags:

python

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 ++?

like image 733
sunkehappy Avatar asked Nov 05 '12 10:11

sunkehappy


People also ask

Does Python allow duplicate values?

Python list can contain duplicate elements. Let's look into examples of removing the duplicate elements in different ways.

Which allow duplicates in Python?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

Can dictionaries have duplicate values?

The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry.

Why set Cannot have duplicate values in Python?

Set by definition is unordered collections of unique elements, so they don't allow duplicates.


2 Answers

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
like image 65
Joseph Victor Zammit Avatar answered Sep 28 '22 00:09

Joseph Victor Zammit


It should look like

a = 6
a += 1
print a
>>> 7
like image 32
alexvassel Avatar answered Sep 28 '22 00:09

alexvassel