Tuple Data Type in Python
Tuples in Python are an ordered collection of different data types.The items in the tuples are separated by commas (,).
- Tuples are immutable objects.
- Tuples are iterable objects.
- Tuples in Python can be indexed/subscripted.
- In forward indexing, the first element of tuple has index 0.
- In backward indexing, the last element of tuple has index –
Data Visualize
Create empty tuple:
Python 3.8.4 (tags/v3.8.2:7b3ddb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = () >>> print(data) () >>> print(type(data))>>> print(len(data)) 0
Create non-empty tuple:
This way to create non-tuple in Python
Python 3.8.4 (tags/v3.8.2:7b3ddb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> roll = (101,) >>> print(roll) (101,) >>> print(type(roll))>>> data = (101,'ankita',95.6,('P','C','M'),[96,91,93]) >>> print(data) (101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]) >>> print(type(data)) >>> print(len(data)) 5
Tuple Indexing:
Used to select the character at a particular index/position.
Syntax: object[index]
Backward Indexing | -5 | -4 | -3 | -2 | -1 |
101 | ankita’ | 95.6 | (‘P’,’C’,’M’) | [96, 91, 93] | |
Forward Indexing | 0 | 1 | 2 | 3 | 4 |
Python 3.8.4 (tags/v3.8.2:7b3ddb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = (101,'ankita',95.6,('P','C','M'),[96,91,93]) >>> print(data) (101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]) >>> print(data[3]) ('P', 'C', 'M') >>> print(data[-1]) [96, 91, 93]
Tuple Index slicing:
Process to select a subset of an entire tuple, Index slicing return view of original tuple.
Syntax: object[start : stop : stride(step)]
start (optional) | starting index value, the default is 0(in fwd indexing) |
stop (optional) | ending index value, is not included in the generated sequence, default is len(object)(in fwd indexing) |
step (optional) | the difference between each item in the sequence, the default is 1(in fwd indexing) |
Python 3.8.4 (tags/v3.8.2:7b3vb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = (101,'ankita',95.6,('P','C','M'),[96,91,93]) >>> print(data) (101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93]) >>> print(id(data)) 2300730249680 >>> print(data[2:4]) (95.6, ('P', 'C', 'M')) >>> print(id(data[2:4]) 2300730249680 >>> print(data[-4:-1]) ('ankita', 95.6, ('P', 'C', 'M')) >>> print(data[-1::-1]) ([96, 91, 93], ('P', 'C', 'M'), 95.6, 'ankita', 101)
Data Insertion
Insertion Tuple in run time is not possible because Tuple is immutable. we can just create the tuple as defined.
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> t1=(1,2,3,4,"Hello",True) >>> type(t1)>>> print(t1) (1, 2, 3, 4, 'Hello', True) >>>
Data Deletion
Deletions in tuples are not allowed, just we can use del command to delete the whole tuple.
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> t1=(1,2,3,4,5,"hello",34) >>> t1.remove(2) Traceback (most recent call last): File "", line 1, in AttributeError: 'tuple' object has no attribute 'remove' >>> t1.clear() Traceback (most recent call last): File " ", line 1, in AttributeError: 'tuple' object has no attribute 'clear' >>> del t1 >>> print(t1) Traceback (most recent call last): File " ", line 1, in NameError: name 't1' is not defined >>>
Tuple Operations:
Operator | Description |
+ | Used to join two tuples, known as tuple concatenation. |
* | Used to repeat the elements of tuple, known as tuple replication. |
in not in | Used to check the value is exist or not, |
is is not | Used to check the object address . |
Python 3.8.4 (tags/v3.8.2:7b3ds59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = (101,'ankita',95.6,('P','C','M'),[96,91,93]) >>> personal = (16,'F','Delhi') >>> all = data + personal >>> print(all) (101, 'ankita', 95.6, ('P', 'C', 'M'), [96, 91, 93], 16, 'F', 'Delhi') >>> print(personal*2) (16, 'F', 'Delhi', 16, 'F', 'Delhi') >>> print('ankit' in data) False >>> print([96,91,93] in data) True >>> print('ankit' not in data) True >>> own = (16,'F','Delhi') >>> print(personal is own) False >>> print(personal is not own) True
Python Tuple Methods:
Function | Description |
index(value, start=0, stop=len(object)) |
|
count(value) | Return number of occurence of the value in tuple. |
Python 3.8.4 (tags/v3.8.2:7b3cc59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = (101,'ankita',95.6,('P','C','M'),[96,91,93]) >>> print(data.index(95.6)) 2 >>> print(data.count(95.6)) 1
Built-in functions on Tuple:
Function | Description |
min(iterable, key=func) | Return the smallest number of the given list.(only with int or float number) |
max(iterable, key=func) | Returns the biggest number of the given list.(only with int or float number) |
sum(iterable, start=0) | Returns the sum of the given list.(only with int or float number) |
sorted((iterable, key=None, reverse=False) | Returns the ascending sorted arrays .(only with int or float number) |
Python 3.8.4 (tags/v3.8.2:7b3acdb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> marks = [68,92,78,88,65] >>> print(min(marks)) 65 >>> print(max(marks)) 92 >>> print(sum(marks)) 391 >>> print(sorted(marks)) [65, 68, 78, 88, 92] >>> print(sorted(marks, reverse=True)) [92, 88, 78, 68, 65] >>> subject = ['Phy','Chem','Maths','Eng','Hindi'] >>> print(sorted(marks, key=lambda marks:subject)) [68, 92, 78, 88, 65]
For further reference about lambda please refer this link “https://salesforcedrillers.com/learn-python/lambda-functions/“