LoginSignup
0
0

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 23. タプルの使い所

Posted at

※この記事はUdemyの
現役シリコンバレーエンジニアが教えるPython3入門+応用+アメリカのシリコンバレー流コードスタイル
の講座を受講した上での、自分用の授業ノートです。
講師の酒井潤さんから許可をいただいた上で公開しています。

■タプルの使い所

◆例

A、B、Cの中から2つ選んでもらうとする。

multiple_choice
choose_two = ('A', 'B', 'C')

# 空のリストを用意
answer = []
# AとCを選択したとする
answer.append('A')
answer.append('C')

print('Choise from this.', choose_two)
print('Your answer is', answer)
result
Choise from this. ('A', 'B', 'C')
Your answer is ['A', 'C']

choose_twoをリストで作ってしまうと、後から選択肢が書き換わってしまう恐れがある。
タプルにしておけば、間違ってchoose_two自体をいじるコードを書いたところでエラーとなるため、
バグの防止につながる。

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0