1.10. Errors in Python#

When an error occurs in your code, Python provides a meaningful explanation that can help you rectify your code. This section introduces you to three common errors that you are likely to encounter at this stage. Additional error types will be introduced as needed.

1.10.1. NameError#

A NameError occurs when you refer to a variable that has not been declared.

3 + moogah
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 1
----> 1 3 + moogah

NameError: name 'moogah' is not defined

1.10.2. SyntaxError#

A SyntaxError occurs when you arrange the operators and operands in an illegal arrangement.

2 + 
  Cell In[2], line 1
    2 +
        ^
SyntaxError: invalid syntax
12 = x
  Cell In[3], line 1
    12 = x
    ^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

1.10.3. ZeroDivisionError#

A ZeroDivisionError occurs when you try to divide by zero.

3 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[4], line 1
----> 1 3 / 0

ZeroDivisionError: division by zero