These are mistakes that I have made developing in Python over the years. This by no means the “definitive” list but these are the type of mistakes that I have found that my colleagues & friends have made at some point or another. If you think other mistakes should be added, comment away!

Mistake 1# Modifying list while iterating

>>>li=[1,2,3]

>>>for i in li:

… li.append(4)

The finite loop turns into infinite loop

Mistake 2# Misinterpreting Python variable scope

When a variable is assigned inside a function python assumes its defined inside

>>>i=[1,2,3]

>>>def print_i():

… print i

[1,2,3]

>>>def modify_i():

… i=i+[4,5]

UnboundLocalError: local variable ‘i’ referenced before assignment

Mistake 3# Thinking True and False are Keywords

In Python version 2.x, True and False are not keywords, so

>>>True = False

>>>if True == False:

… print “end of the world”

Mistake 4# Difference Between Python 2.x and Python 3.x

In Python 2.x

>>>print “hello world”

hello world

In Python 3.x

>>>print “hello world”

File “<stdin>”, line 1

print “hello”

^

SyntaxError: invalid syntax

>>>print (“hello world”)

hello world

Mistake 5# Data Structures are not Immutable Objects

>>>li1 = []

>>>li2=li1

>>>li2.append(1)

>>>print li1

[1]

This is because a single object is created and it is given several different names, so when you change the object, it’s reflected in all the variables which refer that object

Mistake 6# Specifying parameters incorrectly for an exception block

>>>try:

… int(“z”)

>>>except IndexError, ValueError:

… pass

reason this doesn’t work is because IndexError is the type of exception you’re catching, and ValueError is the name of the variable you’re assigning the exception to.

Correct code to catch multiple exceptions is:

>>>try:

… int(“z”)

>>>except (IndexError, ValueError):

… pass

Mistake 7# ‘is’ and ‘==’ operators are not same

>>> a=257

>>> b=257

>>> a is b

False

>>> a == b

True

>>> id(a)

23486712

>>> id(b)

23486736

‘is’ operator checks for object identity and ‘==’ operator checks for identical value.

Mistake 8# Imports Only Work the First Time

You can run a file by importing it at the interactive prompt, but this only works once per session; subsequent imports simply return the already-loaded module. To force Python to reload and rerun a file’s code, call the reload(module) function instead. And while you’re at it, be sure to use parentheses for reload, but not import.

Mistake 9# Integer Division

>>> 5/2

2

You probably wanted:

>>> 5.0/2

2.5

One of the operands need to be float type.

Mistake 10# Misunderstanding variables

>>> class X(object):

… a = 1

…

>>> class Y(X):

… pass

…

>>> class Z(X):

… pass

…

>>> print X.a, Y.a, Z.a

1 1 1

when Y’s variable a is changed

>>> Y.a = 5

>>> print X.a, Y.a, Z.a

1 5 1

the output is expected and when we change the X’s variable a name

>>> X.a = 3

>>> print X.a, Y.a, Z.a

3 5 3

class variables are internally handled as dictionaries and follow what is often referred to as Method Resolution Order (MRO). So in the above code, since the attribute a is not found in class Z, it will be looked up in its base classes (only X in the above example, although Python supports multiple inheritance). In other words, Z doesn’t have its own a property, independent of X. Thus, references to Z.a are in fact references to X.a. This causes a Python problem unless it’s handled properly.