Tuple in Python

In this easy tutorial, we will learn about Tuple in Python. Here, we will discuss how to create, access, slice, insert, update and delete elements from Python Tuple with example.

A Tuple in Python is a sequence of different objects, different objects mean different types of data can be store in a Tuple. The Tuple in Python is similar to a List but Tuples are immutable type so, we can’t change the value of a tuple.

How to Create Tuple

To create a tuple in Python, you need places all the items inside round brackets ( ) separated by comma(,).

#Empty Tuple
n=()

#Tuple with numbers
n=(11, 22, 33, 44, 55)

#Tuple with Text
n=("Red", "Green", "Yellow")

#Tuple with mixed objects
n=(11, 22, "Hello", [2,4,6])

A Tuple with Single Element

A Tuple with single element must end with a comma (,), whether it is inside or outside round brackets.

#without comma it will treated as a normal object.
X=(11)
Y=11
print(type(X))
print(type(Y))

#create a tuple with single element.
X=(11,)
Y=11,
print(type(X))
print(type(Y))
#must be end with comma

Access Element of the Tuple

Elements of the tuple are assigned a unique index number, starting from 0. Negative indexing is also possible in Tuple, the index of the last item is -1. We can access the element of the tuple by referring index number in square [ ] brackets.

#creating a tuple
n=(11, 22, 33, 44, 55)

#print all items of the tuple
print(n)

#print first item
print(n[0])

#print third item
print(n[2])

#print last item
print(n[-1])

#print second last item
print(n[-2])

Slicing Python’s Tuple

Slicing is also possible in the tuple to access sub-items. For slicing the tuple we have to use colon ( : ) operator. Syntax for slicing [start : stop : step].

#create a tuple
n=(11, 22, 33, 44, 55)

#print all the elements
print(n)
print(n[:])
 
#print first to third element (index 0 to 2)
print(n[0:3])
 
#print element of index 1 to 2
print(n[1:3])
 
#print element of index 2 to last
print(n[2:])
 
#print element of index 1 to second last
print(n[1:-1])
 
#print value of index (0,2,4)
print(n[0:5:2])

Loop Through a Tuple

We can loop through a tuple using a for a loop.

#create a tuple
num=(11, 22, 33, 44, 55)
for i in num:
    print(i)
    
#all the item of the tuple will assign to variable i, until the last item.

Update Tuple in Python

As we know tuples are immutable, so we can’t update or change elements of the tuple. However, if the elements of the tuple are mutable than we can edit the elements or we can create new tuple based upon existing two tuples.

#creating a tuple
n=(11, 22, 33, 44, 55)
#n[0]=10     #error, we can't change


#-----------------------------------
n=(11, 22, 33, [1, 2])
#item of the mutable element can be changed.
n[3][0]=99
print(n)
#output : (11, 22, 33, [99, 2])


#-----------------------------------
t1=(1, 2, 3)    #tuple 1
t2=(99, 88, 77)     #tuple 2

#we can concatenate two tuples using + operator
t3 = t1 + t2
print(t3)
#output : (1, 2, 3, 99, 88, 77)

#-----------------------------------
n=(11, 22, 33, 44, 55)
#tuple can be reassign
n=(1, 2, 3, 4, 5)
print(n)
#output : (1, 2, 3, 4, 5)

Delete a Tuple

As we discussed, a tuple is immutable, it means we can’t remove an item from the tuple. But we can delete entire tuple using the del keyword.

n=(11, 22, 33, 44, 55)
print(n)

del n
print(n)
# error, because tuple is deleted

Useful Functions of the Tuple

  • count() – Return count of an item in the tuple.
  • index() – It returns the index of the first matching item.
n=(11, 22, 11, 88, 33, 11, 55)
print(n)

#count an item in the list
print(n.count(11))

#return index of 33 in the list
print(n.index(33))

112
Created on By S. Shekhar

Python – Quiz 14

1 / 4

Which of the following is not true?

2 / 4

For what purpose is slicing used in a tuple?

3 / 4

Which of the following is true for tuple?

4 / 4

n=(11, 22, 33, 22). index(22) = ?

Your score is

The average score is 40%

0%