Hi again and welcome to this new lecture! In the previous lecture we talked about
errors in general and I focused on syntax errors which is one of the two
types of errors that you have in Python. So you have syntax errors and you have
exceptions. Now we're going to talk about exceptions in Python, so every other type
of error that is not a syntax error is an exception. And often all errors,
syntax errors and exceptions they are referred to as errors so you're going to
hear this all over the place. Now I have a new script here that I created for
this lecture. Try to guess what I'm going to get as
output when I execute this script, so I have a equal to 1, n equals to characters 2.
Print int 2.5 and print a plus b. So guess what I'm going to get for a moment.
Okay, now let me run the script Python 3. Errors.py. File errors.py line 4
which is the last line, this one. We get a syntax error, invalid syntax. Now this
can be very confusing for you now because you know you're looking at the
print a plus b expression, but you don't see any error there. So for a beginner of
this can be quite frustrating but try to look back on the left of arrow here, so
think of the arrow that the arrow is pointing at the token and token has not
been written correctly in the script, so in this case you know the problem
here is that this line here it has an open bracket, round bracket
and than it has an int function, and then the int function has its own brackets that is
wrapping inside its input, so these are the brackets of the int function, but the
print function doesn't have a closing bracket. What you did instead, what I did
actually, what I did is that instead of putting a bracket there I actually wrote
another print function so Python was expecting a closing round
bracket, but I wrote I typed in a print function. That's why this is saying that
this print function is not in the correct position. That's the idea.
So always when you see this arrow first look at this line event look before that
also. Sorry about this! So always when you see this arrow here, first look at this
line here but also don't forget that the problem might also be before this
line. Okay, so that was about the syntax error.
I'll explain why I talked about this syntax error here let me execute this
again. So the syntax error was fixed now, but we still have another error. So the
reason I included a syntax error in my code was to show you that Python
actually first checks for syntax errors so basically it parses the code, it looks
for syntax errors, it doesn't execute the code yet. So when I executed Python 3
errors that's why i in here the code was not executed but the interpreter just
checks for syntax errors, it doesn't check for exceptions just yet. So first
you need to fix the syntax errors and that's what we did here. We added
these brackets and we fixed the syntax errors. Now Python is throwing out an exception and
let's, here is where I executed the code and see the next line we have number two
there which is coming from the output of this line here, so from line three we
got the output correctly. So basically again Python executes the script from top
to bottom if it doesn't find
syntax errors. If it finds syntax errors it doesn't execute anything. So 2 is
printed out and then you get this trace back of the error which starts there and
ends in here that's a block of the error. Sometimes the errors may have more than
one type of error. So in this case we have only one type of error which is
a type error, but you may have multiple blocks here. However the most important
error that you must focus on is the lost line of the error, so the last block of
of the error. In this case we have only one block so we're focused on that.
And line 4 which is this one in here. This is the line. Again the line is printing out
just like in the case of syntax error. And you have a type of the error here
which is a type error. So what's a type error? A type error means that there is
something wrong with one of your object types in your script, and you have the
description here. Unsupported operand type for plus so this is trying to say
that the plus operator has an unsupported type so the plus operator in
other words doesn't support one of the types that you have given to it, so it's
either the type of variable a or the type of variable b. So it says int and
string, so it doesn't specifically say whether int or string is an object but
it says that you cannot use a plus operator with an integer and a string.
And that's logically wrong because you cannot add a number to some
text. That's what Python does not understand so it throws a type error.
So exceptions are like logical errors and you need to use your logic now to
fix the error. And you use your logic by carefully inspecting the error and
that's what we did. So what we want to do, we want to fix
this. Now it's up to me whether I meant to concatenate these two objects
or do a mathematical addition operation. So let's say if I was intending to
do an addition between these two numbers then I would have to convert b to a float
or an integer. Save that, execute, and yeah in this case you
don't get any error. You get 2 printed out from the second, from the third line
and 3.0 from the last line that we have just fixed. However if my intention was
to actually print out the concatenation between these two strings, instead of
converting b to a float, I would have to convert a to a string.
In this case I would get the text 12. 12, so that would be
a string object, not a number. Even though here it shows as a number it's just how
the terminal prints it out. So again these are errors that occur in runtime, so when
the script executes. Syntax errors are parsing errors so the interpreter tries
to understand whether the script is syntactically correct, whether
you have followed the Python syntax rules. So you need a bracket,
a closing bracket for an opening bracket. You need a closing quote after the opening
quote and so on. Now there are also other types of exceptions, not only type
error. You know you may have a name error. Let's say here instead of printing out
that, we print out c. Save, execute and here is the trace back.
Again 2 was printed out from the third line and the trace back says line 4 at
print c, name error name c is not defined. So again this is not a syntax
error because you haven't made any mistakes with the syntax. You know you have
this variable name which is correct, you have brackets opening, round brackets
and closing round bracket, so everything is syntactically correct both this c
object Python doesn't know this. You haven't defined this c variable so
Python doesn't know what to print out. You know python is able to print out a
because it knows that a refers to the 1 to integer 1 and so it prints out
integer 1, but c doesn't have anything so you get this name error and whenever
you get a name error you know that this name here has not been defined by you so
to fix this you may want to define c like that and execute and you don't get
any error. You may also have other types of errors such as c divided by 0.
See what you get. Zero division error division by 0. That's the
description of this error so division by 0 is not mathematically possible or
meaningful and since python is based on mathematics it throws an error in
and you need to fix that. You need to remove that expression that divides by 0.
Yeah, that's about errors in Python. I hope you understood a great deal of this
lecture and the previous lecture as well. So it's relatively easy to fix errors,
however sometimes there are errors that you might have
difficulties understanding and fixing them so later for example we're going to
work on libraries and sometimes libraries they have different kinds of
errors, however nothing to worry about because there are other things that you
can do for an arrow that you don't understand.
And I'll talk about that in the next lecture, so I'll see you there!