Python Tuples
- Tuple is immutable.
- It is declared with parenthesis ().
- It is an ordered sequence of items same as a list.
- They are similar to a list. The only difference between the two is that we can’t change elements of Tuple once it is assigned whereas, in a list, elements can be changed.
- While giving a string as input, the comma after it is required or else it will become a string type instead of a Tuple.
Table Of Contents
Tuple Creation
01. Example
Tuple consists Number of Elements
tuple01 = (10,20,30) print("Number Tuple: ",tuple01)
Output:
Number Tuple: (10, 20, 30)
02. Example
Tuple consists of String Elements
tuple02 = ('Python', 'Java', 'Cpp') print("String Tuple: ",tuple02)
Output:
String Tuple: ('Python', 'Java', 'Cpp')
03. Example
Tuple consists of Mixed Data Type Elements
tuple03 = ('zeroones' , 500, 'Python') print("Mixed Tuple: ", tuple03)
Output:
Mixed Tuple: ('zeroones', 500, 'Python')
04. Example
Tuple Consist Tuples(Nested Tuple)
tuple04 = (100,(10, 20, 30),('Python', 'Java', 'Cpp'),('zeroones',500 , 'Python')) print("Nested Tuple: ",tuple04)
Output:
tuple04 = (100,(10, 20, 30),('Python', 'Java', 'Cpp'),('zeroones',500 , 'Python')) print("Nested Tuple: ",tuple04)
NOTE: While giving a string as input, the comma after it is required or else it will become a string type instead of a Tuple.
01. Example
Type Tuple.
tuple01 = ('python','Java') print("Type Of Tuple: ", type(tuple01))
Output:
Type Of Tuple: <class 'tuple'>
02. Example
Type Tuple.
tuple02 = ('python',) print("Type Of Tuple: ", type(tuple02))
Output:
Type Of Tuple: <class 'tuple'>
03. Example
Type String.
tuple03 = ('python') print("Type Of Tuple: ", type(tuple03))
Output:
Type Of Tuple: <class 'str'>
Accessing Tuple Elements Using Index
01. Example
First Element Of Tuple.
tuple01 = ('Python','Java','Cpp') print("First Element Of Tuple: ", tuple01[0])
Output:
First Element Of Tuple: Python
02. Example
Last Element Of Tuple.
tuple01 = ('Python','Java','Cpp') print("Last Element Of Tuple: ", tuple01[-1])
Output:
Last Element Of Tuple: Cpp
03. Example
First Element Of Tuple.
tuple02 = (10,20,30) print("First Element Of Tuple: ", tuple02[0])
Output:
First Element Of Tuple: 10
04. Example
Last Element Of Tuple.
tuple02 = (10,20,30) print("Last Element Of Tuple: ", tuple02[-1])
Output:
Last Element Of Tuple: 30
Accessing Tuple Elements Using Slicing
01. Example
Print Tuple elements from index 1 to 3 excluding the 4th index.
tuple01 = ('Python','Java','Cpp','JavaScript','Html','Css') print("Tuple Output: ", tuple01[1:4])
Output:
Tuple Output: ('Java', 'Cpp', 'JavaScript')
02. Example
Print tuple elements from the start index to till the 3rd last element.
tuple01 = ('Python','Java','Cpp','JavaScript','Html','Css') print("Tuple Output: ", tuple01[:-3])
Output:
Tuple Output: ('Python', 'Java', 'Cpp')
03. Example
Print all Elements Of Tuple.
tuple01 = ('Python','Java','Cpp','JavaScript','Html','Css') print("Tuple Output: ", tuple01[:])
Output:
Tuple Output: ('Python', 'Java', 'Cpp', 'JavaScript', 'Html', 'Css')
Changing Tuple Elements
- Tuples are immutable . But if a tuple has some element that is a mutable data type, it can be changed.
01. Example
Error: ‘tuple’ object does not support item assignment
tuple01 = ('zeroones',['Python','Java','Cpp','JavaScript','Verilog','Vhdl']) print('Tuple Output: ',tuple01[0]) tuple01[0] = 'zeroones.org'
Output:
Tuple Output: zeroones Traceback (most recent call last): File "<string>", line 5, in <module> TypeError: 'tuple' object does not support item assignment
02. Example
Tuple has mutable data type below, it can be changed.
tuple01 = ('zeroones',['Python','Java','Cpp','JavaScript','Verilog']) print('Tuple Output Before Change:n',tuple01[1]) tuple01[1][-2] = 'Vhdl' print('nTuple Output After Change: n',tuple01[1])
Output:
Tuple Output Before Change: ['Python', 'Java', 'Cpp', 'JavaScript', 'Verilog'] Tuple Output After Change: ['Python', 'Java', 'Cpp', 'Vhdl', 'Verilog']
03. Example
Tuple has mutable data type below, it can be changed.
tuple01 = (1000,[10, 20, 30, 40, 50, 60]) print('Tuple Output Before Change:n',tuple01[1]) tuple01[1][-2] = 110 print('nTuple Output After Change: n',tuple01[1])
Output:
Tuple Output Before Change: [10, 20, 30, 40, 50, 60] Tuple Output After Change: [10, 20, 30, 40, 110, 60]
Concatenating Tuples
01. Example
tuple01 = (10, 20, 30, 40) + (50, 60, 70, 80) print('Tuple Output: ',tuple01)
Output:
Tuple Output: (10, 20, 30, 40, 50, 60, 70, 80)
02. Example
tuple02 = ('Python','Java','Cpp') + ('JavaScript','Verilog','Vhdl') print('Tuple Output: ',tuple02)
Output:
Tuple Output: ('Python', 'Java', 'Cpp', 'JavaScript', 'Verilog', 'Vhdl')
Repeat Tuple Elements (* Operator)
01. Example
tuple01 = ((100,200)*2) print('Tuple Output: ',tuple01)
Output:
Tuple Output: (100, 200, 100, 200)
02. Example
tuple02 = (('Python','Java')*2) print('Tuple Output: ',tuple02)
Output:
Tuple Output: ('Python', 'Java', 'Python', 'Java')
03. Example
tuple03 = (('Python',)*2) print('Tuple Output: ',tuple03) print('Tuple Output: ',type(tuple03))
Output:
Tuple Output: ('Python', 'Python') Tuple Output: <class 'tuple'>
04. Example
tuple04 = (('Python')*2) print('Tuple Output: ',tuple04) print('Type Of Tuple04: ',type(tuple04))
Output:
Tuple Output: PythonPython Type Of Tuple04: <class 'str'>
05. Example
tuple05 = ((100)*2) print('Tuple Output: ',tuple05) print('Type Of Tuple05: ',type(tuple05))
Output:
Tuple Output: 200 Type Of Tuple05: <class 'int'>
Tuple Elements Deletion
- We cannot change the elements in the tuple. that also means we can’t delete or remove items from the tuple.
- Use del() function to delete Tuple(Not tuple elements).
01. Example
Tuple deletion is successful.
tuple01 = ('Python','Java','Cpp','JavaScript','Verilog') print('Tuple Before Deletion:n',tuple01) del(tuple01) print('nTuple After Deletion: n',tuple01)
Output:
Tuple Before Deletion: ('Python', 'Java', 'Cpp', 'JavaScript', 'Verilog') Traceback (most recent call last): File "<string>", line 4, in <module> NameError: name 'tuple01' is not defined
02. Example
Tuple does not support elements of Deletion.
tuple01 = ('Python','Java','Cpp','JavaScript','Verilog') print('Tuple Before Deletion:n',tuple01) del(tuple01[0])
Output:
Tuple Before Deletion: ('Python', 'Java', 'Cpp', 'JavaScript', 'Verilog') Traceback (most recent call last): File "<string>", line 3, in <module> NameError: 'tuple' object doesn't support item deletion
Built-in Functions For Tuple
01. Example
len() function: count the number of elements of the tuple and return numerical value.
tuple01 = ('Python','Java','Cpp','JavaScript','Verilog') tuple02 = (10, 20, 30, 40, 50, 60) print('Tuple01 Length : ',len(tuple01)) print('Tuple02 Length: ',len(tuple02))
Output:
Tuple01 Length: 5 Tuple02 Length: 6
02. Example
count() function: return the repetition of a particular tuple element.
tuple01 = ('Python','Java','Python','Python','Verilog') tuple02 = (10, 20, 20, 20, 60, 20, 80, 20, 40) print('Python Word Count In Tuple01: ',tuple01.count("Python")) print('20 Frequency In Tuple02: ',tuple02.count(20))
Output:
Python Word Count In Tuple01: 3 20 Frequency In Tuple02: 5
03. Example
index() function: returns the index of 1st occurrence of element.
tuple01 = ('Python','Java','Python','Python','Verilog') tuple02 = (10, 20, 20, 20, 60, 20, 80, 20, 40) print('Python Word 1st Occuring Index In Tuple01: ',tuple01.index("Python")) print('20 Number 1st Occuring Index In Tuple02: ',tuple02.index(20))
Output:
Python Word 1st Occurrence Index In Tuple01: 0
20 Number 1st Occurrence Index In Tuple02: 1
04. Example
sorted() Function: It takes the element in a tuple and returns the new sorted list. It does not sort the tuple itself as tuples are immutable.
tuple01 = ('Python','Java','JavaScript','Vhdl','Verilog') tuple02 = (40, 20, 50, 30, 10, 80, 70, 60) print("Type Of tuple01 Before Sorting: ",type(tuple01)) print("Type Of tuple02 Before Sorting: ",type(tuple02)) sortTuple01 = sorted(tuple01) sortTuple02 = sorted(tuple02) print("nType Of sortTuple01 After Sorting:ntype:",type(sortTuple01),'nValue: ',sortTuple01) print("nType Of sortTuple02 After Sorting:ntype:",type(sortTuple02),'nValue: ',sortTuple02)
Output:
Type Of tuple01 Before Sorting: <class 'tuple'> Type Of tuple02 Before Sorting: <class 'tuple'> Type Of sortTuple01 After Sorting: type: <class 'list'> Value: ['Java', 'JavaScript', 'Python', 'Verilog', 'Vhdl'] Type Of sortTuple02 After Sorting: type: <class 'list'> Value: [10, 20, 30, 40, 50, 60, 70, 80]
05. Example
max() function: return max value available of the tuple.
min() function: return min value available of tuple.
sum() function: return sum of all the elements of the tuple.
tuple01 = (40, 20, 50, 30, 10, 80, 70, 60) print("Max Value In tuple01: ",max(tuple01)) print("Min Value In tuple01: ",min(tuple01)) print("Sum Of All Values Of tuple01: ",sum(tuple01))
Output:
Max Value In tuple01: 80 Min Value In tuple01: 10 Sum Of All Values Of tuple01: 360
06. Example
MEMBERSHIP: return the boolean value(True/False).
tuple01 = ('Python','Java','JavaScript','Vhdl','Verilog') tuple02 = (10, 20, 30, 40, 50, 60, 70, 80) print('Python' in tuple01) print('python' in tuple01) print(40 in tuple02) print(100 in tuple02)
Output:
True False True False