1. 程式人生 > >Python交換兩個變量值的函數

Python交換兩個變量值的函數

nbsp 方法 pytho fun return code turn ret int

方法1:(錯誤)

def func(a,b):
    a,b = b,a

a = 1
b = 2
func(a,b)
print(a,"   ",b)

方法2:(正確)

def func(a,b):
    return b,a

a = 1
b = 2
a,b = func(a,b)
print(a,"   ",b)

Python交換兩個變量值的函數