Every Python beginner makes the same twelve mistakes. That is good news: it means the errors are predictable, the fixes are short, and once you have seen each one explained with the wrong code next to the right code, you stop making it.
This article is not a Python course. It assumes you have written a few small programs and you keep hitting the same walls: a SyntaxError you cannot see, a list that loses items for no reason, a function that returns None when it should return a number. For each mistake you get four things: the wrong code, the exact error Python gives you, why it happens, and the fixed code. At the end there is a 15-question self-test — do it honestly, check your score, and re-read the mistakes you missed.
If you have not started Python at all yet, begin with our Python for data science for beginners guide, which curates the free courses and a practice plan. If you are also learning databases, the same careful-reading habit helps with our 60 SQL interview questions and answers for beginners.
Photo by Nemuel Sereti on Pexels
How to use this article
Read each mistake in order — they are roughly in the order you will meet them. After every fixed example, type the wrong version into your own editor first, see the error yourself, then type the fix. Reading about an error is worth a tenth of triggering it once and fixing it. The self-test at the end is the check: if you score 12 or more out of 15, the lesson has stuck.
One habit underlies all twelve fixes: read the error message. Python's errors are unusually helpful. SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? is not a scolding — it is the answer.
Mistake 1: = instead of == in a condition
This is the single most common beginner error in every programming language, and Python's error message now tells you exactly what happened.
Wrong code:
score = 85
if score = 85:
print("You passed!")
What Python says:
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
Why it fails: = is assignment — it stores a value. == is comparison — it asks a question. An if needs a question, so = is not allowed there. (Python 3.8 added the walrus operator := for assignment inside an expression, but that is not what you want here.)
Fixed code:
score = 85
if score == 85:
print("You passed!")
The rule: = gives a value a name. == asks whether two values are equal. In every if and while, you almost always want ==.
Mistake 2: The missing colon and the wrong indent
Python uses indentation instead of curly brackets, so two tiny slips — a forgotten colon, or one line indented differently from its neighbours — stop the whole program.
Wrong code:
if score >= 50
print("Pass")
What Python says:
SyntaxError: expected ':'
And the indent version:
if score >= 50:
print("Pass")
IndentationError: expected an indented block after 'if' statement
Why it fails: the colon tells Python "a block starts here", and the indent tells it which lines belong to that block. Without either one, Python cannot tell where the block is.
Fixed code:
if score >= 50:
print("Pass")
The rule: every if, for, while, def and else ends with a colon, and every line inside the block is indented by exactly the same amount — four spaces is the standard. Most editors can be set to insert four spaces when you press Tab; do that now and this mistake mostly disappears.
Mistake 3: The off-by-one error with range()
range() starts at 0 and stops before the number you give it. That pair of facts causes more wrong loops than anything else in beginner Python.
Wrong code — trying to print 1 to 5:
for i in range(5):
print(i)
Output:
0
1
2
3
4
No error, no crash — just the wrong numbers. Silent wrong answers are worse than crashes, because nothing tells you to look.
Fixed code:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
The rule: range(n) gives you 0 to n - 1 — that is n values, starting at zero. range(start, stop) gives you start up to stop - 1. When you need both the item and its position, do not fight range(len(...)) — use enumerate():
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)
0 apple
1 banana
2 cherry
Mistake 4: Changing a list while looping over it
This one is a trap because it sometimes looks like it works. Remove items from a list while you loop over it, and the loop quietly skips items.
Wrong code:
numbers = [1, 2, 4, 6, 3]
for n in numbers:
if n % 2 == 0:
numbers.remove(n)
print(numbers)
Output:
[1, 4, 3]
The 2 and the 6 were removed, but the 4 survived. Why? When the loop removes the 2, every item after it shifts one place left — but the loop still moves forward one step, so it lands on the 6 and never looks at the 4. With four 2s in a row it is even worse: [2, 2, 2, 2] comes out as [2, 2], half the items still there.
Fixed code — build a new list instead:
numbers = [1, 2, 4, 6, 3]
numbers = [n for n in numbers if n % 2 != 0]
print(numbers)
[1, 3]
The rule: never add to or remove from a list while you are looping over that same list. Loop over a copy (for n in numbers[:]:), or — usually better — build a new list with a list comprehension, as above.
Mistake 5: The mutable default argument
This is the mistake that makes beginners think Python is broken. A default list in a function definition is created once, when the function is defined — not fresh on every call.
Wrong code:
def add_item(item, cart=[]):
cart.append(item)
return cart
print(add_item("apple"))
print(add_item("banana"))
Output:
['apple', 'banana']
['apple', 'banana']
The second call was supposed to return ['banana']. Instead it returned the same list, still holding "apple" from the first call. Both calls share one list, because the default [] was created once and every call reuses it. The Python documentation warns about exactly this: "The default value is evaluated only once."
Fixed code:
def add_item(item, cart=None):
if cart is None:
cart = []
cart.append(item)
return cart
print(add_item("apple"))
print(add_item("banana"))
['apple']
['banana']
The rule: never use a list, dictionary or set as a default argument. Use None as the default and create the fresh empty container inside the function. This applies to def f(x=[]), def f(x={}) and def f(x=set()) alike.
Mistake 6: is versus ==
== asks "do these two things have the same value?" is asks "are these two things the exact same object in memory?" Beginners use is where they mean ==, and it works just often enough to be dangerous.
Wrong code:
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True — same values
print(a is b) # False — two different list objects
It gets worse with numbers. Small integers are cached by Python, so 256 is 256 is True — but that is an implementation detail, not a promise:
a = 1000
b = int("1000")
print(a == b) # True
print(a is b) # False
Why it fails: two lists can hold identical values while being two separate objects. is checks identity, not equality, and for numbers and strings the answer depends on internal caching you cannot rely on.
Fixed code:
if a == b:
print("Equal values")
if a is None:
print("Nothing here")
The rule: use == for comparing values — always, for numbers, strings and lists. The one place is is correct is comparing against None: if x is None. That is the convention the whole Python world uses.
Mistake 7: / versus // — division that quietly rounds
In Python 3, / always gives a float, and // always rounds down to a whole number (floor division). Mix them up and your maths is silently wrong.
Wrong code — working out an average:
total = 61
students = 2
print(total / students) # 30.5
print(total // students) # 30
Both run without an error. If you wanted the exact average, // has thrown away the .5. And with negative numbers the rounding surprises people: -7 // 2 is -4, not -3, because floor division rounds down, towards negative infinity.
Fixed code:
total = 61
students = 2
average = total / students
print(average) # 30.5
per_student = total // students
print(per_student) # 30 — only when you genuinely want whole items
The rule: / for real division, // only when you deliberately want to drop the remainder — for example, splitting items into whole groups. If a calculation "looks about right but not exact", check which slash you used.
Mistake 8: input() always returns a string
input() reads what the user types — and it always returns text, even when the user types digits. Add 1 to it and Python complains.
Wrong code:
age = input("How old are you? ") # user types 19
print(age + 1)
What Python says:
TypeError: can only concatenate str (not "int") to str
Why it fails: age is the string "19", not the number 19. Python will not guess that you meant a number — adding a string and an integer is a type error.
Fixed code:
age = int(input("How old are you? "))
print(age + 1) # 20
The rule: convert immediately, at the point you read the input: int(input(...)) for whole numbers, float(input(...)) for decimals. If the user might type something that is not a number, wrap the conversion in a try block (see Mistake 12) instead of letting the program crash.
Mistake 9: Forgetting return — the function that returns None
A function without a return statement does not return "nothing" in the sense of an empty result. It returns the special value None. Use that result in a calculation and the error appears somewhere else entirely, which makes it hard to trace.
Wrong code:
def double(n):
n * 2
result = double(5)
print(result) # None
print(result + 10) # TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Why it fails: n * 2 computes 10 and then throws it away, because nothing told the function to hand the value back. The TypeError shows up on the next line, far from the real mistake.
Fixed code:
def double(n):
return n * 2
result = double(5)
print(result) # 10
print(result + 10) # 20
The rule: if a function is supposed to give you a value, it needs return. print() inside a function is not a return — it shows a value on screen and still hands back None. When a variable mysteriously becomes None, check the function that produced it first.
Mistake 10: list.append() returns None
Same family as Mistake 9, but sneakier, because it looks like an assignment that should work. Methods that change a list in place — append(), sort(), reverse() — return None. The list is changed; the return value is nothing.
Wrong code:
scores = [10, 20]
scores = scores.append(30)
print(scores) # None — you just threw the list away
Fixed code:
scores = [10, 20]
scores.append(30)
print(scores) # [10, 20, 30]
The rule: call append(), sort() and reverse() on their own line, and never assign their result. If you want a new sorted list without changing the original, use the built-in function sorted(scores), which does return a value.
Mistake 11: Changing a global variable inside a function
Reading a global variable inside a function works fine. Assigning to it does not — Python then treats the name as a local variable for the whole function, and reading it before the assignment crashes.
Wrong code:
count = 0
def add_one():
count = count + 1
return count
add_one()
What Python says:
UnboundLocalError: cannot access local variable 'count' where it is not associated with a value
Why it fails: the assignment count = ... inside the function makes Python treat count as local to that function. So count + 1 on the right-hand side tries to read a local variable that does not exist yet — even though a global count is sitting right there.
Fixed code — pass the value in and return the new one (best for beginners):
count = 0
def add_one(count):
return count + 1
count = add_one(count)
print(count) # 1
If you genuinely need to change a global, the global keyword exists — global count as the first line of the function — but needing it is usually a sign the function should take an argument and return a value instead.
The rule: functions should take what they need as arguments and hand back what they compute with return. That one habit removes this whole class of error.
Mistake 12: The bare except that hides the real error
When a program crashes, the temptation is to wrap everything in a bare except: so it "never crashes". That is like turning off a fire alarm because the noise is annoying: the fire is still there, and now you cannot find it.
Wrong code:
try:
age = int("abc")
except:
print("Something went wrong")
It prints "Something went wrong" and carries on — but what went wrong? A typo in the input? A missing file? A bug three lines up? You will never know, because the error message was thrown away. A bare except: also catches KeyboardInterrupt and SystemExit, so even Ctrl+C can stop working.
Fixed code — catch the specific error, and say what it was:
try:
age = int("abc")
except ValueError as e:
print("That was not a number:", e)
That was not a number: invalid literal for int() with base 10: 'abc'
The rule: catch the specific exception you expect — ValueError for bad input, FileNotFoundError for a missing file, ZeroDivisionError for division by zero. The Python documentation on errors and exceptions lists the built-ins. If you truly do not know what might go wrong, except Exception as e: at least keeps the message — but a bare except: should never appear in your code.
Photo by Syirwan Ainu on Pexels
The 12 mistakes at a glance
| # | Mistake | The error you see | The fix |
|---|---|---|---|
| 1 | = in a condition |
SyntaxError |
Use == to compare |
| 2 | Missing colon / bad indent | SyntaxError / IndentationError |
Colon + 4 spaces |
| 3 | Off-by-one with range() |
Wrong numbers, no error | Remember range(n) stops at n - 1 |
| 4 | Changing a list while looping | Items silently skipped | Build a new list |
| 5 | Mutable default argument | Old values reappear | Default None, create inside |
| 6 | is instead of == |
Sometimes works, sometimes not | == for values, is only for None |
| 7 | / instead of // |
Silently rounded maths | / for real division |
| 8 | input() returns a string |
TypeError |
Convert with int() / float() |
| 9 | Forgetting return |
Function returns None |
Add return |
| 10 | Assigning append()'s result |
List becomes None |
Call append() on its own line |
| 11 | Assigning to a global in a function | UnboundLocalError |
Pass arguments, return values |
| 12 | Bare except: |
Real error hidden | Catch the specific exception |
15-question self-test
Answer each question before looking at the key. Every question is about one of the mistakes above — if you get one wrong, the number in brackets tells you which mistake to re-read.
1. What does this print?
for i in range(3):
print(i)
A. 1, 2, 3
B. 0, 1, 2
C. 0, 1, 2, 3
D. 3
2. What is wrong with this code?
if age = 18:
print("Adult")
A. age is not defined
B. = should be ==
C. The colon is missing
D. Nothing is wrong
3. What does this print?
def add(x, items=[]):
items.append(x)
return items
print(add(1))
print(add(2))
A. [1] then [2]
B. [1] then [1, 2]
C. [1, 2] then [1, 2]
D. An error
4. Which comparison is correct for checking whether two lists hold the same values?
A. a is b
B. a == b
C. a = b
D. a equals b
5. What does 7 // 2 give in Python?
A. 3.5
B. 3
C. 4
D. 2
6. What does this print?
def greet(name):
print("Hello " + name)
result = greet("Sara")
print(result)
A. Hello Sara then Hello Sara
B. Hello Sara then None
C. None then Hello Sara
D. An error
7. What is the type of the value returned by input()?
A. int if the user types digits
B. Always str
C. float if the user types a decimal
D. It depends on the question
8. What does this print?
numbers = [2, 2, 2, 2]
for n in numbers:
numbers.remove(n)
print(numbers)
A. []
B. [2]
C. [2, 2]
D. An error
9. What is wrong with this code?
scores = [5, 8]
scores = scores.append(10)
print(scores)
A. append does not exist for lists
B. append returns None, so scores becomes None
C. You cannot append to a list with two items
D. Nothing is wrong
10. What does this code do?
count = 0
def increase():
count = count + 1
increase()
A. Sets count to 1
B. Raises UnboundLocalError
C. Prints 1
D. Does nothing, silently
11. Which except clause is the best practice?
A. except:
B. except Exception:
C. except ValueError as e:
D. except Error:
12. What does -7 // 2 give?
A. -3
B. -3.5
C. -4
D. 3
13. How do you print the numbers 1 to 5 inclusive?
A. range(5)
B. range(1, 5)
C. range(1, 6)
D. range(0, 5)
14. What does this print?
x = [1, 2]
y = [1, 2]
print(x == y, x is y)
A. True True
B. True False
C. False True
D. False False
15. A user types 25 at this prompt. What happens?
age = input("Age: ")
print(age + 5)
A. It prints 30
B. It prints 255
C. It raises TypeError
D. It prints 25 + 5
Answers
- B —
range(3)gives 0, 1, 2. (Mistake 3) - B —
=assigns; a condition needs==. (Mistake 1) - B — the first call returns
[1]; the second call reuses the same default list and returns[1, 2]. (Mistake 5) - B —
==compares values;iscompares identity. (Mistake 6) - B —
//is floor division: 7 // 2 = 3. (Mistake 7) - B —
greetprintsHello Sarabut returnsNone, and thatNoneis what gets printed second. (Mistake 9) - B —
input()always returns a string. (Mistake 8) - C — removing while looping skips every other item, leaving
[2, 2]. (Mistake 4) - B —
append()changes the list in place and returnsNone. (Mistake 10) - B — assigning to
countinside the function makes it local, so reading it raisesUnboundLocalError. (Mistake 11) - C — catch the specific exception you expect. A bare
except:(A) hides the cause. (Mistake 12) - C — floor division rounds down, so -7 // 2 is -4. (Mistake 7)
- C —
range(1, 6)gives 1, 2, 3, 4, 5. (Mistake 3) - B — equal values (
==isTrue) but two different list objects (isisFalse). (Mistake 6) - C —
ageis the string"25", and"25" + 5raisesTypeError. Convert first:int(input("Age: ")). (Mistake 8)
How did you do? 13–15: you have these mistakes beaten — move on to writing real programs. 9–12: solid, but re-read the ones you missed; they will come back in real code. Below 9: work through the article again, typing each wrong example yourself before the fix. The errors only become easy when you have met them once.
Photo by Lukas Blazek on Pexels
What to practise next
The fastest way to lock this in is a week of tiny daily exercises: one 20-line program a day, each using a for loop, a function with a return, and one try block. When something breaks — and it will — read the error message before changing anything. If you can explain why the error happened, in one sentence, you have learned the mistake for good.
From here, the natural next steps in this series are Python for data science for beginners, which turns these basics into data work with Pandas, and our SQL interview questions and answers — the other half of the beginner data toolkit. And if the maths side of test prep is on your list, our statistics notes for TLT, ECT, PST and JEST exams cover mean, median, mode and range with worked examples.


