9 Python Errors That Signal You’re Still a Beginner.
2 min readDec 26, 2023
Python is popular, but even experts make mistakes. Here are 9 common Python errors, explained with examples, to help new coders
- Pythonic Idioms Ignored:
- Wrong: Looping with
for
to create a list.
squares = []
for i in range(5): squares.append(i**2)
- Right: Embrace Pythonic list comprehensions.
squares = [i**2 for i in range(5)]
2. Mutable Defaults Misused:
- Wrong: Default list in function arguments.
def add_item(item, list=[]): list.append(item)
- Right: Use
None
and initialize inside.
def add_item(item, list=None):
if list is None: list = []
list.append(item)
3. ‘is’ Operator Confusion:
- Wrong: Using ‘is’ for value equality.
if a_list is another_list: ...
- Right: Use ‘==’ for equality checks.
if a_list == another_list: ...
4. Indentation Errors:
- Wrong: Misaligned indentation.
def example():
print("Oops!")
- Right: Correctly indented code.
def example():
print("Correct!")