A profile page shows a known name and an optional nickname.
What You Need to Understand
Dictionary lookup is based on keys, not numeric positions. record[key] returns a value or raises KeyError when the key is absent. record.get(key, default) offers a fallback.
When to Use It
A profile page shows a known name and an optional nickname.
Step-by-Step Python Example
profile = {"name": "Maya", "city": "Haifa"}
print(profile["name"])
print(profile.get("nickname", "Not provided"))
print("city" in profile)
How the Example Works
The direct name lookup succeeds; the missing nickname returns the fallback; membership checks keys by default.
Example Output
Maya
Not provided
True
True
None
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
data = {"level": None}
print("level" in data)
print(data.get("level"))
Important Behavior and Edge Cases
A key mapped to None differs from a missing key. get() returning None alone cannot always tell these cases apart.
Try It Yourself
Check both a present and missing key using in and get().
Common Beginner Mistakes
- A key mapped to
Nonediffers from a missing key.get()returningNonealone cannot always tell these cases apart. - Treating missing dictionary keys, replacement precedence, and shallow copies as if they had identical behavior in every context.
Key Takeaways
- Dictionary lookup is based on keys, not numeric positions.
- The direct name lookup succeeds; the missing nickname returns the fallback; membership checks keys by default.
- A key mapped to
Nonediffers from a missing key.