MENU

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

Free - Previous Years Question Papers
Any questions? Ask us!
×