1 of 30

LECTURE NOTES

Learning Outcomes:-

(a) Identify concept of tuples, sets and dictionaries

CP125

Topic 3: Tuples, Sets and Dictionaries

2 of 30

LECTURE NOTES

  • Definition - recap
  • CRUD Operation - Updating
  • CRUD Operation - Deleting
  • Other Operation – Checking Key Existence
  • Comparison Dictionary with Tuple and Set

CP125

Table of Contents - Dictionaries

3 of 30

LECTURE NOTES

CP125

DICTIONARY

Definition of Dictionary:-

A collection of key and value pairs enclosed in curly brackets.

  • These pairs are sometimes called entries.
  • The entire sequence of entries is enclosed in curly braces ({ and }).
  • A colon (:) separates a key and its value.
  • Each dictionary entry consists of a key (identifier) and its corresponding value.

dictionary_name = {

"key1": "value1",

"key2": "value2",

"key3": "value3"

}

Syntax 🡪

- Recap

4 of 30

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

  • Each key must be unique and immutable (e.g., strings, numbers, or tuples), while the values can be of any data type.

Dictionary_name

key

value

key

value

- Recap

5 of 30

LECTURE NOTES

CP125

CRUD Operations

UPDATE (U) – Modifying Existing Elements in a Dictionary

  • Updating a Single Value

DICTIONARY

Syntax

dictionary_name[key] = new_value # Updating a single value

Example

6 of 30

LECTURE NOTES

CP125

CRUD Operations

UPDATE (U) – Modifying Existing Elements in a Dictionary

  • Adding a new key-value pair

DICTIONARY

Syntax

dictionary_name[key] = value

# Adding a new key-value

Example

7 of 30

LECTURE NOTES

CP125

CRUD Operations

UPDATE (U) – Modifying Existing Elements in a Dictionary

  • Using update() to Modify Multiple Values

DICTIONARY

Syntax

dictionary_name.update({key1: new_value1, key2: new_value2})

​

  • This updates an existing key’s value or adds a new key-value pair.
  • It can also update multiple values at once.

​

8 of 30

LECTURE NOTES

CP125

CRUD Operations

UPDATE (U) – Modifying Existing Elements in a Dictionary

  • Using update() to Modify Multiple Values

DICTIONARY

Example

9 of 30

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

10 of 30

LECTURE NOTES

CP125

Try this ! Exercise 2

​

Which of the following correctly adds multiple key-value pairs to a dictionary?

  1. student["age", "grade"] = 21, "A“
  2. student.update({"age": 21, "grade": "A"})
  3. student.add("age", 21)

student.add("grade", "A")

  1. student["age": 21, "grade": "A"]

DICTIONARY

Review Activity : Modifying Existing Elements in a Dictionary

11 of 30

LECTURE NOTES

CP125

Try this ! Exercise 3

​

You have the following dictionary representing a book:

book = { "title": "Python Basics", "author": "John Doe", "price": 50 }

​

  1. Update the price of the book to 55.
  2. Print the updated dictionary.

DICTIONARY

Review Activity : Modifying Existing Elements in a Dictionary

12 of 30

LECTURE NOTES

CP125

Try this ! Exercise 4

​

Given the dictionary below:

employee = {"name": "Aisha","department": "IT","salary": 5000}

​

  1. Add a new key "position" with the value "Software Engineer".
  2. Print the updated dictionary.

​

DICTIONARY

Review Activity : Modifying Existing Elements in a Dictionary

13 of 30

LECTURE NOTES

CP125

Try this ! Exercise 5

​

You have the following dictionary representing a student:

student = {"name": "Ali","age": 20,"course": "Mathematics“ }

​

  1. Update the "age" to 21 and the "course" to "Computer Science" in one operation.
  2. Print the updated dictionary.

​

DICTIONARY

Review Activity : Modifying Existing Elements in a Dictionary

14 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using del to Remove a Specific Key

​

​

​

​

​

​

​

​

​

​

​

DICTIONARY

Syntax

del dictionary_name[key] # Deletes a specific key

  • Removes the specified key and its corresponding value.
  • Raises a KeyError if the key does not exist.

​

.

15 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using del to Remove a Specific Key

​

​

​

​

​

​

​

​

​

​

**'grade' key is removed

​

DICTIONARY

Example

.

16 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using pop( ) to Remove a Key and Return Its Value

DICTIONARY

Syntax

​

removed_value = dictionary_name.pop(key, default_value)

​

  • Removes the key and returns its value.
  • If the key does not exist, it returns default_value (if provided); otherwise, it raises a KeyError.

17 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using pop( ) to Remove a Key and Return Its Value

DICTIONARY

Example

​

18 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using clear( ) to Remove All Elements

DICTIONARY

Syntax

dictionary_name.clear() # Removes all elements

  • Removes all key-value pairs from the dictionary, making it an empty dictionary { }.

19 of 30

LECTURE NOTES

CP125

CRUD Operations

DELETE (D) – Removing Elements from a Dictionary

  • Using clear( ) to Remove All Elements

DICTIONARY

Example

​

20 of 30

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

21 of 30

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

22 of 30

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

23 of 30

LECTURE NOTES

CP125

Other Operations

Checking Key Existence

DICTIONARY

Syntax

​

if key in dictionary_name:

print("Key exists!")

  • Checks if the key is present in the dictionary.
  • Returns True if the key exists, otherwise False.

​

24 of 30

LECTURE NOTES

CP125

Other Operations

Checking Key Existence

DICTIONARY

Example

25 of 30

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

​

26 of 30

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

27 of 30

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

28 of 30

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

29 of 30

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

30 of 30

LECTURE NOTES

  • Definition - recap
  • CRUD Operation - Updating
  • CRUD Operation - Deleting
  • Other Operation – Checking Key Existence
  • Comparison Dictionary with Tuple and Set

CP125

Table of Contents - Dictionaries