Skip to content

Tuples

Tuple ()

A tuple are like lists, but unlike lists we cannot modify them.
They are immutable.
If you don't want lists to change, use tuple.

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple)
print(my_tuple[1])          # indexing in Tuple
print(5 in my_tuple)        # know if it exists or not

new_tuple = my_tuple[1:4]   # slicing in tuple
print(new_tuple)

print(my_tuple.count(2))    # how many times 2 occurs; Output: 1
print(my_tuple.index(4))    # shows index of 4; Output: 3
print(len(my_tuple))        # length of tuple

x, y, z, *other = (1, 2, 3, 4, 5)
print(other)