Introduction
In computer programming, an immutable object is an object whose state cannot be modified after it is created. This contrasts to a mutable object, which can be modified after it is created.
One common use for immutable objects is as keys in dictionaries or sets. Since they can’t be changed, they can be added to or removed from these data structures without worrying about changing the underlying data.
Immutable objects are also often used to represent things that never change, like constants. For example, the value of pi is immutable, because it will always be 3.14 (give or take a few decimal places).
Tuples are immutable because they are typically used to represent data that should never be changed. For example, a tuple containing the latitude and longitude of a city would never need to be changed (assuming the city doesn’t move).
Even if you didn’t intend for a tuple to be immutable, it’s often best to treat it as such. If you try to change the value of an element in a tuple, you’ll get an error:
my_tuple = (1, 2, 3)
my_tuple[0] = 4
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘tuple’ object does not support item assignment
Tuples are called immutable because once they are created, their values cannot be changed.
What are tuples?
Tuples are one of the four main data types in Python. A tuple is a collection of items that is immutable (cannot be changed). Tuples are often used to store data that will not be changed, such as a record of results from a competition.
The word “tuple” comes from the Latin word “tūplum”, which means “double”. This refers to the fact that a tuple is made up of two parts: an immutable object and a mutable object. The immutable object is called the head, and the mutable object is called the tail.
What is the difference between tuples and lists?
Tuple is a data structure that is an immutable, or unchangeable, ordered sequence of elements. Tuples are usually used to store related pieces of information. For example, you might use a tuple to store a person’s name and age.
List is a data structure that is an mutable, or changeable, ordered sequence of elements. Lists are usually used to store similar pieces of information. For example, you might use a list to store a list of numbers, or a list of names.
Why are tuples called immutable?
Immutable means unchangeable. A tuple is called immutable because once it is created, its values cannot be changed. This is in contrast to a list, which can be modified after it is created.
What are the benefits of using tuples?
Tuples are often called immutable because they cannot be modified after they are created. This is different from lists, which can be modified after they are created.
The benefits of using tuples include:
-They are faster than lists.
-They take up less memory than lists.
-They can be used as keys in dictionaries (unlike lists).
Conclusion
In Python, tuples are called as immutable because they cannot be changed after they are created. This means that once you create a tuple, you cannot add, remove, or modify any of its values.