AS/A Computer Science: Fundamentals of data structures

Elementary data types

Elementary data types, also known as primitive data types, are a set of basic data types from which all other data types are constructed. Elementary/primitive data types include:


Composite data types

Composite data types are made of groups of other types of data e.g. A string is made up of a group of characters. Other composite data types include arrays, lists and tuples, all of which are made up of groups of elementary data types and other composite data types.



Arrays and lists

Arrays and lists are quite similar and if you have started learning to program with Python the difference may be even more confusing. This is because in Python there are only lists, defined with square brackets [ ]. We can effectively create arrays by using Python lists according to the rules of arrays. This means they can only contain items of the same data type and they have a fixed size which must be declared when they are created. Arrays are therefore a static data structure. Lists on the other hand, can contain elements with different data types and can grow and shrink after being declared. Lists are a dynamic data structure. It should be noted that as RAM is not limitless most lsts will be given a maximum size to prevent them growing too large.



1D arrays/lists

A 1D array/list has a single group of items such as all the scores on a class test or the details of a student e.g. scores = [8,7,5,9]; studentA = ["Bob",18, 12/5/2005]
Arrays/lists are zero indexed meaning the first item, 8, is in position zero.
Array/list elements are accessed using the format array/list_name[position] e.g. scores[1] would be equal to 7 because 7 is in position 1 of the scores array.
Similarly, if we wanted to change a value in an array/list we can simply reference the listname and its location and provide a new value e.g. scores[3] = 8 would change the value stored in position 3 in the scores array from 9 to 8.



2D arrays/lists

A 2D array/list is an array/list where each element is another array/list e.g. The scores of 4 students on 3 class tests e.g. scores = [[8,7,5,9],[7,8,7,10],[6,7,5,8]]
They can be visualized as a table of values as seen below.
The format for referencing an item in a 2D array/list is array/list_name[array][position] e.g. scores[2][0] would be the value in position 0 in array 2 which is 6.
You can also change elements in a 2D array/list by referencing the list/arrayname and the list within that and array position e.g. scores[2][0] = 7 would change the first score in the second array from 6 to 7.



3D arrays/lists

A 3D array array/list is a list where each element is a 2D list e.g. all the scores on all the class tests for all the students in all the classes
They can be visualized as a set of tables of values as seen below.
The format for referencing an item in a 3D array/list is array/list_name[table][array][column] e.g. scores[0][1][2] would be the value in position 2 in array 1 of table 0 which is 7.
Once again, scores[0][1][2] = 5 would change the value in position two in array 1 in table 0 from 7 to 5.



Tuples

A tuple is another similar type of data structure in that it contains multiple values under a single identifier. The key difference with a tuple is that it is immutable i.e. it cannot be changed once it has been defined. In Python a tuple is defined using round brackets e.g. sizes = ("small", "medium", "large"). They are still accessed using the same notation as an array or list e.g. sizes[1] is medium. If you try to edit, remove or append to a tuple you will receive an error message.


Records

The record data structure is not available in Python but can be mimicked either by using text files with comma separated values, arrays of dictionaries or by using classes in object oriented programming as you will see on the next page of this site. It allows for a record structure to be defined. The structure of a record shows the elements it contains and their data type. We can then create records to store this predefined information for individuals.



Knowledge check


Questions:
Correct:

Question text


© All materials created by and copyright S.Goff