bhylu.com

Advertisements

Python Dictionaries

A dictionary maps unique hashable keys to values. It is a useful representation for a named record or a lookup table.

Understand the idea

Dictionary iteration preserves insertion order in supported modern Python, but lookup is by key rather than numeric position. Assigning an existing key replaces its value without creating a duplicate key. Keep the value shape consistent where possible so the record remains easy to validate and use.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

product = {"name": "Desk lamp", "stock": 8}
print(product["name"])
print(product["stock"])

Read the result

The outputs are Desk lamp and 8.

Try one change

Add a price_cents field and describe the expected type of every field in the record.