WEBVTT

00:00.240 --> 00:07.410
Let's talk about a very important topic if you want to be a great programmer, and that is called debugging.

00:07.620 --> 00:11.000
You see, nobody writes perfect programs.

00:11.010 --> 00:13.410
It's honestly impossible.

00:13.440 --> 00:17.430
We're always going to make mistakes and programs are constantly being improved.

00:17.640 --> 00:25.320
We constantly improve our code as we find more and more mistakes because programs are full of errors

00:25.320 --> 00:29.730
or what we call bugs or exceptions at runtime when the code is running.

00:30.300 --> 00:38.790
You see, the act of finding and removing these bugs or errors from our code is called debugging.

00:39.000 --> 00:46.470
In a big chunk of our time as programmers is debugging code, whether it's our own code or other people's

00:46.470 --> 00:49.560
code, and figuring out, hey, how come this doesn't work?

00:50.010 --> 00:54.690
And the senior developers are really, really good at debugging.

00:55.380 --> 01:01.320
So let's talk about in this video about some of my recommendations for debugging and what some good

01:01.320 --> 01:02.370
practices are.

01:02.850 --> 01:07.380
Now, keep in mind that you get better at debugging code as you code more and more.

01:07.380 --> 01:12.150
So the best thing you can do is to just keep practicing, keep writing Python code.

01:13.230 --> 01:14.940
So what are some of my tips?

01:14.970 --> 01:19.320
Well, first, you definitely want to use some lending.

01:19.380 --> 01:21.250
And what is lending?

01:21.270 --> 01:25.070
Well, we've talked about it right when we set up our developer environment.

01:25.080 --> 01:31.260
Lending allows us to detect as we code some issues with our code.

01:31.290 --> 01:40.290
For example, if I do num plus four here and I get a red underline and you see here that the Pi Flakes

01:40.290 --> 01:46.320
lender notices before I even run my code that hey, there's an undefined named NUM.

01:46.530 --> 01:51.510
So lending allows us to find these errors before we even run our code.

01:51.870 --> 01:59.550
And we saw that we can use IDs like pi charm that has lending already built in or use sublime text or

01:59.550 --> 02:06.540
Visual Studio code and install packages and extensions like PI lent to actually have lending in our

02:06.540 --> 02:06.990
code.

02:07.020 --> 02:09.570
That is a really, really important tool.

02:09.600 --> 02:15.480
Next, I recommend always using some sort of an ID or.

02:16.380 --> 02:23.040
Ed, because these editors and IDs, especially the ones that are specific for Python like Pi char,

02:23.070 --> 02:29.370
have all these built in tools for us to use, such as auto formatting based on Pep eight.

02:29.370 --> 02:34.410
Remember the style official style guide that the Python community has?

02:34.590 --> 02:42.600
It allows us to detect errors in our code with, let's say, highlighting and formatting of our code.

02:43.440 --> 02:47.670
So it becomes easy for us to detect any errors before we even run the code.

02:48.240 --> 02:52.050
Finally, you want to be able to learn to read errors.

02:52.350 --> 02:57.030
And this is something that I try to practice with you as we've learned, Python.

02:57.330 --> 03:04.260
For example, if I do four plus, let's say gibberish here and I click Run.

03:06.230 --> 03:06.950
Right away.

03:06.950 --> 03:07.410
I see.

03:07.430 --> 03:10.080
Hey, on line six.

03:10.100 --> 03:13.370
There's an error in my main PI file.

03:13.580 --> 03:19.310
There's a type error, and I see here that I'm trying to add int and a string.

03:19.950 --> 03:24.030
So we're able to read errors and understand what they mean.

03:25.380 --> 03:28.410
Maybe I do something like this.

03:28.440 --> 03:29.790
If I click Run.

03:30.490 --> 03:31.900
I get a syntax error.

03:33.110 --> 03:36.440
And a syntax error I know means that.

03:36.440 --> 03:42.410
Hey, Python interpreter is trying to read my code, but what you just wrote is not Python.

