WEBVTT

0
00:00.230 --> 00:06.320
Previously, we saw how the len() function gives us a TypeError when we give it a number instead of

1
00:06.320 --> 00:07.160
a string.

2
00:07.280 --> 00:11.330
So let's talk a little bit more about data types and functions.

3
00:11.480 --> 00:17.600
And the way I think about functions is kind of like some sort of fancy machine that you might see in

4
00:17.600 --> 00:18.440
a factory.

5
00:18.440 --> 00:18.800
Right?

6
00:18.800 --> 00:24.530
So in this case, we've got some sort of machine that's going to take potatoes into chips, and we don't

7
00:24.530 --> 00:28.970
really care how it does it, but it's probably going to have to peel the potatoes, wash the potatoes,

8
00:28.970 --> 00:34.920
cut it up, fry it, and then finally return it to us as an output in the form of fries.

9
00:35.100 --> 00:41.070
And if this is the first time that you're watching one of my tutorials, this is probably a health

10
00:41.070 --> 00:41.670
warning,

11
00:41.670 --> 00:43.290
I talk about food a lot.

12
00:43.410 --> 00:47.250
So be prepared if you haven't eaten.

13
00:47.430 --> 00:53.730
Now, let's say that we took the same machine that normally processes potatoes, and we decide to give

14
00:53.740 --> 00:59.410
it a rock, and we just pass this through the function, then we're going to get an error.

15
00:59.410 --> 00:59.980
Right?

16
00:59.980 --> 01:02.290
This is basically what happened here.

17
01:02.290 --> 01:06.760
This len() function doesn't like working with integers,

18
01:06.760 --> 01:13.480
and by forcing this through we end up with an error and our code breaks and it gives us this thing, a

19
01:13.480 --> 01:14.470
TypeError.

20
01:14.470 --> 01:20.530
Now if we hover over this yellow warning we also get the explanation here.

21
01:20.530 --> 01:27.190
Expected type...something called, 'Sized', and got 'int' or an integer instead.

22
01:27.190 --> 01:33.160
So this is already pretty clear where our mistake is even before we hit Run.

23
01:33.160 --> 01:40.390
And the other helpful thing is, when you hover over any function in PyCharm, it gives you the definition

24
01:40.390 --> 01:40.900
of it,

25
01:40.900 --> 01:46.380
and in between the brackets, you can see the data type that that function expects.

26
01:46.380 --> 01:51.840
So the len() function expects a data type with something that's called, Sized.

27
01:51.840 --> 01:59.580
And we can actually click on this link to see the documentation on docs.python.org, which is the official

28
01:59.580 --> 02:01.080
Python documentation.

29
02:01.080 --> 02:07.230
Now I know previously I told you to try using something like W3Schools or using StackOverflow, and

30
02:07.230 --> 02:15.600
the reason is because the documentation from the official Python organization is very, very short.

31
02:15.600 --> 02:18.480
So sometimes it can be enough,

32
02:18.480 --> 02:26.160
but very often I find that it's helpful to find another source to back up this very, very brief explanation.

33
02:26.910 --> 02:30.690
But in our case, in this situation, it does help us.

34
02:30.690 --> 02:37.220
For example, it tells us that len() will return the length, which is the number of items of an object,

35
02:37.220 --> 02:44.900
and the argument, which is what you put in between the brackets may be a sequence such as a string,

36
02:44.900 --> 02:50.480
byte, tuple, list or range, or a collection such as a dictionary set or frozen set.

37
02:50.510 --> 02:54.680
Don't worry about these other data types that we haven't come across yet.

38
02:54.680 --> 02:59.630
We're going to come across them in due time and we're going to be learning all about them.

39
02:59.630 --> 03:03.560
But for now, you already see one that you recognize which is string.

40
03:03.560 --> 03:05.120
So it can take string,

41
03:05.120 --> 03:11.780
but nowhere in this list does it say it can take an integer or any other number data type.

42
03:13.820 --> 03:21.620
So as the first pause exercise, I want you to fix this len() function so that we actually give it data

43
03:21.620 --> 03:23.390
type that it recognizes.

44
03:23.390 --> 03:28.700
And we fix this TypeError so that when we run it, it works as expected.

45
03:28.700 --> 03:32.750
And you can put anything in there as long as it gets rid of the warnings and errors.

