0% found this document useful (0 votes)
4 views5 pages

Xia2 CH12 Lect2

The document discusses the concept of nested dictionaries in Python, providing examples of how to create and manipulate them. It includes several programs demonstrating the use of dictionaries to store employee information, student roll numbers, and marks, along with functionalities like adding, deleting, and displaying elements. The document illustrates the practical application of dictionaries through various coding examples and outputs.

Uploaded by

gokubleachnigoi
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)
4 views5 pages

Xia2 CH12 Lect2

The document discusses the concept of nested dictionaries in Python, providing examples of how to create and manipulate them. It includes several programs demonstrating the use of dictionaries to store employee information, student roll numbers, and marks, along with functionalities like adding, deleting, and displaying elements. The document illustrates the practical application of dictionaries through various coding examples and outputs.

Uploaded by

gokubleachnigoi
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/ 5

Chapter 12: Dictionaries

Lecture: 2
Nesting Dictionaries:
We can add dictionaries as Values inside a dictionary. Storing a dictionary inside another
dictionary is called nesting of dictionaries.

PRG 4:
Program demonstrates the use of nested dictionary.
print('='*52)
Emp ={'Ram Prakash' : {'age': 25, 'salary':30000},
'Divya': {'age': 23, 'salary':20000}}
for KEY in Emp:
print('Employee', KEY, ':')
print('Age:', str(Emp[KEY]['age']))
print('Salary:', str(Emp[KEY]['salary']))
print('='*52)

Output:

====================================================
Employee Ram Prakash :
Age: 25
Salary: 30000
Employee Divya :
Age: 23
Salary: 20000
====================================================

PRG 5:
Program to demonstrates the use of items() method.
print('='*52)
Emp= {'salary' :20000, 'dept' : 'Sales', 'age' : 23, 'name' : 'John'}
Emplist = Emp.items()
print (type (Emp), type (Emplist))
for a in Emplist:
print(a)

print ('-'*52)
seq = Emp.items()
print(type(seq))
for key, val in Emplist:
print (key, val)
print ('='*52)

1
Output:
====================================================
<class 'dict'> <class 'dict_items'>
('salary', 20000)
('dept', 'Sales')
('age', 23)
('name', 'John')
----------------------------------------------------
<class 'dict_items'>
salary 20000
dept Sales
age 23
name John
====================================================
PRG 6:
The dictionary Stud stores the rollnos:names of the students who have been
selected to participate in national event. Write a program to display:
➢ The roll numbers selected.
➢ The students’ names selected
print('='*52)
Stud ={}
N = int(input('How many students?'))
for a in range(N):
r, nm =eval(input('Enter nollno, name:'))
Stud[r]=nm
print("Created dictionary")
print(Stud)
print('-'*52)
print ("1. Selected roll numbers are:")
print(Stud.keys())
print('-'*52)
print ("2. Selected students' names are:")
print(Stud.values())
print('='*52)

Output:
====================================================
How many students?3
Enter nollno, name:12, 'MANISH'
Enter nollno, name:15, 'SUMIT'
Enter nollno, name:14, 'RAMAN'
Created dictionary
{12: 'MANISH', 15: 'SUMIT', 14: 'RAMAN'}
----------------------------------------------------
1. Selected roll numbers are:
dict_keys([12, 15, 14])
----------------------------------------------------
2. Selected students' names are:
dict_values(['MANISH', 'SUMIT', 'RAMAN'])
====================================================
2
PRG 7:
Your school has declared to deposit scholarship amount of Rs. 2500/- to some
selected students.
Write a program to input the selected students’ roll number and create a
dictionary for the same.

print('='*52)
List = []
N = int(input('How many students?'))
print('-'*52)
for a in range(N):
r =int(input('Enter nollno:'))
List.append(r)
Stud = dict.fromkeys(List, 2500)
print('-'*52)
print("Created dictionary")
print(Stud)
print('='*52)

Output:

====================================================
How many students?4
----------------------------------------------------
Enter nollno:12
Enter nollno:26
Enter nollno:14
Enter nollno:20
----------------------------------------------------
Created dictionary
{12: 2500, 26: 2500, 14: 2500, 20: 2500}
====================================================

PRG 8:
Write a menu driven program to create a dictionary Stud which stores marks of
students of a class with roll numbers as the keys and marks as the values.
Perform the following functionalities on the created dictionary Stud:
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.

3
print('='*52)
Stud ={}
n= int(input("How many studnets?"))
for a in range(n):
r, m = eval(input("Enter Roll No., Marks:"))
Stud [r] = m

print('-'*92)
print(' Created dictionary')
print(Stud)
print('-'*92)
while True:
print("1. Add new element to the dictionary Stud")
print("2. Delete element from the dictionary Stud")
print("3. Display dictionary elements")
print("4. Exit.")
ch=int(input("Enter your choice:\t"))
print('-'*92)
if ch== 1:
print('Enter details of new student')
r , m = eval(input("Enter Roll No., Marks:"))
Stud[r] = m
print('-'*92)

elif ch ==2:
rno = int(input("Enter roll no to be deleted:\t"))
if rno in Stud:
del Stud[rno]
print("Roll no.", rno,"deleted from the dictionary")
else:
print("Roll no.", rno, "does not exist in the dictionary.")
print('-'*92)
elif ch ==3:
print("Roll No\tMarks")
for key in Stud:
print(key,'\t',Stud[key])
print('-'*92)
elif ch== 4:
break
else:
print("Wrong choice")

Output:
====================================================
How many studnets?3
Enter Roll No., Marks:12, 82
Enter Roll No., Marks:15, 98
Enter Roll No., Marks:18, 97
--------------------------------------------------------------------------------------------
Created dictionary
{12: 82, 15: 98, 18: 97}
--------------------------------------------------------------------------------------------
4
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.
Enter your choice: 1
--------------------------------------------------------------------------------------------
Enter details of new student
Enter Roll No., Marks:10, 87
--------------------------------------------------------------------------------------------
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.
Enter your choice: 3
--------------------------------------------------------------------------------------------
Roll No Marks
12 82
15 98
18 97
10 87
--------------------------------------------------------------------------------------------
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.
Enter your choice: 2
--------------------------------------------------------------------------------------------
Enter roll no to be deleted: 18
Roll no. 18 deleted from the dictionary
--------------------------------------------------------------------------------------------
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.
Enter your choice: 3
--------------------------------------------------------------------------------------------
Roll No Marks
12 82
15 98
10 87
--------------------------------------------------------------------------------------------
1. Add new element to the dictionary Stud
2. Delete element from the dictionary Stud
3. Display dictionary elements
4. Exit.
Enter your choice: 4
--------------------------------------------------------------------------------------------

You might also like