LoginSignup
0
2

More than 3 years have passed since last update.

【随時更新(予定)】Pythonのデータを格納するオブジェクト

Last updated at Posted at 2019-08-29

はじめに

自分の備忘もかねて、基本中の基本をまとめていきます

データを格納するオブジェクト

リスト(list)
タプル(tuple)
集合(set)
辞書(dictionary)

タプル(Tuple)

tuple.py
location = (13.4125, 103.866667)

集合(Set)

set.py
numbers = {1, 2, 6, 3, 1, 1, 6}

リスト(List)

list.py
list_of_random_things = [1, 3.4, 'a string', True] 
#listはstring, int, floatすべて可能

list_of_month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"]
                   0      1      2      3      4      5       ......
#listのindexは0から始まる(一番初めの要素からの距離)
list_of_month[1] #["Feb"]

#list[(含む):(含まない)]
list_of_month[1:4] #["Feb", "Mar", "Apr" ]
list_of_month[:3] #["Jan", "Feb", "Mar" ]
list_of_month[5:] #["Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"] 

#上書き可能(可変)
my_lst = [1, 2, 3, 4, 5]
my_lst[0] = 'one'
print(my_lst) #['one', 2, 3, 4, 5]

辞書(Dictionary)

dictionary.py
elements = {"hydrogen": 1, "helium": 2, "carbon": 6}
animals = {'dogs': [20, 10, 15, 15], 'cats': [3,4,2,8,2,4], 'rabbits': [2, 3, 3]}
#検索1
print(animals['dogs']) #[20, 10, 15, 15]
#検索2
print(animals['dogs'][3]) #15
#新しいkeyとvalueの追加
animals["birds"] = [4,5,5,6]  
print(animals) #{'dogs': [20, 10, 15, 15], 'cats': [3,4,2,8,2,4], 'rabbits': [2, 3, 3], 'birds': [4,5,5,6]}

まとめ

データ型 順序 変更
追加
削除
表記
タプル(Tuple) × () or tuple() (5.7, 4, 'yes', 5.7)
セット(Set) × { } or set() {5.7, 4, 'yes'}
リスト(List) [] or list() [5.7, 4, 'yes', 5.7]
辞書(Dictionary) × { } or dict() {'Jun': 75, 'Jul': 89}

参考

はじめてにQiitaに当たって書き方を参考に
Qiita書き方

これを使って練習したい(願望)
Hackerrank

0
2
3

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
2