1
00:00:00,000 --> 00:00:05,000
[inaudible]

2
00:00:06,020 --> 00:00:08,210
So here's a blueprint for a car.

3
00:00:08,840 --> 00:00:13,010
And that blueprint that specifies what the color of the car is,

4
00:00:13,010 --> 00:00:17,900
how many wheels it should have, what its mileage is, how much fuel it has,

5
00:00:17,900 --> 00:00:22,310
all of those bits of data combined with all of its functionality

6
00:00:22,340 --> 00:00:26,840
like the ability to drive, the ability to stop and break.

7
00:00:27,470 --> 00:00:32,470
And that blueprint which models a real-life car is known as the class.

8
00:00:33,380 --> 00:00:35,060
And it's from this blueprint,

9
00:00:35,060 --> 00:00:40,040
this class, that we can generate as many objects as we want. Now,

10
00:00:40,040 --> 00:00:43,970
the object is the actual thing that we're going to be using in our code.

11
00:00:44,720 --> 00:00:47,090
The code equivalent of what just happened,

12
00:00:47,150 --> 00:00:50,300
creating a new object from a blueprint,

13
00:00:50,480 --> 00:00:54,770
looks like this in Python. You have the class

14
00:00:55,010 --> 00:00:59,930
which is normally written with the first letter of each word capitalized,

15
00:01:00,350 --> 00:01:02,510
which is known as Pascal case.

16
00:01:03,230 --> 00:01:08,030
This is to differentiate it from all the variable and function names that we

17
00:01:08,030 --> 00:01:12,140
give in Python, where each word is separated by underscores.

18
00:01:12,740 --> 00:01:13,790
So in this case,

19
00:01:14,090 --> 00:01:19,090
the car is the object and it gets created from this car blueprint.

20
00:01:21,320 --> 00:01:26,320
All we have to do to create the object from the class is to give the object a

21
00:01:27,260 --> 00:01:32,150
name, it can be anything you want, set it equal to the name of the class,

22
00:01:32,570 --> 00:01:36,920
and then the parentheses, which in the same way as it activates the function,

23
00:01:37,220 --> 00:01:41,090
it activates the construction of this object from the blueprint.

24
00:01:41,990 --> 00:01:46,220
Now, in order for us to practice this and to see it in action,

25
00:01:46,580 --> 00:01:51,170
we're going to using a library of code that somebody else has created.

26
00:01:51,830 --> 00:01:56,830
And what it allows us to do is to finally start putting graphics onto the

27
00:01:57,290 --> 00:01:59,990
screen. The premise is really simple.

28
00:02:00,410 --> 00:02:05,240
Let's say that somebody somewhere has created a blueprint for a turtle.

29
00:02:05,870 --> 00:02:09,050
Now this turtle has a paintbrush on its back,

30
00:02:09,500 --> 00:02:13,460
and you can tell the turtle to dip the paintbrush in different colors,

31
00:02:13,790 --> 00:02:17,120
pick a different of size paint brushes and most importantly,

32
00:02:17,210 --> 00:02:19,430
you can tell it to draw onto the screen.

33
00:02:20,300 --> 00:02:25,300
This library is called turtle graphics and it's preloaded with every download of

34
00:02:25,820 --> 00:02:29,150
Python so we can get started with it straight away.

35
00:02:30,200 --> 00:02:34,100
So go ahead and open up PyCharm and create a new project.

36
00:02:34,700 --> 00:02:39,440
Now I'm going to call mine, buy the old convention day 16 start

37
00:02:41,240 --> 00:02:45,350
and making sure that I'm on the latest version of Python in my base interpreter,

38
00:02:45,680 --> 00:02:50,660
I'm going to click create. Now, once you've created the new project,

39
00:02:51,080 --> 00:02:54,800
go ahead and create the main.py as we have always done.

40
00:02:55,310 --> 00:02:56,143
In our case,

41
00:02:56,150 --> 00:03:00,610
we're going to be creating an object from a blueprint that somebody else has

42
00:03:00,610 --> 00:03:01,630
already created,

43
00:03:02,230 --> 00:03:05,650
and the blueprint lives in another module called turtle.

44
00:03:05,890 --> 00:03:07,990
So we'll have to import that module.

45
00:03:09,190 --> 00:03:11,680
And we know that from previously,

