Posts

Showing posts with the label Decrement

Behaviour Of Increment And Decrement Operators In Python

Answer : ++ is not an operator. It is two + operators. The + operator is the identity operator, which does nothing. (Clarification: the + and - unary operators only work on numbers, but I presume that you wouldn't expect a hypothetical ++ operator to work on strings.) ++count Parses as +(+count) Which translates to count You have to use the slightly longer += operator to do what you want to do: count += 1 I suspect the ++ and -- operators were left out for consistency and simplicity. I don't know the exact argument Guido van Rossum gave for the decision, but I can imagine a few arguments: Simpler parsing. Technically, parsing ++count is ambiguous, as it could be + , + , count (two unary + operators) just as easily as it could be ++ , count (one unary ++ operator). It's not a significant syntactic ambiguity, but it does exist. Simpler language. ++ is nothing more than a synonym for += 1 . It was a shorthand invented because C compilers ...