SlideShare a Scribd company logo
Fundamental data types
Vs.
Immutable
What is Immutable?
 Once we created an object then we cannot change that
object.
 Immutable means unchangeable.
 All fundamental data types (int, float, complex, bool,
str) is immutable.
 Uses for internal reason(memory utilization).
x = 10
x = 20
x 10
20
Only variable’s reference changed and the
previous value is left unchanged
Representation of variable’s reference
a 10
20
1723196320
1723196480
b
Id is built in func to get the address of an identifier.
Reuse of object for b
Advantage or disadvantage ?
 Some may think that if we keep on changing value of x then we have to
make new object for each ‘x’, so there may be a chance of memory wastage,
performance may down.
But don’t think like that because it is really an advantage feature of Python.
Because if a object is already available then it uses the existing object
suppose we have thousands of variable with same object
x1 = 10
x2 = 10
x3 = 10
xn = 10
10
x2
x1
x3
x5x4
x6
x7
xn
Only one object is shared with n number of variables. No need to create
separate object. This makes memory utilization and better performance
Example
Hyd
v1
v2
v4
v3
Agra
Now v3 changes the city to Agra, then only the link
will change from “Hyd” to “Agra” the object will not
be deleted as it is still shared by v1, v2, and v4
49316480
49317984
‘is’ operator
 Is operator tests if two variables point the same object, not
if two variables have the same value.
Lists are mutable i.e., changeable
Different address though x and y have same value. But lists are
mutable i.e., both x and y points to different object.
But if you assign (y = x) then both points to the same object.
Returns True if both points to same object
Use of ‘is’ operator instead of id to check
variable references
Only one True object is created with 3 references pointing
towards the single object
What if I take x = 257?
It should be True
Different address, means 2 different object for
same x = 257
257x
y 257
58040688
58041344
Cont...
 Internally, reusing of same object is available from 0
to 256 only(most precisely -5 to 256), because this range is
most commonly used by programs.
 In programs we generally use x = 10 or x = 20 etc.
There are very rare case when we use more than 256
values.
 More than 256 is very rare case to use so there is no
need to use the same object. If do so there may be a
chance of memory wastage.
Cont...
 What happens internally is at the time of python
interpreter starts than before execution of program,
interpreter creates 0 to 256 integer objects. i.e., objects
with content 0 to 256 created at the time of interpreter
starts.
 a = 10  interpreter checks that: Is already object for
content 10 is available or not?
 Obviously object with content 10 is already created at the beginning .
 Then ‘a’ just points to already created object. Interpreter doesn’t
create the new object from 0 to 256.
 If a = 500  will create the new object as object with
content 500 will not be found.
Why the range is 0 to 256 only?
Ques: Why Python interpreter doesn’t make more objects i.e.,
more than 256 lets say rages from -1 crore to 1 crore?
Ans: As we have discussed that 0 to 256 objects created at the
time of interpreter starts as 0 to 256 is most commonly used
range. Lets suppose if python creates object from -1 crore to 1
crore i.e., 2 crore objects at time of interpreter starts than
python will take more and more time to start. May be one day
after . So python start up will become very costly.
we mostly use 2 or 3 objects, so for the sake of 3 or 4 objects,
creating 2 crore objects at the beginning is stupidness and
instead of advantage many problem may occur.
Immutability for bool
 In Boolean case, it always reuse the existing object
 It never create the objects at run time, because only two
objects(True & False) are required to create
 These two objects created at the time of interpreter starts.
Same address i.e., same object is shared
Immutability with str
 It always create the object at run time.
 It doesn’t create object at time of interpreter starts because there
may be infinite strings literals a user can choose. Then for how
many and for which literal interpreter makes object at beginning,
this prediction is impossible.
 But still Python gave reuse of same object facility to str type also
because most of the time we use same str literals.
True shows same object is shared
Cont...
Immutability with float & complex
 Surprisingly reusability of same object is not with
float and complex numbers.
Cont...
 If we talk about int only 0 to 256 values are in range of
reusability the same object which is fixed.
 In case of bool only two value are there to hold the concept of
reusability.
 But in terms of float or complex, can you tell how many values
are there between in 0 and 1 only
0.1
0.01
0.11
0.101
infinite
 Then how python will make this much object at the beginning.
 At language level, float and complex assume to be immutable.
0.1 + 0.2j #complex also have floating values
0.11 + 0.101j
0.10 + 0.111j
0.01 + 0.9j ........infinite
Things to remember
 All fundamental data types are immutable.
 Immutable means unchangeable.
 ‘is’ operator test if both variable points to same object or
not.
 Reusing the same object lies in following ranges:
 Int  0 to 256
 Bool  always
 Str  always
 Float  Never
 Complex  Never
 In terms of str python create object at the run time not at
the beginning as str value are infinite.
Reusing of same object is not available. Every time a
new object is created
Immutable?
Class Immutable? Mutable?
Int 
Float 
Complex 
Bool 
Str 
Bytes 
Bytearray 
Range 
List 
Tuple 
Set 
Frozenset 
Dictionary 
Ad

More Related Content

What's hot (20)

Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
Edureka!
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
T PRIYA
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
AkshayAggarwal79
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
International Institute of Information Technology (I²IT)
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
Aswini Dharmaraj
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
Edureka!
 
Python List.ppt
Python List.pptPython List.ppt
Python List.ppt
T PRIYA
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
Karin Lagesen
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
Nilimesh Halder
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
Jayant Dalvi
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
Vardhil Patel
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
AkshayAggarwal79
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
Aswini Dharmaraj
 

Similar to Python-04| Fundamental data types vs immutability (20)

PYTHON OBJECTS - Copy.pptx
 PYTHON OBJECTS - Copy.pptx PYTHON OBJECTS - Copy.pptx
PYTHON OBJECTS - Copy.pptx
sanchiganandhini
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3
Kamal Mukkamala
 
04 Variables
04 Variables04 Variables
04 Variables
thewhiteafrican
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
Manish Jangir
 
04 variables
04 variables04 variables
04 variables
Program in Interdisciplinary Computing
 
Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11
chinthala Vijaya Kumar
 
Basic VB.Net & Ms. Acces Integration to creat database application by. Rizki...
Basic VB.Net & Ms. Acces Integration  to creat database application by. Rizki...Basic VB.Net & Ms. Acces Integration  to creat database application by. Rizki...
Basic VB.Net & Ms. Acces Integration to creat database application by. Rizki...
rizki209761
 
About Python
About PythonAbout Python
About Python
Shao-Chuan Wang
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptx
DeepaRavi21
 
Sessisgytcfgggggggggggggggggggggggggggggggg
SessisgytcfggggggggggggggggggggggggggggggggSessisgytcfgggggggggggggggggggggggggggggggg
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
BlackRabbitCoder
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptx
abhishek364864
 
Java tutorial part 4
Java tutorial part 4Java tutorial part 4
Java tutorial part 4
Mumbai Academisc
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
FaRaz Ahmad
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
Ahmad Bashar Eter
 
P1
P1P1
P1
Actuaria, Facultad de Ciencias, UNAM
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
The Ring programming language version 1.10 book - Part 100 of 212
The Ring programming language version 1.10 book - Part 100 of 212The Ring programming language version 1.10 book - Part 100 of 212
The Ring programming language version 1.10 book - Part 100 of 212
Mahmoud Samir Fayed
 
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptxvectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
VivekSharma34623
 
The Ring programming language version 1.6 book - Part 182 of 189
The Ring programming language version 1.6 book - Part 182 of 189The Ring programming language version 1.6 book - Part 182 of 189
The Ring programming language version 1.6 book - Part 182 of 189
Mahmoud Samir Fayed
 
PYTHON OBJECTS - Copy.pptx
 PYTHON OBJECTS - Copy.pptx PYTHON OBJECTS - Copy.pptx
PYTHON OBJECTS - Copy.pptx
sanchiganandhini
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3
Kamal Mukkamala
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
Manish Jangir
 
Basic VB.Net & Ms. Acces Integration to creat database application by. Rizki...
Basic VB.Net & Ms. Acces Integration  to creat database application by. Rizki...Basic VB.Net & Ms. Acces Integration  to creat database application by. Rizki...
Basic VB.Net & Ms. Acces Integration to creat database application by. Rizki...
rizki209761
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptx
DeepaRavi21
 
Sessisgytcfgggggggggggggggggggggggggggggggg
SessisgytcfggggggggggggggggggggggggggggggggSessisgytcfgggggggggggggggggggggggggggggggg
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptx
abhishek364864
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
FaRaz Ahmad
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
Ahmad Bashar Eter
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
Manish Jangir
 
The Ring programming language version 1.10 book - Part 100 of 212
The Ring programming language version 1.10 book - Part 100 of 212The Ring programming language version 1.10 book - Part 100 of 212
The Ring programming language version 1.10 book - Part 100 of 212
Mahmoud Samir Fayed
 
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptxvectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
VivekSharma34623
 
The Ring programming language version 1.6 book - Part 182 of 189
The Ring programming language version 1.6 book - Part 182 of 189The Ring programming language version 1.6 book - Part 182 of 189
The Ring programming language version 1.6 book - Part 182 of 189
Mahmoud Samir Fayed
 
Ad

Recently uploaded (20)

Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Ad

