Page 304 - Computer Science Class 11 Without Functions
P. 304
(30, 'Hello', 2.4)
>>> myTuple[:4] # yields a tuple comprising 1st to 4th element
(22, 30, 'Hello', 2.4)
>>> myTuple[4:] # yields a tuple comprising 5th to last element
(2, 'Python', (890, 900), 45)
>>> myTuple[:] # yields a tuple comprising 1st to last element
(22, 30, 'Hello', 2.4, 2, 'Python', (890, 900), 45)
>>> myTuple[::2] # yields a tuple comprising alternate elements, starting index 0
(22, 'Hello', 2, (890, 900))
>>> myTuple[-4] # yields fourth element from the right end
2
>>> myTuple[::-1] # yields a tuple in reverse order
(45, (890, 900), 'Python', 2, 2.4, 'Hello', 30, 22)
C T 03 What will be the output produced on the execution of following code?
tuple1 = 30, 40
tuple2 = (30, 40)
print(tuple1 == tuple2)
print(tuple1[1])
12.10.5 Nested Tuples
As mentioned earlier, tuples may contain other compound objects, including lists, dictionaries, and other tuples. So, as
a special case, tuples may be nested inside of other tuples.
Example:
>>> birthDates = ((8, 'August'), (1, 'October'), (30, 'September'))
12.10.6 Elements of Tuple may be Mutable
We have seen above that a tuple is an immutable object. However, an element of a tuple may be mutable. For example,
>>> discipline = ('Science', ['Physics', 'Chemistry', 'Biology'])
>>> discipline[1].append('Environment Science')
>>> discipline
('Science', ['Physics', 'Chmistry', 'Biology', 'Environment Science'])
Table 12.2: Commonly Used Built-in Functions on Tuples
S. No. Function Description Examples
1. len() It returns the number of >>> len((1, 3, 'Mon', 'Wed'))
elements in a tuple. 4
>>> len(())
0
2. max(tpl) It returns the largest element >>> tpl=(3, -2, 0, 78, 25)
from the tuple: tpl. >>> max(tpl)
78
3. min(tpl) It returns the smallest element >>> tpl=(3, -2, 0, 78, 25)
from the tuple: tpl. >>> min(tpl)
-2
302 Touchpad Computer Science-XI

