1
00:00:01,400 --> 00:00:07,610
As a programmer, you you'll spend 60% of your time coding and 40% of your time fixing bugs.

2
00:00:07,610 --> 00:00:12,080
So it's very important to understand what errors are and how to fix them.

3
00:00:12,080 --> 00:00:13,880
So let's revise.

4
00:00:15,190 --> 00:00:15,880
Errors.

5
00:00:16,300 --> 00:00:18,340
We have two types of errors, actually.

6
00:00:18,340 --> 00:00:21,220
We have syntax errors and we have exceptions.

7
00:00:21,220 --> 00:00:27,910
So syntax errors are errors which happen before the interpreter.

8
00:00:29,150 --> 00:00:31,190
Compiles the codes.

9
00:00:31,730 --> 00:00:36,470
So first the interpreter just checks the syntax, right?

10
00:00:36,500 --> 00:00:41,470
It looks if you have the brackets correctly, the commas, the dots.

11
00:00:41,480 --> 00:00:43,850
For example, this line here.

12
00:00:44,540 --> 00:00:50,000
This starts with a square bracket, but it ends with a parentheses.

13
00:00:50,720 --> 00:00:54,950
The correct code would be to have a closing parentheses here.

14
00:00:55,700 --> 00:00:57,050
But that is not the case.

15
00:00:57,050 --> 00:01:01,340
And that's why this code will return of this.

16
00:01:02,160 --> 00:01:02,900
The results.

17
00:01:02,910 --> 00:01:05,430
So this is a syntax error.

18
00:01:06,800 --> 00:01:13,250
An error which happens before the code is compiled and executed for the processor.

19
00:01:14,150 --> 00:01:21,050
So this year tells you with this arrow where the error is, so closing parentheses does not match.

20
00:01:21,320 --> 00:01:22,520
Opening parenthesis.

21
00:01:22,520 --> 00:01:22,790
Right.

22
00:01:22,790 --> 00:01:23,870
That's quite clear.

23
00:01:25,120 --> 00:01:27,990
But error messages are not always clear.

24
00:01:28,000 --> 00:01:29,380
Let's look at this one here.

25
00:01:31,030 --> 00:01:31,380
Right.

26
00:01:31,390 --> 00:01:33,480
So we have this opening bracket.

27
00:01:33,490 --> 00:01:34,900
We have this closing bracket.

28
00:01:34,900 --> 00:01:35,590
That's fine.

29
00:01:36,380 --> 00:01:42,410
But instead of the periods between the variable and the methods, we have a comma here.

30
00:01:42,530 --> 00:01:44,180
So this should be a period.

31
00:01:47,210 --> 00:01:55,460
In that case, we get a syntax error, of course, because the syntax is not correct, but the error

32
00:01:55,460 --> 00:01:56,960
is not so clear.

33
00:01:56,960 --> 00:02:03,440
The message it says that we forgot some parentheses around the comprehension target.

34
00:02:05,150 --> 00:02:13,130
So the interpreter looks at these two and it thinks that perhaps you meant to have them inside parentheses

35
00:02:13,130 --> 00:02:14,000
like that.

36
00:02:15,150 --> 00:02:20,250
So it's not so clear for the interpreter to understand what you're actually doing.

37
00:02:21,660 --> 00:02:29,010
So in that case, you should look carefully at the codes and try to use your experience to understand

38
00:02:29,010 --> 00:02:31,980
where the syntax error actually is.

39
00:02:32,000 --> 00:02:35,160
So in that case, this comma should be replaced with.

40
00:02:36,420 --> 00:02:37,260
A period.

41
00:02:38,440 --> 00:02:41,180
So these two were examples of syntax errors.

42
00:02:41,200 --> 00:02:43,150
Then we have exceptions.

43
00:02:44,050 --> 00:02:47,080
This code here is syntactically correct.

44
00:02:48,440 --> 00:02:56,090
So all the symbols and the rules of the language syntax are respected.

45
00:02:56,090 --> 00:03:01,790
But the problem here is that we have this variable which doesn't exist.

46
00:03:02,090 --> 00:03:05,240
So the variable name itself is fine.

47
00:03:05,480 --> 00:03:11,570
It's syntactically correct, but it has not been declared in the script.

48
00:03:11,600 --> 00:03:14,000
That's why we get this name error.

