From the course: Python Essential Training

How computers think

Computers. What goes on inside their heads? How do they think? Well, it's nothing like this. Thanks. Don't mind him. Here, I'll show you how computers think. You're probably familiar with storing files on computers. When you save a file, like a text file, it has a file name and some content, all the text inside your file. Here, this grid represents your computer's memory, where it stores files and data. And down here, I have a filename. That filename is represented in your computer's file directory, and you can think of it like the file icon that you click on. And stored with that filename is the location or address of exactly where the contents of that file are in memory. See, computers are really good at jumping straight to a specific spot in memory if they know what the address is. And that address is literally a number. every spot in memory gets a unique number. And because the computer can jump directly to that location if it has an address, we call this address a pointer represented by this arrow. And these pointers point to where the start of the file contents are. There's also information stored with the file name. For example, the size of the file. So this file might be four kilobytes long. So the computer knows that if you want to access the contents of this file, it follows the pointer to where the file starts, reads the next four kilobytes, and then loads all of that content for you. Now, when we write and run a computer program, what we're doing is directly interacting with the computer's memory. Programs are constantly making little bits of data, kind of like mini files. These bits of data are called variables. They have names, variable names. They point to little bits of data that we store in the computer's memory. So for example, let's create a simple program. A is equal to two, B is equal to three, and C is equal to A plus B. The computer will read the first line, which assigns the value two to the variable A. So it makes a record that says the variable name A points to the value two, the data at this location. Then it does the same thing for B, makes a variable called B, points it to the data three, stored it, say, this location. Then it comes to this line. The computer first calculates the value of A plus B. In order to do this, it looks at the variable A, sees its memory location, fetches the value two, then goes to B, looks up its location, fetches the value three, adds those two values together, and stores it in a third variable, C. Now, why do we need to know what's actually going on behind the scenes when computers are executing a program? This program is pretty straightforward. I don't need to know how the computer is storing and fetching all of this data when it runs. But consider this situation. A is equal to a list of numbers. One, two, three, four, five. In Python, this notation with the square brackets indicates a list. you have a list of values instead of a single value. So A is equal to one, two, three, four, five. And let's say that B is equal to A. Now, what happens if we modify some value in our variable A? This right here. Let's say we change the one to a six. Turns out we're modifying the value of both A and B. By saying B is equal to A, we're not duplicating A. we're literally assigning B to the same location in memory. And this concept is gonna come up time and time again as we start programming. When we write programs, we're working directly with the computer's memory. We're putting data into it, we're fetching data out, we're manipulating this data. So having a mental model of what's going on behind the scenes is extremely important.

Contents