diff --git a/15_Day_Python_type_errors/15_python_type_errors.md b/15_Day_Python_type_errors/15_python_type_errors.md index 4009a2c0..16c3e713 100644 --- a/15_Day_Python_type_errors/15_python_type_errors.md +++ b/15_Day_Python_type_errors/15_python_type_errors.md @@ -9,7 +9,7 @@ Author: Asabeneh Yetayeh
- First Edition: Nov 22 - Dec 22, 2019 + Second Edition: July, 2021
@@ -35,9 +35,9 @@ ## Python Error Types -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. +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. -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. +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. ### SyntaxError @@ -45,7 +45,7 @@ Let's see the most common error types one by one. First let us open our python i ```py asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print 'hello world' @@ -56,12 +56,11 @@ SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello w >>> ``` -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. +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. ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print 'hello world' @@ -81,9 +80,8 @@ The error was a _SyntaxError_. After the fix our code was executed without a hit **Example 1: NameError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print(age) @@ -93,12 +91,11 @@ NameError: name 'age' is not defined >>> ``` -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. +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. ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print(age) @@ -118,9 +115,8 @@ The type of error was a _NameError_. We debugged the error by defining the varia **Example 1: IndexError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> numbers = [1, 2, 3, 4, 5] @@ -131,16 +127,15 @@ IndexError: list index out of range >>> ``` -In the example above, python raised an _IndexError_, because the list has only indexes from 0 to 4 , so it was out of range. +In the example above, Python raised an _IndexError_, because the list has only indexes from 0 to 4 , so it was out of range. ### ModuleNotFoundError **Example 1: ModuleNotFoundError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import maths @@ -153,9 +148,8 @@ ModuleNotFoundError: No module named 'maths' 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. ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import maths @@ -173,9 +167,8 @@ We fixed it, so let's use some of the functions from the math module. **Example 1: AttributeError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import maths @@ -193,9 +186,8 @@ AttributeError: module 'math' has no attribute 'PI' 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. ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import maths @@ -219,9 +211,8 @@ Now, when we call pi from the math module we got the result. **Example 1: KeyError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> users = {'name':'Asab', 'age':250, 'country':'Finland'} @@ -237,9 +228,8 @@ KeyError: 'county' 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! ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> user = {'name':'Asab', 'age':250, 'country':'Finland'} @@ -261,9 +251,8 @@ We debugged the error, our code ran and we got the value. **Example 1: TypeError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 4 + '3' @@ -276,9 +265,8 @@ TypeError: unsupported operand type(s) for +: 'int' and 'str' 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. ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 4 + '3' @@ -299,9 +287,8 @@ Error removed and we got the result we expected. **Example 1: TypeError** ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from math import power @@ -314,9 +301,8 @@ ImportError: cannot import name 'power' from 'math' There is no function called power in the math module, it goes with a different name: _pow_. Let's correct it: ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from math import power @@ -332,9 +318,8 @@ ImportError: cannot import name 'power' from 'math' ### ValueError ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> int('12a') @@ -349,9 +334,8 @@ In this case we cannot change the given string to a number, because of the 'a' l ### ZeroDivisionError ```py -Last login: Tue Dec 3 15:20:41 on ttys002 asabeneh@Asabeneh:~$ python -Python 3.7.5 (default, Nov 1 2019, 02:16:32) +Python 3.9.6 (default, Jun 28 2021, 15:26:21) [Clang 11.0.0 (clang-1100.0.33.8)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 1/0