46
03:32.750 --> 03:34.040
Pause the video now.

47
03:35.720 --> 03:36.170
All right.

48
03:36.170 --> 03:37.850
This should be relatively easy.

49
03:37.850 --> 03:44.240
We can change this to any string, which we already know is a data type that this function can accept.

50
03:44.240 --> 03:49.180
So we can simply change it to "Hello" or whichever string you can think of.

51
03:49.180 --> 03:54.760
And now when we hit run, all our errors and warnings should now go away.

52
03:54.760 --> 04:01.780
If you see something like "PEP 8 blank line at end of file," and it bothers you, don't worry about it too

53
04:01.780 --> 04:02.110
much,

54
04:02.110 --> 04:09.220
but it's basically saying that in an ideal situation, a Python file should only have one blank line

55
04:09.220 --> 04:10.270
at the very bottom.

56
04:10.270 --> 04:15.190
So if I hit Run again, that will clear up that warning. For everybody out there who has a little bit

57
04:15.190 --> 04:18.010
of OCD like me, this can be a lifesaver.

58
04:18.820 --> 04:27.670
So now that we've seen that in Python, each function expects to work with a specific set of data types,

59
04:27.670 --> 04:34.860
so there's only certain data types that can go into the parentheses of a particular function.

60
04:34.890 --> 04:36.540
Now here comes the question,

61
04:36.540 --> 04:41.940
"Well, how would we know what data type a particular piece of data has?"

62
04:41.940 --> 04:44.940
So how would we know what is the data type of

63
04:44.940 --> 04:45.510
"Hello"?

64
04:45.510 --> 04:49.680
Well it's very easy to visually see it because it has the double quotes around it,

65
04:49.680 --> 04:55.080
and anything with double quotes around it, even if it's a number, is in fact a string.

66
04:55.320 --> 05:03.390
But there is also one function in Python that allows us to check the data type of any piece of data

67
05:03.390 --> 05:04.380
or variable.

68
05:04.380 --> 05:08.400
So all we need to do is to use the keyword "type",

69
05:08.400 --> 05:12.480
and then in between the parentheses we can put in any piece of data.

70
05:12.480 --> 05:14.460
It can accept any data type.

71
05:14.460 --> 05:18.570
And then it will give us in return the data type.

72
05:18.570 --> 05:25.550
But as always in order to see it we have to wrap this inside a set of parentheses and use the print

73
05:25.550 --> 05:29.120
statement to put this into the output area.

74
05:29.120 --> 05:30.350
So let's run that.

75
05:30.350 --> 05:35.030
And you can see it tells us that this is something called a class 'str'.

76
05:35.030 --> 05:39.920
So this is basically saying it checked the data type of this piece of data

77
05:39.920 --> 05:40.640
we gave it,

78
05:40.640 --> 05:45.500
and it told us that it's actually a string which is what we expected as well.

79
05:45.500 --> 05:48.320
So we can do this with various different data types.

80
05:48.320 --> 05:49.880
Let's check it with the number.

81
05:49.880 --> 05:52.400
Well in this case we gave it a whole number,

82
05:52.400 --> 05:53.780
so no decimal places.

83
05:53.780 --> 05:56.000
And it told us it's an integer.

84
05:56.000 --> 05:59.570
We could do this with floats with booleans,

85
05:59.570 --> 06:04.700
and it will always very, very helpfully tell us what is the data type.

86
06:04.700 --> 06:07.490
And this is something called type checking.

87
06:07.670 --> 06:10.760
So now it's time for another quick pause

88
06:10.760 --> 06:13.340
check to make sure you understood what's going on.

89
06:13.340 --> 06:21.110
I want you to write out four lines of code to print out the four different data types that we have already

90
06:21.110 --> 06:22.010
learned about.

91
06:22.010 --> 06:27.230
So it's going to be the string, the integer, the float, and the boolean.

92
06:27.230 --> 06:33.680
And we want all four of these printed out in four lines in the output area, using what you've learned

93
06:33.680 --> 06:40.090
and the correct data type to put in inside the parentheses before the type check.

94
06:40.090 --> 06:42.700
So pause the video now and give this a go.

95
06:46.150 --> 06:49.690
All right so we've got already the integer type.

