1 of 38

དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།

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

2 of 38

DATA STRUCTURE - DICTIONARY

Key Concept # 6

January 2024

PYTHON

Key stage IV

Class X

3 of 38

Main concepts

Sub - concepts

Dictionary

  • Introduction to Dictionary
    • Definition
    • Characteristics
    • Creating a dictionary
  • Accessing elements of Dictionary
  • Traversing the elements of Dictionary
  • Dictionary methods
  • clear(), copy(), pop(), get(), items(), update(), values()

Concept Overview

January 2024

PYTHON

Key stage IV

Class X

4 of 38

  • Explain dictionary in Python program.
  • Describe the key characteristics of dictionary.
  • Create, store and manipulate items in dictionary.
  • Iterate over dictionary items using for loop.
  • Create Python programs that solve a real-world problem using dictionaries.

Objectives

January 2024

PYTHON

Key stage IV

Class X

5 of 38

DICTIONARY

ACTIVITIES

  1. Understanding Dictionary
  2. Demo 1 -Personal Digital Assistant
  3. Activity 1 - Information About the Car
  4. Demo 2 - Dictionary Methods
  5. Activity 2 - Writing the Output
  6. Activity 3 - Storing Students Percentage
  7. Activity 4 - Deleting Elements
  8. Activity 5 - Check your Understanding
  9. Activity 6 - Writing the Output
  10. Activity 7 - Modifying the Dictionary

January 2024

PYTHON

Key stage IV

Class X

6 of 38

Understanding Dictionary

  • A dictionary is a collection of data that stores in key-value pairs.
  • It allow us to associate a value to a unique key, and then to quickly access this value.
  • It is ordered, changeable and do not allow duplicate key.
  • It is created using {} or dict() function.

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

7 of 38

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

8 of 38

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

9 of 38

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

10 of 38

Adding elements into the Dictionary

  • A new dict element can be added by inserting a new key value.
  • The new key value should be unique. Otherwise, old value will be replaced.

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

11 of 38

Accessing elements in the Dictionary

  • In dictionary, items can be accessed by referring to its key name, inside square brackets:

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

12 of 38

Activity 1 - Creating a Dictionary

  1. Write a Python program to create a dictionary with your progress report such as Name, Class,Percentage and rank. Then display the result.

Now, check your understanding!

January 2024

PYTHON

Key stage IV

Class X

13 of 38

Activity 1 - Creating a Dictionary (cont.)

  1. Add new item SUPW grade in the dictionary created in the above question 1.

Additional testing>>>

January 2024

PYTHON

Key stage IV

Class X

14 of 38

Activity 1 - Creating a Dictionary (cont.)

  1. Display only the name and percentage of the student from the dictionary created in the previous question 1.

Some more………………………………..

January 2024

PYTHON

Key stage IV

Class X

15 of 38

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

16 of 38

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

17 of 38

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

18 of 38

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

19 of 38

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

20 of 38

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

21 of 38

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

22 of 38

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

23 of 38

Traversing through a Dictionary

  • Traversing the elements of the dictionary is the systematic process of moving through each item to perform operation on key-value pair.
  • key-value can be traversed using both for and while loops.

Key value

Types of accessing element of dictionary

item()

methods

January 2024

PYTHON

Key stage IV

Class X

24 of 38

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

25 of 38

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

26 of 38

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

27 of 38

Deleting a Dictionary Element

  • The dictionary's elements can be deleted by referring to a key value using the del keyword or by using the pop method.
  • The pop() method removes the item with the specified key name.

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

28 of 38

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

29 of 38

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

30 of 38

Activity 5 - Check your Understanding

Answer the following questions.

  1. How do you remove a key-value pair from a dictionary?

a) Using the remove() method

b) By setting the value to None

c) With the delete() function

d) Using the del keyword

  1. Which of the following statements is Not true about dictionary keys?

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

31 of 38

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

32 of 38

Activity 5 - Check your understanding(cont.)

5. Fill in the blanks with appropriate word(s)

  1. A Python dictionary is a collection of __________pairs.
  2. In Python, a dictionary is created using curly braces, and each key-value pair is separated by a ________.
  3. The _________ method is used to add or update key-value pairs in a dictionary.
  4. To remove a key-value pair from a dictionary, you can use the del keyword or the ______ method.
  5. The _______ method returns a list of all values in a dictionary.

key-value

comma

update()

pop()

values

January 2024

PYTHON

Key stage IV

Class X

33 of 38

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”

}

  1. Write a python code that prints the value of the key Age.
  2. Write a python code that prints the number of items in the dictionary.
  3. Write a python code that prints a list containing the dictionary's keys.

January 2024

PYTHON

Key stage IV

Class X

34 of 38

Activity 6 - Solution

Time to solve >>>> use IDLE or online IDLE compiler

January 2024

PYTHON

Key stage IV

Class X

35 of 38

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:

  1. Change the Name value to "Tshewang".
  2. Modify the village name to "Gelephu".
  3. Remove the mobile number entry.
  4. Add a new entry with the key "school" and value "Tashigang MSS".
  5. Calculate the total marks of the student.
  6. Display all keys and values using a for loop, including the total marks.

Homework: You will see same question in Google classroom. Solve and submit in google � classroom.

January 2024

PYTHON

Key stage IV

Class X

36 of 38

  • A dictionary stores data in key-value pairs, allowing efficient and fast access to values using unique keys.
  • Dictionaries are defined using curly braces {} and consist of key-value pairs separated by commas.
  • Similar to lists, dictionaries support operations like insertion, removal, updating, and access using various functions and methods.
  • Accessing values in a dictionary is done using keys. Keys act as unique identifiers for each value.
  • Functions and methods such as clear(), copy(), pop(), get(), items(), update(), values() can be used to manipulate dictionary elements.

Key Points

January 2024

PYTHON

Key stage IV

Class X

37 of 38

Open Google classroom and click on Dictionary-Quiz.

Instructions:

  • You can attempt only once.
  • You will be given 10 minutes only to complete the Quiz.
  • After completing don’t forget to turn-in.

Check your understanding

January 2024

PYTHON

Key stage IV

Class X

38 of 38

བཀྲིན་ཆེ།

THANK YOU

January 2024

PYTHON

Key stage IV

Class X