yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Tracing program execution | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

Let's trace a program step by step. This is a common pattern we'll use to understand what the computer is doing under the hood when we press the Run button. Tracing program execution like this helps us better read and write programs because we can start to predict what the computer's going to do with each instruction without having to go through a long process of trial and error.

For now, we'll write this all out together so we can make sense of what's happening here. But eventually, we'll get familiar enough with Python where we can trace small blocks of code like this in our heads.

When you press Run, the computer looks at your program line by line. It isn't smart enough to zoom out and look at the big picture and try to understand what the program is trying to do as a whole. When I say line by line, I literally mean that the computer is going to load each line of your program into its working memory one by one. Then, with one instruction loaded into its working memory, it's going to interpret that instruction in isolation. In total isolation.

Now remember that the computer is not a mind reader, so it doesn't understand any nuance or intention behind your instruction. It's only going to do literally what that instruction says according to whatever rules of Python.

Let's see that in action by tracing this program. The computer takes the first line of the program and loads that into working memory. Then the first thing it's going to do is look for any expressions to evaluate. Remember that evaluating an expression just simplifies it down to a single value. Here we have the expression the integer 4 plus the float 2.055, and that evaluates to the float 2.455.

Now all our expressions have been simplified, so the computer's going to peek outside the parentheses and ask, well, what did you want me to do with this value? The instruction print tells it to take the value inside the parentheses and go display that in the console. So it goes over here and it prints 24.5. This instruction is complete, so the computer's ready to move to the next step.

But first, it wants to optimize its brain space. It doesn't really need to remember this instruction anymore. It doesn't need this information, so it just clears out its working memory and forgets, and that makes room for the next instruction.

Now the computer loads the second line into its working memory, and again it looks for any expressions to evaluate. It sees the expression 3 + 2, and it simplifies that down to the integer 5. Notice that there's no print instruction here. We didn't actually ask the computer to do anything with that value, so the computer's thinking, well, hey I just did all this work, I figured out the answer is five, but I guess you don't want me to tell you. So it shrugs, whatever, and it clears its working memory out, forgets that five, and just moves on.

Third line, the computer loads print("learn" + " " + "more"). But there are two operators in this expression; there's two plus signs. So the computer is actually going to evaluate this expression in two steps, reading left to right. First, it evaluates the expression "learn" + " ". Now when we add strings, remember that we are concatenating; we are smushing together, so we get the string "learn ". Then we add the string "more". We concatenate, and we get "learn more".

We're down to a single value, so the computer peaks outside the parentheses, sees that we want it to print that value, and it prints "learn more" in the console. Finally, it clears its working memory and it moves to the next line.

What do you think the last two lines of this program do? Take a second and try and trace it yourself.

Okay, this instruction has the expression the string "81" + the string "1 9.42". Now these may look like integers and floats, but because there are quotation marks around them, the computer is going to treat them like strings. So when we evaluate this expression, we are concatenating strings, and we get the string "811 9.42". Nothing left to simplify here, so the computer pops outside the parentheses, sees the print, and then prints "811 9.42" to the console.

Then it clears out working memory and moves to the last line. The computer loads the last line of the program into working memory. Notice that this whole thing inside the parentheses here is surrounded by quotation marks. That means this is already a single value; it's the string " 2 + 2". It's not the expression the integer 2 + 2 because we already have a single value. There's nothing to simplify here, so the computer pops outside the parentheses, sees the instruction print, and prints "2 + 2" to the console.

Then it clears working memory and jumps to the next line, and oh, there is no next line. We are at the end. We did it! So the computer terminates the program execution, exits, and we have our final result here in the console.

Want to check my work? Copy this program into a code editor and run it for yourself. Is the result the same?

More Articles

View All
High Seas Rivalry | Wicked Tuna: Outer Banks
I’m stuck. We’re staying. Pretty sure Fren’s even staying. Yeah, he has to, though; his title’s on the line. Yeah, he knows. He hasn’t said a word on the radio to us. Uh, he probably won’t. We got three fish; Frenzy’s got four. I got to admit it, I absol…
Michael Burry's Warning for the Stock Market Crash
On May 19, 2005, Michael Bury bought his first credit default swaps in anticipation of the housing crisis: 60 million of credit default swaps from Deutsche Bank, 10 million each on six different bonds. His prediction: the U.S. mortgage-backed security, on…
Positive and negative intervals of polynomials | Polynomial graphs | Algebra 2 | Khan Academy
Let’s say that we have the polynomial p of x, and when expressed in factored form, it is (x + 2)(2x - 3)(x - 4). What we’re going to do in this video is use our knowledge of the roots of this polynomial to think about intervals where this polynomial would…
Avoid THESE Poor Habits Before You Burn Out | Rachel Hollis Interview
Think it really is a way to set your, you know, the last quarter of your life in a way that you really enjoy it. I drove myself into the ground with bad diet, bad sleep, bad habits of every kind. What the [ __ ] was I thinking? So, I guess let’s start wi…
Tim Brady - How Much Equity Should I Give My First Employees?
[Music] How much equity should you give your first set of employees? This is more art than science. Unfortunately, there’s no chart I can point you to where you can look up the number of employees and experience and get an exact figure. That’s not how it…
Just Lost Everything | The Freaky Truth Of $1 Terra Luna
All right guys, I was out of town this last weekend getting beat up by Michael Reeves. But now that I’m back in my office, let’s talk about the collapse of Terra Luna. Because I have to say, this was the most catastrophic large-scale event in cryptocurren…