0% found this document useful (0 votes)
9 views

Dictionaries

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Dictionaries

Uploaded by

trishasag7604
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

2) Dictionary Data type

Description:A dictionary is an unordered set of key-value pair. Each key is separated from its value by a
colon (:), and consecutive items are separated by commas. The entire items in a dictionary are enclosed
in curly brackets ({ }).

1) Demonstrate the different ways of creating Dictionary objects with suitable example programs.

i) Creation of dict object with single value:

d = {'PYTHON PROGRAMMING':99}
print(type(d))
print(d)

Ouptput: <class 'dict'>

{'PYTHON PROGRAMMING': 99}

ii). Creation of dict object with multiple values:

d = {'Happiness':99,'depends':100,'upon ourselves':98}
print(type(d))
print(d)
Output: <class 'dict'>

{'Happiness': 99, 'depends': 100, 'upon ourselves': 98}

iii). Creation of set objects using dict() function:

We can create dict objects by using dict() function.

d = dict()
print(type(d))

Output: <class 'dict'>

d=eval(input("Enter Dictionay:"))
print(d)
print(type(d))
Output:
Enter Dictionay:{'a':100,'b':200,'c':300}
{'a': 100, 'b': 200, 'c': 300}
<class 'dict'>
2) Demonstrate the following functions/methods which operates on dictionary in Python with suitable
examples: i) len() ii)clear() iii) get() iv) pop() v)popitem() vi)keys() vii)values() viii)items() xi)copy()
x)update()

i)len(): It returns the number of items in the dictionary

d=dict({100:"India",200:"Delhi"}) #It creates dictionary with speci


fied elements
print(d)
print(len(d))
Output: {100: 'India', 200: 'Delhi'}

2
ii) clear():
This function is used to remove all entries from the dictionary.
d={100:"India",200:"capital",300:"Delhi"}
print(d)
d.clear()
print(d)
Output: {100: 'India', 200: 'capital', 300: 'Delhi'}

{}
iii) get(): It is used tTo get the value associated with the specified key. There are two forms of get()
method is available in Python.
a) d.get(key): If the key is available then returns the corresponding value otherwise returns None.It wont
raise any error.
d=dict({100:"India",200:"Delhi"}) #It creates dictionary with speci
fied elements
print(d.get(100))

Output: India

d=dict({100:"karthi",200:"saha"}) #It creates dictionary with speci


fied elements
print(d.get(500))
Output: None

b)d.get(key,defaultvalue): If the key is available then returns the corresponding value otherwise returns
default value.

d=dict({100:"India",200:"Delhi"}) #It creates dictionary with speci


fied elements
print(d.get(200,'New Delhi'))
Output: Delhi

iv) pop():It removes the entry associated with the specified key and returns the corresponding value.

If the specified key is not available then we will get KeyError.

d.pop(key)

d={100:"India",200:"Delhi",300:"Mumbai"}
print(d)
print(d.pop(100))
print(d)
print(d.pop(200))
print(d)
Output:

{100: 'India', 200: 'Delhi', 300: 'Mumbai'}

India
{200: 'Delhi', 300: 'Mumbai'}
Delhi
{300: 'Mumbai'}

v) popitem(): It removes an arbitrary item(key-value) from the dictionaty and returns it.

d={100:"karthi",200:"saha",300:"sri"}
print(d)
print(d.popitem())
print(d.popitem())
print(d)
Output:

{100: 'karthi', 200: 'saha', 300: 'sri'}


(300, 'sri')
(200, 'saha')
{100: 'karthi'}

vi) keys(): It returns all keys associated with dictionary

d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
print(d.keys())
for key in d.keys():
print(key)

Output: dict_keys([100, 200, 300])


100
200
300

vii) values(): It returns all values associated with the dictionary.

d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
print(d.values())
for key in d.values():
print(key)

Output: dict_values(['Hyderabad', 'Mumbai', 'Delhi'])

Hyderabad
Mumbai
Delhi

viii)items(): It returns list of tuples representing key-value pairs like as shown below.

[(k,v),(k,v),(k,v)]

d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
list = d.items()
print(list)

Output: dict_items([(100, 'Hyderabad'), (200, 'Mumbai'), (300,


'Delhi')])

ix)copy(): This method is used to create exactly duplicate dictionary(cloned copy).

d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
d1=d.copy()
print(d1)
print(d)
Output: {100: 'Hyderabad', 200: 'Mumbai', 300: 'Delhi'}

{100: 'Hyderabad', 200: 'Mumbai', 300: 'Delhi'}

x)update():

d.update(x)

All items present in the dictionary 'x' will be added to dictionary 'd

d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
d1 ={'a':'pearls', 'b':'corals'}
d.update(d1)
print(d)
Output :
{100: 'Hyderabad', 200: 'Mumbai', 300: 'Delhi', 'a': 'pearls', 'b':
'corals'}

3) Write a Python program to find number of occurrences of each letter present in the given string
word=input("Enter any word: ")
d={}
for x in word:
d[x]=d.get(x,0)+1
for k,v in d.items():
print(k,"occurred ",v," times")
Output: Enter any word: mississippi

m occurred 1 times
i occurred 4 times
s occurred 4 times
p occurred 2 times
4) Write a Python program to find number of occurrences of each vowel present in the given string.
word=input("Enter any word: ")
vowels={'a','e','i','o','u'}
d={}
for x in word:
if x in vowels:
d[x]=d.get(x,0)+1
for k,v in sorted(d.items()):
print(k,"occurred ",v," times")
Output:
Enter any word: Facetious
a occurred 1 times
e occurred 1 times
i occurred 1 times
o occurred 1 times
u occurred 1 times
5) Write a program to accept student name and marks from the keyboard and creates a dictionary. Also
display student marks by taking student name as input.
n=int(input("Enter the number of students: "))
d={}
for i in range(n):
name=input("Enter Student Name: ")
marks=input("Enter Student Marks: ")
d[name]=marks # assigninng values to the keys of the dictionary 'd
'
while True:
name=input("Enter Student Name to get Marks: ")
marks=d.get(name,-1)
if marks== -1:
print("Student Not Found")
else:
print("The Marks of",name,"are",marks)
option=input("Do you want to find another student marks[Yes|No]")
if option=="No":
break
print("Thanks for using our application")
for x in d:
print("\t",x,"\t",d[x])# display information on the screen.

Output: Enter the number of students: 3

Enter Student Name: Alice


Enter Student Marks: 82
Enter Student Name: Bob
Enter Student Marks: 74
Enter Student Name: Chocolate
Enter Student Marks: 90
Enter Student Name to get Marks: Alice
The Marks of Alice are 82
Do you want to find another student marks[Yes|No]y
Enter Student Name to get Marks: Chocolate
The Marks of Chocolate are 90
Do you want to find another student marks[Yes|No]No
Thanks for using our application
Name of Student % of Marks
Alice 82
Bob 74
Chocolate 90

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