WEBVTT

0
00:00.780 --> 00:04.830
In the last lesson, we managed to get our countdown mechanism to work,

1
00:05.160 --> 00:10.100
and we got it to count down from five minutes to zero. Now, everything works,

2
00:10.580 --> 00:14.510
but there's just something a little bit niggling and you'll see it when I hit

3
00:14.510 --> 00:18.200
start. Notice how it says 5:0.

4
00:18.740 --> 00:21.880
Wouldn't it be so much better if it said 5:00?

5
00:23.450 --> 00:25.280
So how would we do that? Well,

6
00:25.550 --> 00:30.320
that is the zero comes from this count_sec because it's taking the count in

7
00:30.320 --> 00:35.210
seconds, dividing it by 60 and finding the remainder using the modulo.

8
00:35.720 --> 00:39.140
Now, when there is no remainder, it's just going to be equal to zero.

9
00:39.620 --> 00:42.500
So we could use an if statement and check well

10
00:42.740 --> 00:45.800
if the count_sec is equal to zero,

11
00:46.190 --> 00:50.990
then instead of making it zero, let's set it to equal a string

12
00:51.020 --> 00:52.160
which is "00".

13
00:52.790 --> 00:56.150
Now this actually, believe it or not, works.

14
00:56.900 --> 00:58.310
And you'll see it when I run it.

15
00:58.340 --> 01:01.250
When I hit start, doesn't that look so much better?

16
01:02.330 --> 01:07.330
But some of you might've realized that there's something a little bit crazy going on

17
01:07.640 --> 01:09.380
here in these two lines of code.

18
01:09.830 --> 01:14.830
We're checking to see if this variable holds data that is equal to the integer,

19
01:16.610 --> 01:18.650
the whole number zero,

20
01:19.250 --> 01:21.890
and then we're setting it to a string.

21
01:22.310 --> 01:26.000
So these are completely two different data types.

22
01:26.030 --> 01:29.120
That's an int and that is a string.

23
01:29.540 --> 01:32.450
So how is it that we're able to do this?

24
01:33.350 --> 01:36.350
So let me pull up the Python console.

25
01:36.560 --> 01:39.260
We know that if we took a integer,

26
01:39.290 --> 01:44.180
say 3, and we try to add it to a string, say a 4,

27
01:44.480 --> 01:45.800
this is not going to work

28
01:45.830 --> 01:50.330
and we get a type error because this is type int

29
01:50.420 --> 01:52.250
and this is type string.

30
01:52.520 --> 01:57.520
So you can't actually add these two together because there's a mismatch.

31
01:58.250 --> 01:59.720
Now here's the crazy thing.

32
01:59.840 --> 02:04.550
Let's create a variable a and let's make it hold the integer with 3.

33
02:05.150 --> 02:10.150
And at this point, you can see that this a variable has a data type of int.

34
02:11.390 --> 02:16.130
If I decide to change that variable and make it hold a different type of data,

35
02:16.430 --> 02:19.400
let's say the number 4, or let's make it simpler,

36
02:19.400 --> 02:23.870
let's make it hold the piece of text, Hello. As soon as that goes through,

37
02:23.870 --> 02:27.410
you can see that a is now of data type string.

38
02:28.010 --> 02:30.950
And the very fact that I'm able to do that,

39
02:31.100 --> 02:36.100
change a variable's data type by changing the content in that variable,

40
02:37.310 --> 02:42.310
this is what's known as dynamic typing. In this sense,

41
02:42.770 --> 02:47.770
Python is a language that's quite unique because on one hand it is strongly

42
02:48.380 --> 02:53.380
typed, in the sense that it holds on to the data type of the variable,

43
02:54.230 --> 02:58.670
so this a is a string and it knows that it's a string

44
02:59.110 --> 03:02.650
and if you do something that is not meant for a string,

45
03:03.010 --> 03:06.640
say if you try to raise a to the power of 2,

46
03:07.090 --> 03:11.440
then that is going to be a type error. Types matter

47
03:11.800 --> 03:15.280
and when you call a function that's expecting a particular type,

48
03:15.610 --> 03:18.070
say if you use the power function

49
03:18.100 --> 03:22.600
which is expecting a integer and you pass in something

50
03:22.630 --> 03:26.980
that's obviously not an integer say, our string here

51
03:27.700 --> 03:32.020
and you try to get it to do some things, say raise it to the power of 3,

