0% found this document useful (0 votes)
8 views16 pages

12cs-Python Revision Tour-Ii-Dictionary

The document is a chapter on Python dictionaries for a Computer Science course, covering key concepts such as creation, accessing elements, and various operations like adding, updating, and deleting elements. It explains the structure of dictionaries, including key-value pairs and methods for manipulating them. Additionally, it includes practical exercises for students to apply their understanding of dictionaries.

Uploaded by

Zilmil Singh
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)
8 views16 pages

12cs-Python Revision Tour-Ii-Dictionary

The document is a chapter on Python dictionaries for a Computer Science course, covering key concepts such as creation, accessing elements, and various operations like adding, updating, and deleting elements. It explains the structure of dictionaries, including key-value pairs and methods for manipulating them. Additionally, it includes practical exercises for students to apply their understanding of dictionaries.

Uploaded by

Zilmil Singh
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
You are on page 1/ 16

COMPUTER SCIENCE

SUBJECT : COMPUTER SCIENCE WITH PYTHON (083)

STANDARD : 12 ACADEMIC YEAR : 21 – 22

CHAPTER : PYTHON REVISION TOUR-II

S. SHUNMUGA SUNDARAM M.E/CSE


CHAPTER COVERS
•String Object
•List Object
•Tuple Object
•Dictionary Object

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


DICTIONARY
• Introduction
• Creating a dictionary
• Accessing elements of a dictionary
• Dictionary Operations
• Traversing a Dictionary
• Adding Elements to Dictionary
• Updating Elements in a dictionary
• Deleting Elements from a Dictionary
• Checking for Existence of a Key
• Dictionary Functions and Methods
S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG
DICTIONARY
• Unordered collection of elements in the form of
Key:Value pairs
• Enclosed with { } – braces (curly bracket)
• Keys are immutable and Values are mutable
• The Keys within a dictionary must be unique
• Data type of dictionary falls under mapping
• Items are separated with , (comma) operator
• Items in a dictionary accessed with their keys not using
index
• Dictionaries are indexed by KEYS
S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG
Creation of a Dictionary
Syntax
<Dict_Name>={<K1>:<V1>, <K2>:<V2>, <K3>:<V3>,…}

Where,
• K1,K2,K3 - Unique keys
• V1,V2,V3 - Values
Example:
D={1:’Apple’,2:’Orange’,3:’Mango’}
D1={‘CS’:’Ram’,’MAT’:’John’,’ENG’:’Om’}
S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG
Accessing a Dictionary
Syntax
<Dict_Name>[<KEY>]
Example:

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Accessing a Dictionary
Example:

Accessing KEYS : <Dict_Name>.keys()

Accessing VALUES : <Dict_Name>.values()

Accessing ITEMS : <Dict_Name>.items()

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
• Traversing
• Adding
• Updating
• Deleting
• Searching

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
Traversing a Dictionary
• Accessing and processing each element

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
Adding / Updating a Dictionary
• KEY is not Exist :
• Adding an element to the existing dictionary
<Dict_Name>[<key>]=<value>
• KEY is Exist :
• Updating a value of an existing key
<Dict_Name>[<key>]=<value>

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
Deleting an element from a Dictionary
• del command
• pop() function
• Method 1 : Using del command
del <Dict_Name>[key]
• Key is exist , it will remove the item “key:value”
pair from the Dictionary
• Key is not exist, it will show “KeyError:key”

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
Deleting an element from a Dictionary
• del command
• pop() function
• Method 2 : Using pop() function
<Dict_Name>.pop(<key>,[”Not Found”])
• Key is exist , it will remove the item “key:value”
pair from the Dictionary
• Key is not exist, it will show “KeyError:key” or user defined message

User Defined Error Message

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Operations
Searching an element from a Dictionary
• Using membership operator like in and not in
• The in operator will return True if the KEY exist otherwise False
• The not in operator will return True if the KEY is not exist otherwise False

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Functions
Methods and Functions of a Dictionary
• len() - Returns number of elements in the dictionary
• clear() - Remove all the items from the dictionary
• get() - Get the item with the given key. Key is not exist then
it will return null or to display user defined message
• Items() - Display all the items in the dictionary
• keys() - Display all the keys in the dictionary
• values() - Display all the values in the dictionary
• update() - Merges Key:Value pairs from the new dictionary into
the existing dictionary (adding or replacing as needed)

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Dictionary Functions
Methods and Functions of a Dictionary
• len()
• clear()
• get()
• Items()
• keys()
• values()
• update()

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG


Do It Yourself:
Write a program to check for presence of a value inside a dictionary and prints its key

S. SHUNMUGA SUNDARAM, M.E/CSE, A.M.I.E, M.I.S.T.E, M.IAENG

You might also like