Week 5: Plotting, Loops, Lists, & Conditionals in Python
Homework
Due Sunday, February 9th by 11:59pm to GitHub in a Jupyter Notebook file in your lab-exercises/week-five
directory.
Part 1 (Figures)
- Create a plot showing the standard deviation (
numpy.std
) of the inflammation data for each day across all patients. - Modify the program to display the three plots on top of one another instead of side by side.
Part 2 (Loops)
- Explain what a
for
loop does.
Python has a built-in function called range
that generates a sequence of numbers. range
can accept 1, 2, or 3 parameters.
- If one parameter is given,
range
generates a sequence of that length, starting at zero and incrementing by 1. For example,range(3)
produces the numbers0, 1, 2
. - If two parameters are given,
range
starts at the first and ends just before the second, incrementing by one. For example,range(2, 5)
produces2, 3, 4
. - If
range
is given 3 parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example,range(3, 10, 2)
produces3, 5, 7, 9
.
- Using
range
, write a loop that usesrange
to print the first 3 natural numbers:1 2 3
- Given the following loop:
word = 'oxygen' for char in word: print(char)
How many times is the body of the loop executed?
-
Write a loop that calculates the same result at
4 ** 6
(i.e., 4096) using multiplication (and without exponentiation). -
Knowing that two strings can be concatenated using the
+
operator, write a loop that takes a string and produces a new string with the characters in reverse order, so ‘Ucla’ becomes ‘alcU’. - The built-in function
enumerate
takes a sequence (e.g. a list) and generates a new sequence of the same length. Each element of the new sequence is a pair composed of the index (0, 1, 2,…) and the value from the original sequence:for idx, val in enumerate(a_list): # Do something using idx and val
The code above loops through
a_list
, assigning the index toidx
and the value toval
.
Suppose you have encoded a polynomial as a list of coefficients in the following way: the first element is the constant term, the second element is the coefficient of the linear term, the third is the coefficient of the quadratic term, etc.
x = 5
coefs = [2, 4, 3]
y = coefs[0] * x**0 + coefs[1] * x**1 + coefs[2] * x**2
print(y)
Write a loop using enumerate(coefs)
which computes the value y
of any polynomial, given x
and coefs
.
Part 3 (Lists)
- Explain what a list is.
- Explain how to create and index lists of simple values.
- Explain how to change the values of individual elements.
- Explain two ways to append values to an existing list.
- Use a for-loop to convert the string “hello” into a list of letters:
["h", "e", "l", "l", "o"]
- Use slicing to access only the last four characters of a string or entries of a list.
string_for_slicing = "Observation date: 02-Feb-2013" list_for_slicing = [["fluorine", "F"], ["chlorine", "Cl"], ["bromine", "Br"], ["iodine", "I"], ["astatine", "At"]]
- Would your solution work regardless of whether you knew beforehand the length of the string or list (e.g. if you wanted to apply the solution to a set of lists of different lengths)? If not, try to change your approach to make it more robust. Hint: Remember that indices can be negative as well as positive.
- So far we’ve seen how to use slicing to take single blocks of successive entries from a sequence. But what if we want to take a subset of entries that aren’t next to each other in the sequence? You can achieve this by providing a third argument to the range within the brackets, called the step size. The example below shows how you can take every third entry in a list:
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] subset = primes[0:12:3] print("subset", subset)
Notice that the slice taken begins with the first entry in the range, followed by entries taken at equally-spaced intervals (the steps) thereafter. If you wanted to begin the subset with the third entry, you would need to specify that as the starting point of the sliced range:
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] subset = primes[2:12:3] print("subset", subset)
Use the step size argument to create a new string that contains only every other character in the string “In an octopus’s garden in the shade”
beatles = "In an octopus's garden in the shade"
Part 4 (Conditionals)
- Consider the code block below:
if 4 > 5: print('A') elif 4 == 5: print('B') elif 4 < 5: print('C')
What would be printed if you ran this code? Explain your answer.
- Write a loop that counts the number of vowels in a character string. Test it on a few individual words and full sentences.
- (Challenge Question, Extra Credit): Write a loop that prints the following (must be exclusively done in Python code (not including the number signs…those are literal comments for the sake of typing up the shape in markdown)).
# # * # * * # * * * # * * * * # * * * * *