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)
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.
Related Questions
t1 = (100, 200, "Global", 3, 3.5, "Exam", [1, 2], (30, 40), (3, 5, 3)) :
Consider the above tuple 't1' and answer the following questions:
(a) len(t1)
(b) 20 not in t1
(c) t1[-8:-4]
(d) t1[5:]
(e) t1.index(5)
(f) t1.index(3, 6)
(g) 10 in t1
(h) t1.count(3)
(i) any(t1)
(j) t1[2]*2
Write the output of the following statements:
(a) >>> tuple([10,20,40])
(b) >>> ("Tea",)*5
(c) >>> tuple ("Item")
WAP to accept values from a user. Add a tuple to it and display its elements one by one. Also display its maximum and minimum value.
Write a program to input any values for two tuples. Print it, interchange it and then compare them.