Tuples in Python

94

The tuple in Python is a collection of items stored in a single variable. It is just like a list in python, but in the list, we have different memory locations which store the different values, but here, we have a single variable that stores a collection of items. It is a collection that is ordered and unchangeable.

Tuples in Python with syntax and example
Tuples in Python with syntax and example

There are plenty of differences between list and tuples which are given below –

S.No Key List Tuple
1 Type List is mutable Tuple is immutable
2 Iteration List iteration is slower and is time consuming Tuple iteration is faster
3 Appropriate for Useful for insertion and deletion of elements Useful for read-only operations like accessing elements.
4 Memory Consumption Consumer more memory Consumes less memory as compared to list
5 Methods Provides many in-built functions Provides fewer in-built functions
6 Error prone List operations are more error prone Less error prone and safe

How to create a Tuple in Python?

Tuple is created by writing all the items inside the parenthesis and two different items separated by commas. However, the parenthesis is optional to use. A tuple may have different items of the same data type or they may differ from each other.

Different types of tuples are given in the example given below –

# Different types of tuples

# Empty tuple

my_tuple = ()
print(my_tuple)

 

# Tuple having integers

my_tuple = (1, 2, 3)
print(my_tuple)

 

# tuple with mixed datatypes

my_tuple = (1, "Rahul", 6.6)
print(my_tuple)

 

# nested tuple

my_tuple = ("rahul", [5, 9, 11], (1, 2, 3))
print(my_tuple) 

Output –

()
(1, 2, 3)
(1, 'Rahul', 6.6)
('rahul', [5, 9, 11], (1, 2, 3))

Accessing elements in a Tuple –

Indexing

 It is a technique to access an element with the help of the index at which it is assigned.

Example –

my_tuple = ('h','a','r','s','h','i', 't')
print(my_tuple[0])
print(my_tuple[5])

Output –

h
i

Also, we can access the elements with the help of negative indexing, which is used in python.

Example –

my_tuple = ('h', 'a', 'r', 's', 'h', 'i', 't')
print(my_tuple[-1])
print(my_tuple[-6])

Output –

t
a

Slicing – In this, we use the slicing operator to get a range of elements from a tuple.

Example –

my_tuple = ('y','u','v','a','y','a','n','a')
print(my_tuple[1:4])

Output –

u
v
a

Deleting a Tuple –

It is not possible to delete or alter a specific value at a tuple. But, however, it is possible to delete the whole tuple.

‘del’ keyword is provided for this purpose.

Example –

my_tuple = ('y','u','v','a','y','a','n','a')
del my_tuple
print(my_tuple)

Output –

Traceback (most recent call last):

File “<string>”, line 12, in <module>

NameError: name ‘my_tuple’ is not defined

Iterating through a tuple –

We can use a for loop to iterate over the tuples in Python

Example –

for name in ('John', 'Addams'):

print("Hello", name)

Output –

Hello John
Hello Kate

So, in this article, we had learn about one more useful data structure provided by python to its users. It has many advantages over the conventional ‘list’ data type, which are discussed above in the table containing differences between them.

Try these Quizzes:

Introduction to Python Quiz – 1

Object Types in Python Quiz with answers

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.