Data Types
Choose suitable types for whole numbers, real numbers, Boolean values and text.
Data types describe the kind of value being stored. Choosing the correct type matters because different operations are valid for different kinds of data.
Predict
Read the code first. Do not run it yet.
age = 14
height = 1.72
member = True
initial = "A"
name = "Alex"
print(type(age), type(height), type(member), type(initial), type(name))- Predict the values that will be printed.
- Match each variable to integer, real, Boolean, character or string.
- Which two values are both represented by Python strings?
Investigate
Use this code to explain exactly how the program works.
items = 6
price = 2.75
in_stock = False
code = "B"
product = "Book"- Choose the most suitable general data type for each value.
- Explain why
codeis a character conceptually even though Python stores it as a string. - Which values could be used directly in arithmetic?
General types and Python
| General data type | Python representation | Example |
|---|---|---|
| Integer | int | 42 |
| Real | float | 3.14 |
| Boolean | bool | True or False |
| Character | one-character str | "A" |
| String | str | "Computer" |
"A" is stored as a string of length 1.Choose the type to fit the data
Counts normally use integers, measurements may need real numbers, yes/no states use Booleans, and names or messages use strings.
Self-study guide
A data type describes the kind of value being stored and therefore what operations make sense for that value. OCR requires integer, real, Boolean, character and string data. Python represents these using int, float, bool and str.
Python has no separate character type. A single character such as "A" is a string of length one. In an OCR question, however, Character and String are still distinct conceptual data types, so you must be able to choose the appropriate one for a scenario.
float, and OCR Character is normally represented by a one-character Python str.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
Integer | int | Whole numbers such as 14 or -3. |
Real | float | Numbers that may contain a fractional part, such as 4.75. |
Boolean | bool | Exactly two logical values: True or False in Python. |
Character | str of length 1 | Python has no separate char type. |
String | str | Text containing zero or more characters. |
Worked example
A profile can contain several different types at the same time.
username = "Nova"
age = 15
rating = 4.7
verified = True
initial = "N"
print(type(username))
print(type(age))
print(type(rating))
print(type(verified))
print(type(initial))<class 'str'> <class 'int'> <class 'float'> <class 'bool'> <class 'str'>
usernameis a string because it contains text.ageis an integer because it is a whole number.ratingis a float because it contains a decimal point.verifiedis Boolean because it stores a true/false state.initialis still a Python string even though it contains only one character.
Another example
The best type depends on what the data means, not just how it looks on the screen.
ticket_number = 1042
price = 12.50
sold_out = False
zone = "B"- A ticket number is stored as an integer here because it is a numeric identifier in this simple example.
- A price may contain a fractional part, so
floatis suitable. - A yes/no state is Boolean.
- A single zone letter corresponds to OCR Character, represented by
strin Python.
Common mistakes
- Writing Boolean values in quotes:
"True"is a string, not a Boolean. - Saying that Python has a
chartype. It does not. - Assuming every number-looking value should be numeric; some identifiers are better stored as strings if arithmetic is never needed.
float.Exam-ready checklist
- Identify the data type of a given value.
- Choose and justify a suitable type for a scenario.
- Translate OCR Integer/Real/Boolean/Character/String to the Python types you use.
- Remember that a one-character Python string represents character data.
What you have learned
- Integer maps to Python
int. - Real maps to Python
float. - Boolean maps to Python
bool. - Character and string values are represented using Python
str.