Intro
In this section Arrays and Lists will be explored. By the end you will be able to recognise the difference between them and when they might be used.
Both Python and C++ will be used for code examples.
Arrays & Lists?
What are Arrays and Lists? They are a data-structure that are classified as a ordered collection. Meaning that one instance of an Array/List can hold many values. This will become clearer as you read on.
Arrays
Moving onto the first data-structure. The Array. We should know that we can store data by defining variables, however we have been limited to storing each new piece of data in a new variable. An Array will allow us to store multiple pieces of data together.
Here is a few important pointers about Arrays:
- Every element must be the same data-type
- The maximum size (number of elements) of the Array is fixed on initialization (the creation of the array)
If we wanted to store the alphabet as separate characters e.g. a, b, c
how would this be accomplished with an Array? The below C++ example shows how we might do this:
|
|
Taking a look the code we are telling the computer that we need a variable containing the char type, then we have the variable name. Along side this we have some square brackets, this sets the number of elements we will have in our Array. After we have given a size we are storing the three characters, in C++ we tell our program to store into the array using the curly brackets with our elements inside.
In computer programs each element of an Array can be accessed by it’s index. But what is this index? Each element that is in the array will be given a unique index; which is a number, generally starting from 0 and increments up by one for each element.
Using the code example from before if we were to display each of the elements index’s, we would get something as shown below:
|
|
FACT: Some languages have their indexes starting from 1 instead of 0!
Say we wanted to access 'b'
, what index should we be accessing? If you thought 1 you would be correct!
Using some C++ code we can access 'b'
and assign it to another variable using the following code:
|
|
We have seen how we could access an element of an array, but how can we change one of the elements? We can assign a new value to index 1 in a similar way we might assign a value to a variable, see the below C++ example:
|
|
Looking at the code we can see that assigning a new value at a specific index is similar to variable assignment. Although, by using the square brackets and index number we are indicating what element we want to change.
FACT: An Array of characters is actually how strings are stored; it’s just implemented “behind the scenes” for us, since it’s such a common type.
You may have noticed we have not seen a Python code example yet. That’s because Python does not have Arrays, instead it uses Lists. Which will be covered next.
Lists
Moving on to Lists. They are similar to Arrays since they allow us to store multiple indexed elements. However unlike Arrays we do not have the limitation of having a required length.
Here are some important points:
- Some languages allow multiple types to be stored in the same List
- The size (number of elements) of the List can grow and shrink (it’s dynamic)
Taking our alphabet example from the previous section a, b, c
using Python we can create a List as shown below:
|
|
Like before each element is given an index starting from 0 and incrementing by one each time. Notice this time the syntax looks slightly different than C++, this is because we don’t have to specify a length. This is great for when we are collecting some data where we don’t know the total length or we may want to add more elements in the future.
FACT: Python can actually store multiple data-types inside of the same List, since it is a dynamic language.
We can access our List items in a similar way in Python:
|
|
|
|
Since we are using a List we can also perform several operations. In this tutorial we will just be looking at “Appending”, which is just a fancy term which means “adding to the end”.
|
|
If this code was run we would of appended the letter “d” to our alphabet List. It would look like this:
|
|
Here’s a list of the common operations that Lists in most languages will have:
- Append
- Insert
- Pop
Conclusion
In this section you should now be able to:
- Recognise both an Array and List
- Decide when it may be suitable to use each
- Why they might be needed
- Know what a String really is “behind the scenes”