Python Tuple

Here, we have discussed about what is python tuple, how to create python tuple, accessing tuple elements, tuple slicing, changing tuple elements.....

In this article, you will about what is python tuple, how to create python tuple, accessing tuple elements, tuple slicing, changing tuple elements, deleting tuple elements and tuple methods.

python tuple, how to create python tuple, accessing tuple elements

What is Python Tuple?

Tuples are python data types which we can use to store objects or elements in a single variable. Tuples are similar to python lists as it contains multiple elements which are separated by the commas (,) but the tuples are immutable which means we can't alter them after creating them.

Create Python Tuple

In python, we can create tuple just by writing all the elements/items within the parenthesis or small brackets separated by commas (,). It is optional to use parentheses but it is helpful in practice.

# Python Empty tuple

tup = ()

print(tup)

# Python Tuple having integers

tup = (14, 9, 0)

print(tup)

# Python tuple with mixed datatypes

tup = (2, "Answersjet", 6.4)

print(tup)

# Python nested tuple

tup = ("Python", [14, 9, 2], (40, 8, 70))

print(tup)

Output

()

(14, 9, 0)

(2, 'Answersjet', 6.4)

('Python', [14, 9, 2], (40, 8, 70))

Similar to list, a tuple also contains strings, integers, objects, floats, etc. We can create a tuple without using the parentheses which is known as tuple packing.

tup = 14, 6.9, "Answersjet"

print(tup)

# Python tuple unpacking

x, y, z = tup

print(x)

print(y) 

print(z)

Output

(14, 6.9, 'Answersjet')

14

6.9

Answersjet

We can also create a tuple with only single element but it is slightly tricky. Writing only one element within the parentheses is not enough, so to proof that it is a tuple, we have to a comma after writing the element.

tup = ("Answersjet")

print(type(tup)) 

# Creating a Python tuple containing one element

tup = ("Answersjet",)

print(type(tup)) 

# Optional: Parentheses

tup = "Answersjet",

print(type(tup))

Output

<class 'str'>

<class 'tuple'>

<class 'tuple'>

Accessing Tuple Elements 

Python allows us to access the elements of a tuple in various ways that are explained as follows;

Tuple Indexing 

Similar to python list, we can access the elements of a tuple by using the index operator . The index starts from 0 which means if we have a tuple of 6 elements then the index will be from 0 to 5. And we can't use floats or other objects in the index, it must be integers only. If we use floats or other objects then it will result into . And in order to index nested tuple, nested indexing is used.

# Python tuple of strings

tup = ('p', 'y', 't', 'h', 'o', 'n')

# displaying all elements

print(tup)

# accessing first element

# prints 'p'

print(tup[0])

# accessing third element

# prints 't'

print(tup[2])

Output

('p', 'y', 't', 'h', 'o', 'n')

p

t

Negative Indexing

We can also access the elements of a tuple by using the negative indexing. The last item is denoted by the index -1 and the second last item is represented by the index -2 and so on.

tup = (14, 9, 'Answersjet', 6.9)

# accessing last element

# prints 6.9

print(tup[-1])

# prints 9

print(tup[-3])

Output

6.9
9

Tuple Slicing

In python, we can also access a specified range of elements in the tuple. To perform tuple slicing, the slicing operator colon [:] is used.

# Accessing python tuple elements using slicing

tup = ('A','n','s','w','e','r','s','j','e','t')

print(tup[1:4])

print(tup[:-7])

print(tup[7:])

print(tup[:])

Output

('n', 's', 'w')

('A', 'n', 's')

('j', 'e', 't')

('A', 'n', 's', 'w', 'e', 'r', 's', 'j', 'e', 't')

Changing Tuple Elements

As we have learnt that python tuples are immutable so we can't make changes or alter a tuple after creating it. But to create new tuple, we can use the portion of the existing tuples. And we can change the nested element, if they are written inside an element which itself is mutable data type.

tup = (12, [14, 9, 8], 'Answersjet')

print(tup)

# changing the element of the list

# this is valid because list is mutable

tup[1][2] = 106

print(tup)

# changing the element of tuple

# This is not valid since tuple elements are immutable

# TypeError: 'tuple' object does not support item assignment

Output

(12, [14, 9, 8], 'Answersjet')
(12, [14, 9, 106], 'Answersjet')

Deleting Tuple Elements

As we know tuples are immutable so that we can't delete the elements of a tuple by using the python delete statement. So to delete the entire tuple we have to use the del keyword with the name of the tuple.

# Deleting  Python tuples

tup = ('a', 'n', 's', 'w', 'e', 'r', 's', 'j', 'e', 't')

# can't delete items

# TypeError: 'tuple' object doesn't support item deletion

# del tup[3]

# Can delete an entire tuple

del tup

# NameError: name 'tup' is not defined

print(tup)

Output

Traceback (most recent call last):

  File "<string>", line 12, in <module>

NameError: name 'tup' is not defined

Python Tuple Methods 

In tuple, there are only two built-in methods are available. These methods are explained in the following table;

S.NoMethod Description
1. index() In the tuple, index of the elements are returned using this method.
2. count() To return the count of numbers of time an element appear in the tuple.

Now let's understand these two Python tuple built-in methods with the help of following example:

tup = ('a', 'n', 's', 'w', 'e', 'r')

print(tup.count('s')) 

print(tup.index('e')) 

Output

1
4

Conclusion

Above we have discussed about what is python tuple, how to create python tuple, accessing tuple elements, tuple slicing, changing tuple elements, deleting tuple elements and tuple methods. Tuples are python data types which we can use to store objects or elements in a single variable. We can create tuple just by writing all the elements/items within the parenthesis or small brackets () separated by commas (,).