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.
2What one looks like
Here is the commonest of them all. The bracket was opened and never closed:
print('Hello' File "missing_bracket.py", line 1
print('Hello'
^
SyntaxError: '(' was never closedPython 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:
print('Hello) File "missing_quote.py", line 1
print('Hello)
^
SyntaxError: unterminated string literal (detected at line 1)marks = 90
print(marks) File "stray_space.py", line 2
print(marks)
IndentationError: unexpected indentThat 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:
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:
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.
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:
print('Starting the program')
print('Everything is fine so far')
total = 10 + 20
print('Total is', totl)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 definedSame 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.
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:
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.
x = 1 # it's fine runs perfectly, because Python sees the # and skips the rest of the line without looking at it.5The usual suspects
Nearly every syntax error a beginner writes is one of these five. When Python says SyntaxError, walk down this list:
print('Hi' — count your ( and ) . Every opened one needs a partner.
print('Hi) — text needs a quote at both ends, and both must be the same kind.
A line that begins with a stray space when it should not. Python counts spaces.
Prnit, PRINT or Print — Python's words are lower case and spelt exactly.
10 = marks does not work. The box goes on the left: marks = 10.
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:
7Recap
^ mark. Only the first one is reported at a time.When does Python find a syntax error?
A program has 10 lines. Line 8 has a syntax error. What does the output show?
Which of these is a syntax error?