A levels Computer Science · Programming and data
Data types and data structures
1Primitive data types
| Type | Stores | Example |
|---|---|---|
| integer | whole numbers | 42 |
| real (float) | numbers with a fractional part | 3.14 |
| Boolean | True or False | True |
| character | a single symbol | 'A' |
| string | a sequence of characters | "Pebble" |
Casting converts between types: int("7") gives 7, str(7) gives "7", float(3) gives 3.0.
Choosing the right type matters: a phone number is stored as a string because it can start with 0, has no arithmetic done on it, and may contain spaces or a + sign.
Check yourself
Which type should a phone number be stored as?
Show the answer
String
Leading zeros, no maths.
2Records and user-defined types
A record groups related fields of different types under one name, for example a student with a name (string), age (integer) and fees paid (Boolean).
from dataclasses import dataclass@dataclass
class Student:
name: str
age: int
paid: bool
s = Student("Amira", 17, True)
print(s.age) # 17
An enumerated type lists the allowed values, such as Day = {Mon, Tue, Wed, Thu, Fri}.
Check yourself
What is a record?
Show the answer
A collection of related fields that can be of different types
Like a row in a table.
3Arrays
An array is a fixed-size collection of elements of the same type, stored in contiguous memory and accessed by index.
An array of 8 integers in contiguous memory. Any element's address is calculated from its index, so access is O(1).
- Accessing any element by index takes the same time, O(1), because its address is base + index × element size.
- A two-dimensional array is a grid, accessed as grid[row][col].
- Python lists act like dynamic arrays; in exam pseudocode, arrays are usually fixed size.
Finding an address
- An array of 4-byte integers starts at address 1000.
- Element 5 is at 1000 + 5 × 4 = 1020.
Check yourself
An array of 2-byte values starts at address 500. What is the address of element 10?
Show the answer
520
500 + 10 × 2.
4Static and dynamic structures
| Static | Dynamic |
|---|---|
| size fixed when created | grows and shrinks while running |
| memory reserved even if unused | uses only the memory it needs |
| fast direct access by index | may need pointers, slower access |
| example: array | example: linked list |
A linked list is a dynamic structure of nodes. Each node stores data and a pointer to the next node. A start pointer shows the first node; the last node points to null.
Inserting into a linked list
- List: Ann → Cal → Eve, and Bob must go between Ann and Cal.
- Store Bob in a free node and set Bob's pointer to Cal.
- Change Ann's pointer to Bob. No data has to move.
Check yourself
What is the main advantage of a linked list over an array?
Show the answer
Items can be inserted or deleted without moving other items
Just change pointers.
5Hash tables
A hash table stores values at an index found by a hash function applied to the key. Lookup is usually O(1).
A collision happens when two keys hash to the same index. One fix is linear probing: try the next free slot. Another is chaining: each slot holds a list.
Hashing with key MOD 11
- Insert 23: 23 MOD 11 = 1, store at slot 1.
- Insert 47: 47 MOD 11 = 3, store at slot 3.
- Insert 12: 12 MOD 11 = 1, collision, so probe to slot 2.
- Insert 35: 35 MOD 11 = 2, taken; slot 3 taken; store at slot 4.
A good hash function is quick to calculate, spreads keys evenly, and gives few collisions. As the table fills up, collisions increase, so it may be rehashed into a larger table.
Check yourself
Using key MOD 7, which slot does key 30 hash to?
Show the answer
2
30 = 4 × 7 + 2.
6Dictionaries and choosing a structure
A dictionary stores key–value pairs and is usually built from a hash table:
stock = {"apple": 12, "pear": 7}
stock["plum"] = 3
print(stock["apple"]) # 12
| Need | Good choice |
|---|---|
| fixed number of items, fast access by position | array |
| frequent inserts and deletes in the middle | linked list |
| fast lookup by key | dictionary or hash table |
| related fields about one thing | record |
Check yourself
Which structure is best for looking up a price from a product code?
Show the answer
A dictionary
Key → value.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera