Fundamentals

7 Common Python Beginner Mistakes and How to Fix Them

The most common beginner problems are indentation, confusing `=` with `==`, mixing strings and numbers, off-by-one ranges, missing keys, mutable defaults and swallowing exceptions. Fix the smallest reproducible case first.

Step by step

  1. Read the final traceback line and identify the exception type.
  2. Check the exact line and value types.
  3. Reduce the problem to a few lines.
  4. Add a small test so the same bug cannot return.

Run this example

Use Python 3. Copy the code into a file or interactive Python environment.

raw_age = "24"
age = int(raw_age)
if age == 24:
    print("Age matched")
print(list(range(3)))

Expected output

Age matched
[0, 1, 2]

Common issue

Do not replace every error with `except: pass`; that hides bugs and makes troubleshooting harder.

Try it yourself

Try adding a string and integer, read the traceback, and fix it with an explicit conversion.

Read the traceback from the final line upward

The last line names the exception and explains what failed. Above it, locate the file and line in your code that supplied the bad value. A NameError often means a misspelled or undefined name; TypeError means an operation received an unsuitable type; ValueError means the type may be acceptable but its value is not.

Handle an expected conversion failure

Catch the specific error you expect. Unexpected errors should remain visible so you can investigate them. This prints a useful message and then demonstrates a valid conversion.

def parse_count(text):
    count = int(text)
    if count < 0:
        raise ValueError("count cannot be negative")
    return count

try:
    parse_count("three")
except ValueError:
    print("Enter a whole number such as 3.")
print(parse_count("3"))

Expected output

Enter a whole number such as 3.
3

Reduce the problem before fixing it

Use a small input that reproduces the failure. Print repr(value) and type(value).__name__ when hidden spaces or unexpected types matter. Change one assumption at a time, then turn the failing case into a test. Catching every Exception and continuing can hide a broken program.

Exercise

Test "0", "12", "-1", and "hello". The first two should return 0 and 12; the last two should raise ValueError. Write a message that distinguishes an invalid count from an unexpected programming error.

Indentation and environment checks

Use four spaces consistently. If an import fails after installing a package, verify that the installer and the script use the same interpreter with python -m pip. Name your file something other than a module you import, such as csv.py, to avoid shadowing that module.

Keep learning

Read the relevant documentation · Free foundational resources

Related guides: Python Functions Explained Through Practical Projects and How to Build a Python Portfolio With Real Projects.

Interested in applying these skills to a product? Explore the optional Python to Profit course. It focuses on product and business building, not replacing foundational Python study.