You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12
+
<small> Second Edition: July, 2021</small>
13
13
</sub>
14
14
</div>
15
15
</div>
@@ -35,17 +35,17 @@
35
35
36
36
## Python Error Types
37
37
38
-
When we write code it's common that we make a typo or some other common error. If our code fails to run, the python interpreter will display a message, containing feedback with information on where the problem occurs and the type of an error. It will also sometimes gives us suggestions on a possible fix. Understanding different types of errors in programming languages will help us to debug our code quickly and also it makes us better at what we do.
38
+
When we write code it is common that we make a typo or some other common error. If our code fails to run, the Python interpreter will display a message, containing feedback with information on where the problem occurs and the type of an error. It will also sometimes gives us suggestions on a possible fix. Understanding different types of errors in programming languages will help us to debug our code quickly and also it makes us better at what we do.
39
39
40
-
Let's see the most common error types one by one. First let us open our python interactive shell. Go to your you computer terminal and write 'python'. The python interactive shell will be opened.
40
+
Let us see the most common error types one by one. First let us open our Python interactive shell. Go to your you computer terminal and write 'python'. The python interactive shell will be opened.
41
41
42
42
### SyntaxError
43
43
44
44
**Example 1: SyntaxError**
45
45
46
46
```py
47
47
asabeneh@Asabeneh:~$ python
48
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
48
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
49
49
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
50
50
Type "help", "copyright", "credits"or"license"for more information.
51
51
>>>print'hello world'
@@ -56,12 +56,11 @@ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello w
56
56
>>>
57
57
```
58
58
59
-
As you can see we made a syntax error because we forgot to enclose the string with parenthesis and python already suggests the solution. Let's fix it.
59
+
As you can see we made a syntax error because we forgot to enclose the string with parenthesis and Python already suggests the solution. Let us fix it.
60
60
61
61
```py
62
-
Last login: Tue Dec 315:20:41 on ttys002
63
62
asabeneh@Asabeneh:~$ python
64
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
63
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
65
64
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
66
65
Type "help", "copyright", "credits"or"license"for more information.
67
66
>>>print'hello world'
@@ -81,9 +80,8 @@ The error was a _SyntaxError_. After the fix our code was executed without a hit
81
80
**Example 1: NameError**
82
81
83
82
```py
84
-
Last login: Tue Dec 315:20:41 on ttys002
85
83
asabeneh@Asabeneh:~$ python
86
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
84
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
87
85
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
88
86
Type "help", "copyright", "credits"or"license"for more information.
89
87
>>>print(age)
@@ -93,12 +91,11 @@ NameError: name 'age' is not defined
93
91
>>>
94
92
```
95
93
96
-
As you can see from the message above, name age is not defined. Yes, it is true. We did not define an age variable but we were trying to print it out as if we had had declared it. Now, lets fix this by declaring it and assigning with a value.
94
+
As you can see from the message above, name age is not defined. Yes, it is true that we did not define an age variable but we were trying to print it out as if we had had declared it. Now, lets fix this by declaring it and assigning with a value.
97
95
98
96
```py
99
-
Last login: Tue Dec 315:20:41 on ttys002
100
97
asabeneh@Asabeneh:~$ python
101
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
98
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
102
99
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
103
100
Type "help", "copyright", "credits"or"license"for more information.
104
101
>>>print(age)
@@ -118,9 +115,8 @@ The type of error was a _NameError_. We debugged the error by defining the varia
118
115
**Example 1: IndexError**
119
116
120
117
```py
121
-
Last login: Tue Dec 315:20:41 on ttys002
122
118
asabeneh@Asabeneh:~$ python
123
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
119
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
124
120
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
125
121
Type "help", "copyright", "credits"or"license"for more information.
126
122
>>> numbers = [1, 2, 3, 4, 5]
@@ -131,16 +127,15 @@ IndexError: list index out of range
131
127
>>>
132
128
```
133
129
134
-
In the example above, python raised an _IndexError_, because the list has only indexes from 0 to 4 , so it was out of range.
130
+
In the example above, Python raised an _IndexError_, because the list has only indexes from 0 to 4 , so it was out of range.
135
131
136
132
### ModuleNotFoundError
137
133
138
134
**Example 1: ModuleNotFoundError**
139
135
140
136
```py
141
-
Last login: Tue Dec 315:20:41 on ttys002
142
137
asabeneh@Asabeneh:~$ python
143
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
138
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
144
139
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
145
140
Type "help", "copyright", "credits"or"license"for more information.
146
141
>>>import maths
@@ -153,9 +148,8 @@ ModuleNotFoundError: No module named 'maths'
153
148
In the example above, I added an extra s to math deliberately and _ModuleNotFoundError_ was raised. Lets fix it by removing the extra s from math.
154
149
155
150
```py
156
-
Last login: Tue Dec 315:20:41 on ttys002
157
151
asabeneh@Asabeneh:~$ python
158
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
152
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
159
153
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
160
154
Type "help", "copyright", "credits"or"license"for more information.
161
155
>>>import maths
@@ -173,9 +167,8 @@ We fixed it, so let's use some of the functions from the math module.
173
167
**Example 1: AttributeError**
174
168
175
169
```py
176
-
Last login: Tue Dec 315:20:41 on ttys002
177
170
asabeneh@Asabeneh:~$ python
178
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
171
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
179
172
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
180
173
Type "help", "copyright", "credits"or"license"for more information.
181
174
>>>import maths
@@ -193,9 +186,8 @@ AttributeError: module 'math' has no attribute 'PI'
193
186
As you can see, I made a mistake again! Instead of pi, I tried to call a PI function from maths module. It raised an attribute error, it means, that the function does not exist in the module. Lets fix it by changing from PI to pi.
194
187
195
188
```py
196
-
Last login: Tue Dec 315:20:41 on ttys002
197
189
asabeneh@Asabeneh:~$ python
198
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
190
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
199
191
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
200
192
Type "help", "copyright", "credits"or"license"for more information.
201
193
>>>import maths
@@ -219,9 +211,8 @@ Now, when we call pi from the math module we got the result.
219
211
**Example 1: KeyError**
220
212
221
213
```py
222
-
Last login: Tue Dec 315:20:41 on ttys002
223
214
asabeneh@Asabeneh:~$ python
224
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
215
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
225
216
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
226
217
Type "help", "copyright", "credits"or"license"for more information.
As you can see, there was a typo in the key used to get the dictionary value. so, this is a key error and the fix is quite straight forward. Let's do this!
238
229
239
230
```py
240
-
Last login: Tue Dec 315:20:41 on ttys002
241
231
asabeneh@Asabeneh:~$ python
242
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
232
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
243
233
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
244
234
Type "help", "copyright", "credits"or"license"for more information.
245
235
>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
@@ -261,9 +251,8 @@ We debugged the error, our code ran and we got the value.
261
251
**Example 1: TypeError**
262
252
263
253
```py
264
-
Last login: Tue Dec 315:20:41 on ttys002
265
254
asabeneh@Asabeneh:~$ python
266
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
255
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
267
256
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
268
257
Type "help", "copyright", "credits"or"license"for more information.
269
258
>>>4+'3'
@@ -276,9 +265,8 @@ TypeError: unsupported operand type(s) for +: 'int' and 'str'
276
265
In the example above, a TypeErroris raised because we cannot add a number to a string. First solution would be to convert the string to intorfloat. Another solution would be converting the number to a string (the result then would be '43'). Let us follow the first fix.
277
266
278
267
```py
279
-
Last login: Tue Dec 315:20:41 on ttys002
280
268
asabeneh@Asabeneh:~$ python
281
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
269
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
282
270
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
283
271
Type "help", "copyright", "credits"or"license"for more information.
284
272
>>>4+'3'
@@ -299,9 +287,8 @@ Error removed and we got the result we expected.
299
287
**Example 1: TypeError**
300
288
301
289
```py
302
-
Last login: Tue Dec 315:20:41 on ttys002
303
290
asabeneh@Asabeneh:~$ python
304
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
291
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
305
292
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
306
293
Type "help", "copyright", "credits"or"license"for more information.
307
294
>>>from math import power
@@ -314,9 +301,8 @@ ImportError: cannot import name 'power' from 'math'
314
301
There is no function called power in the math module, it goes with a different name: _pow_. Let's correct it:
315
302
316
303
```py
317
-
Last login: Tue Dec 315:20:41 on ttys002
318
304
asabeneh@Asabeneh:~$ python
319
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
305
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
320
306
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
321
307
Type "help", "copyright", "credits"or"license"for more information.
322
308
>>>from math import power
@@ -332,9 +318,8 @@ ImportError: cannot import name 'power' from 'math'
332
318
### ValueError
333
319
334
320
```py
335
-
Last login: Tue Dec 315:20:41 on ttys002
336
321
asabeneh@Asabeneh:~$ python
337
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
322
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
338
323
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
339
324
Type "help", "copyright", "credits"or"license"for more information.
340
325
>>>int('12a')
@@ -349,9 +334,8 @@ In this case we cannot change the given string to a number, because of the 'a' l
349
334
### ZeroDivisionError
350
335
351
336
```py
352
-
Last login: Tue Dec 315:20:41 on ttys002
353
337
asabeneh@Asabeneh:~$ python
354
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
338
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
355
339
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
356
340
Type "help", "copyright", "credits"or"license"for more information.
0 commit comments