Debugging is one of the most important skills every Python programmer needs to master.
In this beginner-friendly tutorial, you'll learn how to understand Python error messages instead of being afraid of them. We'll break down Python tracebacks step by step, learn how to identify different error types, and discover practical techniques to fix bugs faster.
In this video you'll learn:
What a Python traceback is
How to read a traceback correctly (from bottom to top)
How to identify the error type
How to understand Python error messages
How to locate the exact line causing the problem
Common Python errors:
- SyntaxError
- IndentationError
- IndexError
- TypeError
- ValueError
- KeyError
Practical debugging tips every Python beginner should know
If you're learning Python, this tutorial will help you become much more confident when solving coding problems.
👍 If you enjoyed this video, don't forget to:
Like the video
Subscribe for more Python tutorials
Leave your questions in the comments
#Python #PythonTutorial #PythonForBeginners #Debugging #Programming #Coding #LearnPython
Show More Show Less View Video Transcript
0:00
I will try to transform how you approach
0:01
errors in Python, turning abstract into
0:03
learning. So, the first thing that you
0:05
need to do in order to handle errors in
0:07
Python is understanding Python's
0:09
traceback.
0:10
See, when your code fails, Python
0:12
displays a traceback, a detailed report
0:14
showing exactly where the error occurred
0:17
and how your program arrived there. So,
0:20
if you want to understand this properly,
0:21
I have this code here.
0:23
If I run this code,
0:24
as you can see, I got some kind of
0:26
error. So, this error is what we call
0:28
traceback. As you can see, traceback,
0:31
mostly we call So, all of these things
0:33
is a traceback. So, inside of this
0:36
traceback, there are some elements which
0:37
help us to handle errors in Python
0:39
because some elements that we can find
0:41
inside of this traceback is error type,
0:44
the error message, the call stack, and
0:46
the location where exact file and line
0:49
that specific error occurred.
0:52
How to read a traceback in Python.
0:55
If you want to read a traceback in
0:57
Python in order to understand the which
0:58
type of error you are getting,
1:00
you need to start from the bottom.
1:04
Okay? And then keep going up because the
1:07
actual error is in the last line, as we
1:09
can see, index error, okay? So, this is
1:11
the error that we got. So, that's why
1:13
it's very important for you to read the
1:15
traceback from the bottom up to the top.
1:20
If you try to read it from the top, then
1:22
go down, you will get lost.
1:25
Then the second thing that you need to
1:26
do is to identify the type, the type of
1:29
error because this tells you what kind
1:31
of a problem occurred. For example,
1:33
here,
1:34
I got I'm reading my traceback from the
1:36
bottom up to top. So, then I will
1:39
identify the type of error. The type of
1:40
error which I got is index errors.
1:43
List index out of range, because when
1:46
you identify the type the error, easily
1:50
you can know what the problem is, okay?
1:52
You can know what is the problem. Okay?
1:54
Because when you identify the type of
1:56
error, this will tell you what type what
1:58
kind of problem occurred and how you can
1:59
handle it.
2:01
Then the third one is to read the
2:03
message, because when you read the
2:05
message, it often contains the solution
2:07
hint. As you can see, for example, if I
2:09
read this message, it says it says print
2:11
ice cream three, error index three
2:13
doesn't exist.
2:14
That's why it's very important for you
2:15
also to read the message. So, how you
2:18
can read a traceback in Python in
2:19
Python? Read it from the bottom up to
2:22
the top. Okay, identify the type of
2:24
error, read the message, then check the
2:27
location. Check the location, because
2:31
if you can if you can check the
2:32
location, you can go directly to the
2:35
indicated line. So, how you can check
2:37
the location? It's very easy, for
2:38
example, here
2:39
here I identify the kind of error, here
2:42
I just read the message, okay, and here
2:44
it gives me some hint, it tells me that
2:46
index three doesn't exist. Then I can
2:48
check the location, because the location
2:49
is here, okay, main python.py, this
2:52
document that we are using, then it told
2:53
me in the line seven. Line seven, if I
2:56
come into my code, line seven is this
2:58
one. So, as you know, the message told
3:01
me index three doesn't exist. Yeah, it's
3:04
actually index three doesn't really
3:05
exist, because if I count it in my list,
3:07
I have one, two, three. I have
3:09
chocolate, vanilla, and strawberry. Here
3:11
I have only three items and I'm trying
3:14
to access index uh which is which is
3:18
three. It doesn't exist, okay, because
3:20
my index, as we know,
3:22
index starts from zero, one, two, and I
3:25
try to
3:27
access three. Three doesn't exist.
3:30
So, now if I want to correct this, it's
3:31
very easy, I can just give either
3:34
two or one or zero, because those those
3:37
values they exist.
3:38
If I give two here, if I save, and if I
3:41
run again this same code, see what I'm
3:43
going to get.
3:46
I got strawberry, so which means no
3:48
error. That's This is how you read a
3:51
traceback in Python, okay? Always read
3:53
it from the bottom and up to the top.
3:56
And also one tip I'll also say, a long
3:59
trace back doesn't mean that several
4:01
errors occurred, okay? Because sometimes
4:03
it just indicates your program called
4:05
many functions because it is like, okay?
4:07
If your function
4:08
called a lot of a lot of functions,
4:11
okay? Obviously that it will show you
4:12
the detailed uh report of how that error
4:16
occurred. So, that's why it's very
4:18
important to read from the bottom up to
4:20
the top. Because if you start reading,
4:21
for example, if you get a very big
4:23
report, a very big trace back, if you
4:25
start reading from the bottom, uh it's
4:28
so If you start reading from the top up
4:30
to the bottom, you will get lost. You
4:32
will not understand it properly. That's
4:33
why it's very important for you to read
4:35
from the bottom, then go to the top.
4:37
Now, let's start to understand some
4:38
common Python error types. The first
4:41
type that we have in Python is syntax
4:43
error. How syntax error see? Syntax
4:46
error occurs when Python can't
4:47
understand your code, like forgetting
4:49
punctuation in a sentence or something
4:52
like that. For example, see?
4:53
If you look in this my code, I have def
4:55
function,
4:56
then message hello world, print message,
4:59
return message. So, here I have some
5:01
mistake which I did, okay? If I save
5:03
this and if I try to run this code,
5:06
obviously I'm going to get an error.
5:09
See? What is error? Syntax error.
5:12
It was expecting a here a comma here,
5:15
okay? Because
5:16
I forget to give this.
5:18
Okay? So, that's why I got this error.
5:20
So, this is a very common error that we
5:22
got always, a syntax error. And if you
5:25
want to correct this error, it's very
5:27
easy. As you can see, I just put there
5:29
and save. Now, if I run this,
5:32
boom, the code is working normally. So,
5:34
it will not print anything because I
5:36
didn't print anything. Some common
5:38
syntax error that can usually happen is
5:40
when we missing, okay? After if, for,
5:43
def, class. When we miss them, usually
5:45
this syntax error will occur. Or, maybe
5:47
sometime unclosed parent brackets or or
5:51
brace will cause also this
5:54
Sometimes unclosed quotes in strings,
5:56
misplaced operators, these also will
5:58
cause syntax errors in your Python code.
6:01
The second error that usually happen is
6:03
indentation error.
6:06
This indentation error
6:08
is like Python use indentation to define
6:10
code blocks, unlike many other programs
6:12
that use So, usually when you use
6:14
inconsistent indentation, this
6:16
indentation error will appear. For
6:17
example, here in my function, if instead
6:19
of me writing return in the same line
6:21
with print, for example, if I just give
6:22
something like this, and if I save this,
6:26
if I try to run this code, let's see. As
6:29
you can see, now I got indentation
6:31
error. Okay? As we said, Python use
6:34
indentation error to format its own
6:36
code, okay? So, this error also happen a
6:39
lot of times, okay? This happened
6:41
because I gave extra space before the
6:44
return. So, if I want to fix this one, I
6:46
just need to come back here, save again,
6:48
then run. This error will not happen
6:51
again.
6:52
Indentation rules are Use always four
6:55
space per level, okay? Which is
6:57
recommended. It doesn't mean that you
6:58
need to use always only four four. You
7:00
can use either two, either one, either
7:02
three. It's up on you, but the
7:04
recommended one is use only four space,
7:07
which is better because even
7:08
automatically by
7:10
some editing code editors, it gives
7:12
gives you four space.
7:14
Be consistent. Never mix space and tabs,
7:17
okay? Always, for example, when you
7:19
write on def function, always give
7:22
an an enter instead of giving space
7:25
manually.
7:26
Because if you give space manually, for
7:28
sure, after some point, you might forget
7:30
how many space that you are giving. So,
7:32
the third error that happen usually is
7:34
runtime errors. Unlike syntax error,
7:36
this only appears when your program is
7:38
running. For example, when your code is
7:39
running and you try to access one
7:41
element in a list and this element
7:43
doesn't exist. Sure, it's going to give
7:46
you the index error. This index error is
7:49
very common. Usually happen when your
7:50
code is running. So, this type of error
7:52
we call it as runtime error. So, apart
7:55
of this example, so there is something
7:57
there another one which also for the
7:58
same type error.
8:00
So, type error usually happen also when
8:03
your code is running. Okay, for example,
8:05
if I have something like this, say, and
8:08
if
8:10
I run this code, as you can see, I got
8:12
type error. Okay, so this type error
8:14
usually is also inside of
8:17
runtime error because this errors
8:19
usually happen when your code is
8:21
running. Okay, for example, in between
8:23
your code
8:24
your code is running normal normal
8:26
normal, then after some time it reach it
8:28
the point that you did something which
8:29
is you are not supposed to do. For
8:31
example, here I try to add
8:33
an integer with a string which is not
8:35
allowed in Python. That's why I got type
8:37
error. Okay, for example, unsupported
8:38
operator type error. Support plus int
8:41
and string is not supported in Python. I
8:43
need to convert one into another one.
8:46
And another type also that can happen is
8:49
value error. Okay, for example, we have
8:51
this code here. I'm trying to convert
8:53
this ABC into int. We know that this is
8:55
not possible because ABC is a string.
8:58
Okay, I can there is no way for me to
8:59
convert this ABC into string. So, that's
9:02
why if I if I do this operation, I'm
9:04
going to get value error. Okay, for
9:06
example, invalid literal for int with
9:08
base 10. This is also one type of error
9:11
that we consider runtime. And apart of
9:13
this also another error, for example,
9:15
look at this code here. I have my
9:17
dictionary. I have name which is equal
9:20
Alice. Then, when I print my dictionary,
9:23
I call age. So, this keyword for sure
9:26
doesn't exist inside of my dictionary.
9:28
That's why we'll get key error. This is
9:30
another error that happen also in
9:32
runtime errors. There are a lot of
9:34
errors that can happen in runtime error.
9:37
For example, attribute error also it
9:40
happens. So, those type of error we call
9:42
them as a runtime error. Usually happen
9:44
when our program is running. Then we try
9:48
to do one operation while our program is
9:50
running then for sure it's going to give
9:52
us some error. So, that's why I always
9:54
give attention into this type of error.
9:56
So, the best way for
9:59
fixing errors in Python is as we said,
10:01
okay? Always read the traceback
10:03
completely, okay? Don't jump into the
10:04
code immediately.
10:06
Reproduce the error. Run again to
10:08
confirm its consistent, okay? Because
10:10
when you fix error, try to run it again
10:12
to see if there is still any error or
10:14
not. And always try to isolate the
10:17
problem. For example, comment out the
10:19
code section to find the source of your
10:21
errors.
10:22
That's why when you're writing any code,
10:24
always comment. Or if you ask any AI to
10:27
write code for you, always ask him also
10:30
to add some comment in your code so that
10:33
you can easily identify where your code
10:36
is having a problem. So, that you can
10:38
also what this function does, what this
10:40
function does. So, all of these things
10:42
is very important and will help you to
10:44
handle the errors in Python easily. So,
10:47
those are some tips that you can use to
10:49
handle error in Python. Yeah, for sure
10:50
there is a lot of ways also that we can
10:53
handle errors in Python for them by
10:55
using try except. So, this will be a a
10:57
talk for another video when we'll talk
10:58
about try except in Python. So, my name
11:02
is Juan. If you like the video, you
11:03
know, give a like, comment in the video
11:05
and see you in the next video.
11:07
Bye.
#Science