03:42.410 --> 03:43.880
I have no idea what you're doing.

03:45.410 --> 03:45.890
Here.

03:45.890 --> 03:49.180
I see that it's end of line while scanning string literal.

03:49.190 --> 03:54.590
So I'm trying to read this string and all of a sudden the program ends like you're supposed to end the

03:54.590 --> 03:55.670
string, remember?

03:56.770 --> 04:01.060
So being able to read these errors is really, really important.

04:01.450 --> 04:07.390
And we've discussed some of them in this course, some of the most common ones like syntax error, where

04:07.390 --> 04:10.420
you're just writing something that Python interpreter doesn't understand.

04:10.780 --> 04:13.210
We have something like name error, right?

04:13.210 --> 04:16.720
Where we use a variable that doesn't exist.

04:17.710 --> 04:19.180
So Python doesn't understand.

04:19.180 --> 04:20.970
Hey, I have no idea what name is.

04:20.980 --> 04:22.810
What's what's the value of name?

04:24.000 --> 04:25.650
We have things like type air.

04:25.680 --> 04:31.470
That is when we mismatch data type and do let's say a string plus four.

04:32.220 --> 04:34.740
We have things like value error.

04:35.470 --> 04:41.800
We have things like key error where we're trying to access a key that doesn't exist or attribute error

04:41.800 --> 04:47.020
where we're trying to access the method of a object and that method doesn't exist.

04:47.710 --> 04:53.440
Now, these errors are something that we've been seeing throughout the course, and we're going to see

04:53.440 --> 04:54.190
a few more.

04:54.580 --> 05:01.780
I would say that there is about 15 to 20 errors that are really, really common that show up 90% of

05:01.780 --> 05:02.380
the time.

05:03.230 --> 05:07.370
And if you remember, you can always go to the Python documentation to learn about them.

05:07.370 --> 05:09.620
But this just comes with practice.

05:09.650 --> 05:14.420
If you see an error that you've never seen before, I recommend going to the documentation and reading

05:14.420 --> 05:16.260
up on it once you see it.

05:16.280 --> 05:19.490
You usually tend to remember when it comes up next.

05:20.080 --> 05:23.920
But let me show you my favorite way to debug code.

05:25.050 --> 05:30.570
And it's called PDB or Python Debugger.

05:31.080 --> 05:35.070
And PDB is a built in module in Python right here.

05:36.270 --> 05:36.750
Again.

05:36.750 --> 05:43.470
Remember, our built in module are part of the standard library that Python comes with and PDB is the

05:43.470 --> 05:46.200
Python debugger for interactive interpreters.

05:47.400 --> 05:51.180
And you can read about it here, but let me show you what it does.

05:52.460 --> 05:52.860
You see.

05:52.880 --> 05:58.070
PDB is extremely useful because it allows us to interact with the code.

05:58.550 --> 06:08.210
So previously when we had a function, let's say we have function ADD and we have num one, num two

06:08.630 --> 06:14.630
and then this function simply returns num one plus num two.

06:15.630 --> 06:23.460
Now before if we wanted to, let's say debug this code and let's say we're adding four plus some gibberish

06:23.460 --> 06:24.930
and it's giving us an error.

06:26.760 --> 06:33.870
The first step that I would take is most likely adding a print in here so I could say print.

06:33.870 --> 06:35.160
Hey, what's what's going on?

06:35.160 --> 06:35.610
Let me see.

06:35.610 --> 06:38.130
Number one and number two and what it's giving me.

06:38.970 --> 06:43.170
And print is a really, really quick way and easy way to debug your code.

06:43.530 --> 06:48.480
I use it all the time, but PDB gives us a little extra boost.

06:49.780 --> 06:51.840
So we can use it like this.

06:51.850 --> 07:00.310
We simply import PDB and instead of using something like a print function, we can simply say, Let

07:00.310 --> 07:01.540
me add a space in here.

07:01.540 --> 07:08.650
We can simply say PDB for Python debugger and then say set trace.

07:09.310 --> 07:15.430
And set trace, I would say is the most useful method that we have on this PDB object that we imported.

