Computer Science
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 ".
Related Questions
Carefully read the given code fragments and figure out the errors that the code may produce.
t = ( 'a', 'b', 'c', 'd', 'e') x, y, z, a, b = t
Carefully read the given code fragments and figure out the errors that the code may produce.
t = ( 'a', 'b', 'c', 'd', 'e') a, b, c, d, e, f = t
Predict the output.
tuple_a = 'a', 'b' tuple_b = ('a', 'b') print (tuple_a == tuple_b)
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))