Chapter 13 Lists, Dictionaries and Tuples Solutions
Question - 21 : - Write a program to input any two matrices and print sum of matrices.
Answer - 21 : -
import random
m1 = input (“Enter total number of rows in the first matrix”)
n1 = input (“Enter total number of columns in the first matrix”)
a = [[random.random() for row in range(m1) for col in range (n1)]
for i in range (m1)
for j in range (n1):
a[i][j] = input()
m2 = input (“Enter total number of rows in the second matrix”)
n2 = input (“Enter total number of columns in the second matrix”)
b = [[random.random () for row in range (m2)] for col in range (n2)]
for i in range (m2):
for j in rnage (n2):
b [i][j] = input ()
c = [[random.random () for row in range (ml)] for col in range (n1)]
if ((m1 = m2) and (n1 = = b)):
print “Output is”
for i in range (m1):
for j in range (n1)]
c[i][j] = a[i][j] + b[i][j]
print c[i][j], ‘it’,
print
else :
print “Matrix addition is not possible”
Question - 22 : - Write a program to input any two matrices and print product of matrices.
Answer - 22 : -
import random
m1 = input (“Enter number of rows in first matrix”)
n1 = input (“Enter number of columns in first matrix”)
a = [[random.random () for row in range (m1)] for col in range (n1)]
for i in range (m1):
for j in range (n1):
a[i][j] = input ()
m2 = input (“Enter the number of rows in the second matrix”)
n2 = input (“Enter the number of columns in the second matrix”)
b = [[random.random () for row in range (m2)] for cal in range (n2)]
for i in range (m2):
for j in range (n2):
b[i][j] = input ()
c = [[random.random () for row in range (ml)] for col in range (n2)]
if (n1 = = m2):
for i in range (m1):
for j in range (n2):
c[i][j] = 0 for kin range (n1):
c[i][j] + = a[i][k]*b[k][j]
print c[i][j], ‘\t’,
print
else:
print “Multiplication is not possible”
Question - 23 : - Write a program to input any matrix and print both diagonal values of the matrix.
Answer - 23 : -
Import random
m = input (“Enter the number of rows”)
n = input (“Enter the number of columns”)
a = [[random.random () for row in range (m)] for cal in mange (n)]
if i in range (m):
for j in range (n):
a[i][j] = input ()
print “First diagonal”
for i in range (m):
print a[i][j], ‘\t’
k = m – 1
print “second diagonal”
for j in range (m):
print a[j][k], ‘\t’
k- = 1
else:
print “Diagonal values are not possible”
Question - 24 : - Write a program to input n x m matrix and find the sum of all numbers.
Answer - 24 : -
def summat (a, m, n):
s = 0
for i in range (m):
for j in range (n):
s + = a[i][j]
return s
Question - 25 : - Write a program to perform various list operations after displaying menu.
Answer - 25 : -
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 (raw_input (“Enter your choice :”)) if ch = = 1 :
print “1.Add element”
print “2.Add a List”
ch1 = int (raw_input
(“Enter choice 1 or 2:”))
if chi = = 1:
item = int
(raw_input (“Enter element:”))
pos = int (raw_input
(“Enter position to add : “))
list.insert (pos, item)
elif chi = = 2 :
list2 = eval (raw_input (“Enter list:”))
list.extend (lst2)
else:
print “Valid choices are 1 or 2”
print “Successfully added” elif ch = = 2 :
pos = int (raw_input
(“Enter position :”))
intem = int (raw_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 (raw_input (“Enter you choice 1 or 2’))
if chi = = 1:
pos = int (raw_input (“Enter position :”))
item = list.pop (pos)
print item, “deleted”
elif ch1 = = 2:
item = int (raw_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 (raw 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”
Question - 26 : - What is tuple ?
Answer - 26 : -
Tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists.
The only difference is that tuples can’t be changed i.e.,tuples are immutable and tuples use parentheses and lists use square brackets. Creating a tuple is as simple as putt ing different comma- separated values and optionally you can put these comma-separated values between parentheses also.
For example :
tup1 = (‘physics’, ‘chemistry’, 1997, 2000);
tup2 = (1,2, 3,4, 5);
tup3 = “a”, “b”, “c”, “d”;
Question - 27 : - Write the output of the given python code :
#!/user/bin/python
tup1 = (12, 34.56);
tup2 = (‘abc’, ‘xyz’);
#Following action is not valid for tuples
#tup1 [0] = 100;
#So let’s create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
Answer - 27 : -
Output :
(12,34.56, ’abc’, ‘xyz’)
Question - 28 : - Write the output of the given python code :
#!/user/bin/python
tuple1, tuple2 = (123, ‘xyz’, ‘zara’, ‘abc’), (456, 700, 200)
print “min value element : “, min (tuple1);
print “min value element : “, min (tuple2);
Answer - 28 : -
Output :
min value element : 123
min value element : 200
Question - 29 : - What is tuple in Python and how can we access values in tuples ?
Answer - 29 : -
Tuple is a sequence of immutable Python object. Tuples are sequences, just like lists. The only difference is that tuples can’t be changed i.e.,tuples are immutable and tuples use parentheses and lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma -separated values between parentheses also.
For example :
tup1 = (‘physics’, ‘chemistry1,1997,2000) ;
tup2 = (1, 2, 3, 4, 5);
tup3 = “a”, “b”, “c”, “d”;
The empty tuple is written as two parentheses containing nothing :
tup1 = ( ) ;
To write a tuple containing a single value you have to include a comma, even though there is only one
tup1 = (50,);
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on. Accessing Values in Tuples :
To access value in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index.
Following is a simple example :
# !/user/bin/python
tup1 = (‘physics’, ‘chemistry’, 1997,2000);
tup2 = (1, 2, 3,4,5,6, 7);
print “tup1[0]”, tup1[0]
print “tup2[1:5]:”, tup2[1:5]
When the above code is executed, it produces the following result :
tup1[0] : physics
tup2[1:5]: [2, 3,4,5]
Question - 30 : - How we update and delete tuples ?
Answer - 30 : -
Updating Tuples :
Tuples are immutable which means you cannot update them or change values of tuple elements. But we are able to take portions of an existing tuples to create a new tuples as follows. Following is a simple example :
#!/user/bin/python
tup1 =(12,34.56);
tup2 = (‘abc’, ‘xyz’);
#Following action is not valid for tuples
#tup1[0] = 100;
#So lets create a new tuple as follows :
tup3 = tup1 + tup2;
print tup3;
When the above code is executed, it produces the following result :
(12,34.56, ‘abc’, ‘xyz’)
Delete Tuple Elements :
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.
To explicity remove an entire tuple, just use the del statement. Following is a simple example :
#!/user/bin/python
tup = (‘physics’, ‘chemistry’, 1997,2000);
print tup;
del tup;
print “After deleting tup:”
print tup;
This will produce following result. Note an exception raised, there is because after del tup tuple does not exist any more :
(‘physics’, ‘chemistry’, 1997,2000)
After deleting tup :
Traceback (most recent call last) :
Fill “test.py”, line 9, in < modulo>
print tup;
Name Error : name ‘tup’ is not defined