07:16.520 --> 07:18.830
You see when I run a set method.

07:22.300 --> 07:28.900
It now gives me an interactive Python debugger that I can now type commands in here and actually test

07:28.900 --> 07:29.560
on my code.

07:30.290 --> 07:36.360
And here's the interesting part right now in this PDB, I'm right inside of line eight.

07:36.380 --> 07:41.630
So my code tried to run, but then it paused as soon as it said PDB set trace.

07:41.630 --> 07:47.510
And now I'm in this world and I can test out what's going on so I can actually say, Hey, num one,

07:47.510 --> 07:48.170
what is it?

07:48.200 --> 07:49.400
Well num one is four.

07:49.400 --> 07:50.420
What about num two.

07:50.750 --> 07:57.050
Well num two is a string and I can be like oh I'm trying to add a string and an int this, this doesn't

07:57.050 --> 08:00.050
work so I can detect errors this way as well.

08:00.680 --> 08:06.140
Now this PDB interactive actually has a ton of useful things.

08:06.140 --> 08:08.270
So if I type in help here.

08:09.670 --> 08:10.300
You see that?

08:10.300 --> 08:14.140
I have commands that I can use in the PDB.

08:14.260 --> 08:16.510
So I have a ton here.

08:16.510 --> 08:21.510
I have exit, which I'm assuming is going to exit me out of this PDB, Python debugger.

08:21.520 --> 08:23.020
But I also have.

08:23.780 --> 08:24.710
Things like.

08:25.490 --> 08:26.570
Let's say list.

08:26.570 --> 08:27.740
Let's see what list does.

08:27.740 --> 08:31.970
So when I type in list here and right now the website is really.

08:32.820 --> 08:35.940
Not showing me too much, but hopefully you can still see list.

08:37.110 --> 08:38.940
And I guess some sort of an output.

08:38.940 --> 08:46.410
I see that I get a list of my code and OC, but let's say I don't know exactly what list does well.

08:47.280 --> 08:50.970
PDB allows us to type, help and then list.

08:51.670 --> 08:57.700
And when we do that, it actually gives us the documentation, just like we have here.

08:58.720 --> 09:00.820
If we go to, let's say, list.

09:03.370 --> 09:03.910
Let's see.

09:03.910 --> 09:05.470
List, list, list, list.

09:06.130 --> 09:06.880
Right here.

09:06.880 --> 09:09.520
It gives me the documentation right in here.

09:09.520 --> 09:12.160
So it says list source for the current file.

09:12.610 --> 09:12.880
All right.

09:12.880 --> 09:15.940
So it just lists the source code that we wrote.

09:16.030 --> 09:17.080
Okay, that's cool.

09:17.080 --> 09:18.190
What else is there?

09:19.090 --> 09:25.330
Well, we also have something like step or se for short and step is really, really cool.

09:26.970 --> 09:29.910
Let's actually use clear to bring this up to the top.

09:29.910 --> 09:33.720
So I'm going to say clear so that it clears all breaks.

09:35.480 --> 09:36.230
Let's do.

09:37.400 --> 09:39.460
Let's make this a little bit bigger so you can see.

09:39.470 --> 09:39.970
There you go.

09:39.980 --> 09:40.580
That's better.

09:40.610 --> 09:40.880
All right.

09:40.880 --> 09:47.120
So now I can actually do something like step and step is going to allow us to go to the next line.

09:47.120 --> 09:49.850
So, again, if we make this a little bit bigger.

09:50.860 --> 09:53.440
Step is going to say, hey, go to line nine.

09:53.440 --> 09:54.370
So let's see what happens.

09:54.370 --> 09:55.660
I'm going to type in step.

09:57.630 --> 09:57.840
Oh.

09:57.840 --> 09:58.860
And I got step twice.

09:58.860 --> 09:59.910
So let's right step.

10:01.730 --> 10:02.600
And look at that.

10:02.720 --> 10:06.770
I go to the next line in my code and it gets me.