49
00:03:15,800 --> 00:03:18,930
So we have syntax errors and we have nightmares.

50
00:03:18,990 --> 00:03:24,860
And then we have attribute errors, which is produced by this code, for example.

51
00:03:26,560 --> 00:03:34,060
This is an attribute error because this method here has been mistyped.

52
00:03:35,090 --> 00:03:37,320
There is not such a method for strings.

53
00:03:37,340 --> 00:03:39,860
The correct method would be strip.

54
00:03:39,890 --> 00:03:42,460
So in that case you get this attribute error.

55
00:03:42,470 --> 00:03:46,460
So an attribute a method is actually an attribute.

56
00:03:47,240 --> 00:03:48,050
So.

57
00:03:49,860 --> 00:03:57,430
Here we are trying to access that strip attributes out of the string object.

58
00:03:57,450 --> 00:03:59,520
That's why we get this error.

59
00:03:59,760 --> 00:04:03,030
STR object has no attribute strip.

60
00:04:04,710 --> 00:04:14,670
So such errors are caught when the interpreter is trying to compile the codes to make sense of the codes.

61
00:04:17,890 --> 00:04:19,300
Here is another example.

62
00:04:21,680 --> 00:04:29,360
So we asked the user to enter a number such as the year of birth, and then we have this variable currency

63
00:04:29,370 --> 00:04:29,870
here.

64
00:04:31,340 --> 00:04:38,240
Then we want to calculate the age of the person using the current year minus the year of birth for that

65
00:04:38,240 --> 00:04:38,630
person.

66
00:04:38,630 --> 00:04:46,940
And we print out age, but the user didn't enter the actual year, but they entered something like this,

67
00:04:46,940 --> 00:04:47,420
right?

68
00:04:47,930 --> 00:04:51,440
In that case, this produces a value error.

69
00:04:52,490 --> 00:04:56,270
It says that this thing cannot be.

70
00:04:58,390 --> 00:05:00,190
Convert it into an integer.

71
00:05:00,190 --> 00:05:05,640
So the INT method expects something like a string which contains a number inside.

72
00:05:05,650 --> 00:05:10,960
For example this here, but this is not really a number.

73
00:05:10,960 --> 00:05:13,810
It contains some dots and things like that.

74
00:05:18,770 --> 00:05:20,780
And that brings us to the.

75
00:05:23,400 --> 00:05:25,920
Topic of handling exceptions.

76
00:05:27,350 --> 00:05:30,620
Okay, so these are exceptions, value error, attribute error.

77
00:05:30,650 --> 00:05:38,300
These are known as exceptions and try accepts helps us to predict an error.

78
00:05:39,380 --> 00:05:44,990
So we get that code and we transform it into this code.

79
00:05:47,160 --> 00:05:51,380
So we tried to get the input from the user.

80
00:05:51,390 --> 00:05:58,440
We tried to convert it into an INT, and then we tried to calculate the age and then we print out the

81
00:05:58,440 --> 00:05:58,930
age.

82
00:05:58,950 --> 00:06:05,100
So if the user will enter the ear correctly, for example, 3001.

83
00:06:06,970 --> 00:06:09,250
This code will be executed.

84
00:06:12,130 --> 00:06:16,990
So in other words, where there are no errors, this code will be executed.

85
00:06:17,470 --> 00:06:23,770
But if this code produces an error, then Python will execute this code.

86
00:06:28,150 --> 00:06:36,310
Now it's better that instead of doing except here you should mention the actual error that you accept.

87
00:06:38,510 --> 00:06:39,870
Which you expect.

88
00:06:39,890 --> 00:06:46,130
So in this case, we expect a value error when the user doesn't enter a correct format, Right.

89
00:06:46,160 --> 00:06:47,260
Like this one.

90
00:06:47,270 --> 00:06:52,610
So it's it's better to specify the error specifically in here.

91
00:06:54,900 --> 00:07:00,690
Now, this line here, it's better to keep it outside of this tri, except because.

92
00:07:01,840 --> 00:07:07,090
You don't really expect an error in this declaration.

93
00:07:10,040 --> 00:07:20,660
So mostly you want to put these dynamic codes inside the try block So whenever possible, try to put

94
00:07:20,660 --> 00:07:24,290
other codes outside of the tri block.

95
00:07:26,570 --> 00:07:27,230
If you can.

