Homework/Forum Response
Respond to discussion minimum 150 Words
The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence. (Lundh 2006) After reading about lists, it seems as if it is an easier way to use the print function when using several values. For instance, to print the days of the week, you have to create 7 different lines of the print command, or have it all displayed at once. But with list, it can be done in one swoop, and possibly only be coded to show certain parts of the list at a time. For instance you might be able to have the user type day 3, and it pulls from the list and shows Tuesday (If you start your week on a Sunday).
Arrays in basic Python are actually lists that can contain mixed datatypes. However, the numarray module contains support for true arrays, including multi-dimensional arrays, as well as IDL-style array operations and the where function. (Python Basics) I'm not really sure how to put in words what an array is, or an example. The best information I can give is to go to my second reference which breaks down arrays with examples. When going through, it seems like it has lists, inside of lists.
Lundh, F. (2006, August). An Introduction to Python Lists. Retrieved August 18, 2016, from http://effbot.org/zone/python-list.htm
Python Basics. (n.d.). Retrieved August 18, 2016, from http://www.astro.ufl.edu/~warner/prog/python.html
-Jonathan
Respond to discussion minimum 150 Words
Lists are useful for collecting information in software. They are sequences that allow many types of data to be stored together in a single place, but each data has its own value and spot in memory, and can be retrieved individually. Or more than one value can be retrieved, or all of them at once. Lists are also contiguous, so as you add more items, more storage spots are added to the end of the location in memory. A benefit to using a list is that all the items do not have to be the same data type, some can be integers, others can be strings of floating point numbers. This makes it very versatile.
Creating a list:
ToDoList = [‘clean house’, ‘mow lawn’]
GroceryList = [‘hamburgers’, ‘buns’, ‘ketchup’]
To access one of the items in a list:
print (ToDoList[1])
#This would return “mow lawn”
An array is different from a list, for one, because it can only store one data type. If the array stores an integer, it can only store integers. When you build an array, you first let python know what the data type will be, then you list it. In the example below, the “i” shows Python that the values will be integers. Everything between the following parenthesis need to be integers as well.
Creating an array:
from array import *
my_array = array('i', [1,2,3,4])
To show the elements in the array:
For i in my_array:
Print(i)
#1 through 4 will be listed on each of the next four lines.
To access on of the items in an array:
my_array[1]
#This will return “2”.