CIS 2020 · Python for Everyone · Module 2 · Study guide

Module 2 review: sections 1.6 to 2.2

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.

1.6 Errors

Every programmer makes mistakes. The book sorts them into three kinds, and the quiz expects you to tell them apart.

The same three errors, side by side.
KindWhen Python noticesDoes it run?Example
SyntaxBefore it runsNo, never startsprint("hi)
RuntimeWhile it runsStarts, then stopstotal / 0
LogicNeverYes, to the endarea = w + h

1.7 Problem solving: algorithm design

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.
Flowchart for the better buy algorithm Start, then read the price and count for each pack, then compute a unit price for each. A decision asks whether unit price 1 is less than unit price 2. If yes, report pack 1. If no, report pack 2. Then stop. Start read price and count for each pack unit price = price / count, for each pack unit1 < unit2 ? report pack 1 report pack 2 Stop yes no
A flowchart is one way to show an algorithm. The diamond is the only place the path splits.

2.1 Variables

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
Assignment, then reassignment After cans_per_pack = 6 the box holds 6. After cans_per_pack = 12 the same box holds 12 and the 6 is gone. cans_per_pack = 6 6 cans_per_pack cans_per_pack = 12 12 cans_per_pack the 6 is overwritten: same box, new value

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.

How total = total + 2 runs Step 1: Python reads the current value of total, which is 0, and works out 0 plus 2, which is 2. Step 2: it stores 2 back into total, replacing the 0. Step 1 compute the right side total + 2 using total = 0 gives 0 + 2 gives 2 Step 2 store it back 2 total now holds 2

Number types

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.

Variable names

The convention in this course: lowercase words joined by underscores, and names that say what the value is. total_cost beats tc.

Constants

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

Comments

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

2.2 Arithmetic

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.

What each operator returns.
ExpressionResultNote
7 / 23.5true division, always a float
7 // 23floor division, fraction dropped
7 % 21remainder
2 ** 532power
5 + 2.07.0an int plus a float is a float

Order of operations

Python follows the usual math rules:

  1. ** first
  2. then * / // %
  3. then + -

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

Calling functions

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)

Quick check

If you remember only these

  • Syntax error: caught before running, the program never starts. Runtime error: starts, then stops. Logic error: runs fine, wrong answer.
  • An algorithm must be unambiguous, executable, and terminating. Pseudocode is the plan written in plain English.
  • = is assignment, not equality. Python computes the right side first, then stores it in the name on the left.
  • An int has no decimal point, a float does. / always gives a float.
  • Names start with a letter or underscore, are case sensitive, and cannot be reserved words. Constants are written in capitals by convention.
  • 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.