Python:Common Mistakes
Overview
This page is designed as a resource for new programmers learning Python for EGR 103. If you are an experiencing an error with which you are not familiar, check the common mistakes below first.
Problems with Variables
These are problems involving the use, saving, and manipulation of variables, often encountered during the first half of the labs in EGR 103.
Assignment vs. Accessing
When a variable starts a line and is immediately followed by a single equals sign, that variable is being assigned a new value. Most other situations involve accessing that variable but not directly modifying its value.
1 # PLACEHOLDER
Calculating in a Vacuum
The result of a calculation must always be stored or used somehow, or else it has no effect. The code below initially results in no change to variable x.
In [1]: x = 0
In [2]: print("x equals {}".format(x))
x equals 0
In [3]: x + 2
Out[3]: 2
In [4]: print("x equals {}".format(x))
x equals 0
In [5]: x = x + 2
In [6]: print("x equals {}".format(x))
x equals 2
Note that after calling x + 2
, the value of x doesn't change from zero, because the result of the calculation is never stored. After storing it in x with x = x + 2
, x becomes 2.
Renaming keywords/reserved words and existing variables
There are some words called keywords (or reserved words in some other languages) that are variables that already exist in Python. For example, for
and ord
are both keywords. While Python allows you to use these as ordinary variable names, doing so is invariably a bad idea.
#PLACEHOLDER, need usual error statement
Pointers and Data Types
For lists and other non-primitive data types, variables act as pointers to the data in memory. In the code below, y points to the same data as x in memory. By modifying the first element (0th index) of list y, x is also inadvertently changed because x points to the same location in memory.
In [1]: x = [1,2]
In [2]: y = x
In [3]: print("x equals {}".format(x))
x equals [1, 2]
In [4]: print("y equals {}".format(y))
y equals [1, 2]
In [5]: y[0] = 3
In [6]: print("x equals {}".format(x))
x equals [3, 2]
In [7]: print("y equals {}".format(y))
y equals [3, 2]
Mutable and Immutable Data Types
Unlike lists and numpy arrays, tuples and strings are not mutable in Python. The following code throws type errors on 5 and 6.
In [1]: x = (1, 2)
In [2]: y = "hello"
In [3]: print("x equals {}".format(x)) #x equals (1, 2)
In [4]: print("y equals {}".format(y)) #y equals hello
In [5]: x[0] = 3
TypeError
In [6]: y[3] = "m"
TypeError
Problems with Methods
These are problems involving the writing and use of methods, often encountered during the second half of the labs in EGR 103.
Printing vs. Returning
Virtually all of the methods that you write in EGR 103 will include a return statement. The return statement will be the last line of code to run.
1 def calc_adder(x):
2 print("x equals {}".format(x)) #x equals x
3 x = x + 1
4 print("x equals {}".format(x)) #x equals x+1
5 return x #returns x+1
6 x = x + 1 #This line of code will never execute
While almost all methods will use a return statement, some will also include print statements. The two are not interchangeable. The code below will always return x+1, but it will only print x if x is positive.
1 def calc_adder2(x):
2 if (x > 0):
3 print("x equals {}".format(x)) #x equals x
4 print("x is positive") #x is positive
5 x = x + 1
6 return x #returns x+1
Methods that Return and Methods that Modify
Most methods that you will use in EGR 103 will return a value that you will store. The code below includes one such method.
In [1]: x = "1 2 3"
In [2]: y = x.split(" ")
In [3]: print("y equals {}".format(y))
y equals ['1', '2', '3']
Occasionally, you will encounter a method that does not return a value, but which modifies the argument directly. The code below includes one such method.
import numpy as np
In [1]: x = [1, 2, 3]
In [2]: np.random.shuffle(x)
In [3]: print("x equals {}".format(x))
x equals [2, 3, 1]
Always check the documentation when using a method. See also: numpy.random.shuffle
Miscellaneous
These are other logic errors or configuration problems you may encounter, as well as some useful debugging practices.
For Loops vs. For-Each Loops
1 # PLACEHOLDER
See also: Enumerate
Testing Your Code
1 if __name__ == '__main__':
2 # PLACEHOLDER