1
00:00:00,009 --> 00:00:01,950
So what we're going to do right now is we're going

2
00:00:01,960 --> 00:00:07,550
to create our code centered around the previous example I gave you

3
00:00:07,750 --> 00:00:12,279
regarding the library, the books and the attributes and methods

4
00:00:12,479 --> 00:00:15,010
associated with each book. So

5
00:00:15,680 --> 00:00:18,809
the first thing we need to do is to define

6
00:00:18,920 --> 00:00:20,979
the class book. So

7
00:00:21,420 --> 00:00:23,260
I'm gonna say class

8
00:00:24,059 --> 00:00:24,709
book

9
00:00:25,809 --> 00:00:31,229
and this is how we would create the class called book very straightforward.

10
00:00:32,330 --> 00:00:33,790
Now, the thing is

11
00:00:34,090 --> 00:00:36,180
we need to

12
00:00:36,509 --> 00:00:37,599
indicate

13
00:00:38,700 --> 00:00:40,369
the attributes

14
00:00:40,819 --> 00:00:44,439
that are going to be associated

15
00:00:44,819 --> 00:00:48,450
with every object of the book.

16
00:00:49,119 --> 00:00:49,630
OK?

17
00:00:50,040 --> 00:00:53,020
So to do this, I'm going to say the fine

18
00:00:54,560 --> 00:00:55,610
and now

19
00:00:56,000 --> 00:00:56,619
in it

20
00:00:58,470 --> 00:01:01,680
underscore underscore and now inside of the brackets,

21
00:01:02,169 --> 00:01:03,330
this is where

22
00:01:03,610 --> 00:01:06,330
we are now going to indicate

23
00:01:06,529 --> 00:01:10,040
what attributes are gonna be associated with each book.

24
00:01:10,919 --> 00:01:16,370
You typically start off with something called self. OK? This is very, very common

25
00:01:16,940 --> 00:01:19,330
in object oriented programming. OK? So

26
00:01:19,550 --> 00:01:24,440
self is a way to refer to each individual object

27
00:01:24,620 --> 00:01:26,290
of the class we're dealing with.

28
00:01:26,300 --> 00:01:28,580
In this case right now, it's going to be each individual book.

29
00:01:28,709 --> 00:01:32,379
So you always want to set off itself and now the actual attribute.

30
00:01:32,389 --> 00:01:33,540
So I'm gonna go with title

31
00:01:34,480 --> 00:01:35,169
also,

32
00:01:36,190 --> 00:01:38,239
and let's add one more, let's say genre,

33
00:01:38,529 --> 00:01:39,470
OK? Why not genre

34
00:01:40,519 --> 00:01:41,110
call on

35
00:01:42,639 --> 00:01:43,599
so far? So good.

36
00:01:44,059 --> 00:01:44,569
OK?

37
00:01:45,180 --> 00:01:50,040
Now we'll have to assign variables to each of our attributes.

38
00:01:50,629 --> 00:01:55,419
So I'm gonna start off by saying self dot title.

39
00:01:55,879 --> 00:01:57,319
You see how this works right now,

40
00:01:57,629 --> 00:02:00,440
self dot title equals title.

41
00:02:01,839 --> 00:02:04,290
And now can you guess the next one, self

42
00:02:05,690 --> 00:02:07,419
dot author

43
00:02:07,769 --> 00:02:09,350
equals author

44
00:02:10,679 --> 00:02:13,309
and then again, self dot

45
00:02:14,229 --> 00:02:14,880
genre

46
00:02:15,630 --> 00:02:16,610
equals to genre

47
00:02:17,649 --> 00:02:18,669
so far? So good.

48
00:02:20,839 --> 00:02:23,860
I'm gonna add one more attribute.

49
00:02:24,940 --> 00:02:30,470
It's not technically an attribute that we've defined thus far.

50
00:02:30,729 --> 00:02:35,919
But it it will be useful once we start creating the methods

51
00:02:36,630 --> 00:02:38,929
that will apply to the book. OK?

52
00:02:39,139 --> 00:02:41,149
And that is we want to indicate whether or not

53
00:02:41,160 --> 00:02:44,110
a book is available to be borrowed or not.

54
00:02:44,119 --> 00:02:44,449
So

55
00:02:44,850 --> 00:02:47,139
I am going to say self dot

56
00:02:47,740 --> 00:02:51,050
available right now, let's say self is

57
00:02:51,490 --> 00:02:51,970
OK,

