Tuple
Python - Tuples
Section titled “Python - Tuples”Tuple is an immutable (unchangeable) collection of elements of different data types. It is an ordered collection, so it preserves the order of elements in which they were defined.
Tuples are defined by enclosing elements in parentheses (), separated by a comma. The following declares a tuple type variable.
()
tpl=() # empty tupleprint(tpl) #output: ()
names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tupleprint(names) #output:('Jeff', 'Bill', 'Steve', 'Yash')
nums = (1, 2, 3, 4, 5) # int tupleprint(nums) #output:(1, 2, 3, 4, 5)
employee=(1, 'Steve', True, 25, 12000) # heterogeneous data tupleprint(employee) #output:(1, 'Steve', True, 25, 12000)`tpl=() # empty tuple print(tpl) #output: ()
names = (‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’) # string tuple print(names) #output:(‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’)
nums = (1, 2, 3, 4, 5) # int tuple print(nums) #output:(1, 2, 3, 4, 5)
employee=(1, ‘Steve’, True, 25, 12000) # heterogeneous data tuple print(employee) #output:(1, ‘Steve’, True, 25, 12000)`Try it However, it is not necessary to enclose the tuple elements in parentheses. The tuple object can include elements separated by a comma without parentheses.
names = 'Jeff', 'Bill', 'Steve', 'Yash' # string tupleprint(names) #output: ('Jeff', 'Bill', 'Steve', 'Yash')
nums = 1, 2, 3, 4, 5 # int tupleprint(nums) #output: (1, 2, 3, 4, 5)
employee=1, 'Steve', True, 25, 12000 # heterogeneous data tupleprint(employee) #output: (1, 'Steve', True, 25, 12000)`names = ‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’ # string tuple print(names) #output: (‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’)
nums = 1, 2, 3, 4, 5 # int tuple print(nums) #output: (1, 2, 3, 4, 5)
employee=1, ‘Steve’, True, 25, 12000 # heterogeneous data tuple print(employee) #output: (1, ‘Steve’, True, 25, 12000)`Try it Tuples cannot be declared with a single element unless followed by a comma.
names = ('Jeff') # considered as string typeprint(names) #output: 'Jeff'print(type(names)) #output: <class 'string'>
names = ('Jeff',) # tuple with single elementprint(names) #output: (Jeff)print(type(names)) #output: <class 'tuple'>`names = (‘Jeff’) # considered as string type print(names) #output: ‘Jeff’ print(type(names)) #output: <class ‘string’>
names = (‘Jeff’,) # tuple with single element print(names) #output: (Jeff) print(type(names)) #output: <class ‘tuple’>`Try it
Access Tuple Elements
Section titled “Access Tuple Elements”Each element in the tuple is accessed by the index in the square brackets []. An index starts with zero and ends with (number of elements - 1), as shown below.
names = ('Jeff', 'Bill', 'Steve', 'Yash')print(names[0]) #output: 'Jeff'print(names[1]) #output: 'Bill'print(names[2]) #output: 'Steve'print(names[3]) #output: 'Yash'
nums = (1, 2, 3, 4, 5)print(nums[0]) #output: 1print(nums[1]) #output: 2print(nums[4]) #output: 5`names = (‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’) print(names[0]) #output: ‘Jeff’ print(names[1]) #output: ‘Bill’ print(names[2]) #output: ‘Steve’ print(names[3]) #output: ‘Yash’
nums = (1, 2, 3, 4, 5)
print(nums[0]) #output: 1
print(nums[1]) #output: 2
print(nums[4]) #output: 5[Try it](/codeeditor?cid=python-3z7rxwjmm) The tuple supports negative indexing also, the same as list type. The negative index for the first element starts from -number of elements and ends with -1 for the last element. -number of elements`
names = ('Jeff', 'Bill', 'Steve', 'Yash')print(names[-4]) #output: 'Jeff'print(names[-3]) #output: 'Bill'print(names[-2]) #output: 'Steve'print(names[-1]) #output: 'Yash'names = ('Jeff', 'Bill', 'Steve', 'Yash') print(names[-4]) #output: 'Jeff' print(names[-3]) #output: 'Bill' print(names[-2]) #output: 'Steve' print(names[-1]) #output: 'Yash'Try it
If the element at the specified index does not exist, then the error “index out of range” will be thrown.
s = names[5] #IndexError: tuple index out of ranges = names[5] #IndexError: tuple index out of range
Tuple elements can be unpacked and assigned to variables, as shown below. However, the number of variables must match with the number of elements in a tuple; otherwise, an error will be thrown.
names = ('Jeff', 'Bill', 'Steve', 'Yash')a, b, c, d = names # unpack tupleprint(a, b, c, d)names = ('Jeff', 'Bill', 'Steve', 'Yash') a, b, c, d = names # unpack tuple print(a, b, c, d)Try it
Update or Delete Tuple Elements
Section titled “Update or Delete Tuple Elements”Tuple is unchangeable. So, once a tuple is created, any operation that seeks to change its contents is not allowed. For instance, trying to modify or delete an element of names tuple will result in an error. However, you can delete an entire tuple using the del keyword.
names``del
names = ('Jeff', 'Bill', 'Steve', 'Yash')names[0] = 'Swati' #throws errordel names[0] #throws errordel names #delete names variablenames = ('Jeff', 'Bill', 'Steve', 'Yash') names[0] = 'Swati' #throws error del names[0] #throws error del names #delete names variableTry it
Tuple Class
Section titled “Tuple Class”The underlying type of a tuple is the tuple class. Check the type of a variable using the type() function.
type()
names = ('Jeff', 'Bill', 'Steve', 'Yash')print(type(names)) #output: <class 'tuple'>
nums = (1,2,3,4,5)print(type(nums)) #output: <class 'tuple'>`names = (‘Jeff’, ‘Bill’, ‘Steve’, ‘Yash’) print(type(names)) #output: <class ‘tuple’>
nums = (1,2,3,4,5)
print(type(nums)) #output: <class ‘tuple’>[Try it](/codeeditor?cid=python-3z7rydxgc) The tuple() constructor is used to convert any iterable to tuple type. tuple()`
tpl = tuple('Hello')print(tpl) #output: ('H','e','l','l','o')
tpl = tuple([1,2,3,4,5])print(tpl) #output: (1,2,3,4,5)
tpl = tuple({1,2,3,4,5}) # converts set to tupleprint(tpl) #output: (1,2,3,4,5)
tpl = tuple({1:"One",2:"Two"}) # converts dictionary to tupleprint(tpl) #output: (1,2)`tpl = tuple(‘Hello’) print(tpl) #output: (‘H’,‘e’,‘l’,‘l’,‘o’)
tpl = tuple([1,2,3,4,5]) print(tpl) #output: (1,2,3,4,5)
tpl = tuple({1,2,3,4,5}) # converts set to tuple print(tpl) #output: (1,2,3,4,5)
tpl = tuple({1:“One”,2:“Two”}) # converts dictionary to tuple print(tpl) #output: (1,2)`Try it
Tuple Operations
Section titled “Tuple Operations”Like string, tuple objects are also a sequence. Hence, the operators used with strings are also available for the tuple.
t1=(1,2,3) t2=(4,5,6) print(t1+t2) #(1, 2, 3, 4, 5, 6) t2+(7,) #(4, 5, 6, 7)t1=(1,2,3) t1*4 #(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)t1=(1,2,3,4,5,6) t1[3] # 4 t1[-2] #5t1=(1,2,3,4,5,6) t1[1:3] #(2, 3) t1[3:] #(4, 5, 6) t1[:3] #(1, 2, 3)t1=(1,2,3,4,5,6) 5 in t1 #True 10 in t1 #Falset1=(1,2,3,4,5,6) 4 not in t1 #False 10 not in t1 #True