Tuples
Tuples
NOTE
Values of type list are mutable i.e., changeable - one can change/add/delete a
list's elements. But the values of type tuple are immutable i.e.,non-changable; one
cannot make changes to a tuple.
The Tuples are depicted through parentheses i.e., round brackets, e.g., following
are some tuples in Python :
() (7,)
(1, 2, 3)
(1, 2.5, 3.7, 9)
('a', 'b', 'c')
('a', 1, "b', 3.5, 'zero')
('One', 'Two', 'Three')
Tuples are immutable sequences i.e., you cannot change elements of a tuple in
place.
Creating Tuples
To create a tuple, put a number of expressions, separated by commas in parentheses.
That is, to create a tuple you can write in the form given below:
T = ()
T = (value, ...)
This construct is known as a tuple display construct.
(1) was treated as an integer expression, hence t stores an integer 1, not a tuple.
To construct a tuple with one element just add a comma after the single element as
shown below:
>>> t = 3,
>>> t
(3,)
To create a one-element tuple, make
sure to add comma at the end
Now t stores a tuple, not integer.
Tuple Operations
Traversing a Tuple
Traversing a tuple means accessing and processing each element of it. The for loop
makes it easy
to traverse or loop over the items in a tuple, as per following syntax:
for <item> in <Tuple>:
process each item here
For example, following loop shows each item of a tuple T in separate lines:
T= ('P', 'u', 'r', 'e')
for a in T:
print(T[a])
The above loop will produce result as:
P
u
r
e
Joining Tuples
The + operator, the concatenation operator, when used with two tuples, joins two
tuples. Consider the example given below:
>>>tpl1 = (1, 3, 5)
>>> tp12 = (6, 7, 8)
>>> tp11 + tp12
(1, 3, 5, 6, 7, 8)
Like strings and lists, you can only use an integer with a * operator when trying
to replicate a tuple.
Recall that index on last limit is not included in the tuple slice. Consider the
following example:
>>> tpl = (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> seq=tpl [3:-3]
>>> seq
(20, 22, 24)
If you want to extract, not consecutive but every other element of the tuple, there
is a way out- the slice steps. The slice steps are used as per following format:
seq=T[start: stop: step]
>>> tpl
(10, 12, 14, 20, 22, 24, 30, 32, 34)
>>>tpl[0: 10: 2]
(10, 14, 22, 30, 34)
>>>tpl[2: 10:3]
(14, 24, 34)
>>> tpl[:: 3]
(10, 20, 30)
Unpacking Tuples
Creating a tuple from a set of values is called packing and its reverse, i.e.,
creating individual values from a tuple's elements is called unpacking.
Unpacking is done as per syntax:
<variable1>, <variable2>, <variable3>, ... =t
where the number of variables in the left side of assignment must match the number
of elements in the tuple.
The length of above tuple t is 4 as there are four elements in it. Now to unpack
it, we can write:
w, x, y, z=t
print(w,"-", x,"-",y,"-",z)
The output will be:
1-2-A-B
Tuple Functions
1. The len() method
This method returns length of the tuple, i.e., the count of elements in the tuple.
Syntax:
len(<tuple>)
>>> employee("John', 10000, 24, 'Sales') >>> len(employee)
2. The max() method
The len() returns the count of elements in the tuple
This method returns the element from the tuple having maximum value. Syntax:
max(<tuple>)
>>>tpl = (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> max(tpl)
34
3. The min() method
Maximum value from tuple ipl is returned
This method returns the element from the tuple having minimum value.
Syntax
min(<tuple>)
>>> tpl (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> min(tpl)