Resource linked in this lesson: Invalid Syntax in Python: Common Reasons for SyntaxError
Appreciating Syntax Errors
00:00 When you start learning Python, the first kind of error you’ll run into is usually the SyntaxError, which naturally results from incorrect use of Python syntax.
00:09 The rules of writing the language. For general syntax errors, the syntax error is raised, but there are a couple subclasses for specific situations, the IndentationError and the TabError.
00:22 Let’s explore all three in the REPL.
00:27
For a fairly common example, let’s look at what happens if you define a list, but forget a comma. numbers = [
1, 2, 3 4]
, but missing a comma between three and four. What happens is, of course you get a SyntaxError:
invalid syntax
, and the very helpful message of Perhaps you forgot a comma?
which you did.
00:48
And how about if you try to do something else, like assigning a value to a reserved keyword class = 'economy'
. Again, you get a SyntaxError for invalid syntax because reserved keywords are not valid variable names, so that’s pretty straightforward.
01:05 What about IndentationError? This one can be a little tricky. Python has what’s known as semantic whitespace. The indentation level of each line is meaningful and not just a way to organize code, like in languages such as JavaScript.
01:19
So if the indentation level is inappropriate, that would be a kind of SyntaxError as well. Thus, the IndentationError. To illustrate, write a function hello_world()
that prints hello on one line and world on the next: def hello_world():
01:35 And see how by default the cursor is positioned four characters into the next line.
01:42
print("Hello")
. Now for the following line, try reducing it to two characters and print("World")
.
01:51
Complete the function definition. And what do you get? Indentationrror: unindent does not match any
outer indentation level
. This happened because you reduced the indentation from four spaces to two, which makes no sense in the context of the function.
02:05 This would be quickly fixed by adjusting the indent. All that leaves is TabError. Officially, Python recommends using spaces for all indentation in your code base, but for historic reasons, tabs are also supported.
02:18 It’s only when you mix tabs and spaces that the TabError will be raised. But raising it is actually a little tricky. Modern code editors will typically convert tabs to spaces and vice versa depending on the settings in your environment.
02:31 In fact, in Python 3.13, even the Python REPL converts tabs to spaces. First close this REPL session
02:40
and to see a TabError, you’ll need to use the old Python REPL by setting the environment variable PYTHON_BASIC
_REPL=1
when starting up the REPL.
02:51
And you can define another hello_world()
function: def
hello_world():
and counting out four spaces, print("Hello")
, 1, 2, 3, 4, print()
"Hello"
and using a tab print("World")
.
03:10
And as soon as you try to go to the next line, there it is. TabError: inconsistent use of tabs
and spaces in indentation
. The great thing about SyntaxErrors is they’re usually a pretty quick fix, and as you gain mastery of Python, it only gets easier.
03:26 This was just a couple of the ways that you can encounter them though. For a more thorough exploration into SyntaxErrors, check out Invalid Syntax in Python: Common Reasons for SyntaxError.
03:38 In the next lesson, you’ll look at the ways that names and imports can lead to exceptions.
Become a Member to join the conversation.