Dictionaries Manipulation Practice
Question:
Write a program to print the worker’s informations (Name age, salary) in records format.
Answer:
Employees = {‘Rohan’ : {‘age’ : 20, ‘salary’ : 10000}, ‘Mohan’ : {‘age’ : 30, ‘salary’ : 15000}}
for key in Employees :
print “Employee”, key + ‘:’
print ‘Age :’ + str (Employees [key] [‘age’]
print ‘Salary :’ + str (Employees [key] [‘salary’])
Write a program to print the worker’s informations (Name age, salary) in records format.
Answer:
Employees = {‘Rohan’ : {‘age’ : 20, ‘salary’ : 10000}, ‘Mohan’ : {‘age’ : 30, ‘salary’ : 15000}}
for key in Employees :
print “Employee”, key + ‘:’
print ‘Age :’ + str (Employees [key] [‘age’]
print ‘Salary :’ + str (Employees [key] [‘salary’])
Question:
What is a key-value pair with reference to Python dictionary ?
Answer:
A dictionary is a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair.
What is a key-value pair with reference to Python dictionary ?
Answer:
A dictionary is a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair.
Question:
What are the characteristics of Python Dictionaries ?
Answer:
The 3 main characteristics of a dictionary are :
What are the characteristics of Python Dictionaries ?
Answer:
The 3 main characteristics of a dictionary are :
1.
Dictionaries are Unordered : The
dictionary elements (key-value pairs) are not in ordered form.
2.
Dictionary Keys are Case Sensitive :
The same key name but with different case are treated as different keys in
Python dictionaries.
3.
No duplicate key is allowed : When
duplicate keys encountered during assignment, the last assignment wins.
4.
Keys must be immutable : We can use
strings, numbers or tuples as dictionary keys but something like [‘key’] is not
allowed.
Question:
Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and to input any name and to print the phone number of the particular name.
Answer:
phonebook = dict( )
Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and to input any name and to print the phone number of the particular name.
Answer:
phonebook = dict( )
n =
int(input("Enter total number of friends"))
i = 1
while
i<=n:
a = input("Enter name")
b = input("Enter phone number")
phonebook[a] = b
i = i + 1
print("Dictionary
is:- ")
print(phonebook)
name =
input("Enter name to search")
if name
in phonebook.keys():
print("Phone number =",
phonebook[name])
else:
print("Given name not exist")