Built-in Tuple Functions
suggest changeTuples support the following built-in functions.
Comparison
Tuples compare lexicographically: the first differing elements decide the order. There is no cmp builtin; it was removed in Python 3. If those elements cannot be ordered, < raises TypeError. == does not.
tuple1 = ('a', 'b', 'c', 'd', 'e')
tuple2 = ('1','2','3')
tuple3 = ('a', 'b', 'c', 'd', 'e')
tuple1 == tuple3
# Out: True
tuple2 < tuple1
# Out: True
(1, 2) < ('a',)
# TypeError: '<' not supported between instances of 'int' and 'str'
(1, 2) == ('a',)
# Out: False
If one tuple is a prefix of the other, the shorter tuple is smaller.
(1, 2) < (1, 2, 3)
# Out: True
Tuple Length
The function len returns the total length of the tuple
len(tuple1)
Out: 5
Max of a tuple
The function max returns item from the tuple with the max value
max(tuple1)
Out: 'e'
max(tuple2)
Out: '3'
Min of a tuple
The function min returns the item from the tuple with the min value
min(tuple1)
Out: 'a'
min(tuple2)
Out: '1'
Convert a list into tuple
The built-in function tuple converts a list into a tuple.
list = [1,2,3,4,5]
tuple(list)
Out: (1, 2, 3, 4, 5)
Tuple concatenation
Use + to concatenate two tuples
tuple1 + tuple2
Out: ('a', 'b', 'c', 'd', 'e', '1', '2', '3')
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents