Exercise 1: For-loops I

# 1. For-loops (looping through to perform basic operations)

# List of weights in lbs (pounds) and list of heights in cm for 12 individuals
weights = [142, 182, 110, 149, 201, 180, 159, 145, 161, 164, 190, 116]
heights = [164, 171, 155, 162, 169, 182, 152, 159, 163, 162, 174, 149]

# 1a - Using a for-loop convert the list of weights into kg putting the result
# into an empty list

# 1b - Using a for-loop convert the list of heights into meters putting the result
# into an empty list

# 1c - Using a for-loop and the zip function and the formula below, calculate
# BMIs for the 12 individuals

# BMI = (weight in kg) / (height in cm)**
# (**2 is python for squared)

Exercise 2: For-loops II

# For-loops 2 (looping through a list of lists...)

# List of total daily income (Mon-Fri) in thousands of $ for 12 consecutive weeks 
data = [[1.43, 1.77, 3.49, 8.18, 0.25],
        [3.76, 8.38, 8.65, 13.88, 0.21],
        [3.86, 1.09, 5.93, 8.34, 6.79],
        [0.43, 4.04, 8.91, 13.02, 9.36],
        [9.75, 2.67, 6.47, 12.96, 4.82],
        [2.98, 8.16, 4.3, 3.96, 4.53],
        [9.56, 0.97, 0.74, 5.94, 0.4],
        [2.63, 4.77, 5.01, 0.89, 1.2],
        [3.06, 1.37, 4.91, 13.02, 1.56],
        [3.82, 1.57, 4.02, 12.66, 4.5],
        [1.01, 3.78, 6.12, 12.52, 1.12],
        [0.88, 1.77, 3.19, 1.85, 4.8]]

# 2a - Use a for-loop to extract the daily income on Monday (first value in each list)
# and then find the mean

# 2b - Use a for-loop to extract the daily income on Thursday, then find the mean
# and the standard deviation

# 2c - Use a ttest to compare daily income between Monday and Thursday (HINT:
# may need to import a package to calculate ttest)

Exercise 3: For-loops III

temps = [36.04107573, 36.3017276 , 36.74023664, 37.24491007, 37.68720239,
       37.9544572 ,  0.        , 37.75348673, 37.33645069, 36.83371721,
       36.37333766, 36.07257545, 36.0080378 , 36.19616312, 36.58903396,
       37.08658207, 37.56207683, 37.89440485,  0.        , 37.8489978 ,
       37.48282837, 36.9936776 , 36.5061372 , 36.14438873, 36.00057325,
       36.11132203, 36.44842622, 36.92602193, 37.4224606 , 37.81129422,
        0.        , 37.92262121, 37.61675846, 37.15380098, 36.65166881,
       36.23826018, 36.01887451, 36.04939158, 36.32203839, 36.76736898,
       37.27195309, 37.70726795, 37.9624344 ,  0.        , 37.73478814,
       37.30995929, 36.80618061, 36.35176973, 36.06246976, 36.01196838]

# These data are from an implanted sensor that is recording body temperature in a
# hamster several times a day. Unfortunately, there seems to be something wrong with the
# sensor as it occasionally reads "0".

# 3a Make a new list (using a for-loop and a conditional if statement) to remove zeros
# and then calculate the mean and standard deviation of this new list.

# 3b With the initial list, use a for-loop and the enumerate function to print out the indices
# of when the sensor reads zero

# 3c Using the initial list, use a for-loop and enumerate to find the values that precede each zero
# measurement. Look at these values relative to the rest of the data and try to determine
# at what temperature the sensor is failing.

# 3-extra Based on the indices from 3b, can you hypothesize what the sampling frequency of the
# data is (i.e. how often was the sensor logging a temerature reading). It may be helpful to plot
# the data, e.g. using matplotlib

Exercise 4: List comprehensions

First, a little introduction to list comprehensions... Once I understood list comprehensions in Python, I started using them all the time in my code. I also saw them in other people's code (especially in the "top" answers on Stack Overflow etc. This is because they are a very quick and efficient way of processing data. And quite easy to understand once you start using them.

So, how do they work... Basically you write a for-loop that gets executed *at the same time* as a list is being created. This means that most of the for-loops you have made in your solutions to previous exercises can be re-written as a single line. No more creating an empty list and then appending to it!

They take the basic form:

    output = [x for value in list_of_stuff] # where x is usually related to value

What does this look like? Well, remember the first exercise when you had to convert lbs into kgs. You could complete this as follows.

    weights = [142, 182, 110, 149, 201, 180, 159, 145, 161, 164, 190, 116]
    weights_in_kgs = [value/2.2 for value in weights]
    print(weights_in_kgs)

Three extra things...

1. You can use conditional statements, e.g. look what happens if you do…

    heavy_weights_in_kgs = [value/2.2 for value in weights if value > 179]

2. You can use enumerate and zip as you would in a normal for-loop, e.g.

    indices_of_heavy_weights = [index for index, v in enumerate(weights) if v > 179]

3. You can use normal indexing on the list which can be very useful for finding, for excample, the first value that fulfills a criterion

    first_heavy_weight = [x for x in weights if x > 179][0]

Note: pay attention to the fact that I used x here instead of v or value. It really doesn't matter as long as it is consistent within the list comprehension.

So the exercise for this week is just to go through the first three challenges trying to replace for-loops with list comprehensions where you think there might be an opportunity.

Here’s a link with LCs explained again with some neat videos and extra examples (e.g. nested lists)

https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/