Grade by Grade
A. Programming Concepts and Skills · Programming Basics: Data, Decisions and Loops

Programming Basics: Data, Decisions and Loops: Unit Review

A1.1 A1.2 A1.3 A1.4 A2.1 A2.2 A2.3 A4.1 A4.2 A4.3 A4.4 A4.5About 40 minutes
Learning goals
  • I can use variables, constants and the four basic data types, and explain how data is stored in binary, hexadecimal, ASCII and Unicode.
  • I can write expressions with arithmetic, comparison and Boolean operators in the correct order.
  • I can write programs with input, processing and output, using sequence, selection and repetition, including nested structures.
  • I can find and fix errors, trace programs and test them with a full range of cases.

Words to know

data type
int, float, str or bool: the kind of value something is.
assignment
name = expression: work out the right side, then store it under the name.
control structures
Sequence, selection (if) and repetition (while, for).
trace table
A hand record of every variable after every step.
test case
An input and its expected output: typical, boundary, extreme or invalid.

Let's review!

In this unit you wrote your first Python programs and learned the building blocks of every program: variables and constants with the four basic data types, how computers store numbers and text in binary, hexadecimal, ASCII and Unicode, arithmetic, comparison and Boolean expressions, input and formatted output, and the three control structures: sequence, selection and repetition, including nested structures. You also learned to read error messages, trace code and test it like a professional.

This review has a few questions from every lesson. Try each one on paper or in Python, then press Show answer to check.

How to use this review

Do one or two sections at a time. For any question with code, predict the output on paper first (a trace table helps), then check by running it in Python. Type the code yourself instead of copying it: copied indentation can include invisible characters Python does not accept. For every question you get wrong, write down why (wrong order of operations? off-by-one in a range? forgot to convert input?) and go back to that lesson's worked examples. Finish with the 10-question quiz.

Sequenceone statementafter anotherSelectionif / elif / elsechoose a branchRepetitionwhile: until a conditionfor: a counted rangeNest them: an if inside a loop counts, totals and finds the highest or lowest;a loop inside a loop handles rows and columns.
The three control structures from this unit, and how nesting combines them.
PriorityOperatorsNotes
1 (first)( )parentheses
2**exponent, right to left
3-xnegative sign
4*, /, //, %left to right
5+, -left to right
6==, !=, <, >, <=, >=comparisons
7not
8and
9 (last)or
Python's order of operations for every operator in this unit (Lessons 4 and 8). When in doubt, add parentheses.
Just for fun

Debugging is like being the detective in a crime movie where you are also the culprit.

Your First Python Programs: Output, Sequence and Comments

1

What does this print?

  • print("A", "B", sep="-", end="!")
  • print("C")
2

Which control structure runs statements one after another from top to bottom?

3

Write a header comment for a program called tip.py that calculates a restaurant tip.

Variables, Constants and Assignment Statements

1

Trace and give the output.

  • a = 4
  • b = a + 2
  • a = b * a
  • b -= 1
  • print(a, b)
2

Which of these names are illegal in Python? my score, score2, 2score, _total, if

3

Write code to swap the values of x and y using a temporary variable.

Data Types: Integers, Floats, Strings and Booleans

1

Give the type of: 0.0, "0", 0, False.

2

What is int(-6.8)? What is float("6")?

3

What is bool("")? What is bool("False")?

Arithmetic Expressions and the Order of Operations

1

Evaluate 7 + 3 * 2 ** 2 - 10 // 4.

2

Convert 250 seconds to minutes and seconds with // and %.

3

What are -9 // 2 and -9 % 2?

How Computers Store Numbers: Binary and Hexadecimal

1

Convert binary 11100101 to decimal and to hex.

2

Convert 90 to binary.

3

How many values can 12 bits represent?

How Computers Store Text: ASCII and Unicode

1

What are ord("D") and chr(101)?

2

Why does ASCII have exactly 128 codes?

3

How many bytes does "Montréal" take in UTF-8? Explain.

Input, Processing and Output with String Expressions

1

The user types 3 at n = input("n: "). What does print(n * 3) show? How do you fix it to show 9?

2

Write an f-string that prints price with exactly 2 decimal places after a dollar sign.

3

What is "ab" + "c" * 3?

Boolean Expressions: Comparison and Logical Operators

