[No audio]
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 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 as an exceptions, 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 the script. So I
have a equals to 1, b equals to
character 2, print int 2.5, and print a + b.
So guess what I'm going to get
for a moment. Again, now let me run the
script, python3 errors.py.
[No audio]
File errors.py line 4, which is the last line.
[No audio]
This one, we get a syntax error,
invalid syntax.
[No audio]
Now, this can be very
confusing for you now, because you know,
you're looking at the prints a + b
expression, but you don't see any error
there. Also, for a beginner, 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 the 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 then it has
int function, and then the int function
has its own brackets that is wrapping
inside its inputs. So these are the
brackets of the int function, but the print
function doesn't have a closing
brackets. 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. As always,
when you see this arrow, first look at
this line, but then look before that,
also. Sorry about this.
So always, when
you see this arrow here, a 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 the syntax error here. Let us
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 python3 errors.py 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 this bracket, 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 2
there, which is coming from the output
of this line here. So from line 3,
we got the output correctly. So
basically, again, Python execute 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 Traceback of the error
[No audio]
which starts there and ends in here, that's
the block of the error.
[No audio]
Sometimes, the errors may have more than one type
of error. So in this case, we have only
one type of error, which is the type
error, but you may have multiple blocks
here. However, the most important error
that you must focus on is the last
line of the error. So the last block of
of the error. In this case, we have only
one block. So we're focus on in that.
In the 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 here you have a type of
the error here, which is a TypeError.
So what's a TypeError? A TypeError
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 '+'. So this
is trying to say that the + operator
has an unsupported type. So the +
operator, in other words, doesn't
support one of the types that you have
given to it. So it either a type of
variable a, or the type of variable b. So
it says int and string. So it doesn't
specifically say that whether int or
string is object. But it says that you
cannot use a + 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 doesn't understand, and it
throws a TypeError. So exceptions are
like logical errors, and you need to use
your logic now to fix the error, and 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 this two objects, or do a
mathematical addition operation. So now
let's say if I was intending to do an
addition between these two numbers, then
I would have to convert b to float or an integer.
Save that execute, and yeah
in this case, you don't get any error, you
get 2 print it 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
[No audio]
print out the concatenation between these two
strings, instead of converting b to a
float without have to convert a to a string.
In this case, I'll get the text
1 and 2, 1 and 2. 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 while the script executes.
Syntax errors are parsing errors, so the
interpreter tries to understand whether
the script is syntactically correct,
whether we are full, whether you have
followed the Python syntax rules. So you
need brackets, a closing bracket for
opening bracket, you need a closing
quote, after the opening quote, and so on.
[No audio]
Now, there are also other types of
exceptions, not only TypeError, you
know, you may have a name error. Let's
say here, instead of printing out all that,
we print out c,
[No audio]
save, execute. And here is a
TraceBack, again 2 was printed out from
the third line, and the TraceBack says line
4 at print c, NameError, name c is not defined.
[No audio]
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
brackets. 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, so it
prints out integer 1, but c doesn't
have anything. So you get this NameError,
and whenever you get a NameError,
you know that's this name here has
not been defined by you. So to fix this,
you may want to defined c, like that and
execute and you don't get any error. You
may also have all types of errors such
as c divided by 0, see what you get.
[No audio]
A ZeroDivisionError, division by zero.
That's the description of this error. So
division by zero is not mathematically
possible or meaningful, and since Python
is based on mathematics, then it throws
an error. In the end, you need to fix
that you need to remove that expression
that divides by 0, and yeah, that's
about errors in Python.
[No audio]
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 the libraries they have
different kinds of errors. However,
nothing to worry about, because there
are other things that you can do
for an error that you don't
understand, and I'll talk about that in
the next lecture. So I will see you there, see you.