58
00:02:52,149 --> 00:02:53,929
is underscore available,

59
00:02:54,660 --> 00:02:55,160
OK?

60
00:02:57,270 --> 00:03:02,270
We want to initialize this to be true at the very beginning. So

61
00:03:02,669 --> 00:03:03,839
at the very beginning,

62
00:03:04,000 --> 00:03:07,070
all books are available to be borrowed.

63
00:03:07,839 --> 00:03:08,339
OK?

64
00:03:09,179 --> 00:03:09,779
Now

65
00:03:10,559 --> 00:03:13,369
we've defined the attributes title author

66
00:03:13,759 --> 00:03:14,250
genre.

67
00:03:14,550 --> 00:03:17,970
We've also added one of the methods,

68
00:03:18,279 --> 00:03:20,690
one of one of the parameters that we're going to use in

69
00:03:20,699 --> 00:03:23,419
our methods that is whether or not the book is available.

70
00:03:23,610 --> 00:03:27,899
So now let us define the actual methods themselves.

71
00:03:28,539 --> 00:03:30,850
And the one that we're going to be dealing with here

72
00:03:31,000 --> 00:03:34,669
is going to be the borrow. So I'm gonna say borrow

73
00:03:35,320 --> 00:03:37,860
and now in brackets self.

74
00:03:38,770 --> 00:03:39,350
OK.

75
00:03:39,979 --> 00:03:42,710
We are creating a method

76
00:03:42,880 --> 00:03:47,119
that we can apply to the object book. So

77
00:03:47,600 --> 00:03:51,800
let us say that if the book is indeed available,

78
00:03:52,070 --> 00:03:53,839
what should happen?

79
00:03:54,059 --> 00:03:56,119
So I'm gonna say if

80
00:03:56,389 --> 00:03:59,000
self self referring to what the book

81
00:03:59,869 --> 00:04:02,100
is in fact available,

82
00:04:03,020 --> 00:04:04,199
what do we do

83
00:04:04,490 --> 00:04:06,990
remember that once the book is available

84
00:04:07,169 --> 00:04:08,240
to be borrowed,

85
00:04:08,419 --> 00:04:10,160
we will need to update

86
00:04:10,419 --> 00:04:12,619
the records in the library to indicate that the book

87
00:04:12,630 --> 00:04:15,520
is no longer available because now it's just been borrowed,

88
00:04:15,529 --> 00:04:15,720
right?

89
00:04:15,729 --> 00:04:16,119
So

90
00:04:16,559 --> 00:04:17,619
we're gonna say

91
00:04:17,880 --> 00:04:18,959
self

92
00:04:19,778 --> 00:04:22,118
uh dot is available

93
00:04:22,829 --> 00:04:25,730
should not be equal to what? False?

94
00:04:26,010 --> 00:04:28,529
Because it's no longer available.

95
00:04:29,399 --> 00:04:32,500
OK? Look at line 11. Again, this goes, this is very, very important. OK?

96
00:04:32,950 --> 00:04:35,459
Right here, we're doing that for the very first time.

97
00:04:35,470 --> 00:04:38,220
Somebody wants to borrow this book or maybe not for the first time,

98
00:04:38,230 --> 00:04:39,980
but the book is currently available.

99
00:04:39,989 --> 00:04:40,899
So what should happen?

100
00:04:41,130 --> 00:04:42,440
We should say that

101
00:04:42,570 --> 00:04:45,700
we should update the records to indicate that the book is no longer available

102
00:04:46,149 --> 00:04:47,179
and then

103
00:04:47,309 --> 00:04:49,540
let us print out something. OK,

104
00:04:49,670 --> 00:04:51,019
let us say print.

105
00:04:51,820 --> 00:04:54,350
And now in brackets, I'm gonna use the F string

106
00:04:54,890 --> 00:05:00,070
because I want to reference the actual title of the book that's been borrowed.

107
00:05:00,309 --> 00:05:02,899
So I'm gonna open up my collar braces

108
00:05:03,200 --> 00:05:06,320
and I'm gonna say self dot What's the title?

109
00:05:07,170 --> 00:05:08,839
OK. And now we can type in

110
00:05:09,140 --> 00:05:12,119
whatever it is we type in has been

111
00:05:12,299 --> 00:05:13,230
boiled.

112
00:05:15,000 --> 00:05:15,570
OK?

113
00:05:16,640 --> 00:05:17,980
At the closing quotes