96
00:07:27,230 --> 00:07:28,520
Otherwise it's fine.

97
00:07:28,550 --> 00:07:31,610
If this was also inside this block.

98
00:07:32,250 --> 00:07:33,690
Somewhere in here.

99
00:07:34,050 --> 00:07:34,420
Right.

100
00:07:34,440 --> 00:07:35,820
That would also be fine.

101
00:07:41,770 --> 00:07:49,870
It's important to know that tri accepts does not catch syntax errors because try accepts only catches

102
00:07:49,870 --> 00:07:55,270
exceptions which are codes when the interpreter is trying to compile the code.

103
00:07:55,750 --> 00:08:04,050
So this code here, for example, has this age variable which is missing a parentheses.

104
00:08:04,060 --> 00:08:07,800
So in that case, if you run this code you'll get a syntax error.

105
00:08:07,810 --> 00:08:15,850
So the try block the try clause will not be able to catch that syntax error, which is a good thing.

106
00:08:15,850 --> 00:08:19,940
So first of all, you always want to have your code syntactically correct.

107
00:08:19,960 --> 00:08:23,020
Then you catch all the exceptions.

108
00:08:25,170 --> 00:08:30,570
And then you might have the question, oh, when to use, accept and when to use even live else.

109
00:08:30,810 --> 00:08:37,049
Well, here is an example of when you use a fails or if it fails.

110
00:08:37,380 --> 00:08:40,850
So the try accepts block always catches errors.

111
00:08:40,860 --> 00:08:45,330
It doesn't catch logical errors or logical cases.

112
00:08:45,330 --> 00:08:52,830
For example, if you you want to tell the user that, oh, you're the age we calculate it for you is

113
00:08:52,830 --> 00:09:00,330
too big, then you would want to find that out with an if condition here.

114
00:09:01,390 --> 00:09:06,340
Because the interpreter doesn't know what a normal age is.

115
00:09:06,340 --> 00:09:09,910
It doesn't know that humans cannot be 150.

116
00:09:09,910 --> 00:09:14,110
So you have to use if else blocks for that.

117
00:09:15,660 --> 00:09:16,260
Right.

118
00:09:16,770 --> 00:09:20,010
Next topic comments and doc strings.

119
00:09:22,690 --> 00:09:30,400
So this year is a docstring doc strings are used to document what a function does, so you place them

120
00:09:30,400 --> 00:09:36,730
inside the function definition and you use these triple quotes here and there.

121
00:09:37,800 --> 00:09:44,820
So this function is then called in here and then for commenting single lines of code or blocks of code

122
00:09:44,820 --> 00:09:48,090
which are not functions, then you use comments.

123
00:09:48,870 --> 00:09:56,040
So this comment here is telling something is trying to tell us something about the next line which is

124
00:09:56,040 --> 00:09:57,160
placed underneath.

125
00:09:57,180 --> 00:09:58,140
So this one.

126
00:09:58,920 --> 00:10:02,190
But try to not overuse comments, for example.

127
00:10:02,190 --> 00:10:10,170
I think this comment here would be unnecessary because it's clear to the programmer that you're actually

128
00:10:10,170 --> 00:10:15,330
calling the function here so you can omit this comment.

129
00:10:15,750 --> 00:10:22,470
So you only want to comment complex code which you think that it will be harder to understand if you

130
00:10:22,470 --> 00:10:25,290
come back two weeks after or something like that.

131
00:10:27,190 --> 00:10:28,120
Modules.

132
00:10:28,120 --> 00:10:35,770
Modules are Python files which can be imported into another Python file.

133
00:10:35,770 --> 00:10:42,460
So basically when you import my file, my file is a file named my file dot py.

134
00:10:45,790 --> 00:10:53,740
And so when you import that python file, you are making available all the functions and the variables

135
00:10:53,740 --> 00:10:57,030
that that script has inside.

136
00:10:57,040 --> 00:11:04,840
So in here, let's say this my file that SPI script had this area function defined.

137
00:11:05,740 --> 00:11:11,740
So let's suppose that this area function was defined in my file, that's P In that case, we can access

138
00:11:11,740 --> 00:11:21,280
that by referring to my file dot area and then we call that area function and get the output in a variable.

139
00:11:22,860 --> 00:11:24,870
Standard libraries are also.

