1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】毎週勉強するシリーズ ~辞書~

Last updated at Posted at 2025-03-04

概要

AI が流行ってる昨今 Python は必須級のスキルになってきてます。
時代に取り残されないようにコツコツと Python を勉強しようと思います!
三日坊主にならないように Qita でアウトプットしながら進めます💪

今日やること

とほほのPython入門 - リスト・タプル・辞書 の辞書を理解する。

辞書(dict)

1. 基本的な書き方

辞書(dict)は {...} でキーと値のペアのリストを保持します。

d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}

2. 各要素へのアクセス

各要素には次のようにアクセスします。

d1 = d['Yamada']
d2 = d['Suzuki']
d3 = d['Tanaka']

3. 要素を追加する

存在しないキーペアで代入することで追加できます。

d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
d['Kimura'] = 60

4. 要素を更新する

存在するキーペアで代入することで追加できます。
今回は Yamada のキーに対する値を 3060 に変更しています。

d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}
d['Yamada'] = 60

5. 全ての要素や値を参照する

items(), keys(), valus() を使用します。
参照される要素の順序は順不同です。

d = {'Yamada': 30, 'Suzuki': 40, 'Tanaka': 80}

for k, v in d.items():
    print(k, v)            # Tanaka 80, Yamada 30, Suzuki 40

for k in d.keys():
    print(k, d[k])         # Suzuki 40, Yamada 30, Tanaka 80

for v in d.values():
    print(v)               # 80, 30, 40

感想

今日は辞書をやってみました!

今回も見ていただきありがとうございました。
また次回お会いしましょう!!

次回やること

とほほのPython入門 - リスト・タプル・辞書 のリスト関数(map(), filter(), reduce())を理解する。

1
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?