Hi, and welcome to this section. In this section of the course, we are going to take a closer look at errors and exceptions in Python and how to handle and fix them inside your code. The first thing I should say to you here is that everyone makes mistakes and errors inside their code. No matter how experienced developer one may be errors and exceptions are always going to occur while you build your scripts and applications. So don't fear them. Don't panic if your code has generated an error.
Instead, try to learn as much as possible from your mistakes, and focus on troubleshooting and fixing your code accordingly. Now in Python, there are two types of errors that your code might generate syntax errors and exceptions. And we're going to take a closer look at both of these types of errors and see how to properly read, interpret and fix them. First of all, please keep in mind that any error that's not a syntax error is an exception in Python. Exceptions are also called runtime errors, because they are generated at some point during the execution of your Python code. What is the syntax errors are as the name implies just small errors that are related to the way you wrote your code.
In short, this refers to the syntax you miss used inside your Python script. So in this video, let's focus on syntax errors. And let's leave exceptions for the next lecture. I have a simple Python script right here containing a couple of syntax errors. Please keep in mind that Python evaluates each line of code in order from top to bottom, validating the syntax of each line and throwing an error whenever the syntax is incorrect. Let's run this script right now in the windows command line and see the results.
So by Thawne, d syntax.pi, enter. Okay, so as you might have already guessed, Python throws a syntax error. Let's read the error together on the first line right here, Python. provides us with the path and name of the file that generated the error. In my case, that is the syntax dot p y. Furthermore, we also get the line in the file where the syntax error has occurred.
So we have line three in this case, if we check our code on line three, we have the print my var piece of code, which of course is incorrect in Python three, because print is a function, and we skipped the parentheses that should have enclosed my var on the next line in the error text. This one right here, we have the exact same line being claimed by Python and right underneath it, we have this small up arrow pointing to the location where the syntax error has occurred. The so called token signals either the exact location of the error if the error has been generated by a single character, or the lack of a single character, or as we can see here, where we like to characters in order for the syntax to be correct, a pair of parentheses. In this case, the up arrow points to the end of the error generating set of characters, meaning to the location where the closing parenthesis should have been.
Now let's move further and read the text of the error itself. It says syntax error. So we know that this is indeed an error related to the syntax we used in the code. Next, the error message provides more details, and also provides a potential solution for fixing the syntax. So we have missing parentheses in call to print. Did you mean print of my var?
And indeed, in order to fix this issue, we should add the parentheses surrounding the name of the variable my var. So let's go ahead and do this inside our script. Okay, I'm going to save the script. And now let's run our program once again. Okay, so it seems we have yet another syntax error in the same file, but this time on line five, when building the for loop, this time, the up arrow, as you can see right here is pointing to the exact location of the syntax error, because as you might have already noticed, we missed the question. After the fourth statement on the last line of the error, Python tells us that this is indeed a syntax error and that our syntax is invalid.
So let's go straight to line five inside our script and fix this right away. So I'm going to add the colon right here. And now I'm going to save the file. Let's run it once again to check the results. And we do have yet another error in the code on line 10. Again, being a syntax error, this time, obviously, I use the square bracket instead of a closing parentheses for the float function.
This type of misspelling might happen when you're in a hurry to write a piece of code, and you mistakenly type in a character that doesn't belong in that location. So let's fix the code once again and run it for the last time. I'm going to replace the square bracket with the closing parentheses, save the file. And now let's run the file. Great. This time we didn't get any more syntax errors.
So at this point, we can say that our code is correctly written Again, don't panic whenever you get a syntax error when running your code. Instead, just focus on carefully reading the error message identifying the location where the error has occurred and fixing it. As I said, Everyone makes mistakes. So don't blame yourself for mistyping some characters in your code. The key thing is to troubleshoot and learn from your mistakes. I'll see you in the next lecture.