Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hey welcome to this new lecture! This is
the most important lecture you have had
3
so far in this course, so please try to
make the most of it and this is actually
5
a new video that I am adding to the
course. I didn't have this lecture a few
7
times ago so I had this quite a while
after I published the course and until
9
now I had explained this concept, so the
errors little by little in other
11
lectures but I felt that was not enough
and I was seeing that students were
13
still having difficulties in
understanding and dealing with errors in
15
Python so I decided to make a dedicated
lecture for errors in Python. So here we
17
are and I said this is the most
important lecture because you know we
19
we learnt about functions and strings
and numbers until now in the previous
21
videos and these are individual concepts
that you know you'll learn, now or later you'll
23
learn functions and strings and etc, but
if you don't know how to read an error
25
how to to understand an error and deal
with it if you don't know that you will
27
have trouble dealing with everything
every other object like functions and
29
streams and numbers etc. So understanding
and error and knowing how to deal with
31
it, how to fix it is very important and
even the most experienced programmers
33
they make errors in their programs. The
difference is that they know how to read
35
these errors. How to understand and
fix them, but everyone makes errors. So if
37
an error shows in your program don't
panic just for all the instructions that
39
I'll give you in this video on how to
fix the error so that's what this
41
lecture is about. So what is an error
anyway? An error in a program is a bug
43
that causes the program to function
incorrectly. Now in Python we have
45
basically two types of errors. We have
syntax errors and we have exceptions.
47
Let's first focus on syntax errors and
let's forget about exceptions. We'll go
49
to exceptions after we explain
syntax errors. I have this code here and
51
actually this is a Python file. This is
the icon showing on a Mac so currently
53
I am on a Mac computer but that doesn't
make the slightest difference. No matter
55
where you are on Linux Windows or Mac
everything is the same. So I have the
57
terminal here and let me go ahead and
execute this program. This has a few
59
errors so let me show them. On Mac you
can call Python 3 with the python3
61
command, on Windows you can just call
python or whatever command you are
63
using. You can also use any editor that
you like and so errors.py is the
65
name I gave to the script, execute.
So this is now an example of a syntax error
67
and this is the entire body of the error
message and this is very important, so
69
whenever you get an error the first
advice I give you is don't panic just
71
focus on the error. Read it line by
line just like you're reading a poem or
73
whatever so the first line of the error
points you to the name of the file that
75
has the error. In this case is errors.py.
Then you have a comma and after
77
the comma you have the line where the
error occurred.
79
So it's line 3 and you can see that line
3 is this one here but you also have for
81
your convenience Python prints out the
line in the terminal so int 9. Here is
83
where the error is and just after that
you have the type of the error so it's a
85
syntax error, you also have other types
of errors such as name error, type error,
87
but those are exceptions and I'll
explain later why we call them
89
exceptions, so this is one type of error
a syntax error and the others are
91
exceptions. So this is the type and then
you have the description after the colon.
93
Sometimes the description is more
specific more detailed. This time is just
95
invalid syntax.
So you have to figure out where
97
you did some error where you
missed some syntax of your program.
99
And you also have this arrow here pointing
upwards and the arrow points you even at the
101
token that the error is occurring or at
the end of the token, so a token is you
103
know, this is the token in this case 9.
So it can be a number, it can be string,
105
anything, so at this point this is
pointing at this, at the token but
107
it could also be like that.
999 save the script, execute. In this
109
case you see that it's pointing you at
the end of the token. So here's, around
111
here you have an error now and I know
that int is actually a function and
113
functions important they need to have
brackets so you need to pass 999 inside
115
brackets like that and you leave that.
Save and execute again. All right, you've
117
got another error but don't panic again.
This is my advice always, instead
119
read the error it says file errors.py
the file line 5, oh line 5 this time.
121
So it's not line 3 anymore which means
that line 3 was fixed so what Python is
123
doing is it's going through all the
lines one by one from top to bottom, it
125
checks the first line it says: "Oh it's
fine, no error there". Goes to the next
127
line no error, goes through the third
line, no error this time, fourth line is
129
also fine, and fifth line has an error so
again it's a syntax error and this one
131
is more specifically described so
missing parentheses in call to print.
133
Note that you get this error only if you
are on Python 3. If you are on Python 2
135
this won't be an error because in Python
2 print was a statement and was not
137
function so with statements like
return or print in Python - you didn't
139
have to pass brackets. This syntax would
be correct.
141
So again this arrow here is pointing you
and the token so we just add
143
brackets there, ctrl s to save the script
and execute again. This time we don't get
145
any error so the script executed fine
and print it out the output here.
147
1 2 3 so we had 3 print functions
printing out output. This doesn't return
149
any output because we're not printing
anything so this just gets the function
151
input and produces some output but
doesn't print anything. If you want to
153
print it, you want to pass the print
statement function there. There could
155
also be other types of syntax errors and
you know such as you want to define a
157
least 1, 2, 3, but instead of
closing it with a square brackets, you use
159
a round bracket instead like that. Save
the script and execute and you see that
161
you got a syntax error, invalid syntax at
line 5 which is this one in here. Again
163
the arrow points you at the token so you
need to figure out how to fix this round
165
bracket here and you know that. You need
to close it with a square bracket so
167
that should fix the issue. So these are
Ao that's about syntax errors. They are
169
very easy to fix. In the next lecture I
will explain exceptions so see you there!
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.