Zum Inhalt

Tuples (Tupel)

Tupel ()

Ein Tupel ähnelt Listen, aber im Gegensatz zu Listen können wir sie nicht ändern.
Sie sind unveränderlich.
Wenn Sie nicht möchten, dass sich Listen ändern, verwenden Sie 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)