Storing data in memory using variables in Python

Imagine you’ve just started your journey in the coding world and don’t quite understand what all these “Hello World” programs are actually doing. At some point in time this was EVERYONE so no need to feel alone or embarrassed. In this article I’ll go into deeper detail over what a Python program is doing when you do the code below.

number = 12
word = "Hi"

In line 01 we are declaring an integer with value 12 and assigning it to the number variable.

At a deeper level Python itself is typically a C++ based program that interprets the py files and throws all the objects and data structures it finds into a private abstracted heap but we will talk about the examples above ignoring this abstraction and discussing the variables as if they were the raw int and string type you see in modern c++ or java.Our number variable will get interpreted as a small integer and will likely get dynamically casted as an int16 or a “short” variable type which can store numbers from 0-65,535 for an unsigned short or -32,768 to 32,767 for a signed integer where the first bit represents if the number is positive or negative.

Behind the scenes when we run our program the number reference will get stored in our variable stack, the program will allocate 16 bits of memory to store our ‘short’ into and assign that a value of 0000 0000 0000 1100 to represent the number 12, and then our number reference will get assigned the memory address which points to our ‘short’.

Engineering tip:
Note the number 12 could also get represented with only 4 bits of memory
space but with most large memory based systems the program will typically
use larger datatypes assuming that our number will change or "mutate". If
we were on an embedded system with low memory requirements we could use a
const int4 to store our value and optimize our memory space

Now in line 02 we are declaring the word “Hi”. Since a computer doesn’t know what a word or a letter even is, or numbers outside of 0 or 1 for that matter, the program has to do something special.

Because of this ASCII was created as a system of representing the main English characters with numbers which can get converted to binary and stored by a computer system.

There is also another system called unicode which is a superset of ASCII
and contains Latin and other characters from various languages.

Back to our “word” variable the program assigns this variable, will allocate two bytes of memory for each of our letters, map the letters to the ASCII binary representations, and update our variable with the memory reference the points to the start of our character array. An array is just a succession of memory space to store values in and can be thought of as buckets of data.

Using the ASCII table we can see “H” has a value of 72 and “i” has a value of 105. This will get stored in memory as the hex values 0x48 and 0x69 or the binary representations of [01001000][01101001] with the memory pointer of “word” looking at the “H” memory address.

I hope this article has helped shed some light of variables and data storage in computer programs. Please comment with any questions or things I may have said incorrectly and follow for more programming and Python focused content!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s