Guide Python Beginner

4.17 Common Syntax Errors

Five error types beginners hit most often — IndentationError, SyntaxError, NameError, missing colons, and unmatched brackets — with what each one actually means.

2 min read

Five error types that account for the vast majority of syntax mistakes, especially for beginners — knowing what each one means makes them far faster to fix.

IndentationError

What Does It Mean?

A block is expected but the indentation is missing or inconsistent.

>>> if True:
... print("bad")
IndentationError: expected an indented block after 'if' statement on line 1

SyntaxError

What Does It Mean?

The general-purpose error for code that doesn’t match Python’s grammar at all — often a missing colon, unmatched bracket, or invalid character sequence.

>>> if True
  File "<stdin>", line 1
    if True
           ^
SyntaxError: expected ':'

NameError

What Does It Mean?

Technically a runtime error, not a syntax error — the code IS valid Python, but references a name that was never defined (often a typo).

>>> print(undefined_var)
Traceback (most recent call last):
NameError: name 'undefined_var' is not defined

Missing Colon

Why Does It Happen?

The single most common syntax mistake for beginners coming from other languages — every compound statement header (if/for/while/def/class) must end in :.

>>> def greet(name)
  File "<stdin>", line 1
    def greet(name)
                   ^
SyntaxError: expected ':'

Unmatched Brackets

What Does It Mean?

An opening (, [, or { without its matching close — Python reports exactly which bracket was never closed.

>>> x = [1, 2, 3
  File "<stdin>", line 1
    x = [1, 2, 3
        ^
SyntaxError: '[' was never closed

Quick Interview Answer

IndentationError means a block’s indentation is missing or inconsistent. SyntaxError is the general grammar violation — most often a missing colon or an unmatched bracket. NameError is different from both: it’s a runtime error, meaning the code is syntactically valid but references a name that was never defined.”

Common Mistakes

  • Confusing NameError (valid syntax, undefined name, caught only at runtime) with SyntaxError (invalid grammar, caught before anything runs).
  • Not reading the caret (^) in the traceback — it points to exactly where the parser gave up, which is usually the fastest way to find the actual problem.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form