変数の中身の入れ替え|Pythonのお勉強
2021/12/19 2022/2/14
変数の中身を入れ替えたりする方法です。
# Swapping values of 2 variables.
x = 100
y = 200
x, y = y, x
print(f"x is {x}, and y is {y}")
# Swapping values of 3 variables.
a = 100
b = 200
c = 300
a, b, c = c, a, b
print(f"a is {a}, b is {b}, and c is {c}")
上記コードの結果はこんな感じです。
>>>
*** Remote Interpreter Reinitialized ***
x is 200, and y is 100
a is 300, b is 100, and c is 200
>>>