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().
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 |
, |
| Floating point | float |
, |
| 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 . 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 ; 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 . 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 ; you cannot have part of a student.
(c) A price has a fractional part, so it uses a float (float), for example .
(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
- Use Python selection with if, elif and else, build conditions with comparison and logical operators, and order branches correctly
A focused answer to the O-Level Computing point on Python selection. Using if, elif and else, comparison operators, combining conditions with and, or and not, and ordering branches so each value falls into the right one.
- Use Python for loops with range and while loops with a condition, including accumulators and avoiding infinite loops
A focused answer to the O-Level Computing point on Python loops. Using for with range for a known count, while with a condition for an unknown count, building accumulators, and avoiding infinite loops.
- Define and call Python functions with parameters and return values, and explain the benefits of using functions
A focused answer to the O-Level Computing point on Python functions. Defining functions with def, passing parameters, returning values, the difference between a parameter and an argument, and why functions make programs reusable.
- Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing
A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.
- Explain why computers use binary, describe place value in binary and hexadecimal, and state why hexadecimal is used as shorthand for binary
A focused answer to the O-Level Computing point on number systems. Why computers use binary, place value in binary and hexadecimal, the meaning of bits and bytes, and why hexadecimal is a compact shorthand for binary.