GCSE · Computer Science · OCR · Spec J277
Casting
Type 1, type 2, add them up. The computer says 12. Your calculator never would, so what is going on? Can one tiny fix make it say 3?
Computer Science · Casting
Why does 1 + 2 make 12?
Step through the same tiny program twice: first with no casting, then with each input cast using int. Keep your eye on what a and b hold.
Imagine the user types 1, then 2. Before you step: what do you think print(a + b) will show?
1 # Run 1: no casting2 a = input()3 b = input()4 print(a + b)5 # Run 2: the fix6 a = int(input())7 b = int(input())8 print(a + b)
| a | b |
|---|---|
Output
Step 1: Run 1 has no casting. In the trace table, quote marks around a value mean it is a string. The user is about to type 1, then 2.
Watch the table: quote marks mean a string, no quote marks means a number. The only thing that changes between the two runs is the casting.
Computer Science · Data types
What type is it?
Which data type does each value have, and which cast could change it?
Still to sort
Integer (0)
A whole number.
Where the line is: A whole number with no decimal point and no quote marks.
Real (floating point) (0)
A number with a decimal part.
Where the line is: A number written with a decimal point, such as 5.0. It is a number, so no quote marks.
String (0)
Text, shown in quote marks.
Where the line is: Anything inside quote marks is a string, even when it is made of digits.
A data type is the kind of data a value is. That decides what a computer can store and which actions can be performed on it. Sort each value by its type.
Predict, then check
One line, one guess. Commit before you peek.
A program runs x = float(input()) and then print(x). The user types 5. What gets printed?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Why a program can think 1 + 2 is 12, and the one-line fix that changes the data type so it gets 3.
What you need to know
- A data type defines the kind of data a computer can store, and the actions that can be performed on it.
- Casting means changing a value from one data type to another.
- In Python you cast with functions: str gives a string, int gives an integer and float gives a real (floating point) number.
Have a goA program holds the integer 9 and needs it as a string. Which function would you use?
str, as in str(9)
str is the function that returns a string. int would give an integer and float a real number.
- The input function hands back a string by default, even if the user types a number.
- To use typed input as a number, cast it, for example by putting the input function inside the int function.
Have a goA program has year = input() and the user types 10. Rewrite the line so year holds an integer.
year = int(input())
Putting input inside int casts the string the user typed into an integer straight away.
- Strings cannot be used for calculations. Adding two strings joins them, so two uncast inputs of 1 and 2 give 12, not 3.
- Casting to float gives a decimal result, so a whole number typed as 5 becomes 5.0. That may not be what you want if you need a whole number.
Have a goThe user types 9 into score = float(input()). Predict what score holds.
9.0
float gives a decimal result, so the whole number 9 becomes 9.0, which is not what you want if you need a whole number.
- Casting does not remove every error. A word cast to a number still causes an error when the program runs.
The big picture
A data type defines the kind of data a computer can store and the actions that can be performed on it. Casting changes a value from one data type to another. In Python, str, int and float cast to a string, an integer and a real (floating point) number. The input function gives back a string by default, even if a number is typed, so two uncast inputs of 1 and 2 add up to the joined string 12. Casting with int fixes that, float gives a decimal such as 5.0, and a word cast to a number still causes an error when the program runs.
Key points
Worked example
Problem
A baker's program asks how many cupcakes are in the oven, then prints how many there will be after 3 more are baked. The user types 4. Write the program and say what it prints.
⚠ Watch out
Believing that typing a number into input gives the program a number. It gives a string, which is why the cast is needed. The second slip is thinking that a cast makes a program safe. A word cast to a number still causes an error when the program runs.
Memory hook
Input hands you words, never numbers. Cast first, calculate second.
Check yourself
Cover the page. Say why two uncast inputs of 1 and 2 give 12, then write the line that reads a whole number from the user as an integer.
Flashcards
(12)What is a data type?
What is casting?
What does str do in Python?
What does int do in Python?
What does float do in Python?
What data type does the input function give back by default?
How do you read a typed whole number so you can calculate with it?
What does + do when both values are strings?
Two uncast inputs of 1 and 2 are added. What is printed?
A whole number entered as 5 is cast with float. What results?
The user types a word and the program casts it to a number. What happens?
Does casting remove every error from a program?
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
- 1D arrays
- 2D arrays
- Basic string manipulation (concatenation, slicing)
- Boolean operators AND, OR, NOT
- Constants
- Data type: Boolean
- Data type: Character and string
- Data type: Integer
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
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 13 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.