Skip to content

Python Tuples




Python – Tuples


As we read earlier about Python CRUD Operation. Now we will study about tuples in python. In python programming language , this is our second collection of data type. In this, we will cover what is a tuple in python, python create tuple, python access tuple, python tuple example, tuple constructor python etc. so let’s start reading about the second collection data type.





What is a Tuple in python ?

Tuple is a collection or sequence of heterogeneous datatype. Tuple is immutable datatype i.e. they cannot be edited or modified. In python programming Tuples are written with Round brackets or Parentheses.



General syntax for tuple :

Tuple variable = (comma separated elements)



Python tuple example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue") 
print(tuple_1)



Output :

(5, 'Black', 3.5, 'White', 75, 'Blue')




How to access the tuple items from a tuple ?

We can access the tuple items by using the index number.



Example :

           0     1      2      3     4     5
           ↓     ↓      ↓      ↓     ↓     ↓
tuple_1 = (5, "Black", 3.5, "White", 75, "Blue")
print(tuple_1[3])



Output :

White




How to access the tuple items by using Negative Indexing ?

Negative indexing means beginning from the end i.e. – 1 refers to the last item, – 2 refers to the second last item,  3 refers to the third last item.



Example :

          -6    -5     -4     -3    -2    -1
           ↓     ↓      ↓      ↓     ↓     ↓
tuple_1 = (5, "Black", 3.5, "White", 75, "Blue")
print(tuple_1[-3])



Output :

White




How to access the tuple items by using Range of Indexes ?

Negative indexing means beginning from the end i.e. – 1 refers to the last item, – 2 refers to the second last item,  3 refers to the third last item.



Example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue")
print(tuple_1[1:5])



Output :

('Black', 3.5, 'White', 75)




How to access the tuple items by using Range of Negative Indexing ?



Example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue")
print(tuple_1[-4:-1])



Output :

(3.5, 'White', 75)




CRUD for Tuples

1. CREATE : once a tuple is created, we cannot append, insert and extend items to it because tuples are immutable.

2. Read : once a tuple is created, we can read or search element data from the tuples with the help of index , _ _contains_ _ , count. we will study it further now. 

3. Update : once a tuple is created, we cannot update or modify the items in a tuple because tuples are immutable.

4. Delete : once a tuple is created, we cannot remove items in a tuple with the help of remove , pop , clear . But you can delete the tuple completely by using del( ) function.




R – READ

Let’s study our second operation of CRUD which is ” R ” for read which means either to read data or to search data from the tuples.

Here are some functions of READ :-

  • index

  • _ _contains_ _

  • count

Let us create a tuple first.

tuple_1 = (1,3,2,1,5,7,1,8)



⇒ index :

Index functions search element into tuple, if found returns index positions of element, otherwise returns error.



General syntax for index( ) :

tuple.index(self, element, start, end)



Example :

tuple_1 = (1,3,2,1,5,7,1,8)
i = tuple_1.index(2)
print(i)



Output :

2




⇒ _ _contains_ _ :

_ _contains_ _( ) function which will return boolean value. True means the element exists and False means there is no such element in the tuple.



General syntax for _ _contains_ _( ) :

tuple._ _contains_ _(self, element)



Example :

tuple_1 = (1,3,2,1,5,7,1,8)
i=tuple_1.__contains__(7)
print(i)



Output :

True




⇒ count( ) :

count( ) function which will return you the count of the element that you pass in it. count( ) function always return integer data type.



General syntax for count ( ) :

tuple.count(self, element)



Example :

tuple_1 = (1,3,2,1,5,7,1,8)
i=tuple_1.count(1)
print(i)



Output :

3

To learn CRUD Operation in detail : Click here





How to change the values of a tuple ?

once a tuple is created, you cannot change its values because tuples are immutable i.e. they cannot be edited or modified.

If you want to change the tuple then you can convert the tuple into a list, changes the list, and again convert the list back into a tuple.



Example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue") 
tuple_2 = list(tuple_1) 
tuple_2[3] = "Red" 
tuple_1 = tuple(tuple_2) 
print(tuple_1)



Output :

(5, 'Black', 3.5, 'Red', 75, 'Blue')

⇒ Once a tuple is created, you cannot add items to it because tuples are Immutable.

⇒  Once a tuple is created, you cannot remove items in a tuple because tuples are Immutable. But you can delete the tuple completely by using del( ) function.





Loop through a tuple

you can print all the tuple items by using the “ For ” loop.



Example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue") 
for i in tuple_1: 
    print(i)



Output :

5 
Black 
3.5 
White 
75 
Blue

Learn loops in detail : https://codifyshow.com/python-loop





How to check the length of a given tuple ?

To check how many items a tuple has, by using the len( ) function.



Example :

tuple_1 = (5, "Black", 3.5, "White", 75, "Blue")
print(len(tuple_1))



Output :

6




Join two tuples

To join or concatenate two or more tuples in python by using + operator. 



Example :

tuple_1 = ("Black","White","Blue") 
tuple_2 = (5,3.5,75) 
tuple_3 = tuple_1 + tuple_2 
print(tuple_3)



Output :

('Black', 'White', 'Blue', 5, 3.5, 75)




tuple( ) constructor python

There is an alternative to make a new tuple by using the tuple( ) constructor.



Example :

tuple_1 = tuple((5, "Black", 3.5, "White", 75, "Blue"))
print(tuple_1)



Output :

(5, 'Black', 3.5, 'White', 75, 'Blue')



>