Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do variables store data in Python, and what are the main data types?

Declare and use Python variables, identify the core data types (int, float, str, bool), and convert between them

A focused answer to the O-Level Computing point on Python variables. Assigning variables, the core data types int, float, str and bool, reading input as a string, and converting between types with int(), float() and str().

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to declare and use variables in Python, to know the core data types (integer, float, string and boolean), and to convert between them. The central idea is that a variable is a named box that holds a value, and the type of that value decides what you can do with it, so reading input and doing arithmetic often needs a deliberate type conversion.

The answer

Variables and assignment

A variable is a named place that stores a value. You create one by assigning to it with =:

score = 0
name = "Bala"

The name goes on the left, the value on the right. You can change a variable later by assigning a new value, and use it in expressions like score = score + 10.

The core data types

Python has four data types you must know:

Type Name Example
Integer int 4242, −7-7
Floating point float 3.143.14, 4.504.50
String str "hello"
Boolean bool True, False

An int is a whole number; a float has a decimal part; a str is text in quotes; a bool is either True or False.

Input always returns a string

The input() function always gives back a string, even if the user types digits. So age = input() makes age the text "15", not the number 1515. Doing arithmetic on it directly causes an error.

Converting between types

To convert a value, wrap it in the type's function:

n = int("15")      # string "15" -> integer 15
x = float("3.5")   # string "3.5" -> float 3.5
s = str(42)        # integer 42  -> string "42"

So to read a number from the user, convert the input straight away:

age = int(input("Enter age: "))

Examples in context

Example 1. A shop till. A till reads a quantity as an int and a unit price as a float, then multiplies them for the line total. Reading the quantity with int(input(...)) and the price with float(input(...)) ensures the multiplication is numeric, not a string error.

Example 2. A yes/no setting. A program stores whether dark mode is on as a bool (True or False). Using a boolean rather than the text "yes" makes the value easy to test directly in an if statement and impossible to mistype.

Try this

Q1. State the data type of the value "42" in Python. [1 mark]

  • Cue. A string (str), because it is in quotes, even though it looks like a number.

Q2. Write a line that reads a price (which may have decimals) from the user as a number. [2 marks]

  • Cue. price = float(input("Enter price: ")).

Q3. Explain why int(input("Age: ")) + 1 works but input("Age: ") + 1 does not. [2 marks]

  • Cue. int(...) converts the string to a number so it can be added to 11; without it, you try to add a string to an integer, which is a TypeError.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksA student writes `age = input('Enter your age: ')` and then `next_year = age + 1`, but the program crashes. (a) Explain why it crashes. (b) Rewrite the code so it works, and state the data type of `age` after your fix.
Show worked answer →

(a) input() always returns a string, so age holds text such as "15", not the number 1515. Trying to do age + 1 mixes a string and an integer, which Python cannot add, so it raises a TypeError and crashes.

(b) Convert the input to an integer with int() before doing arithmetic:

age = int(input('Enter your age: '))
next_year = age + 1
print(next_year)

After the fix, age is an int, so age + 1 works.

Markers reward identifying that input returns a string, the resulting type error, and the fix using int() so that age becomes an integer.

Original4 marksState the most suitable Python data type for each of the following, and give a reason: (a) a person's name, (b) the number of students in a class, (c) a price in dollars and cents, (d) whether a light is on.
Show worked answer →

(a) A name is text, so it uses a string (str), for example "Aisha".

(b) A count of students is a whole number, so it uses an integer (int), for example 3030; you cannot have part of a student.

(c) A price has a fractional part, so it uses a float (float), for example 4.504.50.

(d) On or off is a true/false value, so it uses a boolean (bool), either True or False.

Markers reward str for the name, int for the count, float for the price, and bool for the on/off state, each with a sensible reason.

Related dot points