Reputation: 59
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
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