114
00:05:18,510 --> 00:05:19,510
and there it is

115
00:05:20,019 --> 00:05:20,619
So

116
00:05:20,989 --> 00:05:23,649
two things should happen if the book is available to be borrowed.

117
00:05:23,730 --> 00:05:27,239
First of all, we need to update the records to indicate that the book has no

118
00:05:27,410 --> 00:05:30,630
longer available available because it's now been borrowed.

119
00:05:30,779 --> 00:05:32,549
And then we should print out the message saying

120
00:05:32,760 --> 00:05:34,869
the book has been borrowed.

121
00:05:37,709 --> 00:05:41,529
What is the else statement here for? What are why we indicating

122
00:05:42,230 --> 00:05:43,109
we didn't indicate

123
00:05:43,450 --> 00:05:44,049
because

124
00:05:44,179 --> 00:05:48,089
online 11 where it says if self is available

125
00:05:48,380 --> 00:05:50,130
right now at this stage,

126
00:05:50,299 --> 00:05:54,799
the program is assuming that the book is currently available.

127
00:05:54,964 --> 00:05:56,625
What if in a scenario where

128
00:05:56,954 --> 00:06:00,875
the library user wanted to borrow a book

129
00:06:00,885 --> 00:06:04,625
that wasn't actually available in the first place?

130
00:06:04,795 --> 00:06:07,545
That's what the else statement here is going to represent.

131
00:06:07,554 --> 00:06:12,105
So else if the book wasn't available initially, just simply print,

132
00:06:13,049 --> 00:06:14,350
print uh

133
00:06:15,200 --> 00:06:15,989
print

134
00:06:17,000 --> 00:06:17,500
and then

135
00:06:18,399 --> 00:06:19,899
we can just say sorry,

136
00:06:20,799 --> 00:06:24,630
this book is not currently

137
00:06:26,059 --> 00:06:26,760
available

138
00:06:27,839 --> 00:06:28,640
and I

139
00:06:29,089 --> 00:06:30,540
forgot to put this in code,

140
00:06:31,730 --> 00:06:34,000
sorry, this book is currently

141
00:06:34,119 --> 00:06:35,709
not available.

142
00:06:35,799 --> 00:06:36,350
Of course,

143
00:06:36,359 --> 00:06:40,790
we can also use the F string and self title to reference the actual name of the book.

144
00:06:40,799 --> 00:06:41,160
But

145
00:06:41,350 --> 00:06:42,510
that's not necessary.

146
00:06:42,519 --> 00:06:45,869
We're just gonna say sorry, this book is not currently available.

147
00:06:46,269 --> 00:06:46,790
OK?

148
00:06:47,809 --> 00:06:48,429
Awesome.

149
00:06:49,089 --> 00:06:49,779
Now

150
00:06:49,890 --> 00:06:52,299
let us define another method

151
00:06:52,720 --> 00:06:55,420
which would be to return the book

152
00:06:56,230 --> 00:06:57,529
so we can borrow the book,

153
00:06:57,989 --> 00:06:59,450
but we can also return it. So

154
00:06:59,769 --> 00:07:00,809
I'm gonna say

155
00:07:01,619 --> 00:07:05,839
define return underscore book self.

156
00:07:05,850 --> 00:07:07,739
So we've given the function of the method

157
00:07:07,750 --> 00:07:10,119
called return underscore book and then self.

158
00:07:10,130 --> 00:07:10,649
So in

159
00:07:10,880 --> 00:07:13,239
this case, right now, what should happen

160
00:07:13,600 --> 00:07:15,899
if the book has been returned?

161
00:07:16,070 --> 00:07:21,000
Now, we're gonna say self dot is available should now be equal to what true?

162
00:07:21,010 --> 00:07:22,890
Because the book is now available.

163
00:07:23,140 --> 00:07:25,760
And let's go ahead right now and print a message

164
00:07:27,429 --> 00:07:28,440
and say,

165
00:07:28,679 --> 00:07:32,190
let's also reference. So I'm going to say F string

166
00:07:32,880 --> 00:07:33,649
and then

167
00:07:33,929 --> 00:07:36,679
the name of the book has been returned.

168
00:07:37,679 --> 00:07:38,260
All right.

169
00:07:39,079 --> 00:07:39,739
So

170
00:07:40,489 --> 00:07:42,260
let us try

171
00:07:42,750 --> 00:07:47,029
this. Let's put it into action. OK? So I'm gonna come all the way down here,

