# Read the dict. Don't change it.
# inspection — in, len, keys, values, items, copy

soldier = {"name": "Raven", "score": 85, "active": True}

# ─────────────────────────────────────────────
# in / not in — check if a key exists
# ─────────────────────────────────────────────

print("name" in soldier)        # True
print("rank" in soldier)        # False
print("rank" not in soldier)    # True

# in checks keys only — not values
print(85 in soldier)            # False — 85 is a value, not a key

# useful with if
if "score" in soldier:
    print(soldier["score"])     # 85

# ─────────────────────────────────────────────
# len() — number of key-value pairs
# ─────────────────────────────────────────────

print(len(soldier))             # 3

empty = {}
print(len(empty))               # 0

# ─────────────────────────────────────────────
# keys(), values(), items() — views
# ─────────────────────────────────────────────

print(soldier.keys())
# dict_keys(['name', 'score', 'active'])

print(soldier.values())
# dict_values(['Raven', 85, True])

print(soldier.items())
# dict_items([('name', 'Raven'), ('score', 85), ('active', True)])

# convert to list when needed
print(list(soldier.keys()))     # ['name', 'score', 'active']
print(list(soldier.values()))   # ['Raven', 85, True]

# views update automatically when dict changes
keys_view = soldier.keys()
soldier["rank"] = "Sergeant"
print(keys_view)
# dict_keys(['name', 'score', 'active', 'rank']) — updated

# ─────────────────────────────────────────────
# Checking a value — not a key
# ─────────────────────────────────────────────

soldier = {"name": "Raven", "score": 85}

print(85 in soldier.values())   # True
print(99 in soldier.values())   # False
print("Raven" in soldier.values())  # True

# ─────────────────────────────────────────────
# copy() — independent copy
# ─────────────────────────────────────────────

soldier = {"name": "Raven", "score": 85}
backup = soldier.copy()
backup["score"] = 99

print(soldier)  # {'name': 'Raven', 'score': 85}  — unchanged
print(backup)   # {'name': 'Raven', 'score': 99}

# without copy() — same dict, two names
a = {"name": "Raven"}
b = a
b["score"] = 85
print(a)        # {'name': 'Raven', 'score': 85}  — changed too

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# "key" in d          — True if key exists
# "key" not in d      — True if key missing
# val in d.values()   — True if value exists anywhere
# len(d)              — number of key-value pairs
# d.keys()            — view of all keys
# d.values()          — view of all values
# d.items()           — view of all (key, value) pairs
# d.copy()            — independent copy
#
# in checks keys only
# views are not lists — convert with list() if needed
# views update automatically when the dict changes
# backup = d is not a copy — use .copy()
