Back to Python Mastery
IntermediateTeenagers

Understanding Dictionaries

While lists are for ordered collections, 'dictionaries' are for storing data in 'key-value' pairs. Think of a real dictionary: you look up a word (the 'key') to find its definition (the 'value').

In Python, you create a dictionary using curly braces {}:

student = {
"name": "John Doe",
"age": 21,
"major": "Computer Science"
}

Here, "name", "age", and "major" are the keys, and "John Doe", 21, and "Computer Science" are their corresponding values.

You access a value by its key:
print(student["name"]) # This will print "John Doe"

You can add new key-value pairs:
student["gpa"] = 3.8

Or change an existing value:
student["age"] = 22

Dictionaries are extremely useful for organizing and retrieving related information.

Access Dictionary Data

Given the dictionary, access and print the value associated with the "major" key.