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

时间:2018-12-11 17:53:25

标签: python variables global-variables variable-assignment

我正在研究变量问题,无法弄清楚我的变量在以下过程中如何从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如何变为零。谢谢!

1 个答案:

答案 0 :(得分:2)

您误解了所使用的运算符。在Python中,^运算符是按位XOR。因此1 ^ 1 = 0。您认为1 = 1 ^ 1不正确。如果您尝试提高力量,请使用x**y

相关问题