46
00:03:11,710 --> 00:03:16,710
we can import other modules and use the things that are declared inside the

47
00:03:17,110 --> 00:03:21,340
module by importing it, and then tapping into various things in there.

48
00:03:21,790 --> 00:03:25,600
So for example, if I was to go and create another new file

49
00:03:25,810 --> 00:03:27,400
which I'll call another_module.

50
00:03:29,470 --> 00:03:33,250
Now inside another_module, let's say I create another variable.

51
00:03:34,810 --> 00:03:38,290
And now I can go back to my main.py,

52
00:03:38,320 --> 00:03:42,190
I can import my another_module I just created

53
00:03:42,580 --> 00:03:44,920
and I can tap into that variable

54
00:03:44,950 --> 00:03:49,950
that's inside another module by saying another module dot another variable.

55
00:03:51,100 --> 00:03:54,010
Now let's go ahead and run this code as it is.

56
00:03:55,570 --> 00:04:00,400
And you can see the value of that variable from the other module being used

57
00:04:00,430 --> 00:04:02,200
inside our main.py.

58
00:04:03,070 --> 00:04:07,510
Now I want to do the same thing using turtle, but in this case,

59
00:04:07,540 --> 00:04:11,050
I want to tap into the class called turtle

60
00:04:11,320 --> 00:04:16,269
that's declared inside this turtle module. The wording's a little bit confusing,

61
00:04:16,300 --> 00:04:17,500
but let me show you what I mean.

62
00:04:18,070 --> 00:04:23,070
So I'm going to tap into the turtle module and I'm going to get hold of a

63
00:04:24,640 --> 00:04:28,390
turtle class. So notice how it's got the C here

64
00:04:28,690 --> 00:04:33,070
denoting that this is a blueprint for creating a new turtle

65
00:04:33,070 --> 00:04:38,070
object. Now, remember that to actually construct the object

66
00:04:38,770 --> 00:04:41,260
we need to add the parentheses at the end,

67
00:04:41,710 --> 00:04:46,600
and now we can save all of this into an actual object, which we get to name.

68
00:04:47,020 --> 00:04:49,180
So I'm going to call my turtle timmy

69
00:04:49,870 --> 00:04:54,790
and now I've got a new turtle object. Essentially,

70
00:04:54,790 --> 00:04:57,550
we've done the same thing as we have done up here.

71
00:04:58,060 --> 00:05:01,090
We've imported a module called turtle,

72
00:05:01,630 --> 00:05:05,140
we've tapped into that turtle module here,

73
00:05:05,620 --> 00:05:09,460
and then we've fetched something from that module. In this case,

74
00:05:09,460 --> 00:05:10,990
it's a variable. And in this case,

75
00:05:11,020 --> 00:05:15,580
it's a class which is denoted by the capital T, the Pascal case here.

76
00:05:16,300 --> 00:05:19,300
And then we've used the parentheses to initialize

77
00:05:19,330 --> 00:05:22,870
or basically construct an object from that blueprint

78
00:05:23,230 --> 00:05:26,530
and we've saved it into an object called Timmy.

79
00:05:27,550 --> 00:05:31,990
Now I can simplify this code to make it look more like what we had in the slides

80
00:05:32,290 --> 00:05:35,380
by saying, instead of import turtle, I can say from,

81
00:05:35,380 --> 00:05:39,850
from the turtle module import the turtle class.

82
00:05:40,420 --> 00:05:43,150
So now, instead of writing all of this,

83
00:05:43,210 --> 00:05:45,520
I can simply just write Timmy

84
00:05:45,790 --> 00:05:50,790
equals a new object created from the turtle class and it's constructed.

85
00:05:52,000 --> 00:05:55,540
This is how we would construct our new object.

86
00:05:56,770 --> 00:05:59,780
So now I have this brand new object called Timmy

87
00:06:00,290 --> 00:06:02,480
and if I go ahead and print timmy

88
00:06:02,900 --> 00:06:07,900
you can see that when I run this code what gets printed is a new turtle object

89
00:06:09,770 --> 00:06:11,180
from the turtle module

90
00:06:11,480 --> 00:06:14,990
and it's saved at this location in the computer's memory.

91
00:06:15,590 --> 00:06:17,690
So this is very different from say

92
00:06:17,690 --> 00:06:21,050
if we just printed a string or a number.

93
00:06:21,440 --> 00:06:24,710
It works completely differently. In this case,

