17 Common Python Errors Every Beginner Should Know
This guide lists the 17 most frequent Python runtime and syntax errors that new programmers encounter, explains why each occurs, and provides clear code examples of both the faulty and corrected versions to help readers quickly diagnose and fix their own scripts.
For beginners learning Python, encountering errors is inevitable. The following collection of 17 typical mistakes, their error messages, and example code helps you identify and resolve these problems efficiently.
1. Missing colon after statements
Forgetting the colon (:) at the end of if , for , def , elif , else , class , etc., triggers a SyntaxError: invalid syntax .
<code>if spam == 42
print('Hello!')</code>2. Using = instead of ==
Assigning rather than comparing also raises SyntaxError: invalid syntax .
<code>if spam = 42:
print('Hello!')</code>3. Incorrect indentation
Wrong indentation leads to various IndentationError messages such as "unexpected indent", "unindent does not match any outer indentation level", or "expected an indented block".
<code>print('Hello!')
print('Howdy!')</code>or
<code>if spam == 42:
print('Hello!')
print('Howdy!')</code>4. Forgetting len() in a for loop
Using a list directly in range() causes TypeError: 'list' object cannot be interpreted as an integer .
<code>spam = ['cat', 'dog', 'mouse']
for i in range(spam):
print(spam[i])</code>5. Trying to modify a string
Strings are immutable; attempting item assignment raises TypeError: 'str' object does not support item assignment .
<code>spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)</code>Correct approach:
<code>spam = 'I have a pet cat.'
spam = spam[:13] + 'r' + spam[14:]
print(spam)</code>6. Concatenating non‑string values with strings
Adding an integer to a string raises TypeError: Can't convert 'int' object to str implicitly .
<code>numEggs = 12
print('I have ' + numEggs + ' eggs.')</code>Fix by converting the integer:
<code>numEggs = 12
print('I have ' + str(numEggs) + ' eggs.')
print('I have %s eggs.' % numEggs)</code>7. Missing quotes around string literals
Omitting quotes results in SyntaxError: EOL while scanning string literal .
<code>print(Hello!')
print('Hello!)
myName = 'Al'
print('My name is ' + myName + . How are you?')</code>8. Misspelled variable or function name
This produces NameError: name 'fooba' is not defined .
<code>foobar = 'Al'
print('My name is ' + fooba)
spam = ruond(4.2)
spam = Round(4.2)</code>9. Misspelled method name
Calling a non‑existent method, such as lowerr , raises AttributeError: 'str' object has no attribute 'lowerr' .
<code>spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()</code>10. Index out of range
Accessing a list element beyond its length triggers IndexError: list index out of range .
<code>spam = ['cat', 'dog', 'mouse']
print(spam[6])</code>11. Using a non‑existent dictionary key
Attempting to access a missing key raises KeyError: 'spam' .
<code>spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
print('The name of my pet zebra is ' + spam['zebra'])</code>12. Using a Python keyword as a variable name
This results in SyntaxError: invalid syntax .
<code>class = 'algebra'</code>13. Using an increment operator on an uninitialized variable
Doing spam += 1 without a proper initial value raises NameError: name 'foobar' is not defined .
<code>spam = 0
spam += 42
eggs += 42</code>14. Referencing a local variable before assignment
If a variable is assigned later in the function, using it earlier causes UnboundLocalError: local variable 'foobar' referenced before assignment .
<code>someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()</code>15. Trying to assign to a range object
Since range() returns a range object, the following raises TypeError: 'range' object does not support item assignment .
<code>spam = range(10)
spam[4] = -1</code>Correct way:
<code>spam = list(range(10))
spam[4] = -1</code>16. Using ++ or -- operators
Python does not support these increment/decrement operators, leading to SyntaxError: invalid syntax .
<code>spam = 1
spam++</code>Correct form:
<code>spam = 1
spam += 1</code>17. Forgetting the self parameter in a method
Defining a class method without self causes TypeError: myMethod() takes no arguments (1 given) .
<code>class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()</code>Include self as the first parameter to fix the error.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.