Informatics Practices · Ch 3 — Brief Overview of Python
Syntax Errors
Syntax Errors
Every programming language, Python included, has rules that determine how a program must be written — which symbols go where, what a valid statement looks like. This set of rules is called the syntax of the language, and it plays the same role grammar plays in English: a sentence that breaks the grammar cannot be understood, and a statement that breaks the syntax cannot be interpreted.
The Python interpreter can interpret a statement only if it is syntactically correct. A simple illustration comes from parentheses, which must always occur in pairs:
- the expression
(10 + 12)is syntactically correct — the opening parenthesis has a matching closing one; - the expression
(7 + 11is not, because the right parenthesis is absent.
When any syntax error is present in a program, the interpreter shows one or more error messages and stops the execution right there. The program simply does not run. This is what makes syntax errors the most visible kind of error: they cannot hide, because the interpreter points them out immediately. …