Lists in Python are a type of variable that contains other variables. They are, as the name suggests, a list of variables. They are also an example of a type of data structure - a way for Python to store data.
An example of list is defined as follows:
list_first = [0, "one", 3.0]
In the above, we have a list variable named _firstlist which has three terms in it. Note that when defining a list, we encapsulate terms in square ([ ]) brackets. Note also that a list can contain variables of different types - in the above example, our list contains variables of the integer type (0), string type ("one"), and float (3.0).
Lists might seem simple (and they are) but we are going to introduce a few new concepts in this homework. These concepts are:
Indexing: the system of counting terms in things like lists.
Methods. These are built in functions that exist for some variables.
Let's start with indexing. The index is just the numbering system that we use for items in the list. The important thing to know about lists is that they are ordered i.e. the index of each item stays the same (unless we change it). Look at the list above: what is the index (number) of the item with the value of zero (0)? You'd think it would be one (1), right? Wrong! It is actually element zero (0) of the list. Yes, that's right - in Python we start counting at number zero (0) rather than one (1). The reasons for this are not important, you just have to remember it.
Now to talk about methods. Many variable types, such as lists, have a set of built-in methods. You can involve a method with the following syntax:
variable.method_name(argument1, argument2, ...)
For example, if we wanted to add an element with the value "next" to the end of _listfirst we would use the append method with "next" as the argument:
list_first.append("next")
What do you think will be the output of the following code?
print(list_first)
To explore this topic interactively and learn more about lists, checkout the task3.py & task4.py files that I have created. Load up your Codespace, ensure you are on your branch and get the file with the following command in the terminal:
git checkout Homework task3.py task4.py
ONLY READ THE FOLLOWING AFTER ATTEMPTING TASK 3
If you did something similar to the following:
print("The third element of the list is", list_first[3])
You got an error saying something like:
IndexError: list index out of range
What's going on here? Let's break down the index of each element in this list:
list_first = [0, "one", 3.0]
index:
[0] = 0
[1] = "one"
[2] = 3.0
As you can see, there is no element [3]. There might 3 elements, but the first one is indexed as zero...so the final element is actually [2]. So, you got an IndexError saying that you are trying to access an element that is outside of the range of this list. If this seems extremely frustrating and confusing, that's because it is. I have been coding for years and it still gets me. Luckily, there are a few tricks we can use to avoid this which I will discuss in the next task.
ONLY READ THE FOLLOWING AFTER ATTEMPTING TASK 4
As you've probably figured out, using the index [-1] accesses the final element in the list, regardless of how many elements are in it. It is a safe way to access the final element of a list that you might be changing the size. We can use [-2] to get the last-but-one element and so on.
There are lots of cool thing you can do with lists which we'll touch on in further homeworks. You can for instance nest loops inside other loops i.e. you can put lists inside lists to make two dimensional lists...in fact you put put lists inside the lists inside other lists ... and so on to the maximum depth only limited by the RAM on the computer you are working on.
Another thing we can do with lists is take slices from them - i.e. we can cut bits of them out and do stuff with it. This is a bit tricky though as the positions you could from and end at ... changes. Yeah it's tricky, we'll come back to that.
CONCLUDING REMARKS
If you have made it this far, I just want to say thank you and well done. You've learned the core skills of using Python and built a solid foundation for using this programming language to do some really great stuff. A lot of this stuff is a bit tricky to get your head around, but by getting this far you have invested in your skills and you should be proud of yourself. Keep it up and we will do great things!
Lists in Python are a type of variable that contains other variables. They are, as the name suggests, a list of variables. They are also an example of a type of data structure - a way for Python to store data.
An example of list is defined as follows:
list_first = [0, "one", 3.0]
In the above, we have a list variable named _firstlist which has three terms in it. Note that when defining a list, we encapsulate terms in square ([ ]) brackets. Note also that a list can contain variables of different types - in the above example, our list contains variables of the integer type (0), string type ("one"), and float (3.0).
Lists might seem simple (and they are) but we are going to introduce a few new concepts in this homework. These concepts are:
Let's start with indexing. The index is just the numbering system that we use for items in the list. The important thing to know about lists is that they are ordered i.e. the index of each item stays the same (unless we change it). Look at the list above: what is the index (number) of the item with the value of zero (0)? You'd think it would be one (1), right? Wrong! It is actually element zero (0) of the list. Yes, that's right - in Python we start counting at number zero (0) rather than one (1). The reasons for this are not important, you just have to remember it.
Now to talk about methods. Many variable types, such as lists, have a set of built-in methods. You can involve a method with the following syntax:
variable.method_name(argument1, argument2, ...)
For example, if we wanted to add an element with the value "next" to the end of _listfirst we would use the append method with "next" as the argument:
list_first.append("next")
What do you think will be the output of the following code?
print(list_first)
To explore this topic interactively and learn more about lists, checkout the task3.py & task4.py files that I have created. Load up your Codespace, ensure you are on your branch and get the file with the following command in the terminal:
git checkout Homework task3.py task4.py
ONLY READ THE FOLLOWING AFTER ATTEMPTING TASK 3
If you did something similar to the following:
print("The third element of the list is", list_first[3])
You got an error saying something like:
IndexError: list index out of range
What's going on here? Let's break down the index of each element in this list:
list_first = [0, "one", 3.0]
index:[0] = 0 [1] = "one"
[2] = 3.0
As you can see, there is no element [3]. There might 3 elements, but the first one is indexed as zero...so the final element is actually [2]. So, you got an IndexError saying that you are trying to access an element that is outside of the range of this list. If this seems extremely frustrating and confusing, that's because it is. I have been coding for years and it still gets me. Luckily, there are a few tricks we can use to avoid this which I will discuss in the next task.
ONLY READ THE FOLLOWING AFTER ATTEMPTING TASK 4
As you've probably figured out, using the index [-1] accesses the final element in the list, regardless of how many elements are in it. It is a safe way to access the final element of a list that you might be changing the size. We can use [-2] to get the last-but-one element and so on.
There are lots of cool thing you can do with lists which we'll touch on in further homeworks. You can for instance nest loops inside other loops i.e. you can put lists inside lists to make two dimensional lists...in fact you put put lists inside the lists inside other lists ... and so on to the maximum depth only limited by the RAM on the computer you are working on.
Another thing we can do with lists is take slices from them - i.e. we can cut bits of them out and do stuff with it. This is a bit tricky though as the positions you could from and end at ... changes. Yeah it's tricky, we'll come back to that.
CONCLUDING REMARKS
If you have made it this far, I just want to say thank you and well done. You've learned the core skills of using Python and built a solid foundation for using this programming language to do some really great stuff. A lot of this stuff is a bit tricky to get your head around, but by getting this far you have invested in your skills and you should be proud of yourself. Keep it up and we will do great things!