Python for Geographic Data Analysis - Chapter 1
Python essentials
Learning Objective: This presents some essential programming concepts and how to apply them in the Python language. This chapter covers the necessary background to create your own basic programs and use common data structures and operations in Python.
Exercise 1 - Classifying temperatures
The list of temperatures below were measured at the Helsinki Malmi Airport in April 2013 with night, day, and evening temperatures recorded for each day.
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4,-2.5, -4.6, 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8, -0.1, -4.7, -5.6, 2.6, -2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4, 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3, 1.6, 1.5, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8, 4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6,-0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]
Answer the following questions:
#Solution:
temperatures = [-5.4, 1.0, -1.3, -4.8, 3.9, 0.1, -4.4, 4.0, -2.2, -3.9, 4.4,-2.5, -4.6, 5.1, 2.1, -2.4, 1.9, -3.3, -4.8, 1.0, -0.8, -2.8, -0.1, -4.7, -5.6, 2.6, -2.7, -4.6, 3.4, -0.4, -0.9, 3.1, 2.4, 1.6, 4.2, 3.5, 2.6, 3.1, 2.2, 1.8, 3.3, 1.6, 1.5, 4.7, 4.0, 3.6, 4.9, 4.8, 5.3, 5.6, 4.1, 3.7, 7.6, 6.9, 5.1, 6.4, 3.8, 4.0, 8.6, 4.1, 1.4, 8.9, 3.0, 1.6, 8.5, 4.7, 6.6, 8.1, 4.5, 4.8, 11.3, 4.7, 5.2, 11.5, 6.2, 2.9, 4.3, 2.8, 2.8, 6.3, 2.6,-0.0, 7.3, 3.4, 4.7, 9.3, 6.4, 5.4, 7.6, 5.2]
cold = []
slippery = []
comfortable = []
warm = []
for temperature in temperatures:
if temperature < -2:
cold.append(temperature)
elif temperature >= -2 and temperature < 2:
slippery.append(temperature)
elif temperature >= 2 and temperature < 15:
comfortable.append(temperature)
elif temperature >= 15:
warm.append(temperature)
# How many times was it cold in Helsinki in April 2013?
print(f"It was cold {len(cold)} times in Helsinki in April 2013.")
# How many times was it comfortable?
print(f"It was cold {len(comfortable)} times in Helsinki in April 2013.")
# Was it ever warm?
print(f"It was cold {len(warm)} times in Helsinki in April 2013.")
Result is,
It was cold 15 times in Helsinki in April 2013. It was cold 59 times in Helsinki in April 2013. It was cold 0 times in Helsinki in April 2013.
Exercise 2 - A temperature conversion function
Functions are commonly used for small calculations that occur frequently within a program, such as converting between units.
Create a function to converts temperature in degrees Fahrenheit to degrees Celsius.
Recommended by LinkedIn
Use the new function to convert the temperatures below to Celsius:
#Solution:
# Create temperature conversion function
def fahr_to_celsius(temp_fahrenheit):
converted_temp = (temp_fahrenheit - 32) / 1.8
return converted_temp
# Make a list of temperatures to convert and loop over them to convert
fahr_temps = [32, 68, 91, -17]
for fahr_temp in fahr_temps:
print(f"{fahr_temp} °F is equal to {fahr_to_celsius(fahr_temp):.1f} °C.")
Result is,
32 °F is equal to 0.0 °C. 68 °F is equal to 20.0 °C. 91 °F is equal to 32.8 °C. -17 °F is equal to -27.2 °C.
Exercise 3 - A temperature classifier function
Create a function that classifies temperatures based on Table 1
The function should return the following values for the different categories
Use the function to calculate the returned value the following temperatures:
#Solution:
def temp_classifier(temp_celsius):
"""
Classifies temperatures into 4 different classes according following criteria:
- Less than -2 degrees: returns 0
- Less than +2 degrees and greater than or equal to -2 degrees: returns 1
- Less than +15 degrees and greater than or equal to +2 degrees: returns 2
- Greater than or equal to 15 degrees: returns 3
"""
if temp_celsius < -2:
return 0
elif temp_celsius >= -2 and temp_celsius < 2:
return 1
elif temp_celsius >= 2 and temp_celsius < 15:
return 2
else:
return 3
# Create list of temperatures to classify and classify them
celsius_temps = [17, 2, 1.9, -2]
for celsius_temp in celsius_temps:
print(f"The temperature {celsius_temp} °C is in category {temp_classifier(celsius_temp)}.")
Result is,
The temperature 17 °C is in category 3. The temperature 2 °C is in category 2. The temperature 1.9 °C is in category 1. The temperature -2 °C is in category 1.