impera Open Impera

A levels Computer Science · Programming and data

Data types and data structures

1Primitive data types

TypeStoresExample
integerwhole numbers42
real (float)numbers with a fractional part3.14
BooleanTrue or FalseTrue
charactera single symbol'A'
stringa 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?

  • Boolean

  • String

  • Real

  • Integer

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?

  • A file on disk

  • A single integer

  • A list of values that must all be the same type

  • A collection of related fields that can be of different types

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.
Worked example

Finding an address

  1. An array of 4-byte integers starts at address 1000.
  2. 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?

  • 1000

  • 502

  • 510

  • 520

Show the answer

520

500 + 10 × 2.

4Static and dynamic structures

StaticDynamic
size fixed when createdgrows and shrinks while running
memory reserved even if unuseduses only the memory it needs
fast direct access by indexmay need pointers, slower access
example: arrayexample: 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.

Worked example

Inserting into a linked list

  1. List: Ann → Cal → Eve, and Bob must go between Ann and Cal.
  2. Store Bob in a free node and set Bob's pointer to Cal.
  3. 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?

  • It uses less memory per item because it stores no pointers

  • Any item can be reached directly using its index number

  • It never needs pointers, so it is simpler to implement

  • Items can be inserted or deleted without moving other items

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.

Worked example

Hashing with key MOD 11

  1. Insert 23: 23 MOD 11 = 1, store at slot 1.
  2. Insert 47: 47 MOD 11 = 3, store at slot 3.
  3. Insert 12: 12 MOD 11 = 1, collision, so probe to slot 2.
  4. 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?

  • 30

  • 2

  • 0

  • 4

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
NeedGood choice
fixed number of items, fast access by positionarray
frequent inserts and deletes in the middlelinked list
fast lookup by keydictionary or hash table
related fields about one thingrecord
Check yourself

Which structure is best for looking up a price from a product code?

  • A Boolean

  • A two-dimensional array

  • A linked list

  • A dictionary

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