དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
January 2024
Class X ICT Curriculum
January 2024
PYTHON
Key stage IV
Class X
DATA STRUCTURE - DICTIONARY
Key Concept # 6
January 2024
PYTHON
Key stage IV
Class X
Main concepts | Sub - concepts |
Dictionary |
|
Concept Overview
January 2024
PYTHON
Key stage IV
Class X
Objectives
January 2024
PYTHON
Key stage IV
Class X
DICTIONARY
ACTIVITIES
January 2024
PYTHON
Key stage IV
Class X
Understanding Dictionary
dict_name={key_1:value_1,key_2:vlaue_2,...}
Syntax
std_info={'roll_no':1,'name':'Pema','Class':10}
Example
January 2024
PYTHON
Key stage IV
Class X
Characteristics of Dictionaries
Ordered
Dictionaries are ordered, which means that the items have a defined order, and that order will not change.
Unique
Each value in a dictionary has a Key and keys in dictionaries should be unique.
Mutable
The dictionaries are changeable collections, which implies that we can add or remove items after the creation.
January 2024
PYTHON
Key stage IV
Class X
Demo 1 - Personal Digital Assistant
Write a Python program that acts as personal digital assistant, storing name, class and roll number of a student.
name - Karma, class- X, roll_number- 5
1 2 | info = {"name":"Karma","class":"X","roll_number": 5} print(info) |
Code
Output
{"name":"Karma","class":"X","roll_number": 5} |
January 2024
PYTHON
Key stage IV
Class X
Activity 1 - Information About the Car
Write a Python program to create a dictionary with the following
informations about the car. Then display the items in the dictionary in
the screen.
Brand - Ford, model - Mustang, year - 1964
Your time! ………………. Solve it
January 2024
PYTHON
Key stage IV
Class X
Adding elements into the Dictionary
1 2 3 4 | biodata = {‘name’:’Karma’,’school’:’BHSS’} print() biodata[‘DOB’]= “12/2/2023”#adding DOB to the dict. print(biodata) |
Code
dictionary_name[‘key_name’]=”value”
Syntax
Output
{‘name’:’Karma’,‘school’:’ZMSS’,’DOB’:’12/2/2023’} |
January 2024
PYTHON
Key stage IV
Class X
Accessing elements in the Dictionary
dictionary_name[‘key_name’]
Syntax
Code
Output
1 2 | std_info={'roll_no':1,'name':'Pema','Class':'X'} print(std_info['name']) |
Pema
January 2024
PYTHON
Key stage IV
Class X
Activity 1 - Creating a Dictionary
Now, check your understanding!
January 2024
PYTHON
Key stage IV
Class X
Activity 1 - Creating a Dictionary (cont.)
Additional testing>>>
January 2024
PYTHON
Key stage IV
Class X
Activity 1 - Creating a Dictionary (cont.)
Some more………………………………..
January 2024
PYTHON
Key stage IV
Class X
Dictionary Methods
Python has a set of built-in methods that can use on dictionaries. Here are some of the common dictionary methods in Python:
Sl. No | Method | Description | Syntax |
| | | |
| | | |
| | | |
1
clear()
Remove all elements from the dictionary.
dictionary.clear()
2
3
copy()
get()
Returns a copy of the dictionary.
Returns the value of the specified key.
value = dictionary.get(‘key’)
new_dict = dictionary.copy()
January 2024
PYTHON
Key stage IV
Class X
Dictionary Methods(cont.)
Sl. No | Method | Description | Syntax |
| | | |
| | | |
| | | |
4
6
5
pop()
items()
keys()
Removes the element with the specified key.
Returns a list containing the dictionary's keys.
Returns a list containing a tuple for each key value pair.
dictionary.items()
dictionary.keys()
value = dictionary.pop(‘key’)
January 2024
PYTHON
Key stage IV
Class X
Dictionary Methods(cont.)
Sl. No | Method | Description | Syntax |
| | | |
| | | |
7
8
update()
values()
Updates the dictionary with the specified key-value pairs.
Returns a list of all the values in the dictionary.
dictionary.values()
dictionary.update({‘Key’: ‘Value’})
January 2024
PYTHON
Key stage IV
Class X
Demo 2 - Dictionary Methods
Using the data given below,create dictionary data structures and practice the dictionary methods.
keys = a,b,c
values = 1,2,3
1 2 3 4 5 6 7 8 9 | my_dict = {"a": 1, "b": 2, "c": 3}#creating dictionary print("Original dictionary",my_dict) OP: Original dictionary {'a': 1, 'b': 2, 'c': 3} print(my_dict.get("a")) OP: 1 print(my_dict.items()) OP: dict_items([('a', 1), ('b', 2), ('c', 3)]) |
Code
January 2024
PYTHON
Key stage IV
Class X
1 2 3 4 5 6 7 8 9 | print(my_dict.values()) OP: dict_values([1, 2, 3]) value = my_dict.pop("a") print(value) OP: {'b': 2, 'c': 3}
keys_view = my_dict.keys() print(keys_view) |
Code
January 2024
PYTHON
Key stage IV
Class X
Demo 2 - Dictionary Methods(cont.)
10 11 12 13 14 15 16 17 18 19 20 | # Convert the view object to a list for easier display keys_list = list(keys_view) print(keys_list) values_view = my_dict.values() # Convert the view object to a list for easier display values_list = list(values_view) print(values_list) my_dict.update([("b", 3), ("c", 4)]) print(my_dict) new_dict = my_dict.copy() print("New Dictionary",new_dict) |
January 2024
PYTHON
Key stage IV
Class X
Demo 2 - Dictionary Methods(cont.)
Output
['b', 'c'] [2, 3] {'b': 3, 'c': 4} New Dictionary {'b': 3, 'c': 4} |
January 2024
PYTHON
Key stage IV
Class X
Activity 2 - Writing the Output
Tell your answerS ……………………..
Output
Write output for the Python program given below:
1 2 3 4 5 | person ={"name": "Deki", "Dzongkhag":"Haa","Village": "Bongo"} print(person['name']) print(person.get('Village')) print(person.values()) print(person) |
Code
January 2024
PYTHON
Key stage IV
Class X
Traversing through a Dictionary
Key value
Types of accessing element of dictionary
item()
methods
January 2024
PYTHON
Key stage IV
Class X
Traversing through the Key Values
color
fruit
pet
1 2 3 | my_fav ={"color":"blue","fruit": "apple", "pet": "dog"} for values in my_fav: print(values) |
Code
for key in dict_name:
print(key)
Syntax
Output
for loop iterates over the keys of the dictionary and prints each key on a new line.
January 2024
PYTHON
Key stage IV
Class X
Traversing using the items() method
color -> blue
fruit -> apple
pet -> dog
1 2 3 | my_fav={"color": "blue", "fruit": "apple", "pet": "dog"} for key,value in my_fav.items(): print(key, "->",value) |
Code
for key, value in dict_name.items():
print(key,value)
Syntax
Output
items() will access both key and value and store them in key and value respectively in each iteration.
January 2024
PYTHON
Key stage IV
Class X
Activity 3 - Storing Students Percentage
Write a Python program that ask the user total number of student and their names and percentage respectively .Display the result of each student.
Slove the problem .............................
January 2024
PYTHON
Key stage IV
Class X
Deleting a Dictionary Element
1 2 3 4 | Biodata = {‘name’:’Karma’,’school’:’BHSS’} del Biodata[‘name’]#deleting karma from the dict Biodata.pop(‘school’) print(Biodata) #{‘school’:’BHSS’} |
Code
del variable_name[key_value]#using del keyword
variable_name.pop(key_value)#using pop method
Syntax
Output
{} |
January 2024
PYTHON
Key stage IV
Class X
Activity 4 - Deleting Elements
{'Brand': 'Ford'}
The provided Python program contains some errors. Debug and rewrite the program to ensure it produces the correct output.
1 2 3 4 | car ={"brand": "Ford", "model": "Mustang", "year": 1964} car.pop("Mustang") del “year” print(car) |
Code
Output
model
car[“year”]
January 2024
PYTHON
Key stage IV
Class X
Updating a Dictionary element
{'name': 'Sangay', 'school': 'BHSS'}
The dictionary's elements can be updated by referring to a key_value or by using the update() method.
1 2 3 | Biodata = {‘name’:’Karma’,’school’:’BHSS’} Biodata[‘name’]= “Sangay”#Updating name Sangay print(biodata) |
Code
variable_name[key_Value] = value#using key_value
dict_name.update({"key": "Value"}) #using update method
Syntax
Output
January 2024
PYTHON
Key stage IV
Class X
Activity 5 - Check your Understanding
Answer the following questions.
a) Using the remove() method
b) By setting the value to None
c) With the delete() function
d) Using the del keyword
a) Dictionary keys must be unique
b) Dictionary keys can be repeated
c) Dictionary keys are case-sensitive
d) Dictionary keys can be integers
January 2024
PYTHON
Key stage IV
Class X
Activity 5 - Check your Understanding(cont.)
3. How can you check if a value is present in the values of a dictionary?
a) Using the exists() method
b) With the contains() function
c) Using the has_value() method
d) By using the in keyword with values()
4. What happens if you try to access a key that does not exist in a dictionary?
a) It returns None
b) It raises a KeyError
c)It returns an empty string
d) It appends the key to the dictionary
January 2024
PYTHON
Key stage IV
Class X
Activity 5 - Check your understanding(cont.)
5. Fill in the blanks with appropriate word(s)
key-value
comma
update()
pop()
values
January 2024
PYTHON
Key stage IV
Class X
Activity 6 - Writing the Output
Use the dictionary given below to answer the questions that follow.
1 2 3 4 5 6 | biodata = { “Name”: “Jigme Wangmo”, “Class”: “IX”, “Age”: 16 “Village”: “Bidung” } |
January 2024
PYTHON
Key stage IV
Class X
Activity 6 - Solution
Time to solve >>>> use IDLE or online IDLE compiler
January 2024
PYTHON
Key stage IV
Class X
Activity 7 - Modifying Dictionary
Study the code given below and follow the instruction to achieve the final output:
std_bio_data={“Name”:”Pelden”,”village”:”Bhur”,
”mobile_no”:17485978, “Eng”:98,”Dzo”:99,”Math”:89}
Instructions:
Homework: You will see same question in Google classroom. Solve and submit in google � classroom.
January 2024
PYTHON
Key stage IV
Class X
Key Points
January 2024
PYTHON
Key stage IV
Class X
Open Google classroom and click on Dictionary-Quiz.
Instructions:
Check your understanding
January 2024
PYTHON
Key stage IV
Class X
བཀྲིན་ཆེ།
THANK YOU
January 2024
PYTHON
Key stage IV
Class X