Computer Science
As Per Latest
CBSE
Syllabus
By
Karthiga Prabhakaran
PGT-CS
Introduction to Tuple
Introduction to Dictionary
• A dictionary in Python is an unordered collection of key-value pairs.
• It is mutable, which means you can change its content (add, modify, delete).
• Keys must be unique and immutable (like strings, numbers, tuples).
• Values can be of any data type and can be duplicated.
Syntax:
dict_name = {key1: value1, key2: value2, ...}
#Example
student = {"name": "Raj", "class": 11, "marks": 95}
Introduction to Tuple
Accessing Items in a Dictionary
• You can access values in a dictionary by referring to their key names.
#Example
student = {"name": "Raj", "marks": 95}
print(student["name"]) # Output: Raj
If the key does not exist, using [] will raise a KeyError. Use get() to avoid this.
#Example
print([Link]("age")) # Output: None (no error)
Mutability of Dictionaries
Mutability of Dictionaries
Adding a New Item
#Example
student["age"] = 16
Modifying an Existing Item
#Example
student["marks"] = 98
Traversing a Dictionary
• Traversing means accessing each key-value pair in the dictionary to display, process, or modify data.
Method 1: for key in dict: Accesses keys; values are fetched using dict[key]
student = {"name": "Ravi", "class": 11, "marks": 88}
for key in student:
print(key, "->", student[key])
Method 2: for key, value in [Link](): Directly accesses both key and value.
for k, v in [Link]():
print(k, ":", v)
Method 3: Using keys() and values()
for k in [Link]():
print(k)
for v in [Link]():
print(v)
Dictionary Built-in Functions
Function Purpose Example Output
Returns number of key-
len() len(d) 3
value pairs
Creates dictionary from
dict() dict(a=1, b=2) {'a': 1, 'b': 2}
pairs or keywords
keys() Returns all keys [Link]() dict_keys([...])
values() Returns all values [Link]() dict_values([...])
items() Returns key-value pairs [Link]() dict_items([...])
Returns value for given
get(key) [Link]('name') 'Ravi'
key (None if absent)
Adds/updates one or
update() [Link]({"age": 16}) Updates or adds "age": 16
more key-value pairs
Dictionary Built-in Functions
Function Purpose Example Output
del Deletes key-value pair del d["name"] Removes "name" key
clear() Removes all items from dictionary [Link]() {}
Creates new dictionary from
fromkeys() [Link](["a", "b"], 0) {'a': 0, 'b': 0}
sequence of keys
Returns a shallow copy of Same content, different
copy() [Link]()
dictionary object
pop(key) Removes & returns value for key [Link]("age") Value of "age"
Removes & returns last inserted
popitem() [Link]() ('marks', 85)
item
Returns value of key, sets it if not
setdefault() [Link]("grade", "A") "A" (if not present)
present
sorted(d) Returns sorted list of keys sorted(d) ['class', 'marks', 'name']
Suggested Programs Using
Dictionary
# Employee Dictionary
employees = {
"John": 50000,
"Amit": 60000,
"Sneha": 70000
}
print("Salary of Amit:", employees["Amit"])
# Modifying Salary: # Adding a New Employee
employees["John"] = 55000 employees["Ravi"] = 65000