Data Types
An integer is a whole number with no fractional part. Every integer is either positive, negative or zero, so a score of 3 in a quiz, a temperature of -4 °C and a total of 0 are all integers.
An integer is the right data type when a value can only sensibly be a whole number: you cannot score half a goal or swim two-thirds of a length. In a program an integer is written without a decimal point, and a minus sign in front of a negative value does not stop it being an integer.
The decimal point matters: 8 is an integer, but 8.0 is a real even though its fractional part is zero, and the computer stores and handles the two differently. The point on the end changes the data type of the value, not just the way it looks.
| Value | Integer? | Why |
|---|---|---|
| 512 | Yes | A whole number written without a decimal point |
| -47 | Yes | Negative, but still a whole number |
| 3.25 | No | It has a fractional part, so it is a real |
| 12.0 | No | Written with a decimal point, so it is a real |
Here two integers are stored in variables and printed. A lap count can only be a whole number and the temperature is below zero, so an integer suits both.
# whole numbers only
laps = 12
laps_left = laps - 5
temperature = -3
print(laps_left)
print(temperature)Output:
7
-3 Edexcel GCSE Computer Science · Data Types