All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Data types and type conversion

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Every value has a data type, which decides what you can do with it. Many Unit 2 errors come from using the wrong type.

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

Practise this lesson

The data types (spec 8.1.1)

TypePythonExamplesNotes
Integerint42, -7, 0Whole numbers of any size
Realfloat3.5, -0.25, 2.0Numbers with a decimal point; stored approximately
BooleanboolTrue, FalseCapital T and F
Stringstr"hello", "42", ""A sequence of characters in quotes; immutable
Characterstr of length 1"A", "7", " "Python has no separate char type

Converting between types

int(), float(), str() and bool() convert values; type() tells you a value's type.

print(int("25") + 5)
print(int(7.9))
print(float("2.5") * 2)
print(str(19) + "99")
print(bool(0), bool(3), bool(""), bool("no"))
print(type(3), type(3.0), type("3"))

Output:

30
7
5.0
1999
False True False True
<class 'int'> <class 'float'> <class 'str'>

int() on a float truncates (cuts off the decimal part). int("7.9") fails with ValueError: convert with float() first.

Characters and codes

ord() gives the character code of a single character and chr() turns a code back into a character. Capital letters A to Z are codes 65 to 90 and lower case a to z are 97 to 122.

print(ord("A"), ord("a"), chr(66))
letter = "c"
print(chr(ord(letter) - 32))

Output:

65 97 B
C

Choosing a type

Use int for counts and indexes, float for measurements and money calculations, bool for flags, str for text and for 'numbers' that are identifiers, such as phone numbers or postcodes, which can start with 0 and are never used in arithmetic.

Worked example

A user types 3.75 into input(). How do you get the whole number of pounds and the number of pence as integers?

  1. value = float("3.75") gives 3.75
  2. pounds = int(value) truncates to 3
  3. pence = round(value * 100) % 100 gives 75

Answer: pounds = 3, pence = 75

Key idea: int, float, bool and str are the core types; a character is a one-character string; convert with int(), float(), str(), bool(); int() truncates; ord() and chr() link characters and codes.

Check yourself

  1. What is displayed when this code runs? ```python print(int(9.99)) ```
    Show answer

    9. int() truncates towards zero, so 9.99 becomes 9.

  2. What is displayed when this code runs? ```python a = "5" b = "3" print(a + b) ```
    • 8
    • 53
    • 5 3
    • Error
    Show answer

    53. Both are strings, so + joins them: 53.

  3. Which exception is raised when this code runs? ```python n = int("4.5") ```
    • TypeError
    • ZeroDivisionError
    • NameError
    • ValueError
    Show answer

    ValueError. "4.5" is a string, but not a valid integer, so int() raises ValueError. Use float() first.

  4. What is displayed when this code runs? ```python print(bool(""), bool("False")) ```
    • False True
    • False False
    • True True
    • True False
    Show answer

    False True. An empty string is False; any non-empty string, even "False", is True: False True.

  5. What is displayed when this code runs? ```python print(ord("D") - ord("A")) ```
    Show answer

    3. Codes are consecutive: 68 - 65 = 3.

  6. What is displayed when this code runs? ```python print(chr(ord("m") - 32)) ```
    Show answer

    M. Lower case codes are 32 more than upper case ones, so this gives M.

  7. What is displayed when this code runs? ```python x = 10 / 5 print(type(x)) ```
    • <class 'int'>
    • <class 'str'>
    • <class 'float'>
    • <class 'bool'>
    Show answer

    <class 'float'>. / always produces a float, even when the answer is whole: <class 'float'>.

  8. Which data type is most suitable for storing a mobile phone number such as 07700900123?
    • Integer
    • Real
    • Boolean
    • String
    Show answer

    String. It starts with 0, which an integer would lose, and you never do arithmetic on it.

Practise with instant feedback

Sources

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