0% found this document useful (0 votes)
36 views9 pages

9 Dictionary9

Here are two examples to solve the questions: 1. students = {'rollno1':{'name':'John','age':20}, 'rollno2':{'name':'Mary','age':19}, 'rollno3':{'name':'Bob','age':21}, 'rollno4':{'name':'Lisa','age':18}} rollno = input("Enter rollno to search: ") print(students[rollno]) 2. months = {'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30, 'Oct':31, '

Uploaded by

jayrawal2392007
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)
36 views9 pages

9 Dictionary9

Here are two examples to solve the questions: 1. students = {'rollno1':{'name':'John','age':20}, 'rollno2':{'name':'Mary','age':19}, 'rollno3':{'name':'Bob','age':21}, 'rollno4':{'name':'Lisa','age':18}} rollno = input("Enter rollno to search: ") print(students[rollno]) 2. months = {'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30, 'Oct':31, '

Uploaded by

jayrawal2392007
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/ 9

New

syllabus
2022-23

Chapter 9
Dictionary

Aashif Khan 9928215554


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.
Dictionary
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.
e.g.
dict = {‘Subject': ‘Informatic Practices', 'Class': ‘11'}

Accessing List Item


dict = {'Subject': 'Informatics Practices', 'Class': 11}
print(dict)
print ("Subject : ", dict['Subject'])
print ("Class : ", dict.get('Class'))
OUTPUT
{'Class': '11', 'Subject': 'Informatics Practices'}
('Subject : ', 'Informatics Practices')
('Class : ', 11)
Dictionary
Iterating / Traversing through A Dictionary
Following example will show how dictionary items can be accessed
through loop.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
for i in dict:
print(dict[i])
OUTPUT
11
Informatics Practices
Updating/Manipulating Dictionary Elements
We can change the individual element of dictionary.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
dict['Subject']='computer science'
print(dict)
OUTPUT
{'Class': 11, 'Subject': 'computer science'}
Dictionary
Deleting Dictionary Elements
del, pop() and clear() statement are used to
remove elements from the dictionary.
del e.g.
dict = {'Subject': 'Informatics Practices', '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': 'Informatics Practices'})
('after item delete', {'Subject': 'Informatics Practices'})
('after dictionary delete', <type 'dict'>)
Dictionary
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': 'Informatics Practices', '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': 'Informatics Practices'})
('after item delete', {'Subject': 'Informatics Practices'})
('after clear', {})
Dictionary
Built-in Dictionary Functions

S.No. Function & Description


len(dict)Gives the total length of the dictionary. It is equal
to the number of items in the dictionary.
1

str(dict)Return a printable string representation of a


2 dictionary

type(variable)If variable is dictionary, then it would return


a dictionary type.
3
Dictionary
Built-in Dictionary Methods
S.No. Method & Description
1 dict.clear()Removes all elements of dictionary dict
2 dict.copy()Returns a shallow copy of dictionary dict
3 dict.items()Returns a list of dict's (key, value) tuple pairs
4 dict.keys()Returns list of dictionary dict's keys
dict.setdefault(key, default = None)Similar to get(), but will set
5
dict[key] = default if key is not already in dict
6 dict.update(dict2)Adds dictionary dict2's key-values pairs to dict
7 dict.values()Returns list of dictionary dict's values
Dictionary

Questions.
1. Create dictionary to store 4 student details
with rollno,name,age field.Search student in
list.
2. Create dictionary for month and noofdays for
a year. User is asked to enter month name
and system will show no of days of that
month.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy