A tuple is a sequence of Python objects that are immutable. This means you cannot update or alter the values in any way.

To create a tuple, use the “()” and assign it to a variable.

tup = ()

 

Now let’s create a tuple variable with values for example purposes. Just like a list and dictionary, it can contain mix character types.

tup2 = ('Tacos', 6, 'Rice', 3, 'Michigan')

 

Indexing and Slicing

Tuples can be sliced and indexed like strings and lists.

tup2[:2]
(‘Tacos’, 6)

 

Tuples index start at 0, just like strings and lists.

tup2[3]
3

 

Multiple Assignment

You can assign value for value using a tuple on the left side of the tuple assignment statement to correspond to a value on the right side of the assignment statement in the same indexed position. Let’s demonstrate this.

(x , y, z) = (24, 2017, 'Colorado')

x
y
z
24
2017
Colorado