Debugging Python for Beginners (with most common Examples)
6K views
Feb 24, 2025
In this tutorial, we will learn about the most common things that can go wrong with your code. We will also learn how to read and debug Python errors (traceback). ## Top 10 Most Common Exceptions in Python 1. SyntaxError 2. IndentationError 3. ValueError 4. ModuleNotFoundError 5. IndexError 6. AttributeError 7. TypeError 8. NameError 9. FileNotFoundError 10. KeyError ----- Install Python: - MacOS: https://www.jcchouinard.com/install-python-on-macos/ - Windows: https://www.jcchouinard.com/install-python-on-windows/ - Anaconda: https://www.jcchouinard.com/install-python-with-anaconda-on-windows/
View Video Transcript
0:00
hi everyone my name is Ja in this
0:02
beginner Python tutorial I'm going to
0:04
show you one of this most useful skills
0:07
you can have in your python tool sets
0:09
when you start
0:10
programming and that is debugging your
0:14
code inevitably you will end up with
0:17
some code errors at some point and you
0:20
want to be able to look at this and know
0:22
what to do so the first step I'm going
0:25
to show you is I created a code with
0:29
that create an error and I'm going to
0:31
show you what happens here I'm ending up
0:34
with what we call the trace back and you
0:38
want to be able to read that Trace back
0:40
and be able to debug it because you're
0:43
going to see that a lot of time so when
0:46
you read this I made you a small uh
0:49
visualization that can help you is you
0:52
start reading from the bottom to the top
0:56
and the first thing that you look at is
0:59
the name the exception of the exceptions
1:02
names that you're seeing and right there
1:04
I'm getting the name error and I have
1:07
some information that says name is not
1:10
defined this gives me a hint that I
1:13
haven't defined a variable somewhere and
1:17
I want to look further and I start from
1:21
the file with the actual error so I can
1:24
see that code with error.
1:27
pi has this function which is make hello
1:31
which I called there in my code
1:35
so the higher you are in your Trace back
1:38
the closer you are to what you executed
1:41
so you first executed this make Alo and
1:46
then in order to execute this you call a
1:50
function that is found in your code with
1:52
error right there and I could
1:55
potentially go back to my code with
1:56
error and I can see that the name
1:59
variable was not defined so that's how
2:02
you try to debug uh your Trace
2:07
back uh you also have the the the line
2:11
number where it happens uh and a lot of
2:15
useful
2:19
information now you want to learn the
2:21
main exception types what kind of
2:24
exceptions will you encounter in your
2:26
life what kind of Errors uh and they
2:29
fall back back into three main
2:32
categories and those main categories are
2:36
uh the uh syntax errors the exceptions
2:40
and The Logical
2:42
errors why they have these categories is
2:45
because they're not going to behave the
2:47
same way if you have a syntax error
2:50
nothing will work uh code will break and
2:53
the code block will not be running at
2:55
all it will just throw the error
2:57
instantly and how you get that Sy syntax
3:00
error is if I do well true for example
3:04
and I say
3:06
print
3:07
one in that case it would print one
3:10
forever but since I forgot my uh column
3:13
here then I throw a syntax error and
3:16
nothing else can can can work you have
3:20
the exceptions and you have the logical
3:23
errors the exception is a bit different
3:26
is that it won't break because of a
3:28
syntax error but when you try to do it
3:31
during runtime it will break so if you
3:33
try to do a zero division error for
3:36
example zero division so 9 divided by 0o
3:40
inevitably you will get a zero division
3:43
error but the code could run because if
3:46
I come back here to my w true and I say
3:49
print I uh like I want to print I right
3:54
above here uh you can see that it didn't
3:59
print the letter i before running but if
4:02
I put it above my runtime execution then
4:05
I can see that it printed the I first
4:08
but then return the zero division error
4:11
and that's what we mean by runtime
4:14
error as for logical error uh it will
4:17
not show any uh ER like exceptions like
4:21
the syntax or the except the the runtime
4:24
errors but it will had your code won't
4:28
behave as what you would have expected
4:30
so one common example is if I have a
4:34
equal 5 and b equal 10 and what I want
4:38
to do is I want to sum a plus b and then
4:41
multiply by two uh one thing that you
4:45
might happen is a logical error so I can
4:48
I will print that logical error so let's
4:51
say I want to do 5 + 10 * 2 I do a + b *
4:58
2 if I run this I end up with a problem
5:02
because 5 + 10 * 2 is supposed to be 30
5:07
and in this case it's 25 and the reason
5:09
because that is because it
5:11
did 5 + 10 * 2 so it did did 25 so if I
5:16
want to fix that logical error what I
5:19
need to do is simply put in parentheses
5:23
like you would do in math and then you
5:25
have your fixed
5:27
errors now that we went into the main
5:30
categories of Errors there are multiple
5:33
exceptions that you will encounter in
5:35
your life if you want to show all the
5:37
types of exception you can do print
5:40
there
5:44
locals and then let's
5:47
say thunder net
5:51
built-ins
5:53
thunder so what you'll get here you will
5:57
have a list of all the possible syntax
6:02
errors uh but I'm going to cover the top
6:05
10 most exception uh most common
6:08
exceptions that you will encounter the
6:11
first thing and we already mentioned
6:13
this is the syntax error and the syntax
6:16
error will happen with if it will happen
6:19
uh if blocks try and except blocks while
6:22
blocks and uh for I in range blocks and
6:28
the way this often happens is if you
6:31
forgot to put your column right there
6:35
and you Tred to do so
6:37
if uh a equals B and then you try to
6:43
print a equals
6:47
B then you try this and that's what we
6:50
just saw it it happens all the time uh
6:54
whenever you forgot a column this is one
6:56
of the thing that you'll see the most
6:58
often
7:00
this happens also when you have a
7:02
string and you want to forget
7:05
to you forget to put the uh closing
7:09
string element
7:12
unclosed so if I try to do this I will
7:16
have an exception uh syntax error that
7:18
says you haven't terminated your string
7:22
literal deted a line to and as you can
7:25
see that error I was always start with
7:29
the name and then I see some information
7:31
and I didn't even go up because I knew
7:34
it was because it wasn't closed it's
7:36
easy at over time you get a sense of
7:39
those errors and you spot them
7:44
instantly another
7:46
big potential errors that you will
7:49
encounter is the indentation error the
7:52
indentation error happens when you
7:54
haven't intented your block and this is
7:57
very important because uh um python
8:01
forces you to have some syntax to follow
8:05
up and and if you don't it will break so
8:09
it forces you to have that s that that
8:13
um that indentation and in this case
8:16
otherwise you get indentation error in
8:18
order to fix this you just add a tab and
8:20
then press and it
8:22
works um otherwise you have other
8:25
situation when you define a function
8:32
and you have something that is
8:35
print properly
8:40
indented and when you don't follow
8:43
always the same indentation right now vs
8:45
code just tells me that it's okay it
8:48
just puts it automatically there but
8:50
let's I try to do it manually or I paste
8:53
code from somewhere else and like
8:55
somebody made that error and you have
8:58
uneven and then this will throw an
9:02
indentation error and as you can see I
9:05
didn't even have to try to call the
9:07
function for this to break because it is
9:09
part of the syntax error python wants
9:13
you to have a proper syntax it makes you
9:16
a better programmer because it makes
9:18
your code beautiful but it does throw
9:21
some of these
9:23
exceptions let's say I see the third
9:26
biggest uh culprit is a value error so
9:30
if you try to convert for example a
9:33
string to a int like that works right
9:36
you can change an in uh a number one to
9:40
a number one in integer but if you try
9:43
to do this by changing a string then you
9:46
will have an exception that breaks
9:48
because you're actually trying to change
9:50
text to an integer which doesn't work uh
9:54
it can also happen if you do a float and
9:57
then you have a string that is three 0
10:01
22 like if you have this how do you
10:04
convert this to a float you cannot
10:06
convert it because there's two dot
10:11
notation another very common use case is
10:15
if you have a package and that happens
10:19
all the time you get code you import it
10:21
from a tutorial that you found online
10:24
and then they ask you to import request
10:26
HTML and then you try to run it and you
10:30
have module not found
10:32
error
10:33
um the only way to fix this the simple
10:37
way to fix this is to just do a pip
10:40
install so generally you have the PIP
10:42
install and you have your uh
10:45
requests HTML for example and you just
10:49
run this and in your uh terminal and you
10:53
would fix your
10:56
error there are other kind of imports
10:59
that can break so if you try to import
11:01
Panda for example but you call it pandu
11:04
you're going to have the same error if
11:06
you do import Panda and that's very
11:09
important because you want your casing
11:11
to be always pandas like normal with the
11:14
like the proper import would be Panda so
11:17
you have to fix your casing and it will
11:20
not tell you what the error is but you
11:22
can spotted by the wrong casing here
11:30
you will get index error as well so here
11:33
I have three values so I can take my
11:36
list and index zero and I will get the
11:38
first item of my list but what if I take
11:41
an item that is larger than the list has
11:45
then I can see list index out of range
11:48
that happens very very very
11:52
often another exception that you will
11:55
face is when you are trying to uh make
11:59
make a reference of something that does
12:00
not exist so if you try to append the
12:04
the number four to the list uh then you
12:08
will have my list will work but if you
12:14
try to do add
12:19
five let let's run this it works so if I
12:24
if I return my
12:26
list it works fine
12:30
what if I try to add A5 to the to the
12:33
letter it doesn't work because add is
12:36
not uh a method that you can uh use on a
12:40
list item and you will get the attribute
12:45
error next let's get into the type
12:47
errors and type errors can happen very
12:51
very often and in this case what how
12:53
we're going to fix this is you take your
12:55
result and you when you try to match a a
12:59
to an integer for example you have a
13:01
mismatch type and that will break one
13:03
thing that you can do is you could say
13:06
convert this to a string instead and you
13:09
would fix your type error there are
13:12
other issues when you try to index uh
13:15
something that is not indexable so you
13:18
take this and you try to get the index
13:20
Zero from an integer you cannot do this
13:23
but you would be able to do it on a
13:25
string for example uh so that's one of
13:28
the cases
13:30
if you try to call a non-callable object
13:33
so remember when you want to do a uh
13:36
caller function you just use this
13:39
notation but if you try to do this on a
13:41
list you will get the type error list
13:43
object is not
13:45
callable also if you have a variable
13:48
that is equal none and then you try to
13:52
append something or make a calculation
13:54
on it plus equal 5 then you will get
13:58
this Ty typ error to uh you have other
14:02
situation when you have incompatible
14:05
types comparison I won't go into too
14:08
much detail uh they're all uh variable
14:12
that happens one that happens very often
14:15
is when you try to assign a value and
14:18
you're get the unable type list this is
14:22
come up very often and when you have a
14:25
type mismatch integer equal 3.4
14:32
14 uh earlier we talked about the name
14:35
error and that's very simple if I try to
14:38
call my variable I never created that
14:41
variable vs code is very smart it just
14:44
tells me what it's in white I can see it
14:47
easily that it does not even exist so if
14:50
I run it I automatically get the name
14:53
error and that's when you never defined
14:55
it so if you say my variable equals 5
14:58
and then you call my variable as you can
15:01
see the variable became blue and it
15:04
doesn't break
15:08
anymore next we have IO error this
15:12
happens a little less but it generally
15:15
happens when you try to open a file that
15:18
does not exist
15:22
nonexistent file. text and I just try to
15:27
do as F and I do content or even just F
15:33
read so if I try to read that file I get
15:37
the file not found error which is
15:39
essentially an IO error um you depending
15:44
on your version you might see both uh
15:47
the next uh error that you will see is
15:51
the key
15:53
error
15:54
um and this occurs when you have a
16:00
dictionary and you try to access a key
16:03
that doesn't exist so let's take that uh
16:05
dictionary and I want to select the
16:09
letter A the the key
16:12
a I will be able to return the letter
16:15
one but let's say if I try to access a
16:18
key that does not uh get then I can see
16:21
key error and it doesn't
16:26
exist if you want to prevent those
16:28
runtime errors that we discussed uh like
16:32
this you're trying to print one divided
16:34
by zero you can use try and accept so
16:37
you use
16:39
try and
16:43
accept
16:47
print the what and
16:51
error so as you can see here we used to
16:54
have a zero division error but now it
16:57
works properly didn't throw an error
17:00
didn't break anything if I want to do
17:02
any something else uh it will still
17:06
work
17:07
something it will still print it but it
17:10
Returns the the error if you want to go
17:13
further and have a better knowledge of
17:17
what's going on then you can do
17:20
exception as e and then you rate it so
17:24
what will happen is that will store the
17:26
exception name into your uh EV variable
17:29
and then you can return show it the
17:32
error and then you see it uh the uh try
17:35
and except return that was an error and
17:38
gave you where what the error was uh
17:42
which is super useful in your code to
17:44
handle all the potential exceptions and
17:47
um that's it so we covered almost
17:50
everything we need to know for the most
17:52
common use cases where you encounter
17:55
error feel free to subscribe to my
17:57
channel H or or visit my blog on jar.com
18:02
and uh feel free to like And subscribe
18:05
bye
#Development Tools
#Programming
#Scripting Languages