CIS 2020 · Python for Everyone · Module 2 · Study guide
This covers errors, algorithm design, variables, and arithmetic. There are more worked examples here, because this is where the quiz starts asking you to predict what a line of code prints.
Every programmer makes mistakes. The book sorts them into three kinds, and the quiz expects you to tell them apart.
| Kind | When Python notices | Does it run? | Example |
|---|---|---|---|
| Syntax | Before it runs | No, never starts | print("hi) |
| Runtime | While it runs | Starts, then stops | total / 0 |
| Logic | Never | Yes, to the end | area = w + h |
Work out the solution before you write any Python.
An algorithm is a step by step description of how to solve a problem. To count as one, it has to be all three of these:
You usually write the solution first as pseudocode, which is just the steps in plain, orderly English. Pseudocode has no strict rules. The point is to settle the logic before you fight with syntax.
Here is a typical book example. You have two packs of soda at different prices and sizes, and you want the better deal.
1. Read price1 and count1. unit1 = price1 / count1 2. Read price2 and count2. unit2 = price2 / count2 3. If unit1 < unit2, report pack 1. Otherwise report pack 2.
A variable is a named box in memory that holds one value.
You make a variable with an assignment:
cans_per_pack = 6
The single = is the assignment operator. It is not the equals sign from math. It means take the value on the right and store it in the name on the left. Read it as "cans_per_pack gets 6". You can put a new value in the same box later, and the old one is gone.
cans_per_pack = 6
cans_per_pack = 12 # the box now holds 12
A very common pattern updates a variable using its own current value.
total = 0 total = total + 2
Python always finishes the right side first. It looks up the current total (0), adds 2, gets 2, and only then stores 2 back into total.
An int is a whole number with no fractional part: 6, 0, -25. A float has a decimal point: 6.0, 3.14, -0.5. Division with / always produces a float, so even 4 / 2 gives 2.0.
if, for, def, or class.count and Count are two different variables.The convention in this course: lowercase words joined by underscores, and names that say what the value is. total_cost beats tc.
A constant is a value that should not change while the program runs. Python does not enforce this, so by agreement we write constant names in capitals.
BOTTLE_VOLUME = 2.0 CANS_PER_PACK = 6
Anything after a # on a line is a note for humans and is ignored by Python. Use comments to explain why, not to repeat what the code already says plainly.
# a six pack, used to size the order
CANS_PER_PACK = 6
This is the part the quiz drills. Learn what each operator returns.
The basic operators are + - * /. You must write * for multiplication. 2 * n is fine; 2n is a syntax error.
** raises to a power. 2 ** 10 is 1024./ is true division and always gives a float. 7 / 2 is 3.5.// is floor division. It divides and drops the fraction. 7 // 2 is 3.% is the remainder, also called modulus. 7 % 2 is 1. It is handy for testing even or odd with n % 2 == 0, and for splitting a total, such as turning 234 cents into 2 dollars (234 // 100) and 34 cents (234 % 100).| Expression | Result | Note |
|---|---|---|
| 7 / 2 | 3.5 | true division, always a float |
| 7 // 2 | 3 | floor division, fraction dropped |
| 7 % 2 | 1 | remainder |
| 2 ** 5 | 32 | power |
| 5 + 2.0 | 7.0 | an int plus a float is a float |
Python follows the usual math rules:
** first* / // %+ -Operators at the same level run left to right. Put parentheses around the top or the bottom of any fraction you copy from math: write (a + b) / 2, not a + b / 2.
3 + 4 * 5 ** 2 3 + 4 * 25 # ** first 3 + 100 # * next 103 # + last
round(x) rounds to the nearest whole number, and round(x, 2) keeps two decimal places. abs(x) gives the size of a number without its sign. Both are built in and always available.
The math module has the rest. Import it once, then reach into it with a dot.
import math r = 2.5 area = math.pi * r ** 2 side = math.sqrt(area)
= is assignment, not equality. Python computes the right side first, then stores it in the name on the left.int has no decimal point, a float does. / always gives a float.7 // 2 is 3, 7 % 2 is 1, 2 ** 3 is 8. Precedence: **, then * / // %, then + -.round() and abs() are built in. sqrt and pi live in the math module and need import math.