CBSE Class 12 Computer Science Question 87 of 105

Python Revision Tour II — Question 14

Back to all questions
14
Question

Question 7

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
Output
a is: Hello
b is: Nita
c is: How's
d is: life ?
Hi Nita
Explanation

ntpl is a tuple containing 4 elements. The statement (a, b, c, d) = ntpl unpacks the tuple ntpl into the variables a, b, c, d. After that, the values of the variables are printed.

The statement ntpl = (a, b, c, d) forms a tuple with values of variables a, b, c, d and assigns it to ntpl. As these variables were not modified, so effectively ntpl still contains the same values as in the first statement.

ntpl[0] ⇒ "Hello"
∴ ntpl[0][0] ⇒ "H"

ntpl[1] ⇒ "Nita"
∴ ntpl[1][1] ⇒"i"

ntpl[0][0] and ntpl[1][1] concatenates to form "Hi". Thus ntpl[0][0]+ntpl[1][1], ntpl[1] will return "Hi Nita ".