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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The four primitive types

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

Converting between types

int(), float(), str() and bool() convert values. Watch for these traps:

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.67

The 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.

  1. input() always returns a str, so convert: float('19.99') and int('3').
  2. Multiply, then format with an f-string to 2 decimal places.
  3. ```python price = float("19.99") quantity = int("3") print(f"Total: {price * quantity:.2f}") ```
  4. Output: ``` Total: 59.97 ```

Answer: Convert input before arithmetic; use a format specifier, not round(), for display.

Key idea: int, float, str, bool; type() checks; int() truncates; int('3.7') fails; only empty values are False; round() halves to even; constants in UPPER_CASE.

Check yourself

  1. 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`

  2. 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`

  3. 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`

  4. 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`

  5. 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`

  6. 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.

  7. 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.

  8. 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')).

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.