Tuples support the following build-in functions
If elements are of the same type, python performs the comparison and returns the result. If elements are different types, it checks whether they are numbers.
If we reached the end of one of the lists, the longer list is "larger." If both list are same it returns 0.
tuple1 = ('a', 'b', 'c', 'd', 'e')
tuple2 = ('1','2','3')
tuple3 = ('a', 'b', 'c', 'd', 'e')
cmp(tuple1, tuple2)
Out: 1
cmp(tuple2, tuple1)
Out: -1
cmp(tuple1, tuple3)
Out: 0
The function len
returns the total length of the tuple
len(tuple1)
Out: 5
The function max
returns item from the tuple with the max value
max(tuple1)
Out: 'e'
max(tuple2)
Out: '3'
The function min returns the item from the tuple with the min value
min(tuple1)
Out: 'a'
min(tuple2)
Out: '1'
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)
Use +
to concatenate two tuples
tuple1 + tuple2
Out: ('a', 'b', 'c', 'd', 'e', '1', '2', '3')