GCSE · Computer Science · OCR · Spec J277
Data type: Integer
Type 15 into a Python program and ask it to add 1. You'd expect 16. Instead, the program stops with an error. So what's going on?
Computer Science · Data types
Why can't Python add 1 to your age?
Two versions of the same tiny program. Both ask your age. Only one can work out how old you'll be next year. Step through and watch the data type column.
The user types 15 both times. Before you press Next: what do you think the first print(age + 1) will show?
1 # Version 1: no casting2 age = input("How old are you? ")3 print(age + 1)45 # Version 2: cast with int6 age = input("How old are you? ")7 age = int(age)8 print(age + 1)
| age | data type of age |
|---|---|
| "15" | string |
Output
How old are you? 15
Step 1: The user types 15. Now look at the table: age holds "15", in quotes, and its data type is string. The input function returns what was typed as a string, even when it's a number.
Quotes in the table mean the value is a string. Keep an eye on the data type column: the calculation only works once it says integer.
Computer Science · Data types
Integer or not?
Sort each value. Is it an integer, or not an integer?
Still to sort
Integer (0)
A whole number, stored as a number.
Where the line is: It has to be a whole number AND be stored as a number. Digits that are really a string don't count.
Not an integer (0)
Not a whole number, or not stored as a number at all.
Where the line is: Looking like a number isn't enough. Check whether it's whole, and check where it came from.
Some of these are sneakier than they look.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- Python uses data types so it can store and process information correctly.
- An integer is the data type for whole numbers, like 3.
- A number with a decimal part, like 5.3, isn't a whole number, so it isn't an integer.
Have a goQuick sort: which one of 7, 7.5 and 0.7 is an integer?
7
Only 7 is a whole number. 7.5 and 0.7 both have a decimal part, and an integer stores whole numbers only.
- The input function returns what the user types as a string, even when they type a number.
- So typing 15 gives your program the string "15", not the integer 15.
Have a goJas types 12 at age = input("Age? ") and swears age now holds the number twelve. She'd bet her lunch on it. What does age really hold?
The string "12". Jas owes someone lunch.
input returns the data as a string even if a number is entered. Looking like a number doesn't make it an integer.
- You can't do calculations with strings.
- Casting means converting a value from one data type to another.
- The int function takes a string, converts it to an integer and returns the new integer value.
- That returned integer can be used in calculations, so store it: age = int(age).
Have a gox = int("40") runs. What data type does x hold, and what is x + 2?
An integer, so x + 2 is 42.
int converted the string "40" and returned the integer 40, which x stores. Skip the int and x would be a string you can't calculate with.
- To use typed-in numbers in a calculation, cast them with int or float first.
The big picture
Python uses data types to store and process information correctly. An integer stores whole numbers, such as 3. The input function returns what the user types as a string, even when they type a number, and you can't do calculations with strings. Casting is converting a value from one data type to another: the int function takes the string, converts it to an integer and returns the new integer value, which can then be used in calculations. To use typed-in numbers in a calculation, cast them with int or float first.
Key points
Worked example
Problem
A cinema program asks how many tickets someone wants, then works out the cost at £6 per ticket. The user types 4. Write the program and say what each variable holds.
⚠ Watch out
Calculating straight after input. Typing 15 gives the program the string "15", so age + 1 fails. Two close cousins of the same slip: casting only one of the values in a calculation, and writing int(age) on its own line without storing the result.
Memory hook
Input speaks text. int makes it count. Whatever is typed arrives as a string; cast it with int and it can finally do sums.
Check yourself
Cover the page. Write a three-line program that asks for a number of laps and prints double it. Point to the line where casting happens: what data type is the value before it, and after?
Flashcards
(10)Why does Python use data types?
What does an integer store?
Is 5.3 an integer?
What data type does input return if the user types a number?
Can you do calculations with strings?
What is casting?
Which function casts a value to an integer?
What does int do with the string it is given?
Which two functions can cast input so it can be used as a number?
Why write age = int(age) rather than just int(age)?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
- Basic string manipulation (concatenation, slicing)
- Boolean operators AND, OR, NOT
- Casting
- Constants
- Data type: Boolean
- Data type: Character and string
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
- Local and global variables
- Operators (arithmetic and Boolean)
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.