0% found this document useful (0 votes)
5 views2 pages

Dictionaries Notes

The document provides instructions for creating and manipulating dictionaries in Python, specifically focusing on odd numbers and employee data. It includes examples of displaying keys, values, items, checking for presence, retrieving values, deleting items, and adding new entries. Additionally, it covers operations on a dictionary containing names and ages, such as displaying, modifying, and deleting entries.
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)
5 views2 pages

Dictionaries Notes

The document provides instructions for creating and manipulating dictionaries in Python, specifically focusing on odd numbers and employee data. It includes examples of displaying keys, values, items, checking for presence, retrieving values, deleting items, and adding new entries. Additionally, it covers operations on a dictionary containing names and ages, such as displaying, modifying, and deleting entries.
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/ 2

Chapter-Dictionaries

1.Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is number and the value is the
corresponding number in words. Perform the following operations on this dictionary:
(a) Display the keys
(b) Display the values
(c) Display the items
(d) Find the length of the dictionary
(e) Check if 7 is present or not
(f) Check if 2 is present or not
(g) Retrieve the value corresponding to the key 9
(h) Delete the item from the dictionary corresponding to the key 9

>>> ODD = {1:'One', 3:'Three', 5:'Five', 7:'Seven', 9:'Nine'}


>>> ODD
{1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
(a) Display the keys
>>> ODD.keys()
dict_keys([1, 3, 5, 7, 9])
(b) Display the values
>>> ODD.values()
dict_values(['One', 'Three', 'Five', 'Seven', 'Nine'])
(c) Display the items
>>> ODD.items()
dict_items([(1, 'One'), (3, 'Three'), (5, 'Five'), (7, 'Seven'),(9, 'Nine')])
(d) Find the length of the dictionary
>>> len(ODD)
5
(e) Check if 7 is present or not
>>> 7 in ODD
True
(f) Check if 2 is present or not
>>> 2 in ODD
False
(g) Retrieve the value corresponding to the key 9
>>> ODD.get(9)
'Nine‘
(h) Delete the item from the dictionary corresponding to the key 9
>>> del ODD[9]
>>> ODD
{1: 'One', 3: 'Three', 5: 'Five', 7: 'Seven'}
2. Write a program to enter names of employees and their salaries as input and store them in a dictionary.
emp= dict()
n = int(input("How many employees data u want to take"))
for i in range(0,n):
name = input("Enter the name of the Employee: ")
salary = int(input("Enter the salary: "))
emp[name] = salary
for k in emp:
print(k,emp[k])
output
Enter the number of employees whose data to be stored: 3
Enter the name of the Employee: 101
Enter the salary: 20000
101 20000
Enter the name of the Employee: 102
Enter the salary: 40000
101 20000
102 40000
Enter the name of the Employee: 104
Enter the salary: 30000
101 20000
102 40000
104 30000
3.Write a program to input n employee names and their phone numbers to store it in a dictionary as key:value pair.
Input a name to search and display its phone no if found in dictionary otherwise display an error message.
emp=dict()
n = int(input("How many students? "))
for i in range(0,n):
name= input("Enter name of employee")
phone=input("Enter phone no:")
emp[name]=phone
nm=input("Enter name to search")
if nm in emp:
print("Phone no of",nm,"is",emp[nm])
else:
print("Employee name not found")
Ouput:
How many students? 3
Enter name of employeeRahul
Enter phone no:9897280816
Enter name of employeeTina
Enter phone no:7345617820
Enter name of employeeSam
Enter phone no:8098761234
Enter name to searchTina
Phone no of Tina is 7345617820
4.Assuming dictionary d has name as key and value as age given below
d={‘Tina’:18, ‘Sam’:19, ‘Hina’:17,’ ‘Aman’:20}
Write a statements to perform the following operations on the dictionary:
Display all keys of the dictionary .
Display all values of the dictionary.
Display all items of the dictionary in presentable format.
d) Display the dictionary in sorted order of names
e) Check if a friend ‘John’ is present in the dictionary or not
f) Modify the age of a friend ‘Aman’ to 18
g) Add a new key-value pair {‘Leena’: 20 } in this dictionary and display the modified dictionary
h) Delete a particular friend from the dictionary
i) Display the name and age of all your friends using for loop.(without using functions)

You might also like