Tuple in Python

In this tutorial, we will learn Tuple in Python. In the last tutorial, we learned about List. Tuples are same as Lists except for one difference that we cannot change elements of tuple after assigning them. In the case of the list we know we can change List elements very easily. It means you can say Tuple is a sequence of immutable Python objects. Like List in Tuple, we can add any number of elements and various kind of elements like(String, Integer, Float, List).

Creating a Tuple in Python:

In the list, we use square brackets to create List. But in the case of Tuple, we use parentheses. We can also create a Tuple without parentheses. Here we will learn about both ways to Create a Tuple in Python. Check this example program below.

# Creating a tuple
first_tuple = ()
# Output: ()
print(first_tuple)  
# Initializing Tuple
first_tuple = (9, 8, 7)
# Output: (9, 8, 7) 
print(first_tuple) 
# Tuple with mixed datatypes
first_tuple = (9, "Owlbuddy", 5.5)
# Output: (9, "owlbuddy", 5.5) 
print(first_tuple) 

Creating Tuple without parentheses:

# Creating a tuple without parentheses
first_tuple = 5, "owlbuddy",10.5
# Output: ()
print(first_tuple) 

In case you tuple have only one element then you have to put a comma at the end of the element to use it as a tuple check this example below.

first_tuple = ("owlbuddy")
print(type(first_tuple))  
# Creating a tuple
first_tuple = ("owlbuddy",)  
print(type(first_tuple))  
# Creating tuple without parentheses
first_tuple = "owlbuddy",
print(type(first_tuple)) 

Accessing elements from Tuple:

We can access elements of Tuple using an index number. Like List index number of Tuple elements start from 0. Here is an example to show how we can access elements of Tuple.

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y')  
# output: O
print(first_tuple[0]) 
# output: u 
print(first_tuple[4])  

Negative Indexing:

We can also use Negative indexing access elements of Tuple. Here is a picture which is revealing how negative indexing works in Tuple. The last element of Tuple can we access using -1 Negative Index and second last will have its negative index as -2 and so on for all the elements of Tuple in Python.

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y')  
# output: y
print(first_tuple[-1]) 
# output: u 
print(first_tuple[-4])  

Slicing of Tuple:

We can access a specific range of elements from Tuple using the colon(:) operator. Here is an example of understanding slicing.

# Creating a tuple
first_tuple = ('O','w','l','b','u','d','d','y') 
 
# output: ('l','b','b')
print(first_tuple[2:5]) 
# elements beginning to 2nd
# Output: ('O','w')
print(first_tuple[:-6])
# elements 5th to end
# Output: ('u','d','d','y')
print(first_tuple[4:])
# elements beginning to end
# Output: ('O','w','l','b','u','d','d','y')
print(first_tuple[:])   

Changing a Tuple:

We know Tuples are immutable. Mean we can not change elements after once they defined. But if elements of a Tuple are of mutable type like the list. It means we can change nested elements of Tuple. Here is an example of this.

first_tuple = (10, 5, 6, [8, 2])
# TypeError: 'tuple' object does not support item assignment
first_tuple[2] = 10
# But if we change item of mutable element it will not show error
first_tuple[3][0] = 12  
# Output: (10, 5, 6, [12, 2])
print(first_tuple)

Deleting a Tuple:

We know Tuples are immutable it means we can not change an element of tuple once they assigned. we also can not delete elements from the tuple. But there is a del keyword available in Python. We can use this del keyword to delete entire tuple. Check out this example.

first_tuple = ('O','w','l','b','u','d','d','y')
# we can not delete elements of Tuple it will show an error
del first_tuple[3]
# we can an delete an entire Tuple
del first_tuple
# NameError: name 'first_tuple' is not defined
print(first_tuple)

Use of + and * Operator in Tuple:

Like Lists, these two operators are also present in Tuple. we can use + operator to concatenate two Tuples and we can use * operator to repeat a Tuple a given number of times. Check this example

first_tuple=(5,6,7)
second_tuple=(8,9,10)
# Output: (5,6,7,8,9,10)
print(first_tuple+second_tuple)
# Output: ('owlbuddy', 'owlbuddy', 'owlbuddy')
print(("owlbuddy",) * 3)

Some other Tuple Methods:

Here are some methods which we can use while working with Tuples.

Method Description
count(x) Returns how many times a given element present in Tuple
index(x) Returns the first index of the given element in Tuple.
Spread the love
Scroll to Top