Tuples in Python with examples

A Tuple in Python is a collection of immutable objects that are separated by commas. Here immutable objects mean those objects that can’t be changed or update.

Tuples in Python generally look like lists. But the difference between a Python tuple and a list is that tuple uses parentheses instead of square brackets. Also, objects in tuples can’t be changed whereas items of a Python list can be changed or updated.

Example of tuples in Python

Below is an example of a Tuple:

my_tuple = ('shirt', 'pant', 'skirt', 'shoes', 'watch')

The below code snippet shows the creation of tuples:

# creating a empty tuple
my_tuple = ()
print(my_tuple)

print()

# creating a tuple without parenthesis
my_tuple = 1, 2, 3, 4
print(my_tuple)

print()

# creating a tuple with parenthesis
my_tuple = (1, 2, 3, 4)
print(my_tuple)

print()

# concatenation of tuples
my_tuple1 = (1, 2, 3, 4)
my_tuple2 = ('A', 'B', 'C', 'D')

my_tuple3 = my_tuple1 + my_tuple2
print(my_tuple3)

print()

# nesting a tuple in another tuple
my_tuple1 = (1, 2, 3, 4)
my_tuple2 = ('A', 'B', 'C', 'D')

my_tuple3 = (my_tuple1, my_tuple2)
print(my_tuple3)
Output:
()

(1, 2, 3, 4)

(1, 2, 3, 4)

(1, 2, 3, 4, 'A', 'B', 'C', 'D')

((1, 2, 3, 4), ('A', 'B', 'C', 'D'))

 

Accessing elements from a tuple:

We can access the elements in a list, by passing the index of an element in a square bracket. This method of accessing elements from a sequence is known as indexing. The way we access elements from a tuple is similar to a list or a sequence.

 

We can access nested tuples in the same way we access multidimensional arrays.

 

The below code snippet shows accessing elements from tuples:
# creating a tuple
my_tuple = (1, 2, 3, 4)
print(my_tuple)

print()

# accessing tuple elements using indexing
a = my_tuple[0]
b = my_tuple[3]
print(a)
print(b)

print()

# accessing nested tuples
my_tuple1 = (1, 2, 3, 4)
my_tuple2 = ('A', 'B', 'C', 'D')

my_tuple3 = (my_tuple1, my_tuple2)
print(my_tuple3)

print()

a = my_tuple3[0][2]
b = my_tuple3[1][2]

print(a)
print(b)
Output:
(1, 2, 3, 4)

1
4

((1, 2, 3, 4), ('A', 'B', 'C', 'D'))

3
C

Note: As the tuples are Immutable, we cannot delete the elements from a tuple, the way we can from a list. In this regard, tuples are more similar to strings.

Select a tuple by index number

Just like a Python list, we can also get the value of a tuple object by its index number with the same way as we can see in lists.

For example, if we want to get the object of the index number 3 then below is how we can do it:

my_tuple[3]

The above code will give the output that is given below:

shoes

Slicing in tuple

We can access a part of a tuple in python using slicing. This allows us to access a range of elements in a tuple. Therefore, we can access multiple elements without using iteration. This helps us to keep our code clean and reduces execution times. The syntax below shows the method of slicing a tuple.

 

Syntax:
tuple_name[start_index : end_index]

We can use the slicing feature in our tuple as we can see below:

my_tuple[0:3]

It will give us the output:

('shirt', 'pant', 'skirt')

So we can see that getting the value of a tuple item and apply the slicing process to a tuple is the same as we can see in the case of a Python list.

The below code snippet depicts the slicing of a list:
# creating a tuple
my_tuple1 = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')
print(my_tuple1)

print()

# slicing a tuple from start to given index
my_tuple2 = my_tuple1[:5]
print(my_tuple2)

print()

# slicing a tuple between two indexes
my_tuple2 = my_tuple1[2:5]
print(my_tuple2)

print()

