指定した値と同じ要素がタプルに含まれているか確認する
【開発環境】
OS:Win10(64ビット)
言語:Python3.8.5(64bit)
Python の統合開発環境:IDLE
IDLEの操作は別サイト参照のこと
【in 演算子】
・要素が含まれているかどうかの確認
書式
値 in タプル
サンプルコード
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> mytuple = ("A", "B", "C", "D", "E")
>>> print("B" in mytuple)
True
>>> print("D" in mytuple)
True
>>> print("G" in mytuple)
False
>>>
【count メソッド】
指定の値と同じ値を持つ要素がタプルに何個含まれているか取得する方法
・書式
タプル.count(値)
サンプルコード
>>> mytuple = ("A", "B", "A", "A", "C")
>>> print(mytuple.count("A"))
3
>>> print(mytuple.count("B"))
1
>>> print(mytuple.count("D"))
0
>>>
【index メソッド】
指定の値と同じ値を持つ要素がタプルに含まれている場合に、その要素のインデックスを取得する方法
サンプルコード
>>> mytuple = ("A", "B", "A", "A", "C")
>>> print(mytuple.index("A"))
0
>>> print(mytuple.index("B"))
1
>>> print(mytuple.index("C"))
4
>>>