Data Types
Computers are not humans. A computer does not inherently know that "42" is a number. You must explicitly declare the correct Data Type for every piece of information.
The 5 Core Types
1. Integer
A whole number, positive or negative. Cannot contain fractions.
score = 42
2. Real / Float
A decimal or fractional number.
price = 19.99
3. Boolean
A logical value that can ONLY be True or False.
game_over = False
4. Character
A single alphanumeric letter, number, or symbol.
grade = "A"
5. String
A sequence of characters joined together. Everything inside quote marks is a String, even if it looks like a number!
password = "Pass123!"
Examiner's Eye - The Number Trap!
In an exam, if asked what data type an age or score is, never write "Number". 'Number' is not a specific data type. You will score zero marks. You must explicitly state whether it is an Integer or a Real.
Casting Simulator
Casting is converting a variable from one data type to another. The most common use case is when accepting user input. By default, input() always captures data as a String. You cannot do mathematics on strings! They must be cast to Integers.
Click the Cast button to convert the text '42' into the mathematical integer 42.
Check Your Understanding
1. What data type should be used to store a temperature reading like 36.5?
2. The variable age is currently storing the string "15". Why must we cast it before running age = age + 1?
3. I have a variable phoneNumber = "07700900077". What data type is this?
Written Exam Scenario (AO2/AO3)
Stretch (Grade 9)"Write a single line of pseudocode (or Python) that asks a user to input their dog's weight in kilograms (which may include decimals), and stores it using the most appropriate data type." (2 marks)
Mark 1 for successfully using an
input() function assignment.Mark 2 for explicitly casting the input to a
float() or real() because weight includes decimals. (Casting to an int() would incorrectly truncate decimal data and lose the second mark).