Python Tuples with Example – Python Tutorial

2 weeks ago codedestine 0

In Python tuple are a immutable sequence of objects just like a list. The difference between list and tuple is that data in the list can be changed while data in the tuple can not be changed. Tuples are used to store collection of data such as months in a year, which should not be change.

In this tutorial, we will learn about the following:

  1. Creating Tuple
  2. Tuple Indexing
  3. Tuple Slicing
  4. Tuple Packing & Unpacking
  5. Tuple Immutability
  6. Tuple Deletion

Python Tuple

Creating Tuple

In Python tuple are created by putting, sequence of elements separated by commas inside parentheses “( )” or without it. For example:

# creating tuple with same data type with parentheses 
 t = (1,2,3,4,5) 

# creating tuple with same data type without parentheses
 t = 1,2,3,4,5 

# creating tuple with different data type with parentheses
 t = (1,'Jan',2.0,True) 

# creating tuple with different data type without parentheses
 t = 1,'Jan',2.0,True

# creating empty tuple
 t = () 

# creating tuple with single element with parentheses
 t = (1,) 

# creating tuple with single element without parentheses
 t = 1,

Tuple Indexing

In python tuple are a sequence just like a string and a list and similar to string and a list, we can use indexes to access individual element of the tuple. Index starts from 0 and brackets [] are used after an object variable to access the element. For example:

# create tuple object and assign it to variable t t = ('Jan','Feb','Mar') # accessing element at index 0 print(t[0]) # accessing element at index 1 print(t[1]) # accessing last element of the tuple print(t[2]) # another way of accessing last element of the tuple print(t[-1])

Like string, negative numbers can also be used for indexing. Indexing diagram for tuple (‘Jan’,’Feb’,’Mar’) as follows: 

 Jan   Feb   Mar
 0     1     2
-3    -2    -1

Tuple Slicing

In python tuple can be sliced by using colon “:” operator inside brackets “[]“. 

        Syntax :  [ start index : end index : step ]
                      start index : slice of the given tuple will starts from this index, including element at start index
                      end index : slice of the given tuple will end at this index, excluding element at end index
                      step : it is a frequency of grabbing elements from the given tuple

For example :

# create tuple object and assign it to variable t t = ('Jan','Feb','Mar') print(t[1:]) # This prints a slice of the tuple, starting from index 1 and ending at last index print(t[:1]) # This prints a slice of the tuple, starting from index 0 and ending at index 1 print(t[1:2]) # This prints a slice of the tuple, starting from index 1 and ending at index 2 print(t[::2]) # This prints a slice of the tuple, starting from index 0 and ending at last index and grab every alternate element only # print complete tuple in reverse print(t[::-1])

Tuple Packing & Unpacking

In python tuple are said to be packed when we create a new tuple object and assign it to a variable and tuple are said to be unpacked when we extract each tuple item and assign it to a variable. For example:

# tuple packing - create tuple object and assign it to variable t t = ('Jan','Feb','Mar') # tuple unpacking (month1, month2, month3) = t # print all three variables print(month1) print(month2) print(month3)

Tuple Immutability

In python tuple are immutable, which means that once the tuple is created, none of the element in a tuple can be change or remove. For example:

# create tuple object and assign it to variable t t = ('Jan','Feb','Mar') # change the first element to 'Dec' t[0] = 'Dec' # click run button to see the result

Tuple Deletion

We can use del statement to delete tuple object entirely. For example:

# create tuple object and assign it to variable t t = ('Jan','Feb','Mar') # print tuple t print(t) # delete tuple t del t # print tuple t print(t)

References :-

  1. Tuples Docs

That’s all for Python Tuple with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Next