Edexcel International Advanced Level Computer Science, point 8.2.1
You must be able to select and use appropriate methods for handling numeric data. (Check the PLS for the exact functions allowed.)
a. Rounding
Rounding replaces a number with a nearby simpler value, usually to a number of decimal places or to a whole number.
print(round(2.71828, 3)) # 2.718
print(round(2.5)) # 2 (rounds half to even!)
print(round(3.5)) # 4
print(round(1234, -2)) # 1200 (to the nearest hundred)
round(x, n)rounds to decimal places. A negative rounds to tens, hundreds, and so on.- Python rounds exactly halves to the nearest even number ("banker's rounding"):
round(2.5)is 2 andround(3.5)is 4. Many people expect 3 for 2.5, so check the rules in the question. - Formatting for display only (the value isn't changed):
price = 9.8765
print(f"{price:.2f}") # 9.88 (still stores 9.8765)
- Floating-point numbers are stored approximately (2.1.4), so
round(2.675, 2)gives 2.67, not 2.68. For money, use whole pence (integers) to avoid errors.
Rounding up and down in a particular direction:
import math
print(math.ceil(3.2)) # 4 (always up)
print(math.floor(3.8)) # 3 (always down)
b. Truncation
Truncation cuts off the fractional part and doesn't round. The number moves towards zero.
print(int(3.9)) # 3
print(int(-3.9)) # -3 (towards zero)
import math
print(math.trunc(3.9)) # 3
| Value | round() |
int() (truncate) |
math.floor() |
math.ceil() |
|---|---|---|---|---|
| 3.7 | 4 | 3 | 3 | 4 |
| 3.2 | 3 | 3 | 3 | 4 |
| -3.7 | -4 | -3 | -4 | -3 |
- Integer division
//rounds down (floor):7 // 2is 3, and-7 // 2is -4. - Uses: whole items only (how many boxes fully filled), removing decimals from a value.
c. Randomisation
Generating numbers that are unpredictable, using the random module.
import random
# whole number from 1 to 6 (both included)
print(random.randint(1, 6))
print(random.random()) # real from 0 up to (not including) 1
print(random.uniform(1.0, 5.0)) # real between 1.0 and 5.0
print(random.choice(["a", "b"])) # pick a random item
nums = [1, 2, 3, 4]
random.shuffle(nums) # shuffle in place
print(random.sample(nums, 2)) # 2 different items
randint(a, b)includes both ends.- These are pseudo-random numbers, made by an algorithm from a seed. The same seed gives the same sequence:
random.seed(42)
print(random.randint(1, 100)) # always the same result with seed 42
- Uses: dice and card games, simulations, sampling, shuffling, test data. Not suitable for security (use the
secretsmodule for passwords and keys).
Example: a card game (cards worth 1 to 13)
import random
total = 0
for _ in range(3):
card = random.randint(1, 13)
print("Drew", card)
total = total + card
print("Total:", total)
Other maths functions
The math module (in the PLS) has more functions:
import math
print(math.sqrt(49)) # 7.0
print(math.pow(2, 5)) # 32.0 (always a float)
print(math.pi) # 3.141592653589793
print(math.fabs(-4.5)) # 4.5 (absolute value, as a float)
Dates and times
The datetime module stores and formats dates, and time measures time.
from datetime import datetime, date
import time
today = date.today() # today's date
now = datetime.now() # date and time now
d = datetime.strptime("03/12/2026", "%d/%m/%Y") # text -> date
print(d.strftime("%d %B %Y")) # 03 December 2026
print(d.strftime("%A")) # Thursday
start = time.time() # seconds since 1970
time.sleep(1) # pause for 1 second
print(round(time.time() - start)) # 1
strptime(text, format)reads text as a date;strftime(format)formats a date as text.- Format codes:
%dday (03),%mmonth number (12),%Yfour-digit year,%Bmonth name (December),%Aday name (Thursday),%H:%Mhours and minutes. - The format given to
strptimemust match the text exactly, or it raises aValueError.
d. Data type conversions
Moving between integer, real and string (8.1.1):
print(int("42")) # 42
print(float("3.5")) # 3.5
print(str(7)) # '7'
print(float(3)) # 3.0
print(int(3.99)) # 3 (truncates)
print(int(round(3.99))) # 4 (round first, then convert)
print(int("3.5")) # ValueError! convert via float first:
print(int(float("3.5"))) # 3
int("3.5")fails: go throughfloatfirst.- Check the conversion works with
try/except ValueError(7.1.1). - Bases:
int("1010", 2)is 10, andbin(10)is'0b1010',hex(255)is'0xff'.
Worked example
Task: a shop sells items at £3.99. A customer buys a random number of items from 1 to 10. Work out the total, rounded to 2 decimal places, the number of whole £5 vouchers they earn (one per £5 spent), and display them.
import random
PRICE = 3.99
quantity = random.randint(1, 10) # randomisation
total = round(quantity * PRICE, 2) # rounding
vouchers = int(total // 5) # whole number of vouchers
print("Items:", quantity)
print("Total: £" + f"{total:.2f}")
print("Vouchers:", vouchers)
For quantity = 4: total , and 15.96 // 5 is 3.0, so 3 vouchers. int() converts the float result of // back to an integer.
Exam tip: know the difference between rounding and truncating: int(3.9) is 3 but round(3.9) is 4. Remember randint(a, b) includes both ends. Say whether you are rounding the value or just the display. Use try/except around conversions of user input.
Check yourself
What is the difference between round(3.7) and int(3.7)?
round(3.7) gives 4 (the nearest whole number). int(3.7) gives 3 (it truncates the fraction).
What range of values can random.randint(1, 6) return?
Whole numbers from 1 to 6, including both 1 and 6.
Why does int("3.5") cause an error, and how can it be fixed?
The string isn't a whole number, so int raises ValueError. Convert with float first: int(float("3.5")).
What does a seed do in a random number generator?
It sets the starting point, so the same seed always gives the same sequence of pseudo-random numbers.