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 casting
2 a = input()
3 b = input()
4 print(a + b)
5 # Run 2: the fix
6 a = int(input())
7 b = int(input())
8 print(a + b)
ab

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.

1 / 8

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.

Step 1 of 8: 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..

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.

6 of 6 still to sort.

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?

Computer Science · Casting

What do you really think?

A program contains age = input(). The user types 15.

Which of these is closest to what you think is happening?
How sure are you?

Computer Science · Casting

Your turn to explain

A program asks for two whole numbers and prints their total. Its code is: a = input(), then b = input(), then print(a + b). The user types 4 and then 6, and the program prints 46 instead of 10. Explain why this happens and say how the code should be changed so that it prints 10. [3 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Data type = the kind of data a value is, which decides what can be stored and done with it.
2Casting = converting a value from one data type to another, using str, int or float.
3input always returns a string, so cast it before you calculate with it.
4Plus on two strings joins them. Plus on two numbers adds them.
5float(5) gives 5.0. Cast a word to a number and the program errors when it runs.

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?
It defines the kind of data a computer can store and the actions that can be performed on it.
What is casting?
Changing the data type of a piece of data from one type to another.
What does str do in Python?
It is a function that takes a value, processes it and returns it as a string.
What does int do in Python?
It is a function that takes a value, processes it and returns it as an integer.
What does float do in Python?
It is a function that takes a value, processes it and returns it as a real (floating point) number.
What data type does the input function give back by default?
A string, even if the user types a number.
How do you read a typed whole number so you can calculate with it?
Put the input function inside int, as in int(input()).
What does + do when both values are strings?
It joins them (concatenation) instead of adding. Strings cannot be used for calculations.
Two uncast inputs of 1 and 2 are added. What is printed?
12, because the two strings are joined, not added.
A whole number entered as 5 is cast with float. What results?
5.0, a decimal result. That may not be wanted if a whole number is needed.
The user types a word and the program casts it to a number. What happens?
It still causes an error when the program runs, because a word is not a valid number.
Does casting remove every error from a program?
No. Casting changes the data type, but a value that is not a valid number still causes an error.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.