Chapter 13 Lists, Dictionaries and Tuples Solutions
Question - 31 : - Explain cmp(tuplel, tuple2) with example.
Answer - 31 : -
cmp(tuple1, tuple2)
Despription :
The method cmp( ) compares elements of two tuples.
Syntax :
Following is the syntax for cmp( ) method :
cmp (tuplel, tuple2)
Parameters :
tuple 1 – This is the first tuple to be compared
tuple 2 – This is the second tuple to be compared
Return Value :
If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.
If numbers, perform numeric conversion if necessary and compare.
If either element is a number, then the other element is “larger” (numbers are “smallest”).
Otherwise, types are sorted alphabetically by name.
If we reached the end of one of the tuples, the longer tuple is “larger”. If we exhaust both tuples and share the same data, the result is a tie, meaning that 0 is returned.
Example :
The following example shows the usage of cmp( ) method
#!/user/bin/python
tuple1, tuple2 = (123, ‘xyz’), (456, ‘abc’)
print cmp(tuple1, tuple2);
print cmp(tuple1, tuple1);
tuple3 = tuple2 + (786,);
print cmp (tuple2, tuple3)
Let us compile and run the above program, this will produce the following result :
-1
1
-1
Question - 32 : - Explain tuple(seq) function of the python also give example.
Answer - 32 : -
tuple(seq)
Description :
The method tuple( ) is used to convert list into a tuple.
Syntax :
Following is the syntax for tuple( ) method :
tuple (seq)
Parameters :
• seq – This is a list to be converted into tuple. Return Value :
This method returns the tuple.
Example :
The following example shows the usage of tupe( ) method.
# !/user/bin/python
aList = [123, ‘xyz’, ‘zara’, ‘abc’];
aTuple = tuple (aList)
print “Tuple elements :”,aTuple
Let us compile and run the above program, this will produce the followin’, result :
Tuple elements : (123, ‘xyz’, ‘zara’, ‘abc’)
Question - 33 : - Write a program to input ‘n’ numbers and separate the tuple in the following manner.
Example
T=(10,20,30,40,50,60)
Tl=(10,30,50)
T2=(20,40,60)
Answer - 33 : -
t=tuple( )
n=input(” Enter number of values:”)
for i in range(n):
a=input(“enter elements”)
t=t+(a,)
t1=t2=tuple( )
for i in t:
if i%2 == 0:
t2= t2 + (i,)
else:
tl=tl+(i,)
print “First Tuple”
print t1
print “Second Tuple”
print t2
Output:
Enter number of values: 10
enter elements 1
enter elements 9
enter elements 8
enter elements 5
enter elements 2
enter elements 3
enter elements 6
enter elements 4
enter elements 7
enter elements 10
First Tuple (1, 9, 5, 3, 7)
Second Tuple (8,2,6,4,10)
Question - 34 : - Write a program to input ‘n’ employees’ salary and find minimum & maximum salary among ‘n’ employees.
Answer - 34 : -
t=tuple( )
n=input(“Enter total number of employees”)
for i in range(n):
a=input(“enter salary of employee:”)
t=t+(a,)
print “maximum salary=”,max(t)
print “minimum salary=”,min(t)
Output :
Enter total number of employees 3
enter salary of employee: 7800
enter salary of employee: 8934
enter salary of employee:
6544 maximum salary = 8934
minimum salary = 6544
Question - 35 : - How are dictionaries different from lists ?
Answer - 35 : -
The dictionary is similar to lists in the sense that it is also a collection of data-items just like lists but it is different form lists in the sense that lists are sequential collections and dictionaries are non-sequential collections. In lists, where there is an order associated with the data items because they act as storage units for other objects or variables created. Dictionaries are different from lists and tuples because the group of objects they hold are not in any particular order, but rather each object has its own unique name, commonly known as a key.
Question - 36 : - When are dictionaries more useful than lists ?
Answer - 36 : -
Dictionaries can be much more useful than lists. For example : suppose we wanted to store all our friend’s cell phone numbers. We could create a list of pairs (phone number, name), but once this list becomes long enough searching this list for a specific phone number will get time consuming. Better would be if we could index the list by our friend’s name. This is precisely what a dictionary does.
Question - 37 : - Explain cmp(dict1, dict2) with example.
Answer - 37 : -
Description
The method cmp( ) compares two dictionaries
based on key and values.
Syntax
Following is the syntax for cmp( ) method :
cmp(dict1, dict2)
Parameters
• dict1 – This is the first dictionary to be compared with dict2.
• dict2 – This is the second dictionary to be compared with dict1.
Return value :
This method returns 10 if both dictionaries are equal, – 1 if dictl < dict2 and 1 if dictl > dict2.
Example :
The following example shows the usage of comp( ) method.
# !/user/bin/python
dict1 = {‘Name’ : ‘Zara’, ‘Age’ : 7};
dict2 = {‘Name1 : ‘Mahnaz’, ‘Age’ : 27};
dict3 = {‘Name’ : ‘Abid’, ‘Age’ : 27);
dict4 = {‘Name’ : ‘Zara’, ‘Age’ : 7};
print “Return Value : %d”, % cmp (dict1, dict2)
print “Return Value : %d”, % cmp (dict2, dict3)
print “Return Value : %d”, % cmp (dictl, dict4)
Let us compile and run the above program, this will produce the following result :
Return Value : – 1 .
Return Value : 1
Return Value : 0
Question - 38 : - Explain str(dict) with example.
Answer - 38 : -
Description
The method str( ) produces a printable string representation of a dictionary.
Syntax
Following is the syntax for str( ) method :
str(dict)
Parameters
• diet – This is the dictionary.
Return Value
This method returns string representation.
Example :
The following examples shows the usage of str() method.
# !/user/bin/python
diet = {‘Name’ :’Zara’, ‘Age’ : 7);
print “Equivalent String : %s” %str (dict)
Let us compile and run the above program, this will produce the following result :
Equivalent String : {‘Age’ : 7, ‘Name’ : ‘Zara’}
Question - 39 : - 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 - 39 : -
phonebook = dict( )
n = input (“Enter total number of friends”)
i = 1
while i<=n :
a = raw_input (“Enter name”)
b = raw_input (“Enter phone number”)
phonebook [a] = b
i = i + 1
name = raw_input (“Enter name”)
f = 0
l = phonebook.keys( )
for i in l :
if (comp (i, name) = = 0) :
print “Phone number =”, phonebook[i]
f = 1
if (f = = 0):
print “Given name not exist”
Question - 40 : - Write a Python program to input ‘n’names and phone numbers to store it in a dictionary and to search and print the phone number of that particular name.
Answer - 40 : -
phonebook=dict( )
n=input(” Enter total number of friends to create a telephone book”)
i=l
while i<=n:
a=raw_input(“enter name”)
b=raw_input(“enter phone number”)
phonebook[a]=b
i=i+l
name=raw_input(“enter friend’s name to search for”)
f=0
l=phonebook.keys( )
for i in l:
if(cmp(i,name) == 0):
print “Phone number= “,phonebook[i]
f=l
if (f==0):
print “Given name not exist”
Output:
Enter total number of friends to create a telephone
book 5
enter name Ramu
enter phone number 9842311111
enter name Raju
enter phone number 9842309564
enter name Sarita
enter phone number 9842398765
enter name Sowmya
enter phone number 9842399922
enter name Karan
enter phone number 9842366554
enter friend’s name to search for Sarita
Phone number = 9842398765