Five data types
Every value in a program has a data type, which decides what can be done with it.
| Type | Holds | Examples |
|---|---|---|
| Integer | whole numbers | 17, -3, 0 |
| Real (float) | numbers with a decimal part | 8.50, 21.4 |
| Boolean | one of two values | True, False |
| Character | one single character | "B", "?" |
| String | text, any length | "LS1 4AP", "07700 900123" |
A phone number is a string: it starts with 0 and nobody adds phone numbers together.
Casting changes a value's type: str(7) gives the text "7", int("42") gives the number 42. Joining two strings with + sticks them together: "4" + "5" is "45", but 4 + 5 is 9.
A variable is a named place in memory whose value can change. A constant keeps the same value for the whole run, such as const VAT_RATE = 0.2, so it cannot be changed by mistake and is easy to update in one place.
Arithmetic, MOD and DIV
- DIV gives the whole number part of a division: 17 DIV 5 = 3. Python writes it
//. - MOD gives the remainder: 17 MOD 5 = 2. Python writes it
%. x MOD 2 == 0is the classic test for an even number.
Operators follow the same order as in maths: brackets first, then ^ (powers), then × ÷ MOD DIV, then + and -. So 3 + 4 * 2 is 11, and (3 + 4) * 2 is 14.
Change the numbers and predict each line before you run it on Brainlag Code
mins = 135
hours = mins DIV 60
left = mins MOD 60
print(str(hours) + " h " + str(left) + " min")mins ← 135
hours ← mins DIV 60
left ← mins MOD 60
OUTPUT INT_TO_STRING(hours) + " h " + INT_TO_STRING(left) + " min"mins = 135
hours = mins // 60
left = mins % 60
print(str(hours) + " h " + str(left) + " min")Comparisons and Boolean operators
Comparisons give True or False: == equal (AQA writes =), != not equal (AQA ≠), <, <=, >, >=.
- AND is true only if both sides are true.
- OR is true if either side is true (or both).
- NOT flips true and false.
Brackets show what NOT applies to: NOT (a > 3) AND b < 5 is different from NOT (a > 3 AND b < 5).
Try these in Python: Numbers and Boolean logic on Brainlag Code.
MOD and DIV
Divide, then split the answer into the whole number part (DIV) and the remainder (MOD).