Python for Geographic Data Analysis - Chapter 1
Part 1

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.

Article content
Table1

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:

  • How many times was it cold in Helsinki in April 2013?
  • How many times was it comfortable?
  • Was it ever warm?

#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.

Use the new function to convert the temperatures below to Celsius:

  • 32 °F
  • 68 °F
  • 91 °F
  • -17 °F

#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

  • 0: Cold
  • 1: Slippery
  • 2: Comfortable
  • 3: Warm

Use the function to calculate the returned value the following temperatures:

  • +17 °C
  • +2 °C
  • +1.9 °C
  • -2 °C

#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.

To view or add a comment, sign in

More articles by Sneha A

  • Basic SQL questions

    Write the following queries in SQL, using the university schema Please, Take the sample data from attached link for…

  • Implementing Talend Job for Incremental Data Processing

    Recently I was stuck with one logic for hours..

    6 Comments
  • How to configure tESBConsumer

    It is similar to tRESTClient. It is used to call external API.

  • File upload or File download to the endpoint

    tHTTPClient tHTTPClient is a versatile component in Talend that enables you to access web services hosted on HTTP…

  • Talend ESB - SOAP listener

    Here, the scenario is listening the soap service continuously. Place tESBProviderRequest and tESBProviderResponse and…

  • Is JSON Structure handling difficult?

    Actually not, If you understand the JSON structure. In this article we are going to see about the objects within JSON…

    2 Comments
  • Dynamic Schema - DB to File

    The dynamic column retrieves the columns which are undefined in the schema. It can be only one column, also dynamic…

  • How to convert zipped binary data to String format?

    Here Source is kafka consumer component. It contains all the zipped binary data.

  • Extracting data from Kafka and loading into DB

    Kafka is a distributed event store and stream-processing platform. It can publish and consume JSON and XML Structures.

  • Talend Data Integration - Introduction

    There are many technologies booming now a days in the world. Cloud and Big data is one of the emerging technology in IT.

Others also viewed

Explore content categories