Dictionaries Manipulation Practice
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’])
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.
Question:
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( )
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)
if name in phonebook.keys():
print("Phone number =", phonebook[name])
else:
print("Given name not exist")
Question:
How can we remove list element ?
Answer:
To remove a list element, you can use either the del statement or the remove method
Question:
What is the use of len(list)) in Python?
Answer:
It gives the total length of the list.
Question:
Describe max(list) method.
Answer:
It returns item from the list with max value.
Question:
Describe min(list) method.
Answer:
It returns item from the list with min value.
Question:
What is the use of list(seq) in Python ?
Answer:
It converts a tuple into list.
Question:
What do you mean by list.append(obj)
Answer:
Appends object obj to list
Question:
What do you mean by list.count(obj)
Answer:
Returns count how many times obj occurs in list
Question:
Explain list.extend(seq)
Answer:
Appends the contents of seq to list.
Question:
Is list.reverse() method return a value?
Answer:
This method does not return any value but reverse the given object from the list
Question:
Which method is used to sort objects of list.
Answer:
sort ()
Question:
Which function is used to reverse objects of list in place.
Answer:
list.reverse ()
Question:
Which function is use for removing object obj from list.
Answer:
list.remove(obj)
Question:
Which function is use for returning the lowest index in list that obj appears.
Answer:
List index (obj).
Question:
How are lists different from strings when both are sequences ?
Answer:
The lists and strings are different in following ways :
(a) The lists are mutable sequences while strings are immutable.
(b) Strings store single type of elements, all characters while lists can store elements belonging to
different types.
(c) In consecutive locations, strings store the individual characters while list stores the references of its
elements.
Question:
Write a program to calculate and display the sum of all the odd numbers in the list.
Answer:
pos = 0
sum = 0
while pos < len (L):
if L[pos] %2 = = 1 :
sum = sum + L [pos]
pos = pos + 1
print sum
Question:
Write a program to perform various list operations after displaying menu.
Answer:
ch = 0
list = [ ]
while true :
print “List Menu”
print “1. Adding”
print “2.Modify”
print “3.Delete”
print “4.Sort list”
print “5.Display list”
print “6.Exit”
ch = int (input (“Enter your choice :”))
if ch = = 1 :
print “1.Add element”
print “2.Add a List”
ch1 = int (input(“Enter choice 1 or 2:”))
if chi = = 1:
item = int(input (“Enter element:”))
pos = int (input(“Enter position to add : “))
list.insert (pos, item)
elif chi = = 2 :
list2 = eval (input (“Enter list:”))
list.extend (lst2)
else:
print “Valid choices are 1 or 2”
print “Successfully added”
elif ch = = 2 :
pos = int (input(“Enter position :”))
intem = int (input(“Enter new value :”))
old = list[pos]
list[.pos] = item
print old, ‘modified with vlaue”, item
elif ch = = 3:
print “1.Delete element by position”
print “2.Delete element by value”
ch1 = int (input (“Enter you choice 1 or 2’))
if chi = = 1:
pos = int (input (“Enter position :”))
item = list.pop (pos)
print item, “deleted”
elif ch1 = = 2:
item = int (input (“Enter element:”))
pos = list.remove (item)
print “Succcessfully delted”
else :
print “Valid choices are 1 or 2”
elif ch = = 4 :
print “l.Ascending”
print “2.Descending”
chi = int (input (“Enter choice 1 or 2”))
if chi = = 1:
list. sot ()
elif ch1 = = 2:
list.sort (reverse = True)
else :
print “Valid choices are 1 or 2”
elif ch = = 5:
print list
elif ch = = 6:
break
else :
print “valid choice 1 to 6”