94
00:06:24,740 --> 00:06:29,660
it's actually a object that's being printed. Now,

95
00:06:29,660 --> 00:06:31,640
what can we do with this object?

96
00:06:32,380 --> 00:06:37,380
[inaudible]

97
00:06:37,660 --> 00:06:40,480
Now, as we saw before with our car

98
00:06:40,510 --> 00:06:44,440
object it has certain attributes, right?

99
00:06:44,680 --> 00:06:49,680
Like speed and fuel, data that it could keep track of that are really important

100
00:06:51,190 --> 00:06:55,720
to the modeling of an actual car object. Now,

101
00:06:55,720 --> 00:06:58,720
when it comes to accessing these attributes,

102
00:06:58,840 --> 00:07:03,840
the syntax or the code looks like this. Car in this case is the object

103
00:07:04,480 --> 00:07:09,480
and then we use a dot or a full stop to separate the object from the actual

104
00:07:10,960 --> 00:07:14,530
attribute that we want to get hold of, which in this case is speed.

105
00:07:15,100 --> 00:07:18,940
What this code does is it essentially identifies the object

106
00:07:19,660 --> 00:07:23,590
and then it says, from this object get the speed attribute.

107
00:07:24,130 --> 00:07:28,030
This is actually gonna represent the speed of this particular car

108
00:07:28,030 --> 00:07:31,270
object. Coming back to our turtle.

109
00:07:31,900 --> 00:07:36,900
One of the other classes that is inside this turtle module is something called

110
00:07:37,480 --> 00:07:38,313
screen.

111
00:07:38,680 --> 00:07:43,680
And the screen represents the window in which this turtle is going to show up.

112
00:07:45,400 --> 00:07:47,320
So let's go ahead and create this screen

113
00:07:47,320 --> 00:07:50,260
object. I'm going to call my object my_screen

114
00:07:50,800 --> 00:07:54,970
and then I'm going to create it from this blueprint of a screen that we

115
00:07:54,970 --> 00:07:59,320
imported. Now we can tap into one of the screens properties

116
00:07:59,770 --> 00:08:03,070
which is called the canvas height and canvas width.

117
00:08:03,370 --> 00:08:07,690
So let's see what the height is, and notice how we're using that dot notation.

118
00:08:07,960 --> 00:08:12,670
So this is the object and then separated by a dot we've got the attributes that

119
00:08:12,670 --> 00:08:14,980
we want to access from this object.

120
00:08:15,400 --> 00:08:19,390
So I'm going to print that into the console. Now,

121
00:08:19,390 --> 00:08:21,220
when I go ahead and run this code,

122
00:08:21,550 --> 00:08:25,030
you'll see a window pop up very briefly

123
00:08:25,480 --> 00:08:29,530
and that was the screen on which our turtle is going to show up.

124
00:08:31,060 --> 00:08:33,850
Now, if you take a look in the console,

125
00:08:34,150 --> 00:08:37,510
you can see that not only have we printed the turtle object,

126
00:08:37,840 --> 00:08:42,840
but we've also printed the height of the canvas for this particular screen that

127
00:08:43,600 --> 00:08:44,500
we've created.

128
00:08:45,610 --> 00:08:50,380
So the screen here is the object and that canvas height is an attribute that's

129
00:08:50,380 --> 00:08:52,360
associated with that screen.

130
00:08:58,910 --> 00:09:02,540
Now, in addition to the things that an object has,

131
00:09:02,780 --> 00:09:06,140
so data that it holds like the speed or the fuel

132
00:09:06,470 --> 00:09:11,060
which we've already seen, it also has things that it can do, right?

133
00:09:11,060 --> 00:09:16,060
So functions that are associated with that particular object. And these functions

134
00:09:16,730 --> 00:09:19,880
when it's tied to an object is known as a method.

135
00:09:20,630 --> 00:09:24,530
But essentially we would use it the same way as we have done with any other

136
00:09:24,530 --> 00:09:26,330
function that we've created so far.

137
00:09:27,560 --> 00:09:30,980
The only difference is the syntax. Firstly,

138
00:09:31,010 --> 00:09:35,570
we would tap into the car object and then using that same dot notation that

139
00:09:35,570 --> 00:09:39,500
we saw with the attributes, we're saying car.stop.

