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 is actually
a new video that I am adding to the course. I didn't have this lecture a few
times ago so I had 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
we learnt about functions and strings 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 to understand an error and deal with it if you don't know that you will
have trouble dealing with everything every other object like functions and
streams and numbers etc. So understanding and 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 their programs. The difference is that they know how to read
these errors. How to understand and fix them, but everyone makes errors. So if
an error shows in your program don't panic just for all the instructions that
I'll 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 Python file. This is the icon showing on a Mac so currently
I am 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. So I have the
terminal here and let me go ahead and execute this program. This has a few
errors so let me show them. On Mac you can call Python 3 with the python3
command, on Windows you can just call python or whatever command you are
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 an example of a syntax error
and this is the entire body of the error message and 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. 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 a syntax error and the others are
exceptions. So this is the type and then you have the description after the colon.
Sometimes 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 and the arrow points you even at the
token that the error is occurring or at the end of the token, so a token is you
know, this is the token in this case 9. So it can be a number, it can be 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. 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 important they need to have brackets so you need to pass 999 inside
brackets like that and you leave that. Save and execute again. All right, 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 through 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.
Note that you get this error only if you are on Python 3. If you are on Python 2
this won't be an error because in Python 2 print was a statement and was not
function so with statements like return or print in Python - you didn't
have to pass brackets. This syntax would be correct.
So again this arrow here is pointing you and the token so we just add
brackets there, ctrl s to save the script and execute again. This time we don't get
any error so the script executed fine and print it out the output here.
1 2 3 so we had 3 print functions printing out output. This doesn't return
any output because we're not printing anything so this just gets the function
input and produces some output but 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 define a
least 1, 2, 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. You need to close it with a square bracket so
that should fix the issue. So these are Ao that's about syntax errors. They are
very easy to fix. In the next lecture I will explain exceptions so see you there!