Fundamentals 7 min read

Common Python Errors and How to Fix Them

This article explains the most frequent Python runtime and syntax errors—including IndentationError, TabError, SyntaxError, NameError, IndexError, KeyError, TypeError, and AttributeError—provides clear examples of each mistake, shows the corresponding error messages, and offers step‑by‑step corrections to help beginners debug their code effectively.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Common Python Errors and How to Fix Them

For many beginners, bugs are the biggest fear when writing code; this guide presents a collection of common Python errors and how to resolve them.

1. IndentationError occurs when the code is not indented with the required four spaces per level as defined by PEP8. The following example demonstrates the error and its fix:

<code>a=1
b=2
if a<b:
print a</code>
<code>a=1
b=2
if a<b:
    print a</code>

2. TabError is caused by mixing tabs and spaces for indentation. It is recommended to use spaces only.

3. SyntaxError includes several sub‑cases:

Invalid syntax – missing punctuation, mixed Chinese/English symbols, misspelled keywords, etc.

Invalid character in identifier – presence of unrecognizable characters such as Chinese characters.

Incomplete string – mismatched quotation marks.

Examples:

<code>print( 'hello', 'world')</code>

Reason: the comma is a Chinese comma. Error message: SyntaxError: invalid character in identifier .

<code>result = (1024+(512*2)/128</code>

Reason: missing closing parenthesis. Error message: SyntaxError: unexpected EOF while parsing .

<code>if name == "A"
print("hello")</code>

Reason: missing colon after the if statement. Error message: SyntaxError: invalid syntax .

4. NameError occurs when a variable name is misspelled or not defined:

<code>message = "Hello!"
print(mesage)</code>

Reason: variable name typo. Error message: NameError: name 'mesage' is not defined .

5. IndexError happens when accessing a list element with an out‑of‑range index:

<code>a = [1,2,3]
print(a[3])</code>

Reason: list indices start at 0, so index 3 is out of range. Error message: IndexError: list index out of range .

6. KeyError is raised when trying to access a dictionary key that does not exist:

<code>d = {'a':1,'b':2}
print(d['f'])</code>

Reason: key 'f' is missing. Error message: KeyError: 'f' .

7. TypeError occurs when using incompatible types, such as concatenating a string with an integer without conversion:

<code>age = 18
print("My age is " + age)</code>

Reason: need to convert age to a string. Error message: TypeError: can only concatenate str (not "int") to str .

8. AttributeError is triggered when accessing an attribute that does not exist on an object:

Reason: the attribute is undefined or misspelled; check the class definition and the use of double underscores in __init__ .

In summary, encountering bugs does not mean you are a poor learner; treating each error as a small monster to defeat turns debugging into a learning adventure. Keep practicing and keep eliminating bugs!

debuggingerror handlingSyntaxErrorIndentationError
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.