All subjects · Computer Science · WCP04 Advanced Practical Programming and Problem-solving
Data types, variables, constants and type conversion
Edexcel IAL Computer Science · WCP04 Advanced Practical Programming and Problem-solving
Choosing the right type for each value, and converting safely between types, prevents a large share of the errors you will be asked to fix in the Unit 4 paper.
The four primitive types
- int: whole numbers of any size, such as 42 or -7.
- float: numbers with a fractional part, such as 3.14. Stored in binary, so most decimals are approximations.
- str: text, in quotes. Strings are immutable: methods like upper() return a new string.
- bool: True or False.
type(x) tells you a value's type. Python is dynamically typed: a variable's type is whatever it currently holds, so be deliberate about what you store.
Variables and constants
- An identifier must start with a letter or underscore, may contain letters, digits and underscores, is case sensitive, and must not be a keyword such as class or for.
- Use descriptive snake_case names: total_price, not tp.
- Python has no true constants. By convention a value that must not change is written in UPPER_CASE at the top of the program, such as VAT_RATE = 0.2, and is never reassigned. Named constants remove 'magic numbers' and mean a change is made in one place.
Converting between types
int(), float(), str() and bool() convert values. Watch for these traps:
- int() on a float truncates towards zero; it does not round.
- int('3.7') raises ValueError: convert with float() first.
- bool() is False only for 'empty' values such as 0, '' and []; any non-empty string, even 'False', is True.
- round() uses banker's rounding (halves go to the even neighbour), and floats like 2.675 are stored slightly below the half.
print(type(7), type(7 / 7), type("7"), type(7 > 3))
print(int(3.99), int(-3.99), float("2.5"), str(42) + "!")
print(bool(0), bool(""), bool("False"), bool([0]))
print(round(2.5), round(3.5), round(2.675, 2))Output:
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
3 -3 2.5 42!
False False True True
2 4 2.67The PLS and real Python. The PLS describes round(x, n) as using 'the 0.5 rule', but Python 3 itself rounds an exact half to the even neighbour, as the last line shows. Your code is run in real Python, so predict what Python does; if a question needs halves always rounded up, use the decimal module with ROUND_HALF_UP (see the floating-point lesson). The PLS also describes int() as removing the fractional part, which matches Python: int(-3.99) is -3.
Worked example
A user types '19.99' for a price and '3' for a quantity. Write code that prints the total cost to 2 decimal places.
- input() always returns a str, so convert: float('19.99') and int('3').
- Multiply, then format with an f-string to 2 decimal places.
- ```python price = float("19.99") quantity = int("3") print(f"Total: {price * quantity:.2f}") ```
- Output: ``` Total: 59.97 ```
Answer: Convert input before arithmetic; use a format specifier, not round(), for display.
Check yourself
- What does this program print?
```python
print(int(-7.8), int(7.8))
```
Show answer
-7 7. int() truncates towards zero, so -7.8 becomes -7 and 7.8 becomes 7. Output: `-7 7`
- What does this program print?
```python
print(round(0.5), round(1.5), round(2.5))
```
Show answer
0 2 2. Python rounds halves to the nearest even number: 0, 2, 2. Output: `0 2 2`
- What does this program print?
```python
print(bool("False"), bool(0.0))
```
- False False
- True False
- True True
- False True
Show answer
True False. Any non-empty string is True, even 'False'. 0.0 is zero, so False. Output: `True False`
- What does this program print?
```python
print("3" * 3, str(3) + "3", int("3") + 3)
```
Show answer
333 33 6. '3' * 3 repeats the string: 333. str(3) + '3' joins strings: 33. int('3') + 3 adds numbers: 6. Output: `333 33 6`
- What does this program print?
```python
print(type(8 / 2).__name__)
```
Show answer
float. / always produces a float, even when the result is whole (4.0). Output: `float`
- Which of these is a valid Python identifier?
- 2nd_total
- total-score
- _total
- class
Show answer
_total. _total starts with an underscore and contains only letters and underscores.
- How should a value that never changes, such as the number of judges (5), be written in a Python program?
- NUM_JUDGES = 5 at the top of the program, and the name used everywhere
- The number 5 typed wherever it is needed
- num_judges = 5 inside each function that uses it
- A global variable that functions update
Show answer
NUM_JUDGES = 5 at the top of the program, and the name used everywhere. Python's convention for constants is an UPPER_CASE name defined once. It removes magic numbers and makes changes safe.
- What happens when Python runs int("4.0")?
- It returns 4
- A ValueError is raised
- It returns 4.0
- It returns '4'
Show answer
A ValueError is raised. int() can only convert a string that looks like a whole number. Use int(float('4.0')).
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification, Issue 1 (March 2026)
- Pearson Edexcel IAL Computer Science Unit 4 Programming Language Subset (PLS)
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 and Unit 4)
Last updated 8 November 2026. Spotted a mistake? Tell us.