No Python tuples are not mutable data type. Tuples are immutable, meaning that once a tuple has been created, the items in it can’t change.
Example tuple are not mutable in Python
Simple example code once a tuple has been created you can’t change the order, append, delete or insert the entry.
tup1 = (1, 2, 3) tup1 = 4
Is a list of tuples mutable or is it immutable in Python?
Answer: A list of tuples can be modified as a list is a mutable entity. However, an individual tuples cannot be modified. Consider the below example.
Modifying the list itself
tup1 = [(1, 2), (3, 4)] tup1 = 4 print(tup1)
Output: [(1, 2), 4]
Modifying a tuple inside the list
tup1 = [(1, 2), (3, 4)] tup1 = 1 print(tup1)
Output: TypeError: ‘tuple’ object does not support item assignment
Do comment if you have any doubts and suggestions on this Python tuple tutorial.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.