Python:Common Mistakes

From PrattWiki
Revision as of 21:41, 29 January 2020 by Taj26 (talk | contribs) (Added category for EGR 103)
Jump to navigation Jump to search

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.

In [1]: x = 1

In [2]: y = 2

In [3]: x = y

In [4]: print("x equals {}".format(x))
x equals 2

Note that in the code above, x = y, accesses the value of y and assigns it to x. The variable which is being assigned a new value is always on the left.

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 on input line 3 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.

In [1]: x = 1

In [2]: y = 2

In [3]: sum = x + y

In [4]: z = sum(x, y)
TypeError: 'int' object is not callable

sum is a keyword for a function that returns the sum of 2 arguments or of a list. Declaring it as a variable overwrites this function. If you accidentally name a variable the same as a keyword, you will need to click Remove all variables.

See also: Variable Explorer

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. Note that this does not hold true for primitive data types like ints.

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: 'tuple' object does not support item assignment

In [6]: y[3] = "m"                        
TypeError: 'tuple' object does not support item assignment

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 process. 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.

1 import numpy as np
2 x = [1, 2, 3]
3 np.random.shuffle(x)
4 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

The following code uses a for-each loop to access every item in a list.

In [1]: x = [11, 22, 33]

In [2]: for item in x:
   ...:     print(item * 2)
   ...:
22
44
66

The following code uses a for loop to iterate through each index in a list and then print the item at that index.

In [1]: x = [11, 22, 33]

In [2]: for index in range(len(x)):
   ...:     print(index, x[index] * 2)
   ...:
0 22
1 44
2 66

See also: Enumerate

Testing Your Code

It is often useful to test the methods you write within the same script. One way of accomplishing this is to append the following code at the bottom of your script, calling any methods you've created, and then clicking Run.

1 def foo(x):
2     return x + 1
3 
4 if __name__ == '__main__':
5     print("foo returns {}".format(foo(0)))  #foo returns 1

Network Errors

Most assignments for EGR 103 require that your script is in the correct lab directory. Occasionally, Spyder will save your script to a different default location (e.g. Desktop). This will be indicated in the Path, at the top left corner of the Spyder window. In order for these scripts to access other files and to be imported into LaTeX, this Path should be very similar to what you seen when running pwd in MobaXterm.

If you are unable to connect to your virtual machine, ensure that (1) you are connected to Duke Blue, (2) you are entering your NetID and password correctly, and (3) you are running ssh with the -XY adverb.

Variable Explorer

The Variable explorer is an invaluable debugging tool included within Spyder. By default, it is located in the top right of the Window, in the same box as File explorer and Help. By selecting it, you are able to view the Name, Type, Size, and Value of all variables you have declared during your current session.

By using Run current cell or Run current line, you can step through your code, tracking the value of variables in the Variable explorer to see if they match your expectations.

Occasionally, variables may share common names between scripts. If you notice you are receiving outputs wildly different from what you expect, you may find it useful to click Remove all variables (the eraser icon).