WEBVTT

0
00:00.000 --> 00:05.000
[inaudible]

1
00:06.020 --> 00:08.210
<v 1>So here's a blueprint for a car.</v>

2
00:08.840 --> 00:13.010
And that blueprint that specifies what the color of the car is,

3
00:13.010 --> 00:17.900
how many wheels it should have, what its mileage is, how much fuel it has,

4
00:17.900 --> 00:22.310
all of those bits of data combined with all of its functionality

5
00:22.340 --> 00:26.840
like the ability to drive, the ability to stop and break.

6
00:27.470 --> 00:32.470
And that blueprint which models a real-life car is known as the class.

7
00:33.380 --> 00:35.060
And it's from this blueprint,

8
00:35.060 --> 00:40.040
this class, that we can generate as many objects as we want. Now,

9
00:40.040 --> 00:43.970
the object is the actual thing that we're going to be using in our code.

10
00:44.720 --> 00:47.090
The code equivalent of what just happened,

11
00:47.150 --> 00:50.300
creating a new object from a blueprint,

12
00:50.480 --> 00:54.770
looks like this in Python. You have the class

13
00:55.010 --> 00:59.930
which is normally written with the first letter of each word capitalized,

14
01:00.350 --> 01:02.510
which is known as Pascal case.

15
01:03.230 --> 01:08.030
This is to differentiate it from all the variable and function names that we

16
01:08.030 --> 01:12.140
give in Python, where each word is separated by underscores.

17
01:12.740 --> 01:13.790
So in this case,

18
01:14.090 --> 01:19.090
the car is the object and it gets created from this car blueprint.

19
01:21.320 --> 01:26.320
All we have to do to create the object from the class is to give the object a

20
01:27.260 --> 01:32.150
name, it can be anything you want, set it equal to the name of the class,

21
01:32.570 --> 01:36.920
and then the parentheses, which in the same way as it activates the function,

22
01:37.220 --> 01:41.090
it activates the construction of this object from the blueprint.

23
01:41.990 --> 01:46.220
Now, in order for us to practice this and to see it in action,

24
01:46.580 --> 01:51.170
we're going to using a library of code that somebody else has created.

25
01:51.830 --> 01:56.830
And what it allows us to do is to finally start putting graphics onto the

26
01:57.290 --> 01:59.990
screen. The premise is really simple.

27
02:00.410 --> 02:05.240
Let's say that somebody somewhere has created a blueprint for a turtle.

28
02:05.870 --> 02:09.050
Now this turtle has a paintbrush on its back,

29
02:09.500 --> 02:13.460
and you can tell the turtle to dip the paintbrush in different colors,

30
02:13.790 --> 02:17.120
pick a different of size paint brushes and most importantly,

31
02:17.210 --> 02:19.430
you can tell it to draw onto the screen.

32
02:20.300 --> 02:25.300
This library is called turtle graphics and it's preloaded with every download of

33
02:25.820 --> 02:29.150
Python so we can get started with it straight away.

34
02:30.200 --> 02:34.100
So go ahead and open up PyCharm and create a new project.

35
02:34.700 --> 02:39.440
Now I'm going to call mine, buy the old convention day 16 start

36
02:41.240 --> 02:45.350
and making sure that I'm on the latest version of Python in my base interpreter,

37
02:45.680 --> 02:50.660
I'm going to click create. Now, once you've created the new project,

38
02:51.080 --> 02:54.800
go ahead and create the main.py as we have always done.

39
02:55.310 --> 02:56.143
In our case,

40
02:56.150 --> 03:00.610
we're going to be creating an object from a blueprint that somebody else has

41
03:00.610 --> 03:01.630
already created,

42
03:02.230 --> 03:05.650
and the blueprint lives in another module called turtle.

43
03:05.890 --> 03:07.990
So we'll have to import that module.

44
03:09.190 --> 03:11.680
And we know that from previously,

