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 :- 


t = ('a', 'b', 'c', 'd', 'e')
print(t[5])


(b) TypeError: 'tuple' object does not support item assignment

Correct code :- 


t = ('a', 'b', 'c', 'd', 'e')
if t[0] == 'A' : # tuple is immutable


(c) TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

Correct code :- 


t1 =  (3,) # (3) is Integer but (3,) is Tuple
t2 = (4, 5, 6)
t3 = t1 + t2
print(t3)


(d) TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

Correct code :- 


t2 = (4, 5, 6)
t3 = (6, 7)
print(t3 + t2) # We cannot substract Tuple


(e) TypeError: can't multiply sequence by non-int of type 'tuple'

Correct code :- 


t3 = (6, 7)
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 = ('a', 'b', 'c', 'd', 'e')
t = (1, 2, 3, 4, 5 )


(g) invalid syntax

Correct code :- 


n = 1 # any number
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 = ('a', 'b', 'c', 'd', 'e')
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')
t = ('a', 'b', 'c', 'd', 'e','f')


14 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Answer these too:
    j]
    t=('a', 'b', 'c' 'd' 'e')
    x,y,z,a,b=t

    ReplyDelete
  2. The j and k part are missing please add them...

    ReplyDelete
    Replies
    1. Sorry, In my book there are only upto i. So, if you have j,k,l part then send me.

      Delete
  3. Given below is the List
    M=[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

    ReplyDelete
    Replies
    1. Output :-

      A:-
      [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.

      Delete
  4. d part is wrong _ it will not give error

    ReplyDelete
    Replies
    1. It will give error please check it in your idle python.

      Delete
  5. Hey you have missed these questions:-

    d) t1= (3, )
    t2= (4, 5, 6)
    print(t3)

    g) odd = 1, 3 ,5
    print(odd + [2,4,6]) [4]

    ReplyDelete
    Replies
    1. Please send me photo of questions on telegram.

      Delete
  6. Please mention the corrected code as well

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post