Module 3.2.4
Module 3.2.4
Prof. Sarju S
19 November 2024
Module 3
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON - list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Python Dictionary
Page 4
What is a Dictionary?
► A dictionary in Python is a built-in data type that allows you to store and manage data in a
structured way using key-value pairs.
► This means each value is associated with a unique key, making it easy to access, update, or
delete specific elements.
► A dictionary is defined by enclosing key-value pairs within curly braces {}, with each key-
value pair separated by a colon : and pairs separated by commas.
Syntax Example
my_dict = { student = {
"key1": "value1", "name": "Alice",
"key2": "value2", "age": 20,
"key3": "value3" "courses": ["Math", "Science"]
} }
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Characteristics of a Dictionary
► Key Uniqueness: Each key in a dictionary must be unique. If a duplicate key is added, the
latest value overwrites the previous one.
► Key Type: Keys must be immutable types (e.g., strings, numbers, tuples), but values can be
of any type.
# Defining a dictionary with keys of immutable types: string, number, and tuple
my_dict = {
"name": "Alice", # String key
42: "Answer to everything", # Integer key
(1, 2): "Point" # Tuple key
}
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Accessing Dictionary Elements
student = {
"name": "Alice",
"age": 20,
"courses": ["Math", "Science"]
}
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Adding or Updating Elements
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Removing Elements
student.pop("grade", "Key not found") # Removes 'grade' and returns its value
► Using .popitem(): Removes the last inserted key-value pair in Python 3.7+.
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Dictionary Methods
Operation Explanation Example
python student = {"name": "Alice", "age": 20}
print(student.get("name")) # Output: Alice
Retrieves value for a specified key;
.get() print(student.get("grade", "N/A"))
returns default if key is not found. # Output: N/A (since "grade" key doesn't exist)
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Dictionary Methods
Operation Explanation Example
python student = {"name": "Alice", "age": 20}
new_data = {"grade": "A", "age": 21}
Adds or updates multiple key-value pairs from student.update(new_data)
.update()
another dictionary. print(student)
# Output: {'name': 'Alice', 'age': 21, 'grade': 'A’}
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Common Dictionary Methods
Operation Explanation Example
python student = {"name": "Alice", "age": 20}
Removes all elements from the
.clear() student.clear() print(student) # Output: {}
dictionary.
*A shallow copy is a duplicate of an object, but it only copies the references to the elements in the object, not the actual objects themselves. This means
that if the original object contains mutable objects (like lists or dictionaries), both the original and the shallow copy will point to the same objects, so
changes made to mutable elements in one will be reflected in the other.
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Iterating Through a Dictionary
► You can use loops to iterate over keys, values, or both in a dictionary
► Iterating over keys
for key in student.keys():
print(key)
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Example Use Case: Counting Word Frequencies
word_count[word] = word_count.get(word, 0) + 1
•This updates the count for the current word in the word_count dictionary:
•If the word exists in the dictionary, its count is incremented by 1.
•If the word does not exist, it is added to the dictionary with a count of 1.
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Nested Dictionaries
students = {
"student1": {"name": "Alice", "age": 20},
"student2": {"name": "Bob", "age": 22}
}
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercise
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/dictionary-telephone-directory.py
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
[email protected]
Page 17 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.