Menu

Digital Pradesh

Government notices, courts and money – explained from the source

In Python, tuple is a mutable data type

The statement "In Python, tuple is a mutable data type" is False because tuples are immutable. However, if a tuple contains mutable elements (like a list), those elements can be modified, but the tuple's structure itself cannot be changed.

Mutable Data Type

State True or False — “In Python, tuple is a mutable data type”

Understanding the Statement: “In Python, tuple is a mutable data type”

The given statement is: “In Python, tuple is a mutable data type.”

Answer: False

Explanation in Layman’s Terms:

A tuple in Python is a type of data structure used to store multiple items in a single variable. It is similar to a list, but the key difference is that a tuple is immutable. This means once you create a tuple, you cannot change its elements — you can’t add, remove, or modify the values inside it.

Think of a tuple as a sealed box. Once you’ve placed items inside the box and sealed it, you cannot open the box to change its contents. However, you can still look at what’s inside or create a new box with different items if needed.

For example:

# Creating a tuple
my_tuple = (1, 2, 3)

# Trying to modify a tuple (this will raise an error)
my_tuple[0] = 10  # ❌ This will throw a TypeError: 'tuple' object does not support item assignment

Explanation in Technical Terms:

In Python, tuples are an ordered collection of elements enclosed in parentheses (). Unlike lists, tuples are immutable, meaning that the memory allocated for a tuple cannot be altered after its creation. Any operation attempting to modify a tuple’s elements will result in a TypeError.

This immutability arises because tuples are designed for fixed data collections, ensuring the integrity of data. However, while the tuple itself is immutable, if it contains mutable objects like lists or dictionaries, those objects can still be changed.

Example with mutable elements:

# Tuple containing a list (mutable element inside an immutable tuple)
my_tuple = (1, 2, [3, 4])

# Modifying the list inside the tuple
my_tuple[2][0] = 99
print(my_tuple)  # Output: (1, 2, [99, 4])

In this case, the tuple’s structure remains the same, but the list inside it has been modified.

True vs. False Examples:

  • False Example: If you attempt to change the tuple directly, Python will throw an error:
            my_tuple = (1, 2, 3)
            my_tuple[1] = 99  # ❌ TypeError: 'tuple' object does not support item assignment
    
  • True Example (Hypothetical): If tuples were mutable, you could modify them like lists:
            my_tuple = (1, 2, 3)
            my_tuple[1] = 99  # Hypothetical: This would work if tuples were mutable.
    

    But this is not the case in Python.

The statement “In Python, tuple is a mutable data type” is False because tuples are immutable. However, if a tuple contains mutable elements (like a list), those elements can be modified, but the tuple’s structure itself cannot be changed.

Updates and corrections

No corrections or updates have been noted for this article since publication on 18 Dec 2024.

Found an error? Report it with the article link and your evidence. Read our editorial standards.

Topics

About the author

Isrg Rajan

Isrg Rajan is the founder and editor of Digital Pradesh (isrgrajan.com), which he started at the end of 2011 and has published since 2012. He edits the site, sets its editorial standards and is responsible for the articles published under this name.