I once asked a student of mine, who is barely 10 years of age, to write a piece of code to find the location of the number 7 in a list. He gave that usual confused look, and I braced myself for the question I knew was coming. With a tilt of his head, he asked me something so innocent and yet sent me down a rabbit hole of thought. "Can't the computer just see it's right there on the screen, at the 5th position?" Seems like a simple question, but it took me quite a while to formulate an answer.. If I were to answer that question again, here's how I would do it.
I will respond with 1 word and two sentences. “Abstraction”, “data structures are fake”, and “dumb but fast”.
"Computer Science is only two things. Adding abstractions and taking them off."
A computer consists of many layers. Each layer builds on top of the other and provides some sort of function to the user or the layers above it.
The hardware that stores memory and computes outputs to inputs, the operating systems that talk to that hardware, and your text editor that helps you write your Python code. All layers help each other get that job done.
Today We will try to take off abstractions in hopes of finding the limitations of each layer. You will find out that things that are possible at the hardware level are going to be the limit of what is possible at the operating system level, and all the levels above.
It is a car's engine that determines how fast a car can go, not the speedometer.
So how does Python process information? Will have to look at a layer below it
Python is a layer of abstraction on top of this language called C (who knew!). C being a low-level language it can talk to the hardware using the operating system. It can ask for the operating system to store a particular integer or character at a particular location in memory and ask for it later. So when Python stores a variable. In reality, it is asking C to do it.
Similar to a coat check or dry cleaning.
That is how things such as variables are stored in a computer. Below is an example of how C stores a variable in memory. Don’t worry about all the syntax just make sure you understand the mechanism of storage.
// Declare a pointer (a tag that points to an address) to an integer
int *ptr;
// Allocate memory for one integer at this will be the storage unit where your clothes are stored.
ptr = (int*)malloc(sizeof(int));
// Assign a value to the allocated memory, store the value in that memory.
*ptr = 42; //assign the integer value 42
As we move on just take one thing from this
What if I told you arrays are not real, at least at the hardware level? At that level, the hardware has no idea what an array is. How strange!
Even in C, the programming language at times has no idea what an array is. What do I mean by that? Do you remember the variable we created in C using a pointer? That is our array. If you are a high-level programmer programming in your cushy job in Python you might charge me for Blasphemy. But hear me out. An array is just a tag that leads us to a particular memory address from where we can retrieve what was stored in there. Like our helpful coat check. What about all the other items in the array? Where did they go?
With an array, we just add a clause that all the other numbers in the array will be stored in subsequent memory spaces. We can assume the same thing when we are retrieving them. The way numbers are on the screen is how they are represented in memory. Right next to each other.
Just remember, each whole number (integer) in a computer's memory occupies 4 units of space, called bytes. So, to find the address of the third number in the sequence of integers, you count three numbers ahead, which means you skip 12 units of space (since 3 numbers × 4 bytes each).