LESSON NOTE/GUIDE
|
#Creating a list of numbers. numbers = [4,6,9,2,0] #We can print any element print(numbers[0]) #will output 4 print(numbers[3]) #will
output 2 #We can also print the whole list print(numbers) #will output [4,6,9,2,0] |
It is often necessary to work with
large amounts of data. Creating a
variable name for each value would be very difficult. Instead, we create a single name and
store all the data under that name.
This is called a data
structure and there are many different types of these. In Python, the most commonly used data
structure is the list. The first statement above creates a
list called numbers. We know it’s a
list because of the square brackets used around the values. This list contains five values that we
tend to refer to as elements. Each element in a list is
associated with an index
number. For example, the first
element which has value 4, has the index number 0. The next element, with value 6, has index
1. And so on… In the second statement, we see how
we can access the value of an element.
We simply use the list name followed by the index number inside
square brackets. In this case, we
access index 0 and print it out to screen. In the third statement, we output
element with index 3 to screen. In the fourth statement, we output
the entire list. It is important to note that index
numbers in Python start at zero. |
PROGRAM
2 – CREATING A LIST WITH A RANGE
numbers = list(range(1, 10)) print(numbers) |
EXPLANATION In the first statement, we create a
sequence from 1 to 9 (10 is not included).
We then convert that to a list. The output will be: [1, 2, 3, 4, 5, 6,
7, 8, 9] So using range, we can conveniently
create lists of consecutive numbers. |
PROGRAM
3 – GETTING VALUES FROM A LIST
veggies = ["carrots", "onions",
"peas"] print("I
love", veggies[2]) print ("I hate", veggies[1]) |
EXPLANATION We can create lists that contain
strings as values. The first statement creates a list
called veggies. It contains three
elements that are all strings (as opposed to the numbers in the first
program). The second statement outputs “I
love peas” and the third statement outputs “I hate onions”. |
PROGRAM
4 – SETTING VALUES IN A LIST
THE CODE my_list = [4, 9, 12, -4, 7] print(my_list) my_list[0] = 2 my_list[3] = my_list[1]*2 print(my_list) |
EXPLANATION The program will output: [4,
9, 12, -4, 7] [2,
9, 12, 18, 7] Here is a line by line explanation: The first line creates a list
called mylist with five numbers in it. The second line outputs it to screen. The third line sets the value of
element 0 to 2. So the value 4 is
now gone and replaced by a 2. The fourth line will set the value
of element 3. It will be set to the
value of element 1 times 2. Since
element 1 has a value of 9, and since 9 times 2 is 18, then element 3 gets
the value 18. The last line simply outputs the
list with the new values for elements 0 and 3 showing. |
PROGRAM
5 – LENGTH OF A LIST
fnlist = [4,5,8,7,2,4,25,6,7,4,1,99,9,1]
|
EXPLANATION The first line of code simply
creates a list called fnlist which is short for favourite numbers list that contains the favourite number of each student in the class. The next line makes use of the len function to get the length of the list. This is the number of elements in the
list. This value gets stored in the
variable called count. The final line simply outputs the
value of count along with a message. |
PROGRAM
6 – GETTING A RANDOM VALUE FROM A LIST
import random treasureItems =
["50 gold", "a sword", "boots", "a
skull"] rn = random.randint(0,3) print("You open the chest and find", treasureItems[rn]) |
EXPLANATION The first line of code simply
imports the random library. The second line of code creates a
list called treasureItems that contains 4
elements. Each element is a possible
treasure find in a game. The third line of code generates a
random integer between 0 and 3 inclusively.
Those numbers are specifically chosen because the possible index
values for the list treasureItems range from 0 to
3. The last line outputs a message
that ends with a randomly selected item from the list. |
PROBLEM
WITH THE ABOVE CODE The problem with the above program
is that if the programmer changes the amount of items in the list, then the
range for the random number also needs to be updated – more specifically,
the upper value of the range needs to be updated. While this may seem easy in our short
program, in a real application, those two lines of code may be thousands of
lines apart from one another. THE
SOLUTION TO THE PROBLEM The solution is to tell Python to
calculate the upper value of the range for the random number. This upper value is always the length of
the list minus one. So, the upper value should be: len(listname) - 1 |
IMPROVED
CODE Below, the code in red is the code
that has been changed from the original solution. import random treasureItems =
["50 gold", "a sword", "boots", "a
skull"] rn = random.randint(0, len(treasureItems) - 1) print("You
open the chest and find", treasureItems[rn]) |
PROGRAM
7 – ADDING TO A LIST
coolPeople = ["Maddy", "Alyx",
"Nicky", "Matti"] #we can add at the end by using append coolPeople.append("Scott") print(coolPeople) #we can add anywhere using insert coolPeople.insert(0,
"Jay") print(coolPeople) |
EXPLANATION The first line creates a list
called coolPeople with four names in it. The second line of code adds the
name “Scott” to the list by using the append method. The third line of code outputs the
list to screen: ["Maddy", "Alyx", "Nicky", "Matti", "Scott"] The fourth line of code inserts “Jay”
at element zero. It shuffles
everything afterwards. The fifth line of code outputs the list
to screen: ["Jay", "Maddy",
"Alyx", "Nicky", "Matti", "Scott"] |
PROGRAM
8 – REMOVING FROM A LIST
coolPeople =
["Noah", "Emma", "Nicky", "Matti"] #we can remove from the list using remove and the value coolPeople.remove("Matti") print(coolPeople) #we can remove an element using del and the index number del(coolPeople[1]) print(coolPeople) |
EXPLANATION The first line creates a list
called coolPeople with four names in it. The second line removes the element
with “Matti” in it. The third line outputs the list to
screen: ["Noah", "Emma", "Nicky"] The fourth line deletes the element
at index 1 (which contains “Alyx”). The fifth line outputs the list to
screen: ["Noah", "Nicky"] |
PROGRAM
9 – STARTING WITH AN EMPTY LIST
swlist = [] newname = "Leia" swlist.append(newname) newname = "Luke" swlist.append(newname) newname = "Han" swlist.append(newname) newname = "Ahsoka" swlist.append(newname) print(swlist) |
EXPLANATION The first line creates an empty
list named swlist. The second line creates a variable
with the value “Leia” stored in it.
The third line then adds the value of the variable to the end of the
list using the append method. Lines 4 and 5 append the word
“Luke” to the end of the list. Lines 6 and 7 append “Han” to the
end of the list. Lines 8 and 9 append “Ahsoka” to the end of the list. Line 10 prints the list out to
screen. |
FUTURE PROGRAM 10 – CHECKING IF A LIST CONTAINS A VALUE
data = [2, 6, 4, 8, 0]
if 2 in data:
print (“yep”)