140
00:11:27,450 --> 00:11:28,470
Python files.

141
00:11:28,470 --> 00:11:32,700
They can be one single dot P file or multiple files.

142
00:11:33,870 --> 00:11:37,890
For example, glop is a standard library request is a standard library.

143
00:11:37,890 --> 00:11:41,070
So these are libraries which you don't have to install.

144
00:11:41,070 --> 00:11:43,020
These are included with Python.

145
00:11:44,570 --> 00:11:49,910
So you just import them and then of course, you use them similarly as we did with this.

146
00:11:49,910 --> 00:11:51,680
So my file area.

147
00:11:51,890 --> 00:11:53,350
Glop that glop.

148
00:11:53,360 --> 00:12:00,890
So glop is a function in the glop library area is a function in the my file module.

149
00:12:01,400 --> 00:12:07,400
So this is a local module and this would be a standard library or a standard module.

150
00:12:07,400 --> 00:12:13,700
You can see depending of whether this is a single file or multiple files.

151
00:12:20,630 --> 00:12:26,330
So that particular function globe would return this list.

152
00:12:26,360 --> 00:12:32,180
Now, sometimes you don't really know if that is a method or a function.

153
00:12:32,180 --> 00:12:38,630
So if Globe is a library or a module, then this is probably a function.

154
00:12:40,090 --> 00:12:46,000
But if this was a class, then this would have to be called a method.

155
00:12:47,280 --> 00:12:52,350
Now it's really it doesn't matter much what you call this, a method or a function.

156
00:12:52,650 --> 00:12:58,530
But if you really want to find out in part charm, you can right click and then you can go to the go

157
00:12:58,530 --> 00:13:00,960
to menu and then go to implementations.

158
00:13:00,960 --> 00:13:03,690
And there you will see what this really is.

159
00:13:03,690 --> 00:13:08,790
If it's inside the glob that P file or if it's inside a class.

160
00:13:10,690 --> 00:13:11,020
Right.

161
00:13:11,020 --> 00:13:19,870
So here I've also tried to use the get function of the requests library so that it's used to get the

162
00:13:19,870 --> 00:13:22,870
contents of a website and then print it out.

163
00:13:22,870 --> 00:13:25,690
So the content is HTML, as you can see here.

164
00:13:25,690 --> 00:13:33,250
It also contains other information which is inside the HTML, for example, this text here.

165
00:13:34,800 --> 00:13:42,030
And so one third party libraries are libraries which have not been included with the Python interpreter.

166
00:13:42,030 --> 00:13:46,620
So you have to install them with PIP installed followed by the library name.

167
00:13:50,680 --> 00:13:54,730
Their behavior is, of course, the same as the standard library.

168
00:13:54,730 --> 00:13:58,000
So you use them using the same syntax, Of course.

169
00:13:59,020 --> 00:13:59,680
Web apps.

170
00:13:59,680 --> 00:14:09,220
Web apps needs a third party library, also known as a framework, a web framework such as Tremlett.

171
00:14:09,220 --> 00:14:15,730
So we did use Tremlett in the course, and we'll use it again to create more complex web apps.

172
00:14:15,730 --> 00:14:22,360
So I'm not going to go deeper into explaining Web apps because we're going to work over and over again

173
00:14:22,360 --> 00:14:24,010
with them throughout the course.

174
00:14:24,430 --> 00:14:29,800
We also covered p one simple Google a library to create desktop apps.

175
00:14:31,570 --> 00:14:34,600
We're going to have more Google apps in the course.

176
00:14:34,600 --> 00:14:37,840
So I'm not going to explain those here as well.

177
00:14:39,400 --> 00:14:45,400
So you have a chance to practice and practice these over and over again throughout the course, either

178
00:14:45,400 --> 00:14:51,000
by instructor led projects or by your own projects, which I'll give you.

179
00:14:51,010 --> 00:14:56,680
I'll hand you out the exercises, the requirements, and you'll be able to work on your own.

180
00:14:57,070 --> 00:15:01,150
So thanks for following this series of reviews.

181
00:15:01,570 --> 00:15:07,240
And let's move on to the real parts of the course building applications.

182
00:15:07,900 --> 00:15:09,670
So it's going to be fun, I promise.

183
00:15:09,670 --> 00:15:11,140
And I'll see you in the next video.

