LECTURE NOTES
Learning Outcomes:-
(a) Identify concept of tuples, sets and dictionaries
CP125
Topic 3: Tuples, Sets and Dictionaries
LECTURE NOTES
CP125
Table of Contents - Dictionaries
LECTURE NOTES
CP125
DICTIONARY
Definition of Dictionary:- |
A collection of key and value pairs enclosed in curly brackets. |
dictionary_name = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Syntax 🡪
- Recap
LECTURE NOTES
CP125
DICTIONARY
A phone book: {"Savannah":"476-3321", "Nathaniel":"351-7743"}
student = {
"name": "Ali",
"age": 20,
"course": "Computer Science"
}
print(student["name"]) # Output: Ali
Example 1
Example 2
Dictionary_name
key
value
key
value
- Recap
LECTURE NOTES
CP125
CRUD Operations
UPDATE (U) – Modifying Existing Elements in a Dictionary
DICTIONARY
Syntax dictionary_name[key] = new_value # Updating a single value |
Example |
LECTURE NOTES
CP125
CRUD Operations
UPDATE (U) – Modifying Existing Elements in a Dictionary
DICTIONARY
Syntax dictionary_name[key] = value # Adding a new key-value |
Example |
LECTURE NOTES
CP125
CRUD Operations
UPDATE (U) – Modifying Existing Elements in a Dictionary
DICTIONARY
Syntax dictionary_name.update({key1: new_value1, key2: new_value2})
|
LECTURE NOTES
CP125
CRUD Operations
UPDATE (U) – Modifying Existing Elements in a Dictionary
DICTIONARY
Example |
LECTURE NOTES
CP125
Try this ! Exercise 1 |
What will be the output of the following code? student = {"name": "Alice", "age": 20, "grade": "B"} # Updating an existing value student["grade"] = "A" # Adding a new key-value pair student["city"] = "New York" print(student) |
DICTIONARY
Review Activity : Modifying Existing Elements in a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 2 |
Which of the following correctly adds multiple key-value pairs to a dictionary?
student.add("grade", "A")
|
DICTIONARY
Review Activity : Modifying Existing Elements in a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 3 |
You have the following dictionary representing a book: book = { "title": "Python Basics", "author": "John Doe", "price": 50 }
|
DICTIONARY
Review Activity : Modifying Existing Elements in a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 4 |
Given the dictionary below: employee = {"name": "Aisha","department": "IT","salary": 5000}
|
DICTIONARY
Review Activity : Modifying Existing Elements in a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 5 |
You have the following dictionary representing a student: student = {"name": "Ali","age": 20,"course": "Mathematics“ }
|
DICTIONARY
Review Activity : Modifying Existing Elements in a Dictionary
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
DICTIONARY
Syntax del dictionary_name[key] # Deletes a specific key |
|
.
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
**'grade' key is removed
DICTIONARY
Example |
.
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
DICTIONARY
Syntax removed_value = dictionary_name.pop(key, default_value) |
|
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
DICTIONARY
Example |
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
DICTIONARY
Syntax dictionary_name.clear() # Removes all elements |
|
LECTURE NOTES
CP125
CRUD Operations
DELETE (D) – Removing Elements from a Dictionary
DICTIONARY
Example |
LECTURE NOTES
CP125
Try this ! Exercise 1 |
What will be the output of the following code? student = {"name": "Ali", "age": 20, "course": "Math"} del student["age"] print(student) A) {'name': 'Ali', 'age': 20, 'course': 'Math'}�B) {'name': 'Ali', 'course': 'Math'}�C) KeyError�D) {'name': 'Ali'} |
DICTIONARY
Review Activity : Removing Elements from a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 2 |
What will be stored in the variable removed_value after executing the following code? car = {"brand": "Toyota", "model": "Camry", "year": 2022} removed_value = car.pop("model") print(removed_value) A) "Toyota"�B) "Camry"�C) {"brand": "Toyota", "year": 2022}�D) KeyError |
DICTIONARY
Review Activity : Removing Elements from a Dictionary
LECTURE NOTES
CP125
Try this ! Exercise 3 |
What happens when you apply the clear() method to a dictionary? data = {"id": 101, "name": "John", "age": 25} data.clear() print(data) A) {'id': 101, 'name': 'John', 'age': 25}�B) {}�C) None�D) KeyError |
DICTIONARY
Review Activity : Removing Elements from a Dictionary
LECTURE NOTES
CP125
Other Operations
Checking Key Existence
DICTIONARY
Syntax if key in dictionary_name: print("Key exists!") |
|
LECTURE NOTES
CP125
Other Operations
Checking Key Existence
DICTIONARY
Example |
LECTURE NOTES
CP125
Try this ! Exercise 1 |
What will be the output of the following code? person = {"name": "John", "age": 30, "city": "New York"} if "age" in person: print("Key exists!") else: print("Key does not exist!") |
DICTIONARY
Review Activity : Checking Key Existence
LECTURE NOTES
CP125
Feature | Dictionary (dict) | Tuple (tuple) | Set (set) |
Definition | Key-value pairs | Ordered collection of elements | Unordered collection of unique elements |
Mutable? | ✅ Yes (values can change, keys cannot) | ❌ No (immutable) | ✅ Yes (elements can be added/removed) |
Ordered? | ✅ Yes (Python 3.7+) | ✅ Yes | ❌ No |
Duplicates Allowed? | ❌ No (keys must be unique) | ✅ Yes | ❌ No |
Access Method | By key (dict[key]) | By index (tuple[index]) | Cannot access by index (only iteration) |
Common Use Cases | Storing structured data (e.g., JSON, records) | Fixed data that should not change (e.g., coordinates, database records) | Unique collections (e.g., removing duplicates, set operations) |
Comparison of Dictionary, Tuple, and Set in Python
LECTURE NOTES
CP125
Try this ! Exercise 1 |
Which of the following statements is TRUE based on the characteristics of Dictionary, Tuple, and Set? A) A tuple is mutable and allows modification after creation.�B) A dictionary allows duplicate keys.�C) A set is ordered and supports indexing.�D) A dictionary allows value modification, but keys must remain unique. |
Review Activity : Comparison of Dictionary, Tuple, and Set
LECTURE NOTES
CP125
Try this ! Exercise 2 |
Which of the following statements correctly describes how elements are accessed in different data structures? A) A dictionary accesses values using keys (dict[key]).�B) A tuple accesses values using keys.�C) A set allows direct access using an index like a list.�D) A dictionary and a set both support accessing elements by index. |
Review Activity : Comparison of Dictionary, Tuple, and Set
LECTURE NOTES
CP125
Try this ! Exercise 3 |
Which of the following data structures allows duplicate elements? A) Dictionary (dict)�B) Tuple (tuple)�C) Set (set)�D) Both Dictionary and Set |
Review Activity : Comparison of Dictionary, Tuple, and Set
LECTURE NOTES
CP125
Table of Contents - Dictionaries