Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,340 --> 00:00:05,080
Previously we saw how the Len function gives us a type error when we give it a
2
00:00:05,080 --> 00:00:06,880
number instead of a string.
3
00:00:07,570 --> 00:00:11,110
So let's talk a little bit more about data types and functions.
4
00:00:11,740 --> 00:00:16,270
And the way I think about functions is kind of like some sort of fancy machine
5
00:00:16,270 --> 00:00:19,690
that you might see in a factory, right? So in this case,
6
00:00:19,960 --> 00:00:24,460
we've got some sort of machine that's going to take potatoes into chips and we
7
00:00:24,460 --> 00:00:25,810
don't really care how it does it,
8
00:00:26,230 --> 00:00:29,020
but it's probably going to have to peel the potatoes, wash the potatoes,
9
00:00:29,020 --> 00:00:30,070
cut it up, fry it,
10
00:00:30,460 --> 00:00:34,780
and then finally return it to us as an output in the form of fries.
11
00:00:35,410 --> 00:00:39,490
And if this is the first time that you're watching one of my tutorials, um,
12
00:00:39,580 --> 00:00:43,660
this is probably a health warning. I talk about food a lot. Um,
13
00:00:45,070 --> 00:00:47,020
so be prepared if you haven't eaten.
14
00:00:47,710 --> 00:00:52,710
Now let's say that we took the same machine that normally processes potatoes and
15
00:00:52,990 --> 00:00:57,700
we decided to get a rock and we just pass this through the function,
16
00:00:58,270 --> 00:01:02,170
then we're getting an error, right? This is basically what happened here.
17
00:01:02,590 --> 00:01:06,610
This length function doesn't like working with integers.
18
00:01:07,090 --> 00:01:08,800
And by forcing this through,
19
00:01:08,800 --> 00:01:13,800
we end up with an error and our code breaks and it gives us this thing, a type
20
00:01:13,840 --> 00:01:14,673
error.
21
00:01:14,920 --> 00:01:19,920
Now do you remember previously we had this challenge where we asked you to get
22
00:01:20,110 --> 00:01:24,400
the um, get an input from the user, what is your name?
23
00:01:24,910 --> 00:01:29,650
And then we use the length function to get the number of characters of their
24
00:01:29,650 --> 00:01:34,510
name. And let's just save that to a variable. Let's call it, um,
25
00:01:34,900 --> 00:01:38,440
num_char equals this.
26
00:01:39,040 --> 00:01:44,040
And then we print your name has plus num_char, number of characters + characters.
27
00:01:49,600 --> 00:01:54,600
So in this case you would expect it to prompt the user to enter their name.
28
00:01:54,910 --> 00:01:56,800
Let's say I entered my name Angela,
29
00:01:57,550 --> 00:02:00,910
and the length of that string would be 6
30
00:02:00,940 --> 00:02:05,770
so this would be equal to six. And so this would get substituted in here.
31
00:02:05,860 --> 00:02:10,150
And then with string concatenation, we end up printing
32
00:02:10,150 --> 00:02:14,380
"Your name has six characters." But this is not what happens.
33
00:02:14,410 --> 00:02:19,090
And I know that some of you might have tried this for the previous challenge and
34
00:02:19,090 --> 00:02:21,670
you would have gotten a type error.
35
00:02:22,180 --> 00:02:26,560
So it tells you that you can only concatenate strings,
36
00:02:26,950 --> 00:02:29,680
not integers. So in this case,
37
00:02:29,680 --> 00:02:33,400
we were concatenating a string to an integer.
38
00:02:33,880 --> 00:02:37,060
This is what we get. We end up with an error.
39
00:02:37,930 --> 00:02:42,930
So how can we prevent these type errors and how can we see the data type that
40
00:02:43,060 --> 00:02:45,010
we're working with? Well,
41
00:02:45,010 --> 00:02:48,640
we could use a function called type,
42
00:02:49,180 --> 00:02:53,710
which basically will check whatever you put between the parentheses and give you
43
00:02:53,740 --> 00:02:56,620
the type of data that it is. For example,
44
00:02:56,620 --> 00:02:59,830
if I go ahead and just comment out this line of code
45
00:02:59,830 --> 00:03:04,660
which breaks and go ahead and put num_char,
46
00:03:04,750 --> 00:03:09,670
so this particular piece of data inside this type function,
47
00:03:10,090 --> 00:03:15,090
and then I'm going to print the type of num_char.
48
00:03:16,210 --> 00:03:19,900
And now if I hit run, let's enter my name.
49
00:03:20,530 --> 00:03:24,100
And you can see it's telling you that this is an integer.
50
00:03:24,670 --> 00:03:29,440
So adding a string to an integer doesn't make any sense,
51
00:03:29,620 --> 00:03:31,180
which is why you got the type error.
52
00:03:32,110 --> 00:03:37,110
Now every so often when you're writing code and you're not quite sure what the
53
00:03:37,720 --> 00:03:40,000
type of data of something might be,
54
00:03:40,240 --> 00:03:45,240
then you can simply just put it inside a type check function,
55
00:03:45,610 --> 00:03:48,940
right? Like so. And then you'll get an answer.
56
00:03:49,360 --> 00:03:51,760
Now in addition to type checking,
57
00:03:52,120 --> 00:03:57,120
we can also do type conversion or you might hear it called type casting where we
58
00:03:58,600 --> 00:04:02,890
change a piece of data from one particular data type to another.
59
00:04:03,430 --> 00:04:04,300
So it, for example,
60
00:04:04,300 --> 00:04:09,300
we know that this variable num_char has the data type of integer.
61
00:04:10,750 --> 00:04:15,520
If we wanted to turn it into a string so that this line of code wouldn't break
62
00:04:15,520 --> 00:04:16,353
anymore,
63
00:04:16,480 --> 00:04:20,680
then we could convert it into a string by writing str
64
00:04:21,280 --> 00:04:25,960
and then inside the parentheses, we put in the variable that we want to convert,
65
00:04:26,020 --> 00:04:30,310
which is called num_char. And now we save this into a new variable,
66
00:04:30,340 --> 00:04:33,160
we'll call it new_num_char.
67
00:04:33,910 --> 00:04:38,910
And once we've converted this into a string and stored it under this name,
68
00:04:39,250 --> 00:04:43,840
then we can use it inside our print statement, new_num_char,
69
00:04:44,320 --> 00:04:49,320
and now it won't break anymore. And it tells us your name has six characters.
70
00:04:50,890 --> 00:04:55,210
So in this case we've taken a integer data type,
71
00:04:55,690 --> 00:05:00,690
put it inside the parentheses of a str function,
72
00:05:01,120 --> 00:05:05,830
which takes a object in between the parentheses and converts it into a string,
73
00:05:06,490 --> 00:05:09,010
which we then store inside this new_num_char
74
00:05:09,100 --> 00:05:14,100
and we can now use it in our print statement because all the data types of all
75
00:05:14,290 --> 00:05:19,290
the pieces of data that we're adding together have the same data type, string.
76
00:05:20,740 --> 00:05:24,610
Now you're not limited to only converting numbers into strings.
77
00:05:24,940 --> 00:05:27,400
You can convert a whole bunch of different data types.
78
00:05:27,760 --> 00:05:32,760
So let me go ahead and comment out this code and I can show you that
79
00:05:33,040 --> 00:05:37,600
let's say that we had a variable a, which we set two a number,
80
00:05:37,870 --> 00:05:39,040
right? 123.
81
00:05:39,970 --> 00:05:44,970
And if I do a type check on a and print this,
82
00:05:45,730 --> 00:05:50,020
you'll see that as you would expect, it should give you integer, right?
83
00:05:50,230 --> 00:05:51,670
A is an integer.
84
00:05:52,360 --> 00:05:57,360
But now let's say I converted a into a string by wrapping it inside the string
85
00:05:59,300 --> 00:06:03,710
function, then hit enter. You can see it's now of type string.
86
00:06:05,210 --> 00:06:09,410
Now I can do this with other things as well. So for example,
87
00:06:09,710 --> 00:06:14,710
I could turn my whole number here into a float and when I run my code,
88
00:06:16,940 --> 00:06:20,630
now we see that the type of my variable is a float
89
00:06:21,050 --> 00:06:23,090
and that's pretty straightforward, right?
90
00:06:23,420 --> 00:06:26,600
But let me ask you two quick questions before we wrap up.
91
00:06:26,960 --> 00:06:31,790
What will this line of code print? Alright, you guessed it,
92
00:06:31,850 --> 00:06:36,850
this will print 170.5 but what's actually happening behind the scenes is we're
93
00:06:37,730 --> 00:06:42,730
converting this string 100.5 into a floating-point number and then we're adding
94
00:06:45,980 --> 00:06:50,510
70 to 100.5 and finally we're printing the results.
95
00:06:50,900 --> 00:06:54,950
But what about this line? What will print here? Have a guess.
96
00:06:56,840 --> 00:07:01,840
Well, this time we get 70100. So in summary,
97
00:07:02,120 --> 00:07:06,830
you can use the type function to investigate the data type you're working with
98
00:07:07,220 --> 00:07:12,220
and you can use functions like string, int, or float to convert to that data
99
00:07:12,380 --> 00:07:16,190
type. In the next lesson, I've got a coding challenge for you.
100
00:07:16,370 --> 00:07:18,500
So head over there and we'll get started.
9095
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.