Skip to content

Commit b9d5e59

Browse files
committed
python type error
1 parent 0a9ffb9 commit b9d5e59

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

Diff for: 15_Day_Python_type_errors/15_python_type_errors.md

+23-39
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<sub>Author:
1111
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12-
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12+
<small> Second Edition: July, 2021</small>
1313
</sub>
1414
</div>
1515
</div>
@@ -35,17 +35,17 @@
3535

3636
## Python Error Types
3737

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.
3939

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.
4141

4242
### SyntaxError
4343

4444
**Example 1: SyntaxError**
4545

4646
```py
4747
asabeneh@Asabeneh:~$ python
48-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
48+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
4949
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
5050
Type "help", "copyright", "credits" or "license" for more information.
5151
>>> print 'hello world'
@@ -56,12 +56,11 @@ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello w
5656
>>>
5757
```
5858

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.
6060

6161
```py
62-
Last login: Tue Dec 3 15:20:41 on ttys002
6362
asabeneh@Asabeneh:~$ python
64-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
63+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
6564
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
6665
Type "help", "copyright", "credits" or "license" for more information.
6766
>>> print 'hello world'
@@ -81,9 +80,8 @@ The error was a _SyntaxError_. After the fix our code was executed without a hit
8180
**Example 1: NameError**
8281

8382
```py
84-
Last login: Tue Dec 3 15:20:41 on ttys002
8583
asabeneh@Asabeneh:~$ python
86-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
84+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
8785
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
8886
Type "help", "copyright", "credits" or "license" for more information.
8987
>>> print(age)
@@ -93,12 +91,11 @@ NameError: name 'age' is not defined
9391
>>>
9492
```
9593

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.
9795

9896
```py
99-
Last login: Tue Dec 3 15:20:41 on ttys002
10097
asabeneh@Asabeneh:~$ python
101-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
98+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
10299
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
103100
Type "help", "copyright", "credits" or "license" for more information.
104101
>>> print(age)
@@ -118,9 +115,8 @@ The type of error was a _NameError_. We debugged the error by defining the varia
118115
**Example 1: IndexError**
119116

120117
```py
121-
Last login: Tue Dec 3 15:20:41 on ttys002
122118
asabeneh@Asabeneh:~$ python
123-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
119+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
124120
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
125121
Type "help", "copyright", "credits" or "license" for more information.
126122
>>> numbers = [1, 2, 3, 4, 5]
@@ -131,16 +127,15 @@ IndexError: list index out of range
131127
>>>
132128
```
133129

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.
135131

136132
### ModuleNotFoundError
137133

138134
**Example 1: ModuleNotFoundError**
139135

140136
```py
141-
Last login: Tue Dec 3 15:20:41 on ttys002
142137
asabeneh@Asabeneh:~$ python
143-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
138+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
144139
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
145140
Type "help", "copyright", "credits" or "license" for more information.
146141
>>> import maths
@@ -153,9 +148,8 @@ ModuleNotFoundError: No module named 'maths'
153148
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.
154149

155150
```py
156-
Last login: Tue Dec 3 15:20:41 on ttys002
157151
asabeneh@Asabeneh:~$ python
158-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
152+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
159153
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
160154
Type "help", "copyright", "credits" or "license" for more information.
161155
>>> import maths
@@ -173,9 +167,8 @@ We fixed it, so let's use some of the functions from the math module.
173167
**Example 1: AttributeError**
174168

175169
```py
176-
Last login: Tue Dec 3 15:20:41 on ttys002
177170
asabeneh@Asabeneh:~$ python
178-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
171+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
179172
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
180173
Type "help", "copyright", "credits" or "license" for more information.
181174
>>> import maths
@@ -193,9 +186,8 @@ AttributeError: module 'math' has no attribute 'PI'
193186
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.
194187

195188
```py
196-
Last login: Tue Dec 3 15:20:41 on ttys002
197189
asabeneh@Asabeneh:~$ python
198-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
190+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
199191
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
200192
Type "help", "copyright", "credits" or "license" for more information.
201193
>>> import maths
@@ -219,9 +211,8 @@ Now, when we call pi from the math module we got the result.
219211
**Example 1: KeyError**
220212

221213
```py
222-
Last login: Tue Dec 3 15:20:41 on ttys002
223214
asabeneh@Asabeneh:~$ python
224-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
215+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
225216
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
226217
Type "help", "copyright", "credits" or "license" for more information.
227218
>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
@@ -237,9 +228,8 @@ KeyError: 'county'
237228
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!
238229

239230
```py
240-
Last login: Tue Dec 3 15:20:41 on ttys002
241231
asabeneh@Asabeneh:~$ python
242-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
232+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
243233
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
244234
Type "help", "copyright", "credits" or "license" for more information.
245235
>>> user = {'name':'Asab', 'age':250, 'country':'Finland'}
@@ -261,9 +251,8 @@ We debugged the error, our code ran and we got the value.
261251
**Example 1: TypeError**
262252

263253
```py
264-
Last login: Tue Dec 3 15:20:41 on ttys002
265254
asabeneh@Asabeneh:~$ python
266-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
255+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
267256
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
268257
Type "help", "copyright", "credits" or "license" for more information.
269258
>>> 4 + '3'
@@ -276,9 +265,8 @@ TypeError: unsupported operand type(s) for +: 'int' and 'str'
276265
In the example above, a TypeError is raised because we cannot add a number to a string. First solution would be to convert the string to int or float. Another solution would be converting the number to a string (the result then would be '43'). Let us follow the first fix.
277266

278267
```py
279-
Last login: Tue Dec 3 15:20:41 on ttys002
280268
asabeneh@Asabeneh:~$ python
281-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
269+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
282270
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
283271
Type "help", "copyright", "credits" or "license" for more information.
284272
>>> 4 + '3'
@@ -299,9 +287,8 @@ Error removed and we got the result we expected.
299287
**Example 1: TypeError**
300288

301289
```py
302-
Last login: Tue Dec 3 15:20:41 on ttys002
303290
asabeneh@Asabeneh:~$ python
304-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
291+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
305292
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
306293
Type "help", "copyright", "credits" or "license" for more information.
307294
>>> from math import power
@@ -314,9 +301,8 @@ ImportError: cannot import name 'power' from 'math'
314301
There is no function called power in the math module, it goes with a different name: _pow_. Let's correct it:
315302

316303
```py
317-
Last login: Tue Dec 3 15:20:41 on ttys002
318304
asabeneh@Asabeneh:~$ python
319-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
305+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
320306
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
321307
Type "help", "copyright", "credits" or "license" for more information.
322308
>>> from math import power
@@ -332,9 +318,8 @@ ImportError: cannot import name 'power' from 'math'
332318
### ValueError
333319

334320
```py
335-
Last login: Tue Dec 3 15:20:41 on ttys002
336321
asabeneh@Asabeneh:~$ python
337-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
322+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
338323
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
339324
Type "help", "copyright", "credits" or "license" for more information.
340325
>>> int('12a')
@@ -349,9 +334,8 @@ In this case we cannot change the given string to a number, because of the 'a' l
349334
### ZeroDivisionError
350335

351336
```py
352-
Last login: Tue Dec 3 15:20:41 on ttys002
353337
asabeneh@Asabeneh:~$ python
354-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
338+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
355339
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
356340
Type "help", "copyright", "credits" or "license" for more information.
357341
>>> 1/0

0 commit comments

Comments
 (0)