I once asked a student of mine, who is barely 10 years of age, to write a for loop in Python 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. "Why do I need a for loop to look at everything? 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 what I would tell him.
"Computer science is only two things. Adding abstractions and taking them away."
A computer consists of many layers, each building on the one below it and providing some sort of function to the layer above it. There’s the hardware layer that physically stores data and performs computations using electronics, the operating system that communicates to the hardware, and applications like your text editor that rely on the operating system to get resources they need. All these layers stack on top of each other. Together, they enable you to get certain tasks done - like watching TikTok videos in the middle of the night.
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. It is a car's engine that determines how fast a car can go, not the speedometer. The way the CPU sees and processes information is going to in some way dictate how you write your code in Python and Java.
So, let’s revisit our question: why can’t the computer simply look at the screen and identify where the number 7 is located in a list? First of all, the computer does not actually “see” images of the screen like we do - all it “sees” is data, which are just 0s and 1s arranged in meaningful patterns to represent information, like the number 7. The data then gets turned into graphics that you see on the screen. But the question persists - since the computer can “see” the data even in 0s and 1s, why can’t it just know that the data representing number 7 is right there at the 5th position? Why do we need to tell it to look at the numbers one by one?
To answer this, remember I said that the way the hardware works informs the way we write code. Let’s go to the hardware level and think about how the CPU actually “sees” the data. It works by looking at small bits of data sequentially — much like a person looking through a tiny straw, the CPU has really narrow “sight” and can only focus on a small part at a time. It also has bad “memory”. The CPU does not retain data long-term; it only stores a tiny bit of data that is necessary for computation, in these tiny regions called registers. The bulk of the data is stored in a region called Random Access Memory (RAM), and will get loaded into the CPU bit by bit when it’s their turn to be processed by the CPU.
To a CPU, items in a list might seem like closed doors that it has to open one by one to access and process.
So, we just looked at how the CPU has to process data bit by bit. We need to sequentially load some 0s and 1s into registers while doing simple calculations like if two values are equal. Therefore, it’s up to the layers above the CPU to instruct it what numbers to load when, and what calculations it needs to perform. Depending on how a programming language is designed (you can design your own programming language ), the programmer might be able to type find position of number 7 in numbers_list
(essentially the index() method in Python) and get the result - but under the hood, at some point, that instruction needs to be turned into something like a for loop in Python that instructs the CPU to look at one number at a time, because that’s how it’s designed to work. In most cases, the code you write in Python will be converted into Assembly, which is a really low-level language that contains all the instructions you could give to the CPU, like MOV (Move), CMP (Compare), and ADD. If you look at the assembly code for the Python for loop you wrote, you will see that it is indeed just loading one number of the list into the CPU at a time, and comparing it to your target, 7!
All of that is hidden behind something like position = numbers_list.index(7)
which hides behind layers of abstractions. No matter how you write a set of instructions in python or any high-level language it will at some point have to be converted into 0s and 1s.
numbers_list = [1, 2, 7, 4, 7, 9]
position = numbers_list.index(7)
print(position)
# same as using a for loop under the hood.
x = 7
for i in range(len(numbers_list)):
if numbers_list[i] == x:
print("found")
Humans aren’t that different from computers — when faced with complex information, we too tend to analyze it bit by bit! Imagine searching for Waldo in an 'I Spy' book. You would probably look at each item one at a time, which is similar to how a CPU performs a linear search — examining each person sequentially until Waldo is found. Computers can perform such tasks billions of times faster than humans. So, it might not be good at examining small arrays on the screen without being methodical about it, but the way it is built allows it to perform tasks such as examining trillions of items in a second. Were you able to find Waldo?
Credits
Thanks to the people of reddit for their comments
spederan : looking for Waldo analogy with linear search
not-just-yeti : the analogy on Computers have 16 eyes or more — registers
electropoptart : Thoughtful comments
All my friends that read drafts for this essay
Further readings
List implementation in python using Cpython (an API for Python to create objects in C).
There is another budding field of AI where there is such a thing as computer vision different than looking for an item in an array ( traditional computation), where a computer looks at the world to make sense of it. Similar to face detection. Here is a cool article I found [Source]