172
00:07:48,049 --> 00:07:49,540
let's say book

173
00:07:50,579 --> 00:07:51,820
underscore A

174
00:07:53,410 --> 00:07:54,600
equals

175
00:07:54,920 --> 00:07:57,959
now to the actual object called book.

176
00:07:59,079 --> 00:08:01,049
And now inside,

177
00:08:01,750 --> 00:08:05,369
let us provide the name the title of the book, first of all.

178
00:08:05,540 --> 00:08:08,410
So let's say Python 101.

179
00:08:09,049 --> 00:08:12,980
OK. What's next the author, right? So let's call the author

180
00:08:13,529 --> 00:08:14,670
Alice.

181
00:08:15,589 --> 00:08:17,890
OK? And then what's the genre gonna be?

182
00:08:18,440 --> 00:08:20,799
Uh let's say programming, right? Programming.

183
00:08:21,929 --> 00:08:22,070
And

184
00:08:22,260 --> 00:08:25,059
now let us create another object.

185
00:08:25,779 --> 00:08:28,880
This object will be represented by book

186
00:08:30,540 --> 00:08:32,039
underscore B

187
00:08:33,049 --> 00:08:35,830
equals. And now we get book

188
00:08:36,270 --> 00:08:37,599
and now in brackets,

189
00:08:38,308 --> 00:08:40,520
let's say cyber

190
00:08:40,659 --> 00:08:43,270
201. OK. And then

191
00:08:43,690 --> 00:08:45,809
the author here is going to be Jack.

192
00:08:47,169 --> 00:08:48,880
And then what's going to be the genre,

193
00:08:49,599 --> 00:08:52,919
let's just say tech technology, right? Technology should be the genre

194
00:08:53,950 --> 00:08:55,210
and there it is.

195
00:08:55,690 --> 00:08:56,210
So

196
00:08:56,460 --> 00:08:58,169
gonna come all the way down here

197
00:08:58,390 --> 00:08:59,330
and now

198
00:08:59,539 --> 00:09:02,130
let us call the methods.

199
00:09:02,489 --> 00:09:03,090
OK.

200
00:09:03,429 --> 00:09:05,130
We have everything in place right now.

201
00:09:05,229 --> 00:09:08,299
Let us call our methods which are of course, what b

202
00:09:09,070 --> 00:09:10,489
and then return.

203
00:09:10,750 --> 00:09:11,239
So

204
00:09:11,940 --> 00:09:14,429
I can say book, first of all

205
00:09:14,679 --> 00:09:15,809
underscore

206
00:09:16,169 --> 00:09:16,630
A

207
00:09:16,739 --> 00:09:17,109
dot

208
00:09:18,200 --> 00:09:18,580
Borrow

209
00:09:20,450 --> 00:09:21,369
in brackets.

210
00:09:22,289 --> 00:09:22,869
All right.

211
00:09:23,859 --> 00:09:25,950
So, first of all, we're gonna borrow the book

212
00:09:26,900 --> 00:09:27,710
and then

213
00:09:29,200 --> 00:09:30,890
let me run the program

214
00:09:32,330 --> 00:09:37,739
to use it right now. It says, ok, Python 101 has been borrowed

215
00:09:37,909 --> 00:09:41,719
because over here it's going to indicate online 11.

216
00:09:41,799 --> 00:09:44,969
It's going to indicate at the very beginning that, ok, the book is available,

217
00:09:45,010 --> 00:09:46,530
it's now been borrowed.

218
00:09:46,539 --> 00:09:50,390
So we'll have to update the records to say it's false. And now we're gonna say

219
00:09:50,539 --> 00:09:52,590
the book has been borrowed.

220
00:09:53,340 --> 00:09:53,929
But now

221
00:09:54,849 --> 00:09:57,570
at this current stage, the book isn't available, right?

222
00:09:57,580 --> 00:10:01,880
So let us target the L statement. It should now print,

223
00:10:02,000 --> 00:10:06,099
sorry, this book is not currently available. If I want

224
00:10:06,729 --> 00:10:10,010
the method again, what if somebody else now comes in

225
00:10:10,190 --> 00:10:12,239
and wants to borrow the book

226
00:10:12,450 --> 00:10:15,219
that's already not available? What's gonna happen?

227
00:10:15,359 --> 00:10:16,559
I'm gonna run the program

