You can't be a good programmer until you have learned how to use a debugger.
So in this video you will learn how to use a debugger and that will make you a better programmer.
Let's start by writing a very small program.
For example, we have some texts.
I love Sun and we have a word variable love, and now we want to calculate the frequency of that words
used in this text.
For that, we would first want to get the list of words for this string.
So this is string, but we want to convert it into a list of strings so that each item, each word is
separated.
For that we need to use text that split.
Sometimes we may make errors in the codes, typos or other things, and we don't know until we run the
program.
So let's continue.
Then we want to calculate how many times the word love occurs in the texts in this text.
So for that, we need to use text that count and we enter the word variable.
Sometimes we may use some quotes which shouldn't be used there, but we'll fix that using a debugger.
Finally the frequency would be.
Equal to.
Colds divided by the length of the words list.
So this is a list.
Remember, what we get from words is.
Something like this.
All right?
When we use the split method, we get a list.
So with three items, therefore, we say, let's say one.
So the words love is one time.
It's a it occurs one time in the text.
So we will get one for this and the length would be three of the words list.
So one times three we get 0.33 times 100.
We get the frequency and we print out the frequency.
So that's a program.
We create it.
Now, let's run this program.
To run the program.
You know that you can use the normal triangle button and but then you get an error.
So you can fix this error by using your logic, try to read the error and so on.
But if the error is a bit harder to understand, you want to use a more powerful way and that is to
use a debugger.
So let's use a debugger to fix this error and to fix this error using a debugger.
You want to begin by creating a debugging breakpoint.
The breakpoint now should be at the line where the error code, which is line four.
So you go to line four and in between the line number and this vertical border, you can click somewhere
in the middle.
That should create this breakpoint which looks like this red dot.
You cannot create breakpoints at empty lines, so please be aware of that.
So once you create a breakpoint at the point where the error occurred, you can access the debugger
by right clicking over the script and go to the debug main item, click that and that will execute your
script until this line.
So that's what a debugger does.
It executes your script partially.
So this line has not been executed yet and we don't get any errors here.
What we get is a debug.
Payne So this panel here is part of the debug tab, so you can click that to display and hide it.
And this debug panel has two tabs, the debugger tab and the console tab.
First, we will use the console tab and then I'll show you what the debugger tab is for.
So the console tab is mostly used when you get an error message like the one we got in the run tab,
right?
So we break the script of the error line and then we go to the console and in the console now we try
to experiment with this line.
So first thing I would like to do is execute that line.
And of course you would get the same error, because what we're doing now is that the execution has
frozen at this point.
So we have access to the variables, to the text variable, to the word variable.
For example, text is the text and word is the word love in this case.
And so we can take a look at the arrow, try to use our logic, what's going on.
So as you can imagine, debugging is not an exact science.
Still, you have to do the work as a programmer.
The debugger is just a helper.
It doesn't fix your code.
So it's just it just gives you this python console which has access to this point in the script.
So perhaps you might want to do a deer text to really see if sleep is part of this variable because
the error is saying that sleep is not part of this.
So we that will realize that actually you need to use the split methods.
Which is somewhere here splits.
So once you realize that what you did, what mistake you did, then you stop the debugger.
You can go to this button to stop the debugger, and then you go here and you change that.
To split, you remove the breakpoints and then you run the script again to see if that fixed the error
or not.
Right.
So no errors anymore.
So we use the debugger successfully to fix the error, right?
So we fix the error and we have no errors anymore.
But having no errors doesn't mean that your program is working fine.
You should double check the output you got.
In this case, the output is 0.0.
But we know that the words laugh occurs once in this script.
So what we should get instead here is 33.3, three three, something like that.
So in that case, we won't use the debugger again using another method.
In this case, we don't have an error, so we don't know where to place the breakpoints.
In that case, you should place the breakpoints in the very first line and then go and right click go
to debug Main.
This time, what we want to do is we want to execute these lines step by step and see how the output
changes.
To do that, you can use the debug tab this time, not the console tab, but the debug tab and then
find the step into my code button, which is the third one in here.
So please press that one time.
What you'll see is that we've got a variable here.
Text is equal to I love sun.
So when we press that button, Python executed the first line, right?
Because before that python, the interpreter hadn't executed any line because the breakpoint was at
the very first line.
So when we press that for the first time, you execute the first line, so you check the outputs.
I love sun.
You press the button again.
You see that the second variable was declared, so the second line was executed.
You see now this is highlighted.
That means if you press the button again, this will be executed.
So let's press the button again.
Right.
You see that you have a list of words, so you make sure that the list is correctly constructed.
I love some three strings in the list.
That's fine.
So what we're doing here is we are checking the intermediate result step by step, using the debugger
step into my code tool.
The next step is to execute this line.
So go to step into my code again.
Check the output counts.
Oh, so count is zero, and that's where the error is.
So we were able to figure out where the output is incorrectly calculated.
So before frequency is calculated, we've got count which is containing the wrong value, this variable.
So now that we know the problematic line, we can look closely at this line.
So try to figure out what's wrong with this line.
So perhaps the count method is fine.
Or perhaps this variable, it's not a variable, but it's a string.
So word is a variable, right?
But we have placed it here as a string.
So in that case, you should delete those quotes, stop the debugger, run the script again.
And this time we get the correct output.
So that was how to debug a script which doesn't have functions.
But what if we had a function definition in the script?
The process might feel a bit different.
So let's make sure you understand the debugging process when you have function definitions.
So let's create a function here.
Define get frequency.
Which has as arguments the text variable and the word variable.
So let's remove the variable declarations because we will get them from the user when the function is
called.
And let's indent these.
So we get the text, we split it, we get the count by using this expression.
And the frequency also is also part of the function.
And then we call the function get frequency.
I love son, and the word is love.
So we want to count the frequency of the words in that text.
And let's store this in variable frequency.
And this is complaining that the frequency doesn't return anything.
So let's return the frequency.
That's what this function should calculate.
Then let's add some conditionals, such as if frequency is greater than five, then we print out high
frequency.
Else print low frequency.
So what if we had the same mistakes in here?
So instead of splits, we would have sleep and instead of.
Words, we would have the strong words.
If we execute this function all this time, we get.
Two lines which are problematic.
So this line here, line eight and also line two.
Now the line which has the problem is indicated here in the last part of the message, in my case,
it's line two that has a problem.
So you should go to line two, which is in here, and you should place a breakpoint in that line.
Then go to right click and debug main.
Then again you want to go to console.
So the console now has access to the variables inside the function, for example, the text variable.
The word variable.
However, note that it doesn't have access to the frequency variable yet because we have placed the
breakpoint at this line.
So that means the debugger executed the first line.
So you have access to text and words, but the debugger has not yet executed this line or that line.
So again, the process to fix this error is to try to execute this line.
And you see that you've got that air sleep and then perhaps you can do their text to check the methods
you can provide and so on.
So the idea is that this console basically is like a tunnel.
It has access to this inside the function, to this place, to these variables which are inside the
function.
And that's great opportunity for you as a programmer, because otherwise to check the values of the
intermediate variables in here, you'd have to use like print functions like print count and print frequency
here and so on, and execute the script using the normal triangle button.
So the debugger lets you access the inside of a function.
Right.
So once you figure out the error, you stop the script, you change this to split or remove the breakpoint.
And executes mean again.
So we get low frequency this time.
That's not the expected output because we know that we should get high frequencies since 33.3 is higher
than five.
So this should be executed.
In that case, again, we place the breakpoint in the first line, in the first executable line actually.
So you shouldn't place the breakpoint in here this time.
You should place a breakpoint at this line.
Why?
Well, because that is a line which is executed first by Python.
All right.
So Python, the interpreter reads the function definition, but it doesn't really execute anything.
Right?
It's a function definition.
It doesn't do anything.
The first line that the interpreter executes is this line.
And this line then executes those lines.
So those lines are only executed after this is executed.
And once these are executed.
Frequency is returns.
Then the interpreter goes to the other lines.
So let's play the breakpoints in here, which is technically the first line that is executed and go
to debug Main.
So this time again, we should use the debugger tab just like we did when the script was a plain script
without a function definition.
So in this debugger tab, now we can go to the step into my code.
I'll explain the other buttons as well.
So with Step into my Code, you execute that once and now you see that this line was executed.
And the next line that that was executed after that was this line.
Right.
Now, the next line for execution is this.
So if I press on, step into my code again, that will be executed.
And again in here you can see the variables.
Texts.
I love sun words.
Love the words is a list.
So we are inside the function.
Now the scope is displaying the variables of the function.
But here on the left you have also this on the scope, the module scope, where you'll see the global
variables which are outside of the function.
In this case, it could be the frequency variable.
You don't see it yet here because this line has not been fully executed yet since for this line to be
fully executed, that means these lines should be executed.
But we're still working on that.
So let's go to the get frequency scope in here.
And again, we want to press step into my code.
So count was executed.
We see count here, it's zero.
So in that case, we should be able as a programmer to say that, oh, count is not really zero.
So we should look into this code and see what's going on here.
Of course, you can always use the console.
Right?
So you can.
Get that line and execute it here.
And check the count.
So it's zero.
So you have the same value of the variable as in here.
So you should think about this line and try to figure out that, oh, we should remove those quotes
from their.
And so at that point, you stop the script, you right click execute again and you get the correct output
this time.
Now, let me show you the other buttons in the debugger tool.
So let's place the breakpoint at this line eight.
And this time I would also like to create actually creates another variable here.
Message is equal to Hello, just a variable.
And print out the message variable at the end of the script.
Let's debug.
So this line is about to be executed now where the breakpoint is.
So if you go to step into my code, that will go inside the function again.
No, if you go to module.
To the scope, to the global scope of the module, you'll see the message variable.
So that one here.
And inside the jet frequency scope, you have the variables of this function.
Of course, you can use again, step into my code to go over the variables.
Now, let's stop this.
Right click again.
Go to debug Main.
What happens if you use step over?
So please press and step over with the breakpoints at line ten in the frequency variable.
Step over.
What will happen this time is that you don't go into your code, you don't go inside functions, but
you ignore those stations.
So to say you skip those stations, they are executed.
These lines were executed actually.
But the bus, the debugger doesn't stop those lines.
It just passes over them, executes them quickly and goes to the next line in the global scope of the
script.
So in this case, it's about to execute this line.
So we got the variable value for frequency.
You can also see these variables on the right here.
So the value is 33.3, 33.
And if you go to step over again, it goes to the next line print.
Step over.
It goes to this line.
It didn't go to these lines because frequency was higher than five.
Right.
So it's executed high frequency and it ignores else, of course, because frequency was not less than
five.
So now it's about to execute a print message.
Press that again.
And when you get this process finished, that means the debugger executed all the lines and it successfully
stopped.
So that was how to use the step over tool.
Let's try the other buttons.
The button you want to use them most of the time is step into my code.
Anyway, it's good to know the other buttons.
Still step over is what we used.
Let's try.
Step into press.
Step into.
It goes again to the function definition.
Pressed again.
It goes to that again.
Press it again.
Again.
And again, until the function is executed.
So what's happened here is that step into look to the same as step into my code.
But the difference is that if you happen to have some other modules in your code, for example, let's
introduce the Globe module here, a standard library.
And then if you had something like glob that glob here, you can have anything like that.
And then you debug again.
And you step into step into step into.
That will take you now to the codes of the Globe library.
You see, this is Globe dot pie.
So basically this is trying to debug issues in this library when you use the step into.
But if I stop this now and I right click again and I try to do the same thing, but with the step into
my code button, step into my code goes through your function lines and then goes to glob dot glob,
but it doesn't go inside globe that globe.
So that's a difference step into goes inside the code of libraries and step into my code goes only into
your code.
Right.
We have stepped out as well.
Step out.
Basically puts you out of the function definition.
So basically, if you're running one of these lines currently and you don't want to run the other lines
of the function definition, that will take you out and it will continue with lines of the global scope.
So these lines.
And Ron to cursor is that if you place the cursor in the last line for example, the debugger is currently
here and now I place the cursor in here and I press on around to cursor that's will make the debugger
skip these other lines and go directly into this line.
Well, it's not skipping actually.
These lines are executed of course, but the debugger doesn't stop at those stations.
So think of the debugger as a bus and think of the lines as stations.
The bus always, always passes through those stations and and executes them.
But it doesn't necessarily stop there unless you tell the debugger the bus to stop at those stations.
So that's what these buttons are about.
And that was about debugging.
So the most important thing to remember is that you should play with the console when you have an error
which shows in the command line and you should play with a debugger buttons here where you don't have
an error, but you want to check the intermediate results of your code to understand where the output
is incorrectly calculated.
With that, thanks for following this and I'll see you later.