Programming (blocks and Python)

KS3 Computing revision notes, key terms and practice questions.

Variables and data types

  • A variable is a named store for data that can change: score = 0. A constant is a value that doesn't change.
  • Data types: integer (a whole number, such as 7), float or real (a decimal, such as 2.5), string (text, in quotes, such as "Sam") and Boolean (True or False).
  • Input and output in Python: name = input("What is your name? ") and print("Hello", name).
  • input() always gives a string, so use int() to turn it into a whole number: age = int(input("How old are you? ")).

Selection and iteration

  • Selection: if score >= 50: … elif … else: … In Python, indentation shows which lines belong inside the if statement.
  • A FOR loop repeats a set number of times: for i in range(5): repeats 5 times, with i going from 0 to 4.
  • A WHILE loop repeats while a condition is true, for example until the user guesses the right answer.
  • Block-based languages such as Scratch use the same ideas, with blocks instead of typed code.

Operators and lists

  • Arithmetic: + − * /, // (whole-number division), % (remainder) and ** (power). 17 // 5 is 3 and 17 % 5 is 2.
  • Comparison: == (equal to), != (not equal to), <, >, <= and >=. Logic: and, or, not.
  • A list stores several items: fruits = ["apple", "pear"]. Positions (indexes) start at 0, so fruits[0] is "apple".

Errors and testing

  • A syntax error breaks the rules of the language, such as a missing bracket, so the program won't run.
  • A logic error lets the program run, but it gives the wrong result.
  • Test with normal data (typical values), boundary data (at the edge of what is allowed) and invalid data (values that should be rejected).

Key terms

Variable
A named store for data that can change.
Constant
A value that doesn't change while a program runs.
Integer
A whole number.
Float
A number with a decimal point (also called real).
String
Text, written inside quotation marks.
Boolean
A value that is either True or False.
FOR loop
A loop that repeats a set number of times.
WHILE loop
A loop that repeats while a condition is true.
List
A data structure that stores several items in order.
Index
The position of an item in a list, starting at 0.
Syntax error
A mistake that breaks the rules of the programming language.
Logic error
A mistake that makes a program give the wrong result.

Practise Programming (blocks and Python): 15 questions