45
03:11.710 --> 03:16.710
we can import other modules and use the things that are declared inside the

46
03:17.110 --> 03:21.340
module by importing it, and then tapping into various things in there.

47
03:21.790 --> 03:25.600
So for example, if I was to go and create another new file

48
03:25.810 --> 03:27.400
which I'll call another_module.

49
03:29.470 --> 03:33.250
Now inside another_module, let's say I create another variable.

50
03:34.810 --> 03:38.290
And now I can go back to my main.py,

51
03:38.320 --> 03:42.190
I can import my another_module I just created

52
03:42.580 --> 03:44.920
and I can tap into that variable

53
03:44.950 --> 03:49.950
that's inside another module by saying another module dot another variable.

54
03:51.100 --> 03:54.010
Now let's go ahead and run this code as it is.

55
03:55.570 --> 04:00.400
And you can see the value of that variable from the other module being used

56
04:00.430 --> 04:02.200
inside our main.py.

57
04:03.070 --> 04:07.510
Now I want to do the same thing using turtle, but in this case,

58
04:07.540 --> 04:11.050
I want to tap into the class called turtle

59
04:11.320 --> 04:16.270
that's declared inside this turtle module. The wording's a little bit confusing,

60
04:16.300 --> 04:17.500
but let me show you what I mean.

61
04:18.070 --> 04:23.070
So I'm going to tap into the turtle module and I'm going to get hold of a

62
04:24.640 --> 04:28.390
turtle class. So notice how it's got the C here

63
04:28.690 --> 04:33.070
denoting that this is a blueprint for creating a new turtle

64
04:33.070 --> 04:38.070
object. Now, remember that to actually construct the object

65
04:38.770 --> 04:41.260
we need to add the parentheses at the end,

66
04:41.710 --> 04:46.600
and now we can save all of this into an actual object, which we get to name.

67
04:47.020 --> 04:49.180
So I'm going to call my turtle timmy

68
04:49.870 --> 04:54.790
and now I've got a new turtle object. Essentially,

69
04:54.790 --> 04:57.550
we've done the same thing as we have done up here.

70
04:58.060 --> 05:01.090
We've imported a module called turtle,

71
05:01.630 --> 05:05.140
we've tapped into that turtle module here,

72
05:05.620 --> 05:09.460
and then we've fetched something from that module. In this case,

73
05:09.460 --> 05:10.990
it's a variable. And in this case,

74
05:11.020 --> 05:15.580
it's a class which is denoted by the capital T, the Pascal case here.

75
05:16.300 --> 05:19.300
And then we've used the parentheses to initialize

76
05:19.330 --> 05:22.870
or basically construct an object from that blueprint

77
05:23.230 --> 05:26.530
and we've saved it into an object called Timmy.

78
05:27.550 --> 05:31.990
Now I can simplify this code to make it look more like what we had in the slides

79
05:32.290 --> 05:35.380
by saying, instead of import turtle, I can say from,

80
05:35.380 --> 05:39.850
from the turtle module import the turtle class.

81
05:40.420 --> 05:43.150
So now, instead of writing all of this,

82
05:43.210 --> 05:45.520
I can simply just write Timmy

83
05:45.790 --> 05:50.790
equals a new object created from the turtle class and it's constructed.

84
05:52.000 --> 05:55.540
This is how we would construct our new object.

85
05:56.770 --> 05:59.780
So now I have this brand new object called Timmy

86
06:00.290 --> 06:02.480
and if I go ahead and print timmy

87
06:02.900 --> 06:07.900
you can see that when I run this code what gets printed is a new turtle object

88
06:09.770 --> 06:11.180
from the turtle module

89
06:11.480 --> 06:14.990
and it's saved at this location in the computer's memory.

90
06:15.590 --> 06:17.690
So this is very different from say

91
06:17.690 --> 06:21.050
if we just printed a string or a number.

