N3/N4  ·  SDD  ·  Software Design & Development

Variables, Data Types and Simple Arithmetic

National 3 & National 4 Computing Science Lesson SDD1 of 10 Approx 55 min Python & IDLE required — see SDD0
Learning intentions
  • Understand what a variable is and why programs use them
  • Know Python's basic data types: integer, float, string, boolean
  • Use arithmetic operators to calculate and store values
Success criteria
  • I can create a variable and assign it a value
  • I can identify the data type of a value
  • I can write a program that performs a calculation using variables
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. Which of these is a whole number, with no decimal point?

2. What is 10 divided by 3, written as a decimal (roughly)?

3. True or false: the word "Hello" is a whole number.

Key vocabulary

Variable
A named container that stores a value while a program runs. The value can change ("vary") as the program runs.
Data type
The kind of value being stored — for example a whole number, a decimal, or text.
Integer
A whole number, with no decimal point. In Python: int. Example: 12.
Float (real)
A number with a decimal point. In Python: float. Example: 3.5.
String
A piece of text, always written inside quotation marks. In Python: str. Example: "Hello".
Boolean
A value that is only ever True or False. In Python: bool.
Assignment
Storing a value in a variable using the = symbol, e.g. age = 13.
Operator
A symbol that performs an action on values, such as + for addition.

Variables, Data Types and Simple Arithmetic

What is a variable?

A variable is a named container that stores a value while your program is running. Instead of writing the same number or piece of text over and over, you give it a name and refer to that name instead. In Python, you create a variable by choosing a name and using the = symbol to store a value in it — this is called assignment.

For example, age = 13 creates a variable called age and stores the value 13 inside it. From that point on, wherever your program uses age, it means "whatever value is currently stored there." Variables are called that because the value can change — later in the same program you could write age = 14 and the old value would be replaced.

Variable names must follow some rules: they cannot start with a number, cannot contain spaces, and cannot use most punctuation. Good variable names describe what they store — total_cost is much clearer than x.

Data types in Python

Every value in Python has a data type, which tells the program what kind of value it is and what can be done with it. The four data types you need for N3/N4 are:

Data typePython nameExampleUsed for
Integerint12Whole numbers — counts, ages, scores
Float (real)float3.5Numbers with decimal places — prices, measurements
Stringstr"Hello"Text — names, messages, labels
BooleanboolTrueYes/no, on/off type values

You can check a value's data type using Python's built-in type() function, for example type(age). Choosing the right data type matters: a number written with quotation marks around it, like "12", is a string, not an integer — even though it looks like a number. Python will not let you do arithmetic with it until it is converted.

Arithmetic operators

Python uses symbols called operators to perform calculations on numbers stored in variables:

OperatorMeaningExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
//Floor (whole-number) division7 // 23
%Modulus (remainder)7 % 21

Notice that / always gives a float, even when the answer is a whole number, while // throws away anything after the decimal point. % is useful whenever you need to know what is "left over" — for example, checking whether a number is even by testing number % 2 == 0.

Order of operations

Python follows the same order of operations you use in maths (often remembered as BIDMAS/BODMAS): brackets first, then multiplication/division, then addition/subtraction. 2 + 3 * 4 evaluates to 14, not 20, because the multiplication happens first. Use brackets to force a different order: (2 + 3) * 4 evaluates to 20.

Worked examples

Example 1 — creating and printing a variable

A program needs to store a pupil's age and display it.

1
Choose a sensible variable name: age.
2
Assign a value: age = 13. This creates the variable and stores 13 in it.
3
Display it: print(age) outputs 13. Note there are no quotation marks around age in the print() line — that would print the letters a-g-e instead of the value stored inside it.
Example 2 — checking a data type

A programmer isn't sure whether a value has been stored as a number or as text.

1
Given the line score = "45" — note the quotation marks.
2
Run print(type(score)). Python outputs <class 'str'>, confirming this is a string, not a number, despite looking numeric.
3
If score needs to be used in a calculation, it must first be converted using int(score) or float(score).
Example 3 — arithmetic with variables

A program calculates the total cost of 3 items priced at £2.50 each.

