Kendriya Vidyalaya O.N.G.C Mehsana

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

KENDRIYA VIDYALAYA

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.

GRADES DICTIONARY GRADES LIST


KEYS VALUES INDEX VALUES
Mohan A 0 A
Soham A+ 1 A+
Emily B 2 B
Mikey C 3 C
John A 4 A
• Using a dictionary allows us to access the grade of each student by
providing the name of the student (key). On the other hand, to be able
to access the grade of a particular student, we need an additional list.
• The new list contains the names of the students and has the exact same
order as the grades list.

NAMES LIST GRADES LIST


KEYS VALUES INDEX VALUES
0 Mohan 0 A
1 Soham 1 A+
2 Emily 2 B
3 Mikey 3 C
4 John 4 A
• Thus, a dictionary is a better choice than a list for such cases.
• After this short introduction, let’s start on examples to dive deep into
dictionaries. The examples will cover the features of dictionaries as
well as the functions and methods to operate on them.
CREATING A DICTIONARY
• In Python, a dictionary can be created by placing a sequence of elements
within curly {} braces, separated by ‘comma’. Dictionary holds pairs of
values, one being the Key and the other corresponding pair element being
its Key:Value.
• Values in a dictionary can be of any data type and can be duplicated,
whereas keys can’t be repeated and must be immutable.
• Note – Dictionary keys are case sensitive, the same name but different
cases of Key will be treated distinctly. 
• Example:-
empty_dict = {}

grades = {‘Mohan':'A', ‘Soham':'A+', ‘Emily':'B', 'Mikey':'C', ‘John':'A'}


ADDING ELEMENTS TO A
DICTIONARY
• Addition of elements can be done in multiple ways. One value at a time
can be added to a Dictionary by defining value along with the key e.g.
Dict[Key] = ‘Value’.
• Updating an existing value in a Dictionary can be done by using the built-
in update() method. Nested key values can also be added to an existing
Dictionary.
• Note- While adding a value, if the key-value already exists, the value gets
updated otherwise a new Key with the value is added to the Dictionary.
• Example:-
Dict = {}
print("Empty Dictionary: ") OUTPUT
print(Dict) Empty Dictionary: {}
# Adding elements one at a time
Dict[0] = ‘Hello'
Dict[2] = ‘World'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
OUTPUT
print(Dict)
Dictionary after adding 3 elements: {0: ‘Hello', 2: ‘World', 3: 1}
ACCESSING ELEMENTS OF A
DICTIONARY
• In order to access the items of a dictionary refer to its key name. Key can be
used inside square brackets.
• Example:- 
Dict = {1: ‘Hello', 'name': ‘World', 3: ‘Python'}
# accessing a element using key
print("Accessing a element using key:") OUTPUT
print(Dict['name']) Accessing a element using key: World
• There is also a method called get() that will also help in accessing the
element from a dictionary.This method accepts key as argument and
returns the value.
• Example:-
Dict = {1: ‘Hello', 'name': ‘World', 3: ‘Python'}
# accessing a element using get()
print("Accessing a element using get:")
print(Dict.get(3)) OUTPUT
Accessing a element using get: Python
  ADDING AN ITEM
• Dictionaries are mutable so we can update, add items. The syntax for
adding an item is the same. If the given key exists in the dictionary,
the value of the existing item is updated. Otherwise, a new item (i.e.
key-value pair) is created.
• Example:-
my_dictionary = { "one": 1, "two": 2 }
print("Before:", my_dictionary)
my_dictionary["three"] = 3
print("After:", my_dictionary)

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

You might also like