Now before you start getting really committed to print and start making
friendship bracelets for this print statements,
there's another tool that I really want to talk about
and it's a tool that's bigger, it's fancier, it's more complex.
So what is it?
It's a debugger and we've already seen debuggers a little bit.
So I've already shown you pythontutor.com
and you've also seen us use the debugger in the Thonny editor right here.
Now, in this example, I'm going to stick with the web version of Python Tutor
just so that you don't have to install anything extra.
But if we go ahead and comment out the previous block and uncomment
the last one, you can see that we have yet another problem to solve
and yet another piece of code, that's not doing what it's supposed to do.
So in this case,
we've got a function that's supposed to mutate a list so that it takes a list as
an input
and then it's supposed to multiply each of the items in the list
so the final output should be something like 2, 4,
6, 10, and so on and so forth.
Basically taking each of these numbers and multiplying them by 2,
which happens on this line of code. But unfortunately when we run it,
we just get a list with a single value, 26. So what's going on?
Let's try and debug it.
So I want you to copy these lines of code and then paste it into Python Tutor
and then go ahead and click Visualize Execution.
Now that it's inside the debugger, we can do a lot of things with it.
We can step through the code to see it line by line. So for example,
the first thing that happens is we create a function called mutate and this
mutate gets past a input called a_list.
So now inside this function, mutate,
we've got a variable called a_list and it's this list with these values.
Now, as we keep going, we can see we create a b_list, which is an empty list
and then we use the for-loop to loop through each of the items in the list.
So the first time this item is equal to the first item in the list,
which is 1,
and then we multiply it by 2 to create this new_item variable,
which is now equal to 2.
And now the loop goes back to the beginning and we keep doing this with each of
the numbers in the list. We multiply 2 to each of the numbers.
Now, once we're done with that loop, the next line that happens is this one.
This b_list gets appended with the new item and finally,
we print out the b_list. Now this is where the problem occurs.
Notice how b_list is just a list with one value.
And that is in fact what gets printed. So with this debugger,
we've managed to see line line what the computer is doing,
which is really helpful in itself.
But we can actually make this process even simpler. Inside
most debuggers, you can put a breakpoint
which tells the computer to stop what you're doing at this particular line.
And then at that moment in time,
I want to examine what all the variables and all the functions are doing.
So in our line, the key problem happens right here.
If I click on this line, you can see it becomes red
and this is where it happens, this is the break point basically.
So now I can scroll over to that point in time I can examine the values of all of
my variables. a_list is still what I expect it to be,
but b_list is completely empty. And this item happens to be on the last item
number 13 and the new item is doing what it needs to do,
multiplying it by 2,
but none of them are being added to my list because if I click next,
you can see this b_list is equal to just one value.
And it happens to be the last value that new_item was equal to.
So by using this debugger,
you should now have a good idea of what the problem is.
Pause the video and try to fix this code so that the mutated list with all of
its six values gets printed. Pause the video now.
All right.
So the simple problem is that when we are adding the new item to this b_list,
we're doing it outside the loop.
It's when this loop has completed and at the very end
do we actually call this line.
So what we needed to do to fix this code is just to indent that line over.
And now when we run the code,
you can see that the proper output gets printed
and if we change this code in there, the debugger, if we edited it right here,
then when we visualize it, you can see if I put a breakpoint here
that right line of code gets executed six times
and if we go to that line of code and we hit next,
you can see that the b_list is now getting new values added each time as we
would expect it to. And when we examine all of our variables
especially this b_list variable,
it's exactly what we expect it to equal each step of the code.
So a debugger can be really useful.
It's almost like you've put a print statement on all the variables in your
code,
and you can isolate a particular time point to see what each of the values are.
And as we become even more experienced,
we're going to be using the debugger more and more.