1 00:00:00,000 --> 00:00:01,115 [No audio] 2 00:00:01,140 --> 00:00:03,540 Hey, welcome to this new lecture. This 3 00:00:03,540 --> 00:00:06,060 is the most important lecture you have 4 00:00:06,060 --> 00:00:09,720 had so far in this course. So please try 5 00:00:09,720 --> 00:00:11,850 to make the most of it, and this 6 00:00:11,850 --> 00:00:13,680 actually a new video that I'm adding to 7 00:00:13,680 --> 00:00:15,720 the course. I didn't have this lecture a 8 00:00:15,720 --> 00:00:18,120 few times ago. So I added this quite a 9 00:00:18,120 --> 00:00:21,600 while after I published the course, and 10 00:00:21,930 --> 00:00:23,730 until now, I had explained this 11 00:00:23,730 --> 00:00:25,770 concept, so the errors little by 12 00:00:25,770 --> 00:00:28,350 little in other lectures, but I felt 13 00:00:28,350 --> 00:00:30,720 that was not enough, and I was 14 00:00:30,720 --> 00:00:34,050 seeing that students were still having 15 00:00:34,050 --> 00:00:35,940 difficulties in understanding and 16 00:00:35,940 --> 00:00:38,160 dealing with errors in Python. So I 17 00:00:38,160 --> 00:00:41,400 decided to make a dedicated lecture for 18 00:00:41,400 --> 00:00:45,960 errors in Python. So here we are, and I 19 00:00:45,960 --> 00:00:47,730 said, this is the most important lecture 20 00:00:47,730 --> 00:00:49,410 because you know, we'll learn about 21 00:00:49,410 --> 00:00:53,580 functions and streams and numbers until 22 00:00:53,580 --> 00:00:56,970 now in the previous videos, and these 23 00:00:56,970 --> 00:00:59,010 are individual concepts that, you know, you'll 24 00:00:59,010 --> 00:01:00,780 learn now or later, you'll learn 25 00:01:00,780 --> 00:01:03,720 functions, and strings, and etc. But if 26 00:01:03,720 --> 00:01:06,060 you don't know how to read an error, how 27 00:01:06,060 --> 00:01:08,760 to understand an error and deal with 28 00:01:08,760 --> 00:01:11,190 it, if you don't know that, you will 29 00:01:11,190 --> 00:01:14,010 have troubles dealing with everything, 30 00:01:14,040 --> 00:01:16,140 every other objects, like functions, and 31 00:01:16,140 --> 00:01:19,740 streams, and anomalies, etc. So 32 00:01:19,770 --> 00:01:22,050 understanding an error and knowing how 33 00:01:22,050 --> 00:01:24,270 to deal with it, how to fix it is very 34 00:01:24,270 --> 00:01:27,600 important, and even the most experienced 35 00:01:27,600 --> 00:01:29,820 programmers, they make errors in a 36 00:01:29,820 --> 00:01:32,490 programs. The difference is that they 37 00:01:32,490 --> 00:01:35,370 know how to read these errors, how to 38 00:01:35,370 --> 00:01:37,470 understand that and fix them. But 39 00:01:37,470 --> 00:01:39,780 everyone makes errors. So if an error 40 00:01:39,780 --> 00:01:43,140 shows in your program, don't panic, just 41 00:01:43,170 --> 00:01:44,820 follow the instructions that I will give 42 00:01:44,820 --> 00:01:47,280 you in this video on how to fix the 43 00:01:47,280 --> 00:01:49,380 error. So that's what this lecture is 44 00:01:49,380 --> 00:01:52,530 about. So what is an error anyway? An 45 00:01:52,530 --> 00:01:56,340 error in a program is a bug that causes 46 00:01:56,340 --> 00:01:59,610 the program to function incorrectly. Now 47 00:01:59,610 --> 00:02:01,920 in Python, we have basically two types 48 00:02:01,920 --> 00:02:04,590 of errors. We have syntax errors, and we 49 00:02:04,590 --> 00:02:07,920 have exceptions. Let's first focus on 50 00:02:07,920 --> 00:02:09,720 syntax errors, and let's forget about 51 00:02:09,745 --> 00:02:12,475 exceptions. We'll go to exceptions, 52 00:02:12,515 --> 00:02:15,845 after we explain syntax errors. I have 53 00:02:15,870 --> 00:02:18,690 this code here, and actually, this is a 54 00:02:18,690 --> 00:02:21,300 pointer file. This is the icon showing 55 00:02:21,330 --> 00:02:23,700 on a Mac. So currently, I'm on a Mac 56 00:02:23,700 --> 00:02:25,590 computer. But that doesn't make the 57 00:02:25,590 --> 00:02:28,080 slightest difference. No matter where 58 00:02:28,105 --> 00:02:29,995 you are, on Linux, Windows, or Mac, 59 00:02:30,240 --> 00:02:33,390 everything is the same, and so, I have a 60 00:02:33,390 --> 00:02:35,400 terminal here, and let me go ahead and 61 00:02:35,430 --> 00:02:37,740 execute this program. This has a few 62 00:02:37,740 --> 00:02:41,670 errors. So let me show you. On Mac, you 63 00:02:41,670 --> 00:02:44,820 can call python3 with the python3 64 00:02:44,845 --> 00:02:46,500 command, on Windows, you can just 65 00:02:46,500 --> 00:02:49,830 call python, or whatever command you're 66 00:02:49,830 --> 00:02:52,440 using, you can also use any editor that 67 00:02:52,440 --> 00:02:55,920 you like, and so errors.py is 68 00:02:55,920 --> 00:02:59,550 the name I gave to the script, execute. 69 00:03:00,870 --> 00:03:06,600 So this is now example of a syntax 70 00:03:06,630 --> 00:03:10,620 error, and this is the entire body of 71 00:03:10,620 --> 00:03:12,990 the error message. I know this is very 72 00:03:12,990 --> 00:03:15,480 important. So whenever you get an 73 00:03:15,480 --> 00:03:17,790 error, the first advice I give you is 74 00:03:17,790 --> 00:03:21,111 don't panic, just focus on the error, read it 75 00:03:21,136 --> 00:03:22,680 line by line, just like you're 76 00:03:22,680 --> 00:03:26,880 reading a poem or whatever. So the first 77 00:03:26,910 --> 00:03:31,980 line of the error points you to the name 78 00:03:31,980 --> 00:03:33,720 of the file that has the error, in this 79 00:03:33,720 --> 00:03:37,020 case is errors.py, and then you have a 80 00:03:37,020 --> 00:03:38,610 comma, and after the comma, you have the 81 00:03:38,610 --> 00:03:41,760 line where the error occurred. So it's 82 00:03:41,760 --> 00:03:45,040 line 3, and you can see that line 3 83 00:03:45,065 --> 00:03:46,920 is this one here. But you also have 84 00:03:46,920 --> 00:03:49,020 for your convenience, Python prints out 85 00:03:49,020 --> 00:03:52,980 the line in the terminal. So int 9 86 00:03:52,980 --> 00:03:55,170 here is where the error is, and just 87 00:03:55,170 --> 00:03:58,110 after that you have the type of the 88 00:03:58,110 --> 00:04:00,750 error. So it's a syntax error. You also 89 00:04:00,750 --> 00:04:02,910 have other types of errors such as name 90 00:04:02,910 --> 00:04:04,800 error, type error, but those are 91 00:04:04,800 --> 00:04:07,230 exceptions, and I'll explain later why 92 00:04:07,230 --> 00:04:09,990 we call them exceptions. So this is one 93 00:04:09,990 --> 00:04:12,360 type of error, syntax error, and the others 94 00:04:12,360 --> 00:04:15,180 are exceptions. So this is a type and 95 00:04:15,180 --> 00:04:17,070 then you have a description after the 96 00:04:17,095 --> 00:04:21,990 column. Some times the description is more 97 00:04:21,990 --> 00:04:24,660 specific, more detailed, this time is 98 00:04:24,660 --> 00:04:28,440 just invalid syntax. So you have to 99 00:04:28,440 --> 00:04:32,010 figure out where you did some error, 100 00:04:32,040 --> 00:04:35,580 where you missed some syntax of 101 00:04:35,580 --> 00:04:38,430 your program, and you also have this 102 00:04:38,430 --> 00:04:42,300 arrow here pointing upwards. The arrow 103 00:04:42,725 --> 00:04:46,415 points you either at the token, that the 104 00:04:46,440 --> 00:04:49,290 error is occuring or at the end of 105 00:04:49,290 --> 00:04:52,320 the token. So a token is, you know, this 106 00:04:52,320 --> 00:04:56,700 is a token, in this case, 9. So it can 107 00:04:56,700 --> 00:04:59,220 be a number, it can be a string, anything. 108 00:05:00,000 --> 00:05:02,070 So at this point, this is pointing at 109 00:05:02,100 --> 00:05:05,580 this, at the token. But it could also be like 110 00:05:05,580 --> 00:05:11,610 that, 999, save the script, execute, and in 111 00:05:11,610 --> 00:05:13,080 this case, you see that it's pointing 112 00:05:13,080 --> 00:05:15,840 you at the end of the token. So here's 113 00:05:15,870 --> 00:05:18,180 around here, you have an error now, and 114 00:05:18,180 --> 00:05:21,600 I know that int is actually a function, 115 00:05:21,990 --> 00:05:24,270 and functions in Python, they need to 116 00:05:24,270 --> 00:05:29,280 have brackets. So you need to pass 999, 117 00:05:29,340 --> 00:05:32,880 inside brackets, like that, and you 118 00:05:32,880 --> 00:05:34,621 leave that, save 119 00:05:34,659 --> 00:05:36,827 [No audio] 120 00:05:36,852 --> 00:05:40,321 and execute again. All right, 121 00:05:40,346 --> 00:05:42,414 [No audio] 122 00:05:42,439 --> 00:05:44,250 you've got another error, but 123 00:05:44,250 --> 00:05:46,920 don't panic, again, this is my advice 124 00:05:46,920 --> 00:05:50,460 always. Instead, read the error, it 125 00:05:50,460 --> 00:05:53,190 says, File errors.py the file 126 00:05:53,220 --> 00:05:56,527 line 5, oh line 5 this time, so it's not line 3 127 00:05:56,552 --> 00:05:59,021 anymore, which means that line 3 128 00:05:59,469 --> 00:06:02,490 was fixed. So what Python is doing 129 00:06:02,520 --> 00:06:04,800 is, it's going through all the lines one 130 00:06:04,800 --> 00:06:07,290 by one from top to bottom, it checks the 131 00:06:07,290 --> 00:06:10,020 first line, it says, Oh, it's fine. No 132 00:06:10,020 --> 00:06:11,760 error there, goes to the next line, 133 00:06:11,910 --> 00:06:13,950 no error, goes to the third line, 134 00:06:13,950 --> 00:06:16,350 no error this time. Fourth line is also 135 00:06:16,350 --> 00:06:19,440 fine, and fifth line has an error. So 136 00:06:19,500 --> 00:06:21,870 again, it's a syntax error, and this one 137 00:06:21,870 --> 00:06:25,050 is more specifically described. So 138 00:06:25,380 --> 00:06:27,840 Missing parentheses in call to print. 139 00:06:29,370 --> 00:06:31,080 Now that you'll get this error only 140 00:06:31,080 --> 00:06:32,820 if you are on python3, if you are 141 00:06:32,850 --> 00:06:34,920 on python2, this won't be an 142 00:06:34,920 --> 00:06:38,940 error, because in python2, print was 143 00:06:39,480 --> 00:06:42,210 a statement and was not a function. So 144 00:06:42,240 --> 00:06:45,060 with statements like return or print in 145 00:06:45,060 --> 00:06:46,620 python2, you didn't have to pass 146 00:06:46,620 --> 00:06:49,080 brackets, the syntax would be correct. 147 00:06:49,620 --> 00:06:52,350 So again, this arrow here is pointing 148 00:06:52,375 --> 00:06:57,381 you at the token. So we just add brackets there, 149 00:06:57,405 --> 00:06:59,405 [No audio] 150 00:06:59,419 --> 00:07:01,890 Ctrl S to save the script 151 00:07:01,950 --> 00:07:04,620 and execute again, this time, we don't 152 00:07:04,620 --> 00:07:06,720 get any error. So the script executed 153 00:07:06,720 --> 00:07:09,150 fine, and printed out the output here, 154 00:07:09,277 --> 00:07:11,795 1, 2, 3. So we have 3 print 155 00:07:11,820 --> 00:07:14,700 functions, printing out output. This 156 00:07:14,700 --> 00:07:16,740 doesn't return any output because we're 157 00:07:16,740 --> 00:07:19,950 not printing anything. So how this just gets 158 00:07:19,950 --> 00:07:23,010 the function input and produces some 159 00:07:23,010 --> 00:07:24,750 output. But that doesn't print anything. 160 00:07:25,730 --> 00:07:27,210 If you want to print it, you want to 161 00:07:27,210 --> 00:07:29,580 pass the print statement function there. 162 00:07:30,095 --> 00:07:32,375 There could also be other types of syntax 163 00:07:32,400 --> 00:07:36,390 errors, and you know, such as you want 164 00:07:36,390 --> 00:07:39,990 to do, form a list 1, 2, and 3. 165 00:07:40,260 --> 00:07:44,010 But instead of closing it with a square 166 00:07:44,010 --> 00:07:45,630 brackets, you use a round bracket 167 00:07:45,630 --> 00:07:47,880 instead, like that, save the script and 168 00:07:47,880 --> 00:07:50,910 execute, and you see that you got a 169 00:07:50,910 --> 00:07:54,120 syntax error, invalid syntax at line 170 00:07:54,120 --> 00:07:57,990 5, which is this one in here. Again, 171 00:07:57,990 --> 00:08:00,480 the arrow points you at the token. So 172 00:08:00,480 --> 00:08:04,410 you need to figure out how to fix this 173 00:08:04,440 --> 00:08:06,650 round bracket here, and you know that 174 00:08:06,675 --> 00:08:08,745 [No audio] 175 00:08:08,770 --> 00:08:10,597 you need to close it with a square bracket, 176 00:08:10,770 --> 00:08:12,932 so that should fix the issue. So this 177 00:08:12,957 --> 00:08:15,120 error, so that's about syntax 178 00:08:15,120 --> 00:08:17,880 errors, they are very easy to fix. In 179 00:08:17,880 --> 00:08:19,740 the next lecture, I'll explain 180 00:08:20,190 --> 00:08:22,440 exceptions. So see you later.