1
00:00:00,480 --> 00:00:01,680
Instructor: Every time I go to the states,

2
00:00:01,680 --> 00:00:05,040
I always get very confused with what to wear,

3
00:00:05,040 --> 00:00:08,610
because I never know what the temperature actually is

4
00:00:08,610 --> 00:00:09,854
because I think in Celsius

5
00:00:09,854 --> 00:00:12,270
and it's really hard actually

6
00:00:12,270 --> 00:00:15,060
to get used to thinking in Fahrenheit,

7
00:00:15,060 --> 00:00:17,070
like what is cold, what is warm.

8
00:00:17,070 --> 00:00:18,060
So in this exercise,

9
00:00:18,060 --> 00:00:21,330
we're going to build a Python program to do that for us.

10
00:00:21,330 --> 00:00:22,950
We're going to take an input,

11
00:00:22,950 --> 00:00:26,040
which is already in the format of a dictionary,

12
00:00:26,040 --> 00:00:30,060
and each day has a temperature associated with it,

13
00:00:30,060 --> 00:00:32,549
a number in Celsius,

14
00:00:32,549 --> 00:00:35,220
and we're going to output, similarly,

15
00:00:35,220 --> 00:00:38,550
a dictionary where the keys are exactly the same.

16
00:00:38,550 --> 00:00:41,310
Monday, Tuesday, through to Sunday,

17
00:00:41,310 --> 00:00:46,290
however we're going to convert the values into Fahrenheit.

18
00:00:46,290 --> 00:00:48,810
In the description pane, you'll see the formula

19
00:00:48,810 --> 00:00:52,410
that is used to convert Celsius into Fahrenheit,

20
00:00:52,410 --> 00:00:55,980
and what you are gonna be doing is writing some code

21
00:00:55,980 --> 00:00:58,200
to do some dictionary comprehension,

22
00:00:58,200 --> 00:01:02,340
and take a dictionary and create a dictionary.

23
00:01:02,340 --> 00:01:05,160
If you take a look at the example input,

24
00:01:05,160 --> 00:01:08,760
and what the example output would look like,

25
00:01:08,760 --> 00:01:12,000
then you'll understand what our goal is.

26
00:01:12,000 --> 00:01:14,820
And if you take a look at line one,

27
00:01:14,820 --> 00:01:17,820
you'll see that we have this code called eval,

28
00:01:17,820 --> 00:01:20,610
which all it does is takes the input as a string

29
00:01:20,610 --> 00:01:23,490
and converts it into a dictionary.

30
00:01:23,490 --> 00:01:26,040
You can try running the code and printing out weather_c

31
00:01:26,040 --> 00:01:29,730
to see what that variable looks like as it is right now.

32
00:01:29,730 --> 00:01:32,580
But your goal is to write the code

33
00:01:32,580 --> 00:01:34,830
that takes the example input,

34
00:01:34,830 --> 00:01:37,470
dictionaries with Celsius values,

35
00:01:37,470 --> 00:01:39,990
converted into the example output,

36
00:01:39,990 --> 00:01:42,720
dictionary with Fahrenheit values.

37
00:01:42,720 --> 00:01:43,893
So have a go now.

38
00:01:50,316 --> 00:01:53,550
The first thing we do in our dictionary comprehension

39
00:01:53,550 --> 00:01:56,850
is to create the new variable, weather_f,

40
00:01:56,850 --> 00:01:59,793
and then set it equal to a new dictionary.

41
00:02:01,020 --> 00:02:03,780
What we're going to loop through

42
00:02:03,780 --> 00:02:08,780
is a list of weather items in Celsius.

43
00:02:10,620 --> 00:02:15,540
Now we're using the .items method that comes from Python,

44
00:02:15,540 --> 00:02:20,540
which takes our dictionary and outputs a list

45
00:02:20,940 --> 00:02:25,940
where each key and value pair is an item in the list.

46
00:02:26,940 --> 00:02:31,440
And if you print out weather_c.items as it is,

47
00:02:31,440 --> 00:02:34,530
you'll see that each key value pair

48
00:02:34,530 --> 00:02:37,931
is converted to what's known as a tuple.

49
00:02:37,931 --> 00:02:39,510
A tuple is something

50
00:02:39,510 --> 00:02:42,330
that is contained within a set of parentheses

51
00:02:42,330 --> 00:02:44,370
where the values in the tuple

52
00:02:44,370 --> 00:02:46,200
are associated with each other,

53
00:02:46,200 --> 00:02:48,600
but they can have different data types.

54
00:02:48,600 --> 00:02:49,433
So in our case,