96
06:49.690 --> 06:53.200
So let's put the string one above it.

97
06:53.560 --> 06:58.750
And we know that all strings go inside a set of double quotes.

98
06:58.750 --> 07:01.750
And then we want a floating point number.

99
07:01.750 --> 07:05.530
So we could do a 3.14.

100
07:05.530 --> 07:12.910
And finally, we want a boolean type which we know is either True or False with a capital T and capital

101
07:12.910 --> 07:13.390
F.

102
07:13.390 --> 07:17.890
So now if I hit Run, we should see class str, int, float and bool.

103
07:17.890 --> 07:25.650
The full collection of all the major primitive Python data types that we learned about in the last lesson.

104
07:25.650 --> 07:31.590
If that was confusing at all, review the last lesson and hopefully this all makes perfect sense.

105
07:32.550 --> 07:37.230
Now what if we aren't happy with the assigned data type?

106
07:37.260 --> 07:42.330
What if we want to convert a piece of data into a different data type?

107
07:42.330 --> 07:48.030
Well, then we would need to learn about something called type conversion, also known as type casting

108
07:48.030 --> 07:49.020
in Python.

109
07:49.020 --> 07:53.550
So let's go ahead and clear the board to make this less confusing.

110
07:53.550 --> 07:56.850
And let's start out with a very simple string.

111
07:56.850 --> 08:03.180
So we all know that this is a string because it's enclosed in a set of double quotes.

112
08:03.180 --> 08:07.320
But what if I wanted to turn it into an integer?

113
08:07.320 --> 08:16.640
Well, I can use one of the built-in functions called "int" and I can convert this into a number.

114
08:17.030 --> 08:25.760
So whereas previously we saw that if we tried to print the string "123" + "456",

115
08:25.760 --> 08:31.460
then we would end up with 1, 2, 3, 4, 5, 6.

116
08:31.460 --> 08:40.730
And because both of these pieces of data are string data types, the plus sign does concatenation instead

117
08:40.730 --> 08:43.700
of maths and adding these two together.

118
08:43.700 --> 08:50.960
But now that we've converted this 1, 2, 3 into an integer, you can see that if we replace

119
08:50.960 --> 08:59.240
this string with this type casted integer and we could do the same for 4, 5, 6.

120
08:59.240 --> 09:05.630
Now if I hit Run, you'll see that it actually treats them as numbers and adds them together.

121
09:05.630 --> 09:08.840
So this is the power of type conversion.

122
09:08.840 --> 09:16.370
Now, one of the dangerous things about this is of course sometimes you can't convert things into a

123
09:16.370 --> 09:17.600
different data type.

124
09:17.690 --> 09:26.090
For example, if I tried to convert, "ABC" into a number, well, that doesn't really make sense.

125
09:26.090 --> 09:30.100
If I gave that to you, what would ABC be as a number?

126
09:30.100 --> 09:30.910
I don't know.

127
09:30.910 --> 09:35.740
So if we run this, you can see we get something called a ValueError.

128
09:35.740 --> 09:40.600
So I hope you're collecting these errors in a notebook somewhere because we're coming across lots of

129
09:40.600 --> 09:40.930
them,

130
09:40.930 --> 09:45.010
and you will come across more in your lifetime as a Python developer.

131
09:45.100 --> 09:55.120
So this is saying that there is an invalid literal for the int, which is ABC, and so it doesn't really

132
09:55.120 --> 09:58.840
make sense to convert this into a number is what it's trying to tell you.

133
09:58.870 --> 10:04.300
So you have to be really careful when you're doing type conversions because sometimes you can end

134
10:04.300 --> 10:08.350
up with errors and sometimes you can just simply lose data.

135
10:08.350 --> 10:14.500
But we can use this method to convert things into all of the four major primitive types that we've learned

136
10:14.500 --> 10:21.210
about. So we can convert to an integer, we can convert into a float, we can convert into a string,

137
10:21.210 --> 10:24.780
and we can even convert into a bool.

138
10:26.100 --> 10:33.030
So to check that you've understood how to convert between different data types and why we get different

139
10:33.030 --> 10:40.260
type errors, I have a final pause challenge for you, which is to make this line of code run without

140
10:40.260 --> 10:40.830
errors.