10:06.800 --> 10:08.030
Hey, you got a typo?

10:08.030 --> 10:08.510
Error.

10:08.540 --> 10:09.520
The error that we got.

10:09.530 --> 10:11.250
So let's add another line in here.

10:11.270 --> 10:19.640
Let's say that we're trying to add four plus five in here or four multiply by five and assigning assign

10:19.640 --> 10:22.310
it to a variable.

10:24.170 --> 10:26.450
If I stop this and run this again.

10:28.570 --> 10:30.100
I'm now inside.

10:30.130 --> 10:30.820
Over here.

10:32.640 --> 10:35.310
If I type in tea, I get a name.

10:35.310 --> 10:35.760
Air.

10:35.880 --> 10:37.560
Tea is not defined because.

10:37.560 --> 10:42.630
Well, I'm inside the code here on line eight, but we haven't ran line nine yet.

10:42.630 --> 10:44.220
So let's do step.

10:45.120 --> 10:46.800
And now I'm on the next side.

10:47.940 --> 10:52.950
So if I do pt now I get hey t equals to 20.

10:53.190 --> 10:53.760
Awesome.

10:53.760 --> 10:57.570
So step I can step through the code which is really, really useful.

10:58.080 --> 11:03.720
I also have things like continue which lets me continue through the code until I return something and

11:03.720 --> 11:06.750
I continue and exit out of PDB and look at that.

11:06.750 --> 11:07.620
I get the error.

11:10.100 --> 11:11.570
Let's run this again.

11:11.600 --> 11:18.320
Another useful command is a and a actually gives me all the arguments of the current function that we're

11:18.320 --> 11:18.560
in.

11:18.560 --> 11:19.700
So look at that.

11:19.700 --> 11:22.610
I get all the arguments which again is really useful.

11:23.240 --> 11:25.190
There's also the command w.

11:25.860 --> 11:31.050
Which shows me the context of the current line it is executing.

11:32.200 --> 11:35.710
So you can see here where we are, we're in the ad.

11:36.240 --> 11:36.990
Function.

11:38.130 --> 11:43.740
We called it with these arguments, and we're now about to run this line.

11:45.970 --> 11:48.040
And then finally, as we're debugging this.

11:48.040 --> 11:51.460
All right, I think I finally see that we have an error.

11:51.460 --> 11:59.500
We should have given it a number so I can now just exit out of here and simply fix this to five.

11:59.620 --> 12:02.950
So now I can just remove my PDB and everything.

12:02.950 --> 12:05.410
Should be working fine with my code.

12:06.680 --> 12:07.280
Awesome.

12:07.760 --> 12:12.710
So PTB is awesome to step through your code and debug and try different things.

12:12.710 --> 12:19.520
It does require a bit of practice, but after a day of just playing around with PDB, you will become

12:19.520 --> 12:20.900
really, really good at debugging.

12:20.930 --> 12:28.730
So after this video, I recommend that you write PDB and hopefully you have a large enough Python file

12:28.730 --> 12:35.540
that you can just step over and experiment with different commands using help, using list to see what

12:35.540 --> 12:37.010
you have available to you.

12:38.060 --> 12:39.710
Actually, I want to show you one last thing.

12:40.340 --> 12:44.210
So let's say we go back to our code, our PDB set trace.

12:44.780 --> 12:45.800
I click run.

12:47.670 --> 12:53.430
Could I do something like this where I notice that I type A and I have.

12:53.430 --> 13:00.330
All right, my code is working, but could I change num two to now equal some gibberish?

13:02.230 --> 13:04.320
Well, let's see if I do next now.

13:06.100 --> 13:08.740
And let's do next to run this final line.

13:10.120 --> 13:11.140
I get an air.

13:11.200 --> 13:19.710
So I'm also able to change variables in here and write Python code to perhaps test out my functions.

13:19.720 --> 13:21.190
This is now working.

13:21.190 --> 13:25.300
I can test out, hey, what happens if we change this variable to another value?

13:26.220 --> 13:28.050
Again, very, very useful.
