We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Jo identifier. Just
11.01 What is an Array?
under a sing) :
AN array isa data structure that can hold a set of data items on holds data of a specific
28 a Variable holds data and is identified by a name, so an att ay 2 ‘7 hold one data value
type and is referred to by its name or label While avariable can ony ATT, of 9
an array can hold several values. For example, f you wished {0 store
array.
students you could either do this in 25 variables or a single
of
An array is an example of a container data type. Python does not support arrays ae 7
the box. If arrays are required then the array built-in library must be imported first,
lled tuples, lists and
because Python has a rich set of alternative container data types ca j
dictionaries. You will see later in this chapter that the Python version of the FOR loop is
designed to work very closely with these container data types.
{tis recommended that, when implementing your pseudocode and flowchart sol
Python, you use lists to replicate arrays. A list can do everything an array can do and more.
For example, lists are able to store a variety of data types at the same time and can be
lengthened or shortened as required.
Itis worth noting that, although lists are more flexible than arrays, arrays are more efficient
for most simple processes.
11.02 Declaring an Array
Declaring an array is a similar process to declaring a variable. The difference is that you need
to define the size of the array, determined by the number of data items that the array is
required to hold, Each individual value held within an array is identified by an index number,
Index numbers are sequential and, in Python, as with many other programming | ‘
lany
the numbering starts from zero. 6 languages,
Table 11.01 is a diagrammatic representation of an array designed to hold
the
of the Greek alphabet. le first five letters,
Table 11.01
The pseudocode format for declaring an array capable of hold
Note how the range of indices is 0 to 4: ing the five values is as follows,
Not all languages number array items from zero and yoy may see ay
start from 1. In this erample the final index would then becomes example of arrays that
i at
‘Scanned with CamScannergreek heter[i 5) OF String
When writing pseudoco,
hod of ny is
following pseudocode must 2150 follow that format
and the ith data item in “tw
de, either met
table; ho
ner itis vital to
array is declare
(0:4) then the
* data item held in index
index, th the first
Ber
The syntax for declaring a ist (the a
"2Y substitute recommended forimplementn jour
algorithms in Python) is. a
my_list = (None]+4
Python’ ist container does nothave tos
that the syntax for
*0t@ 2 declared number of data items, This means
Seun& an empty list ofa given length dove not look a lot ike the
declaration, The code
Pseudocode array above creates an array with four empty data spaces
andis equivalent to
in pseudocode.
Ifyou are not sure how thi
is works, start an interactive session and enter the line of code
above. To see what has been created add the following line
Print (my list)
FURTHER INFORMATION
Python programmers would normally just declare an empty list such as
my list = ()
towhich data items can be added as required.
imei, nin fay that is ready filled with content. In
fate an arr
‘AtSome point you will want to creat
Pseudocode, it is achieved like this:
abel ale.
Ireck letters = (Alpha, Bett, Gamma Delis Ps
‘Scanned with CamScannerROSSER Race UU aa
the:
strings. In Python, t
This is now a Predefined array with five spaces that will only store String
equivalent code would be ilon']
a', ‘Eps.
S¥eek letters = ['alpha', ‘petat, ‘Gamma’, "Delta
11.04 Using Arrays
Arrays offer programmers advantages over simple variables.
Many data items to be stored under a single identifier. They
to reference any individual data item by the appropriate array ind “
to perform read, write or search operations by looping through the data items.
arrays particularly effective when working with data records
Satrrrcs
‘As we have seen, they allow
give the programmer the ability
jex and to use iteration
his makes,
standing ofthe use ofa variable
To read a data item, you reference it by the array name and the index number. For example,
holds the data item ‘Gamma’, The same logic applies when writing values to an array. The
following code would writ the leter‘Cto the spected index position, replacing the original
data item:
The syntax in Python for these two operations, using lists, i identical to the Pseudocod:
le.
a Reading and Writing Data Items
Integer array .
Declare an artay named task’ that capable of holding four integers, Wi
userto input an negro selected ray postion. Then add code to sng coe 1 OW the
the value held in a selected array position, 'e User to output
Dare
Bure 11.01(b) shows the
Ponding pseudocode for
Figure 11.01(a) shows the flowchart for the input process and fi
flowchart for the output process. Figure 11.02 shows the corres
these processes.
‘Scanned with CamScannerInput
index
data_item
tasklinder)
ourrut
tasklindex)
(a) (b)
ata_item
Figure 11.01 Array input (a) and output (b) flowchart
M Input Pseudocode 1 Oulu Pseudocode
task{0:3]
INPUT index
INPUT index, data_item
ourPur task index)
task[index] — data_item
Figure 11.02 Array input and output pseudocode
The interface could be
AP ed to produce the system. :
Foe ete 1103. Bit sina the subroutines foreach of the input and
designed as shown in Figure 11.03.
i ‘aout and display the output. As the functions
output processes. Entry widgets accept the inpu'
are so. angi the oe ny bas been taken to demonstrate a little more tkinter GUI code.
LSet)
Output
Display:
Item to Add: Value in Index: Cd
Index to be Used:
Figure 1.03 a Gul interface desien
‘Scanned with CamScanner