Lesson 4 (Lists)

lists

So far, we have used variables to store one value at a time. For example, we could have stored the name of one amino acid as a variable:

But as you just learned, there are 20 different amino acids. So if we wanted to store all 20 amino acids as variables, we would have to create 20 different variables (e.g., my_amino_acid_1, my_amino_acid_2, ..., my_amino_acid_20). An easier way to store many valuables is, quite intuitively, in a list. A list is simply a changable, ordered sequence of elements. Just like strings are defined as ordered characters between quotes "", lists are defined as comma-delimited elements between square brackets []. (Lists can also store any data type or combination of data types — for example, strings and floats can be in a single list together.) We can then store all 20 amino acids as a list:

Recall that a protein is a polypeptide: a chain of amino acids connected to each other. Thus, we can create a protein as a list of amino acids:

Zero indexing

We have said that a list is an ordered sequence of elements. This ordering is useful, in that we can access the first, fifth, or last element in the list very quickly and easily. The number in the list that an element occurs is called that element's index. To print any element in a list, use the syntax my_list[ index ]. But be careful: Python lists are zero-indexed! That is, the first element in the list has index 0, and the fifth element in the list has index 4. So, to print the first amino acid in our protein:

One obvious question to ask about a list is how long is it? There is an easy function that gives the length of any list: len()

Now it's your turn!

  1. Print the last amino acid in our protein list! Hint: remember that the list is zero-indexed! What if you didn't know how long your list was?