5.1. Introduction to Arrays#
An array is a fundamental data structure found in most programming languages. Arrays allow you to store a preset number of values (elements) together in one block of memory. This allows for quick access for values in the array, if you know you want the third value, you can go directly to it. This is in contrast to lists, which can have their elements spread out in the computer’s memory, making accessing elements slower.
An array is a little like a chest of drawers. Each drawer (or element) can hold
some stuff (a value), and all the drawers are stacked in a sequence. If we want
the contents of the third drawer, we can simply open the third drawer without
checking the first and second drawers. Like drawers that can only store items
of a specific size or type, arrays can only hold values of the same type like
int
or float
.
In Python, it is common to use lists to represent arrays.
You’ve already encountered lists in Python, but there’s a twist—you can have lists of lists! These are similar to multidimensional arrays found in other programming languages.
Question 1
How is an array stored in memory?
As one contiguous block.
Spread out across memory.
Solution
A.
Question 2
How is a list stored in memory?
As one contiguous block.
Spread out across memory.
Solution
Solution is locked