Python-04| Fundamental data types vs immutability

  • 2. What is Immutable?  Once we created an object then we cannot change that object.  Immutable means unchangeable.  All fundamental data types (int, float, complex, bool, str) is immutable.  Uses for internal reason(memory utilization). x = 10 x = 20 x 10 20 Only variable’s reference changed and the previous value is left unchanged
  • 3. Representation of variable’s reference a 10 20 1723196320 1723196480 b Id is built in func to get the address of an identifier. Reuse of object for b
  • 4. Advantage or disadvantage ?  Some may think that if we keep on changing value of x then we have to make new object for each ‘x’, so there may be a chance of memory wastage, performance may down. But don’t think like that because it is really an advantage feature of Python. Because if a object is already available then it uses the existing object suppose we have thousands of variable with same object x1 = 10 x2 = 10 x3 = 10 xn = 10 10 x2 x1 x3 x5x4 x6 x7 xn Only one object is shared with n number of variables. No need to create separate object. This makes memory utilization and better performance
  • 5. Example Hyd v1 v2 v4 v3 Agra Now v3 changes the city to Agra, then only the link will change from “Hyd” to “Agra” the object will not be deleted as it is still shared by v1, v2, and v4 49316480 49317984
  • 6. ‘is’ operator  Is operator tests if two variables point the same object, not if two variables have the same value. Lists are mutable i.e., changeable Different address though x and y have same value. But lists are mutable i.e., both x and y points to different object. But if you assign (y = x) then both points to the same object. Returns True if both points to same object
  • 7. Use of ‘is’ operator instead of id to check variable references Only one True object is created with 3 references pointing towards the single object
  • 8. What if I take x = 257? It should be True Different address, means 2 different object for same x = 257 257x y 257 58040688 58041344
  • 9. Cont...  Internally, reusing of same object is available from 0 to 256 only(most precisely -5 to 256), because this range is most commonly used by programs.  In programs we generally use x = 10 or x = 20 etc. There are very rare case when we use more than 256 values.  More than 256 is very rare case to use so there is no need to use the same object. If do so there may be a chance of memory wastage.
  • 10. Cont...  What happens internally is at the time of python interpreter starts than before execution of program, interpreter creates 0 to 256 integer objects. i.e., objects with content 0 to 256 created at the time of interpreter starts.  a = 10  interpreter checks that: Is already object for content 10 is available or not?  Obviously object with content 10 is already created at the beginning .  Then ‘a’ just points to already created object. Interpreter doesn’t create the new object from 0 to 256.  If a = 500  will create the new object as object with content 500 will not be found.
  • 11. Why the range is 0 to 256 only? Ques: Why Python interpreter doesn’t make more objects i.e., more than 256 lets say rages from -1 crore to 1 crore? Ans: As we have discussed that 0 to 256 objects created at the time of interpreter starts as 0 to 256 is most commonly used range. Lets suppose if python creates object from -1 crore to 1 crore i.e., 2 crore objects at time of interpreter starts than python will take more and more time to start. May be one day after . So python start up will become very costly. we mostly use 2 or 3 objects, so for the sake of 3 or 4 objects, creating 2 crore objects at the beginning is stupidness and instead of advantage many problem may occur.
  • 12. Immutability for bool  In Boolean case, it always reuse the existing object  It never create the objects at run time, because only two objects(True & False) are required to create  These two objects created at the time of interpreter starts. Same address i.e., same object is shared
  • 13. Immutability with str  It always create the object at run time.  It doesn’t create object at time of interpreter starts because there may be infinite strings literals a user can choose. Then for how many and for which literal interpreter makes object at beginning, this prediction is impossible.  But still Python gave reuse of same object facility to str type also because most of the time we use same str literals. True shows same object is shared
  • 15. Immutability with float & complex  Surprisingly reusability of same object is not with float and complex numbers.
  • 16. Cont...  If we talk about int only 0 to 256 values are in range of reusability the same object which is fixed.  In case of bool only two value are there to hold the concept of reusability.  But in terms of float or complex, can you tell how many values are there between in 0 and 1 only 0.1 0.01 0.11 0.101 infinite  Then how python will make this much object at the beginning.  At language level, float and complex assume to be immutable. 0.1 + 0.2j #complex also have floating values 0.11 + 0.101j 0.10 + 0.111j 0.01 + 0.9j ........infinite
  • 17. Things to remember  All fundamental data types are immutable.  Immutable means unchangeable.  ‘is’ operator test if both variable points to same object or not.  Reusing the same object lies in following ranges:  Int  0 to 256  Bool  always  Str  always  Float  Never  Complex  Never  In terms of str python create object at the run time not at the beginning as str value are infinite. Reusing of same object is not available. Every time a new object is created
  • 18. Immutable? Class Immutable? Mutable? Int  Float  Complex  Bool  Str  Bytes  Bytearray  Range  List  Tuple  Set  Frozenset  Dictionary 