- 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 and 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.
And 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 gonna 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 four plus the float, 20.55.
And that evaluates to the float 24.55.
Now, all our expressions have been simplified,
so the computer's gonna 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.55.
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 three plus two,
and it simplifies that down to the integer five.
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's 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 the string learn plus the string space.
Careful, this is not the empty string.
There's one little space character
in between these quotation marks.
Plus the string more.
But there are two operators in this expression.
There's two plus signs.
So the computer's actually going to evaluate this expression
in two steps, reading left to right.
First, it evaluates the expression, learn plus space.
Now, when we add strings,
remember that we are concatenating.
We are smushing together.
So we get the string learn space.
Then we add the string more.
We concatenate and we get learn space more.
We're down to a single value,
so the computer peaks outside the parentheses,
sees that we wanted to print that value,
and it prints learn space 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 plus the string 19.42.
Now, these may look like integers and floats,
but because there are quotation marks around them,
the computer's going to treat them like strings.
So when we evaluate this expression,
we're concatenating strings
and we get the string 8119.42.
Nothing left to simplify here.
So the computer pops outside the parentheses,
sees the print, and then prints 8119.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, the two character,
space character, plus character, space character,
two character.
It's not the expression the integer two plus two.
Because we already have a single value,
there's nothing to simplify here.
So the computer pops out the parentheses,
sees the instruction print,
prints two space plus space two to the console,
and clears working memory.
Then it jumps to the next line of the program,
and oh, there is no next line.
We are at the end, we did it.
So the computer terminates the program execution,
it exits, and we have a final result here in the console.
Wanna check my work?
Copy this program into a code editor
and run it for yourself.
Is the result the same?