As we delve more deeply into programming in Python, I wanted to examine and try to understand one of the most important data structures Python uses: a list. Arrays and lists are sometimes used as synonyms in coding, and I wasn't sure why. In my research, here is the simple definition I found: lists are data structures which hold multiple data points in a structured, ordered set which can be used and manipulated in many different ways. Lists are an efficient way to store lots of related data at once in a specific sequence which still allows you to pull out, or "index," the individual data points. It seems as though an array is simply a more complicated list which requires a knowledge of exactly how many data pieces it will hold, but most of the time in Python the terms list and array will refer to the same type of code.
For my first program, I used PythonAnywhere to create a single, simple list. The format for creating a list first defines the name of the list, similar to defining a variable. Just like x = 3 creates a variable called "x," my first program creates a list called "first_list." Each individual data point is stored inside a set of brackets [ ] and separated by a comma. In order to have the Python Interpreter display this set of data, I used the print command, which promptly outputted my list- in the exact same order I wrote it.
In my second program, I created a slightly more complicated list, using string-type data points (which are a connected series of characters) instead of integers. These string data points require a set of ' ' quotation marks around them to separate them from the names of variables. Here, I defined a list of flowers. I then printed each separate data point using it's index number, i.e., it's place in the sequence. Each index begins with number zero, so the total index numbers are always the number of data points minus one. On my first attempt at indexing, I tried to print (flowers[1]) through (flowers[7]). However, because my list index only went from 0-6, Python printed out "IndexError: list index out of range". On my second attempt, Python first printed out the entire list, and then printed each individual string; 'marigold,' 'peony,' etc... in order.
Instead of using positive index numbers to refer to the items in our list, we can use negative index numbers instead. This method assigns the last item in our list the index number "-1" and each item going backwards from the last item is a more negative number. In this specific list, index number -1 is 'daisy', number -2 is 'poppy', and number -7 is 'marigold'. When dealing with very long lists with a lot of data points, it can be easy to use negative index numbers to call an item close to the end of the list.
For my last program, I wanted to show one or two examples of how lists can be changed and manipulated in Python. Index numbers can be used not just to call and display data points in our sequence, but also to change the data within our list. Each individual data point can be referred to by it's number in the list, just like the name of a variable. Instead of having a variable flower0 = 'marigold' and creating seven variables, we can use a list where (flower[0]) is defined as 'marigold'. Just like when we set the value in a variable with an equals sign, x = 10, we can insert a new value or new string in place of an old one by setting (flower[0] equal to a new flower, like 'sunflower.' Now when we call and print index number zero in the list flower, Python spits out 'sunflower' instead of 'marigold.' (Remember- you can also modify data points within a list using negative index numbers instead!)
In my investigation into lists, I discovered so many different ways you can use and change them: deleting variables, printing out specific sections of them, multiplying and adding items within them or the list itself, and stacking lists within lists within lists. I hope to explore more about lists in the future, but I think this was a strong first look at "What is a List in Python?"
Citations:
Tagliaferri, Lisa. "Understanding Lists in Python 3." _DigitalOcean_, 2 Nov. 2016, www.digitalocean.com/community/tutorials/understanding-lists-in-python-3. Accessed 20 Mar. 2018.