140
00:09:39,710 --> 00:09:43,640
So essentially what we're doing is we're getting hold of the object and then

141
00:09:43,640 --> 00:09:47,870
we're calling this function that's associated with that object,

142
00:09:48,170 --> 00:09:52,790
which is known as a method. So coming back to our code here,

143
00:09:53,060 --> 00:09:57,830
one of the things that we can do with this screen object that we've created here

144
00:09:58,070 --> 00:10:02,840
called my_screen is we can tap into one of its functions.

145
00:10:03,320 --> 00:10:06,110
And because it's a function that's tied to an object,

146
00:10:06,110 --> 00:10:07,640
it's actually called a method.

147
00:10:08,150 --> 00:10:11,930
So the method name is called exitonclick.

148
00:10:12,530 --> 00:10:17,390
And what this will allow us to do is instead of having our screen show up and

149
00:10:17,390 --> 00:10:20,510
then quickly disappear when our code ends,

150
00:10:21,110 --> 00:10:26,110
this exitonclick will allow our program to continue running until we click on

151
00:10:27,980 --> 00:10:30,530
the screen and then it exits our code.

152
00:10:31,010 --> 00:10:36,010
So let's try this again and notice how we've now got our screen show up

153
00:10:36,770 --> 00:10:41,240
and this canvas has a height of 300 and a width of 300,

154
00:10:41,930 --> 00:10:46,730
but now we can see our turtle in the form of the arrow. So this is our

155
00:10:46,730 --> 00:10:48,830
a little Timmy shown up in the middle.

156
00:10:49,280 --> 00:10:53,600
And our code is only going to exit when the screen detects a click

157
00:10:53,720 --> 00:10:58,720
because we've called this function that's associated with the screen object.

158
00:10:59,000 --> 00:11:00,530
So now if I click on the screen,

159
00:11:00,800 --> 00:11:05,390
you can see process finished with exit code zero, our code is ended and the screen

160
00:11:05,390 --> 00:11:08,900
has disappeared. Now there are other functions as well.

161
00:11:08,900 --> 00:11:13,310
So if we take our little timmy object and we say, well, timmy,

162
00:11:13,520 --> 00:11:17,090
why don't we change the shape of you on the screen

163
00:11:17,150 --> 00:11:19,550
and we'll change you into an actual turtle?

164
00:11:20,750 --> 00:11:22,310
So now when I run the code,

165
00:11:23,180 --> 00:11:27,770
you can see that object in the center is now in the shape of a turtle.

166
00:11:29,000 --> 00:11:29,300
Now,

167
00:11:29,300 --> 00:11:33,590
if you want to know how I know about all these things I can do with the turtle

168
00:11:33,590 --> 00:11:36,440
graphics library, then of course,

169
00:11:36,470 --> 00:11:40,160
I'm getting all of this information from the documentation.

170
00:11:40,610 --> 00:11:42,200
So in the course resources,

171
00:11:42,230 --> 00:11:45,740
you'll see a link to this Turtle graphics documentation,

172
00:11:46,160 --> 00:11:49,520
and it tells you all the things that you can do with your turtle.

173
00:11:49,940 --> 00:11:50,900
So for example,

174
00:11:50,900 --> 00:11:55,510
you can call all of these methods that are associated with your turtle object to

175
00:11:55,510 --> 00:11:58,180
get your turtle to move and draw,

176
00:11:58,540 --> 00:12:03,400
and you can also use it to change its position or change its coordinates,

177
00:12:03,580 --> 00:12:07,720
as well as doing things like changing the color or resetting.

178
00:12:08,140 --> 00:12:12,250
And you can browse through this list to see how you would use each of these

179
00:12:12,250 --> 00:12:15,040
things. So for example,

180
00:12:15,370 --> 00:12:18,550
if I wanted to change the color of my turtle,

181
00:12:18,880 --> 00:12:22,600
I can do that by simply just calling this method, color,

182
00:12:23,020 --> 00:12:25,420
and then inside the parentheses

183
00:12:25,450 --> 00:12:29,650
I pass in the argument of the actual color string.

184
00:12:30,400 --> 00:12:35,400
Now you can choose from a whole range of colors just by putting in the name as a

185
00:12:36,280 --> 00:12:40,240
string into that method which is called color.

186
00:12:40,870 --> 00:12:45,870
So go ahead and see if you can change our turtle timmy's color from the default

