SlideShare ist ein Scribd-Unternehmen logo
1 von 18
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 

Weitere ähnliche Inhalte

Was ist angesagt?

Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python pptPriyanka Pradhan
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in pythonJothi Thilaga P
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of PythonElewayte
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Edureka!
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONvikram mahendra
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming pptismailmrribi
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialQA TrainingHub
 

Was ist angesagt? (20)

Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Values and Data types in python
Values and Data types in pythonValues and Data types in python
Values and Data types in python
 
Introduction to Basics of Python
Introduction to Basics of PythonIntroduction to Basics of Python
Introduction to Basics of Python
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Pointers
PointersPointers
Pointers
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 

Ähnlich wie Python-04| Fundamental data types vs immutability

PYTHON OBJECTS - Copy.pptx
 PYTHON OBJECTS - Copy.pptx PYTHON OBJECTS - Copy.pptx
PYTHON OBJECTS - Copy.pptxsanchiganandhini
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3Kamal Mukkamala
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep DiveManish Jangir
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptxDeepaRavi21
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxabhishek364864
 
javaimplementation
javaimplementationjavaimplementation
javaimplementationFaRaz Ahmad
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAhmad Bashar Eter
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureManish 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 212Mahmoud 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)).pptxVivekSharma34623
 
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 189Mahmoud Samir Fayed
 

Ähnlich wie Python-04| Fundamental data types vs immutability (20)

PYTHON OBJECTS - Copy.pptx
 PYTHON OBJECTS - Copy.pptx PYTHON OBJECTS - Copy.pptx
PYTHON OBJECTS - Copy.pptx
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3
 
04 Variables
04 Variables04 Variables
04 Variables
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
04 variables
04 variables04 variables
04 variables
 
Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11
 
About Python
About PythonAbout Python
About Python
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptx
 
C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
 
PRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptxPRESENTATION ON PYTHON.pptx
PRESENTATION ON PYTHON.pptx
 
Java tutorial part 4
Java tutorial part 4Java tutorial part 4
Java tutorial part 4
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 
P1
P1P1
P1
 
Javascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big PictureJavascript Prototypal Inheritance - Big Picture
Javascript Prototypal Inheritance - Big Picture
 
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
 
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
 
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
 
iOS Memory Management
iOS Memory ManagementiOS Memory Management
iOS Memory Management
 
python
pythonpython
python
 

Mehr von Mohd Sajjad

Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data typesMohd Sajjad
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & ImportMohd Sajjad
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Python-00 | Introduction and installing
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installingMohd Sajjad
 
Secure your folder with password/without any software
Secure your folder with password/without any softwareSecure your folder with password/without any software
Secure your folder with password/without any softwareMohd Sajjad
 

Mehr von Mohd Sajjad (6)

Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python-00 | Introduction and installing
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installing
 
Secure your folder with password/without any software
Secure your folder with password/without any softwareSecure your folder with password/without any software
Secure your folder with password/without any software
 
SNMP Protocol
SNMP ProtocolSNMP Protocol
SNMP Protocol
 

Kürzlich hochgeladen

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Kürzlich hochgeladen (20)

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

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 