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

③for/forの考え方を日常生活のイベントに当てはめてみた!

0
Posted at

✨Introduction

for/forの考え方が必要だと思い、まとめました。

🍱 ①:お弁当を詰める(おかずの種類 × その中の個別アイテム)

日常の流れ
  • おかずの種類を順番に見る(for
  • その種類の中の個別のおかずを順番に詰める(for
lunch_box.py
bento = {
    "main": ["唐揚げ", "ハンバーグ"],
    "side": ["卵焼き", "ほうれん草", "ポテサラ"],
    "dessert": ["みかん", "ゼリー"]
}

for category, items in bento.items():        # 大きいまとまり(種類)
    print(f"--- {category} ---")
    for item in items:                       # 小さいまとまり(個別アイテム)
        print(f"詰める: {item}")

🧺 ②:洗濯物(服の種類 × ポケットの中身)

日常の流れ
  • 服を1着ずつ見る(for
  • その服のポケットを全部確認する(for
laundry.py
clothes = {
    "ジーンズ": ["", "レシート"],
    "ジャケット": ["イヤホン"],
    "パーカー": []
}

for cloth, pockets in clothes.items():       # 服を順番に見る
    print(f"{cloth} のポケットを確認中…")
    for item in pockets:                     # ポケットの中身を全部確認
        print(f"{item} を取り出した")

🛒 ③:スーパーの棚(棚 × 商品)

日常の流れ
  • 棚を順番に見る(for
    *棚の中の商品を順番にチェックする(for
shopping.py
supermarket = {
    "お菓子": ["ポッキー", "チョコ", "グミ"],
    "飲み物": ["", "コーラ", "お茶"],
    "野菜": ["にんじん", "玉ねぎ"]
}

for shelf, items in supermarket.items():     # 棚ごとに回る
    print(f"=== {shelf} コーナー ===")
    for item in items:                       # 商品を順番にチェック
        print(f"商品: {item}")

🚉 ④:電車の路線図(路線 × 駅)

日常の流れ
  • 路線を順番に見る(for
  • その路線の駅を順番に確認する(for
train_lines.py
lines = {
    "東西線": ["中野", "高田馬場", "早稲田", "飯田橋", "大手町"],
    "山手線": ["新宿", "渋谷", "品川"]
}

for line, stations in lines.items():         # 路線を順番に見る
    print(f"--- {line} ---")
    for station in stations:                 # 駅を順番に確認
        print(f"駅: {station}")

🎮 ⑤:ゲームのステージ(ステージ × 敵キャラ)

日常の流れ
  • ステージを順番に進む(for
  • そのステージの敵を全部処理する(for
game_stage.py
game = {
    "ステージ1": ["スライム", "コウモリ"],
    "ステージ2": ["ゴブリン", "オオカミ", "魔法使い"]
}

for stage, enemies in game.items():          # ステージを順番に進む
    print(f"=== {stage} ===")
    for enemy in enemies:                    # 敵を全部処理
        print(f"敵を倒した: {enemy}")

🎯 Conclusion

for/for は “大きい箱 × 小さい箱” を全部処理する構造。
日常生活のほぼ全部に存在していて、Python に落とすと直感的に理解できる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?