Instructor: Every time I go to the states,
I always get very confused with what to wear,
because I never know what the temperature actually is
because I think in Celsius
and it's really hard actually
to get used to thinking in Fahrenheit,
like what is cold, what is warm.
So in this exercise,
we're going to build a Python program to do that for us.
We're going to take an input,
which is already in the format of a dictionary,
and each day has a temperature associated with it,
a number in Celsius,
and we're going to output, similarly,
a dictionary where the keys are exactly the same.
Monday, Tuesday, through to Sunday,
however we're going to convert the values into Fahrenheit.
In the description pane, you'll see the formula
that is used to convert Celsius into Fahrenheit,
and what you are gonna be doing is writing some code
to do some dictionary comprehension,
and take a dictionary and create a dictionary.
If you take a look at the example input,
and what the example output would look like,
then you'll understand what our goal is.
And if you take a look at line one,
you'll see that we have this code called eval,
which all it does is takes the input as a string
and converts it into a dictionary.
You can try running the code and printing out weather_c
to see what that variable looks like as it is right now.
But your goal is to write the code
that takes the example input,
dictionaries with Celsius values,
converted into the example output,
dictionary with Fahrenheit values.
So have a go now.
The first thing we do in our dictionary comprehension
is to create the new variable, weather_f,
and then set it equal to a new dictionary.
What we're going to loop through
is a list of weather items in Celsius.
Now we're using the .items method that comes from Python,
which takes our dictionary and outputs a list
where each key and value pair is an item in the list.
And if you print out weather_c.items as it is,
you'll see that each key value pair
is converted to what's known as a tuple.
A tuple is something
that is contained within a set of parentheses
where the values in the tuple
are associated with each other,
but they can have different data types.
So in our case,
we've got a string and we've got a number
that has been split out where each item
is added into the list.
The next part is to loop through each of those tuples.
We're using our for loop,
as standard in our dictionary comprehension,
and we're defining what we are looping through
and giving each of those parts
of the tuple a name to work with.
We're writing for parentheses,
which is the structure of a tuple,
the first part is day,
which is our string that represents a day of the week,
comma, and then the temperature,
which is gonna represent the number
that we're gonna work with later on.
So now we have a for loop
that loops through each of the items
in our dictionary as a list,
and we have a way to access the day of the week string
and the temperature value.
Now, all that's left to do
is to structure each of the values
in our new dictionary that we wanna output.
The key value structure is simple.
The key is going to be the day of the week,
completely unaltered,
and then we add the colon, and then we put in the value.
Now the value is where we do our conversion.
So we take the temp variable,
which represents the temperature in Celsius from weather_c,
and we apply the formula that converts it into Fahrenheit,
which is to multiply it by nine, divide by five,
and then add 32, and that will convert our Celsius degrees
into Fahrenheit degrees.
And it gets assigned to the value
of each of the new items in the dictionary.
This line of code alone on line five is incredibly terse.
It's incredibly succinct.
For better or for worse.
It can be difficult to understand,
but once you get the hang of it,
and once you figure out how to read it,
using, say, the keyword method,
or whichever method that you are comfortable with,
then it becomes easier to decode and understand,
and it can be really, really powerful
in just one line of code.
Hopefully you managed to achieve this.
If not, go back to your code, fix it,
and make sure you understand what's going on
before you proceed.
There's no shame in fixing your code
after looking at the solution.
This is all a part of the learning process.