Q. Find The Output Generated by Following Code Fragments
Q. Find The Output Generated by Following Code Fragments
Answer =
>>> a
>>> b
>>> c
3
>>> type(a)
<class 'int'>
>>> type((a,b,c))
<class 'tuple'>
>>>
(c) It will give an error because there not enough value for unpacking of tuple.
(d) It will generate an error because there not enough value for unpacking of tuple.
>>> a
>>> p
>>> type(a)
<class 'int'>
>>> type(p)
<class 'int'>
>>>
>>> a
>>> p
1
>>> type(a)
<class 'int'>
>>> type(p)
<class 'int'>
>>> b
>>> q
>>> c
>>> r
>>> d
>>> s
>>> e
>>> t
>>> type(c)
<class 'int'>
>>> type(t)
<class 'int'>
>>>
(g) <class 'str'>
(h) <class 'tuple'>
(i) <class 'int'>
(j) It generate: -
(6, 7, 4, 5, 6)
(4, 5, 6, 6, 7)
>>>
(l)
(6, 7, 4, 5, 6)
(4, 5, 6, 6, 7)
>>>
(m) Output: -
(6, 7, 6, 7, 6, 7)
(6, 7, 6, 7, 6, 7)
(n) Output: -
(o) Values are: -
>>> a
()
>>> b
>>> c
(88, 85)
>>> d
>>> e
83
>>> f
(80, 88)
>>> g
()
>>> h
Answer =
(a)
>>> T[1][0: : 2]
('are', 'few')
(b)
True
(c)
>>> T [ : 1 ] + T[ 1 ]
(d)
>>> T[ 2 : : 2 ]
('that', 'will')
(e)
True
>>>
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(16)
(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 =
Answer =
a is: Hello
b is: Nita
c is: How's
d is: life?
Hi Nita
Q. Predict the output:
tuple_a = 'a', 'b'
tuple_b = ('a', 'b')
print (tuple_a == tuple_b)
Answer =
Output:-
True
>>>
Q. Find the error. Following code intends to create a tuple with
three identical strings. But even after successfully executing
following code (No error reported by Python). The len() returns
a value different from 3 Why ?
tup1 = ('Mega') * 3
print(len(tup1))
Answer = Because here ‘tup1’ is not tuple, it is string. If we modify the program then len() return 3.
Program is: -
tup1 = ('Mega',) * 3
print(len(tup1))
Q. Predict the output:
tuple1 = ('Python') * 3
print(type(tuple1))
Answer =
Output: -
<class 'str'>
>>>
Q. Predict the output:
x = (1, (2, (3, (4,))))
print (len(x))
print( x[1][0])
print(2 in x)
y = (1, (2, (3,), 4), 5)
print len((y))
print(len(y[1])
print( y[2] = 50)
z = (2, (1, (2,), 1), 1)
print (z[z[z[0]]])
Answer =
If correct code is :-
x = (1, (2, (3, (4,))))
print (len(x))
print( x[1][0])
print(2 in x)
y = (1, (2, (3,), 4), 5)
print (len((y)))
print(len( y[1] ) )
print( y[2] ==50 )
z = (2, (1, (2,), 1), 1)
print (z[z[z[0]]])
Then Output is
2
2
False
3
3
False
(1, (2,), 1)
Q. What will the following code produce?
Tup1 = (1,) * 3
Tup1 [0] = 2
print (Tup1)
Answer =
It will give an error that ‘TypeError: 'tuple' object does not support item assignment’.
Q. What will be the output of the following code snippet?
Tup1 = ((1, 2),) * 7
print (len(Tup1 [3: 8 ]))
Answer =
Output is: -
>>>