-
Notifications
You must be signed in to change notification settings - Fork 0
Chapter 8 (Arrays)
In this lesson, we will learn what an array is and how it is useful.
Let's code a list of crew members in variables.
crew_member_0 = "Clay"
crew_member_1 = "George"
crew_member_2 = "Nick"
crew_member_3 = "Alex"
crew_member_4 = "Karl"
crew_member_5 = "Cal"
crew_member_6 = "Alyssa"
crew_member_7 = "Thomas"
crew_member_8 = "Toby"
crew_member_9 = "Wilbur"This isn't a very good way of doing things - we can't loop over the crew members, and we can't do things like removing members easily. And what if instead of 10 crew members, we had 1,000?
An array is a special type of variable which holds many values. We can code our 10 crew members in an array like so:
>>> crew_members = ["Clay", "George", "Nick", "Alex", "Karl", "Cal", "Alyssa", "Thomas", "Toby", "Wilbur"]
>>> print(crew_members)
['Clay', 'George', 'Nick', 'Alex', 'Karl', 'Cal', 'Alyssa', 'Thomas', 'Toby', 'Wilbur']You can refer to individual elements of an array with their index number:
>>> print("The first crew member is " + crew_members[0])
The first crew member is Clay
>>> x = crew_members[7]
>>> print("The eighth crew member is " + x)
The eighth crew member is ThomasRemember that in Python, we start counting from 0!
We can also modify elements of an array. For most cases, a reference to an array element crew_members[0] is just like a reference to a variable crew_member_0.
>>> crew_members[5] = "Red"
>>> print(crew_members[5])
RedRecall that the for loop iterates over a sequence? That sequence can be an array. In other words, it can be used to execute code for each member of the array.
>>> for i in crew_members:
... print(i)
Clay
George
Nick
Alex
Karl
Red
Alyssa
Thomas
Toby
WilburArrays also have a few methods you can use on them:
- The
len()method returns the number of elements in the array.
>>> print(len(crew_members))
10- The
append()method adds another element to the end of the array.
>>> crew_members.append("Felix")
>>> print(crew_members[10])
Felix- For removing elements from arrays, you have two choices.
The
remove()method searches for the first instance of its input in the array, and removes it.
>>> crew_members.remove("Alyssa")
>>> for i in crew_members:
... print(i)
Clay
George
Nick
Alex
Karl
Red
Thomas
Toby
Wilbur
FelixThe pop() method removes the element at the provided index number. It also returns the removed value, which means you can use it in functions.
>>> print(crew_members.pop(5) + "was ejected")
Red was ejected
>>> for i in crew_members:
... print(i)
Clay
George
Nick
Alex
Karl
Thomas
Toby
Wilbur
Felix- The
insert()method inserts a new element at the specified index position.
>>> crew_members.insert(3, "Tyler")
>>> for i in crew_members:
... print(i)
Clay
George
Nick
Tyler
Alex
Karl
Thomas
Toby
Wilbur
FelixExercise: Given an array unsorted_array = [10, 4, 2, 5, 20, 21, 3], sort it from lowest to highest.
Thankfully, Python lists have their own sort() method to do this more easily.
>>> unsorted_array.sort()
>>> print(unsorted_array)
[2, 3, 4, 5, 10, 20, 21]
>>> crew_members.sort()
>>> print(crew_members)
['Alex', 'Clay', 'Felix', 'George', 'Karl', 'Nick', 'Thomas', 'Toby', 'Tyler', 'Wilbur']By a team of students from NUS High School, aiming to make coding easier for everyone.