Python if a>;b:b,a=a,b的意思是

Python if a>;b:b,a=a,b的意思是,python,Python,我刚刚开始自学Python,我偶然发现了一个基于用户输入的排序活动 代码如下: a = int(input("Enter Num1:")) b = int(input("Enter Num2:")) c = int(input("Enter Num3:")) d = int(input("Enter Num4:")) if a > b: b, a = a, b if b > c: c, b = b, c

我刚刚开始自学Python,我偶然发现了一个基于用户输入的排序活动

代码如下:

a = int(input("Enter Num1:"))
b = int(input("Enter Num2:"))
c = int(input("Enter Num3:"))
d = int(input("Enter Num4:"))

if a > b: b, a = a, b
if b > c: c, b = b, c
if c > d: d, c = c, d
if a > b: b, a = a, b
if b > c: c, b = b, c
if a > b: b, a = a, b

print(d, c, b, a)

我对if-else语句有一个基本的了解,但是我不知道这一行中发生了什么
if-a>b:b,a=a,b
。请给我解释一下。谢谢

它的a交换操作将a的值分配给b,反之亦然

a, b = 3, 4 # assign 3 to a, 4 to b
a, b = b, a
print(a, b) # prints "4 3"

x,y
是分解的元组,其中
x
是有序对的第一个元素,
y
是第二个元素<代码>=是赋值运算符。所以基本上
b,a=a,b
交换值:
b
变成
a
a
变成
b

pair = (1, 2)
(x, y) = pair
x, y = pair  # you can omit parenthesis
x, y = y, x  # assign new values