This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.
Strings, reals, booleans and integers are all primitive data types: they only store one value at a time.
An array lets you store more than one value of the same primitive data type in the same variable.
A variable can store one primitive data type or it can contain an array.
An array stores data in the order that you specify in your code. The position of a value inside an array is called the index.
Python doesn't have arrays but it does have lists, which are more powerful and let you store a mixture of different data types.
This means what we think of as being the 1st item in an array has an index of 0.
An array let your store more than one piece of data into the same variable as an ordered list.
This example stores three strings into the array called names.
An array lets you store more than one piece of data into the same variable as an ordered list.
This example stores three numbers into the array called ages.
An array lets you store more than one piece of data into the same variable as an ordered list.
Data in an array can be accessed using an index. This is a number which defines the position in the list, which starts counting from 0
This example displays the first string in an array called names
If an array is like a list of data, a 2d array is like a grid of data where the data is stored in one variable but organised into rows and columns like a spreadsheet.
This example stores details of 3 different fruit for a supermarket.
It will then display the colour of the first fruit.
The first index will select the row then the second index selects the column.
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list called colours and adds three strings into it before displaying the first item in the list.
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list called numbers and adds three integer numbers into it before displaying the sum of all the numbers added together.
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list of strings called names. It adds 3 names to the list then removes the first one.
It can be really useful to split one string into lots of smaller parts whenever you see a particular character.
This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"
It will then display the first colour: red
Loading...