7 Common Python Mistakes Beginners Make (and How to Fix Them)

Python is known for being beginner-friendly, but new programmers still hit familiar pitfalls. This guide covers the most common mistakes and how to avoid them—so you can write cleaner, more Pythonic code.

1. Mutable Default Arguments

Using a mutable object (like a list or dict) as a default argument is a classic trap. The same object is reused across all function calls, so modifications persist.

# Wrong - dangerous!
def add_item(item, my_list=[]):
    my_list.append(item)
    return my_list

add_item(1)  # [1]
add_item(2)  # [1, 2] - unexpected!

# Right
def add_item(item, my_list=None):
    if my_list is None:
        my_list = []
    my_list.append(item)
    return my_list

2. Modifying a List While Iterating

Removing items from a list while looping over it can skip elements or cause errors. Iterate over a copy instead.

# Wrong
for item in my_list:
    if item == "remove":
        my_list.remove(item)  # Skips next element!

# Right
for item in my_list[:]:  # Iterate over a copy
    if item == "remove":
        my_list.remove(item)

# Or use list comprehension
my_list = [x for x in my_list if x != "remove"]

3. Confusing == with is

== compares values; is compares identity (whether two variables point to the same object). Use is only for None, True, and False.

x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)  # True - same values
print(x is y)  # False - different objects

if value is None:  # Correct for None
    pass

4. Forgetting the Colon

Python uses colons to start blocks. Forgetting the colon after if, for, def, or class causes a syntax error. It's an easy typo to miss.

# Wrong
if x > 5
    print("big")

# Right
if x > 5:
    print("big")

5. Incorrect Indentation

Python uses indentation to define blocks. Mixing tabs and spaces, or using wrong indent levels, causes IndentationError. Use 4 spaces consistently (PEP 8 standard).

6. Not Using with for File Handling

Always use with when opening files. It automatically closes the file when done, even if an error occurs.

# Wrong
f = open("file.txt")
data = f.read()
f.close()  # What if read() raises an error?

# Right
with open("file.txt") as f:
    data = f.read()
# File closed automatically

7. Ignoring Exception Types

Catching a broad Exception can hide bugs. Catch specific exceptions when possible, and let others propagate so you can fix the root cause.

# Too broad
try:
    risky_operation()
except Exception:
    pass  # Hides all errors!

# Better
try:
    risky_operation()
except FileNotFoundError:
    handle_missing_file()
except ValueError as e:
    print(f"Invalid value: {e}")

Ready to practice? Check out our Python course for more tutorials and hands-on examples.