100 days of code: Day 21 - maximum minimum

Last modified on 2020-06-29

Source: 100-days-of-code.

Given a dictionary in Python of key-value pairs composed of dates-temperatures, I created a program to find the maximum and minimum temperatures and the dates recorded …

temps = {'2014-02-02': '18.4', '2014-01-26': '16.2', '2014-01-19': '17.6', '2014-01-12': '19.8'}
# Set a starting date and value for high and low temperatures.
h_date, l_date = list(temps)[0], list(temps)[0]
h_temp, l_temp = temps[h_date], temps[h_date]

for k, v in temps.items():
    if v > h_temp:
        h_temp = v
        h_date = k
    elif v < l_temp:
        l_temp = v
        l_date = k

h_message = f'High temperature: {h_temp}C recorded on {h_date}.'
l_message = f'Low temperature: {l_temp}C recorded on {l_date}.'

print(h_message)
print(l_message)

Run …

$ ./max_min_dict.py 
High temperature: 19.8C recorded on 2014-01-12.
Low temperature: 16.2C recorded on 2014-01-26.

Happy hacking!