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 casting
2 age = input("How old are you? ")
3 print(age + 1)
4
5 # Version 2: cast with int
6 age = input("How old are you? ")
7 age = int(age)
8 print(age + 1)
agedata 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.

1 / 5

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.

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

Exam line: input returns a string, even if a number is entered. Cast it with int before you use it in a calculation.

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.

5 of 5 still to sort.

Some of these are sneakier than they look.

Computer Science · Data types

Did int do its job?

Kai writes this program, then types 2 at the prompt: lives = input("Lives: ") int(lives) print(lives + 1)

Kai used int, so he expects 3. What actually happens on the last line? Pick the idea closest to yours.
How sure are you?

Computer Science · Data types

One cast short

This program should add up a player's scores from two rounds. Instead it stops with an error. Which line is to blame?

A student's program — which line goes wrong?

Computer Science · Data types

Put it in writing

A program uses input to read two numbers from the user, then adds them together. Explain why the program must use the int function before the two numbers are added. [3 marks]

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

Exam line: One idea per mark: input gives a string; strings can't be used in calculations; int casts the string to an integer.

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

1Data types let Python store and process information correctly; an integer stores whole numbers, such as 3.
2input returns a string, even when a number is entered, and you can't do calculations with strings.
3Casting converts a value from one data type to another: int returns a new integer value made from the string.
4Cast input with int (or float) before using it in a calculation, and store the result.

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?
To store and process information correctly.
What does an integer store?
Whole numbers, for example 3.
Is 5.3 an integer?
No. It isn't a whole number.
What data type does input return if the user types a number?
A string. input defaults to returning a string, even if a number is entered.
Can you do calculations with strings?
No. Cast them to integers or floats first.
What is casting?
Converting a value from one data type to another.
Which function casts a value to an integer?
int
What does int do with the string it is given?
Converts it to an integer and returns the new integer value.
Which two functions can cast input so it can be used as a number?
int or float.
Why write age = int(age) rather than just int(age)?
int returns a new value. Without storing it, age is still a string.

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 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.