Search⌘ K

Syntax Errors and Exceptions

Understand the difference between syntax errors and runtime exceptions in Python. Learn to identify common causes, interpret error messages, and handle exceptions gracefully to prevent program crashes and provide user-friendly feedback.

We'll cover the following...

Syntax errors

If things go wrong during compilation, the following reasons could be why:

  • Something in the program is not written per the Python language guidelines.
Python 3.8
print 'Hello' # ( ) is missing
  • The error is reported by the interpreter/compiler.
Python 3.8
b = 2.5
d = 'Nagpur'
a = b + float(d) # d is a string, so it cannot be converted to float
  • A required action is needed to rectify the issue.
Python 3.8
import math
a = math.pow(3) # pow( ) needs two arguments

Other common syntax errors are:

  • Leaving out a symbol, such as a colon, comma, or brackets.
  • Misspelling a keyword.
  • Incorrect
...