92
06:21.440 --> 06:24.710
It works completely differently. In this case,

93
06:24.740 --> 06:29.660
it's actually a object that's being printed. Now,

94
06:29.660 --> 06:31.640
what can we do with this object?

95
06:32.380 --> 06:37.380
<v 0>[inaudible]</v>

96
06:37.660 --> 06:40.480
Now, as we saw before with our car

97
06:40.510 --> 06:44.440
object it has certain attributes, right?

98
06:44.680 --> 06:49.680
Like speed and fuel, data that it could keep track of that are really important

99
06:51.190 --> 06:55.720
to the modeling of an actual car object. Now,

100
06:55.720 --> 06:58.720
when it comes to accessing these attributes,

101
06:58.840 --> 07:03.840
the syntax or the code looks like this. Car in this case is the object

102
07:04.480 --> 07:09.480
and then we use a dot or a full stop to separate the object from the actual

103
07:10.960 --> 07:14.530
attribute that we want to get hold of, which in this case is speed.

104
07:15.100 --> 07:18.940
What this code does is it essentially identifies the object

105
07:19.660 --> 07:23.590
and then it says, from this object get the speed attribute.

106
07:24.130 --> 07:28.030
This is actually gonna represent the speed of this particular car

107
07:28.030 --> 07:31.270
object. Coming back to our turtle.

108
07:31.900 --> 07:36.900
One of the other classes that is inside this turtle module is something called

109
07:37.480 --> 07:38.313
screen.

110
07:38.680 --> 07:43.680
And the screen represents the window in which this turtle is going to show up.

111
07:45.400 --> 07:47.320
So let's go ahead and create this screen

112
07:47.320 --> 07:50.260
object. I'm going to call my object my_screen

113
07:50.800 --> 07:54.970
and then I'm going to create it from this blueprint of a screen that we

114
07:54.970 --> 07:59.320
imported. Now we can tap into one of the screens properties

115
07:59.770 --> 08:03.070
which is called the canvas height and canvas width.

116
08:03.370 --> 08:07.690
So let's see what the height is, and notice how we're using that dot notation.

117
08:07.960 --> 08:12.670
So this is the object and then separated by a dot we've got the attributes that

118
08:12.670 --> 08:14.980
we want to access from this object.

119
08:15.400 --> 08:19.390
So I'm going to print that into the console. Now,

120
08:19.390 --> 08:21.220
when I go ahead and run this code,

121
08:21.550 --> 08:25.030
you'll see a window pop up very briefly

122
08:25.480 --> 08:29.530
and that was the screen on which our turtle is going to show up.

123
08:31.060 --> 08:33.850
Now, if you take a look in the console,

124
08:34.150 --> 08:37.510
you can see that not only have we printed the turtle object,

125
08:37.840 --> 08:42.840
but we've also printed the height of the canvas for this particular screen that

126
08:43.600 --> 08:44.500
we've created.

127
08:45.610 --> 08:50.380
So the screen here is the object and that canvas height is an attribute that's

128
08:50.380 --> 08:52.360
associated with that screen.

129
08:58.910 --> 09:02.540
Now, in addition to the things that an object has,

130
09:02.780 --> 09:06.140
so data that it holds like the speed or the fuel

131
09:06.470 --> 09:11.060
which we've already seen, it also has things that it can do, right?

132
09:11.060 --> 09:16.060
So functions that are associated with that particular object. And these functions

133
09:16.730 --> 09:19.880
when it's tied to an object is known as a method.

134
09:20.630 --> 09:24.530
But essentially we would use it the same way as we have done with any other

135
09:24.530 --> 09:26.330
function that we've created so far.

136
09:27.560 --> 09:30.980
The only difference is the syntax. Firstly,

137
09:31.010 --> 09:35.570
we would tap into the car object and then using that same dot notation that

138
09:35.570 --> 09:39.500
we saw with the attributes, we're saying car.stop.

139
09:39.710 --> 09:43.640
So essentially what we're doing is we're getting hold of the object and then

140
09:43.640 --> 09:47.870
we're calling this function that's associated with that object,

141
09:48.170 --> 09:52.790
which is known as a method. So coming back to our code here,

142
09:53.060 --> 09:57.830
one of the things that we can do with this screen object that we've created here

143
09:58.070 --> 10:02.840
called my_screen is we can tap into one of its functions.

144
10:03.320 --> 10:06.110
And because it's a function that's tied to an object,

145
10:06.110 --> 10:07.640
it's actually called a method.

146
10:08.150 --> 10:11.930
So the method name is called exitonclick.

147
10:12.530 --> 10:17.390
And what this will allow us to do is instead of having our screen show up and

148
10:17.390 --> 10:20.510
then quickly disappear when our code ends,

149
10:21.110 --> 10:26.110
this exitonclick will allow our program to continue running until we click on

150
10:27.980 --> 10:30.530
the screen and then it exits our code.

151
10:31.010 --> 10:36.010
So let's try this again and notice how we've now got our screen show up

152
10:36.770 --> 10:41.240
and this canvas has a height of 300 and a width of 300,

153
10:41.930 --> 10:46.730
but now we can see our turtle in the form of the arrow. So this is our

154
10:46.730 --> 10:48.830
a little Timmy shown up in the middle.

155
10:49.280 --> 10:53.600
And our code is only going to exit when the screen detects a click

156
10:53.720 --> 10:58.720
because we've called this function that's associated with the screen object.

157
10:59.000 --> 11:00.530
So now if I click on the screen,

158
11:00.800 --> 11:05.390
you can see process finished with exit code zero, our code is ended and the screen

159
11:05.390 --> 11:08.900
has disappeared. Now there are other functions as well.

160
11:08.900 --> 11:13.310
So if we take our little timmy object and we say, well, timmy,

161
11:13.520 --> 11:17.090
why don't we change the shape of you on the screen

162
11:17.150 --> 11:19.550
and we'll change you into an actual turtle?

163
11:20.750 --> 11:22.310
So now when I run the code,

164
11:23.180 --> 11:27.770
you can see that object in the center is now in the shape of a turtle.

165
11:29.000 --> 11:29.300
Now,

166
11:29.300 --> 11:33.590
if you want to know how I know about all these things I can do with the turtle

167
11:33.590 --> 11:36.440
graphics library, then of course,

168
11:36.470 --> 11:40.160
I'm getting all of this information from the documentation.

169
11:40.610 --> 11:42.200
So in the course resources,

170
11:42.230 --> 11:45.740
you'll see a link to this Turtle graphics documentation,

171
11:46.160 --> 11:49.520
and it tells you all the things that you can do with your turtle.

172
11:49.940 --> 11:50.900
So for example,

173
11:50.900 --> 11:55.510
you can call all of these methods that are associated with your turtle object to

174
11:55.510 --> 11:58.180
get your turtle to move and draw,

175
11:58.540 --> 12:03.400
and you can also use it to change its position or change its coordinates,

176
12:03.580 --> 12:07.720
as well as doing things like changing the color or resetting.

177
12:08.140 --> 12:12.250
And you can browse through this list to see how you would use each of these

178
12:12.250 --> 12:15.040
things. So for example,

179
12:15.370 --> 12:18.550
if I wanted to change the color of my turtle,

180
12:18.880 --> 12:22.600
I can do that by simply just calling this method, color,

181
12:23.020 --> 12:25.420
and then inside the parentheses

182
12:25.450 --> 12:29.650
I pass in the argument of the actual color string.

183
12:30.400 --> 12:35.400
Now you can choose from a whole range of colors just by putting in the name as a

184
12:36.280 --> 12:40.240
string into that method which is called color.

185
12:40.870 --> 12:45.870
So go ahead and see if you can change our turtle timmy's color from the default

186
12:46.960 --> 12:51.960
which is just black to one of the colors that you see in this link here,

187
12:53.290 --> 12:55.840
and the link, of course, is in the course resources.

188
12:59.170 --> 13:03.070
So let's say I want to change it to a coral color like this one.

189
13:03.670 --> 13:07.510
All I have to do is tap into my object which is called timmy,

190
13:08.050 --> 13:11.860
and then call the method which is called color onto it.

191
13:12.220 --> 13:14.260
And you can see all the methods are 

192
13:14.260 --> 13:16.870
denoted by a little M here in the circle.

193
13:17.500 --> 13:21.520
Now this color, of course, expects some sort of input.

194
13:22.180 --> 13:27.180
So the input that it expects is a actual string of a color.

195
13:28.030 --> 13:32.500
And my string is going to be coral, which I picked up from here.

196
13:33.640 --> 13:37.510
So now let's run our code and see what it looks like now.

197
13:37.960 --> 13:39.580
Once the screen refreshes,

198
13:39.610 --> 13:43.090
you can see that our turtle is now a nice coral color,

199
13:43.600 --> 13:46.990
and we've managed to do all of this to change the shape,

200
13:47.020 --> 13:52.020
change the color, all because we were able to call these functions

201
13:52.720 --> 13:53.800
which belong to this

202
13:53.800 --> 13:58.800
object. Now see if you can use the documentation here and figure out how to get the

203
13:59.440 --> 14:02.560
turtle to move forward by a hundred paces.

204
14:03.760 --> 14:06.250
Pause the video and try to complete this challenge.

205
14:08.860 --> 14:09.190
All right.

206
14:09.190 --> 14:13.420
So we saw earlier that it has a whole bunch of move and draw methods.

207
14:13.870 --> 14:18.550
So we can simply use this forward method to get it to move forward.

208
14:19.030 --> 14:21.070
And in terms of the parameters that it takes,

209
14:21.340 --> 14:25.750
it takes a number which is going to determine the distance that it's going to

210
14:25.750 --> 14:26.740
move forward by.

211
14:27.580 --> 14:31.600
So we said that we wanted our turtle to move forward by a hundred paces.

212
14:33.040 --> 14:37.900
All I have to do is specify the object that I want to move which is timmy,

213
14:38.470 --> 14:43.360
and then call that method forward and pass in the distance.

214
14:43.450 --> 14:47.650
So I said a hundred paces. So now let's run our code again,

215
14:48.430 --> 14:53.180
and you can see that little turtle just move forward by a hundred paces.

216
14:53.630 --> 14:58.630
So now we've seen how to create a new Object from a blueprint,

217
15:00.020 --> 15:05.020
we've seen how we can tap into its attributes by using the object name dot and

218
15:08.060 --> 15:09.350
then the name of the attribute.

219
15:09.650 --> 15:14.450
So this is the same as having a variable that's associated with that object.

220
15:14.930 --> 15:15.590
And finally,

221
15:15.590 --> 15:20.300
we've seen how we can call methods that are associated with the object.

222
15:20.540 --> 15:21.410
For example, here

223
15:21.410 --> 15:25.190
when we changed the shape or when we changed the color or when we told it to go

224
15:25.190 --> 15:27.830
forwards. Now in the next lesson,

225
15:27.860 --> 15:32.000
I'm going to show you how you can get hold of more external libraries

226
15:32.300 --> 15:37.160
not just turtle, but a whole world of packages that you can tap into.

227
15:37.460 --> 15:42.230
And you can start using these packages of code that other programmers have

228
15:42.230 --> 15:43.130
already developed,

229
15:43.430 --> 15:48.430
and simply by creating objects from the existing blueprints and reading the

230
15:48.710 --> 15:52.070
documentation to get it to do various things that you want it to.

231
15:52.550 --> 15:55.580
So for all of that and more, I'll see you on the next lesson.