# slicing a tuple from a given index to the end
my_tuple2 = my_tuple1[2:]
print(my_tuple2)

print()

# slicing the tuple from end to start using negative indexes
my_tuple2 = my_tuple1[::-1]
print(my_tuple2)
Output:
('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')

('A', 'B', 'C', 'D', 'E')

('C', 'D', 'E')

('C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')

('J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A')

 

Example Python code with tuple

You can run the Python code that you can see below on your system:

my_tuple = ('shirt', 'pant', 'skirt', 'shoes', 'watch')
print(my_tuple)
print(my_tuple[3])
print(my_tuple[0:3])

The above code will print the tuple, get the value of tuple with item index number 3 and give the output after slicing a tuple. See the output below:

('shirt', 'pant', 'skirt', 'shoes', 'watch')
shoes
('shirt', 'pant', 'skirt')

 

 

Adding tuples

We can add two or more tuples and create a new tuple. below is how we can do it:

my_tpl1 = ('bag', 215, 'red', 56, 'basket')
my_tpl2 = ('star', 'moon', 865, 77)
my_tpl3 = my_tpl1 + my_tpl2
print(my_tpl3)

if we run our above Python code, then we will able to see a new tuple. The output of the code will be:

('bag', 215, 'red', 56, 'basket', 'star', 'moon', 865, 77)

We can see that we have created a new tuple.

Built-in methods for tuples in Python:

The Python interpreter has several functions and types built into it that are always available. Besides, there are built-in methods we can use concerning tuples in python.

 

The below code snippet shows the built-in methods for tuples:
my_tuple = tuple() 
methods = dir(my_tuple) 
print(methods)
Output:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', 'count', 'index']

 

Now let us look at some of these built-in methods for tuples:

 

 1. len(): The len() method returns the length of the tuple. Meaning, the method returns the number of elements present in a tuple. The length is returned as an integer value and is stored in a variable.

 

Syntax:
len(tuple_name)

 

The below code snippet shows the working of the len() method:
# creating a tuple
my_tuple = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')
print(my_tuple)

print()

# obtaining the length of a tuple using len()
a = len(my_tuple)
print('The length of the tuple is:', a)
Output:
('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')

The length of the tuple is: 10

2. max():  The max() method, as the name implies returns the maximum of all the elements in the tuple.

 

Syntax:
max(tuple_name)

 

The below code snippet shows the working of the max() method:
# creating a tuple
my_tuple = (1, 2, 3, 4)
print(my_tuple)

print()

# obtaining the maximum element using max()
a = max(my_tuple)
print('Maximum element is:', a)
Output:
(1, 2, 3, 4)

Maximum element is: 4

3. min(): The max() method, as the name implies returns the minimum of all the elements in the tuple.

Syntax:
min(tuple_name)
The below code snippet shows the working of the min() method:
# creating a tuple
my_tuple = (1, 2, 3, 4)
print(my_tuple)

print()

# obtaining the minimum element using min()
a = min(my_tuple)
print(‘Minimum element is:', a)
Output:
(1, 2, 3, 4)

Minimum element is: 1

 

Deleting a tuple in Python:

We can delete a tuple in python by using the del() method. We need to pass the name of the tuple as a parameter to the del() method.

Syntax:
del(tuple_name)
The below code snippet shows the working of the del() method:
# creating a tuple
my_tuple = (1, 2, 3, 4)
print(my_tuple)

print()

# deleting a tuple using del()
del (my_tuple)
print(my_tuple)
Output:
NameError: name 'my_tuple' is not defined

 

This is the end of this article. Please refer to the articles called Lists in Python | Part 1 and Lists in Python | Part 2 to learn about lists in Python. The lists and tuples are very similar in Python.

I hope, from this article, you got some idea on tuples in Python. Also, you have learned how to get a tuple item value by index number, how to slice a tuple, and how to create a new tuple by adding two or more tuples.

Leave a Reply

Your email address will not be published.