6.3.1 Data types and structured types

Why Data Types Matter

Every value stored in a program has a data type that determines what it represents and what operations are valid on it. Choosing the wrong type leads to errors — attempting arithmetic on a string, or storing a decimal value in an integer variable. Choosing the right type makes programs correct, efficient and readable.

Primitive Data Types

TypeWhat it storesPython exampleC# / VB.NET type
IntegerWhole numbers (positive, negative, zero)score = 47int
Real (float)Decimal / fractional numbersprice = 3.99double / float
BooleanTrue or False onlylogged_in = Truebool
CharA single charactergrade = "A" (Python uses string length 1)char

Structured Data Types

A string is a sequence of characters. In Python it is a built-in type with many manipulation methods. Strings are zero-indexed: the first character is at index 0.

name = "Alice"
print(len(name))          # 5
print(name[0])            # A  (first character)
print(name[1:3])          # li (indices 1 and 2)
print(name.upper())       # ALICE
print(name.lower())       # alice
string name = "Alice";
Console.WriteLine(name.Length);          // 5
Console.WriteLine(name[0]);              // A
Console.WriteLine(name.Substring(1, 2)); // li
Console.WriteLine(name.ToUpper());       // ALICE
Console.WriteLine(name.ToLower());       // alice
Dim name As String = "Alice"
Console.WriteLine(name.Length)           ' 5
Console.WriteLine(name(0))               ' A
Console.WriteLine(name.Substring(1, 2))  ' li
Console.WriteLine(name.ToUpper())        ' ALICE
Console.WriteLine(name.ToLower())        ' alice

In Python, both 1D arrays (lists) and 2D arrays (lists of lists) are implemented using the list type. Elements are accessed by index (zero-indexed).

# 1D array (list)
scores = [72, 85, 60, 91]
print(scores[0])           # 72 (first element)
scores[2] = 65             # update element at index 2
print(len(scores))         # 4

# 2D array (list of lists) — 3 rows, 3 columns
grid = [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]
print(grid[1][2])          # 6 (row 1, column 2)
// 1D array
int[] scores = {72, 85, 60, 91};
Console.WriteLine(scores[0]);     // 72
scores[2] = 65;
Console.WriteLine(scores.Length); // 4

// 2D array
int[,] grid = { {1,2,3}, {4,5,6}, {7,8,9} };
Console.WriteLine(grid[1, 2]);    // 6
' 1D array
Dim scores() As Integer = {72, 85, 60, 91}
Console.WriteLine(scores(0))      ' 72
scores(2) = 65
Console.WriteLine(scores.Length)  ' 4

' 2D array
Dim grid(,) As Integer = { {1,2,3}, {4,5,6}, {7,8,9} }
Console.WriteLine(grid(1, 2))     ' 6

A record groups related fields that describe one entity (e.g. a student record with name, age and score). In Python, a list is used as a record at GCSE — each index position holds one field.

# Record as a list: [name, age, score]
student = ["Alice", 16, 85]
print(student[0], "age", student[1], "scored", student[2])

# Table of records: list of lists
students = [
    ["Alice", 16, 85],
    ["Bob",   17, 72],
    ["Carol", 16, 91]
]
for s in students:
    print(s[0], "->", s[2])
// Simple record using parallel arrays
string[] names  = {"Alice", "Bob", "Carol"};
int[]    ages   = {16, 17, 16};
int[]    scores = {85, 72, 91};
for (int i = 0; i < names.Length; i++)
    Console.WriteLine(names[i] + " -> " + scores[i]);
' Simple record using parallel arrays
Dim names()  As String  = {"Alice", "Bob", "Carol"}
Dim ages()   As Integer = {16, 17, 16}
Dim scores() As Integer = {85, 72, 91}
For i As Integer = 0 To names.Length - 1
    Console.WriteLine(names(i) & " -> " & scores(i))
Next

 Key Takeaways

  • Primitive types: integer (whole), real (decimal), Boolean (True/False), char (single character).
  • String: sequence of characters; zero-indexed; supports len, slicing, upper/lower.
  • 1D array (list): ordered collection; accessed by index 0 to len-1.
  • 2D array: list of lists; accessed by grid[row][col] in Python.
  • Record: groups related fields for one entity; in Python represented as a list.