The purpose of this lab is to gain additional practice working with
lists. It looks at how to copy lists, process lists, use the special Python
syntaxes of list comprehension, the zip
function, and the sum
iterator.
Editor
in Spyder, create a new file for the functions you will
write in
this lab. Then save the new file with a name
representative of this lab.list = [1, 2, 3] newList = list print(list, newList)It looks like this code would put the elements of
list
into the newList
. Add a line
of code after the print statement that changes the second
element of newList
to 5. Then print both lists
again. What happens? In the next couple of exercises, you
will experiment with three different methods to actually make
a copy of a list.copy1
that takes a list
as a parameter and returns a new list that is a copy of the
original list. To create this copy, your function should
first create an empty list. It should then loop through the
original list, and append each element to the new list. When
the loop is finished,the function returns the new list.copy2
that takes a list
as a parameter and returns a new list that is a copy of the
original list. To create this copy, your function should
create an empty list and use the concatenate operator (+) with
the empty new list and the original list. The function should
then return the new list.copy3
that takes a list
as a parameter and returns a new list that is a copy of the
original list. To create this copy, your function should use
slicing on the original list to get all of the elements. Your
function should then return the new list.for
loop to iterate over the elements of the list, using one of
these patterns:
for item in list_name: for index in range(len(list_name)): do something with item do something with list_name[index]We will use these patterns in the next several exercises. (Try both patterns for one of the functions below, to see the difference.)
total
that takes a list of numeric values (float
or int) as a parameter and returns the cumulative total of all
elements in the list. The function will need to create a variable
(call it count
or something similar)
with initial value 0, which will be used to keep track of the
total. Use one of the loop patterns above to iterate over all of the
elements in the list.
This count
variable will get updated in the body of the loop that iterates
over the elements of the list. At the end of the function, this
variable gets returned. average
, that takes a list of
numeric values as input and returns the average of the elements
in the list. This function should look almost exactly like the
total
function, but will have one additional
calculation.for loop
inside square brackets.)
We will experiment with this in the next few exercises.
random.randint
function that we
used in the
earlier mini-lab on lists, or you may just create these
temperatures
and add them to the list yourself.
newTemps = [f+10 for f in
temps]
where temps
is the original list of temperatures.
This line of code is the same as:
newTemps = []
for f in temps:
newTemps.append(f+10)
Add this 1 new line of code to your file and test that it works.for loop
to access the corresponding
elements from both lists to print at one time. Python
offers a special syntax to do this with the zip
function. It turns a set of n lists into one list
of n-tuples, where the element of the first
n-tuple contains the first elements of each of
the lists, and so on. So we could write something
like:
for f, c in zip(temps, newTemps):
print f, c
Add this code to your program and test that it
works.sum
iterator that
actually
allows us to write the code for the total
function that we wrote earlier in one
line by using list comprehension. If we
had a list called numbers
, to get the sum
of all of
these numbers, we could write:
count = sum(x for x in
numbers)
Create a second version of your total
function
from above called total2
that uses
the sum iterator to calculate the total. The total should be returned at the end of the function.zip
function should be used to traverse the two
lists for printing. (See Exercises 2.7, 2.8, and 2.9 in
your textbook.)
Stop and Think: What does this equation compute? What are the values of v0 and g in this formuala? Should either (or both) of them be parameters to your function(s)? (If you are unsure of how to answer any of these functions, please ask an instructor or TA.