Skip to content
absolute beginner

Common Python Errors for Beginners

Most Python bugs are not mysteries. They are messages: Python tells you what it expected, where it got confused, and which line to look at. Learn to read…

Published 2026-05-11Updated 2026-09-159 min read
A striking cobra with raised hood in its natural outdoor setting, highlighting its intricate scales and patterns.
A striking cobra with raised hood in its natural outdoor setting, highlighting its intricate scales and patterns. Photo by Anil Sharma on Pexels.

Most Python bugs are not mysteries. They are messages: Python tells you what it expected, where it got confused, and which line to look at. Learn to read those messages and you stop guessing and start fixing.

Why Python Errors Happen

Errors are not a sign that you are failing. They are the interpreter's way of saying, "I found something I cannot understand or execute." Every error message is a clue about what your code actually does versus what you meant it to do.

The fastest way to get comfortable with errors is to stop treating them as roadblocks and start treating them as feedback. Run the code, read the message, make one small change, and run it again. That loop—run, read, fix, rerun—is how every programmer learns.

Run each snippet below in the same environment you used for your first script, whether that is a saved file or the interactive interpreter. Seeing the traceback yourself is half the lesson.

How to Read a Python Error Message

When Python hits a problem, it prints a traceback. At first it looks like noise, but it is a structured report with three useful parts:

  • The error type: the name at the end, like SyntaxError, NameError, or TypeError. This tells you which category of mistake you made.
  • The line number: where Python noticed the problem. Start there.
  • The message: a short description, often with a caret (^) pointing at the exact spot.

Read the last line of the traceback first. That is where Python names the error and usually tells you what it expected. The lines above it show the chain of calls that led there.

Tip: When you see an error, do not rewrite the whole program. Read the message, fix the one thing it points to, and rerun. Most fixes are one line.

A Decision Rule Before You Fix Anything

A flowchart showing code being run, the traceback being read, a decision between syntax and runtime errors, a small fix being made, and the code being run again.
Use the same loop for every error: run the code, read the traceback, classify the problem, make one small fix, and rerun.

Before you touch the code, ask one question: could Python even parse your program? That single question splits nearly every beginner error into two camps.

  • If Python could not parse the code, you have a syntax problem—a missing colon, an unclosed quote, a stray bracket. The fix is structural.
  • If Python parsed the code but broke while running it, you have a runtime problem—a name that does not exist, a type that does not fit, an index past the end. The fix is about a value or a position.

From there, the same sequence applies to every error in this article: read the last traceback line, find the value or name it points to, decide what Python expected versus what your code supplied, then change one thing and rerun. Watch how that rule drives each example below.

Syntax Errors: The Rules of the Language

A SyntaxError means Python could not even parse your code—it does not follow the grammar of the language. This is the most common error while you are still learning, because it catches typos and missing punctuation.

Common causes:

  • Forgetting a colon (:) after if, for, while, or def
  • Missing or extra parentheses, brackets, or quotation marks
  • A stray comma or an unclosed string
if x == 5
    print("five")
  File "example.py", line 1
    if x == 5
             ^
SyntaxError: expected ':'

Python expected a colon at the end of the if line, and your code supplied nothing there. The caret points right at the missing colon. Add it and the error disappears.

Knowledge check

Check your understanding

Answer this question before you continue.

What change fixes this code so Python can parse it?
Debugging

Focus: Identify a missing colon as the cause of a SyntaxError and repair the structure.

if x == 5
    print("five")

Indentation Errors: Whitespace Is Part of the Syntax

Python uses indentation to mark blocks of code, where many other languages use curly braces. That means the spaces at the start of a line are part of the structure, not decoration. An IndentationError is really a syntax problem: Python could not parse your code because the block structure was unclear. It just reports that failure with a more specific name.

def greet():
print("hello")  # missing indentation
  File "example.py", line 2
    print("hello")
    ^
IndentationError: expected an indented block after function definition

Python expected the body of greet() to be indented, and your code supplied a line at the same level as the def. To avoid these errors:

  • Use four spaces for each level of indentation.
  • Never mix tabs and spaces in the same file.
  • Let your editor handle indentation automatically.

Consistent indentation does more than avoid errors—it makes the structure of your code visible at a glance.

Knowledge check

Check your understanding

Answer this question before you continue.

Which repair gives `greet` a valid function body?
Single Choice

Focus: Recognize that a function body must be indented and use consistent indentation.

def greet():
print("hello")

Name Errors: Using Something That Does Not Exist

A NameError means you used a name that Python has never seen. The most common cause is a typo, or using a variable before you have assigned it.

name = "Ada"
print(nmae)
Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(nmae)
          ^^^^
NameError: name 'nmae' is not defined

Python expected a name it had seen before, and your code supplied nmae, which was never assigned. Check the spelling and make sure the name is defined before you use it. Remember that Python is case-sensitive: Name and name are two different names.

Knowledge check

Check your understanding

Answer this question before you continue.

What is the smallest fix for this code?
Debugging

Focus: Fix a NameError by correcting a misspelled name or defining it before use.

name = "Ada"
print(nmae)

Type Errors: Mixing Incompatible Data

A TypeError happens when you try an operation on data of the wrong kind. The classic beginner case is adding a string and a number.

