What is a dictionary in Python?
A dictionary stores data as key: value pairs inside curly braces { }. You use the key to get the value. Keys must be unique, and from Python 3.7 a dictionary also remembers the order in which items were added.
student = {"name": "Robin", "age": 20}
print(student["name"])
student["city"] = "Delhi"
print(student)
print(student.get("phone", "not found"))
Output:
Tutorials Class - Output Window
Robin
{'name': 'Robin', 'age': 20, 'city': 'Delhi'}
not found
Using get() is safer than square brackets, because it does not raise a KeyError when the key is missing. See the Python Dictionary chapter for more methods.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |