Advertisements
Advertisements
Question
What will be the output of the following code?
tuple1 = (1, 2, 3)
tuple2 = tuple1
tuple1 += (4,)
print(tuple1 == tuple2)
Options
True
False
tuple1
Error
MCQ
Solution
False
Explanation:
-
Initially, both
tuple1
andtuple2
are(1, 2, 3)
. -
Tuples are immutable, so when we do
tuple1 += (4,)
, it creates a new tuple(1, 2, 3, 4)
and assigns it totuple1
. -
Now,
tuple1
is(1, 2, 3, 4)
, buttuple2
is still(1, 2, 3)
. -
Comparison:
(1, 2, 3, 4)
is not equal to(1, 2, 3)
.- So, the result will be
False
.
shaalaa.com
Is there an error in this question or solution?