Dictionaries
Source: 3-Data Structures/3.4-Dictionaries.ipynb
Start here — no coding background needed
What you will learn
Look up values by a name (key) — like a phone contact book.
In simple words
Dictionary `{key: value}` — find phone by name, price by product code.
Think of it like this
Contacts app: name → phone number.
Words to know:
- dict — Key → value pairs in curly braces
- key — The label you look up
Ways to store many values together — shopping lists, contacts, unique items.
Easy example — run this first. Change values and press Run again.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Reference notes (from full bootcamp)
Optional — deeper detail for when you are ready
Dictionaries
Video Outline:
- Introduction to Dictionaries
- Creating Dictionaries
- Accessing Dictionary Elements
- Modifying Dictionary Elements
- Dictionary Methods
- Iterating Over Dictionaries
- Nested Dictionaries
- Dictionary Comprehensions
- Practical Examples and Common Errors
Introduction to Dictionaries
Dictionaries are unordered collections of items. They store data in key-value pairs.
Keys must be unique and immutable (e.g., strings, numbers, or tuples), while values can be of any type.
Expected (from notebook): <class 'dict'>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{}Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul', 'age': 32, 'grade': 24}
<class 'dict'>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 24, 'age': 32}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul', 'age': 32, 'grade': 'A'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): A 32 A None Not Available
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul', 'age': 32, 'grade': 'A'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul', 'age': 33, 'grade': 'A'}
{'name': 'Anshul', 'age': 33, 'grade': 'A', 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul', 'age': 33, 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
dict_keys(['name', 'age', 'address'])
dict_values(['Anshul', 33, 'India'])
dict_items([('name', 'Anshul'), ('age', 33), ('address', 'India')])
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul1', 'age': 33, 'address': 'India'}
{'name': 'Anshul1', 'age': 33, 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul2', 'age': 33, 'address': 'India'}
{'name': 'Anshul2', 'age': 33, 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul2', 'age': 33, 'address': 'India'}
{'name': 'Anshul2', 'age': 33, 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'name': 'Anshul2', 'age': 33, 'address': 'India'}
{'name': 'Anshul3', 'age': 33, 'address': 'India'}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): name age address
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): Anshul3 33 India
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): name:Anshul3 age:33 address:India
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'student1': {'name': 'Anshul', 'age': 32}, 'student2': {'name': 'Peter', 'age': 35}}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): Peter 35
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
dict_items([('student1', {'name': 'Anshul', 'age': 32}), ('student2', {'name': 'Peter', 'age': 35})])Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
student1:{'name': 'Anshul', 'age': 32}
name:Anshul
age:32
student2:{'name': 'Peter', 'age': 35}
name:Peter
age:35
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{1: 1, 2: 2, 3: 3, 4: 4}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook):
{'a': 1, 'b': 3, 'c': 4}
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Conclusion
Dictionaries are powerful tools in Python for managing key-value pairs. They are used in a variety of real-world scenarios, such as counting word frequency, grouping data, storing configuration settings, managing phonebooks, tracking inventory, and caching results. Understanding how to leverage dictionaries effectively can greatly enhance the efficiency and readability of your code.
Practice test — try yourself
Write code, press Check. Wrong answer shows the correct code to copy & run.
student = {"name":"Anshul","city":"Dehradun"}. Print the city.