Accessing Values in Tuples
Accessing Values in Tuples
The main difference between the tuples and the lists is that the tuples cannot be
changed unlike lists. Tuples use parentheses, whereas lists use square brackets.
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though
there is only one value.
tup1 = (50,)
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so
on.
#!/usr/bin/python3
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
157
Python 3
Updating Tuples
Tuples are immutable, which means you cannot update or change the values of tuple
elements. You are able to take portions of the existing tuples to create new tuples as the
following example demonstrates.
#!/usr/bin/python3
To explicitly remove an entire tuple, just use the del statement. For example-
#!/usr/bin/python3
tup = ('physics', 'chemistry', 1997, 2000);
print (tup)
del tup;
print "After deleting tup : "
print tup
Note: An exception is raised. This is because after del tup, tuple does not exist any more.
In fact, tuples respond to all of the general sequence operations we used on strings in
the previous chapter.
159
Python 3
No Enclosing Delimiters
No enclosing Delimiters is any set of multiple objects, comma-separated, written without
identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples,
as indicated in these short examples.
1 cmp(tuple1, tuple2)
2 len(tuple)
3 max(tuple)
4 min(tuple)
5 tuple(seq)
Description
The len() method returns the number of elements in the tuple.
Syntax
Following is the syntax for len() method-
len(tuple)
Parameters
tuple - This is a tuple for which number of elements to be counted.
160
Python 3
Return Value
This method returns the number of elements in the tuple.
Example
The following example shows the usage of len() method.
#!/usr/bin/python3
tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc')
print ("First tuple length : ", len(tuple1))
print ("Second tuple length : ", len(tuple2))
Description
The max() method returns the elements from the tuple with maximum value.
Syntax
Following is the syntax for max() method-
max(tuple)
Parameters
tuple - This is a tuple from which max valued element to be returned.
Return Value
This method returns the elements from the tuple with maximum value.
Example
The following example shows the usage of max() method.
#!/usr/bin/python3
tuple1, tuple2 = ('maths', 'che', 'phy', 'bio'), (456, 700, 200)
print ("Max value element : ", max(tuple1))
print ("Max value element : ", max(tuple2))
161
Python 3
Description
The min() method returns the elements from the tuple with minimum value.
Syntax
Following is the syntax for min() method-
min(tuple)
Parameters
tuple - This is a tuple from which min valued element is to be returned.
Return Value
This method returns the elements from the tuple with minimum value.
Example
The following example shows the usage of min() method.
#!/usr/bin/python3
tuple1, tuple2 = ('maths', 'che', 'phy', 'bio'), (456, 700, 200)
print ("min value element : ", min(tuple1))
print ("min value element : ", min(tuple2))
Description
The tuple() method converts a list of items into tuples.
Syntax
162
Python 3
tuple( seq )
Parameters
seq - This is a tuple to be converted into tuple.
Return Value
This method returns the tuple.
Example
The following example shows the usage of tuple() method.
#!/usr/bin/python3
list1= ['maths', 'che', 'phy', 'bio']
tuple1=tuple(list1)
print ("tuple elements : ", tuple1)