String: str
Text such as "Hello". Quotes also make "123" a string: its digits are characters rather than a number to add to.
Loading…
Free GCSE Computer Science guide
Choose a suitable type, assign a value to a variable and trace what changes when the program updates it. Start with the workshop examples, then try the questions before opening their answers.
Content owner: Michael Print · Written for GCSE learners · Checked against official specifications
A variable gives a name to a value used by a program. Assigning a new value to that name changes what later statements use. A data type describes the kind of value and the operations that make sense for it.
In counter = 7, counter is the variable name and 7 is an integer value. In Python, you do not write a separate type declaration before making this assignment.
strText such as "Hello". Quotes also make "123" a string: its digits are characters rather than a number to add to.
intA whole number such as 7, 0 or -3. Use it for a count of completed tasks or the number of items in a list.
floatA floating-point number such as 7.5. GCSE questions often call this a real data type. 5.0 is also a float, even though its mathematical value is a whole number.
boolEither True or False. A name such as is_raining describes a yes-or-no state. Python's Boolean literals start with a capital letter.
Specifications also name the character type. Python has no separate character type: "A" is a string of length one. Use the terminology and syntax requested in an exam question.
message = "Hello"
number_text = "123"
counter = 7
temperature = 7.5
is_raining = True
print(number_text + "1") # 1231
print(counter + 1) # 8Adding two strings joins their text; adding two integers performs arithmetic. The first print outputs 1231, while the second outputs 8.
In counter = counter + 1, Python reads the current value of counter, adds one, then assigns the result back to that name. It does not claim that a number equals itself plus one.
counter = 0
counter = counter + 1
print(counter) # 1
counter = counter + 1
print(counter) # 2| Statement | Value after the statement |
|---|---|
| counter = 0 | 0 |
| counter = counter + 1 | 1 |
| counter = counter + 1 | 2 |
Give variables meaningful names, initialise them before reading their value and remember that names are case-sensitive: counter and Counter are different names.
Worked example
counter = 0 gives the counter its initial value.counter + 1, evaluates to 0 + 1, which is 1.counter refer to that result.print(counter) therefore displays 1.input() returns a string. If the user enters 14, the returned value is "14". int() converts a suitable integer string to an integer so arithmetic can use it.
age_text = input("Enter your age: ")
age = int(age_text)
next_age = age + 1
print(next_age)Entering 14 makes this program print 15. This example assumes a valid whole-number input. Text such as "fourteen" cannot be converted by int() and raises a ValueError; conversion does not automatically validate every possible input.
A short captioned explanation from Michael's Python fundamentals workshop. Use the examples on this page to practise the idea afterwards.
Strings store text, including quoted digits. Integers store whole numbers; floats represent numbers such as decimal measurements. Booleans represent true-or-false values.
A short captioned explanation from Michael's Python fundamentals workshop. Use the examples on this page to practise the idea afterwards.
A variable has a name and a current value. Assignment evaluates the right-hand expression first, so counter = counter + 1 takes the old counter value and replaces it with a value one greater.
These are independently written questions with indicative marking guidance. Trace the values or write your answer before opening the solution.
State the Python data type of each value: "123", 123, 7.5, 5.0, False and "False".
Indicative answer: "123": string; 123: integer; 7.5: float/real; 5.0: float/real; False: Boolean; "False": string. Award one mark for each correct type.
Use the counter program above. State its two outputs in order, then explain how counter = counter + 1 changes the value.
Indicative answer: The program prints 1, then 2 (1 each). Each update evaluates the current counter plus one (1) and assigns that result back to counter (1).
A program uses the code below. The user enters 14, but it raises a type error instead of printing their age next year. Explain the problem and repair the program.
age = input("Enter your age: ")
print(age + 1)Indicative answer: input() returns a string (1). Convert it using int() before adding an integer (1). A correct repair prints 15 for input 14 (1).
age = int(input("Enter your age: "))
print(age + 1)Choose a suitable data type for each item: the number of students in a room; a temperature that may include decimals; whether a door is locked.
Indicative answer: Number of students: integer; temperature that may include decimals: float/real; whether a door is locked: Boolean. Award one mark for each suitable type.
This guide covers introductory types, variables, assignment and input conversion. It supports a part of the programming content rather than the whole specification.
These are independently written explanations and practice questions. CompSciTutoring.co.uk is not affiliated with or endorsed by an examination board. The marking guidance is indicative; always check the syllabus for your examination year.
Found an error? Email a correction.
Use the free examples first, then explore one-to-one tuition if guided practice would help.