[No audio]
Hey, welcome to this new lecture. This
is the most important lecture you have
had so far in this course. So please try
to make the most of it, and this
actually a new video that I'm adding to
the course. I didn't have this lecture a
few times ago. So I added this quite a
while after I published the course, and
until now, I had explained this
concept, so the errors little by
little in other lectures, but I felt
that was not enough, and I was
seeing that students were still having
difficulties in understanding and
dealing with errors in Python. So I
decided to make a dedicated lecture for
errors in Python. So here we are, and I
said, this is the most important lecture
because you know, we'll learn about
functions and streams and numbers until
now in the previous videos, and these
are individual concepts that, you know, you'll
learn now or later, you'll learn
functions, and strings, and etc. But if
you don't know how to read an error, how
to understand an error and deal with
it, if you don't know that, you will
have troubles dealing with everything,
every other objects, like functions, and
streams, and anomalies, etc. So
understanding an error and knowing how
to deal with it, how to fix it is very
important, and even the most experienced
programmers, they make errors in a
programs. The difference is that they
know how to read these errors, how to
understand that and fix them. But
everyone makes errors. So if an error
shows in your program, don't panic, just
follow the instructions that I will give
you in this video on how to fix the
error. So that's what this lecture is
about. So what is an error anyway? An
error in a program is a bug that causes
the program to function incorrectly. Now
in Python, we have basically two types
of errors. We have syntax errors, and we
have exceptions. Let's first focus on
syntax errors, and let's forget about
exceptions. We'll go to exceptions,
after we explain syntax errors. I have
this code here, and actually, this is a
pointer file. This is the icon showing
on a Mac. So currently, I'm on a Mac
computer. But that doesn't make the
slightest difference. No matter where
you are, on Linux, Windows, or Mac,
everything is the same, and so, I have a
terminal here, and let me go ahead and
execute this program. This has a few
errors. So let me show you. On Mac, you
can call python3 with the python3
command, on Windows, you can just
call python, or whatever command you're
using, you can also use any editor that
you like, and so errors.py is
the name I gave to the script, execute.
So this is now example of a syntax
error, and this is the entire body of
the error message. I know this is very
important. So whenever you get an
error, the first advice I give you is
don't panic, just focus on the error, read it
line by line, just like you're
reading a poem or whatever. So the first
line of the error points you to the name
of the file that has the error, in this
case is errors.py, and then you have a
comma, and after the comma, you have the
line where the error occurred. So it's
line 3, and you can see that line 3
is this one here. But you also have
for your convenience, Python prints out
the line in the terminal. So int 9
here is where the error is, and just
after that you have the type of the
error. So it's a syntax error. You also
have other types of errors such as name
error, type error, but those are
exceptions, and I'll explain later why
we call them exceptions. So this is one
type of error, syntax error, and the others
are exceptions. So this is a type and
then you have a description after the
column. Some times the description is more
specific, more detailed, this time is
just invalid syntax. So you have to
figure out where you did some error,
where you missed some syntax of
your program, and you also have this
arrow here pointing upwards. The arrow
points you either at the token, that the
error is occuring or at the end of
the token. So a token is, you know, this
is a token, in this case, 9. So it can
be a number, it can be a string, anything.
So at this point, this is pointing at
this, at the token. But it could also be like
that, 999, save the script, execute, and in
this case, you see that it's pointing
you at the end of the token. So here's
around here, you have an error now, and
I know that int is actually a function,
and functions in Python, they need to
have brackets. So you need to pass 999,
inside brackets, like that, and you
leave that, save
[No audio]
and execute again. All right,
[No audio]
you've got another error, but
don't panic, again, this is my advice
always. Instead, read the error, it
says, File errors.py the file
line 5, oh line 5 this time, so it's not line 3
anymore, which means that line 3
was fixed. So what Python is doing
is, it's going through all the lines one
by one from top to bottom, it checks the
first line, it says, Oh, it's fine. No
error there, goes to the next line,
no error, goes to the third line,
no error this time. Fourth line is also
fine, and fifth line has an error. So
again, it's a syntax error, and this one
is more specifically described. So
Missing parentheses in call to print.
Now that you'll get this error only
if you are on python3, if you are
on python2, this won't be an
error, because in python2, print was
a statement and was not a function. So
with statements like return or print in
python2, you didn't have to pass
brackets, the syntax would be correct.
So again, this arrow here is pointing
you at the token. So we just add brackets there,
[No audio]
Ctrl S to save the script
and execute again, this time, we don't
get any error. So the script executed
fine, and printed out the output here,
1, 2, 3. So we have 3 print
functions, printing out output. This
doesn't return any output because we're
not printing anything. So how this just gets
the function input and produces some
output. But that doesn't print anything.
If you want to print it, you want to
pass the print statement function there.
There could also be other types of syntax
errors, and you know, such as you want
to do, form a list 1, 2, and 3.
But instead of closing it with a square
brackets, you use a round bracket
instead, like that, save the script and
execute, and you see that you got a
syntax error, invalid syntax at line
5, which is this one in here. Again,
the arrow points you at the token. So
you need to figure out how to fix this
round bracket here, and you know that
[No audio]
you need to close it with a square bracket,
so that should fix the issue. So this
error, so that's about syntax
errors, they are very easy to fix. In
the next lecture, I'll explain
exceptions. So see you later.