EASYTUTORGUIDE

Practical tutorials, tools, courses, digital skills, and business promotion.

Free Learning

JavaScript – Chapter 2: Development Environment

Learn what JavaScript is, where it runs, why it is important, and how beginners can start using it with simple examples and clear output.

Beginner Friendly JavaScript Basics Web Development Code Examples
JavaScript Lesson 2 Chapter 2 Topics Day / Night Mode

Main reading content

python-course-chapter-10

Chapter 10: Dictionaries

Learn how Python dictionaries store information as key-value pairs.

Goal: Create, read, update, remove, loop through, copy, merge, and apply dictionaries in practical Python programs.

Chapter 10 Topics

10.1 Introduction to Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how introduction to dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Ali", "age": 14}
print(student)

Output

{'name': 'Ali', 'age': 14}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.2 Keys and Values

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how keys and values works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Sara", "age": 15}
print(student["name"])

Output

Sara

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.3 Creating Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how creating dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

car = {"brand": "Toyota", "year": 2024}
print(car)

Output

{'brand': 'Toyota', 'year': 2024}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.4 Accessing Values

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how accessing values works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

person = {"name": "Mina", "city": "Toronto"}
print(person["city"])

Output

Toronto

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.5 Using get()

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how using get() works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

person = {"name": "Mina"}
print(person.get("age", "Not found"))

Output

Not found

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.6 Adding Items

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how adding items works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

book = {"title": "Python Basics"}
book["pages"] = 250
print(book)

Output

{'title': 'Python Basics', 'pages': 250}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.7 Changing Values

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how changing values works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

product = {"price": 10}
product["price"] = 12
print(product)

Output

{'price': 12}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.8 Updating Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how updating dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

user = {"name": "Ali"}
user.update({"age": 14, "city": "Vaughan"})
print(user)

Output

{'name': 'Ali', 'age': 14, 'city': 'Vaughan'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.9 Removing Items

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how removing items works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

colors = {"a": "red", "b": "blue"}
del colors["a"]
print(colors)

Output

{'b': 'blue'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.10 pop()

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how pop() works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

scores = {"math": 90, "science": 85}
removed = scores.pop("math")
print(removed)
print(scores)

Output

90
{'science': 85}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.11 popitem()

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how popitem() works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

data = {"a": 1, "b": 2}
item = data.popitem()
print(item)
print(data)

Output

('b', 2)
{'a': 1}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.12 Using del

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how using del works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

account = {"name": "Sam", "active": True}
del account["active"]
print(account)

Output

{'name': 'Sam'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.13 Clearing Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how clearing dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

settings = {"sound": True, "theme": "dark"}
settings.clear()
print(settings)

Output

{}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.14 Checking Keys

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how checking keys works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Ali", "grade": 8}
print("grade" in student)

Output

True

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.15 Dictionary Length

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionary length works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Ali", "grade": 8}
print(len(student))

Output

2

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.16 Getting Keys

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how getting keys works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

car = {"brand": "Toyota", "year": 2024}
print(list(car.keys()))

Output

['brand', 'year']

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.17 Getting Values

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how getting values works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

car = {"brand": "Toyota", "year": 2024}
print(list(car.values()))

Output

['Toyota', 2024]

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.18 Getting Key-Value Pairs

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how getting key-value pairs works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

car = {"brand": "Toyota", "year": 2024}
print(list(car.items()))

Output

[('brand', 'Toyota'), ('year', 2024)]

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.19 Looping Through Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how looping through dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Ali", "grade": 8}
for key, value in student.items():
    print(key, value)

Output

name Ali
grade 8

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.20 Nested Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how nested dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

school = {"student1": {"name": "Ali", "grade": 8}}
print(school["student1"]["name"])

Output

Ali

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.21 Dictionaries Containing Lists

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionaries containing lists works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Sara", "subjects": ["Math", "Science"]}
print(student["subjects"][0])

Output

Math

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.22 Lists Containing Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how lists containing dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

students = [{"name": "Ali"}, {"name": "Sara"}]
print(students[1]["name"])

Output

Sara

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.23 Copying Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how copying dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

original = {"a": 1, "b": 2}
copy_data = original.copy()
print(copy_data)

Output

{'a': 1, 'b': 2}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.24 Merging Dictionaries

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how merging dictionaries works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

first = {"a": 1}
second = {"b": 2}
first.update(second)
print(first)

Output

{'a': 1, 'b': 2}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.25 Dictionary Union Operators

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionary union operators works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

first = {"a": 1}
second = {"b": 2}
combined = first | second
print(combined)

Output

{'a': 1, 'b': 2}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.26 Dictionary Unpacking

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionary unpacking works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

first = {"a": 1}
second = {"b": 2}
combined = {**first, **second}
print(combined)

Output

{'a': 1, 'b': 2}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.27 Dictionary Comprehensions

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionary comprehensions works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

squares = {number: number ** 2 for number in range(1, 4)}
print(squares)

Output

{1: 1, 2: 4, 3: 9}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.28 Conditional Dictionary Comprehensions

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how conditional dictionary comprehensions works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

even_squares = {n: n ** 2 for n in range(1, 6) if n % 2 == 0}
print(even_squares)

Output

{2: 4, 4: 16}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.29 setdefault()

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how setdefault() works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

profile = {"name": "Ali"}
profile.setdefault("city", "Toronto")
print(profile)

Output

{'name': 'Ali', 'city': 'Toronto'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.30 fromkeys()

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how fromkeys() works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

keys = ["name", "age", "city"]
record = dict.fromkeys(keys, "Unknown")
print(record)

Output

{'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.31 Sorting Dictionary Data

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how sorting dictionary data works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

scores = {"Ali": 88, "Sara": 95, "Mina": 90}
ordered = dict(sorted(scores.items()))
print(ordered)

Output

{'Ali': 88, 'Mina': 90, 'Sara': 95}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.32 Hashable Keys

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how hashable keys works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

valid = {("x", 1): "point"}
print(valid[("x", 1)])

Output

point

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.33 Dictionary Performance

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how dictionary performance works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

phone_book = {"Ali": "111", "Sara": "222"}
print(phone_book.get("Sara"))

Output

222

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.34 Practical Dictionary Applications

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how practical dictionary applications works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

inventory = {"apple": 5, "banana": 3}
inventory["apple"] += 2
print(inventory)

Output

{'apple': 7, 'banana': 3}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.35 Chapter Practice Exercises

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how chapter practice exercises works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

student = {"name": "Ali", "scores": [80, 90, 85]}
print(sum(student["scores"]) / len(student["scores"]))

Output

85.0

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

10.36 Chapter Mini Project

A dictionary is useful when information has labels instead of only positions. In this topic, you learn how chapter mini project works in Python. The example uses clear key-value data so a beginner can see how the dictionary stores information, how Python reads the instruction, and how the result appears. Dictionaries are mutable, which means their contents can usually be changed after creation.

Example

contacts = {}
contacts["Ali"] = "416-555-0101"
contacts["Sara"] = "416-555-0102"
print(contacts)

Output

{'Ali': '416-555-0101', 'Sara': '416-555-0102'}

Output explanation: Python runs the statements from top to bottom. It creates or changes the dictionary as shown, then the print statement displays the requested value or the final dictionary. The output matches the keys, values, method, or operation used in the example.

Chapter 2: Development EnvironmentPrepare Your JavaScript Workspace

A modern course built to help learners study step by step with clarity, comfort, and confidence.