python中基本运算符
Python is considered to be a consistent and readable language. Unlike in Java, python does not support the increment (++) and decrement (--) operators, both in precedence and in return value.
Python被认为是一致且易读的语言。 与Java不同,python在优先级和返回值上均不支持递增(++)和递减(-)运算符 。
For example, in python the x++ and ++x or x-- or --x is not valid.
例如,在python中, x ++和++ x或x--或--x无效。
x=1
x++
Output
输出量
File "main.py", line 2
x++
^
SyntaxError: invalid syntax
x=1
print(--x)
# 1
print(++x)
# 1
print(x--)
'''
File "main.py", line 8
print(x--)
^
SyntaxError: invalid syntax
'''
The reason for this is, in python integers are immutable and hence cannot be changed. So, we will have to do the following for incrementing,
原因是,在python中,整数是不可变的,因此无法更改。 因此,我们必须执行以下操作才能递增,
x = 1
x=x+1
print(x)
x=x-1
print(x)
x +=2
print(x)
x -= 1
print(x)
Output
输出量
2
1
3
2
翻译自: https://www.includehelp.com/python/behavior-of-increment-and-decrement-operators.aspx
python中基本运算符