141
10:40.830 --> 10:46.770
So I want you to go ahead and copy this line of code, paste it into your code file, and try to hit run.

142
10:46.770 --> 10:55.740
Now go ahead and enter a name triggered by this input, and then you will see that you get a TypeError.

143
10:55.740 --> 10:57.810
So read through the TypeError,

144
10:57.840 --> 11:03.600
see if you can understand what it's trying to tell you, and fix this code so that it does what you

145
11:03.600 --> 11:09.920
expect it to do, which is to print out the number of letters in the name that the user submits into

146
11:09.920 --> 11:10.610
the input.

147
11:10.640 --> 11:11.900
Pause the video.

148
11:11.900 --> 11:12.830
Give this a go.

149
11:17.150 --> 11:17.750
All right.

150
11:17.750 --> 11:24.140
So in order to break this problem down, let's use some variables to simplify things and make it easier

151
11:24.140 --> 11:24.770
to read.

152
11:24.770 --> 11:32.720
Let's take this second part of this print statement, and let's take it out into separate variables.

153
11:32.720 --> 11:42.320
So we know that we can get the name_of_the_user by using this input() function,

154
11:42.620 --> 11:48.170
and I'm making these variable names extra wordy just so that when you read it, it makes it a little

155
11:48.170 --> 11:49.250
bit more sense.

156
11:49.250 --> 11:54.240
And I'm actually going to delete the rest of this as well, just to make everything easy to read.

157
11:54.270 --> 12:00.120
So next we need the length_of_name,

158
12:01.320 --> 12:05.580
and this is done by using the len() function,

159
12:05.580 --> 12:09.720
and we pass in the name of the user that we got from line one.

160
12:09.870 --> 12:16.980
So now finally, what we have here is the length of the name which we want to tag to the end of this print

161
12:16.990 --> 12:17.800
statement.

162
12:17.830 --> 12:20.740
Now what is this error trying to tell us?

163
12:20.740 --> 12:28.930
It's saying you can only concatenate string to a string, not an integer, which is what it sees in

164
12:28.930 --> 12:30.730
this line of code.

165
12:30.730 --> 12:32.740
So where is the integer?

166
12:32.740 --> 12:38.920
Well, we know that when we calculate the length of something it gives us a number.

167
12:38.920 --> 12:40.180
It gives us a whole number.

168
12:40.180 --> 12:43.060
So this is probably an integer.

169
12:43.060 --> 12:43.480
well,

170
12:43.480 --> 12:46.510
we can verify this by using a type check.

171
12:46.510 --> 12:46.900
Right?

172
12:46.900 --> 12:50.530
We can print the first part of this concatenation,

173
12:50.530 --> 12:58.960
and we can put it inside a type function to check what is the data type of this first part.

174
12:58.960 --> 13:06.930
And then we can also check the data type of the second part, which is now called, length_of_name.

175
13:07.500 --> 13:09.330
So let's comment this out.

176
13:09.330 --> 13:11.190
Let's go ahead and run this code.

177
13:11.190 --> 13:16.650
Enter a name and you can see the first one is a string and the second one is an integer.

178
13:16.650 --> 13:19.950
So this is a type str,

179
13:19.950 --> 13:22.110
and this is a type int.

180
13:22.110 --> 13:29.190
So now it makes sense that when we're printing, we can't simply concatenate a string to a number,

181
13:29.190 --> 13:30.690
it doesn't know how to do that.

182
13:30.720 --> 13:38.880
So how can we convert this length_of_name into the same data type as this first part, which is a string?

183
13:38.880 --> 13:41.040
Well, you learnt how to do that already.

184
13:41.040 --> 13:50.010
All we need to do is to wrap this inside a set of parentheses, which is the string type conversion.

185
13:50.010 --> 13:53.970
And so now we have this part which is now a string,

186
13:53.970 --> 13:56.420
and then we add it to another string.

187
13:56.420 --> 14:01.490
And now if I run this code again then you'll see that it works without any issues.

188
14:01.490 --> 14:04.700
Number of letters in your name: 6, printed out.

189
14:05.390 --> 14:13.370
So hopefully, this makes sense to you and you'll be able to understand in the future how to do type conversions,

190
14:13.370 --> 14:16.880
and how to understand when you have different type errors

191
14:16.880 --> 14:18.620
what has gone wrong.