How are Variables stored in Python
Published by
sanya sanya
How Variables are Stored in Python ?
At the heart of every computer is a processor, which is a small chip that can perform calculations and execute instructions. The processor communicates with the other components of the computer, such as memory and input/output devices, through a series of electronic signals. These signals are represented as binary digits, or bits, which can be either 0 or 1.
Memory is a crucial component of a computer, as it allows the processor to store and retrieve data. There are several types of memory in a computer, but the most important type for our purposes is called Random Access Memory (RAM). RAM is a type of volatile memory, which means that its contents are lost when the computer is turned off or restarted.
When you run a Python program, it gets loaded into RAM so the processor can execute it. As your program runs, it may need to store information for later use. This is where variables come in. Variables are used to store data in a program. When you create a variable in Python, you are essentially reserving a block of memory where the variable's value will be stored. The location of this memory is determined by the interpreter, and is represented by a unique identifier known as the variable's memory address.
In Python, variables are dynamically typed, which means that you don't need to specify the type of a variable when you create it. Instead, Python infers the type based on the value that you assign to the variable. For example, if you assign the value 42 to a variable, Python will create a block of memory to store the number 42, and assign the memory address of that block to the variable.
To understand how variables are stored in memory, it's helpful to know a bit about how memory is organized in a computer. Memory is divided up into a series of bytes, which are the smallest addressable unit of memory. A byte is a group of 8 bits, and can store a single character or number. The size of a byte is fixed in Python, and is always 8 bits.
Memory is also divided up into pages, which are contiguous blocks of memory that are typically 4 kilobytes (KB) in size. A kilobyte is equal to 1024 bytes, so a page is equal to 4096 bytes. Pages are used to manage memory more efficiently, as the operating system can allocate and free memory in whole pages, rather than individual bytes.
When you create a variable in Python, the interpreter allocates a block of memory that is large enough to hold the value of the variable. The size of the block of memory depends on the type of the variable. For example, a variable that stores an integer (i.e., a whole number) requires a block of memory that is 4 bytes in size. A variable that stores a string (i.e., a sequence of characters) requires a block of memory that is large enough to hold the string.
The memory block that is allocated for a variable is represented by its memory address, which is a unique identifier that tells Python where the variable is stored in memory. The memory address is an integer value that is typically displayed in hexadecimal format. For example, the memory address of a variable might be 0x7ffeefbff580.
Now, you might be thinking, "Okay, but what does that location in memory actually look like?" Well, let's get creative here. Imagine that your computer's memory is a giant library, with each shelf representing a different location in memory. When you create a variable in Python, it's like checking out a book from the library. The librarian (a.k.a. the Python interpreter) assigns you a shelf where you can store your book (a.k.a. your variable's value). When you're done with the book, you return it to the librarian and they put it back on the shelf for someone else to use.
All these explanations might not be sufficient to make you understand clearly how the variables are stored. So let's clear the concept using an example!
Imagine that you're a librarian, and you're responsible for storing and organizing books in a library. The books represent the data in a Python program, and the shelves in the library represent the memory in a computer.
When you create a variable in Python, it's like putting a new book on a shelf in the library. The book represents the value of the variable, and the shelf represents the memory location where the value is stored.
Let's say that you create a variable called ‘ x ’ and assign it the value 42.
So the question which might arise is where does that value get stored in the computer?
Well, it's kind of like putting a book on a shelf in a library. When I create the variable ‘ x ’, Python reserves a block of memory in the computer to store the value 42. It's like putting a book with the title ‘ x ’ on a shelf in the library, and filling the pages of the book with the number 42.
So again the question arises, how does Python know where to find that value when I refer to ‘ x ’ in the program?
That's where the memory address comes in. When Python creates the variable ‘ x ’, it assigns a unique memory address to the block of memory where the value 42 is stored. It's like putting a label on the spine of the book with the address of the shelf where it's stored. So when you refer to ‘ x ’ in your program, Python knows to look at that memory address to find the value 42.
What happens when the value of ‘ x ’ is changed?
It's like erasing the contents of the book and writing something new. When you assign a new value to ‘ x ’, Python replaces the old value in the block of memory with the new value. So if you assign ‘ x ’ the value 10, Python erases the pages of the book with the number 42 and replaces them with the number 10.
Below is the example of where the variables are stored in memory, i.e their memory location.
x = 10 #integer variable greet = "hello" #string variable complex_number = 2+3j #complex_number variable # prints the memory location of the integer object x print("Memory location of x is: ", id(x)) # prints the memory location of the string object "hello" print("Memory location of greet is: ", id(greet)) # prints the memory location of the complex number object 2+3j print("Memory location of complex_number is: ", id(complex_number))
Output
Memory location of x is: 2314050300496
Memory location of greet is: 2314130349424
Memory location of complex_number is: 2314135103984
Library
WEB DEVELOPMENT
FAANG QUESTIONS