52
03:32.380 --> 03:34.300
then again, we get type error.

53
03:34.330 --> 03:37.510
It knows what type is supposed to go into that function

54
03:37.840 --> 03:40.570
and it makes sure that you comply with that.

55
03:40.600 --> 03:43.150
This is where the language is strongly typed.

56
03:43.660 --> 03:48.660
But the part where the language is dynamically typed is that even though it

57
03:48.700 --> 03:51.160
knows what type a variable is,

58
03:51.220 --> 03:54.100
it knows what type a function expects,

59
03:54.580 --> 03:59.580
you can also dynamically change the data type of any variable like this.

60
04:02.500 --> 04:06.130
That is basically what we're using here. We're using dynamic

61
04:06.130 --> 04:07.510
typing to change

62
04:07.510 --> 04:12.510
the data type of count_sec from a integer to a string.

63
04:13.210 --> 04:18.210
And then we use that string to display inside this canvas text.

64
04:19.390 --> 04:20.530
Now there is however,

65
04:20.530 --> 04:25.530
still a bit of a bug with this and you'll see it once it dips below 10 seconds.

66
04:27.430 --> 04:30.430
Notice that as soon as it goes to 9 seconds,

67
04:30.820 --> 04:34.660
instead of saying 09, which is what you would want or 08,

68
04:34.690 --> 04:35.523
06,

69
04:35.740 --> 04:40.740
it just lists the number of seconds until it reaches 00.

70
04:41.380 --> 04:43.690
So how can we fix this?

71
04:44.770 --> 04:47.050
The key lies in these two lines of code.

72
04:47.740 --> 04:49.810
See if you can do this as a challenge,

73
04:50.290 --> 04:55.290
pause the video and see if you can make it so that when it's nine seconds

74
04:55.390 --> 04:56.200
remaining,

75
04:56.200 --> 05:01.200
it says 09 or 08 or 07 and also 00 of course.

76
05:03.370 --> 05:07.840
Pause the video and see if you can complete this challenge. All right.

77
05:07.840 --> 05:12.700
So basically what we want to check for is when the count in seconds is less

78
05:12.700 --> 05:17.050
than 10, because now it's going to be in the single digits

79
05:17.230 --> 05:20.590
and instead of having just one digit,

80
05:20.650 --> 05:22.870
we wanna add a zero in front of it.

81
05:23.470 --> 05:26.890
So the way we can do this is through just an f-string.

82
05:27.340 --> 05:32.340
We can use an f-string to add the zero in front and then add the count in

83
05:32.620 --> 05:34.390
seconds afterwards,

84
05:34.600 --> 05:39.220
formatting that into the standard kind of time format.

85
05:40.270 --> 05:44.290
Now that we fixed that if we run our app again

86
05:44.590 --> 05:46.450
it goes down to 10 seconds

87
05:47.140 --> 05:51.190
you can see it now says 09, 08, 07

88
05:51.460 --> 05:54.430
and it's nicely formatted using our f-string.

89
05:55.660 --> 06:00.660
And we're able to do because of the flexibility of the data types in Python,

90
06:01.130 --> 06:02.480
checking it as a number,

91
06:02.510 --> 06:07.430
making sure it's less than 10 and then formatting it as a string and using the

92
06:07.430 --> 06:08.263
f-string.

93
06:08.900 --> 06:13.070
Now, this is not a feature that's available in all programming languages.

94
06:13.370 --> 06:17.090
For example, languages like C or Java or Swift,

95
06:17.420 --> 06:19.280
you won't be able to do this.

96
06:19.880 --> 06:24.470
Once you create a variable and you give it a certain type of data,

97
06:24.680 --> 06:27.830
then it must forever hold on to that type of data.

98
06:28.520 --> 06:33.520
But Python allows you to change the data type of a variable just by assigning it

99
06:34.820 --> 06:36.980
to a different type of value.

100
06:37.970 --> 06:42.800
There's a really well-worded Stack Overflow answer that addresses this exact

101
06:42.800 --> 06:43.040
thing

102
06:43.040 --> 06:47.870
I'm talking about, talking about the fact that Python is a strongly dynamically

103
06:47.870 --> 06:52.220
typed language. And I recommend that if you want to dig more into this,

104
06:52.370 --> 06:56.870
to have a read of this answer and I'll link to this in the course resources.