0% found this document useful (0 votes)
21 views

Memory Allocation For Variables in Python 3

Uploaded by

akvakarthik2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Memory Allocation For Variables in Python 3

Uploaded by

akvakarthik2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Memory Allocation For Variables In

Python 3
BY: A.K.V.S CHAKRAVARTHY
BRANCH: INFORMATION TECHNOLOGY
ROLL.NO: 22891A1201
AGENDA

Introduction Memory
Prerequisites Conclusion
to variables Management
Introduction to variables
what is the need of variables?
Variables are a fundamental component of programming languages.
When working with a programming language, we often need to retrieve
and store data. This data resides in system memory locations, each of
which has a specific hexadecimal address. To manipulate this data, it's
necessary to know the memory address of each system memory
location, which can be a challenging task. Therefore, we assign
temporary names to these system memory locations, and these names
are known as variables.
Prerequisites
Before diving into the subject let us discuss few prerequisites.
Object in Memory Management:

• In Python, everything is an object. An object is a unit of data in memory that holds both a
value (the data itself) and information about its type.
• Objects can represent various types of data, such as numbers, strings, lists, functions, and
even custom-defined types.
• Objects are units of data in memory that hold values and have types.
• Here I insist don’t get confused between the object concept in OOP’s and memory
management.
Memory Management
The actual story begins
In python when you declare and OBJECT

define a variable and assign a value Type


Value

to it, in system memory an object


>>> a = 26
Count
Other Data

will be created and this object ID: 22e896

contains the information like type,


value, count, etc.
OBJECT

Type Int
Here the type of the value assigned to the
variable will be the type of the object >>> a = 26
Value 26

and the value will be stored in the object; Count 1


here the count means, how many
variables are referred to this object.
Other Data

ID: 22e896
Now, after the creation of this
object in system memory, the a
OBJECT
memory address or id will be 22e896
Type Int
assigned to the variable to which Value 26

the value is assigned. That means Count 1

the variable will hold the object id Other Data


ID: 22e896
instead of holding the value and
the variable refers to the object.
If the pre-existing variable is
assigned to a new variable, >>> a = 26
then instead of creating new >>> b = a

object, both the new variable a


and the pre existing variable 22e896 OBJECT

will refer to the same object in Type


Value
Int
26
system memory. Now as the b Count 22
number of variables which are 22e896 Other Data

referring to the same object ID: 22e896

increased the count in object


also increases.
If a new value is assigned to >>> a = 26 OBJECT
>>> b = a
the pre-existing variable then >>> a = 2+6j Type Int
a new object will be created Value 26
in memory and this variable a Count 1
22e986
refers to the new object. Other Data
Where as the new object will ID: 22e896
be referring to the same
b
object. As the number of 22e896
OBJECT
variables referring to the old Type complex
object got reduced, its count Value 2+6j
value also decreases. Count 1
Other Data
ID: 22e986
OBJECT
>>> a = 26
When an object is no longer >>> b = a
Type Int
referenced (e.g., a variable >>> a = 2+6j Value 26 Garbage collector will
deallocate this object

goes out of scope or is >>> b = True Count 0 from memory

reassigned to a different Other Data


a
object), it means that the 22e986 ID: 22e896
object is no longer accessible
b OBJECT
and can be safely deallocated.
22e689 Type complex
This invokes the Python's
Value 2+6j
garbage collector which free
Count 1
the memory associated with
OBJECT Other Data
the unreferenced object.
Type bool ID: 22e986
Value True
Count 1
Other Data
ID: 22e689
Conclusion
The dynamic nature of variables in Python helps in programming efficiently by:
1. Simplifying code with no need for explicit type declarations.
2. Providing flexibility to change types and values during runtime.
3. Speeding up development by reducing type-related overhead.
4. Reducing type-related bugs by catching them at runtime.
5. Enabling code reuse and duck typing for behavior-centric coding.
6. Supporting quick prototyping and interactive development.
7. Allowing concise data structures and expressive solutions.
Overall, Python's dynamic typing promotes faster, more expressive, and flexible programming, though it should be used
thoughtfully with clear documentation and testing.

You might also like