Kendriya Vidyalaya O.N.G.C Mehsana
Kendriya Vidyalaya O.N.G.C Mehsana
Kendriya Vidyalaya O.N.G.C Mehsana
O.N.G.C MEHSANA
NAME:- Ansh Agarwal
Class:- XI A
Roll No.:- 20
Subject:- Computer Science
Project Name :- Dictionary
Project Teacher’s Name:- Namrata Mam
INTRODUCTION TO
DICTIONARY
• Dictionary is an unordered collection of key-value pairs. Each entry
has a key and value.
• A dictionary can be considered as a list with special index.
• The keys must be unique and immutable. So we can use strings,
numbers (int or float), or tuples as keys. Values can be of any type.
EXAMPLE
• Consider a case where we need to store grades of students. We can
either store them in a dictionary or a list.
OUTPUT
Before: {‘one’: 1, ‘two’: 2}
After: {‘one’: 1, ‘two’: 2, ‘three’: 3}
DELETING AN ITEM
•We can use the del or pop function to delete an item. We just pass the key of
the item to be deleted.
•Unlike the del function, the pop function returns the value of the deleted item.
Thus, we have the option to assign it to a variable.
•The del keyword method uses the keyword from the dictionary to remove an
item.
•The clear() method clears all the items in the dictionary.
•The pop() method uses the keyword from the given dictionary to remove an
item from that dictionary.
•The popitem() removes the last item added to the dictionary.
THE DEL KEYWORD
• Example:-
countries={"Ghana": "Accra", "China": "Beijing"}
del countries["China"]
print(countries)
OUTPUT
{'Ghana': 'Accra'}
THE CLEAR() METHOD
• Example:-
countries={"Ghana": "Accra", "China": "Beijing"}
countries.clear()
print(countries)
OUTPUT
{}
THE POP() METHOD
• Example:-
countries={"Ghana": "Accra", "China": "Beijing"}
countries.pop("China")
print(countries)
OUTPUT
{'Ghana': 'Accra'}
THE POPITEM() METHOD
• Example:-
countries={"Ghana": "Accra", "China": "Beijing"}
countries.popitem()
print(countries)
OUTPUT
{'Ghana': 'Accra'}
FINDING LENGTH OF
DICTIONARY
• The len() function returns the number of items in a dictionary (i.e. the
length).
• Example:-
dict_leng = {"Alex":10,"Peter":40,"Harry":60}
print("Length of dictionary is",len(dict_leng))
OUTPUT
3
COPYING A DICTIONARY
• Python Dictionary copy() method returns a shallow copy of the
dictionary.
• Example:-
original = {1:‘Hello', 2:‘World'}
new = original.copy() // Operation
OUTPUT
original: {1: 'one', 2: 'two'}
new: {1: 'one', 2: 'two'}
THANK YOU