In Python, you may encounter two main types of errors, syntax errors and exceptions. a syntax error occurs when you don't follow pythons syntax and maybe you forget to add a colon or the indentation is not proper. Let's try this in the Python interpreter and skip the colon following a for statement on purpose. So let's try for i in range of 10. Enter. So we got a syntax error as expected.
This is an example of an error in Python. Now let's talk about exceptions. Unlike syntax errors, exceptions are raised during the execution of the program interrupting the normal flow of the application. Let's see a couple of examples. First, let's use the same for loop to generate one of the many types of exceptions called the name error. I will misspell the name of the range function and wait for Python to notice my mistake.
So for i in colon, enter, and let's say print I, what do we have here? A name error. This means either that I misspelled a word in my code. In this case, the name of the function, or the variable or function I'm trying to use is not yet defined in my namespace. Don't worry, we will talk more about namespaces soon. Now, back to exceptions.
Another type of exception is raised when trying to divide a number by zero. So let's try this again. Let's say four divided by zero, and we get the zero division error exception. Fair enough, we cannot divide a number by zero. So Python raises a special exception for this particular case. Also, please notice that Python helps us localize the cause of the exception by also displaying the line of code that generated the exception and the number of the line as well.
There are many types of exceptions in Python. We won't analyze all of them now because you will most likely encounter most exceptions as you start creating and troubleshooting your own real programs. However, you can find the comprehensive list of Python three exceptions in one of the links I have attached to this lecture. Okay, this has been a general introduction to exceptions in Python. In the next lecture, we are going to see how to handle exceptions inside our programs.