1
price = 2.50 and quantity = 3 — two variables, one float and one integer.
2
total = price * quantity stores the result of the calculation, 7.5, in a new variable called total.
3
print(total) outputs 7.5. The calculation happens once, on the right-hand side of the =, before the result is stored.
Now you try

A recipe scales its ingredients based on how many people it serves. It uses rice_per_person = 200 (grams) and people = 4, then calculates total_rice = rice_per_person * people.

Answer the following:

  1. What data type is rice_per_person?
  2. What value will total_rice hold after the calculation runs?
  3. If people had instead been written as people = "4", what would go wrong when the program tried to calculate total_rice?
  1. rice_per_person is an integer (int) — it is a whole number with no decimal point.
  2. total_rice would hold 800 (200 × 4).
  3. Python would raise an error, because "4" is a string, not a number — you cannot multiply a number by a piece of text in Python. The program would need to convert it with int(people) first.
Common mistakes
Storing a number with quotation marks around it. total = "10" creates a string, not an integer, even though it looks like a number. Arithmetic on it will fail until it is converted.
Mixing text and numbers when printing. print("Age: " + age) causes an error if age is an integer — Python cannot join text and numbers with + directly. Convert first with str(age), or separate with a comma: print("Age:", age).
Invalid variable names. Names like 2total (starts with a number) or total cost (contains a space) will cause errors. Use total_cost instead.
Confusing / and //. 7 / 2 gives 3.5, but 7 // 2 gives 3. Choosing the wrong one gives a technically-running program with the wrong answer, which is harder to spot than an error message.
Tip

N4 pupils: it isn't enough to just use variables correctly — you need to be able to explain the purpose of the data type you chose for each variable in your evidence. Practise saying, out loud, "I used a float here because the value needs decimal places" rather than just writing the code.

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.

1. Which data type would best store a pupil's exam mark out of 100? TYPE 1

2. What is the result of 7 // 2 in Python? TYPE 1

3. Which of these is a valid Python variable name? TYPE 1

4. What data type is the value True? TYPE 1

5. What is 10 % 3 in Python? TYPE 1

6. In your own words, explain what a variable is and give one example of why a program might need one. TYPE 2 N4

A variable is a named container that stores a value while a program runs, so the value can be reused or changed without rewriting it every time. For example, a quiz program could use a variable called score to keep track of how many questions a pupil has answered correctly, updating it each time instead of storing a separate value for every question.

7. A programmer wrote age = "13" instead of age = 13. Explain what data type this creates and why it could cause a problem later in the program. TYPE 2 N4

Writing age = "13" creates a string, not an integer, because the value is inside quotation marks. This could cause a problem later if the program tries to use age in a calculation, such as adding 1 to it — Python cannot perform arithmetic directly on a string, so the program would raise an error unless age is first converted using int(age).

8. Practical: In IDLE, create a new file called sdd1_arithmetic.py. Write a program that stores two numbers in variables, adds them together, and prints the result. Test it with 5 and 3, then with 12 and 8. Record your output below. TYPE 3

num1 = 5
num2 = 3
total = num1 + num2
print(total)


Expected output: 8, then (after changing the values) 20.

9. Practical: In sdd1_arithmetic.py, extend your program to also calculate and print the average of the two numbers. Test with the same values as Q8 and record your output. TYPE 3

average = total / 2
print(average)


Expected output for 5 and 3: total 8, average 4.0. For 12 and 8: total 20, average 10.0. Note the average always shows as a float, since / always produces a decimal result.
Teacher notes — Shift+T to hide

Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.

Key misconception to address: pupils very often type numbers in quotation marks out of habit from other subjects, or because IDLE's input() always returns a string (which is covered next lesson) — reinforce the "quotation marks mean text" rule early and often.

Live demo suggestion: deliberately write print("Total: " + total) where total is an integer, run it live, and let pupils read the TypeError message before explaining it.

Extension idea (S5 / N5-bridging, non-assessed): introduce naming conventions used at N5 (e.g. constants in capitals) as a "look ahead," kept clearly separate from the assessed task set.

Assessment standards covered: N3 O2.1; N4 O1.2, O1.3, O2.2.