Edexcel International Advanced Level Computer Science, point 17.1.2
The theory is in 7.1.2 (variables and constants) and 8.1.1 (data types), and 18.1.1 covers floating-point methods in depth. Here the skill is selecting the right ones for a larger problem. Check the PLS for what is allowed.
Choosing a data type
| Data | Type | Why |
|---|---|---|
| Counts, ages, quantities | int |
Exact whole numbers |
| Measurements (height, temperature) | float |
Fractions needed, small rounding errors are acceptable |
| Money | int (pence) or Decimal |
Floats can't store 0.1 exactly (11.3.1) |
| Names, addresses, text | str |
Characters |
| Phone numbers, IDs, postcodes | str |
Not used in arithmetic, may start with 0 |
| Yes/no, flags | bool |
Two states |
| Dates and times | datetime (library) |
Correct date arithmetic |
| One value from a fixed set | str or constants |
Readable ("admin") |
| A collection | list, tuple, dict, set (17.1.3) | Depends on use |
| Nothing / unknown | None |
Shows "no value" |
Floats and money
print(0.1 + 0.2) # 0.30000000000000004
price_pence = 1999 # store money as whole pence
total = price_pence * 3
print(f"£{total / 100:.2f}") # £59.97
Type conversion
Convert deliberately and safely (8.1.1, 9.2.3):
def read_int(prompt, default=0):
try:
return int(input(prompt))
except ValueError:
return default
# 42 3.5 7 False 3
print(int("42"), float("3.5"), str(7), bool(""), int(3.9))
input()returns a string.int()truncates.int("3.5")fails (useint(float("3.5"))).- Convert as early as possible, then work in the right type.
Choosing variable names
- Describe the meaning (
total_price,is_valid), not the type (list1). snake_casefor variables,UPPER_CASEfor constants (9.2.1).- One purpose per variable.
- Keep the scope small (local, not global, 9.1.2).
Using constants
Name every fixed value that has a meaning:
VAT_RATE = 0.2
MAX_ATTEMPTS = 3
STATUS_ACTIVE = "active"
- One place to change it.
- Explains the code.
- A group of related constants can be put in a dictionary, a tuple or a class.
Worked example
Task: a program records a customer's name, a membership number (with leading zeros), the number of items bought, the price of each (in pounds and pence), and whether they are a member, then works out the total with a 10% discount for members.
MEMBER_DISCOUNT = 10 # constant: percent
name = "Amy Lee" # str
member_no = "00472" # str: leading zeros matter
items = 3 # int: whole count
# int: pence, avoids float errors
price_pence = 1299
is_member = True # bool
total_pence = items * price_pence # int arithmetic
if is_member:
total_pence = total_pence * (100 - MEMBER_DISCOUNT) // 100
print(f"{name} ({member_no}): £{total_pence / 100:.2f}")
Output: Amy Lee (00472): £35.07 (, pence).
Why these types: the membership number is a string (it would lose its leading zeros as an integer), money is in whole pence (no rounding errors), and the discount is a named constant.
Exam tip: choose the type from how the data is used, and say why (phone number: string; money: integer pence or Decimal). Name constants. Convert input() immediately and handle ValueError.
Check yourself
Why is a phone number stored as a string?
It isn't used in arithmetic and may start with a zero, which an integer would drop.
Why shouldn't money be stored as a float?
Floats can't represent many decimal fractions exactly, so rounding errors appear; use whole pence or Decimal.
What is the value of int(7.9)?
7, because int truncates.
Give two reasons to use a named constant
It explains the meaning of the value and it can be changed in one place.