228
00:10:16,659 --> 00:10:21,190
and now you say it, say sorry, this book is not currently available.

229
00:10:22,599 --> 00:10:23,549
Ok?

230
00:10:23,869 --> 00:10:24,750
But

231
00:10:25,289 --> 00:10:26,409
what if,

232
00:10:26,700 --> 00:10:27,659
what if

233
00:10:28,530 --> 00:10:31,190
I now return the book?

234
00:10:32,150 --> 00:10:33,099
What's he gonna say?

235
00:10:33,789 --> 00:10:37,969
It's gonna say Python 101 has been returned.

236
00:10:38,700 --> 00:10:39,400
And now

237
00:10:39,929 --> 00:10:41,239
if I run

238
00:10:42,830 --> 00:10:46,570
the return method again, I'm sorry, the borrow method again

239
00:10:48,450 --> 00:10:49,609
one more time

240
00:10:50,109 --> 00:10:51,109
was it gonna say

241
00:10:51,419 --> 00:10:54,789
it's gonna say Python 101 has been browed.

242
00:10:55,000 --> 00:10:56,190
So you can see right now

243
00:10:56,409 --> 00:10:58,840
it works perfectly fine. So

244
00:10:58,989 --> 00:11:05,159
just to give you a very quick recap first of all, we had to create a class called book.

245
00:11:05,659 --> 00:11:09,320
And now inside we have to define the attributes

246
00:11:09,520 --> 00:11:12,419
associated with each book.

247
00:11:12,429 --> 00:11:16,789
And we did that by saying, define on the score and the score in it on the score and then

248
00:11:17,210 --> 00:11:20,479
the parameters of the attributes in brackets. And then

249
00:11:20,599 --> 00:11:24,929
we assigned each attribute to a value. So self dot title

250
00:11:25,150 --> 00:11:28,530
will be equal to title. Self dot author will be equal to author.

251
00:11:28,539 --> 00:11:30,159
Self dot journal will be equal to genre.

252
00:11:30,559 --> 00:11:33,849
And then we also created a method in here that

253
00:11:33,859 --> 00:11:38,289
will indicate whether or not we can borrow the book.

254
00:11:38,440 --> 00:11:41,150
So this is basically a boolean attribute.

255
00:11:41,159 --> 00:11:45,030
We said, OK, self dot is available at the very beginning should be equal to true.

256
00:11:45,580 --> 00:11:49,840
And now we created two different methods. The first one here would be the

257
00:11:50,200 --> 00:11:50,479
bole.

258
00:11:50,849 --> 00:11:53,340
So what happens when a book is borrowed?

259
00:11:53,640 --> 00:11:56,039
If it was initially available,

260
00:11:56,200 --> 00:12:00,549
we'll update the vehicles to say self is available is no longer available.

261
00:12:00,559 --> 00:12:01,390
So it's false.

262
00:12:01,570 --> 00:12:03,599
And then we should print out the title that says

263
00:12:03,960 --> 00:12:06,190
sorry that this book has been borrowed.

264
00:12:06,200 --> 00:12:11,309
However, else if the book was already borrowed initially,

265
00:12:11,320 --> 00:12:12,729
we should just simply print out sorry.

266
00:12:12,739 --> 00:12:14,590
This book is not currently available.

267
00:12:14,820 --> 00:12:18,570
And then the second method what happens when the book is returned?

268
00:12:18,719 --> 00:12:21,409
We should now update our records yet again to

269
00:12:21,419 --> 00:12:24,419
indicate that self is available is now true.

270
00:12:24,559 --> 00:12:28,570
And then we can print out the name of the book saying it has been returned.

271
00:12:28,760 --> 00:12:29,950
And finally,

272
00:12:30,169 --> 00:12:34,010
we learned how to create individual objects in individual books.

273
00:12:34,020 --> 00:12:37,510
We can simply say book underscore A equals book. And then

274
00:12:37,780 --> 00:12:40,200
in brackets, we have to fill in the values for

275
00:12:40,890 --> 00:12:41,590
the

276
00:12:42,210 --> 00:12:43,150
attributes.

277
00:12:43,340 --> 00:12:47,609
And then we created another book, book B which is Cyber to One Jack Technology.

278
00:12:47,619 --> 00:12:50,479
And then we called the Methods

279
00:12:50,729 --> 00:12:53,380
and that's pretty much it. So thank you for watching the video.

280
00:12:53,650 --> 00:12:55,460
I will see you in the next class.

