 In this video, we're going to look at our very first example of a Python program again. And this time, we're going to run it in PythonTutor to see how we can use PythonTutor to show interactive programs even better. So let's create a new file here. And let's call it a first example, Revisited. So remember that the example that we had was finding the average of all even numbers in a list. And I gave you a list to work with. We call the list numbers. And let's put the numbers in there from the previous video. So 7, 11, 8, 5, and 3. Then we have 12, 6, and 2. Then we have 9, 10, 1, and 4. So these are the numbers that are given. And remember that our very first solution approach to this problem went as follows. So we started with a for loop, and we wrote for number in numbers. And then we said that for every even number, we want to do something. And so we have to check for even numbers. So we say if number modulo divided by 2 has no rest, then we want to do something. And the way about that was we initialized two variables called total and count. And we fed them both to 0. And then what we did inside the for loop, whenever we encounter an even number, we'll go ahead and we take our old total and add to it the current number. Because we know that the current number that we are looping over is even. Okay, we're checking that before. And then in the same way, for the variable count, we update the old count and simply add 1. And at the end of the cell, to calculate the average would simply say total divided by count, which gives us 7.0 as before. So now let's make a couple of simplifications here. So we saw in a previous video that whenever we have a situation where we are updating a variable, we can use a Nythas syntax instead of saying total will be set to the old total plus number. We go ahead and say total plus equals number. And in the same spirit, we say count plus equals one. So this is a very minor modification, but it makes the code, I guess, a little bit more readable. And this still works, of course. Then also in a previous video, we had an alternative solution. And the alternative solution went like this. We wrote a so-called list comprehension, which is an expression that derives a new list object out of an existing one. It goes like this. We go ahead and we copy pay or we write brackets because we want to create a new list. Then we copy paste the for loop without the colon here. We paste it in there. Before that, we have to write the variable that we are using in the loop, which is basically the number variable here. And then we take, in this case, the if line here, the if statement, and we also copy paste it here without the colon. And if I go ahead and execute this, I get back a new list object, which only has the even numbers. And now to make this a little bit more concise, instead of saying number singular, we will simply replace that with just n. And now we have a very compact expression that derives a new list out of the existing list that only contains the even numbers. And now we go ahead and we assign that to a variable called evens. So we are giving a nice understandable name to what we are doing. And in the next step, we're introducing a variable called average. And we assign to it the result of the calculation, which is going to be the sum of the evens divided by the length of the evens or the number of even numbers. And then at the end of the cell, we simply will evaluate the expression average. So we will simply basically show ourselves the end result. So these are two different approaches to the same problem. And now what we are going to do is we're going to the online tool Python tutor. And we are going to execute this program as it runs in Python tutor. So let me first go ahead and copy paste our numbers list. And then we're going ahead and we copy paste our first solution over here. And let's go ahead and the result here. It's also assigned that to a variable called average. If we don't do that, we will the result will not be visible in Python tutor, but that's just the minor detail. And now let's simply go ahead and click on visualize code and see what we are going to see here. So now on the left hand side, we see that we have all the code and the code has not yet been run. So the red arrow here shows us which line is going to be run when we click on next on the right hand side. We see what is going on in memory. So let's click next ones. And this is going to create a new list object here called numbers. And there is a reference going to list object. And in the previous video, we saw that this is a simplification. So these numbers here, they don't really live inside the list object, but they are rather objects on their own. So inside the list, we only have references to other objects. But again, this is a simplification in Python tutor. And now also, maybe you are confused by the term here global frame. We will learn about frames in a future video. For now, just understand that the term global here means that these variables are visible anywhere in the code. And this is roughly how we can translate this for now. Let's continue. I click next one more time. And on the left hand side, we see the red arrow shows me which line is going to be executed next. And the green arrow shows me which line was executed last. So let's go ahead and create a new variable called total. And we know again that this is a simplification. The zero is really on the right hand side as an object. And we have a reference going from the total variable to the zero object. But this is a simplification that is OK to work with. We also create a variable called count, also set it to zero. And now we are going to run over the numbers list. And in every iteration of the loop, we will set number singular to the next number in line, so to say. So let's do that. Let's click on next ones. And number is automatically set to seven, because it's the first number that is in the list. Then what we are going to do is we are going to check if number divided by two has no rest. And the answer to that is, of course, false. So what we are going to see is if I execute now the next line, we're going to see that the red arrow jumps right back to the top. So if I execute the if number divided by two line, the red arrow jumps right back to the for loop. So in other words, two lines here are never executed in the first iteration of the loop. So next iteration, on the right hand side, number singular is set to 11. 11 is also an odd number. And that means we are going to jump right back to the top of the for loop. Next number is eight. And now what we see is that the number eight, it is even. So now we are going inside the if statement. So it is the so-called if branch, that is sometimes what we call that. And the two lines that we see here, the total plus equals number and the count plus equals one, they are now going to be executed. So let's click on next. Now the red arrow jumps for the first time into this inner code block. And we are going to add two total, which currently is zero, the number, the value of the current number, which happens to be eight here, right? So on the right hand side, we see that number is eight. And this eight is going to be added to the total. Okay, so if I click next ones, total will now become eight. If I click the next button one more time, count will be incremented by one. So zero becomes one here. And the red arrow jumps back to the top of the for loop. Next iteration is the number, number becomes five. It is odd. So that means we are not going to execute the code here. We are jumping right back to the top and go to the next number, which is three, which is also odd. Nothing is going to happen. Okay, so let's click one more time. The next number will be 12. And 12 of course is an even number. So that means we're going inside the if branch. And that means to the total, which is currently eight, we are going to add 12. Okay, this is now going to become 20. And then count will also be increased by one to make it two. And then the red arrow jumps back to the top of the for loop. And the next number would be six and then two and so on. So I will now speed this up a little bit. The six is an even number. So we are going to add it. The total will become 26. The next number is two. This will also be added to total, which will become 28. The count is four in the meantime, nine. The next number is going to be odd. So we are going to skip that addition. Then comes the 10. This will make the total 38 and the count five. Then comes one, which is odd, which means we skip it. And then comes the last number in line, which is the four. And we add it to 38 to make it 42. And then the count becomes six, of course. Okay. And now we are going to jump to the last line here. Where, so now the for loop is done. Okay. So after the numbers, we have iterated over all the numbers. The entire for loop is done. And we are going to jump to the last line. And at the right-hand side, it's going to be evaluated first. So total will be divided by count. So 42 will be divided by six. This will become 7.0. And 7.0 will be stored in a new variable called average. Okay. And that ends the program. Okay. So now what we see here is how we can use Python Tutor to run our code interactively. And as a beginner, you will make a couple of mistakes, of course, in the beginning. And I think Python Tutor is a very good tool for you to copy paste over your code and run it step by step in slow motion. And this way you can probably find errors that you make quite easily. Okay. Let's also go ahead and copy over the alternative solution right here. And we don't need all of this temporary stuff. So all we do is we create the numbers list and then we do the calculation like this. And so we create the numbers list as before. And now something interesting can be observed. It's now on in the next line, which is the line that starts with events. The right-hand side is going to be evaluated first. And this is going to be a list comprehension and a list comprehension sometimes can be viewed or it should be viewed as a hidden for loop, so to say. So what is going to happen is we get a new area here on the right-hand side in blue, a new frame, but we will learn what a frame is in detail in a future video. And right here, what we see here is as we click next a couple of times, we see that there is a variable called N. This is of course the N that we have here on the left-hand side. And what's going to happen is we see how the list comprehension here is really just a for loop, a hidden for loop. And as I said, the list comprehension will derive a new list object out of an existing one. So what is going to happen is once the loop is done, as the return value of the list comprehension, we get back a new list object. And now we see that we have a second list object here with only the even numbers. And next, we are simply going to use the built-in function sum and len. And they will basically sum up all the numbers here and then also count the number of elements inside the even list. And this will also result in 7.0. But we see that compared to our first solution, the approach is totally different. So here we have a temporary list, which is basically in the middle step, holding only the even numbers. And we also see how this is affecting the memory usage of our program. So our first solution looped over the list and did all the calculations on the fly and came up with the end result. Here in this solution, in this alternative, we have a second copy. We have a temporary copy, which is evens, the evens list. So in a worst case scenario, let's say I give you a numbers list that only has even numbers in that, the evens list will be of the same size. It will have the same number of elements as the numbers list. Therefore, the implication is that our second solution here may lead to a situation where it takes twice the memory as our first solution approach. So the code results in the same result. That is good. So both, in that sense, both solutions are equivalent, but the second solution here has a higher memory usage. So you may think that this solution is actually worse, right? In the video on best practices, I explained to you that this solution is preferable because it's easier to read than the other solution up here. So the second solution for a programmer if you want to maintain a code base, it is easier to read and understand. However, it has a downside. It takes more memory possibly. However, in also a future video, we will see a way where we can write code in this way, in the alternative way, without creating a second list object, okay? So this is then an advanced topic of how we deal with memory here. And this is of course an important topic because imagine that the given numbers list is so big that it occupies let's say 60% or 70% of our computer's memory. Then this could lead to a situation where our computer runs out of memory and then it simply dies. You all know that you all have experienced your computer dying and then you have to hard reset it, so to say. And that is an example of a computer program dying if the computer runs out of memory. And this could happen in this situation, given big data, okay? For our toy data set here, this is not gonna going to happen, but for real life data set, this could happen. So we have to find in a future video another approach of how we can solve a problem in a memory efficient way. But in this video, I only wanted to, or I mainly wanted to show you how you can use Python Tutor to show an interactive program and how you can use it to make visible what is going on in the memory. And then other things should will be discussed in other videos, of course, okay? So I hope you like Python Tutor and I can just encourage you to use Python Tutor early on when you run into problems and you don't know how to proceed, then just use Python Tutor to debug your code. And then this is a good way to learn how to program.