1
00:00:00,090 --> 00:00:03,420
A long time ago, we've talked about Python data types.

2
00:00:03,540 --> 00:00:08,370
So the data types that we've been working with include things like integers,

3
00:00:08,610 --> 00:00:12,180
strings, floating point numbers and booleans.

4
00:00:12,720 --> 00:00:16,980
And we've seen how you can interchange between different data types by casting.

5
00:00:17,460 --> 00:00:21,780
And we've also seen that in Python, the data types are flexible.

6
00:00:21,780 --> 00:00:25,800
So you could create a variable and then change its data type later on.

7
00:00:26,220 --> 00:00:27,990
This is known as dynamic typing.

8
00:00:28,890 --> 00:00:32,790
Now I want to show you another thing that you can do with data types to make

9
00:00:32,790 --> 00:00:34,530
your code less error prone.

10
00:00:35,160 --> 00:00:39,900
Let's say that we were to create a variable, for example, a variable called age.

11
00:00:40,020 --> 00:00:42,720
Now, normally we would set it with an equal sign.

12
00:00:43,140 --> 00:00:48,140
But you can also simply declare its data type and then leave it as is.

13
00:00:48,750 --> 00:00:51,210
So this means that later on, at some point,

14
00:00:51,240 --> 00:00:55,140
once you've actually gotten the age of from the user or you've worked it out,

15
00:00:55,410 --> 00:01:00,390
then at that point, you can set it. And this age now has to match

16
00:01:00,390 --> 00:01:02,850
the data type over here. So for example,

17
00:01:02,850 --> 00:01:07,850
if I was to change this to 12 using a string, then you can see that my 

18
00:01:08,430 --> 00:01:08,910
PyCharm,

19
00:01:08,910 --> 00:01:13,910
my IDE, is now being very helpful in telling me that this age thing ages ago

20
00:01:13,950 --> 00:01:18,060
when you created it, you said it should be an integer. Instead, I got a string.

21
00:01:18,600 --> 00:01:23,010
So this is quite helpful. You can do this with all of the basic data types.

22
00:01:23,010 --> 00:01:27,870
So for example, age is an int, name is a string,

23
00:01:28,710 --> 00:01:33,710
height is a float and is_human is a boolean.

24
00:01:34,800 --> 00:01:39,800
Now we can also specify the data type inside a function.

25
00:01:40,290 --> 00:01:41,490
So for example,

26
00:01:41,490 --> 00:01:46,490
if I was to create a function here called a police_check and it took the age as

27
00:01:47,850 --> 00:01:52,800
an input, and then inside the police_check function, I would check well,

28
00:01:52,800 --> 00:01:55,170
if the age is over 18,

29
00:01:55,470 --> 00:02:00,060
then maybe we would have some sort of variable called can_drive and we could set

30
00:02:00,060 --> 00:02:01,050
that to true.

31
00:02:01,650 --> 00:02:05,070
But otherwise, we can set that to false

32
00:02:05,630 --> 00:02:06,463
right?

33
00:02:07,880 --> 00:02:10,669
Now, we're going to return this as the output.

34
00:02:11,510 --> 00:02:15,860
So now at some point we can call our police_check function,

35
00:02:15,920 --> 00:02:19,550
pass in the age and if we print this out,

36
00:02:19,730 --> 00:02:24,260
you can see that it's going to give me false for age 12.

37
00:02:24,710 --> 00:02:29,120
And if I decide to say I'm 19, well, then we get true.

38
00:02:29,810 --> 00:02:34,810
We can actually use the output from this function to create a print statement,

39
00:02:35,390 --> 00:02:36,223
for example.

40
00:02:38,420 --> 00:02:42,980
So if the police check passes, then the policeman tells you, you may pass.

41
00:02:43,370 --> 00:02:46,700
Otherwise you might have to pay a fine or spend a night in jail.

42
00:02:47,150 --> 00:02:48,500
So this is our function

43
00:02:49,100 --> 00:02:52,220
and if we were to use this function

44
00:02:52,220 --> 00:02:54,140
police_check at some point

45
00:02:54,380 --> 00:02:59,240
way down the line where we've forgotten what we created for this function

46
00:02:59,530 --> 00:03:03,250
and we can't look it up very easily without scrolling up hundreds of lines of

47
00:03:03,250 --> 00:03:08,020
code, now, if at this point we've mistaken the input type

48
00:03:08,080 --> 00:03:11,920
and we put in, for example, 12 as a string,

49
00:03:12,250 --> 00:03:16,810
then this is actually going to give us a type error and it throws an exception

50
00:03:16,840 --> 00:03:17,470
crashing our

51
00:03:17,470 --> 00:03:22,470
app. One of the ways that we can make our lives a little bit easier is by

52
00:03:22,480 --> 00:03:27,310
declaring a type for this input. So we do it in the same way as you see above.

53
00:03:27,370 --> 00:03:32,370
We can add a colon and then we can specify the data type of this particular

54
00:03:32,440 --> 00:03:36,190
input. So now when we actually write this line of code,

55
00:03:36,250 --> 00:03:39,610
you can see immediately we get this part highlighted.

56
00:03:40,000 --> 00:03:44,680
And if I hover over it, it tells me that this input expected a datatype

57
00:03:44,680 --> 00:03:48,130
that's an integer and instead I gave it a string.

58
00:03:48,400 --> 00:03:53,290
So this is a quick hint for us to fix our code before we even run it and get

59
00:03:53,290 --> 00:03:54,580
into trouble and create

60
00:03:54,630 --> 00:03:56,850
bugs. In addition,

61
00:03:56,880 --> 00:04:01,880
you can also specify the data type of the output of a function,

62
00:04:02,850 --> 00:04:07,740
and you do that by creating a little arrow with a hyphen and an angle bracket.

63
00:04:08,310 --> 00:04:13,310
So now we can say that this particular function is expected to return a boolean

64
00:04:14,640 --> 00:04:15,473
data type.

65
00:04:15,840 --> 00:04:20,839
And my return statement does in fact comply with this because can_drive can only

66
00:04:21,240 --> 00:04:25,560
be true or false. Now, if I forget that this is what I need

67
00:04:25,650 --> 00:04:29,460
and at some point I return let's say a string,

68
00:04:30,570 --> 00:04:33,660
then also I'm getting this warning highlight here.

69
00:04:33,720 --> 00:04:37,230
And when I hover over it again, it's expecting a boolean

70
00:04:37,290 --> 00:04:39,150
but instead I'm giving it a string.

71
00:04:39,840 --> 00:04:42,870
This is known as a type hint in Python.

72
00:04:43,320 --> 00:04:46,680
And it is a feature that we got relatively recently from Python

73
00:04:47,100 --> 00:04:52,080
and it has a lot of benefits especially when you want your IDE, for example

74
00:04:52,080 --> 00:04:52,913
PyCharm,

75
00:04:52,980 --> 00:04:57,980
to help you spot potential bugs and keep your code safer and spend less time

76
00:04:58,500 --> 00:05:00,480
debugging and more time writing code.

77
00:05:01,170 --> 00:05:05,580
So we've seen how we can declare a data type for a variable like this

78
00:05:05,910 --> 00:05:10,830
and we've also seen how we can declare data type for the return type like this.

79
00:05:11,430 --> 00:05:12,600
That's all there is to it.

