You are on page 1of 19

MUTABLE AND IMMUTABLE

OBJECTS
BY MAHI MUDGAL
WHAT WE GET IN THIS

In Python everything is an object, and got to use id and is to deepen our understanding of what's happening under
the hood when using Python to create and modify objects.

We also learned the difference between mutable objects, that can be modified after creation,
and immutable objects, which cannot.

We saw that

We then learned why dictionary keys have to be immutable in Python.

Understanding how Python "sees" objects is a key to becoming a better Python programmer. I hope this ppt will
help you on your journey to mastering Python.
DIFFERENCE BETWEEN MUTABLE AND
IMMUTABLE OBJECTS
. The property of whether or not data objects can be modified in the same memory
location where they are stored is called mutability

MUTABLE IMMUTABLE
• Mutable objects are those object whose • Immutable objects are those objects
value or content can be changed as and whose value or content can not be
when required. changed .
• FOR EXAMPLE:-list,dictionaries • FOR EXAMPLE:-Tuple,String,Numeric
DATA TYPE IN PHYTHON

SET
Numeric:- Any representation of data which has numeric value. Python identifies three types of numbers –
integer, float and complex number.

Integer, float and complex number all are


immutable (unchangeable in phython)
Code to show immutability
OBJECT 1 :-FLOAT (Any Decimal number) Object2:-Integer (+,-,and 0)
OBJECT 3:-COMPLEX NO.
Complex number - A number with a real and imaginary component is represented as a + bj inPython where a and b are
floats and j = √-1 Examples: 4+6j, -2.3+6.4j

CODE TO SHOW IMMUTABILITY


SEQUENCE Sequence An ordered collection of similar or different data types. The built-in
Sequence data types in Python are – String, List and Tuple.

String A collection of one or more characters put in single, double or triple quotes.
1-STRING:- Examples: ‘Hello’, "Hello", "'Hello'", """Hello"""

CODE FOR SHOWING IMMUTABILITY OF AN STRING

EXAMPLE 1:- String EXAMPLE 2:-string

CODE CODE
Tuple- An ordered collection of one or more data items, not

Object 2 :- Tuple
necessarily of same type put in parentheses. The contents of a
tuple cannot be modified – it is immutable - after the tuple is
created. Examples: (1,"Ravi", 75.50, True)

CODE TO SHOW IMMUTABILITY OF


TUPLE:-
INPUT OUTPUT
LIST AN ORDERED COLLECTION OF ONE OR MORE DATA ITEMS, NOT NECESSARILY OF SAME TYPE, PUT IN SQUARE BRACK

Object 3:-list Examples: [1,"Ravi",75.50, True]


we can modify the items within a list. Modifying a list means to change an item, or add a new item, or
remove an existing item. Here are some methods of the built-in List class that help in modifying lists

List is mutable and it can be shown in numerous way

1)

2) insert() - Inserts an item in list at the specified index.


3) Append():- Adds an item at the end of the list.

4) Remove():- Removes the specified item from the list.


pop()
5) Removes and returns the last object in list

6) reverse()
Reverses the order of items in a list.
Rearranges items in the list according to alphabetical order. Default is ascending order. For descending
7) SORT:- order put reverse=True as argument in function bracket.

ASCENDING ORDER

DSCENDING ORDER
WE CAN CONVERT ONE SEQUENCE OBJECT TO ANOTHER

IT CAN HELP IN CHANGING SOME THING IN IMMUTABLE OBJECT

The Following utility functions help in converting one sequence data type to other

Changing tuple and string in list

We can also change list or string in tuple


LIST AND TUPLE DIFFERENCE
set refers to an unordered collection of objects without any duplicates. An object of type set may be created
by enclosing the elements of the set within curly brackets. The set objects may also be created by set function on
tuple ,list and string
The elements of a set are required to be immutable objects. Therefore,whereas objects of types such as int, float,
tuple, and str can be members of a set, a list cannot be a member of a set. Unlike lists, we cannot access elements
of a set through indexing or slicing.

EXAMPLE:-3

Code
Tuple ,list and string can be converted in set by using set function

L is printed only once


Dictionary An unordered collection of data in key:value pair form. Collection of such pairs is enclosed in
curly brackets. Example: {1:"Superman", 2:"Wonder Woman", 3:"Thor", 4: "Hulk", 5:"Black Widow"}

Dictionary are unordered collection of object because it dosent remember the place of object in it
For example

Code to show dictionary is mutable


INTRESTING FACT ABOUT
DICTIONARY
DICTIONARY KEY IS IMMUTABLE

INPUT OUTPUT

BUT THEN TO DICTIONARY IS MUTABLE:-


Because value in dictionary is mutable
THANKYOU
FOR LISTENING

You might also like