Q. Carefully read the given code fragments and figure out the errors that the code may produce.
(a)
t = ('a', 'b', 'c', 'd', 'e')
print(t[5])
(b)
t = ('a', 'b', 'c', 'd', 'e')
t[0] = 'A'
(c)
t1 = (3)
t2 = (4, 5, 6)
t3 = t1 + t2
print(t3)
(d)
t2 = (4, 5, 6)
t3 = (6, 7)
print(t3 - t2)
(e)
t3 = (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
t6 = t3 * (3,)
print(t4)
print(t5)
print(t6)
(f)
t = ('a', 'b', 'c', 'd', 'e')
1, 2, 3, 4, 5, = t
(g)
t = ('a', 'b', 'c, d', 'e')
1n, 2n, 3n, 4n, 5n = t
(h)
t = ('a', 'b', 'c', 'd', 'e')
x, y, z, a, b = t
(i)
t = ('a', 'b', 'c', 'd', 'e')
a, b, c, d, e, f = t
Answer =
(a) IndexError: tuple index out of range
Correct code :-
print(t[5])
(b) TypeError: 'tuple' object does not support item assignment
Correct code :-
if t[0] == 'A' : # tuple is immutable
(c) TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Correct code :-
t2 = (4, 5, 6)
t3 = t1 + t2
print(t3)
(d) TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
Correct code :-
t3 = (6, 7)
print(t3 + t2) # We cannot substract Tuple
(e) TypeError: can't multiply sequence by non-int of type 'tuple'
Correct code :-
t4 = t3 * 3
t5 = t3 * (3)
t6 = t3 * (3) # (3) is integer but (3,) id Tuple so we can not multiply the tuple to tuple
print(t4)
print(t5)
print(t6)
(f) Syntax error
Correct code :-
t = (1, 2, 3, 4, 5 )
(g) invalid syntax
Correct code :-
t = ('a', 'b', 'c, d', 'e')
t = ( 1*n , 2*n, 3*n, 4*n, 5*n )
(h) It will give no error.
Correct code :-
t = ( 'x', 'y', 'z', 'a', 'b' )
(i) ValueError: not enough values to unpack (expected 6, got 5)
Correct code :-
t = ('a', 'b', 'c', 'd', 'e','f')
Answer these too:
ReplyDeletej]
t=('a', 'b', 'c' 'd' 'e')
x,y,z,a,b=t
Please, See (h) Part of that Question.
DeleteThe j and k part are missing please add them...
ReplyDeleteSorry, In my book there are only upto i. So, if you have j,k,l part then send me.
DeleteGiven below is the List
ReplyDeleteM=[10,20,30,40,50,60,70,80]
N=[100,120,140]
Find the following output:
a. Print(M+N)
b. Print(M[::2])
c. Print(N*2)
d. Print(M[0]+M[-1]+N[0])
Can you plz help me with this question
Output :-
DeleteA:-
[10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140]
B:-
[10, 30, 50, 70]
C:-
[100, 120, 140, 100, 120, 140]
D:-
190
Tell me which part is hard for you.
d part is wrong _ it will not give error
ReplyDeleteIt will give error please check it in your idle python.
DeleteTell me whole question.
ReplyDeleteHey you have missed these questions:-
ReplyDeleted) t1= (3, )
t2= (4, 5, 6)
print(t3)
g) odd = 1, 3 ,5
print(odd + [2,4,6]) [4]
Please send me photo of questions on telegram.
DeletePlease mention the corrected code as well
ReplyDeleteOk, I have done it.
DeleteHiii
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )