What is an Array?
An Array is a data structure that can be used to store many items in one place, sort of like a house. If the house is an array, some of the things you may find in it include; doors, windows, and rooms.
Room’s are also an Array, as they store items. Let’s take a look inside… the Kitchen.
In programming, we can create a kitchen (array) and store its items. Here’s an example using Python:
What can we do with an Array?
Access its values:
In an Array, every element is assigned a number called an index. Index numbers are consecutive integers starting from 0. For example, if we wanted to find the kettle in the kitchen array above, we can get it by specifying it’s index.
Modify its values:
Let’s add an item our Kitchen…
Iterate over it:
If an array contains N elements, we can iterate over consecutive numbers from index 0 to index N − 1 and check every index. The length of an array can be found using the len() function.
N = len(kitchen)
In Python, we can iterate over a list using a ‘for loop’. Here’s an example:
Some basic Array operations:
Here are a few other useful Python list operations: