Edexcel International Advanced Level Computer Science, point 8.1.1
A data type says what kind of value a variable holds, and what operations are allowed on it. Choosing the right type gives correct results and sensible use of memory. You must be able to select and use each type and convert between them. (Check the PLS for the exact detail.)
The types
| Type | Holds | Python name | Examples |
|---|---|---|---|
| Integer | Whole numbers (positive, negative or zero) | int |
42, -7, 0 |
| Real | Numbers with a fractional part | float |
3.14, -0.5, 2.0 |
| Character | One symbol (a letter, digit or punctuation mark) | str of length 1 |
"A", "7", "?" |
| Boolean | True or false | bool |
True, False |
| String | A sequence of characters (text) | str |
"Hello", "12 High St" |
Python has no separate character type: a character is a string of length 1.
age = 17 # integer
height = 1.82 # real (float)
grade = "B" # character (a string of one character)
passed = True # Boolean
name = "Amy Lee" # string
print(type(age), type(height), type(passed), type(name))
Choosing a type
| Data | Best type | Why |
|---|---|---|
| Number of items | Integer | Whole, exact |
| Price, weight | Real | Has a fractional part (or integer pence to avoid rounding errors) |
| Yes/no flag | Boolean | Two states |
| A name | String | Text |
| A phone number | String | Not used in arithmetic, and can start with 0 |
| A postcode, ID | String | Mixes letters and digits |
Operations depend on the type
print(5 + 3) # 8 (integers added)
print("5" + "3") # 53 (strings joined)
print(5 / 2) # 2.5 (real division)
print(5 // 2) # 2 (integer division)
print("ab" * 3) # ababab (string repeated)
print(True + True) # 2 (Booleans act as 1 and 0)
Adding a string and an integer is a TypeError: "Age: " + 17 fails.
Type conversion (casting)
You convert between types with the built-in functions:
| Function | Converts to | Examples | Notes |
|---|---|---|---|
int() |
Integer | int("42") is 42, int(3.9) is 3 |
Truncates reals (drops the fraction). int("abc") is a ValueError |
float() |
Real | float("3.5") is 3.5, float(2) is 2.0 |
|
str() |
String | str(42) is "42" |
Needed to join a number to text |
bool() |
Boolean | bool(0) is False, bool("") is False, bool(5) is True |
Zero and empty are False |
chr() / ord() |
Character and code | ord("A") is 65, chr(97) is "a" |
Character codes (2.3.1) |
tickets_text = input("How many tickets: ") # always a string
tickets = int(tickets_text) # convert to integer
print("Cost: £" + str(tickets * 4.5)) # number back to a string
Implicit and explicit conversion
- Explicit: you write the conversion (
int(x)). - Implicit: Python converts automatically in some cases:
3 + 2.5gives5.5(the integer becomes a float).
Handling bad conversions
try:
n = int(input("Number: "))
except ValueError:
print("That wasn't a whole number")
(Exception handling: 7.1.1.)
Worked example
Task: a program reads a price as text, adds a £2.50 delivery charge, and prints "Total: £12.50". Choose and convert the types.
price_text = "10" # string from input()
price = float(price_text) # convert to real
total = price + 2.5 # real arithmetic
# real -> string with 2 decimals
print("Total: £" + format(total, ".2f"))
input()returns a string, so it is converted withfloat().- The calculation uses reals.
- To join the number to text, it is converted to a string.
format(total, ".2f")gives12.50. - Without the conversion,
price_text + 2.5is aTypeError(a string can't be added to a number).
Exam tip: input() always gives a string: convert it before arithmetic. int() truncates, and doesn't round: int(3.9) is 3. Choose the type by how the data is used (a phone number is a string). Catch ValueError when converting user input.
Check yourself
What data type is the value returned by input()?
String.
What does int(7.9) return?
7 (it truncates, dropping the fraction).
Why is a phone number usually stored as a string?
It isn't used in arithmetic and it can start with a zero, which an integer would lose.
What is the result of "5" + "3", and of 5 + 3?
"53" (strings are joined) and 8 (integers are added).
Which exception does int("abc") cause?
ValueError.