Tuple datatype and tuple operations
Few data types like list, dictionary are mutable and others like string datatype, tuple are immutable.
Mutable datatypes means changes can be done and are reflected in same variable.
Immutable datatypes means changes can be done but not in same variable.
Tuple is similar to list datatype but only difference between them is that list is mutable and tuple is immutable data type. We can change the contents of list even after assigning values but we cannot change the contents of tuple once assigned with its values.
An empty tuple represents zero or null value.
A tuple datatype can be defined with use of parentheses () containing values separated with comma ” , “.
Each value is termed to be an item of the tuple.
Tuple datatype is Immutable
As we know immutable data type indicates datatypes which are non editable or unchangeable in same variable name.
We cannot modify existing string and so tuples are immutable datatype.
We can create a new tuple with reference to existing or original tuple.
Tuples are immutable - Justified
If we try to change or assign any new value using [ ] operator in existing tuple, we get an error message.
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
This TypeError: ‘tuple’ object does not support item assignment proves that we cannot modify our existing tuple.
In our case itv holds original tuple and itv=10 tries to edit original string at position 4 with 10
As we receive above message, it becomes clear that tuple datatype cannot update original value and tuples are immutable.
How to deal with immutable tuple?
To remove TypeError while updating tuple, we can create a new tuple based on existing tuple.
>>> itv=(1,2,3,4,5,6) >>> itv2=itv+(7,8,9) >>> itv2 (1, 2, 3, 4, 5, 6, 7, 8, 9) >>>
This example tells us about how to update a tuple with new variable.
In this example concatenation of tuple slicing is done with tuple .
According to the location of character which we wish to update we can slice the tuple and concatenate with required update.
How to search in a tuple?
We can search a tuple or letter from a tuple .
Following example will help us for better understanding :
for i in range(len(itv)):
print("match not found")
match not found
Python supports various tuple operations which helps in performing tasks easily.
Various tuple operations include concatenation, repetition, in operator, not in operator, range and slice operators.
Some of the tuple operations are explained below:
List of tuple operations in python
- + : Concatenation operator
- This operator is used to join or merge two tuples together, i.e. values to the either side of + operator will be merged or concatenated.
>>> itv+ itv2
(1, 2, 3, 4, 5, 6, 10, 20, 30, 40, 50, 60)
- * : Repetition operator
- This operator creates new tuple by creating copies of same tuple.
- This operator here does not multiply number with tuple but it creates copies of tuple as per specified number.
- If a single value tuple is defined without trailing comma than it is not considered as tuple it becomes an integer and so it gets multiplied and not repeated
(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)
(8, 8, 8, 8)
>>> itv1=(8) #without comma it will be integer not a tuple
>>> itv1 * 4
-  : Slice operator
- This operator is used to get a character from the given string by specifying the index position in .
>>> s='python programming'
- [:] : Range Slice operator
- This operator is used to slice a tuple by specifying start and end position and also an optional parameter of step value.
(1, 2, 3, 4, 5, 6)
(6, 5, 4, 3, 2, 1)
- in : Membership operator (in)
- This operator will return True is character is present in given tuple and False otherwise.
>>> 5 in itv
>>> 7 in itv
- not in : Membership operator(not in)
- This operator will return True is character is not present in given tuple and False otherwise.
>>> 5 not in itv
>>> 7 not in itv