Page 166 -
P. 166
data in files and arrays
An array lets you manage a whole train of data
So far, you’ve used variables to store only a single piece of data. But
sometimes, you want to refer to a whole bunch of data all at once. For
that, you need a new type of variable: the array.
An array is a “collection variable” or data structure. It’s designed to
group a whole bunch of data items together in one place and give them
a name.
Think of an array as a data train. Each car in the train is called an
array element and can store a single piece of data. If you want
to store a number in one element and a string in another, you
can.
Each car holds
Here comes the a single piece of
data train. data.
You might think that as you are storing all of that data in an array,
you still might need variables for each of the items it stores. But this is
not the case. An array is itself just another variable, and you can give it
its own variable name:
my_array = [7, "24", "Fish", "hat stand"]
All aboard
the my_array
express!
my_
array 7 "24" "Fish" "hat stand"
Even though an array contains a whole bunch of data items, the array
itself is a single variable, which just so happens to contain a collection of data.
Once your data is in an array, you can treat the array just like any other
variable.
So how do you use arrays?
you are here 4 131