From the course: Python Essential Training

How computers think - Python Tutorial

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 file name. That file name 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 file name 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 4 kilobytes long. So the computer knows that if you want to access the contents in this file, it follows the pointer to where the file starts, reads the next 4 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 2, B is equal to 3, and C is equal to A plus B. The computer will read the first line, which is assigns the value 2 to the variable A. So it makes a record that says the variable name A points to the value 2, the data at this location. Then it does the same thing for B, makes a variable called B, points it to the data 3 stored at, 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 2, then goes to B, looks up its location, fetches the value 3, 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. 1, 2, 3, 4, 5. 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 1, 2, 3, 4, 5. 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 1 to a 6. 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 going to 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