This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00hey welcome to a new exclusive video which we post weekly and in this video we will take a look at a very very useful tool for learners called python tutor so if you're learning python or also other languages this is used this is available for python java c c plus plus and javascript um if you are learning python this tool will help you understand the inner works of how the
SPEAKER_00programming language works in our example Python so I'll take you through a tour of this tool. I'll explain you why this is very useful and How you can make the best of this tool so you understand Python from a deeper perspective that will help you in the long run so you can go to Python tutor.com and then click on Python and We'll scroll down here we can ignore that what's above I'll explain you everything step-by-step so you
SPEAKER_00don't have to go through that example I'll choose the latest version Python 3.11 and let me take let me write some codes here and explain you what this tool does so in this code we have a list of integers this list has been stored in the numbers variable and then we iterate over that list so for x in numbers We calculate the square of each number, so 1 in the square of 2, 2 in the square of 2, etc.
SPEAKER_00And we store the result in Y and then we print Y. Normally in an IDE you execute the code and you would get If we execute this same code, we'd get this result. So one, four, nine, 16, each number in the square of two. But to understand what happens behind the scenes, how Python executes the code, you can use Python Tutor like this. You press on the visualize execution button here.
SPEAKER_00And that will display here on the right a view, a panel. And now, you see this red arrow pointing to the first line? That arrow indicates that if you press next, Python Tutor will execute the first line. Because what happens actually when we execute a script in a normal IDE, is that the interpreter executes the line one by one, from top to bottom,
SPEAKER_00so it executes numbers first, then this, that, that, but it does so in a split of a second, so we only see the final output. But this interface here will execute everything step by step, one line at a time. So by pressing next, you are executing the line highlighted with a red arrow. So let's press on next to execute that line. So when we press next, you see now this returns to green, which means it was just executed.
SPEAKER_00And then the arrow goes to the next line. So if we press next again, it will execute this line for x in numbers. But we want to stop here and take a look on the right panel. You can see here that we have two columns, frames and objects. And here we have global frame. In other words, these are the variables that are existing currently in this point of the execution. We have the numbers variable.
SPEAKER_00So this variable has been declared. And here with the arrow, we see what this variable refers to. So it refers to a object. So each variable actually refers to an object in Python. So the object here is the list one, two, three, four, and the variable is numbers. So you can see here the items of the list and the indexes. So one has an index of zero, two has an index of one. So one, two, three, four are these items right here.
SPEAKER_00One, two, three, four. So at this point, in memory, there is this object and this frame. If we press next, we'll see that a new frame was added, a new variable. And its value... Now, we don't have an arrow here. Python Tutor is displaying the value just here since this is a small object, so just one. and you'd see it here so at this point x was created as a variable because for x in
SPEAKER_00numbers when you do for x you are creating a variable on the fly right so using an assignment operator is not the only way to create variables in python You can also create variables like so. So that's the benefit of Python Tutor. It allows you to understand some things which you don't see in a normal execution of a script as it happens in an IDE. So let's go next again and this line will be executed.
SPEAKER_00Try to imagine what will happen as an exercise and I'll go ahead and press next. So we got y is equal to one because y is equal to one in the power of two and that gives one. So now we have a new variable and one as the value of that variable. And you can also see, for example, that the loop is being executed one by one. Now, let's execute this print y. There we go.
SPEAKER_00Now, this time, there's no new variables. So we have we still have numbers x and y. But in the output, we've got the first output. So one, Because print y outputted this. So these are what exist in memory and these here are what the users see on the terminal as output. Now the next line is this to be executed. You see the last was this, print y, now we are here.
SPEAKER_00Because that's what happens in a for loop. So the execution goes in a loop. That's why it's called a loop. Until all the list items have been consumed. So next. You see now for x in numbers, x became 2. So now the x frame refers to object 2. You'll see that y will refer to 4. Yes.
SPEAKER_00And then next, we got a new print and so on. We press next, next, next. So the variables are changing the object any time we are going through these executions and so on. And that was the last execution. So once the list is exhausted, then the next button becomes grayed out. So that means all the code was run. You see here, done running 14 steps.
SPEAKER_00So that was a simple example how to use Python Tutor. You can try to experiment with it with more complex code. You can have a function, for example, here on top. So an exercise would be to, for example, if you want to exercise, you can try this code. So a function a list, then we iterate through that list and we call the function step by step. So you can try to visualize the execution of this code and see how it helps you
SPEAKER_00understand the inner workings of Python execution. There is also another service called pandas tutor if you are into working with pandas. So it does the same thing it visualizes the execution of pandas code. So when you're working with data frames and analyzing data, and yes, that's about today's video. I hope you enjoyed it. And I hope this is helpful. Thanks a lot. See you in the next ones.
This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we built a desktop app that merges and splits PDFs on your own computer. Today it gets three more tools: Compress,...
This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we turned four Excel files into a PDF report with Polars. Today the robot sends that PDF by email, and the...
This is a free preview of a paid episode. To hear more, visit dailypythonprojects.substack.com Yesterday we read a 1 GB log file with one generator. Today we connect small generators into a pipeline, and point it at a...
This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe
This is a public episode. If you'd like to discuss this with other subscribers or get access to bonus episodes, visit dailypythonprojects.substack.com/subscribe