What is a Dictionary?

In Python, a dictionary is a collection of key-value pairs.

download (2).png

Here are the key characteristics of a Python dictionary:

  1. Unordered: Dictionaries don't maintain order. (Ordered (Python 3.7+))
  2. Unique Keys: Keys must be unique.
  3. Mutable: You can change the dictionary by adding, updating, or removing items.
  4. Keys are Immutable: Keys can be any immutable type like strings, numbers, or tuples.

download (1).png

1. Creating a Dictionary

student = {
    "name": "Tarif",
    "age": 22,
    "course": "Computer Science"
}
print(student)

'''
**{'name': 'Tarif', 'age': 22, 'course': 'Computer Science'}**
'''

2. Accessing Values

print(student["name"])  # Output: Tarif
print(student["age"])   # Output: 22