LoginSignup
1
0

Pythonで「タプルの要素を取得する」の動作を確認してみた

Posted at

概要

Pythonで「タプルの要素を取得する」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
mytuple = ("Orange", "Lemon", "Apple")
print(mytuple[0])
print(mytuple[1])
print(mytuple[2])

mytuple = ("Orange", "Lemon", "Apple")
print(mytuple[-1])
print(mytuple[-2])
print(mytuple[-3])

mytuple = ("Orange", "Lemon", "Apple")
#print(mytuple[3])

mytuple = ("Orange", "Lemon", "Apple")
print(len(mytuple))

mytuple = ("Orange", "Lemon", "Apple")
print(mytuple[len(mytuple) - 1])

animaltuple = ("Cat", "Dog", "Cow", "Monkey")
print("最初の要素は " + animaltuple[0] + " です。")
print("最後の要素は " + animaltuple[len(animaltuple) - 1] + " です。")

以下のコマンドを実行しました。

$ python3 sample.py 
Orange
Lemon
Apple
Apple
Lemon
Orange
3
Apple
最初の要素は Cat です。
最後の要素は Monkey です。

まとめ

何かの役に立てばと。

1
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
1
0