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

Tracing arithmetic expressions | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

How does the computer evaluate expressions with multiple operators, multiple function calls, or even nested function calls? That's a function call inside the parentheses of another function call. To examine this order of operations, let's trace a program with some complex arithmetic expressions.

As always, the first thing the computer does when we run the program is load the first instruction into its working memory. When it sees an instruction like this, where there are nested function calls, it's going to search for the innermost parentheses first. Then it starts evaluating from the inside out. Here, in the innermost parentheses we have the string "how many servings." This is already a single value, so it doesn't need simplifying.

Okay, so the computer's going to peek outside of those parentheses and ask, "What did you want me to do with this value, how many servings?" It sees the input function. The computer takes that value, displays it in the console, pops up a prompt, and then waits for the user to enter something. The computer's done with this part of the instruction, but it won't continue evaluating until it gets an answer back from the user.

Let's say I type in five and hit enter. Where does that five go? Well, the input function returns out whatever value the user entered. Remember that the input function always returns that entered value as a string. So this isn't the integer five; it's the string "five." Great! Now we have a much simpler expression. The computer goes to the parentheses first, sees that it's already a single value, so there's nothing to do. It peeks outside the parentheses and sees the int function.

The int function takes that string "five," casts it to an integer, and returns out the value five. Now we just have a standard assignment statement. This statement tells the computer to go off to its memory, allocate a new chunk of it to remember a new value, then it tags it with the name servings and sticks that value five in there. This instruction is now complete. The computer did everything, so it's going to clear out that working memory and look for that next line of the program.

Okay, we load the second instruction into working memory. Looks like we're just doing some math here. We follow PEMDAS or GEMAS—that's parentheses, exponents, then multiplication and division, and then addition and subtraction. So we've got parentheses first. The computer evaluates 6 - 2. Then we're left with only multiplication and division operators, which are at the same level of precedence.

So the computer just evaluates left to right. The first thing it sees is this variable servings. It needs to go off and access that chunk of memory that was associated with the name servings. So it goes here, sees servings, follows that line, and it finds the value five. So we substitute that in here. 5 * 4 is 20. Then we have 20 divided by 3. This is the float division operator, so we get 6.666 repeating.

Now it sees that equal sign and it says, "Ah, you want me to assign this value." I finally have to the variable spices. It checks its memory and sees that it has no recollection of spices, so it allocates a new chunk, tags it with a name, and sticks that value in there for later. All done! Clear it out.

This next line is blank, so the computer skips that. Then the line after that is a comment because it starts with this hashtag character, so the computer ignores that too. Comments are for humans, not for computers. Load in that next line and here we have two sets of parentheses. But those parentheses are at the same level of precedence; they're not one inside of the other.

So that means we're just going to evaluate them left to right, starting with those left parentheses. The computer is going to grab that value of spices from its memory and substitute it in. Now it has two values inside these parentheses separated by a comma, but they're both simplified all the way down, so we're good here. We pop out the parentheses and we see the min function, and the min function returns out the minimum of those two input values.

Now it jumps to the next set of parentheses, substitutes in the value of servings—that's five—two single values. It pops outside the parentheses, sees that it wants to take the maximum. 5 is greater than 4, and now we just have two numbers to add together. We have a single value now on the right-hand side of the equal sign, so we can go ahead and do that assignment.

Checks in its memory and sees, "Oh, I already know spices, so I'm just going to erase what's in there and replace it with this new value." Cool! Clear out working memory, move on to the next line. Now, that's a lot of parentheses we're starting in and we're moving out. We've got an expression here that needs simplifying. We're substituting in spices and then we're adding three to get that down to a single value.

Now everything inside here has been simplified, so we peek out and see the round function. The round function tells the computer to take that first value and round it to the number of decimal places that the second value says. Here we're rounding to two decimal places. Now we're in the next innermost parentheses. We already have a single value, so we pop outside those parentheses and we see the string function.

The string function casts this to a string and returns out that value. Now we're in the last set of parentheses. We're concatenating two strings, so we just smoosh them together. And what should it do with this value? Oh, print it! There's no next line, so it terminates the program execution.

And when it terminates, that means the computer also forgets everything in its short-term memory, so all these values go away too. That's it for this program. Remember, if you ever have a complex expression, the computer always evaluates innermost parentheses to outmost.

More Articles

View All
When business goals backfire: How to adjust to unintended consequences | Big Think
There are several questions, essential questions that leaders and managers should ask themselves when they look at how they want KPIs to have an impact and influence on their organizations. The most important one is obvious: What’s most important to the o…
Donald Trump's Immigration Policy Is Racist, but He's No Fool, Says Jesse Ventura | Big Think
Donald Trump is not a fool. He’s been a friend of mine for 25 years. I call him a friend. I don’t agree with him on many of his issues. We’re 180 degrees apart. But Donald Trump is not a dumb man. You don’t acquire the wealth and the power in the private…
You Swallowed a Tapeworm – What Now? #kurzgesagt #shorts
BL, you swallowed a tapeworm by accident. What now? Maybe you ate infected raw meat, or perhaps you drank contaminated water. Well, that’s less important now; they’re in your body. How bad it might get depends on whether it’s a lava or an egg. Option on…
5 Money Lessons I Wish I Learnt Sooner
Hey guys! Welcome back to day three of the new money advent calendar. We’ve started off strong, three videos in a row. Um, I’m going to get real tested at like the 20th and the 21st of December, 22nd of December. Yeah, it’s going to be tough. I have a fee…
Khanmigo is now available to the public (US only)| Personalized AI tutor & teaching assistant
Hi everyone, Sal Khan here, and I’m excited to announce that Khan Migo, our generative AI-powered tutor on Khan Academy, is now generally available! This is especially powerful as we go into back to school. If you have Khan Migo, your student has it on th…
Ending Your Inner Civil War (Carl Jung's Psychology)
What drives people to war with themselves is the suspicion or the knowledge that they consist of two persons in opposition to one another. The conflict may be between the sensual and the spiritual man, or between the ego and the shadow. Carl Jung, Swiss …