Afrikaans
Akan
Albanian
Amharic
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
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 1
Hey welcome to this new lecture! This is the most important lecture you have had 3
2
so far in this course, so please try to make the most of it and this is actually 5
3
a new video that I am adding to the course. I didn't have this lecture a few 7
4
times ago so I had this quite a while after I published the course and until 9
5
now I had explained this concept, so the errors little by little in other 11
6
lectures but I felt that was not enough and I was seeing that students were 13
7
still having difficulties in understanding and dealing with errors in 15
8
Python so I decided to make a dedicated lecture for errors in Python. So here we 17
9
are and I said this is the most important lecture because you know we 19
10
we learnt about functions and strings and numbers until now in the previous 21
11
videos and these are individual concepts that you know you'll learn, now or later you'll 23
12
learn functions and strings and etc, but if you don't know how to read an error 25
13
how to to understand an error and deal with it if you don't know that you will 27
14
have trouble dealing with everything every other object like functions and 29
15
streams and numbers etc. So understanding and error and knowing how to deal with 31
16
it, how to fix it is very important and even the most experienced programmers 33
17
they make errors in their programs. The difference is that they know how to read 35
18
these errors. How to understand and fix them, but everyone makes errors. So if 37
19
an error shows in your program don't panic just for all the instructions that 39
20
I'll give you in this video on how to fix the error so that's what this 41
21
lecture is about. So what is an error anyway? An error in a program is a bug 43
22
that causes the program to function incorrectly. Now in Python we have 45
23
basically two types of errors. We have syntax errors and we have exceptions. 47
24
Let's first focus on syntax errors and let's forget about exceptions. We'll go 49
25
to exceptions after we explain syntax errors. I have this code here and 51
26
actually this is a Python file. This is the icon showing on a Mac so currently 53
27
I am on a Mac computer but that doesn't make the slightest difference. No matter 55
28
where you are on Linux Windows or Mac everything is the same. So I have the 57
29
terminal here and let me go ahead and execute this program. This has a few 59
30
errors so let me show them. On Mac you can call Python 3 with the python3 61
31
command, on Windows you can just call python or whatever command you are 63
32
using. You can also use any editor that you like and so errors.py is the 65
33
name I gave to the script, execute. So this is now an example of a syntax error 67
34
and this is the entire body of the error message and this is very important, so 69
35
whenever you get an error the first advice I give you is don't panic just 71
36
focus on the error. Read it line by line just like you're reading a poem or 73
37
whatever so the first line of the error points you to the name of the file that 75
38
has the error. In this case is errors.py. Then you have a comma and after 77
39
the comma you have the line where the error occurred. 79
40
So it's line 3 and you can see that line 3 is this one here but you also have for 81
41
your convenience Python prints out the line in the terminal so int 9. Here is 83
42
where the error is and just after that you have the type of the error so it's a 85
43
syntax error, you also have other types of errors such as name error, type error, 87
44
but those are exceptions and I'll explain later why we call them 89
45
exceptions, so this is one type of error a syntax error and the others are 91
46
exceptions. So this is the type and then you have the description after the colon. 93
47
Sometimes the description is more specific more detailed. This time is just 95
48
invalid syntax. So you have to figure out where 97
49
you did some error where you missed some syntax of your program. 99
50
And you also have this arrow here pointing upwards and the arrow points you even at the 101
51
token that the error is occurring or at the end of the token, so a token is you 103
52
know, this is the token in this case 9. So it can be a number, it can be string, 105
53
anything, so at this point this is pointing at this, at the token but 107
54
it could also be like that. 999 save the script, execute. In this 109
55
case you see that it's pointing you at the end of the token. So here's, around 111
56
here you have an error now and I know that int is actually a function and 113
57
functions important they need to have brackets so you need to pass 999 inside 115
58
brackets like that and you leave that. Save and execute again. All right, you've 117
59
got another error but don't panic again. This is my advice always, instead 119
60
read the error it says file errors.py the file line 5, oh line 5 this time. 121
61
So it's not line 3 anymore which means that line 3 was fixed so what Python is 123
62
doing is it's going through all the lines one by one from top to bottom, it 125
63
checks the first line it says: "Oh it's fine, no error there". Goes to the next 127
64
line no error, goes through the third line, no error this time, fourth line is 129
65
also fine, and fifth line has an error so again it's a syntax error and this one 131
66
is more specifically described so missing parentheses in call to print. 133
67
Note that you get this error only if you are on Python 3. If you are on Python 2 135
68
this won't be an error because in Python 2 print was a statement and was not 137
69
function so with statements like return or print in Python - you didn't 139
70
have to pass brackets. This syntax would be correct. 141
71
So again this arrow here is pointing you and the token so we just add 143
72
brackets there, ctrl s to save the script and execute again. This time we don't get 145
73
any error so the script executed fine and print it out the output here. 147
74
1 2 3 so we had 3 print functions printing out output. This doesn't return 149
75
any output because we're not printing anything so this just gets the function 151
76
input and produces some output but doesn't print anything. If you want to 153
77
print it, you want to pass the print statement function there. There could 155
78
also be other types of syntax errors and you know such as you want to define a 157
79
least 1, 2, 3, but instead of closing it with a square brackets, you use 159
80
a round bracket instead like that. Save the script and execute and you see that 161
81
you got a syntax error, invalid syntax at line 5 which is this one in here. Again 163
82
the arrow points you at the token so you need to figure out how to fix this round 165
83
bracket here and you know that. You need to close it with a square bracket so 167
84
that should fix the issue. So these are� Ao that's about syntax errors. They are 169
85
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.