In python, you can make use of the augmented assignment operator to increase or decrease the value of a variable by 1. An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. It is very common to make use of augmented assignment operator in python.
Other languages such as JAVA has ++ and — operators to perform similar operations. These operators can be used for either pre-increment or post-increment. When you use pre-increment, the value of the expression is taken after performing the increment. When you use post-increment, though, the value of the expression is taken before incrementing, and stored for later use, after the result of incrementing is written back into the variable.
Python doesn’t implement this directive for increment/decrement operations. However, what python does implement is the unary operator + and – . For an example – when used ahead of a variable acts as a unary negation operator. This also entails that you can use any number of such operators ahead of an expression or variable.
Therefore ++variable and —variable is perfectly legal in python. However the results aren’t the same for Python and JAVA. Following code blocks should help understand the intentions of the blog.
>>> i = 7 >>> - i -7 >>> + i 7 >>> -- i 7 >>> ++ i 7 >>> --- i -7 >>> +++ i 7 >>> i ++ File "", line 1 i ++ ^ SyntaxError: invalid syntax >>> i -- File "", line 1 i -- ^ SyntaxError: invalid syntax >>>
As a JAVA developer unknown of this scenario, you could clearly mess up a python codebase. However the unary operators + and – are not valid post variable which totally makes sense.