Python - 变量赋值和重新赋值 - X 在这里发生了什么?



我正在处理一个关于变量的谜题,无法弄清楚我的变量是如何在下面的过程中从1变为0的:

x = 1
y = 0
# first assignment
x = x^y
print(f'x value is {x} after reassignment')
# x value is 1 after reassignment
# second assignment
y = y^x
print(f'y value is {y} after reassignment')
# y value is 1 after reassignment
print(f'{y} = {y} ^ {x}')
# returns 1 = 1 ^ 1
# x value is now 1 but somehow ZERO after the assignment below
x = x^y   
print(f'{x} = {y} ^ {x} how did x become zero from this assignment?')
# returns 0 = 1 ^ 0

我想知道x是怎么变成零的。非常感谢。

您误解了正在使用的运算符。在Python中,^运算符是位XOR。因此CCD_ 2。您关于1 = 1 ^ 1的假设是不正确的。如果您正试图提高功率,请使用x**y

最新更新