TUPLE Data Structure in PYTHON is also an ordered collection of Python objects. The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by tuple class. We can represent tuples using parentheses ( ).
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.
Creation of TUPLE Data Structure in PYTHON without the use of parentheses is known as Tuple Packing.
tuple is the read only version of list
1) t=(10,20,30,40)
2) type(t) 3) <class ‘tuple’> 4) t[0]=100 5) TypeError: ‘tuple’ object does not support item assignment 6) >>> t.append(“durga”) 7) AttributeError: ‘tuple’ object has no attribute ‘append’ 8) >>> t.remove(10) 9) AttributeError: ‘tuple’ object has no attribute ‘remove’ |
1) Tuple is exactly same as List except that it is immutable. i.e once we creates Tuple object, we cannot perform any changes in that object.
2) Hence Tuple is Read only version of List.
3) If our data is fixed and never changes then we should go for Tuple.
4) Insertion Order is preserved
5) Duplicates are allowed
6) Heterogeneous objects are allowed.
7) We can preserve insertion order and we can differentiate duplicate objects by using index. Hence index will play very important role in Tuple also.
8) Tuple support both +ve and -ve index. +ve index means forward direction (from left to right) and -ve index means backward direction (from right to left)
9) We can represent Tuple elements within Parenthesis and with comma seperator.
10) Parenethesis are optional but recommended to use.
Accessing Elements of Tuple:
We can access either by index or by slice operator
1) By using Index:
1) t = (10, 20, 30, 40, 50, 60)
2) print(t[0]) à 10
3) print(t[-1]) à 60
4) print(t[100]) à IndexError: tuple index out of range
2) By using Slice Operator:
1) t=(10,20,30,40,50,60)
2) print(t[2:5])
3) print(t[2:100])
4) print(t[::2])
Output
(30, 40, 50)
(30, 40, 50, 60)
(10, 30, 50)
Important Functions of Tuple:
1) len()
To return number of elements present in the tuple.
Eg: t = (10,20,30,40)
print(len(t)) à 4
2) count()
To return number of occurrences of given element in the tuple
Eg: t = (10, 20, 10, 10, 20)
print(t.count(10)) à 3
3) index()
- Returns index of first occurrence of the given element.
- If the specified element is not available then we will get ValueError.
Eg: t = (10, 20, 10, 10, 20)
print(t.index(10)) à 0
print(t.index(30)) à ValueError: tuple.index(x): x not in tuple
4) sorted()
To sort elements based on default natural sorting order
1) t=(40,10,30,20)
2) t1=sorted(t)
3) print(t1)
4) print(t)
Output
[10, 20, 30, 40]
(40, 10, 30, 20)
5) min() And max() Functions:
These functions return min and max values according to default natural sorting order.
1) t = (40,10,30,20)
2) print(min(t)) à 10
3) print(max(t)) à 40
6) cmp():
It compares the elements of both tuples.
If both tuples are equal then returns 0
If the first tuple is less than second tuple then it returns -1
If the first tuple is greater than second tuple then it returns +1
1) t1=(10,20,30)
2) t2=(40,50,60) 3) t3=(10,20,30) 4) print(cmp(t1,t2)) à -1 5) print(cmp(t1,t3)) à 0 6) print(cmp(t2,t3)) à +1 |
Note: cmp() function is available only in Python2 but not in Python 3
This is all about TUPLE Data Structure in PYTHON.
Related Posts:
Python Tutorial – Learn Python
What is Python? What makes Python so Powerful?
Variables in Python – Constant, Global & Static Variables
Namespacing and Scopes in Python
Differences between List and Tuple
DICTIONARY Data Structure in PYTHON
Database Programming in Python
What is Multithreading in Python?
Python Exception Handling Using try, except and finally statement