02-data-types.py
python-programming / programming-foundations  ·  Lesson 02
Lesson 02 — Programming foundations

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.

data-types-predict.py
age = 14
height = 1.72
member = True
initial = "A"
name = "Alex"
print(type(age), type(height), type(member), type(initial), type(name))
Predict before running
  • 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.

data-types-investigate.py
items = 6
price = 2.75
in_stock = False
code = "B"
product = "Book"
Investigate the code
  • Choose the most suitable general data type for each value.
  • Explain why code is 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 typePython representationExample
Integerint42
Realfloat3.14
BooleanboolTrue or False
Characterone-character str"A"
Stringstr"Computer"
Python has no separate character typeA single character such as "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.

OCR focusChoose suitable data types for a scenario and use them practically. Know that OCR Real corresponds to Python float, and OCR Character is normally represented by a one-character Python str.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
IntegerintWhole numbers such as 14 or -3.
RealfloatNumbers that may contain a fractional part, such as 4.75.
BooleanboolExactly two logical values: True or False in Python.
Characterstr of length 1Python has no separate char type.
StringstrText containing zero or more characters.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

A profile can contain several different types at the same time.

lesson-02-worked.py
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))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
Work through it line by line
  • username is a string because it contains text.
  • age is an integer because it is a whole number.
  • rating is a float because it contains a decimal point.
  • verified is Boolean because it stores a true/false state.
  • initial is 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.

lesson-02-example-2.py
ticket_number = 1042
price = 12.50
sold_out = False
zone = "B"
Why this example matters
  • 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 float is suitable.
  • A yes/no state is Boolean.
  • A single zone letter corresponds to OCR Character, represented by str in Python.

Common mistakes

Avoid these errors
  • Writing Boolean values in quotes: "True" is a string, not a Boolean.
  • Saying that Python has a char type. It does not.
  • Assuming every number-looking value should be numeric; some identifiers are better stored as strings if arithmetic is never needed.
Exam-safe warningOCR uses the term Real. In Python, the practical equivalent you will normally use is float.

Exam-ready checklist

You should now be able to
  • 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.