When I write Python in parallel with C or Java code, I sometimes want to write operators like i ++ and i-- for increments and decrements ...
However, as you can see by trying this, an error will occur.
>>> i=1
>>> i++
SyntaxError: invalid syntax
There seems to be some theory as to why operators like "++" and "-" are not implemented in Python, but they are the creators of Python [benevolent dictator](http: // www. Former Google engineer Guido van Rossum, artima.com/weblogs/viewpost.jsp?thread=235725), understands Python for both writers and readers without giving complex meanings to "+" and "-". It seems that he wanted to make it easy.
By the way, languages like Ruby and Scala don't have these operators either. There seem to be various reasons for "not" here either ...
Anyway, I personally appreciate the specifications because I don't have to worry about the errors caused by the front and back.
In Python, there is no problem if you write as follows.
>>> i+=1 #Increment
>>> print(i)
2
>>> i-=1 #Decrement
>>> print(i)
1
Also, regarding frequently used for statements
>>> for(int i=0; i<10; i++)
not
>>> for i in range(0, 10)
You just have to write.
By the way, you don't usually see it, but you can write it as follows by using complement. If you want to write ...
>>> i=-~1
>>> print(i)
2
As a bonus, the character increment (?) Can be written as:
>>> print(chr(ord('a')+1))
b
[LINUX Shugendo-Increment of PYTHON! ](Https://linuxshugendo.wordpress.com/2014/02/28/python-%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%AF%E3%83%AA % E3% 83% A1% E3% 83% B3% E3% 83% 88% EF% BC% 81 /) Why are there no ++ and -- operators in Python? Weekend Kyoto-Character Increment
Recommended Posts