0% found this document useful (0 votes)
105 views14 pages

Class 11 Cs Dictionary

The document discusses dictionaries in Python. It defines dictionaries as unordered collections of key-value pairs where keys must be unique. It describes how to create, access, iterate through, update, delete elements from, and use built-in functions on dictionaries. Examples are provided for each operation like creating a dictionary with curly braces, accessing values using get() or indexing, iterating through a dictionary with a for loop, updating and deleting elements using assignment, del, pop(), and clear().

Uploaded by

Sam Kumar
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)
105 views14 pages

Class 11 Cs Dictionary

The document discusses dictionaries in Python. It defines dictionaries as unordered collections of key-value pairs where keys must be unique. It describes how to create, access, iterate through, update, delete elements from, and use built-in functions on dictionaries. Examples are provided for each operation like creating a dictionary with curly braces, accessing values using get() or indexing, iterating through a dictionary with a for loop, updating and deleting elements using assignment, del, pop(), and clear().

Uploaded by

Sam Kumar
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/ 14

RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021

DICTIONARY IN PYTHON

Computer Science
Class XI (As per CBSE Board)
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021

Dictionary
It is an unordered collection of items where each item consist of a
key and a value.
It is mutable (can modify its contents) but Key must be unique
and immutable.
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Creating a Dictionary
It is enclosed in curly braces {} and each item is separated from other item by a comma(,).
Within each item, key and value are separated by a colon (:).Passing value in dictionary at
declaration is dictionary initialization.get() method is used to access value of a key
e.g.
dict = {‘Subject': ‘Computer Science', 'Class': ‘11'}
Accessing List Item
dict = {'Subject': 'Computer Science', 'Class': 11} print(dict)
print ("Subject : ", dict['Subject'])
print ("Class : ", dict.get('Class'))
OUTPUT
{'Class': '11', 'Subject': 'Computer Science'}
('Subject : ', 'Computer Science')
('Class : ', 11)
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Iterating / Traversing through a Dictionary
Following example will show how dictionary items can be accessed through loop.
e.g.
dict = {'Subject': 'Computer Science', 'Class': 11} for i in dict:
print(dict[i])
OUTPUT
11
Computer Science
Updating/Manipulating Dictionary Elements
We can change the individual element of dictionary.e.g.
dict = {'Subject': 'Computer Science', 'Class': 11} dict['Subject']='computer science' print(dict)
OUTPUT
{'Class': 11, 'Subject': 'computer science'}
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Deleting Dictionary Elements
del, pop() and clear() statement are used to remove elements
from the dictionary.
del e.g.

dict = {'Subject': 'Computer Science', 'Class': 11} print('before del',


dict)

del dict['Class'] # delete single element print('after item


delete', dict) del dict #delete whole dictionary
print('after dictionary delete', dict)
Output
('before del', {'Class': 11, 'Subject': 'Computer Science'})
('after item delete', {'Subject': 'Computer Science'})
('after dictionary delete', <type 'dict'>)
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
pop() method is used to remove a particular item in a dictionary.
clear() method is used to remove all elements from the dictionary.
e.g.

dict = {'Subject': 'Computer Science', 'Class': 11} print('before del',


dict)
dict.pop('Class')
print('after item delete', dict)
dict.clear()
print('after clear', dict)

Output
('before del', {'Class': 11, 'Subject': 'Computer Science'})
('after item delete', {'Subject': 'Computer Science'})
('after clear', {})
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021

Built-in Dictionary Functions


RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021
Built-in Dictionary Functions
RAM CHARAN/CLASS XI/COMPUTER SCIENCE/YEAR 2021

Create a dictionary with names of employees, their salary and


access them
employees ={'Aman':{'salary':'10000'},'Mayur':{'salary':'51000'}}
employee1 = employees['Aman']
print(employee1)

OUTPUT
{'salary': '10000'}

You might also like