Getting Started with Python · Class 11 · CBSE · Science Computer Science

Explain the difference between a syntax error and a runtime error in Python, and give one example of each.

Practice question from Getting Started with Python (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

medium · Subjective · Q-9E0268

Explain the difference between a syntax error and a runtime error in Python, and give one example of each.

Answer: A syntax error occurs when Python cannot understand the structure of a statement, such as in if x > 5 print(x), because the colon is missing. A runtime error occurs while a syntactically valid program is running, such as print(10 / 0), which raises a ZeroDivisionError.

Solution

A syntax error prevents Python from correctly parsing the program, whereas a runtime error occurs during execution after the program has been parsed successfully.

More from Getting Started with Python

All of Getting Started with Python

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed