I have recently started coding on python.. Today I came across a statement 10--2.. At first, I thought it might give an error but when I compiled the code the answer came out to be 12.. I don't have any clue how was this possible.. I have knowledge about increment and decrement operators but this doesn't make sense.. If anyone of you can describe the logic it will be of great help..
1 Answer
There is no increment or decrement operators (i++, i--) in Python unlike some other languages(c, Java)
If you want to do increment you need to do something like i = i+1 or simply i += 1
Here what happens is Python treats 10--2 as 10 - (-2),
That is
10 - (-2) = 10 + 2 = 12
Similarly, 10-+2 = 10 - 2 = 8
You can even do
10++2 = 12
10-+2 = 8
--decrement operator. What you see is actually10-(-2).