Difference between revisions of "Python:Common Mistakes"
(additional descriptions added) |
m (added variable problems and some method problems) |
||
Line 8: | Line 8: | ||
===Assignment vs. Accessing=== | ===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. | ||
+ | <syntaxhighlight lang=python line> | ||
+ | #PLACEHOLDER | ||
+ | </syntaxhighlight> | ||
===Calculating in a Vacuum=== | ===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. | ||
+ | <syntaxhighlight lang=python line> | ||
+ | x = 0 | ||
+ | print("x equals {}".format(x)) #x equals 0 | ||
+ | x + 2 | ||
+ | print("x equals {}".format(x)) #x equals 0 | ||
+ | x = x + 2 | ||
+ | print("x equals {}".format(x)) #x equals 2 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===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. | ||
+ | |||
+ | <syntaxhighlight lang=python line> | ||
+ | x = [1, 2] | ||
+ | y = x | ||
+ | print("x equals {}".format(x)) #x equals [1, 2] | ||
+ | print("y equals {}".format(x)) #y equals [1, 2] | ||
+ | y[0] = 3 | ||
+ | print("x equals {}".format(x)) #x equals [3, 2] | ||
+ | print("y equals {}".format(x)) #y equals [3, 2] | ||
+ | </syntaxhighlight> | ||
===Mutable and Immutable Data Types=== | ===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. | ||
+ | <syntaxhighlight lang=python line> | ||
+ | x = (1, 2) | ||
+ | y = "hello" | ||
+ | print("x equals {}".format(x)) #x equals (1, 2) | ||
+ | print("y equals {}".format(x)) #y equals hello | ||
+ | x[0] = 3 #TypeError | ||
+ | y[3] = "m" #TypeError | ||
+ | </syntaxhighlight> | ||
==Problems with Methods== | ==Problems with Methods== | ||
Line 20: | Line 55: | ||
===Printing vs. Returning=== | ===Printing vs. Returning=== | ||
+ | Virtually all of the methods that you write in [http://pundit.pratt.duke.edu/wiki/EGR_103/Spring_2020 EGR 103] will include a return statement. The return statement will be the last line of code to run. | ||
+ | |||
+ | <syntaxhighlight lang=python line> | ||
+ | def calc_adder(x): | ||
+ | print("x equals {}".format(x)) #x equals x | ||
+ | x = x + 1 | ||
+ | print("x equals {}".format(x)) #x equals x+1 | ||
+ | return x #returns x+1 | ||
+ | x = x + 1 #This line of code will never execute | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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. | ||
+ | <syntaxhighlight lang=python line> | ||
+ | def calc_adder2(x): | ||
+ | if (x > 0): | ||
+ | print("x equals {}".format(x)) #x equals x | ||
+ | print("x is positive") #x is positive | ||
+ | x = x + 1 | ||
+ | return x #returns x+1 | ||
+ | </syntaxhighlight> | ||
===Methods that Return and Methods that Modify=== | ===Methods that Return and Methods that Modify=== |
Revision as of 22:04, 15 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.
1 x = 0
2 print("x equals {}".format(x)) #x equals 0
3 x + 2
4 print("x equals {}".format(x)) #x equals 0
5 x = x + 2
6 print("x equals {}".format(x)) #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(x)) #y equals [1, 2]
5 y[0] = 3
6 print("x equals {}".format(x)) #x equals [3, 2]
7 print("y equals {}".format(x)) #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(x)) #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
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
See also: Enumerate