Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

IMMUTABLE VS MUTABLE OBJECTS / DATA

PYTHON PROGRAMMING

 These objects are an abstraction for data, and Python has an amazing variety of data structures
that you can use to represent data or combine them to create your own custom data.
 A first fundamental distinction that Python makes on data is about whether or not the value of
an object changes.
 If the value can change, the object is called MUTABLE, while if the value cannot change, then
the object is called IMMUTABLE.

() - mutable VS [] –Immutable
 In Python, everything is treated as an object. Every object has these three attributes:

- Identity – This refers to the address that the object refers to in the computer’s memory.
- Type – This refers to the kind of object that is created. For example- integer, list, string
etc.
- Value – This refers to the value stored by the object. For example – List=[1,2,3] would
hold the numbers 1,2 and 3
While ID and Type cannot be changed once it’s created, values can be changed for Mutable
objects.

MUTABLE

 Mutable is a fancy way of saying that the internal state of the object is changed/mutated/ So,
the simplest definition is: An object whose internal state can be changed is mutable. On the
other hand immutable doesn’t allow any change in the object once it has been created.
 Mutable is when something is changeable or has the ability to change. In Python, ‘mutable’ is
the ability of objects to change their values. These are often the objects that store a collection of
data.
 Objects of built-in type that are mutable are:
 Lists
 Sets
 Dictionaries
 User-Defined Classes (It purely depends upon the user to define the characteristics)
IMMUTABLE OBJECT

 Immutable is the when no change is possible over time. In Python, if the value of an object
cannot be changed over time, then it is known as immutable. Once created, the value of these
objects is permanent.
 Immutable objects can not be changed after being created. Some of immutable types include
numeric data types, strings, bytes, frozen sets, and tuples.
 Objects of built-in type that are immutable are:
 Numbers (Integer, Rational, Float, Decimal, Complex & Booleans)
 Strings
 Tuples
 Frozen Sets
 User-Defined Classes (It purely depends upon the user to define the characteristics)
Object mutability is one of the characteristics that makes Python a dynamically typed language.
Though Mutable and Immutable in Python is a very basic concept, it can at times be a little
confusing due to the intransitive nature of immutability.

Immutability of Tuple

Tuples are immutable and hence cannot have any changes in them once they are created in Python. This
is because they support the same sequence operations as strings. We all know that strings are
immutable. The index operator will select an element from a tuple just like in a string. Hence, they are
immutable.

Exceptions in immutability

Like all, there are exceptions in the immutability in python too. Not all immutable objects are really
mutable. This will lead to a lot of doubts in your mind. Let us just take an example to understand this.

Consider a tuple ‘tup’.

Now, if we consider tuple tup = (‘GreatLearning’,[4,3,1,2]) ;


We see that the tuple has elements of different data types. The first element here is a string which as we
all know is immutable in nature. The second element is a list which we all know is mutable. Now, we all
know that the tuple itself is an immutable data type. It cannot change its contents. But, the list inside it
can change its contents. So, the value of the Immutable objects cannot be changed but its constituent
objects can. change its value.

Types of Immutable Objects

 Python numbers
Python numbers are a data type that stores numeric values. It’s an immutable data type –
means that it can change the value of the data variable in a new value allocated. In this tutorial,
you will learn about Python number data types with examples.

Example:
Here directly showing the number and using a variable. Printing a value and type in the console.
In output showing its type is class – <class ‘int’>

There are also types of Python Numbers like : Int Type, Float Type, Complex Numbers.

 Int type
A whole number (Int or integer ), + positive or – negative, without decimals of no limit length.
Example:
 Float type
A Float or floating-point number is a number that can positive or negative and containing one or
more decimals. Float numbers can also be scientific numbers with an “e” to indicate the power
of 10.
Example:

--------------------------------------------------------------------------------------------------------------------------
 Python Float Variable | Declare and Use – floating-point
Python float variable declaration happens automatically when you assign a float value to a
variable. The equal sign (=) is used to assign values to variables.
You can declare a variable, operand to the left of the = operator is the name of the variable and
the operand to the right of the = operator is the value stored in the variable.

Example:

 Floating-point number user input


by using a float() function you can convert a number into a Floating-point number.
Example:

----------------------------------------------------------------------------------------------------------------------

 Booleans
Booleans represent one of two values: True or False in Python. Boolean expression is needed in
if statement, for loop, comparison Operations to evaluate the outcome True or False in Python.
Here are some simple examples:

 Comparing numbers
This well help to determine if the statement is true or false.

 If statement
It executes if block code based on condition in if statement, Python language returns True or
False.
Example:
----------------------------------------------------------------------------------------
 Python Strings
Python Strings is a Datatype. Strings literals (Any constant value which can be assigned to the
variable is called literal/constant) in python are surrounded by either single quotation marks or
double quotation marks.

'Hello World' is the same as "Hello World"

--------------------------------------------------------------------------------------------

 Python Tuples
Python Tuples: are very similar to Lists, the only difference is that tuples are not mutable, so you
cannot change a tuple. Lists are used much more than tuples so tuples are only using very
specific scenarios. A tuple is a sequence of immutable Python objects (data structure). A tuple
consists of a number of values separated by commas.
Example:

You might also like