print("5" + 5)
Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print("5" + 5)
          ~~~~^~~
TypeError: can only concatenate str (not "int") to str

Here the + is doing string concatenation, and string concatenation cannot combine a string and an integer directly. Python expected both sides of this + to be strings, and your code supplied an integer on the right. The fix depends on what result you want:

  • If you want arithmetic, convert the string to a number: print(int("5") + 5) gives 10.
  • If you want text, convert the number to a string: print("5" + str(5)) gives "55".

When you hit a TypeError, decide what output the program should produce, then convert the values so they match that intent.

Knowledge check

Check your understanding

Answer this question before you continue.

Which corrected line prints `53` as text?
Output Prediction

Focus: Choose a conversion that makes operands compatible when the intended result is text.

The original operation is `5 + "3"`, and the goal is text rather than arithmetic.

ZeroDivisionError: Dividing by Zero

Dividing any number by zero is mathematically undefined, and Python refuses to guess. It raises a ZeroDivisionError.

result = 10 / 0
Traceback (most recent call last):
  File "example.py", line 1, in <module>
    result = 10 / 0
             ~~~^~~
ZeroDivisionError: division by zero

Python expected a number it could divide by, and your code supplied zero. If your program divides by a value that could be zero, check it first:

denominator = 0
if denominator != 0:
    result = 10 / denominator
else:
    print("Cannot divide by zero")

Index Errors: Stepping Past the End

An IndexError happens when you try to reach a position in a list or string that does not exist. Python lists start at index 0, so the last valid index is always one less than the length.

my_list = [1, 2, 3]
print(my_list[3])
Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(my_list[3])
          ~~~~~~~~^
IndexError: list index out of range

Python expected an index between 0 and 2, and your code supplied 3. The list has three items at indexes 0, 1, and 2. When you loop over a list, iterate over the list directly—for item in my_list:—and you avoid this error entirely.

A Quick Reference for Common Errors

Use this table as your decision aid when you meet an unfamiliar traceback. These are the most common Python errors beginners hit, and each row pairs the message with the smallest fix. For every row, ask the same question: what did Python expect, and what did your code supply?

ErrorWhat Python expectedTypical fix
SyntaxErrorCode that follows Python's grammarCheck colons, parentheses, quotes
IndentationErrorA clearly marked code blockUse consistent spaces, no tabs
NameErrorA name that was already definedCheck spelling and definition order
TypeErrorMatching data types for an operationConvert types so they match your intent
ZeroDivisionErrorA denominator that is not zeroGuard the denominator before dividing
IndexErrorAn index inside the list or stringRemember indexes start at 0

Practice: Repair and Verify Each Snippet

Now apply the decision rule instead of just reading about it. For each snippet below, do three things before you read the fix: decide whether Python could parse the code, name the error type you expect, and state what Python expected versus what your code supplied.

## 1
for i in range(3)
    print(i)
## 2
message = "hello"
print(mesage)
## 3
total = 5 + "3"
## 4
items = [10, 20, 30]
print(items[3])

Pause here. Write down your diagnosis for all four before reading on.

Here is the full repair-and-verify sequence. For each snippet, the corrected code runs and prints a visible result, so you can confirm the fix worked.

Snippet 1 is a SyntaxError—the for line is missing its colon, so Python cannot parse it. Add the colon:

for i in range(3):
    print(i)
0
1
2

Snippet 2 is a NameError—mesage is misspelled, so Python never saw that name. Fix the spelling:

message = "hello"
print(message)
hello

Snippet 3 is a TypeError—string concatenation cannot combine an integer and a string. Convert one side to match your intended result. If you want text, convert the number to a string:

total = 5 + str(3)
print(total)
53

Snippet 4 is an IndexError—index 3 is past the end of a three-item list. Use index 2, or loop over the list directly:

items = [10, 20, 30]
print(items[2])
30

Conclusion

Every programmer, no matter how experienced, reads error messages every day. The skill is not avoiding errors—it is reading them quickly and fixing them with one small change. Run the code, read the last line of the traceback, fix the one thing it names, and rerun.

Write a small program that deliberately triggers each error in the table above. Fix it, watch the message disappear, and confirm the corrected output prints what you expect. Repeat until you can predict the error before Python prints it. That practice is what turns error messages from frustration into a working tool.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

When reading a Python traceback, which part should a beginner read first?
Question 1 of 2Misconception Check

Focus: Use the final traceback line to identify the error category and its immediate message.

What does this corrected code print?
Question 2 of 2Output Prediction

Focus: Determine the valid zero-based index range for a list and avoid indexing past its end.

items = [10, 20, 30]
print(items[2])

References

  1. 8. Errors and Exceptions — Python 3.14.7 documentationdocs.python.org
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

Expansive desert landscape with golden sand dunes illuminated by sunrise, showcasing natural patterns and tranquility.
absolute beginner
6 min read

Your First Python Program

Your first program is not really about the words "Hello, World!" It is about proving the whole loop works: you write code, the computer runs it, and you…

Read tutorial
A woman engineer focuses on software analysis using a laptop indoors.
absolute beginner
8 min read

How to Install Python

Python is installed when your computer can do two things: find the python command, and run a tiny program with it. This guide walks you through that on…

Read tutorial