0% found this document useful (0 votes)
3 views6 pages

Notes Dictionary

The document provides an overview of dictionaries in Python, detailing their characteristics, syntax for creation, and methods for accessing, adding, and deleting items. It explains how to traverse dictionaries and use built-in functions like len(), pop(), and update(). Additionally, it includes practical implementation examples for creating a dictionary to store names and phone numbers.

Uploaded by

habibikuzaka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Notes Dictionary

The document provides an overview of dictionaries in Python, detailing their characteristics, syntax for creation, and methods for accessing, adding, and deleting items. It explains how to traverse dictionaries and use built-in functions like len(), pop(), and update(). Additionally, it includes practical implementation examples for creating a dictionary to store names and phone numbers.

Uploaded by

habibikuzaka
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

M.E.

S INDIAN SCHOOL, DOHA - QATAR


Notes-8 2025- 2026

Section : BOYS/GIRLS Date : 10-11-25


Class & Div. : XI ALL DIVISIONS Subject: Computer Science
Lesson / Topic: UNIT-2 :- PROGRAMMING AND THINKING SKILLS I-
DICTIONARIES IN PYTHON
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

DICTIONARIES IN PYTHON

[Link]:
Dictionaries are mutable, unordered collections with elements in the form of a KEY:
VALUE pair that associates key to the values.
Characteristics of the Dictionary:
 A dictionary is an unordered set of key value pairs.
Dict1 = {1: ‘January’, 2: ‘February’, 3:’March’, 4:’April’}
 Unlike list, strings and tuples, dictionary is not a sequence, because it is
an unordered set of elements.
 Indexed by keys not by numbers.
 Keys must be unique. Each of the keys within a dictionary must be unique.
However two unique keys have same values.

Like lists, dictionaries are mutable.(UPDATING )

New Value

Creating A Dictionary
It is enclosed in curly braces {} and each item is separated from other item by a
comma(,).

F 061, Rev 01, dtd 10th March 2020 1|Page


Within each item, key and value are separated by a colon (:).The value can be of
any type but the keys must be of an immutable data type such as strings, numbers or
tuples.
In dictionary each pair will consider as an element.
SYNTAX: <dict_name>= {<key> :< value>, <key> :< value>…..}
e.g. Dict = {‘Subject': ‘Computer Science', 'Class': 11}, Here keys are Subject and
Class and values are Computer Science and 11 and it contains 2 elements.
Dict1 = {1: ‘January’, 2: ‘February’, 3:’March’, 4:’April’}
D2 = { } # empty dictionary
Vowels= {‘Vowel1’:’a’, ‘Vowel2’:’e’, ‘Vowel3’:’i’, ‘Vowel4’:’o’, ‘Vowel5’:’u’}
#Here you can see the keys and values are enclosed in quotes because of string type.
I. Accessing Dictionary Item
 To access the value from the dictionary, we need to use key in key: value
pair.
 In dictionary the elements ae accessed through keys not by index.
 e.g:

Keys: 1, 2, 3
Values: January, February, March

 A dictionary that takes a key and finds the corresponding values in known
ad lookup.
II. Traversing through the Dictionary
Using Python loops we can traverse through the Dictionary.
SYNTAX: for <item > in <Dictionary>:
Process each items here
e.g. 1:

OUTPUT

F 061, Rev 01, dtd 10th March 2020 2|Page


e.g:2 OUTPUT

III. Accessing keys or value simultaneously


To see all the keys in one go use the built in method keys (). <Dictionary>.keys ()
To see all the values in one go use the built in method values ().
<Dictionary>.values ()
e.g:

IV. Add item to dictionary

To add an item to the dictionary, we use square brackets [ ] for accessing and
initializing dictionary values.
e.g.

V. Add an element to an existing dictionary.


You can add element to an existing dictionary.
e.g.
OUTPUT

VI. Deleting Dictionary Elements

We can remove an item from the existing dictionary using del command or pop()
method.

F 061, Rev 01, dtd 10th March 2020 3|Page


OUTPUT

VII. ‘in’ and ‘not in’ operator in Dictionary

VIII. FUNCTIONS AND METHODS in Dictionary

1. len ( ) :- This method returns number of key-value pairs in the given dictionary.

Eg:

2. pop () :- It is used to remove a particular item in a dictionary but it also returns


the deleted
value.
e.g.

3. clear () :-It removes all items from the particular dictionary.


e.g.

4. items () :-It returns the content of dictionary as a list of key and value. The key
and value pair will be in the form of a tuple, which is not in any particular order.
e.g.

5. update ( ):- Dictionary Elements of Two dictionaries can be merged into one by
using update () method.
e.g.
F 061, Rev 01, dtd 10th March 2020 4|Page
OUTPUT

eg2:

In this example we can see that the elements in the d2 is overridden the
elements in the d1.

6. get ( ):- It is used to access dictionary elements.


e.g.

OUTPUT:

IX. PRACTICAL IMPLEMETATION OF PYTHON DICTIONARY

Write a python program to input ‘n’ names and phone numbers to store it in a
dictionary and print the phone number of a particular name.

F 061, Rev 01, dtd 10th March 2020 5|Page


OUTPUT:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

F 061, Rev 01, dtd 10th March 2020 6|Page

You might also like