Page 152 - Computer Science Class 11 Without Functions
P. 152
● Tuples
A tuple is a sequence of objects (not necessarily of the same type) enclosed in parenthesis: (). Just like lists, data
items in a tuple are separated by commas (,). However, there is a striking difference between a list and a tuple. A tuple
is an immutable object, i.e., once created, the values in a tuple cannot be changed using an assignment operator. In
contrast, we have already seen that a list is a mutable object, i.e., the values in a list can be easily changed using an
assignment operator.
>>> vowels = ('a','e','i','o','u')
>>> vowels
('a', 'e', 'i', 'o', 'u')
>>> weekDays = ('Monday', 'Tusday', 'Wednesday', 'Thursday', 'Friday')
>>> weekDays
('Monday', 'Tusday', 'Wednesday', 'Thursday', 'Friday')
On realising that you wrongly typed Tuesday, it would be tempting to modify weekDays[1]
to 'Tuesday'
>>> weekDays[1] = 'Tuesday'
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
weekDays[1] = 'Tuesday'
TypeError: 'tuple' object does not support item assignment
Note that an attempt to modify an element of a tuple has resulted in a type error because we have applied an illegal
operation to a tuple.
7.1.4 Set
A set is an unordered and unindexed collection of data items separated by a comma and enclosed within curly
brackets:{}. Unlike a list, a set cannot have duplicate values. For example,
>>> set1 = {1, 2, 3, 4}
>>> set1
{1, 2, 3, 4}
>>> set2 = {1, 2, 3, 5, 1, 6, 2}
>>> set2
{1, 2, 3, 5, 6}
Note that the duplicate entries in set2 have been removed. A set may be modified using the operator |=. As follows:
>>> s = {1, 29, 4}
>>> id(s)
2309188291712
>>> s = {54, 9}/s |= {54, 9}
>>> s
{1, 4, 54, 9, 29}
>>> id(s)
2309188291712
7.1.5 None
NoneType has a single value: None. It is used to signify the absence of value in a given statement. None supports
no special operations. The value of None is neither False nor 0 (zero). Consider the following examples:
>>> type(None)
<class 'NoneType'>
>>> print(None)
None
However, if you execute the command,
None
Nothing is printed. In a logical expression, the value of None is False.
150 Touchpad Computer Science-XI

