- 27th Jan 2024
- 15:06 pm
This exercise is meant to help you to understand how to use functions in Python. The student is asked to create a simple tool that converts temperatures from one temperature-type to another and then classify those temperatures into specific temperature classes.
- Part 1 - Simple temperature calculator,
- Part 2 - Temperature classifier
- Part 3 - Classify temperatures
Free Solution - Week 3 Exercise: Data Wrangling - Converting and Classifying Temperature Data
Please note that this is a sample assignment solved by our Python Programmers. These solutions are intended to be used for research and reference purposes only. If you can learn any concepts by going through the reports and code, then our Python Tutors would be very happy.
- Option 1 - To download the complete solution along with Code, Report and screenshots - Please visit our Python Assignment Sample Solution page
- Option 2 - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- Option 3 - You can check the partial solution for this assignment in this blog below
Assignment Solution
# -*- coding: utf-8 -*-
"""temp_analyzer.py
"""
# Python libraries are not required for this Exercise, but if you do load any libraries, do so before the dataset
# This file should contain all your Python code, including functions
# tempData list has 336 integers in the list. In later assignments we will load an external
# datafile (e.g. .txt or .csv), but as this dataset is not very large, we'll place the data directly in the Python code
# List of half-hourly temperature values (in degrees Fahrenheit) for one week
tempData = [19, 21, 21, 21, 23, 23, 23, 21, 19, 21, 19, 21, 23, 27, 27, 28, 30, 30, 32, 32, 32, 32, 34, 34,
34, 36, 36, 36, 36, 36, 36, 34, 34, 34, 34, 34, 34, 32, 30, 30, 30, 28, 28, 27, 27, 27, 23, 23,
21, 21, 21, 19, 19, 19, 18, 18, 21, 27, 28, 30, 32, 34, 36, 37, 37, 37, 39, 39, 39, 39, 39, 39,
41, 41, 41, 41, 41, 39, 39, 37, 37, 36, 36, 34, 34, 32, 30, 30, 28, 27, 27, 25, 23, 23, 21, 21,
19, 19, 19, 18, 18, 18, 21, 25, 27, 28, 34, 34, 41, 37, 37, 39, 39, 39, 39, 41, 41, 39, 39, 39,
39, 39, 41, 39, 39, 39, 37, 36, 34, 32, 28, 28, 27, 25, 25, 25, 23, 23, 23, 23, 21, 21, 21, 21,
19, 21, 19, 21, 21, 19, 21, 27, 28, 32, 36, 36, 37, 39, 39, 39, 39, 39, 41, 41, 41, 41, 41, 41,
41, 41, 41, 39, 37, 36, 36, 34, 32, 30, 28, 28, 27, 27, 25, 25, 23, 23, 23, 21, 21, 21, 19, 19,
19, 19, 19, 19, 21, 23, 23, 23, 25, 27, 30, 36, 37, 37, 39, 39, 41, 41, 41, 39, 39, 41, 43, 43,
43, 43, 43, 43, 43, 43, 43, 39, 37, 37, 37, 36, 36, 36, 36, 34, 32, 32, 32, 32, 30, 30, 28, 28,
28, 27, 27, 27, 27, 25, 27, 27, 27, 28, 28, 28, 30, 32, 32, 32, 34, 34, 36, 36, 36, 37, 37, 37,
37, 37, 37, 37, 37, 37, 36, 34, 30, 30, 27, 27, 25, 25, 23, 21, 21, 21, 21, 19, 19, 19, 19, 19,
18, 18, 18, 18, 18, 19, 23, 27, 30, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34, 34, 36, 36, 36, 36,
36, 32, 32, 32, 32, 32, 32, 32, 32, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 28, 28]
# Begin your functions and code here, do not forget to document your code with comments
def fahrToCelsius(tempFahrenheit):
"""
This function converts the input Fahrenheit temperature to Celsius.
It uses the formula given below:
Temperature(in Celsius) = [Temperature(in Fahrenheit) - 32] / 1.8
Parameters:
tempFahrenheit (float): The temperature in Fahrenheit to be converted to Celsius.
Returns:
float: The temperature in Celsius after the conversion from Fahrenheit.
"""
convertedTemp = (tempFahrenheit - 32) / 1.8
return convertedTemp
def tempClassifier(tempCelsius):
"""
This function reclassifies the input temperature based on the given criteria.
Return value Classification criteria
0 temperatures below -2 degrees (Celsius)
1 temperatures from -2 up to +2 degrees (Celsius) [1]
2 temperatures from +2 up to +15 degrees (Celsius) [2]
3 temperatures above +15 degrees (Celsius)
[1] Values -2 and +2 should belong to this class; [2] Value +15 should belong to this class
Parameters:
tempCelsius (float): The temperature in Celsius to be reclassified.
Returns:
int: The reclassified temperature as an integer number (0, 1, 2, or 3).
"""
if tempCelsius < -2:
return 0
elif -2 <= tempCelsius <= 2:
return 1
elif 2 < tempCelsius <= 15:
return 2
else:
return 3
# Create empty list to store temperature classes
tempClasses = []
# Iterate over Fahrenheit temperatures, comvert them into Celsius Degree temprature and
# Classift the Celsius Degree temprature
# Store the temprature classes
for tempFahr in tempData:
tempCelsius = fahrToCelsius(tempFahr)
tempClass = tempClassifier(tempCelsius)
tempClasses.append(tempClass)
# Count the number of temperatures in each class
numZeros = tempClasses.count(0)
numOnes = tempClasses.count(1)
numTwos = tempClasses.count(2)
numThrees = tempClasses.count(3)
# Print out the results
print(f"There are {numZeros} temperatures in class 0 : Temperature(in degrees Celsius) <-2 .")
print(f"There are {numOnes} temperatures in class 1 :-2<=Temperature(in degrees Celsius) <=2 .")
print(f"There are {numTwos} temperatures in class 2 :2 print(f"There are {numThrees} temperatures in class 3 :above 15 degrees Celsius.")
About The Author - Dr. Anika Gupta
Dr. Anika Gupta, a seasoned data scientist and educator, brings her expertise to the "Free Solution - Week 3 Exercise: Data Wrangling." With a Ph.D. in Data Science, Dr. Gupta is dedicated to simplifying complex data manipulation processes. Through this exercise solution, she provides valuable insights into converting and classifying temperature data, offering a practical resource for learners seeking hands-on experience in data wrangling.