55
00:02:49,433 --> 00:02:52,170
we've got a string and we've got a number

56
00:02:52,170 --> 00:02:55,020
that has been split out where each item

57
00:02:55,020 --> 00:02:56,643
is added into the list.

58
00:02:58,440 --> 00:03:03,180
The next part is to loop through each of those tuples.

59
00:03:03,180 --> 00:03:04,680
We're using our for loop,

60
00:03:04,680 --> 00:03:07,440
as standard in our dictionary comprehension,

61
00:03:07,440 --> 00:03:10,350
and we're defining what we are looping through

62
00:03:10,350 --> 00:03:12,690
and giving each of those parts

63
00:03:12,690 --> 00:03:15,210
of the tuple a name to work with.

64
00:03:15,210 --> 00:03:17,070
We're writing for parentheses,

65
00:03:17,070 --> 00:03:18,870
which is the structure of a tuple,

66
00:03:18,870 --> 00:03:20,910
the first part is day,

67
00:03:20,910 --> 00:03:24,480
which is our string that represents a day of the week,

68
00:03:24,480 --> 00:03:26,580
comma, and then the temperature,

69
00:03:26,580 --> 00:03:28,860
which is gonna represent the number

70
00:03:28,860 --> 00:03:31,410
that we're gonna work with later on.

71
00:03:31,410 --> 00:03:33,120
So now we have a for loop

72
00:03:33,120 --> 00:03:35,280
that loops through each of the items

73
00:03:35,280 --> 00:03:37,830
in our dictionary as a list,

74
00:03:37,830 --> 00:03:41,370
and we have a way to access the day of the week string

75
00:03:41,370 --> 00:03:43,653
and the temperature value.

76
00:03:44,490 --> 00:03:45,990
Now, all that's left to do

77
00:03:45,990 --> 00:03:48,450
is to structure each of the values

78
00:03:48,450 --> 00:03:51,450
in our new dictionary that we wanna output.

79
00:03:51,450 --> 00:03:53,730
The key value structure is simple.

80
00:03:53,730 --> 00:03:55,890
The key is going to be the day of the week,

81
00:03:55,890 --> 00:03:57,630
completely unaltered,

82
00:03:57,630 --> 00:04:01,170
and then we add the colon, and then we put in the value.

83
00:04:01,170 --> 00:04:04,620
Now the value is where we do our conversion.

84
00:04:04,620 --> 00:04:06,750
So we take the temp variable,

85
00:04:06,750 --> 00:04:11,040
which represents the temperature in Celsius from weather_c,

86
00:04:11,040 --> 00:04:15,030
and we apply the formula that converts it into Fahrenheit,

87
00:04:15,030 --> 00:04:17,970
which is to multiply it by nine, divide by five,

88
00:04:17,970 --> 00:04:22,380
and then add 32, and that will convert our Celsius degrees

89
00:04:22,380 --> 00:04:24,060
into Fahrenheit degrees.

90
00:04:24,060 --> 00:04:26,580
And it gets assigned to the value

91
00:04:26,580 --> 00:04:29,760
of each of the new items in the dictionary.

92
00:04:29,760 --> 00:04:34,140
This line of code alone on line five is incredibly terse.

93
00:04:34,140 --> 00:04:36,180
It's incredibly succinct.

94
00:04:36,180 --> 00:04:38,010
For better or for worse.

95
00:04:38,010 --> 00:04:39,690
It can be difficult to understand,

96
00:04:39,690 --> 00:04:41,310
but once you get the hang of it,

97
00:04:41,310 --> 00:04:44,490
and once you figure out how to read it,

98
00:04:44,490 --> 00:04:46,560
using, say, the keyword method,

99
00:04:46,560 --> 00:04:50,130
or whichever method that you are comfortable with,

100
00:04:50,130 --> 00:04:53,485
then it becomes easier to decode and understand,

101
00:04:53,485 --> 00:04:55,440
and it can be really, really powerful

102
00:04:55,440 --> 00:04:57,450
in just one line of code.

103
00:04:57,450 --> 00:04:59,340
Hopefully you managed to achieve this.

104
00:04:59,340 --> 00:05:01,470
If not, go back to your code, fix it,

105
00:05:01,470 --> 00:05:03,600
and make sure you understand what's going on

106
00:05:03,600 --> 00:05:04,950
before you proceed.

107
00:05:04,950 --> 00:05:07,020
There's no shame in fixing your code

108
00:05:07,020 --> 00:05:08,850
after looking at the solution.

109
00:05:08,850 --> 00:05:11,373
This is all a part of the learning process.

