1 00:00:00,000 --> 00:00:02,165 [No audio] 2 00:00:02,190 --> 00:00:03,870 Hi again and welcome to this new 3 00:00:03,870 --> 00:00:05,910 lecture. In the previous lecture, we 4 00:00:05,910 --> 00:00:09,270 talked about errors in general, and I 5 00:00:09,330 --> 00:00:12,900 focused on syntax errors, which is one 6 00:00:12,900 --> 00:00:14,520 of the two types of errors you have in 7 00:00:14,520 --> 00:00:16,950 Python. So you have syntax errors, and 8 00:00:16,950 --> 00:00:20,130 you have exceptions. Now we're going to 9 00:00:20,310 --> 00:00:22,740 talk about exceptions in Python. So 10 00:00:22,740 --> 00:00:24,780 every other type of error that is not a 11 00:00:24,780 --> 00:00:28,200 syntax error as an exceptions, and often 12 00:00:29,040 --> 00:00:30,720 all errors, syntax errors and 13 00:00:30,720 --> 00:00:32,250 exceptions, they are referred to as 14 00:00:32,275 --> 00:00:36,030 errors. So you're going to hear this all 15 00:00:36,030 --> 00:00:38,370 over the place. Now I have a new script 16 00:00:38,370 --> 00:00:41,790 here, that I created for this lecture. 17 00:00:43,447 --> 00:00:46,050 Try to guess what I'm going to get as 18 00:00:46,050 --> 00:00:49,020 output when I execute the script. So I 19 00:00:49,020 --> 00:00:51,990 have a equals to 1, b equals to 20 00:00:52,680 --> 00:00:59,081 character 2, print int 2.5, and print a + b. 21 00:00:59,490 --> 00:01:01,680 So guess what I'm going to get 22 00:01:03,030 --> 00:01:07,770 for a moment. Again, now let me run the 23 00:01:07,770 --> 00:01:12,055 script, python3 errors.py. 24 00:01:12,080 --> 00:01:15,209 [No audio] 25 00:01:15,234 --> 00:01:19,795 File errors.py line 4, which is the last line. 26 00:01:19,819 --> 00:01:21,819 [No audio] 27 00:01:21,844 --> 00:01:23,821 This one, we get a syntax error, 28 00:01:24,180 --> 00:01:25,284 invalid syntax. 29 00:01:25,308 --> 00:01:27,386 [No audio] 30 00:01:27,411 --> 00:01:28,650 Now, this can be very 31 00:01:28,650 --> 00:01:30,960 confusing for you now, because you know, 32 00:01:30,960 --> 00:01:34,200 you're looking at the prints a + b 33 00:01:35,550 --> 00:01:37,890 expression, but you don't see any error 34 00:01:37,890 --> 00:01:41,220 there. Also, for a beginner, this can be 35 00:01:41,220 --> 00:01:44,550 quite frustrating. But try to look back 36 00:01:45,240 --> 00:01:49,680 on the left of arrow here. So think of 37 00:01:49,680 --> 00:01:52,620 the arrow that the arrow is pointing at 38 00:01:52,620 --> 00:01:54,960 the token, and the token has not been 39 00:01:55,410 --> 00:01:57,840 written correctly in the script. So in 40 00:01:57,840 --> 00:02:00,210 this case, you know, the problem here is 41 00:02:00,210 --> 00:02:04,500 that this line here, it has an open 42 00:02:04,530 --> 00:02:08,220 bracket, round bracket, and then it has 43 00:02:08,250 --> 00:02:11,280 int function, and then the int function 44 00:02:11,340 --> 00:02:14,940 has its own brackets that is wrapping 45 00:02:14,970 --> 00:02:17,700 inside its inputs. So these are the 46 00:02:17,700 --> 00:02:19,740 brackets of the int function, but the print 47 00:02:19,740 --> 00:02:21,420 function doesn't have a closing 48 00:02:21,420 --> 00:02:24,360 brackets. What you did instead, what I 49 00:02:24,360 --> 00:02:26,460 did actually, what I did is that 50 00:02:26,460 --> 00:02:28,890 instead of putting a bracket there, I 51 00:02:28,890 --> 00:02:32,400 actually wrote another print 52 00:02:32,550 --> 00:02:37,213 function. So Python was expecting a 53 00:02:37,238 --> 00:02:39,180 closing round bracket, but I 54 00:02:39,180 --> 00:02:41,490 wrote, I typed in a print function. 55 00:02:41,760 --> 00:02:43,770 That's why this is saying that this 56 00:02:43,800 --> 00:02:46,590 print function is not in the correct 57 00:02:46,650 --> 00:02:49,980 position, that's the idea. As always, 58 00:02:49,980 --> 00:02:52,650 when you see this arrow, first look at 59 00:02:52,650 --> 00:02:55,290 this line, but then look before that, 60 00:02:55,680 --> 00:02:58,505 also. Sorry about this. 61 00:02:59,845 --> 00:03:00,990 So always, when 62 00:03:00,990 --> 00:03:03,690 you see this arrow here, a first look at 63 00:03:03,690 --> 00:03:06,270 this line here, but also don't forget 64 00:03:06,270 --> 00:03:09,030 that the problem might also be before 65 00:03:09,030 --> 00:03:12,660 this line. Okay, so that was about the 66 00:03:12,660 --> 00:03:16,410 syntax error. I'll explain why I talked 67 00:03:16,410 --> 00:03:18,810 about the syntax error here. Let us 68 00:03:18,840 --> 00:03:20,970 execute this again. So the syntax error 69 00:03:20,970 --> 00:03:22,980 was fixed now, but we still have another 70 00:03:22,980 --> 00:03:26,160 error. So the reason I included a syntax 71 00:03:26,190 --> 00:03:29,160 error in my code was to show you that 72 00:03:29,160 --> 00:03:31,920 Python actually first checks for syntax 73 00:03:31,950 --> 00:03:35,130 errors. So basically, it parses the 74 00:03:35,130 --> 00:03:37,890 code, it looks for syntax errors, it 75 00:03:37,890 --> 00:03:41,310 doesn't execute the code yet. So when I 76 00:03:41,310 --> 00:03:44,100 executed python3 errors.py in 77 00:03:44,100 --> 00:03:46,380 here, the code was not executed, but the 78 00:03:46,380 --> 00:03:48,660 interpreter just checks for syntax 79 00:03:48,690 --> 00:03:52,020 errors. It doesn't check for exceptions 80 00:03:52,080 --> 00:03:54,480 just yet. So first, you need to fix the 81 00:03:54,480 --> 00:03:56,040 syntax errors, and that's what we did 82 00:03:56,040 --> 00:03:57,900 here, we added this bracket, and we fixed 83 00:03:57,900 --> 00:03:59,850 the syntax errors. Now Python is 84 00:03:59,850 --> 00:04:03,720 throwing out an exception, and let's 85 00:04:03,750 --> 00:04:06,600 here is where I executed the code, and 86 00:04:06,630 --> 00:04:08,610 see the next line we have number 2 87 00:04:08,610 --> 00:04:11,550 there, which is coming from the output 88 00:04:11,575 --> 00:04:14,065 of this line here. So from line 3, 89 00:04:14,165 --> 00:04:16,625 we got the output correctly. So 90 00:04:16,650 --> 00:04:18,240 basically, again, Python execute the 91 00:04:18,265 --> 00:04:20,321 script from top to bottom 92 00:04:20,345 --> 00:04:24,870 if it doesn't find syntax errors, if it 93 00:04:24,870 --> 00:04:26,700 finds syntax errors, it doesn't execute 94 00:04:26,700 --> 00:04:30,120 anything. So 2 is printed out, and 95 00:04:30,120 --> 00:04:32,757 then you get this Traceback of the error 96 00:04:32,781 --> 00:04:34,985 [No audio] 97 00:04:35,010 --> 00:04:38,970 which starts there and ends in here, that's 98 00:04:38,970 --> 00:04:40,506 the block of the error. 99 00:04:40,531 --> 00:04:42,546 [No audio] 100 00:04:42,571 --> 00:04:47,760 Sometimes, the errors may have more than one type 101 00:04:47,760 --> 00:04:49,740 of error. So in this case, we have only 102 00:04:49,740 --> 00:04:51,450 one type of error, which is the type 103 00:04:51,450 --> 00:04:54,510 error, but you may have multiple blocks 104 00:04:54,510 --> 00:04:57,420 here. However, the most important error 105 00:04:57,445 --> 00:05:00,145 that you must focus on is the last 106 00:05:00,270 --> 00:05:02,820 line of the error. So the last block of 107 00:05:02,850 --> 00:05:05,880 of the error. In this case, we have only 108 00:05:05,905 --> 00:05:08,395 one block. So we're focus on in that. 109 00:05:09,317 --> 00:05:13,082 In the line 4, which is this one in here. 110 00:05:14,255 --> 00:05:16,025 This is the line, again the line is 111 00:05:16,050 --> 00:05:18,210 printing out, just like in the case of 112 00:05:18,210 --> 00:05:20,910 syntax error, and here you have a type of 113 00:05:20,910 --> 00:05:22,890 the error here, which is a TypeError. 114 00:05:23,730 --> 00:05:25,590 So what's a TypeError? A TypeError 115 00:05:25,590 --> 00:05:28,710 means that there is something wrong with 116 00:05:29,503 --> 00:05:33,323 one of your object types in your script. 117 00:05:34,925 --> 00:05:36,629 And you have the description here, 118 00:05:36,816 --> 00:05:41,550 unsupported operand type for '+'. So this 119 00:05:41,575 --> 00:05:44,785 is trying to say that the + operator 120 00:05:45,185 --> 00:05:47,465 has an unsupported type. So the + 121 00:05:47,490 --> 00:05:49,200 operator, in other words, doesn't 122 00:05:49,200 --> 00:05:50,970 support one of the types that you have 123 00:05:50,995 --> 00:05:54,865 given to it. So it either a type of 124 00:05:55,230 --> 00:05:58,440 variable a, or the type of variable b. So 125 00:05:58,440 --> 00:06:01,890 it says int and string. So it doesn't 126 00:06:01,890 --> 00:06:04,380 specifically say that whether int or 127 00:06:04,500 --> 00:06:07,350 string is object. But it says that you 128 00:06:07,350 --> 00:06:10,920 cannot use a + operator with an 129 00:06:10,920 --> 00:06:14,151 integer and a string, and that's 130 00:06:14,176 --> 00:06:16,350 logically wrong, because you 131 00:06:16,350 --> 00:06:20,730 cannot add a number to some text. That's 132 00:06:20,730 --> 00:06:22,800 what Python doesn't understand, and it 133 00:06:22,800 --> 00:06:25,920 throws a TypeError. So exceptions are 134 00:06:25,920 --> 00:06:28,410 like logical errors, and you need to use 135 00:06:28,410 --> 00:06:32,370 your logic now to fix the error, and use 136 00:06:32,370 --> 00:06:34,740 your logic by carefully inspecting the 137 00:06:34,740 --> 00:06:37,855 error, and that's what we did. So 138 00:06:37,879 --> 00:06:42,210 what we want to do, we want to fix this. Now 139 00:06:42,210 --> 00:06:44,730 it's up to me whether I meant to 140 00:06:44,970 --> 00:06:48,960 concatenate this two objects, or do a 141 00:06:48,960 --> 00:06:52,830 mathematical addition operation. So now 142 00:06:52,830 --> 00:06:55,800 let's say if I was intending to do an 143 00:06:55,800 --> 00:06:57,510 addition between these two numbers, then 144 00:06:57,535 --> 00:07:03,083 I would have to convert b to float or an integer. 145 00:07:04,263 --> 00:07:09,078 Save that execute, and yeah 146 00:07:09,120 --> 00:07:10,800 in this case, you don't get any error, you 147 00:07:10,800 --> 00:07:14,220 get 2 print it out from the second, from 148 00:07:14,220 --> 00:07:17,370 the third line, and 3.0 from the last 149 00:07:17,370 --> 00:07:21,180 line that we have just fixed. However, 150 00:07:21,210 --> 00:07:23,055 if my intention was to actually 151 00:07:23,079 --> 00:07:25,568 [No audio] 152 00:07:25,593 --> 00:07:28,050 print out the concatenation between these two 153 00:07:28,050 --> 00:07:31,230 strings, instead of converting b to a 154 00:07:31,230 --> 00:07:35,405 float without have to convert a to a string. 155 00:07:36,952 --> 00:07:38,970 In this case, I'll get the text 156 00:07:39,000 --> 00:07:42,810 1 and 2, 1 and 2. So that would 157 00:07:42,810 --> 00:07:47,550 be a string object, not a number. Even 158 00:07:47,550 --> 00:07:49,590 though here, it shows as a number. It's 159 00:07:49,590 --> 00:07:52,830 just how the terminal prints it out. So 160 00:07:52,830 --> 00:07:56,130 again, these are errors that occur in 161 00:07:56,155 --> 00:07:58,645 runtime so while the script executes. 162 00:07:59,015 --> 00:08:01,805 Syntax errors are parsing errors, so the 163 00:08:01,830 --> 00:08:04,050 interpreter tries to understand whether 164 00:08:04,050 --> 00:08:06,510 the script is syntactically correct, 165 00:08:06,810 --> 00:08:08,370 whether we are full, whether you have 166 00:08:08,370 --> 00:08:12,960 followed the Python syntax rules. So you 167 00:08:12,960 --> 00:08:14,700 need brackets, a closing bracket for 168 00:08:14,700 --> 00:08:16,920 opening bracket, you need a closing 169 00:08:16,920 --> 00:08:19,297 quote, after the opening quote, and so on. 170 00:08:19,321 --> 00:08:21,545 [No audio] 171 00:08:21,590 --> 00:08:24,230 Now, there are also other types of 172 00:08:24,510 --> 00:08:27,540 exceptions, not only TypeError, you 173 00:08:27,540 --> 00:08:31,680 know, you may have a name error. Let's 174 00:08:31,680 --> 00:08:35,220 say here, instead of printing out all that, 175 00:08:35,250 --> 00:08:36,460 we print out c, 176 00:08:36,484 --> 00:08:38,488 [No audio] 177 00:08:38,513 --> 00:08:41,700 save, execute. And here is a 178 00:08:41,725 --> 00:08:45,265 TraceBack, again 2 was printed out from 179 00:08:45,455 --> 00:08:49,355 the third line, and the TraceBack says line 180 00:08:49,380 --> 00:08:56,055 4 at print c, NameError, name c is not defined. 181 00:08:56,079 --> 00:08:58,872 [No audio] 182 00:08:58,897 --> 00:09:00,180 So again, this is not a 183 00:09:00,180 --> 00:09:02,640 syntax error, because you haven't made 184 00:09:02,640 --> 00:09:04,410 any mistakes with the syntax. You know, 185 00:09:04,410 --> 00:09:06,750 you have this variable name, which is 186 00:09:06,796 --> 00:09:09,076 correct. You have brackets opening 187 00:09:09,180 --> 00:09:11,220 round brackets and closing round 188 00:09:11,220 --> 00:09:13,170 brackets. So everything is syntactically 189 00:09:13,170 --> 00:09:15,810 correct, both this c object Python 190 00:09:15,810 --> 00:09:18,420 doesn't know this, you haven't defined 191 00:09:18,420 --> 00:09:20,940 this c variable. So Python doesn't know 192 00:09:20,940 --> 00:09:23,250 what to print out. You know Python is able to 193 00:09:23,250 --> 00:09:26,490 print out a because it knows that a 194 00:09:26,525 --> 00:09:30,515 refers to the 1, to integer 1, so it 195 00:09:30,540 --> 00:09:32,310 prints out integer 1, but c doesn't 196 00:09:32,310 --> 00:09:35,155 have anything. So you get this NameError, 197 00:09:35,877 --> 00:09:38,055 and whenever you get a NameError, 198 00:09:38,079 --> 00:09:40,955 you know that's this name here has 199 00:09:40,980 --> 00:09:43,350 not been defined by you. So to fix this, 200 00:09:43,350 --> 00:09:47,220 you may want to defined c, like that and 201 00:09:47,278 --> 00:09:49,888 execute and you don't get any error. You 202 00:09:49,920 --> 00:09:52,200 may also have all types of errors such 203 00:09:52,200 --> 00:09:55,393 as c divided by 0, see what you get. 204 00:09:55,430 --> 00:09:57,555 [No audio] 205 00:09:57,586 --> 00:10:01,235 A ZeroDivisionError, division by zero. 206 00:10:01,589 --> 00:10:04,169 That's the description of this error. So 207 00:10:04,169 --> 00:10:06,539 division by zero is not mathematically 208 00:10:06,569 --> 00:10:09,509 possible or meaningful, and since Python 209 00:10:09,539 --> 00:10:12,269 is based on mathematics, then it throws 210 00:10:12,269 --> 00:10:14,159 an error. In the end, you need to fix 211 00:10:14,159 --> 00:10:16,139 that you need to remove that expression 212 00:10:16,139 --> 00:10:18,419 that divides by 0, and yeah, that's 213 00:10:18,419 --> 00:10:21,188 about errors in Python. 214 00:10:21,212 --> 00:10:23,298 [No audio] 215 00:10:23,323 --> 00:10:26,489 I hope you understood a great deal of this lecture 216 00:10:26,489 --> 00:10:28,709 and the previous lecture as well. So 217 00:10:28,709 --> 00:10:30,929 it's relatively easy to fix errors. 218 00:10:31,204 --> 00:10:33,754 However, sometimes, 219 00:10:33,779 --> 00:10:35,849 there are errors that you might have 220 00:10:35,849 --> 00:10:38,039 difficulties understanding and fixing 221 00:10:38,039 --> 00:10:41,219 them. So later, for example, we're 222 00:10:41,244 --> 00:10:43,229 going to work on libraries, and 223 00:10:43,229 --> 00:10:44,459 sometimes the libraries they have 224 00:10:44,459 --> 00:10:46,889 different kinds of errors. However, 225 00:10:46,889 --> 00:10:48,749 nothing to worry about, because there 226 00:10:48,749 --> 00:10:50,494 are other things that you can do 227 00:10:51,087 --> 00:10:52,529 for an error that you don't 228 00:10:52,529 --> 00:10:55,199 understand, and I'll talk about that in 229 00:10:55,199 --> 00:10:58,280 the next lecture. So I will see you there, see you.