1

With x = 6 and y = 3, evaluate x > 5 and y > 5 or not x == 6.

2

Write one condition that is True when n is from 10 to 20 inclusive and odd.

3

What is the difference between = and ==?

Selection: if, elif and else

1

What is printed for t = 15?

  • if t >= 20:
  •     print("warm")
  • elif t >= 10:
  •     print("mild")
  • else:
  •     print("cold")
2

Why must if mark >= 50: not come first in an elif chain that also checks >= 80 and >= 70?

3

Give test values for if speed > 50:.

Nested and Compound Selection

1

What is printed for a = 2, b = 7?

  • if a > 0:
  •     if b % a == 0:
  •         print("divides")
  •     else:
  •         print("no")
  • else:
  •     print("bad")
2

Rewrite if a: with if b: nested directly inside (and nothing else) as one if.

3

What is a guard? Give an example.

Repetition with while Loops

1

Trace and give the output.

  • n = 1
  • while n < 30:
  •     n = n * 2 + 1
  • print(n)
2

Name the three parts every while loop needs.

3

Write a validation loop that asks for a PIN until it is from 1000 to 9999.

Counted Repetition: for Loops and range

1

List the values of range(3, 15, 4) and range(9, 0, -3).

2

What is printed?

  • total = 0
  • for k in range(2, 11, 2):
  •     total += k
  • print(total)
3

Would you use for or while to read numbers until the user types 0?

Nested Structures: Counting, Totals, Highest and Lowest

1

How many stars are printed?

  • for i in range(3):
  •     for j in range(i + 1):
  •         print("*", end="")
  •     print()
2

Why start highest at the first value rather than 0?

3

Where must the accumulator count = 0 go in a counting loop, and why?

Finding and Fixing Errors: Syntax, Run-Time and Logic

1

Classify: (a) a missing ) (b) int("seven") (c) > used where >= was needed.

2

Which line of a traceback should you read first?

3

A program prints two lines, then ZeroDivisionError. What kind of error is it?

Tracing, Testing and Writing Clean Code

1

List the four kinds of test cases.

2

A program accepts ages 0 to 120. Give six good test values.

3

What is a magic number, and how do you fix it?

Check your understanding

Quiz

1.

What does print(17 // 5, 17 % 5) show?

2.

The user types 5 at x = input(). What is type(x)?

3.

What is binary 101010 in decimal?

4.

What is chr(ord("a") + 2)?

5.

Which expression is True when x is outside the range 1 to 10?

6.

For x = 0: if x > 0: → A, elif x == 0: → B, else: → C. Which runs?

7.

How many times does for i in range(2, 12, 3): run?

8.

Which loop is infinite?

9.

A program runs without messages but gives a wrong average. What kind of error is it?

10.

Which set of tests is best for if age >= 18:?

For parents and teachers

Use the review to find what to revisit:

  • print spacing, sep/end, comments → Lesson 1 (Your First Python Programs).
  • Trace mistakes with x = x + 1, swaps, naming rules → Lesson 2 (Variables, Constants and Assignment).
  • Confusing "5" and 5, or conversion errors → Lesson 3 (Data Types).
  • Order of operations, // and % → Lesson 4 (Arithmetic Expressions).
  • Binary and hex conversions → Lesson 5 (Binary and Hexadecimal).
  • Character codes, ord/chr, UTF-8 byte counts → Lesson 6 (ASCII and Unicode).
  • Forgetting to convert input, f-string formatting → Lesson 7 (Input, Processing and Output).
  • and/or/not and comparisons → Lesson 8 (Boolean Expressions).
  • Wrong elif order, indentation, boundary errors → Lesson 9 (Selection).
  • Nested ifs and guards → Lesson 10 (Nested and Compound Selection).
  • Infinite loops, sentinel and validation loops → Lesson 11 (while Loops).
  • Off-by-one with range → Lesson 12 (for Loops).
  • Accumulators in the wrong place, highest/lowest, nested loops → Lesson 13 (Nested Structures).
  • Reading error messages → Lesson 14 (Errors).
  • Weak test plans, messy code → Lesson 15 (Tracing, Testing and Clean Code).

The next unit, Arrays and Subprograms, stores many values in lists, works inside strings, writes functions, and searches and sorts data, using every skill from this unit.