Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: Python:递增和递减运算符的行为
你好,这个我试过了。
++num
并且 num 完全没有变化,总是在初始化时显示值
如果我++num改为num+=1然后它工作。
num+=1
所以,我的问题是那个++操作员是如何工作的?
++
python中没有++运算符。您将一元+两次应用于变量。
+
答:Python 中没有++运算符。+= 1是增加数字的正确方法,但请注意,由于整数和浮点数在 Python 中是不可变的,
+= 1
>>> a = 2 >>> b = a >>> a += 2 >>> b 2 >>> a 4
此行为不同于可变对象的行为,可变对象b在操作后也会更改:
b
>>> a = [1] >>> b = a >>> a += [2] >>> b [1, 2] >>> a [1, 2]