KrishnaChaitanya67
KrishnaChaitanya67

Reputation: 59

Why does python have a pre-increment operator but not post-increment?

i tried using pre-increment operator(++var_name) in python 3.7.1 and it doesn't produce any Syntaxerror unlike post-increment operator (var_name++) which produced a Syntaxerror.Can someone explain this??

count = 0
++count
print(count)

and the output is :

0
[Finished in 0.7s]

but when i use post-increment the output was different

count = 0
count++
print(count)

t

he output is :

 count++
          ^
SyntaxError: invalid syntax
[Finished in 0.1s]

Upvotes: 3

Views: 306

Answers (2)

Diego Lopez
Diego Lopez

Reputation: 39

Python doesn't support ++, but you can do:

count += 1

Upvotes: 1

cs95
cs95

Reputation: 403100

Python does not have an increment operator, pre or post. ++count is interpreted as two operations of the unary + operator: (+(+count)), which does nothing here, so the result is 0.

To increment, the only option is to use the in-place addition operator, and increment by 1:

count += 1

Upvotes: 4

Related Questions