Tuples in Python

A tuple is a sequence of immutable objects, immutable means values inside tuple cannot be changed. It can be used to collect different types of object.

The objects are enclosed within parenthesis and separated by comma.

Tuple is similar to list. Only the difference is that list is enclosed between square bracket, tuple between parenthesis and List has mutable objects whereas Tuple has immutable objects.

Python Tuple Example

tuples in python

There can be an empty Tuple also which contains no object. As you can also see in example of empty tuple named tup1

Python Empty Tuple Example

  • tuple1=()  

Python Single Object Tuple Example

For a single valued tuple, there must be a comma at the end of the value.

  • Tuple1=(10)  

Python Tuple of Tuples Example

Tuples can also be nested, it means we can pass tuple as an element to create a new tuple. See, the following example in which we have created a tuple that contains tuples an the object.

tuples and sets in python

Accessing Tuple

Accessing of tuple is pretty easy, we can access tuple in the same way as List.  

Python Tuple Operations

Python allows us to perform various operations on the tuple. Following are the common tuple operations.

Adding Tuples Example

Tuple can be added by using the concatenation operator(+) to join two tuples.

Adding tuple

Note: The new sequence formed is a new Tuple.

Replicating Tuple Example

Replicating means repeating. It can be performed by using ‘*’ operator by a specific number of time.

  1. tuple1=(10,20,30);  
  2. tuple2=(40,50,60);  
  3. print tuple1*2  
  4. print tuple2*3  


(10, 20, 30, 10, 20, 30)
(40, 50, 60, 40, 50, 60, 40, 50, 60)

Python Tuple other Operations

Creating Tuple from Existing Example

We can create a new tuple by assigning the existing tuple, see the following example.An example of this is also shown by us in a screenshot used above where we have written tup1=tup2 and another example is shown bellow,

  1. data1=(10,20,30)  
  2. data2=(40,50,60)  
  3. data3=data1+data2  
  4. print data3  


(10, 20, 30, 40, 50, 60)

Python Tuple Deleting Example

Deleting individual element from a tuple is not supported. However the whole of the tuple can be deleted using the del statement.

tuple in python

  • data=(10,20,‘rahul’,40.6,‘z’)  
  • print data  
  • del data      #will delete the tuple data  
  • print data  #will show an error since tuple data is already deleted  


(10, 20, 'rahul', 40.6, 'z')
Traceback (most recent call last):
  	File "C:/Python27/t.py", line 4, in 
    	print data
NameError: name 'data' is not defined

We have shown you two examples hopefully you will understand better.

Functions of Tuple

There are following in-built Type Functions

min(tuple)It returns the minimum value from a tuple.
max(tuple)It returns the maximum value from the tuple.
len(tuple)It gives the length of a tuple
tuple(sequence)It converts the sequence into tuple.

Python Tuple min(tuple) Method Example

This method is used to get min value from the sequence of tuple.

  • data=(10,20,‘rahul’,40.6,‘z’)  
  • print min(data)  



Python Tuple max(tuple) Method Example

This method is used to get max value from the sequence of tuple.

  • data=(10,20,‘rahul’,40.6,‘z’)  
  • print max(data)  



Python Tuple len(tuple) Method Example

This method is used to get length of the tuple.

  1. data=(10,20,‘rahul’,40.6,‘z’)  
  2. print len(data)  



4) tuple(sequence):


  1. dat=[10,20,30,40]  
  2. data=tuple(dat)  
  3. print data  


(10, 20, 30, 40)

Why should wee use Tuple? (Advantages of Tuple)

  1. Processing of Tuples are faster than Lists.
  2. It makes the data safe as Tuples are immutable and hence cannot be changed.
  3. Tuples are used for String formatting.

Set In Python

  • Set in python are similar as they are in mathematics(Set is a collection of elements).
  • Set in python is the collection of unique elements.
  • Set never follows any sequence,(hence they cannot be indexed)
  • Set is represented by { } curly braces
  • Faster then Tuples




set in python

in this example we have shown you how to use set in python, and if  we have repeated any element then it store it once only, plus it’s element’s can’t be indexed still you have a nu,bers of function’s which you can use in set