187
00:12:46,960 --> 00:12:51,960
which is just black to one of the colors that you see in this link here,

188
00:12:53,290 --> 00:12:55,840
and the link, of course, is in the course resources.

189
00:12:59,170 --> 00:13:03,070
So let's say I want to change it to a coral color like this one.

190
00:13:03,670 --> 00:13:07,510
All I have to do is tap into my object which is called timmy,

191
00:13:08,050 --> 00:13:11,860
and then call the method which is called color onto it.

192
00:13:12,220 --> 00:13:14,260
And you can see all the methods are 

193
00:13:14,260 --> 00:13:16,870
denoted by a little M here in the circle.

194
00:13:17,500 --> 00:13:21,520
Now this color, of course, expects some sort of input.

195
00:13:22,180 --> 00:13:27,180
So the input that it expects is a actual string of a color.

196
00:13:28,030 --> 00:13:32,500
And my string is going to be coral, which I picked up from here.

197
00:13:33,640 --> 00:13:37,510
So now let's run our code and see what it looks like now.

198
00:13:37,960 --> 00:13:39,580
Once the screen refreshes,

199
00:13:39,610 --> 00:13:43,090
you can see that our turtle is now a nice coral color,

200
00:13:43,600 --> 00:13:46,990
and we've managed to do all of this to change the shape,

201
00:13:47,020 --> 00:13:52,020
change the color, all because we were able to call these functions

202
00:13:52,720 --> 00:13:53,800
which belong to this

203
00:13:53,800 --> 00:13:58,800
object. Now see if you can use the documentation here and figure out how to get the

204
00:13:59,440 --> 00:14:02,560
turtle to move forward by a hundred paces.

205
00:14:03,760 --> 00:14:06,250
Pause the video and try to complete this challenge.

206
00:14:08,860 --> 00:14:09,190
All right.

207
00:14:09,190 --> 00:14:13,420
So we saw earlier that it has a whole bunch of move and draw methods.

208
00:14:13,870 --> 00:14:18,550
So we can simply use this forward method to get it to move forward.

209
00:14:19,030 --> 00:14:21,070
And in terms of the parameters that it takes,

210
00:14:21,340 --> 00:14:25,750
it takes a number which is going to determine the distance that it's going to

211
00:14:25,750 --> 00:14:26,740
move forward by.

212
00:14:27,580 --> 00:14:31,600
So we said that we wanted our turtle to move forward by a hundred paces.

213
00:14:33,040 --> 00:14:37,900
All I have to do is specify the object that I want to move which is timmy,

214
00:14:38,470 --> 00:14:43,360
and then call that method forward and pass in the distance.

215
00:14:43,450 --> 00:14:47,650
So I said a hundred paces. So now let's run our code again,

216
00:14:48,430 --> 00:14:53,180
and you can see that little turtle just move forward by a hundred paces.

217
00:14:53,630 --> 00:14:58,630
So now we've seen how to create a new Object from a blueprint,

218
00:15:00,020 --> 00:15:05,020
we've seen how we can tap into its attributes by using the object name dot and

219
00:15:08,060 --> 00:15:09,350
then the name of the attribute.

220
00:15:09,650 --> 00:15:14,450
So this is the same as having a variable that's associated with that object.

221
00:15:14,930 --> 00:15:15,590
And finally,

222
00:15:15,590 --> 00:15:20,300
we've seen how we can call methods that are associated with the object.

223
00:15:20,540 --> 00:15:21,410
For example, here

224
00:15:21,410 --> 00:15:25,190
when we changed the shape or when we changed the color or when we told it to go

225
00:15:25,190 --> 00:15:27,830
forwards. Now in the next lesson,

226
00:15:27,860 --> 00:15:32,000
I'm going to show you how you can get hold of more external libraries

227
00:15:32,300 --> 00:15:37,160
not just turtle, but a whole world of packages that you can tap into.

228
00:15:37,460 --> 00:15:42,230
And you can start using these packages of code that other programmers have

229
00:15:42,230 --> 00:15:43,130
already developed,

230
00:15:43,430 --> 00:15:48,430
and simply by creating objects from the existing blueprints and reading the

231
00:15:48,710 --> 00:15:52,070
documentation to get it to do various things that you want it to.

232
00:15:52,550 --> 00:15:55,580
So for all of that and more, I'll see you on the next lesson.

