Invalid Syntax
00:00 Invalid Syntax in Python.
00:04 When you run your Python code, the interpreter will first parse it, it is then converted into bytecode,
00:11 and this bytecode is then executed. Syntax errors occur at the parsing stage of this process, which means that, sadly, the rest of the process won’t happen and your program won’t get run.
00:23 If the interpreter finds invalid syntax at this parsing stage, it will attempt to show you where the error occurred. When you’re first learning Python, it can be frustrating to get syntax errors.
00:34 Python will attempt to help you determine where the invalid syntax is in your code, but the traceback it provides can be a little confusing. Sometimes the code it points to can be perfectly fine, as we’ll see in the next section.
00:47
Because syntax errors occur during the parsing stage of this process, you can’t handle invalid syntax in Python like you can other exceptions. Even if you try to wrap a try
and except
block around code with invalid syntax, you’ll still see the interpreter raise a SyntaxError
.
01:04 We’ll investigate this code a little more in the next section.
Become a Member to join the conversation.