Python Menu

Tuple Sequence in Python


In Python Tuple is used to store the sequence of immutable python objects. Tuple is similar to lists since the value of the items stored in the list can be changed whereas the tuple is immutable and the value of the items stored in the tuple cannot be changed.

A tuple can be written as the collection of comma-separated values enclosed with the small brackets.


Syntax:
Tuple= (value1, value2…)

Example:
T1 = ()
T2 = (10, 30, 20, 40, 60)
T3 = ("C", "Java", "Python")
T4 = (501,"abc", 19.5)
T5 = (90,)
print(T1)
print(T2)
print(T3)
print(T4)
print(T5)
Output:
()
(10, 30, 20, 40, 60)
('C', 'Java', 'Python')
(501, 'abc', 19.5)
(90,)

Tuple indexing:

The indexing and slicing in tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length (tuple) - 1.

The items in the tuple can be accessed by using the slice operator. Python also allows us to use the colon operator to access multiple items in the tuple.

Tuple indexing

Unlike other languages, python provides us the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (right most) of the tuple has the index -1, its adjacent left element is present at the index -2 and so on until the left most elements is encountered.

Tuple Operators

Operator Description
+ It is known as concatenation operator used to concatenate two tuples
* It is known as repetition operator. It concatenates the multiple copies of the same tuple.
[ ] It is known as slice operator. It is used to access the item from tuple.
[ : ] It is known as range slice operator. It is used to access the range of items from tuple.
in It is known as membership operator. It returns if a particular item is present in the specified tuple.
not in It is also a membership operator and It returns true if a particular item is not present in the tuple.
Example:
num=(1,2,3,4,5)
lang=('python','c','java','php')
print(num+lang)
print(num*2)
print(lang[2])
print(lang[1:4])
print('cpp' in lang)
print(6 not in num)
Output:
(1, 2, 3, 4, 5, 'python', 'c', 'java', 'php')
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
java
('c', 'java', 'php')
True
True

How to add or remove elements to a list?

Unlike lists, the tuple items cannot be updated or deleted as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name.

Example:
mytuple=('python','c','java','php')
mytuple[3]="html"	
#'tuple' object does not support item assignment
print(mytuple)
del mytuple[3]		
# 'tuple' object doesn't support item deletion
print(mytuple)
del mytuple			
#deletes entire tuple
Output:
'tuple' object does not support item assignment
'tuple' object doesn't support item deletion

Tuple Functions

Python provides the following built-in functions which can be used with the tuples.

  • len()
  • max()
  • min()
  • tuple()
  • sum()
  • sorted()
  • index()
  • count()

☞ len()

In Python len() is used to find the length of tuple,i.e it returns the number of items in the tuple.

Syntax:
len(tuple)

Example:
num=(1,2,3,4,5,6)
print(“length of tuple :”,len(num))
Output:
length of tuple : 6

☞ max()

In Python max() is used to find maximum value in the tuple.

Syntax:
max(tuple)

Example:
num=(1,2,3,4,5,6)
lang=('java','c','python','cpp')
print("Max of tuple :",max(num))
print("Max of tuple :",max(lang))
Output:
Max of tuple : 6
Max of tuple : python

☞ min()

In Python min() is used to find minimum value in the tuple.

Syntax:
min(tuple)

Example:
num=(1,2,3,4,5,6)
lang=('java','c','python','cpp')
print("Min of tuple :",min(num))
print("Min of tuple :",min(lang))
Output:
Min of tuple: 1
Min of tuple : c

☞ sum()

In python, sum(tuple) function returns sum of all values in the tuple. Tuple values must in number type.

Syntax:
sum(tuple)

Example:
num=(1,2,3,4,5,6)
print(“sum of tuple items :”,sum(num))
Output:
sum of tuple items: 21

☞ sorted()

In python, sorted (tuple) function is used to sort all items of tuple in an ascending order. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the tuple into descending order.

Syntax:
sorted (tuple[,reverse=True])

Example:
num=(1,3,2,4,6,5)
lang=('java','c','python','cpp')
print(sorted(num))
print(sorted(lang))
print(sorted(num,reverse=True))
Output:
(1, 2, 3, 4, 5, 6)
('c', 'cpp', 'java', 'python')
(6, 5, 4, 3, 2, 1)

☞ tuple (sequence)

The tuple() method takes sequence types and converts them to tuples. This is used to convert a given string or list into tuple.

Syntax:
tuple(sequence)

Example:
str="python"
tuple1=tuple(str)
print(tuple1)
num=[1,2,3,4,5,6]
tuple2=tuple(num)
print(tuple2)
Output:
('p', 'y', 't', 'h', 'o', 'n')
(1, 2, 3, 4, 5, 6)

☞ count()

In python count() method returns the number of times element appears in the tuple. If the element is not present in the tuple, it returns 0.

Syntax:
tuple.count (item)

Example:
num=(1,2,3,4,3,2,2,1,3,4,5,7,8)
cnt=num.count(2)	
print("Count of 2 is:",cnt)
cnt=num.count(10)	
print("Count of 10 is:",cnt)
Output:
Count of 2 is: 3
Count of 10 is : 0

☞ index()

In python index () method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If tuple contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.

Syntax:
tuple.index(x[, start[, end]])

Example:
lang = ('p', 'y', 't', 'h', 'o','n','p','r','o','g','r','a','m') 
print("index of t is:",lang.index('t'))
print("index of p is:",lang.index('p'))
print("index of p is:",lang.index('p',3,10))
print("index of p is:",lang.index('z'))
Output:
index of t is: 2
index of p is: 0
index of p is: 6
ValueError: 'z' is not in tuple.

Next Topic :Set sequence in Python