Difference between revisions of "Python:Common Mistakes"

From PrattWiki
Jump to navigation Jump to search
m (minor grammar edit)
(Calculating in a Vacuum)
Line 17: Line 17:
 
The result of a calculation must always be stored or otherwise delivered to a method handler. The code below initially results in no change to variable x.
 
The result of a calculation must always be stored or otherwise delivered to a method handler. The code below initially results in no change to variable x.
  
<syntaxhighlight lang=python line>
+
<syntaxhighlight lang=python>
x = 0
+
In [1]: x = 0
print("x equals {}".format(x))   #x equals 0
+
 
x + 2
+
In [2]: print("x equals {}".format(x))
print("x equals {}".format(x))   #x equals 0
+
Out[2]: x equals 0
x = x + 2
+
 
print("x equals {}".format(x))   #x equals 2
+
In [3]: x + 2
 +
Out[3]: 2
 +
 
 +
In [4]: print("x equals {}".format(x))
 +
Out[4]: x equals 0
 +
 
 +
In [5]: x = x + 2
 +
 
 +
In [6]: print("x equals {}".format(x))
 +
Out[6]: x equals 2
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 16:07, 16 January 2020

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 otherwise delivered to a method handler. The code below initially results in no change to variable x.

In [1]: x = 0

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

In [3]: x + 2
Out[3]: 2

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

In [5]: x = x + 2

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

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.

1 x = [1, 2]
2 y = x
3 print("x equals {}".format(x))    #x equals [1, 2]
4 print("y equals {}".format(y))    #y equals [1, 2]
5 y[0] = 3
6 print("x equals {}".format(x))    #x equals [3, 2]
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.

1 x = (1, 2)
2 y = "hello"
3 print("x equals {}".format(x))    #x equals (1, 2)
4 print("y equals {}".format(y))    #y equals hello
5 x[0] = 3                          #TypeError
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.

1 x = "1 2 3"
2 y = x.split(" ")
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 
3 x = [1, 2, 3]
4 np.random.shuffle(x)
5 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

Network Errors

Variable Explorer

Renaming Existing Variables