Skip to content

Computer Science · Ch 5 — Getting Started with Python

Syntax Errors

5.13.1

Syntax Errors

Like every programming language, Python has its own rules that determine its syntax — the grammar its statements must follow. The interpreter will interpret a statement only if it is syntactically correct, that is, written exactly as the rules of Python require.

If any syntax error is present, the interpreter:

  • shows one or more error messages, and
  • stops execution right there — the program does not run at all.

Example: unbalanced parentheses

A simple rule of Python syntax is that parentheses must come in matching pairs.

>>> (10 + 12)     # syntactically correct
22

But leave off the closing parenthesis and the rule is violated:

>>> (7 + 11      # missing the right parenthesis - SYNTAX ERROR

The interpreter cannot make sense of the incomplete expression and reports a syntax error instead of a result. …