KnowledgeBoat Logo
|

Computer Science

Consider two tuples t1 & t2 given below:

t1 = (100, 200, 300)
t2 = (10, 20, 30, 40)

Write the output of the following statements:

(a)

t1, t2 = t2, t1  
print(t1)   
print (t2)

(b) print (t1!=t2)

(c) print (t1 < t2)

Python

Python Tuples

1 Like

Answer

(a)

(10, 20, 30, 40)
(100, 200, 300)

Working

In the code t1, t2 = t2, t1, Python performs tuple unpacking and assignment simultaneously. This means that t1 is assigned the value of t2, and t2 is assigned the value of t1. As a result, t1 becomes (10, 20, 30, 40) and t2 becomes (100, 200, 300).

(b)

True

Working

The code print(t1 != t2) compares the tuples t1 and t2 to check if they are not equal. Since t1 is (100, 200, 300) and t2 is (10, 20, 30, 40), they have different lengths and different elements. Therefore, it evaluates to True.

(c)

False

Working

The code print(t1 < t2) compares the tuples t1 and t2 element by element. Since t1 = (100, 200, 300) and t2 = (10, 20, 30, 40), the comparison starts with the first elements, where 100 < 10 is False. Therefore, the tuple t1 is not less than t2, and the code prints False.

Answered By

3 Likes


Related Questions