Exam Pseudocode in Python: Every Construct Side by Side
The Computer Science Principles exam writes its code questions in its own pseudocode, so the fastest way to read them is to know exactly how each line would look in Python, and where the two languages quietly disagree.
You get the reference sheet in the exam, so you do not have to memorize the notation. What costs points is the handful of places where it behaves differently from Python. Each one is marked below, and you can type any pseudocode from this page into the pseudocode runner to watch it run.
How do assignment and DISPLAY work?
| Pseudocode | Python |
|---|---|
a ← 7 | a = 7 |
a = b (a comparison) | a == b |
a ≠ b | a != b |
DISPLAY(a) | print(a, end=" ") |
x ← INPUT() | x = input() |
The arrow ← stores a value. A single = in pseudocode compares, like == in Python. Mixing these up is the most common slip when reading exam code.
DISPLAY shows a value followed by a space, not a new line. Three DISPLAY calls in a row print on one line.
10 7 False
How do the arithmetic operators compare?
| Pseudocode | Python | Example |
|---|---|---|
a + b, a - b, a * b | the same | |
a / b | a / b | 17 / 5 is 3.4 |
a MOD b | a % b | 17 MOD 5 is 2 |
RANDOM(a, b) | random.randint(a, b) | RANDOM(1, 3) can be 1, 2 or 3 |
MOD has the same precedence as * and /, the same as % in Python, so 2 + 7 MOD 3 is 2 + 1, which is 3. RANDOM(a, b) includes both ends, and so does Python's random.randint(a, b). (Python's random.randrange(a, b) leaves out b, so do not swap them.)
3.4 2 3 [1, 2, 3]
How do IF and the loops translate?
| Pseudocode | Python |
|---|---|
IF(condition) { ... } | if condition: |
IF(condition) { ... } ELSE { ... } | if condition: ... else: ... |
REPEAT n TIMES { ... } | for _ in range(n): |
REPEAT UNTIL(condition) { ... } | while not condition: |
FOR EACH item IN aList { ... } | for item in a_list: |
NOT, AND, OR | not, and, or |
The reference sheet has no ELSE IF. A chain of choices is written as an IF nested inside an ELSE, which Python would write with elif.
REPEAT UNTIL stops when its condition becomes true, the opposite of Python's while, which keeps going while its condition is true. The condition is checked before each pass, so the body can run zero times.
count: 3 passes: 0
Why do lists start at 1?
Exam lists number their elements from 1: the first element of aList is aList[1]. Python counts from 0, so the same element is a_list[0]. Any index from the exam becomes one less in Python.
| Pseudocode | Python |
|---|---|
aList ← [10, 20, 30] | a_list = [10, 20, 30] |
aList[1] (the first element) | a_list[0] |
aList[i] ← x | a_list[i - 1] = x |
APPEND(aList, x) | a_list.append(x) |
INSERT(aList, i, x) | a_list.insert(i - 1, x) |
REMOVE(aList, i) | a_list.pop(i - 1) |
LENGTH(aList) | len(a_list) |
Two more differences matter:
a_list[-1] is the last element), and insert with a large index quietly appends, but on the exam index 0, a negative index or one past the end is an error.a_list = b_list makes both names refer to the same list, so changing one changes the other. Use b_list.copy() to get the exam's behaviour.shared: [1, 2, 3, 4] copied: [1, 2, 3]
70 [80, 85, 90] Error: index 0 is outside the list of 3
How are procedures written?
| Pseudocode | Python |
|---|---|
PROCEDURE name(a, b) { ... } | def name(a, b): |
RETURN(expression) | return expression |
result ← name(1, 2) | result = name(1, 2) |
Arguments are matched to parameters in order, as in Python. RETURN can appear anywhere inside a procedure and ends it at once.
17
What about the robot?
Some questions show a robot in a grid. MOVE_FORWARD() moves it one square in the direction it faces, ROTATE_LEFT() and ROTATE_RIGHT() turn it 90 degrees in place, and CAN_MOVE(direction) is true when the square in that direction (left, right, forward or backward, relative to where it faces) is open. If the robot is told to move into a blocked square or off the grid, it stays put and the program stops. Python has no built-in robot; the grid robot lesson builds one, and the pseudocode runner draws the grid for you.
Practice
Translate this pseudocode into a Python function count_above(values, limit):
PROCEDURE countAbove(values, limit)
{
count ← 0
FOR EACH v IN values
{
IF (v > limit)
{
count ← count + 1
}
}
RETURN (count)
}Write exam_insert(a_list, i, value) that behaves like the exam's INSERT(aList, i, value): it puts value at position i, counting from 1, and shifts the later elements right. Return the list.
If i is not a valid position in the list (less than 1 or more than its length), return the string "error" instead and leave the list unchanged.
Check your understanding
What is displayed?
aList ← [1, 2, 3]bList ← aListAPPEND (aList, 4)DISPLAY (LENGTH(bList))
What is displayed?
count ← 7REPEAT UNTIL (count > 5){count ← count - 1}DISPLAY (count)
Which two Python lines do the same thing as REMOVE(aList, 1)?
Select 2 answers.
What is displayed?
PROCEDURE mystery(n){IF (n MOD 2 = 0){RETURN (n / 2)}RETURN (n * 3)}DISPLAY (mystery(4))DISPLAY (mystery(5))
Which Python expression gives every value that RANDOM(1, 6) can?
Exam topics: the exam reference sheet, used across 3.1 Variables and Assignments (AAP-1.B), 3.3 Mathematical Expressions (AAP-2.C), 3.6 to 3.8 Conditionals and Iteration (AAP-2.H, AAP-2.K), 3.10 Lists (AAP-2.N, AAP-2.O), 3.12 Calling Procedures (AAP-3.A) and 3.15 Random Values (AAP-3.E)