| Goals: |
|
A data structure is a way of organizing data. For our purposes, we will work with one type of data structure in Python, namely lists. In Python, a list is a finite sequence of things, separated by commas, beginning and ending with brackets [ and ], respectively. For example, [3, 6, 3, 5, -1] is a list. In a list, order and location matter. For example, the following lists are all different: [2], [2, 2], [2, 3], [3, 2].
An element in a list is located by its index. The first index is 0, and the index increases by 1 for each element as you scan from left to right. For example, in the list [3, 6, -2], element 3 has index 0, 6 has index 1, and -2 has index 2. In Python, to access the value with index i in a list L, the syntax is L[i]. For example, suppose L = [3, 4, 1, 5, -3, 9]. Then L[0] returns 3, while L[2] returns 1.
The number of elements in a list is its length. For example, [9,9] has length 2, while [[1, 2], 3, -4] has length 3. A useful function, called len, returns the length of a given list. For example, if L = [3, 4, 1, 5, -3, 9], then len(L) returns 6. The function len is a key function for working through elements of a list.
Compute the sum of squares of a list of numbers i.e., compute
Solution. Because we can compute the length of a list, we can use a while loop to build the sum, terminating once the list of elements is traversed:
Note that in the loop the last iteration of the loop occurs when the index of the last element in the list; when gets bumped up to the loop terminates. Figure 5.18 shows the code in action:
Lists have built in functions, called methods. We’ll make use of two, namely append and count. Here’s how each works: Suppose L is the name of a list stored in Python’s memory, then:
L.append(x) appends x to the end of list L;
L.count(x) returns the number of times x is in list L.
Figure 5.19 provides examples of using the two methods:
Suppose we have a 4-sided die with faces marked as 1, 2, 3, and 4. And, suppose that if in rolling the die, the chance of rolling one of the numbers is given by the following discrete distribution:
Suppose the die is rolled twice, what is the probability that the two rolls sum to 5?
Solution. We can approximate an answer using a simulation, using a list to store the simulated data.
Using randbetween(1,5), we can simulate rolling the die once by declaring:
To simplify the problem, we can define a function, say roll, that simulates rolling a single die. Then executing roll() + roll() will simulate rolling twice and computing the sum. Appending these results to a list will build a simulated sample, and from that we can compute the sample proportion of 5’s. Here’s the code for roll:
Figure 5.20 shows roll in action:
Lastly, we need to count the number of 5’s in a sample and divide by the sample size. We can do that with the following command:
(Recall that float will convert the integer into a floating-point number.) Adjusting the return line in go will yield results as shown in Figure 5.22.
What is the output of the following code:
Without using a library, such as numpy, write a function that takes as input a list of numbers, and outputs their mean. Compare your output to the output of the numpy function mean.
Write a function that takes as input a list, and outputs a list with elements listed in the opposite order.