WEBVTT

0
00:00.090 --> 00:03.420
A long time ago, we've talked about Python data types.

1
00:03.540 --> 00:08.370
So the data types that we've been working with include things like integers,

2
00:08.610 --> 00:12.180
strings, floating point numbers and booleans.

3
00:12.720 --> 00:16.980
And we've seen how you can interchange between different data types by casting.

4
00:17.460 --> 00:21.780
And we've also seen that in Python, the data types are flexible.

5
00:21.780 --> 00:25.800
So you could create a variable and then change its data type later on.

6
00:26.220 --> 00:27.990
This is known as dynamic typing.

7
00:28.890 --> 00:32.790
Now I want to show you another thing that you can do with data types to make

8
00:32.790 --> 00:34.530
your code less error prone.

9
00:35.160 --> 00:39.900
Let's say that we were to create a variable, for example, a variable called age.

10
00:40.020 --> 00:42.720
Now, normally we would set it with an equal sign.

11
00:43.140 --> 00:48.140
But you can also simply declare its data type and then leave it as is.

12
00:48.750 --> 00:51.210
So this means that later on, at some point,

13
00:51.240 --> 00:55.140
once you've actually gotten the age of from the user or you've worked it out,

14
00:55.410 --> 01:00.390
then at that point, you can set it. And this age now has to match

15
01:00.390 --> 01:02.850
the data type over here. So for example,

16
01:02.850 --> 01:07.850
if I was to change this to 12 using a string, then you can see that my 

17
01:08.430 --> 01:08.910
PyCharm,

18
01:08.910 --> 01:13.910
my IDE, is now being very helpful in telling me that this age thing ages ago

19
01:13.950 --> 01:18.060
when you created it, you said it should be an integer. Instead, I got a string.

20
01:18.600 --> 01:23.010
So this is quite helpful. You can do this with all of the basic data types.

21
01:23.010 --> 01:27.870
So for example, age is an int, name is a string,

22
01:28.710 --> 01:33.710
height is a float and is_human is a boolean.

23
01:34.800 --> 01:39.800
Now we can also specify the data type inside a function.

24
01:40.290 --> 01:41.490
So for example,

25
01:41.490 --> 01:46.490
if I was to create a function here called a police_check and it took the age as

26
01:47.850 --> 01:52.800
an input, and then inside the police_check function, I would check well,

27
01:52.800 --> 01:55.170
if the age is over 18,

28
01:55.470 --> 02:00.060
then maybe we would have some sort of variable called can_drive and we could set

29
02:00.060 --> 02:01.050
that to true.

30
02:01.650 --> 02:05.070
But otherwise, we can set that to false

31
02:05.630 --> 02:06.463
<v 1>right?</v>

32
02:07.880 --> 02:10.670
<v 0>Now, we're going to return this as the output.</v>

33
02:11.510 --> 02:15.860
So now at some point we can call our police_check function,

34
02:15.920 --> 02:19.550
pass in the age and if we print this out,

35
02:19.730 --> 02:24.260
you can see that it's going to give me false for age 12.

36
02:24.710 --> 02:29.120
And if I decide to say I'm 19, well, then we get true.

37
02:29.810 --> 02:34.810
We can actually use the output from this function to create a print statement,

38
02:35.390 --> 02:36.223
for example.

39
02:38.420 --> 02:42.980
So if the police check passes, then the policeman tells you, you may pass.

40
02:43.370 --> 02:46.700
Otherwise you might have to pay a fine or spend a night in jail.

41
02:47.150 --> 02:48.500
So this is our function

42
02:49.100 --> 02:52.220
and if we were to use this function

43
02:52.220 --> 02:54.140
police_check at some point

44
02:54.380 --> 02:59.240
way down the line where we've forgotten what we created for this function

45
02:59.530 --> 03:03.250
and we can't look it up very easily without scrolling up hundreds of lines of

46
03:03.250 --> 03:08.020
code, now, if at this point we've mistaken the input type

47
03:08.080 --> 03:11.920
and we put in, for example, 12 as a string,

48
03:12.250 --> 03:16.810
then this is actually going to give us a type error and it throws an exception

49
03:16.840 --> 03:17.470
crashing our

50
03:17.470 --> 03:22.470
app. One of the ways that we can make our lives a little bit easier is by

51
03:22.480 --> 03:27.310
declaring a type for this input. So we do it in the same way as you see above.

52
03:27.370 --> 03:32.370
We can add a colon and then we can specify the data type of this particular

53
03:32.440 --> 03:36.190
input. So now when we actually write this line of code,

54
03:36.250 --> 03:39.610
you can see immediately we get this part highlighted.

55
03:40.000 --> 03:44.680
And if I hover over it, it tells me that this input expected a datatype

56
03:44.680 --> 03:48.130
that's an integer and instead I gave it a string.

57
03:48.400 --> 03:53.290
So this is a quick hint for us to fix our code before we even run it and get

58
03:53.290 --> 03:54.580
into trouble and create

59
03:54.630 --> 03:56.850
<v 2>bugs. In addition,</v>

60
03:56.880 --> 04:01.880
<v 0>you can also specify the data type of the output of a function,</v>

61
04:02.850 --> 04:07.740
and you do that by creating a little arrow with a hyphen and an angle bracket.

62
04:08.310 --> 04:13.310
So now we can say that this particular function is expected to return a boolean

63
04:14.640 --> 04:15.473
data type.

64
04:15.840 --> 04:20.840
And my return statement does in fact comply with this because can_drive can only

65
04:21.240 --> 04:25.560
be true or false. Now, if I forget that this is what I need

66
04:25.650 --> 04:29.460
and at some point I return let's say a string,

67
04:30.570 --> 04:33.660
then also I'm getting this warning highlight here.

68
04:33.720 --> 04:37.230
And when I hover over it again, it's expecting a boolean

69
04:37.290 --> 04:39.150
but instead I'm giving it a string.

70
04:39.840 --> 04:42.870
This is known as a type hint in Python.

71
04:43.320 --> 04:46.680
And it is a feature that we got relatively recently from Python

72
04:47.100 --> 04:52.080
and it has a lot of benefits especially when you want your IDE, for example

73
04:52.080 --> 04:52.913
PyCharm,

74
04:52.980 --> 04:57.980
to help you spot potential bugs and keep your code safer and spend less time

75
04:58.500 --> 05:00.480
debugging and more time writing code.

76
05:01.170 --> 05:05.580
So we've seen how we can declare a data type for a variable like this

77
05:05.910 --> 05:10.830
and we've also seen how we can declare data type for the return type like this.

78
05:11.430 --> 05:12.600
<v 2>That's all there is to it.</v>