LoginSignup
9

More than 5 years have passed since last update.

posted at

Organization

Python: Tips - 値の入れ替え

変数の値を入れ替える方法

変数a:100と変数b:20の値を入れ替えたい時には、普通は変数cを作成して、aの値を一旦cに保持して、abの値を入れ替える。

swap.py
a = 100
b = 20

pythonの変数の多重代入を使うともっと簡単に値の交換ができる

easy_swap.py
# 多重代入
a, b = 100,20

# 値の交換も
a, b = b, a

ちなみに、Rubyも同じことができる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
9