LambdaLabTM
Computer Science · Class 11 · Errors
ErrorsSyntax⏱️ 7 min read

Syntax Errors

A mistake in a program is called an error, or a bug. There are three kinds, and this lesson is about the loudest one: the error you make while writing, which Python catches before your program has run a single line.

1Syntax means the rules of writing

English has rules: a sentence starts with a capital letter and ends with a full stop. Python has rules too. Brackets are opened and closed, text is wrapped in matching quotes, a line starts in the right place. Those rules are called the syntax of the language.

Break one, and you have a syntax error. Python cannot even work out what you were asking for, so it does not try. It stops, points at the line, and waits for you to fix it.

Key Takeaway
A syntax error is a broken rule of the language. Python finds it before running the program — so the program does not run at all.

2What one looks like

Here is the commonest of them all. The bracket was opened and never closed:

missing_bracket.py
print('Hello'
Output
  File "missing_bracket.py", line 1
    print('Hello'
         ^
SyntaxError: '(' was never closed

Python tells you three useful things every time: the line number, a ^ mark under roughly where it got confused, and the words SyntaxError. Read the line it names first — the mistake is usually there, and sometimes on the line just above it.

Two more you will meet in the first week:

missing_quote.py
print('Hello)
Output
  File "missing_quote.py", line 1
    print('Hello)
          ^
SyntaxError: unterminated string literal (detected at line 1)
stray_space.py
marks = 90
  print(marks)
Output
  File "stray_space.py", line 2
    print(marks)
IndentationError: unexpected indent

That last one surprises everybody. In Python, the spaces at the start of a line are part of the rules, not decoration. One extra space and the file will not run. (IndentationError is just a syntax error with a more helpful name.)

3Nothing runs at all — not even the good lines

This is the part students find hardest to believe. Python reads your whole file before it runs any of it. If line 5 has a syntax error, lines 1 to 4 do not run either, even though there is nothing wrong with them.

Try it. The program below prints two perfectly good lines first, and the mistake is right at the bottom. Press Run and watch how much output you get:

nothing_runs.py

Not one word. No Starting the program, no Total is — only the error. Now add the missing ) at the end of the last line and run it again. All three lines appear at once.

4“But Python is an interpreted language!”

If you have been paying attention in class, that last result should bother you. You were told Python is an interpreted language — it takes your program a line at a time. So surely the good lines should run, and the program should only fall over when it reaches the bad one?

It is a fair objection, and the answer is that running a file happens in two steps, not one:

Step 1 · Check & translate
The whole file at once

Python reads every line and turns the file into instructions it can run. This step is all or nothing: one broken rule anywhere and there is nothing to hand on to step 2.

Step 2 · Run
Now one line at a time

Here Python is the line-at-a-time interpreter you were told about. This is the step that prints things — and the step where a run-time error can stop it halfway.

A syntax error is caught in step 1, so step 2 never begins and nothing prints. Compare it with a mistake that can only be found in step 2 — total spelt wrongly. Every line is legal Python, so the file translates fine, starts running, and dies at the bad line with the good lines already on screen:

runs_then_stops.py
print('Starting the program')
print('Everything is fine so far')

total = 10 + 20
print('Total is', totl)
Output
Starting the program
Everything is fine so far
Traceback (most recent call last):
  File "runs_then_stops.py", line 5, in <module>
    print('Total is', totl)
                      ^^^^
NameError: name 'totl' is not defined

Same file, same length, mistake on the same line — and this time the first two lines really did run. That is the difference between the two steps, and it is the whole difference between a syntax error and the run-time errors in the next lesson.

Interpreted does not mean unchecked
“Interpreted” means Python translates and runs your program as you launch it, instead of making a separate machine-code file first, the way C does. It does not mean Python starts running before it has read the file. It reads first, then runs.

And there is one place where your instinct is exactly right. At the interactive prompt, each line is its own little program: Python checks it, runs it, and asks for the next one. So a syntax error there costs you only that line — everything you typed before it has already happened:

Python prompt — interactive mode
>>> print('one')
one
>>> print('two'
File "<stdin>", line 1
print('two'
^
SyntaxError: '(' was never closed
>>> print('three')
three

one was printed and stayed printed. In script mode the same two lines give you nothing at all, because there the file is checked as a whole before anything runs.

Step 1 is also where your comments disappear
Reading the file is where Python throws away everything that was only for human eyes: comments, blank lines and spare spaces. None of them survive into step 2, which is why a comment can never slow a program down or change what it does. It is also why an apostrophe inside a comment is harmless — x = 1  # it's fine runs perfectly, because Python sees the # and skips the rest of the line without looking at it.
This makes syntax errors the easy kind
An error that stops the program before it starts is annoying, but it is honest. Python tells you the file, the line and the problem. The two kinds in the next lessons are far better at hiding.

5The usual suspects

Nearly every syntax error a beginner writes is one of these five. When Python says SyntaxError, walk down this list:

A bracket left open

print('Hi' — count your ( and ) . Every opened one needs a partner.

A quote left open

print('Hi) — text needs a quote at both ends, and both must be the same kind.

A space at the start

A line that begins with a stray space when it should not. Python counts spaces.

A misspelt keyword

Prnit, PRINT or Print — Python's words are lower case and spelt exactly.

Assigning to the wrong side

10 = marks does not work. The box goes on the left: marks = 10.

Two statements crammed together

print('a') print('b') on one line. One instruction per line.

6Your turn: fix these three

Three syntax errors are hiding below — one per line group. Run the program, read the message, fix the line it names, and run again. Python reports only the first error it meets, so you will fix them one at a time:

fix_me.py
One error at a time
Python stops at the first broken rule, so a file with three syntax errors shows you one message. Fix it, run again, meet the next. That is normal — it is not the computer being difficult.

7Recap

Key Takeaway
A syntax error means a rule of the Python language was broken — an unclosed bracket or quote, a stray space, a misspelt keyword. Python finds it before running the file, so nothing at all runs, and it shows you the line number and a ^ mark. Only the first one is reported at a time.
Quick Check

When does Python find a syntax error?

Quick Check

A program has 10 lines. Line 8 has a syntax error. What does the output show?

Quick Check

Which of these is a syntax error?