【发布时间】:2023-02-04 13:52:46
【问题描述】:
代码太简单了,我知道这段代码可以正常工作
# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
我想要任何其他方法来执行此操作,是否有任何标准方法可以执行此操作,如果是,请告诉我。
【问题讨论】:
-
简单的解决方案-
x, y = y, x -
试试
x, y = y, x? -
请注意,如果您必须使用更复杂的表达式(如 e. G。交换
x和y[x]。
标签: python variables syntax swap