Harmony Virtual Lab

Digital Technologies · Programming in Python

Coding in Python

Write real Python – print, variables, loops and if statements – and have each program checked automatically.

Start this experimentAll Digital Technologies experiments

Aim

To write, run, test and debug simple Python programs using variables, arithmetic, loops and selection.

Apparatus

  • This code editor (works offline)
  • Optional: Python installed on a computer

Theory

  • print("Hello") displays text; print(3 * 4) displays 12.
  • A variable stores a value: price = 500.
  • A for loop repeats: for i in range(1, 6): runs with i = 1, 2, 3, 4, 5.
  • if / elif / else chooses what to do. Lines inside a loop or an if are indented by 4 spaces.
  • sum(list), len(list) and round(x, 1) are built in.

This editor runs a teaching subset of Python offline. It supports variables, numbers, strings, f-strings, lists, if/elif/else, for and while loops and common built-in functions, but not def, import or input().

Procedure

  1. Choose a task and read it carefully.
  2. Write your program in the editor and press Run to see the output.
  3. Read any error message: it tells you the line and the kind of mistake.
  4. Press Check task to test your program automatically.
  5. Work through all six tasks.

What you do in the virtual lab

Set up the apparatus on screen, take readings and record them in a table (Task; Lines of code; Result). Simulated instruments give slightly different readings each time, as real ones do. Finish with a short quiz; every answer is explained.

Teachers can set the experiment for a class, see each student’s results and use it as a WAEC- or NECO-style practical paper in exam practice.

Practice questions

  1. What does this print? x = 5 then print(x * 2 + 1)

    1. 15
    2. x * 2 + 1
    3. 11
    4. 5 * 2 + 1
    Show the answer

    C. 11 Multiplication first: 5 × 2 = 10, then + 1 = 11.

  2. How many times does the body of for i in range(3): run?

    1. 3
    2. 2
    3. 4
    4. 0
    Show the answer

    A. 3 range(3) gives 0, 1 and 2 – three values.

  3. Why does print("Age: " + 15) give an error?

    1. print cannot show numbers
    2. The quote marks are wrong
    3. 15 is too big
    4. You cannot join a string and a number with + without converting the number first
    Show the answer

    D. You cannot join a string and a number with + without converting the number first Use print("Age: " + str(15)) or print("Age:", 15).

Do the experiment and the full quiz

Related Digital Technologies experiments

  • Logic gates and truth tablesDigital Technologies · SSS 1 – SSS 2Switch the inputs of AND, OR, NOT, NAND, NOR and XOR gates and build each truth table.
  • Binary, octal and hexadecimalDigital Technologies · JSS 2 – SSS 1Flip eight bit-switches to see how computers store numbers, then hit target values in binary.
  • Programming a robot: sequence and loopsDigital Technologies · JSS 1 – SSS 1Write a program of moves and turns to steer a robot to its goal, then shorten it with a loop.
  • How strong is a password?Digital Technologies · JSS 1 – SSS 2Test made-up passwords, measure their strength in bits and see how long an attacker would need to guess them.
  • Spreadsheet formulas and functionsDigital Technologies · JSS 2 – SSS 2Use cell references, SUM, AVERAGE, MAX and IF to complete a shop sales record and a class result sheet.
  • Train a simple AIDigital Technologies · JSS 3 – SSS 3Teach a computer to tell ripe plantain from unripe by giving it labelled examples, then test how accurate it is.