2.5.1. Solutions#

Solution to Exercise 2.14

# 1
for i in range(5, 17, 2):
    print(i)

# 2
for i in range(6):
    print(10**i)

# 3
for i in range(9):
    print(i % 3)

Solution to Exercise 2.15

import numpy as np
import matplotlib.pyplot as plt

temp_max = np.array([11, 11, 15, 14, 13, 9, 9, 9, 8, 7])
temp_min = np.array([8, 6, 11, 8, 7, 4, 4, 4, 3, 3])

plt.plot(temp_max, label="Max")
plt.plot(temp_min, label = "Min")
plt.xlabel("Day")
plt.ylabel("Temperature (degrees C)")
plt.legend()
plt.title("Daily Temperature in London")

Solution to

import numpy as np
import matplotlib.pyplot as plt

pressure_data = np.array([1018.3, 1018.3, 1015.7, 1014.3, 1011.8, 1011.4, 1015.5, 1016.0, 1016.9, 1016.8, 1016.4, 1017.5, 1018.8, 1018.1, 1017.1, 1018.4, 1022.0, 1022.8, 1021.8, 1020.5, 1021.0, 1019.8, 1018.9, 1018.4, 1017.8, 1018.1, 1019.6, 1017.4, 1015.8, 1015.5, 1017.5, 1018.9, 1017.7, 1014.4, 1014.2, 1016.0, 1016.1, 1015.6, 1016.4, 1015.7, 1016.6, 1019.6, 1021.6, 1021.4, 1020.6, 1017.6, 1016.5, 1016.2, 1013.0, 1005.4, 1007.4, 1012.2, 1015.2, 1016.1, 1014.3, 1012.4, 1014.2, 1013.1, 1012.9, 1012.1, 1010.6, 1010.0, 1010.5, 1010.3, 1007.4, 1008.9, 1007.4, 1006.9, 1009.8, 1014.8, 1014.9, 1016.6, 1014.1, 1011.1, 1010.7, 1009.8, 1011.9, 1012.6, 1011.8, 1009.8, 1008.9, 1010.6, 1009.9, 1010.2, 1009.5, 1009.0, 1007.1, 1007.1, 1007.5, 1005.0, 1004.2, 1004.2, 1007.2, 1005.0, 1002.9, 1007.4, 1010.4, 1010.6, 1008.6, 1006.2, 1005.9, 1006.8, 1004.6, 1002.4, 1003.2, 1004.4, 1003.1, 1000.9, 998.6, 999.9, 1001.6, 1002.1, 1004.1])
n = len(pressure_data)
pressure_smoothed = np.zeros(n)

for i in range(n-1):
    pressure_smoothed[i] = 0.5*(pressure_data[i] + pressure_data[i+1])

# I'll set the last element of the array to the same as the 
# previous element, otherwise it remains zero

pressure_smoothed[n-1] = pressure_smoothed[n-2]

plt.figure(figsize=(8,4))   
plt.plot(pressure_data)
plt.plot(pressure_smoothed)

Solution to

Coming soon.