0% found this document useful (0 votes)
392 views

Q. Find The Output Generated by Following Code Fragments

The document contains multiple code snippets testing various tuple operations in Python and questions about predicting their output. Tuples are immutable ordered sequences of elements that can contain heterogeneous or homogeneous data types. The snippets cover tuple packing and unpacking, indexing, slicing, length, membership testing, repetition, addition, and other common tuple operations. Predicted outputs are provided for each code block to check understanding of how tuples work in Python.

Uploaded by

Vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
392 views

Q. Find The Output Generated by Following Code Fragments

The document contains multiple code snippets testing various tuple operations in Python and questions about predicting their output. Tuples are immutable ordered sequences of elements that can contain heterogeneous or homogeneous data types. The snippets cover tuple packing and unpacking, indexing, slicing, length, membership testing, repetition, addition, and other common tuple operations. Predicted outputs are provided for each code block to check understanding of how tuples work in Python.

Uploaded by

Vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Q.

Find the output generated by following code fragments:


 
(a)
plane = ("Passengers", "Luggage")
plane[1] = "Snakes"
 
(b)
(a, b, c) = (1, 2, 3)
 
(c)
(a, b, c, d) = (1, 2, 3)
 
(d)
a, b, c, d = (1, 2, 3)
 
(e)
a, b, c, d, e = (p, q, r, s, t) = t1
 
(f)
What will be the values and types of variables a, b, c, d, e, f, p, q, r, s, t after
executing part e above?
 
(g)
t2 = ('a')
type(t2)
 
(h)
t3 = ('a',)
type(t3)
 
(i)
T4 = (17)
type(T4)
 
(j)
T5 = (17,)
type(T5)
 
(k)
tuple = ('a', 'b', 'c', 'd', 'e')
tuple = ('A',) + tuple [1:]
print (tuple)
 
(l)
t2 = (4, 5, 6)
t3 = (6, 7)
t4  = t3 + t2
t5 = t2 + t3
print(t4)
print(t5)
 
(m)        
t3 =  (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
print(t4)
print(t5)
 
(n)
t1 = (3, 4)
t2 = ('3', '4')
print(t1 + t2)
 
(o) What will be stored in variables a, b, c, d, e, f, g, h after following
statements?
 
perc = (88, 85, 80, 88, 83, 86)
a = perc[2:2]
b = perc[2:]
c = perc[:2]
d = perc[:-2]
e = perc[-2]
f = perc[2:-2]
g = perc[-2:2]
h = perc[:]

Answer =

(a) It will generate an error because tuple is immutable.

(b) After running the program, we get that 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.

(e) If value of t1 = (1, 2, 3, 4, 5) Then after running the program.

>>> a

>>> p

>>> type(a)

<class 'int'>

>>> type(p)

<class 'int'>

>>> 

(f) If value of t1 = (1, 2, 3, 4, 5) Then after running the program.

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

('A', 'b', 'c', 'd', 'e')

(k) It give output: -

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

(3, 4, '3', '4')


>>> 

(o) Values are: -

>>> a

()

>>> b

(80, 88, 83, 86)

>>> c

(88, 85)

>>> d

(88, 85, 80, 88)

>>> e

83

>>> f

(80, 88)

>>> g

()

>>> h

(88, 85, 80, 88, 83, 86)


Q. What does each of the following expressions evaluate to?
Suppose that T is the tuple
 
("These", ("are", "a", "few", "words"), "that", "we", "will", "use")
 
(a)
T[1][0: : 2]
 
(b)
"a" in T [1] [ 0 ]
 
(c)
T [ : 1 ] + T[ 1 ]
 
(d)
T[ 2 : : 2 ]
 
(e)
T[2][2] in T[1]

Answer =

Output are follow:-

(a)

>>> T[1][0: : 2]

('are', 'few')

(b)

>>> "a" in T [1] [ 0 ]

True

(c)

>>> T [ : 1 ] + T[ 1 ]

('These', 'are', 'a', 'few', 'words')

(d)

>>> T[ 2 : : 2 ]

('that', 'will')

(e)

>>> T[2][2] in T[1]

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 =

(a) IndexError: tuple index out of range

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

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

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

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

(f) syntax error

(g) invalid syntax

(h) It will give no error.

(i) ValueError: not enough values to unpack (expected 6, got 5)

Q. What would be the output of following code if


ntpl = ("Hello", "Nita", "How's", "life?")
 
(a, b, c, d) = ntpl
print ( "a is:", a)
print("b is:", b)
print("c is:", c)
print("d is:", d)
ntpl = (a, b, c, d)
print (ntpl[0][0] + ntpl[1][1], ntpl[1])
 

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 = 

It will give an error.

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

>>> 

You might also like