Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,160 --> 00:00:02,399
hey what's going on everybody it's bro
2
00:00:02,399 --> 00:00:03,360
hope you're doing well
3
00:00:03,360 --> 00:00:04,799
and in this video i'm going to help you
4
00:00:04,799 --> 00:00:06,560
get started with python so
5
00:00:06,560 --> 00:00:11,280
sit back relax and enjoy the show
6
00:00:11,360 --> 00:00:13,920
if you wouldn't mind please like comment
7
00:00:13,920 --> 00:00:14,880
and subscribe
8
00:00:14,880 --> 00:00:16,880
one like equals one prayer for the
9
00:00:16,880 --> 00:00:18,400
youtube algorithm
10
00:00:18,400 --> 00:00:20,320
i'm gonna tell you why you need to learn
11
00:00:20,320 --> 00:00:22,640
python it's the easiest programming
12
00:00:22,640 --> 00:00:24,480
language to learn as well as being the
13
00:00:24,480 --> 00:00:26,560
most popular in the world right now
14
00:00:26,560 --> 00:00:28,800
and according to glassdoor the salary
15
00:00:28,800 --> 00:00:31,199
for new python developers in the united
16
00:00:31,199 --> 00:00:32,159
states is
17
00:00:32,159 --> 00:00:35,440
64 000 if any of this sounds good to you
18
00:00:35,440 --> 00:00:36,079
well then
19
00:00:36,079 --> 00:00:37,840
let's get started if you need to
20
00:00:37,840 --> 00:00:39,719
download python go to
21
00:00:39,719 --> 00:00:42,480
python.org downloads and click this
22
00:00:42,480 --> 00:00:44,000
yellow download python
23
00:00:44,000 --> 00:00:47,440
button open when done check add
24
00:00:47,440 --> 00:00:51,440
python 3.9 or another version to path
25
00:00:51,440 --> 00:00:53,840
install now and then give it a little
26
00:00:53,840 --> 00:00:56,000
bit it states here that setup was
27
00:00:56,000 --> 00:00:58,320
successful so let's close out of this
28
00:00:58,320 --> 00:01:00,879
our next step is to download an ide
29
00:01:00,879 --> 00:01:02,239
that's an acronym for
30
00:01:02,239 --> 00:01:04,400
integrated development environment think
31
00:01:04,400 --> 00:01:05,280
of it as
32
00:01:05,280 --> 00:01:07,040
software that helps us write other
33
00:01:07,040 --> 00:01:09,680
software one ide that i recommend
34
00:01:09,680 --> 00:01:11,799
is pycharm you can find it at
35
00:01:11,799 --> 00:01:13,600
jetbrains.com
36
00:01:13,600 --> 00:01:17,040
pycharm go to download if you would like
37
00:01:17,040 --> 00:01:19,200
to pay for a professional version you
38
00:01:19,200 --> 00:01:19,920
can click
39
00:01:19,920 --> 00:01:21,920
this download button but since i don't
40
00:01:21,920 --> 00:01:23,680
like to pay for things i'm going to use
41
00:01:23,680 --> 00:01:26,799
the free community version at this point
42
00:01:26,799 --> 00:01:28,560
we just have to follow the standard
43
00:01:28,560 --> 00:01:29,920
installation procedure
44
00:01:29,920 --> 00:01:32,320
click next you can pick a destination
45
00:01:32,320 --> 00:01:34,720
folder but i'll keep the default
46
00:01:34,720 --> 00:01:37,119
next if you would like a desktop
47
00:01:37,119 --> 00:01:38,079
shortcut
48
00:01:38,079 --> 00:01:40,640
then check this i would also recommend
49
00:01:40,640 --> 00:01:41,439
updating the
50
00:01:41,439 --> 00:01:44,880
path variable next
51
00:01:44,880 --> 00:01:48,079
install and then give it a minute or two
52
00:01:48,079 --> 00:01:50,399
then after finishing i have a desktop
53
00:01:50,399 --> 00:01:52,159
shortcut so i can just click on this to
54
00:01:52,159 --> 00:01:53,520
run pycharm
55
00:01:53,520 --> 00:01:56,640
let's create a new project name this
56
00:01:56,640 --> 00:01:58,560
project whatever you like i'm going to
57
00:01:58,560 --> 00:01:59,759
name this project
58
00:01:59,759 --> 00:02:03,280
hello world and i recommend not creating
59
00:02:03,280 --> 00:02:03,680
a
60
00:02:03,680 --> 00:02:05,920
main dot pi welcome script right now
61
00:02:05,920 --> 00:02:07,920
i'll show you how to do that manually
62
00:02:07,920 --> 00:02:09,919
and let's create
63
00:02:09,919 --> 00:02:12,319
if you need a python file to work with
64
00:02:12,319 --> 00:02:14,000
this is how to create one go to
65
00:02:14,000 --> 00:02:17,760
file new python file and i'm going to
66
00:02:17,760 --> 00:02:18,160
name
67
00:02:18,160 --> 00:02:23,200
this main and then click python file
68
00:02:23,200 --> 00:02:25,120
all right ladies and gentlemen we now
69
00:02:25,120 --> 00:02:27,120
have an empty python script that we can
70
00:02:27,120 --> 00:02:28,720
use to begin coding
71
00:02:28,720 --> 00:02:31,040
now if you checked this box create a
72
00:02:31,040 --> 00:02:33,440
main.pi welcome script and then created
73
00:02:33,440 --> 00:02:34,640
a new project
74
00:02:34,640 --> 00:02:37,200
then your main python file will probably
75
00:02:37,200 --> 00:02:38,879
look a little something like this but
76
00:02:38,879 --> 00:02:40,400
that's completely fine we can just
77
00:02:40,400 --> 00:02:41,920
delete this
78
00:02:41,920 --> 00:02:43,840
i don't know about you guys but the font
79
00:02:43,840 --> 00:02:45,360
is very small for me
80
00:02:45,360 --> 00:02:47,360
so let's change that we can change that
81
00:02:47,360 --> 00:02:49,360
by going to file
82
00:02:49,360 --> 00:02:52,560
settings editor font and you can
83
00:02:52,560 --> 00:02:54,000
increase the font size
84
00:02:54,000 --> 00:02:56,480
let's say 25 you can also change the
85
00:02:56,480 --> 00:02:57,680
font too
86
00:02:57,680 --> 00:03:01,200
what's a good one uh how about
87
00:03:01,200 --> 00:03:04,239
that then click apply and
88
00:03:04,239 --> 00:03:06,879
okay much better we can actually see
89
00:03:06,879 --> 00:03:07,360
something
90
00:03:07,360 --> 00:03:09,519
to run a python script just click the
91
00:03:09,519 --> 00:03:11,200
screen play button in the corner
92
00:03:11,200 --> 00:03:13,680
alternatively you can go to the run tab
93
00:03:13,680 --> 00:03:15,360
and then go to run
94
00:03:15,360 --> 00:03:17,280
okay so this box that appeared at the
95
00:03:17,280 --> 00:03:20,000
bottom this is referred to as a terminal
96
00:03:20,000 --> 00:03:21,440
window this will display
97
00:03:21,440 --> 00:03:23,840
any output that your program has and
98
00:03:23,840 --> 00:03:25,200
this line at the end
99
00:03:25,200 --> 00:03:27,519
process finished with exit code zero
100
00:03:27,519 --> 00:03:29,120
that just means that there were no
101
00:03:29,120 --> 00:03:30,560
errors in this program
102
00:03:30,560 --> 00:03:32,319
so currently this program doesn't do
103
00:03:32,319 --> 00:03:34,080
anything but let's change that let's
104
00:03:34,080 --> 00:03:35,200
print something
105
00:03:35,200 --> 00:03:37,680
let's print a message so to print a
106
00:03:37,680 --> 00:03:39,760
message to the terminal window
107
00:03:39,760 --> 00:03:42,080
type print and then add a set of
108
00:03:42,080 --> 00:03:42,959
parentheses
109
00:03:42,959 --> 00:03:44,720
and then you can type a message but it
110
00:03:44,720 --> 00:03:46,000
has to be within quotes
111
00:03:46,000 --> 00:03:48,959
either double quotes or single quotes it
112
00:03:48,959 --> 00:03:49,920
doesn't matter
113
00:03:49,920 --> 00:03:52,879
so let's think of a message i don't know
114
00:03:52,879 --> 00:03:53,599
i
115
00:03:53,599 --> 00:03:57,519
love pizza then let me run this
116
00:03:57,519 --> 00:04:00,319
and in my terminal window it says i love
117
00:04:00,319 --> 00:04:01,120
pizza
118
00:04:01,120 --> 00:04:03,439
to print another line just add another
119
00:04:03,439 --> 00:04:04,640
print statement
120
00:04:04,640 --> 00:04:06,799
print and then your message within
121
00:04:06,799 --> 00:04:08,239
quotes
122
00:04:08,239 --> 00:04:12,799
it's really good
123
00:04:13,519 --> 00:04:16,000
and well what do you know it printed i
124
00:04:16,000 --> 00:04:17,040
love pizza
125
00:04:17,040 --> 00:04:19,120
it's really good so ladies and gentlemen
126
00:04:19,120 --> 00:04:21,759
you just wrote your first python program
127
00:04:21,759 --> 00:04:24,080
by the way you can change the font as
128
00:04:24,080 --> 00:04:26,320
well as the font color of your terminal
129
00:04:26,320 --> 00:04:27,520
window by going to
130
00:04:27,520 --> 00:04:31,199
file settings then go to editor
131
00:04:31,199 --> 00:04:34,240
color scheme console font and console
132
00:04:34,240 --> 00:04:34,960
colors
133
00:04:34,960 --> 00:04:38,160
let's change the font let's say i would
134
00:04:38,160 --> 00:04:39,120
like
135
00:04:39,120 --> 00:04:43,199
whatever this font is then click apply
136
00:04:43,199 --> 00:04:45,520
let's change the font color too by going
137
00:04:45,520 --> 00:04:47,280
to console colors
138
00:04:47,280 --> 00:04:51,360
for any output you can click number two
139
00:04:51,360 --> 00:04:54,320
and select a color let's say i would
140
00:04:54,320 --> 00:04:54,800
like
141
00:04:54,800 --> 00:04:59,759
bright green that should be decent
142
00:04:59,759 --> 00:05:02,160
and for anything else let's say this
143
00:05:02,160 --> 00:05:04,400
will be a dark color so i can barely see
144
00:05:04,400 --> 00:05:05,360
it
145
00:05:05,360 --> 00:05:08,400
so then let's click apply and okay
146
00:05:08,400 --> 00:05:10,479
oh yeah that's much better well
147
00:05:10,479 --> 00:05:12,400
everybody that was your very first
148
00:05:12,400 --> 00:05:14,240
python program i'll post
149
00:05:14,240 --> 00:05:16,479
any useful links and timestamps for this
150
00:05:16,479 --> 00:05:18,880
video in the comments section down below
151
00:05:18,880 --> 00:05:22,800
but yeah welcome to coding with python
152
00:05:22,800 --> 00:05:25,199
hey you yeah i'm talking to you if you
153
00:05:25,199 --> 00:05:26,240
learned something new
154
00:05:26,240 --> 00:05:28,639
then help me help you in three easy
155
00:05:28,639 --> 00:05:29,280
steps
156
00:05:29,280 --> 00:05:31,360
by smashing that like button drop a
157
00:05:31,360 --> 00:05:32,560
comment down below
158
00:05:32,560 --> 00:05:34,240
and subscribe if you'd like to become a
159
00:05:34,240 --> 00:05:38,640
fellow bro
160
00:05:38,640 --> 00:05:56,540
[Music]
161
00:05:56,960 --> 00:05:59,360
hey what's going on everybody it's bro
162
00:05:59,360 --> 00:06:01,120
hope you're doing well and in this video
163
00:06:01,120 --> 00:06:02,400
i'm going to explain how
164
00:06:02,400 --> 00:06:05,280
variables work in python so sit back
165
00:06:05,280 --> 00:06:06,400
relax
166
00:06:06,400 --> 00:06:09,440
and enjoy the show
167
00:06:09,759 --> 00:06:11,360
if you find this video helpful please
168
00:06:11,360 --> 00:06:12,800
remember to like
169
00:06:12,800 --> 00:06:15,280
comment and subscribe your support will
170
00:06:15,280 --> 00:06:17,120
help keep this channel running
171
00:06:17,120 --> 00:06:20,319
all right let's do this a variable is a
172
00:06:20,319 --> 00:06:22,880
container for a value it behaves as the
173
00:06:22,880 --> 00:06:24,319
value that it contains
174
00:06:24,319 --> 00:06:26,720
it's much like in algebra lessons back
175
00:06:26,720 --> 00:06:28,560
in the day where we had to solve for
176
00:06:28,560 --> 00:06:29,440
whatever value
177
00:06:29,440 --> 00:06:31,919
x was and then we can reuse x and it
178
00:06:31,919 --> 00:06:34,000
behaved exactly as that value
179
00:06:34,000 --> 00:06:35,759
well with programming we can do that
180
00:06:35,759 --> 00:06:37,680
plus we can assign variables of
181
00:06:37,680 --> 00:06:38,960
different data types
182
00:06:38,960 --> 00:06:40,720
so they are not limited to just numbers
183
00:06:40,720 --> 00:06:42,800
we can assign whole words
184
00:06:42,800 --> 00:06:44,639
numbers and even these things called
185
00:06:44,639 --> 00:06:46,800
booleans which are either true or false
186
00:06:46,800 --> 00:06:48,639
but i'll get more in depth into data
187
00:06:48,639 --> 00:06:49,840
types a little bit later on in this
188
00:06:49,840 --> 00:06:50,479
video
189
00:06:50,479 --> 00:06:51,919
so let's create a variable and this is
190
00:06:51,919 --> 00:06:54,000
how to do so we need a unique name for
191
00:06:54,000 --> 00:06:54,880
this variable
192
00:06:54,880 --> 00:06:57,919
let's say name name equals some value
193
00:06:57,919 --> 00:06:59,680
now the first data type that we'll cover
194
00:06:59,680 --> 00:07:01,199
is strings a string
195
00:07:01,199 --> 00:07:03,919
is a series of characters so to create a
196
00:07:03,919 --> 00:07:04,479
string
197
00:07:04,479 --> 00:07:06,720
we can either use single quotes or
198
00:07:06,720 --> 00:07:08,319
double quotes in python
199
00:07:08,319 --> 00:07:10,479
and we can assign this a value of
200
00:07:10,479 --> 00:07:11,759
whatever your own name is
201
00:07:11,759 --> 00:07:14,319
so i will assign this variable name a
202
00:07:14,319 --> 00:07:15,360
value of bro
203
00:07:15,360 --> 00:07:18,000
now this variable will behave exactly as
204
00:07:18,000 --> 00:07:18,800
if it was
205
00:07:18,800 --> 00:07:20,880
this value this name and then we can
206
00:07:20,880 --> 00:07:22,960
reuse this variable for something
207
00:07:22,960 --> 00:07:25,840
so let's print whatever our name is
208
00:07:25,840 --> 00:07:26,560
print
209
00:07:26,560 --> 00:07:28,800
name and this will print whatever your
210
00:07:28,800 --> 00:07:30,800
name is to the console window
211
00:07:30,800 --> 00:07:32,720
now when you print your variable make
212
00:07:32,720 --> 00:07:34,479
sure you don't put it within quotes
213
00:07:34,479 --> 00:07:36,479
because what we're doing then is
214
00:07:36,479 --> 00:07:37,840
literally printing
215
00:07:37,840 --> 00:07:41,199
the word name as you can see here so
216
00:07:41,199 --> 00:07:42,960
if you need to use your variable for
217
00:07:42,960 --> 00:07:44,400
something make sure it's not within
218
00:07:44,400 --> 00:07:45,199
quotes
219
00:07:45,199 --> 00:07:48,319
now we can combine our string variable
220
00:07:48,319 --> 00:07:50,720
with another string so within our print
221
00:07:50,720 --> 00:07:52,000
statement let's print
222
00:07:52,000 --> 00:07:55,280
a string literal such as hello space
223
00:07:55,280 --> 00:07:58,319
plus name so we are combining strings
224
00:07:58,319 --> 00:07:59,840
and then we're printing the results to
225
00:07:59,840 --> 00:08:02,160
the console window so this will print
226
00:08:02,160 --> 00:08:05,120
hello bro now you can actually check the
227
00:08:05,120 --> 00:08:06,639
data type of a variable
228
00:08:06,639 --> 00:08:08,319
so i'm going to turn this line into a
229
00:08:08,319 --> 00:08:10,319
comment so what you'll do
230
00:08:10,319 --> 00:08:12,160
is take the name of the variable
231
00:08:12,160 --> 00:08:14,319
surround this with a set of parentheses
232
00:08:14,319 --> 00:08:16,800
and then precede the set of parentheses
233
00:08:16,800 --> 00:08:17,680
with the type
234
00:08:17,680 --> 00:08:20,080
function and this will print the data
235
00:08:20,080 --> 00:08:21,840
type of this variable to the console
236
00:08:21,840 --> 00:08:22,479
window
237
00:08:22,479 --> 00:08:25,199
and you can see that it says class str
238
00:08:25,199 --> 00:08:26,160
short for string
239
00:08:26,160 --> 00:08:28,319
a string is a series of characters a
240
00:08:28,319 --> 00:08:30,240
name is a good example of a string
241
00:08:30,240 --> 00:08:32,958
a series of characters so that is how to
242
00:08:32,958 --> 00:08:34,640
check the data type
243
00:08:34,640 --> 00:08:37,679
of a variable just use the type function
244
00:08:37,679 --> 00:08:39,519
now you can actually combine variables
245
00:08:39,519 --> 00:08:41,519
together as long as they're of the same
246
00:08:41,519 --> 00:08:44,480
data type so let's change our name to
247
00:08:44,480 --> 00:08:45,920
maybe first name and we'll create a
248
00:08:45,920 --> 00:08:47,839
second variable called lastname
249
00:08:47,839 --> 00:08:49,760
now a common naming convention with
250
00:08:49,760 --> 00:08:51,440
variables if it has
251
00:08:51,440 --> 00:08:53,839
two words separate each word with an
252
00:08:53,839 --> 00:08:54,480
underscore
253
00:08:54,480 --> 00:08:56,160
it's not necessary but it's common
254
00:08:56,160 --> 00:08:58,080
practice for python and honestly i
255
00:08:58,080 --> 00:08:59,680
sometimes forget about that but don't
256
00:08:59,680 --> 00:09:00,640
tell anybody
257
00:09:00,640 --> 00:09:02,640
so let's create another variable called
258
00:09:02,640 --> 00:09:03,680
last name last
259
00:09:03,680 --> 00:09:07,360
name equals whatever your last name is
260
00:09:07,360 --> 00:09:09,519
and then let's create a third variable
261
00:09:09,519 --> 00:09:10,959
called full name
262
00:09:10,959 --> 00:09:14,160
full underscore name and let's combine
263
00:09:14,160 --> 00:09:18,000
first name plus last
264
00:09:18,000 --> 00:09:22,080
name and let us display full name
265
00:09:22,080 --> 00:09:25,120
along with hello plus full name
266
00:09:25,120 --> 00:09:26,880
now these two names were combined
267
00:09:26,880 --> 00:09:28,640
together to create a whole
268
00:09:28,640 --> 00:09:30,399
new variable actually what i think i'll
269
00:09:30,399 --> 00:09:32,480
do is add a space
270
00:09:32,480 --> 00:09:35,120
within my variable so i'm doing a bunch
271
00:09:35,120 --> 00:09:37,200
of string concatenation i'm combining
272
00:09:37,200 --> 00:09:39,600
the variable first name plus a space
273
00:09:39,600 --> 00:09:40,560
character plus
274
00:09:40,560 --> 00:09:43,040
last name so the result is hello
275
00:09:43,040 --> 00:09:44,720
whatever your first name and last name
276
00:09:44,720 --> 00:09:45,279
is
277
00:09:45,279 --> 00:09:48,480
so that is a variable of the string data
278
00:09:48,480 --> 00:09:51,040
type it is a series of characters
279
00:09:51,040 --> 00:09:53,360
now with strings we cannot normally use
280
00:09:53,360 --> 00:09:55,200
these for any sort of math
281
00:09:55,200 --> 00:09:57,279
that's where the next data type comes in
282
00:09:57,279 --> 00:09:59,040
for the time being i just turned all of
283
00:09:59,040 --> 00:10:00,800
these lines into comments and we are
284
00:10:00,800 --> 00:10:02,399
going to move on to the int
285
00:10:02,399 --> 00:10:04,640
data type short for integer this time
286
00:10:04,640 --> 00:10:06,160
let's create a variable called
287
00:10:06,160 --> 00:10:08,640
age and we will assign this a value of
288
00:10:08,640 --> 00:10:09,680
whatever your age is
289
00:10:09,680 --> 00:10:12,079
let's just say that i'm 21 now when you
290
00:10:12,079 --> 00:10:12,880
assign
291
00:10:12,880 --> 00:10:15,519
a value of the int data type make sure
292
00:10:15,519 --> 00:10:16,320
that it is
293
00:10:16,320 --> 00:10:18,160
not within quotes because then
294
00:10:18,160 --> 00:10:19,920
technically it would be a string then
295
00:10:19,920 --> 00:10:21,600
and you can see that the color scheme
296
00:10:21,600 --> 00:10:23,360
for this number actually changed
297
00:10:23,360 --> 00:10:25,839
so if this was a string we would treat
298
00:10:25,839 --> 00:10:26,399
it different
299
00:10:26,399 --> 00:10:28,720
than an int data type because with
300
00:10:28,720 --> 00:10:30,640
strings we cannot normally use them for
301
00:10:30,640 --> 00:10:31,680
any sort of math
302
00:10:31,680 --> 00:10:34,399
so we now have a variable called edge
303
00:10:34,399 --> 00:10:35,920
and we can actually
304
00:10:35,920 --> 00:10:38,000
increase or decrease or do whatever we
305
00:10:38,000 --> 00:10:38,959
want with the edge
306
00:10:38,959 --> 00:10:41,040
let's increase our age by one let's say
307
00:10:41,040 --> 00:10:42,880
it's our birthday to do so we would say
308
00:10:42,880 --> 00:10:43,600
age
309
00:10:43,600 --> 00:10:46,560
equals age plus one and then let's print
310
00:10:46,560 --> 00:10:48,399
our edge to the console window
311
00:10:48,399 --> 00:10:51,200
print edge and you can see that it says
312
00:10:51,200 --> 00:10:52,320
i am 22.
313
00:10:52,320 --> 00:10:53,839
now there's a shorthand way of writing
314
00:10:53,839 --> 00:10:55,760
this what you'll do is type
315
00:10:55,760 --> 00:10:58,640
age plus equals one so that's kind of
316
00:10:58,640 --> 00:11:00,399
like a shortcut and this would work the
317
00:11:00,399 --> 00:11:01,040
same too
318
00:11:01,040 --> 00:11:03,200
now let's print the data type of our
319
00:11:03,200 --> 00:11:05,040
inch variable because we can within a
320
00:11:05,040 --> 00:11:07,760
print statement i will print the edge
321
00:11:07,760 --> 00:11:10,959
and add my edge variable to this type
322
00:11:10,959 --> 00:11:13,120
function and this will print the data
323
00:11:13,120 --> 00:11:15,040
type of my edge variable to the console
324
00:11:15,040 --> 00:11:15,839
window
325
00:11:15,839 --> 00:11:17,839
and i will turn this line into a comment
326
00:11:17,839 --> 00:11:19,680
just so that it doesn't get in the way
327
00:11:19,680 --> 00:11:21,920
now if i were to print the data type of
328
00:11:21,920 --> 00:11:23,040
my age variable
329
00:11:23,040 --> 00:11:25,519
as you can see it is of the int data
330
00:11:25,519 --> 00:11:27,040
type short for integer
331
00:11:27,040 --> 00:11:30,720
it only stores a whole integer number
332
00:11:30,720 --> 00:11:33,519
now what if this was a string so what
333
00:11:33,519 --> 00:11:34,240
i'm going to do
334
00:11:34,240 --> 00:11:36,880
is surround my value within a set of
335
00:11:36,880 --> 00:11:37,839
quotes
336
00:11:37,839 --> 00:11:39,519
and i will turn this line into a comment
337
00:11:39,519 --> 00:11:41,040
so that it does not get in the way
338
00:11:41,040 --> 00:11:43,040
you can see that the data type of my age
339
00:11:43,040 --> 00:11:44,079
variable is now
340
00:11:44,079 --> 00:11:46,240
a string and with strings we cannot
341
00:11:46,240 --> 00:11:48,320
normally use them for any sort of math
342
00:11:48,320 --> 00:11:50,480
so let's attempt to increase our age
343
00:11:50,480 --> 00:11:52,000
variable by one
344
00:11:52,000 --> 00:11:54,959
and we ran into an error a type air can
345
00:11:54,959 --> 00:11:56,639
only concatenate string
346
00:11:56,639 --> 00:11:59,600
to string not end now the point being is
347
00:11:59,600 --> 00:12:00,000
that
348
00:12:00,000 --> 00:12:02,000
it's important to use the appropriate
349
00:12:02,000 --> 00:12:03,519
data type because
350
00:12:03,519 --> 00:12:05,279
with strings we cannot normally use them
351
00:12:05,279 --> 00:12:06,959
for any sort of math you'll want to use
352
00:12:06,959 --> 00:12:09,279
a data type of a numeric value
353
00:12:09,279 --> 00:12:11,360
an int or a float which we'll discuss a
354
00:12:11,360 --> 00:12:13,279
little bit later now here's a situation
355
00:12:13,279 --> 00:12:13,920
to consider
356
00:12:13,920 --> 00:12:15,680
what if we would like to display our
357
00:12:15,680 --> 00:12:17,920
edge variable along with a string
358
00:12:17,920 --> 00:12:19,760
literal much like what we did with this
359
00:12:19,760 --> 00:12:22,320
line involving a string literal such as
360
00:12:22,320 --> 00:12:22,800
hello
361
00:12:22,800 --> 00:12:25,040
plus the user's full name so let's
362
00:12:25,040 --> 00:12:26,800
attempt to do so and this is not going
363
00:12:26,800 --> 00:12:28,399
to work and i'll explain why
364
00:12:28,399 --> 00:12:30,160
so within our print statement let's say
365
00:12:30,160 --> 00:12:31,760
something such as
366
00:12:31,760 --> 00:12:35,120
your age is plus
367
00:12:35,120 --> 00:12:38,320
edge now we're going to run into a type
368
00:12:38,320 --> 00:12:38,880
error
369
00:12:38,880 --> 00:12:40,800
that's because we attempted to use
370
00:12:40,800 --> 00:12:42,720
string concatenation with a string
371
00:12:42,720 --> 00:12:43,440
literal
372
00:12:43,440 --> 00:12:46,240
and a variable of a different data type
373
00:12:46,240 --> 00:12:47,360
a variable of the
374
00:12:47,360 --> 00:12:50,399
int data type in order to display our
375
00:12:50,399 --> 00:12:52,320
edge along with the string we would need
376
00:12:52,320 --> 00:12:54,480
to convert our edge variable to the
377
00:12:54,480 --> 00:12:55,040
string
378
00:12:55,040 --> 00:12:57,040
data type and one way in which we can do
379
00:12:57,040 --> 00:12:59,120
that is by type casting
380
00:12:59,120 --> 00:13:00,880
and we'll cover this more in the next
381
00:13:00,880 --> 00:13:02,880
video so we're going to surround
382
00:13:02,880 --> 00:13:05,360
our age variable that is of the in data
383
00:13:05,360 --> 00:13:07,040
type with a set of parentheses
384
00:13:07,040 --> 00:13:09,519
and precede this with str short for
385
00:13:09,519 --> 00:13:11,279
string and this will convert our
386
00:13:11,279 --> 00:13:12,000
variable
387
00:13:12,000 --> 00:13:13,839
to a string that will allow us to
388
00:13:13,839 --> 00:13:15,519
display it with another string
389
00:13:15,519 --> 00:13:17,519
now if we were to run this program this
390
00:13:17,519 --> 00:13:19,360
way this will now display
391
00:13:19,360 --> 00:13:22,639
your age is and in this case 22 so if
392
00:13:22,639 --> 00:13:24,800
you need to display a variable of a
393
00:13:24,800 --> 00:13:26,000
different data type
394
00:13:26,000 --> 00:13:27,760
along with the string you would just
395
00:13:27,760 --> 00:13:29,920
need to use a stringcast to convert that
396
00:13:29,920 --> 00:13:31,360
data type to a string
397
00:13:31,360 --> 00:13:32,800
but we'll cover this more in the next
398
00:13:32,800 --> 00:13:34,560
video on typecasting
399
00:13:34,560 --> 00:13:37,839
so that is the int data type it stores a
400
00:13:37,839 --> 00:13:40,240
whole integer number and the next data
401
00:13:40,240 --> 00:13:41,040
type is the
402
00:13:41,040 --> 00:13:43,120
float data type and i'm going to turn
403
00:13:43,120 --> 00:13:44,880
these lines into comments and we can
404
00:13:44,880 --> 00:13:45,440
move on
405
00:13:45,440 --> 00:13:47,680
now the float data type is a numeric
406
00:13:47,680 --> 00:13:48,959
value that can store
407
00:13:48,959 --> 00:13:52,000
a number that includes a decimal portion
408
00:13:52,000 --> 00:13:54,720
an int data type cannot store a decimal
409
00:13:54,720 --> 00:13:56,240
portion that's why it's in
410
00:13:56,240 --> 00:13:58,160
short for integer it only stores a whole
411
00:13:58,160 --> 00:14:00,399
number this time let's say we have a
412
00:14:00,399 --> 00:14:00,959
height
413
00:14:00,959 --> 00:14:02,959
and i will assign this a perfectly
414
00:14:02,959 --> 00:14:06,240
normal height of maybe 250.5
415
00:14:06,240 --> 00:14:08,800
centimeters so this decimal portion is
416
00:14:08,800 --> 00:14:09,519
important
417
00:14:09,519 --> 00:14:11,519
that's indicating that this is a
418
00:14:11,519 --> 00:14:13,920
floating point number a float for short
419
00:14:13,920 --> 00:14:15,920
so let's print our height to the console
420
00:14:15,920 --> 00:14:17,360
window print
421
00:14:17,360 --> 00:14:19,360
height and this will print whatever my
422
00:14:19,360 --> 00:14:20,720
height is and now
423
00:14:20,720 --> 00:14:23,440
let's check the type of our height using
424
00:14:23,440 --> 00:14:25,040
the type function
425
00:14:25,040 --> 00:14:27,199
so within a print statement i'm going to
426
00:14:27,199 --> 00:14:28,480
add my height
427
00:14:28,480 --> 00:14:30,000
surround this with another set of
428
00:14:30,000 --> 00:14:32,240
parentheses and precede this
429
00:14:32,240 --> 00:14:35,760
with the type function and the data type
430
00:14:35,760 --> 00:14:38,399
of my variable height is a float short
431
00:14:38,399 --> 00:14:39,680
for a floating point number
432
00:14:39,680 --> 00:14:42,000
a numeric value that contains a decimal
433
00:14:42,000 --> 00:14:42,720
portion
434
00:14:42,720 --> 00:14:44,639
and now for practice let's print our
435
00:14:44,639 --> 00:14:46,240
height along with a string
436
00:14:46,240 --> 00:14:48,880
literal so let's create a string literal
437
00:14:48,880 --> 00:14:50,160
such as
438
00:14:50,160 --> 00:14:53,279
your height is
439
00:14:53,279 --> 00:14:55,279
plus height but guess what we're going
440
00:14:55,279 --> 00:14:56,880
to do some type casting
441
00:14:56,880 --> 00:14:58,399
so surround height with a set of
442
00:14:58,399 --> 00:15:00,800
parentheses and precede this with
443
00:15:00,800 --> 00:15:03,120
str so that we convert height to a
444
00:15:03,120 --> 00:15:04,399
string so that we can use
445
00:15:04,399 --> 00:15:06,079
string concatenation within this print
446
00:15:06,079 --> 00:15:07,839
statement and this will print the
447
00:15:07,839 --> 00:15:08,720
message
448
00:15:08,720 --> 00:15:12,240
your height is 250.5 actually let's add
449
00:15:12,240 --> 00:15:14,000
centimeters to the end so
450
00:15:14,000 --> 00:15:17,279
plus cm
451
00:15:17,279 --> 00:15:19,519
and there we go and that is what a
452
00:15:19,519 --> 00:15:20,720
floating point number is
453
00:15:20,720 --> 00:15:22,880
float for short it is a numeric value
454
00:15:22,880 --> 00:15:25,040
that contains a decimal portion
455
00:15:25,040 --> 00:15:26,560
now the last data type that we're going
456
00:15:26,560 --> 00:15:28,399
to cover is the boolean
457
00:15:28,399 --> 00:15:30,720
data type it is a variable that can only
458
00:15:30,720 --> 00:15:31,839
store true
459
00:15:31,839 --> 00:15:34,560
or false let's say we have a variable
460
00:15:34,560 --> 00:15:35,680
called human
461
00:15:35,680 --> 00:15:37,680
if we were to assign this a boolean
462
00:15:37,680 --> 00:15:40,639
value this would be either true or
463
00:15:40,639 --> 00:15:42,720
false and we can print whatever this
464
00:15:42,720 --> 00:15:44,720
value is let's print whatever
465
00:15:44,720 --> 00:15:47,839
human is and this will display false now
466
00:15:47,839 --> 00:15:49,600
the reason that you may want to use
467
00:15:49,600 --> 00:15:51,600
booleans instead of strings even though
468
00:15:51,600 --> 00:15:52,959
you can still store
469
00:15:52,959 --> 00:15:54,800
a string representation of the word
470
00:15:54,800 --> 00:15:56,079
false or true
471
00:15:56,079 --> 00:15:58,399
is that these are very useful when we
472
00:15:58,399 --> 00:15:59,600
get to if statements
473
00:15:59,600 --> 00:16:01,839
we can check to see if some statement is
474
00:16:01,839 --> 00:16:04,320
true so that is a good use of
475
00:16:04,320 --> 00:16:07,120
boolean values now let's check the data
476
00:16:07,120 --> 00:16:08,320
type of
477
00:16:08,320 --> 00:16:11,680
my human variable so let's print
478
00:16:11,680 --> 00:16:15,120
type human and this will print to the
479
00:16:15,120 --> 00:16:16,320
console window
480
00:16:16,320 --> 00:16:18,399
bool short for boolean now make sure
481
00:16:18,399 --> 00:16:20,399
you're not putting this within quotes
482
00:16:20,399 --> 00:16:22,320
because then it's technically a string
483
00:16:22,320 --> 00:16:23,839
and they behave differently
484
00:16:23,839 --> 00:16:26,000
now the data type is a string and you
485
00:16:26,000 --> 00:16:27,759
may have noticed as well that the font
486
00:16:27,759 --> 00:16:29,440
color has changed between
487
00:16:29,440 --> 00:16:31,680
strings and boolean values so that's
488
00:16:31,680 --> 00:16:32,959
another good indicator
489
00:16:32,959 --> 00:16:35,279
now what if you need to display your
490
00:16:35,279 --> 00:16:37,440
value along with a string using string
491
00:16:37,440 --> 00:16:38,320
concatenation
492
00:16:38,320 --> 00:16:39,920
well guess what we're going to typecast
493
00:16:39,920 --> 00:16:43,759
again so let's print something
494
00:16:44,240 --> 00:16:48,639
r u a human
495
00:16:48,639 --> 00:16:50,880
so we need to use string concatenation
496
00:16:50,880 --> 00:16:52,560
so i will convert
497
00:16:52,560 --> 00:16:55,920
my boolean variable of human to a string
498
00:16:55,920 --> 00:16:58,639
and i have been programmed to inform you
499
00:16:58,639 --> 00:16:59,279
that
500
00:16:59,279 --> 00:17:01,920
yes i am in fact a human that is true
501
00:17:01,920 --> 00:17:02,800
well everyone
502
00:17:02,800 --> 00:17:05,199
that is the basics of variables it is a
503
00:17:05,199 --> 00:17:07,439
container for a value and they behave
504
00:17:07,439 --> 00:17:09,760
as the value that they contain there are
505
00:17:09,760 --> 00:17:11,599
four basic data types
506
00:17:11,599 --> 00:17:13,359
strings which store a series of
507
00:17:13,359 --> 00:17:14,880
characters inc
508
00:17:14,880 --> 00:17:18,000
which store a whole integer floats which
509
00:17:18,000 --> 00:17:20,079
are floating point numbers a numeric
510
00:17:20,079 --> 00:17:21,839
value with a decimal
511
00:17:21,839 --> 00:17:24,480
and booleans which only store true or
512
00:17:24,480 --> 00:17:25,119
false
513
00:17:25,119 --> 00:17:27,039
and they're very useful with if
514
00:17:27,039 --> 00:17:29,600
statements so that is it for variables
515
00:17:29,600 --> 00:17:31,280
if you would like a copy of all this
516
00:17:31,280 --> 00:17:33,200
code i will copy and paste all of this
517
00:17:33,200 --> 00:17:35,039
to the comments section down below
518
00:17:35,039 --> 00:17:37,600
but yeah that is how variables work in
519
00:17:37,600 --> 00:17:38,640
python
520
00:17:38,640 --> 00:17:41,039
hey people what's going on it's bro hope
521
00:17:41,039 --> 00:17:41,840
you're doing well
522
00:17:41,840 --> 00:17:43,520
and in this video i'm going to explain
523
00:17:43,520 --> 00:17:45,679
multiple assignment in python so
524
00:17:45,679 --> 00:17:50,400
sit back relax and enjoy the show
525
00:17:50,480 --> 00:17:52,080
well then since you clicked on this
526
00:17:52,080 --> 00:17:53,919
video i should probably explain what
527
00:17:53,919 --> 00:17:55,120
multiple assignment is
528
00:17:55,120 --> 00:17:57,120
now multiple assignment allows us to
529
00:17:57,120 --> 00:17:59,360
assign multiple variables
530
00:17:59,360 --> 00:18:02,320
at the same time using one line of code
531
00:18:02,320 --> 00:18:03,600
here's an example of us
532
00:18:03,600 --> 00:18:05,679
using standard assignment let's say we
533
00:18:05,679 --> 00:18:06,960
have a variable
534
00:18:06,960 --> 00:18:09,679
name and i will set this to a value of
535
00:18:09,679 --> 00:18:11,120
my name but you can use your name for
536
00:18:11,120 --> 00:18:12,240
this example
537
00:18:12,240 --> 00:18:15,440
let's say edge equals 21 and how about a
538
00:18:15,440 --> 00:18:16,880
variable called
539
00:18:16,880 --> 00:18:19,679
attractive i think i'm going to set this
540
00:18:19,679 --> 00:18:20,480
to true
541
00:18:20,480 --> 00:18:22,559
okay so we have a bunch of variables and
542
00:18:22,559 --> 00:18:23,520
then we can print
543
00:18:23,520 --> 00:18:25,600
the value of these variables with some
544
00:18:25,600 --> 00:18:26,960
print statements
545
00:18:26,960 --> 00:18:30,880
so let's print name age and attractive
546
00:18:30,880 --> 00:18:34,880
so we have name age attractive
547
00:18:34,880 --> 00:18:37,280
and as you would expect this prints bro
548
00:18:37,280 --> 00:18:38,799
21 and true
549
00:18:38,799 --> 00:18:40,799
now another way in which we could write
550
00:18:40,799 --> 00:18:42,320
the same code is to use
551
00:18:42,320 --> 00:18:44,400
multiple assignment and this allows us
552
00:18:44,400 --> 00:18:46,320
to assign multiple variables
553
00:18:46,320 --> 00:18:48,799
at the same time using one line of code
554
00:18:48,799 --> 00:18:50,080
so i'm going to turn
555
00:18:50,080 --> 00:18:52,720
all of these lines into comments and
556
00:18:52,720 --> 00:18:53,360
this time
557
00:18:53,360 --> 00:18:55,679
we will only use one line of code but to
558
00:18:55,679 --> 00:18:57,440
do this we're going to list all of our
559
00:18:57,440 --> 00:18:58,320
variables
560
00:18:58,320 --> 00:19:00,960
separated with a comma so that would be
561
00:19:00,960 --> 00:19:01,679
name
562
00:19:01,679 --> 00:19:05,600
comma edge comma attractive
563
00:19:05,600 --> 00:19:08,160
and we will set them equal to those
564
00:19:08,160 --> 00:19:08,880
values
565
00:19:08,880 --> 00:19:10,880
but in the same order separated by
566
00:19:10,880 --> 00:19:12,240
commas so that would be
567
00:19:12,240 --> 00:19:15,840
bro comma 21 comma true
568
00:19:15,840 --> 00:19:17,600
and this would do the same thing only
569
00:19:17,600 --> 00:19:19,440
using one line of code
570
00:19:19,440 --> 00:19:21,600
here's another example let's say we have
571
00:19:21,600 --> 00:19:22,480
four variables
572
00:19:22,480 --> 00:19:25,280
spongebob patrick sandy and squidward
573
00:19:25,280 --> 00:19:25,600
and
574
00:19:25,600 --> 00:19:27,440
let's pretend that they're all around
575
00:19:27,440 --> 00:19:28,799
maybe 30 years old
576
00:19:28,799 --> 00:19:30,559
at least it seems like they're around 30
577
00:19:30,559 --> 00:19:31,919
years old but i could be wrong
578
00:19:31,919 --> 00:19:34,240
i'm not too familiar with spongebob lore
579
00:19:34,240 --> 00:19:36,880
so let's set them all equal to 30.
580
00:19:36,880 --> 00:19:39,039
now we could write this using four lines
581
00:19:39,039 --> 00:19:40,080
of code or
582
00:19:40,080 --> 00:19:42,160
we could use multiple assignment to
583
00:19:42,160 --> 00:19:43,200
write the same thing
584
00:19:43,200 --> 00:19:45,919
using one line of code so if all of
585
00:19:45,919 --> 00:19:46,880
these variables
586
00:19:46,880 --> 00:19:49,200
are all equal to the same value there's
587
00:19:49,200 --> 00:19:51,039
a little bit of different syntax here
588
00:19:51,039 --> 00:19:53,360
so let's begin with the first variable
589
00:19:53,360 --> 00:19:54,799
of spongebob
590
00:19:54,799 --> 00:19:58,720
equals patrick
591
00:19:59,280 --> 00:20:02,559
equals sandy equals
592
00:20:02,559 --> 00:20:06,159
squidward then we're going to set them
593
00:20:06,159 --> 00:20:09,200
all equal to 30 and
594
00:20:09,200 --> 00:20:11,280
this should work just fine using only
595
00:20:11,280 --> 00:20:12,400
one line of code
596
00:20:12,400 --> 00:20:14,080
well everybody that's pretty much it for
597
00:20:14,080 --> 00:20:15,760
multiple assignment it allows us to
598
00:20:15,760 --> 00:20:16,240
assign
599
00:20:16,240 --> 00:20:18,559
multiple variables at the same time
600
00:20:18,559 --> 00:20:20,240
using one line of code
601
00:20:20,240 --> 00:20:22,000
i will post all of this code in the
602
00:20:22,000 --> 00:20:23,600
comments section down below
603
00:20:23,600 --> 00:20:25,360
and well yeah that's how multiple
604
00:20:25,360 --> 00:20:27,200
assignment works in python
605
00:20:27,200 --> 00:20:29,520
hey what's going on everybody it's bro
606
00:20:29,520 --> 00:20:30,480
hope you're doing well
607
00:20:30,480 --> 00:20:31,600
and in this video i'm going to
608
00:20:31,600 --> 00:20:33,760
demonstrate some useful string methods
609
00:20:33,760 --> 00:20:34,799
in python so
610
00:20:34,799 --> 00:20:39,520
sit back relax and enjoy the show
611
00:20:39,679 --> 00:20:42,400
all right let's do this in this video
612
00:20:42,400 --> 00:20:43,840
i'm going to be explaining and
613
00:20:43,840 --> 00:20:44,640
demonstrating
614
00:20:44,640 --> 00:20:47,039
a few useful methods available to us for
615
00:20:47,039 --> 00:20:47,679
strings
616
00:20:47,679 --> 00:20:49,919
now let's create a variable called name
617
00:20:49,919 --> 00:20:51,520
for this example and you can use your
618
00:20:51,520 --> 00:20:52,559
own name if you want
619
00:20:52,559 --> 00:20:54,960
so we now have a variable called name
620
00:20:54,960 --> 00:20:56,960
and for my first example
621
00:20:56,960 --> 00:20:59,360
let's print the length of our name
622
00:20:59,360 --> 00:21:00,159
variable
623
00:21:00,159 --> 00:21:03,280
so to do that we can surround our name
624
00:21:03,280 --> 00:21:05,919
variable with the length method and this
625
00:21:05,919 --> 00:21:06,640
will print
626
00:21:06,640 --> 00:21:09,280
and return how long the length of our
627
00:21:09,280 --> 00:21:09,919
string is
628
00:21:09,919 --> 00:21:11,840
and for me the length of my name
629
00:21:11,840 --> 00:21:13,200
variable is three
630
00:21:13,200 --> 00:21:15,360
for three characters if i were to
631
00:21:15,360 --> 00:21:17,280
increase the size of my name
632
00:21:17,280 --> 00:21:19,919
well then that's going to change to a so
633
00:21:19,919 --> 00:21:20,480
that is
634
00:21:20,480 --> 00:21:22,320
the length method and i'm just going to
635
00:21:22,320 --> 00:21:24,159
turn this line into a comment
636
00:21:24,159 --> 00:21:26,799
and let's move on so next we have the
637
00:21:26,799 --> 00:21:27,760
find method
638
00:21:27,760 --> 00:21:30,080
i just copy and pasted print name so i
639
00:21:30,080 --> 00:21:31,200
can reuse it
640
00:21:31,200 --> 00:21:33,679
name dot and actually if you have
641
00:21:33,679 --> 00:21:34,799
intellisense setup
642
00:21:34,799 --> 00:21:37,360
if you add dot after your variable
643
00:21:37,360 --> 00:21:39,280
you'll get a suggestion or a few
644
00:21:39,280 --> 00:21:40,400
suggestions of
645
00:21:40,400 --> 00:21:42,400
different methods and other features
646
00:21:42,400 --> 00:21:44,240
that you have access to so
647
00:21:44,240 --> 00:21:48,400
let's find find which is
648
00:21:48,400 --> 00:21:51,760
uh screw this i'll just type find find
649
00:21:51,760 --> 00:21:54,240
then we can find the first index of
650
00:21:54,240 --> 00:21:55,600
where this character is
651
00:21:55,600 --> 00:21:57,360
let's say i would like to find where
652
00:21:57,360 --> 00:21:58,799
capital b is
653
00:21:58,799 --> 00:22:01,120
well that is at index zero because
654
00:22:01,120 --> 00:22:03,120
computers always start with zero
655
00:22:03,120 --> 00:22:05,360
so this first character would be zero
656
00:22:05,360 --> 00:22:06,480
then r
657
00:22:06,480 --> 00:22:10,080
would be one and then o would be
658
00:22:10,080 --> 00:22:12,240
two for this example all right so that
659
00:22:12,240 --> 00:22:14,080
is how to find a character
660
00:22:14,080 --> 00:22:17,360
within a string moving on so let's
661
00:22:17,360 --> 00:22:19,919
capitalize our name and i should
662
00:22:19,919 --> 00:22:23,200
probably make this lowercase
663
00:22:23,200 --> 00:22:26,720
so name dot capitalize
664
00:22:26,720 --> 00:22:29,520
and this will make my name capitalized
665
00:22:29,520 --> 00:22:31,039
but if there's any spaces
666
00:22:31,039 --> 00:22:33,200
and then additional words it's not going
667
00:22:33,200 --> 00:22:34,240
to capitalize that
668
00:22:34,240 --> 00:22:37,360
only the first letter and your string
669
00:22:37,360 --> 00:22:40,400
next we have upper this will make your
670
00:22:40,400 --> 00:22:42,000
string all uppercase
671
00:22:42,000 --> 00:22:45,280
name upper and now
672
00:22:45,280 --> 00:22:49,039
it is uppercase guess what's next
673
00:22:49,039 --> 00:22:52,960
lower name dot lower
674
00:22:52,960 --> 00:22:54,799
and i should probably make this capital
675
00:22:54,799 --> 00:22:57,120
again
676
00:22:57,280 --> 00:23:00,559
and everything is lowercase
677
00:23:00,559 --> 00:23:02,799
we're making some good progress today
678
00:23:02,799 --> 00:23:04,000
next we have
679
00:23:04,000 --> 00:23:07,200
is digit this will return true or false
680
00:23:07,200 --> 00:23:11,200
depending on if our string is a digit
681
00:23:11,200 --> 00:23:14,000
which it's not so this will print false
682
00:23:14,000 --> 00:23:15,840
if i changed my string to a bunch of
683
00:23:15,840 --> 00:23:17,039
numbers like 1 2
684
00:23:17,039 --> 00:23:19,919
3 then this will return true let's
685
00:23:19,919 --> 00:23:20,960
change that back
686
00:23:20,960 --> 00:23:25,200
and move on next we have
687
00:23:25,679 --> 00:23:28,480
is alpha
688
00:23:28,799 --> 00:23:32,480
are these alphabetical characters
689
00:23:33,280 --> 00:23:35,120
oh make sure you add the parentheses
690
00:23:35,120 --> 00:23:37,520
afterwards my bad
691
00:23:37,520 --> 00:23:41,039
false because we added a space within
692
00:23:41,039 --> 00:23:42,559
here
693
00:23:42,559 --> 00:23:46,080
now it's true so you can use is alpha to
694
00:23:46,080 --> 00:23:46,799
check to see
695
00:23:46,799 --> 00:23:49,200
if your string contains only letters
696
00:23:49,200 --> 00:23:51,919
alphabetical letters
697
00:23:51,919 --> 00:23:55,440
next we have count
698
00:23:55,440 --> 00:23:57,760
we can count how many characters are
699
00:23:57,760 --> 00:23:58,880
within our string
700
00:23:58,880 --> 00:24:00,480
i would like to count how many o's are
701
00:24:00,480 --> 00:24:02,640
in here and this should return
702
00:24:02,640 --> 00:24:06,320
two that's right okay we're almost at
703
00:24:06,320 --> 00:24:07,039
the end
704
00:24:07,039 --> 00:24:09,679
we can replace characters within our
705
00:24:09,679 --> 00:24:10,640
string
706
00:24:10,640 --> 00:24:13,919
so we use the replace method
707
00:24:13,919 --> 00:24:16,799
and we pass in two arguments the
708
00:24:16,799 --> 00:24:18,000
character we would like to
709
00:24:18,000 --> 00:24:20,159
replace and the character we would like
710
00:24:20,159 --> 00:24:21,840
to replace our character with
711
00:24:21,840 --> 00:24:24,559
i would like to change all o's to a's
712
00:24:24,559 --> 00:24:25,120
and now
713
00:24:25,120 --> 00:24:28,080
my name is brock hod oh you guys are
714
00:24:28,080 --> 00:24:29,279
gonna like this next one
715
00:24:29,279 --> 00:24:31,760
this isn't technically a method but it's
716
00:24:31,760 --> 00:24:33,279
a really neat feature
717
00:24:33,279 --> 00:24:34,720
so one thing that we can do that's
718
00:24:34,720 --> 00:24:36,640
available to us with python is that we
719
00:24:36,640 --> 00:24:38,159
can display a string
720
00:24:38,159 --> 00:24:40,559
multiple times by multiplying that
721
00:24:40,559 --> 00:24:42,320
string by a given number
722
00:24:42,320 --> 00:24:44,159
so let's say i would like to display my
723
00:24:44,159 --> 00:24:46,080
name three times i can just take my
724
00:24:46,080 --> 00:24:47,039
string variable
725
00:24:47,039 --> 00:24:49,600
then add times three and this will print
726
00:24:49,600 --> 00:24:50,240
my name
727
00:24:50,240 --> 00:24:53,440
three times bro bro bro like i said not
728
00:24:53,440 --> 00:24:54,159
technically
729
00:24:54,159 --> 00:24:56,240
a method but it's a useful feature that
730
00:24:56,240 --> 00:24:57,600
you might be interested in that's
731
00:24:57,600 --> 00:24:58,400
related
732
00:24:58,400 --> 00:25:01,039
all right everybody so that is a few
733
00:25:01,039 --> 00:25:02,960
useful methods available to us for
734
00:25:02,960 --> 00:25:04,960
strings if you would like a copy of all
735
00:25:04,960 --> 00:25:05,679
this code
736
00:25:05,679 --> 00:25:07,600
i will post this in the comments section
737
00:25:07,600 --> 00:25:09,760
down below but yeah those are
738
00:25:09,760 --> 00:25:12,400
a few useful methods available to us for
739
00:25:12,400 --> 00:25:13,039
strings
740
00:25:13,039 --> 00:25:14,880
hey what's going on everybody it's you
741
00:25:14,880 --> 00:25:16,640
bro hope you're doing well and in this
742
00:25:16,640 --> 00:25:18,000
video i'm going to teach you guys all
743
00:25:18,000 --> 00:25:19,919
about typecasting in python so
744
00:25:19,919 --> 00:25:24,240
sit back relax and enjoy the show
745
00:25:24,400 --> 00:25:26,480
all right welcome back everybody i have
746
00:25:26,480 --> 00:25:28,320
a super quick video for you guys today
747
00:25:28,320 --> 00:25:30,159
on the subject of typecasting now
748
00:25:30,159 --> 00:25:32,240
typecasting is the ability to convert
749
00:25:32,240 --> 00:25:35,200
the data type of a value to another data
750
00:25:35,200 --> 00:25:35,679
type
751
00:25:35,679 --> 00:25:36,880
here's an example that i've written
752
00:25:36,880 --> 00:25:38,720
already i'm just going to paste it
753
00:25:38,720 --> 00:25:41,440
i have three variables for this example
754
00:25:41,440 --> 00:25:42,320
x y
755
00:25:42,320 --> 00:25:45,440
and z x contains one this is a whole
756
00:25:45,440 --> 00:25:46,080
integer
757
00:25:46,080 --> 00:25:48,880
into for short y contains two point zero
758
00:25:48,880 --> 00:25:50,720
this is a floating point number
759
00:25:50,720 --> 00:25:52,400
float four short you can tell it's a
760
00:25:52,400 --> 00:25:53,840
float because it ends with a decimal
761
00:25:53,840 --> 00:25:54,559
portion
762
00:25:54,559 --> 00:25:57,520
and z contains a string of three a
763
00:25:57,520 --> 00:25:58,000
string
764
00:25:58,000 --> 00:26:00,320
is a series of characters and you can
765
00:26:00,320 --> 00:26:01,440
tell it's a string because it's
766
00:26:01,440 --> 00:26:03,440
surrounded with a set of quotes
767
00:26:03,440 --> 00:26:05,279
and remember with strings we cannot
768
00:26:05,279 --> 00:26:08,000
normally perform math on strings
769
00:26:08,000 --> 00:26:11,120
so i have three print statements for x y
770
00:26:11,120 --> 00:26:13,200
and z and as you would expect they
771
00:26:13,200 --> 00:26:14,400
contain the values of
772
00:26:14,400 --> 00:26:17,120
one two point zero and three now let's
773
00:26:17,120 --> 00:26:18,960
begin by converting both y
774
00:26:18,960 --> 00:26:21,520
and z to the integer data type via
775
00:26:21,520 --> 00:26:23,279
typecasting in order to perform
776
00:26:23,279 --> 00:26:24,080
typecasting
777
00:26:24,080 --> 00:26:27,039
on a value or variable type that value
778
00:26:27,039 --> 00:26:28,960
or variable and surround this with a set
779
00:26:28,960 --> 00:26:32,000
of parentheses whoops i missed and then
780
00:26:32,000 --> 00:26:32,880
pre-save this
781
00:26:32,880 --> 00:26:34,720
with the data type you'd like to convert
782
00:26:34,720 --> 00:26:36,640
this value or variable to
783
00:26:36,640 --> 00:26:39,360
i would like to convert y to an integer
784
00:26:39,360 --> 00:26:40,960
and then you can use this for whatever
785
00:26:40,960 --> 00:26:41,520
you need
786
00:26:41,520 --> 00:26:43,679
so i'm going to replace y within my
787
00:26:43,679 --> 00:26:44,799
print statement
788
00:26:44,799 --> 00:26:47,840
with y after we typecast it as
789
00:26:47,840 --> 00:26:50,960
an integer and this will display 1 2
790
00:26:50,960 --> 00:26:54,240
not 2.0 it's the whole integer
791
00:26:54,240 --> 00:26:57,919
of 2 and then 3. so this is not a
792
00:26:57,919 --> 00:26:58,880
permanent change
793
00:26:58,880 --> 00:27:00,960
if you need to make this as a permanent
794
00:27:00,960 --> 00:27:02,320
change to y
795
00:27:02,320 --> 00:27:05,600
you'd have to reassign it so let's say y
796
00:27:05,600 --> 00:27:07,600
equals y and then we're going to
797
00:27:07,600 --> 00:27:09,120
typecast y
798
00:27:09,120 --> 00:27:10,559
so let's round this with a set of
799
00:27:10,559 --> 00:27:12,400
parentheses and precede this with the
800
00:27:12,400 --> 00:27:14,000
data type we'd like to convert
801
00:27:14,000 --> 00:27:17,120
y2 so now y is a whole
802
00:27:17,120 --> 00:27:20,480
integer and then we will print y without
803
00:27:20,480 --> 00:27:23,600
typecast this time and our result is
804
00:27:23,600 --> 00:27:27,600
one two three now z z is storing a
805
00:27:27,600 --> 00:27:29,440
string and with strings we cannot
806
00:27:29,440 --> 00:27:31,760
normally perform any math on strings
807
00:27:31,760 --> 00:27:33,679
for example within my print statement i
808
00:27:33,679 --> 00:27:35,360
will state z
809
00:27:35,360 --> 00:27:37,360
times three you would think that the
810
00:27:37,360 --> 00:27:39,360
result would be nine right
811
00:27:39,360 --> 00:27:41,279
well you're wrong it's going to print
812
00:27:41,279 --> 00:27:42,880
three three times
813
00:27:42,880 --> 00:27:46,320
if you need to convert a string to a int
814
00:27:46,320 --> 00:27:47,919
or a float you would just have to
815
00:27:47,919 --> 00:27:50,640
typecast it so this time let's type cast
816
00:27:50,640 --> 00:27:53,679
z as an integer z
817
00:27:53,679 --> 00:27:56,799
int and we will type cast z as an
818
00:27:56,799 --> 00:28:00,240
integer so then if we have z times three
819
00:28:00,240 --> 00:28:04,159
the result is now 9 instead of 333
820
00:28:04,159 --> 00:28:06,559
so that's the basic formula for type
821
00:28:06,559 --> 00:28:08,720
casting now let's convert these to
822
00:28:08,720 --> 00:28:11,120
floating point numbers but let's include
823
00:28:11,120 --> 00:28:12,159
x as well because
824
00:28:12,159 --> 00:28:14,960
x is feeling left out so let's convert x
825
00:28:14,960 --> 00:28:15,360
to
826
00:28:15,360 --> 00:28:17,840
a floating point number so float
827
00:28:17,840 --> 00:28:19,520
parenthesis and we will surround
828
00:28:19,520 --> 00:28:22,960
this with x and display x
829
00:28:22,960 --> 00:28:26,159
and actually let's convert y and z to
830
00:28:26,159 --> 00:28:29,600
floats as well because why not
831
00:28:29,600 --> 00:28:33,520
the results are now 1.0 2.0 and 9.0
832
00:28:33,520 --> 00:28:35,840
remember that we multiplied z times
833
00:28:35,840 --> 00:28:36,559
three
834
00:28:36,559 --> 00:28:38,799
and then if we need to convert these
835
00:28:38,799 --> 00:28:41,039
values to a string
836
00:28:41,039 --> 00:28:45,840
we would use the string cast
837
00:28:46,240 --> 00:28:49,520
so the results now are 1 2.0
838
00:28:49,520 --> 00:28:52,960
and 333 now you might be thinking to
839
00:28:52,960 --> 00:28:53,520
yourself
840
00:28:53,520 --> 00:28:55,200
when would you need to convert an
841
00:28:55,200 --> 00:28:57,600
integer or a float to a string
842
00:28:57,600 --> 00:28:59,919
here's a situation let's say within our
843
00:28:59,919 --> 00:29:02,159
print statements we need to print
844
00:29:02,159 --> 00:29:04,080
these variables along with another
845
00:29:04,080 --> 00:29:06,080
string so within our print statement
846
00:29:06,080 --> 00:29:09,279
let's say x is plus
847
00:29:09,279 --> 00:29:11,360
x we would like to display these
848
00:29:11,360 --> 00:29:12,320
together
849
00:29:12,320 --> 00:29:14,000
and let me copy this and do the same
850
00:29:14,000 --> 00:29:15,360
thing for y
851
00:29:15,360 --> 00:29:18,640
we'll say y is plus y
852
00:29:18,640 --> 00:29:21,360
you cannot normally display a string
853
00:29:21,360 --> 00:29:22,799
along with an integer
854
00:29:22,799 --> 00:29:24,880
or a float because we're using some
855
00:29:24,880 --> 00:29:26,320
string concatenation
856
00:29:26,320 --> 00:29:28,159
so this will result in a compilation
857
00:29:28,159 --> 00:29:30,480
error can only concatenate
858
00:29:30,480 --> 00:29:33,760
string not int to string so that's a
859
00:29:33,760 --> 00:29:34,559
situation
860
00:29:34,559 --> 00:29:36,080
in which you might need to use
861
00:29:36,080 --> 00:29:38,080
typecasting to convert an
862
00:29:38,080 --> 00:29:40,960
integer or float to a string so let's
863
00:29:40,960 --> 00:29:41,600
display
864
00:29:41,600 --> 00:29:45,200
x and y as strings via typecasting
865
00:29:45,200 --> 00:29:47,520
so we'll surround x and y with a set of
866
00:29:47,520 --> 00:29:49,279
parentheses and precede this with the
867
00:29:49,279 --> 00:29:50,960
data type we'd like to convert these
868
00:29:50,960 --> 00:29:51,840
values to
869
00:29:51,840 --> 00:29:53,840
we would like to convert x and y to both
870
00:29:53,840 --> 00:29:55,840
strings and now this will compile and
871
00:29:55,840 --> 00:29:56,159
run
872
00:29:56,159 --> 00:29:58,159
just fine well everybody that's the
873
00:29:58,159 --> 00:30:00,159
basics of typecasting
874
00:30:00,159 --> 00:30:02,000
if you would like a copy of all my code
875
00:30:02,000 --> 00:30:03,679
i will post everything in the comments
876
00:30:03,679 --> 00:30:04,559
down below
877
00:30:04,559 --> 00:30:06,399
if you found this video helpful don't be
878
00:30:06,399 --> 00:30:08,320
afraid to help me out by smashing that
879
00:30:08,320 --> 00:30:09,039
like button
880
00:30:09,039 --> 00:30:11,440
drop a comment down below and subscribe
881
00:30:11,440 --> 00:30:14,320
if you'd like to become a fellow bro
882
00:30:14,320 --> 00:30:16,640
hey what's going on everybody it's bro
883
00:30:16,640 --> 00:30:17,600
hope you're doing well
884
00:30:17,600 --> 00:30:18,960
and in this video i'm going to teach you
885
00:30:18,960 --> 00:30:20,720
guys how we can accept some user input
886
00:30:20,720 --> 00:30:22,000
in python so sit
887
00:30:22,000 --> 00:30:26,240
back relax and enjoy the show
888
00:30:26,240 --> 00:30:27,919
ladies and gentlemen this is where the
889
00:30:27,919 --> 00:30:30,000
fun begins in this video i'm going to
890
00:30:30,000 --> 00:30:31,679
explain how we can accept some user
891
00:30:31,679 --> 00:30:32,720
input in python
892
00:30:32,720 --> 00:30:35,360
and we do so by using the input function
893
00:30:35,360 --> 00:30:37,279
and then we type in our input to the
894
00:30:37,279 --> 00:30:38,320
console window
895
00:30:38,320 --> 00:30:40,080
but it would probably be good practice
896
00:30:40,080 --> 00:30:41,840
to let the user know what we want them
897
00:30:41,840 --> 00:30:43,200
to type in exactly
898
00:30:43,200 --> 00:30:45,520
so let's ask for somebody's name within
899
00:30:45,520 --> 00:30:47,440
quotes inside the input function i'm
900
00:30:47,440 --> 00:30:48,960
going to type
901
00:30:48,960 --> 00:30:52,320
what is your name and now
902
00:30:52,320 --> 00:30:54,799
our program will wait for us to enter in
903
00:30:54,799 --> 00:30:55,760
some user input
904
00:30:55,760 --> 00:30:57,200
so you can use your own name for this
905
00:30:57,200 --> 00:30:58,880
example i'm going to type bro
906
00:30:58,880 --> 00:31:00,960
now to submit some user input you just
907
00:31:00,960 --> 00:31:02,240
type or press
908
00:31:02,240 --> 00:31:04,880
enter now you can see that my process
909
00:31:04,880 --> 00:31:06,640
finished with exit code zero that means
910
00:31:06,640 --> 00:31:08,080
the program is done running
911
00:31:08,080 --> 00:31:09,519
so we should probably do something with
912
00:31:09,519 --> 00:31:11,519
this value we can actually assign it to
913
00:31:11,519 --> 00:31:13,360
a variable so we can use it elsewhere in
914
00:31:13,360 --> 00:31:14,159
our program
915
00:31:14,159 --> 00:31:17,360
so to do so we precede input with
916
00:31:17,360 --> 00:31:20,000
a variable like name works name equals
917
00:31:20,000 --> 00:31:20,559
input
918
00:31:20,559 --> 00:31:22,799
and when we accept some user input we
919
00:31:22,799 --> 00:31:24,559
will assign it to this variable that we
920
00:31:24,559 --> 00:31:25,200
called them
921
00:31:25,200 --> 00:31:27,279
and now we can do something with it so
922
00:31:27,279 --> 00:31:28,960
let's print a message
923
00:31:28,960 --> 00:31:31,120
involving our name variable that we have
924
00:31:31,120 --> 00:31:32,240
so how about
925
00:31:32,240 --> 00:31:35,360
hello plus name
926
00:31:35,360 --> 00:31:37,039
now when i run this program we can
927
00:31:37,039 --> 00:31:38,559
accept some user input
928
00:31:38,559 --> 00:31:40,320
and then do something with that input
929
00:31:40,320 --> 00:31:42,480
such as display message that says
930
00:31:42,480 --> 00:31:44,399
hello bro and then you can type in
931
00:31:44,399 --> 00:31:45,760
whatever you want for your name
932
00:31:45,760 --> 00:31:48,000
let's say i'm dude what is your name
933
00:31:48,000 --> 00:31:49,360
dude hello dude
934
00:31:49,360 --> 00:31:51,600
now this next part is very important
935
00:31:51,600 --> 00:31:54,159
when we accept user input it is always
936
00:31:54,159 --> 00:31:55,039
of the string
937
00:31:55,039 --> 00:31:58,240
data type a series of characters what if
938
00:31:58,240 --> 00:32:00,000
we need to accept a number
939
00:32:00,000 --> 00:32:01,919
well you can but you can't normally
940
00:32:01,919 --> 00:32:04,080
perform any math on it because it's of
941
00:32:04,080 --> 00:32:05,360
the string data type
942
00:32:05,360 --> 00:32:07,519
we would need to cast it as either of
943
00:32:07,519 --> 00:32:09,039
the integer data type
944
00:32:09,039 --> 00:32:11,679
or of the float data type so here's an
945
00:32:11,679 --> 00:32:14,320
example of why we can't perform any math
946
00:32:14,320 --> 00:32:16,960
on a string let's say we have a variable
947
00:32:16,960 --> 00:32:17,360
called
948
00:32:17,360 --> 00:32:20,720
edge age equals input and let's
949
00:32:20,720 --> 00:32:23,760
ask how old somebody is how old
950
00:32:23,760 --> 00:32:28,240
are you now i will add one to somebody's
951
00:32:28,240 --> 00:32:30,000
age let's pretend it's their birthday
952
00:32:30,000 --> 00:32:32,399
age equals age plus one this will add
953
00:32:32,399 --> 00:32:33,440
one to their age
954
00:32:33,440 --> 00:32:35,519
now let's try and run this what is your
955
00:32:35,519 --> 00:32:37,519
name bro
956
00:32:37,519 --> 00:32:40,159
how old are you let's say that i'm 21.
957
00:32:40,159 --> 00:32:42,799
okay we ran into an error a type error
958
00:32:42,799 --> 00:32:46,080
can only concatenate string to string
959
00:32:46,080 --> 00:32:47,760
with what i mentioned previously we
960
00:32:47,760 --> 00:32:49,200
cannot normally use
961
00:32:49,200 --> 00:32:51,679
strings for any sort of mathematical
962
00:32:51,679 --> 00:32:53,279
equation or operation
963
00:32:53,279 --> 00:32:55,600
we'll want to cast it to the integer or
964
00:32:55,600 --> 00:32:56,320
the float
965
00:32:56,320 --> 00:32:59,360
data type so let's cast our user input
966
00:32:59,360 --> 00:33:01,600
to the integer data type and we will
967
00:33:01,600 --> 00:33:04,720
surround our user input with a cast
968
00:33:04,720 --> 00:33:06,480
surround your input with a set of
969
00:33:06,480 --> 00:33:08,559
parenthesis and precede this with
970
00:33:08,559 --> 00:33:11,279
int and now we can use this user input
971
00:33:11,279 --> 00:33:13,360
in a mathematical operation
972
00:33:13,360 --> 00:33:15,120
so let's try it even though this won't
973
00:33:15,120 --> 00:33:16,799
display age yet so
974
00:33:16,799 --> 00:33:20,000
what is your name bro 21 and our program
975
00:33:20,000 --> 00:33:20,880
finished with
976
00:33:20,880 --> 00:33:22,640
an x a code of zero there were no
977
00:33:22,640 --> 00:33:24,880
problems now let's attempt to display
978
00:33:24,880 --> 00:33:25,600
this edge
979
00:33:25,600 --> 00:33:28,559
so i will print
980
00:33:28,720 --> 00:33:31,919
u r plus
981
00:33:31,919 --> 00:33:35,279
age plus years
982
00:33:35,279 --> 00:33:36,880
old we're doing some string
983
00:33:36,880 --> 00:33:38,960
concatenation here
984
00:33:38,960 --> 00:33:42,559
what is your name bro how old are you 21
985
00:33:42,559 --> 00:33:44,880
all right we ran into a type error again
986
00:33:44,880 --> 00:33:47,360
can only concatenate string to string
987
00:33:47,360 --> 00:33:50,480
you cannot normally display a variable
988
00:33:50,480 --> 00:33:52,080
of the integer data type
989
00:33:52,080 --> 00:33:54,480
along with strings because we're doing
990
00:33:54,480 --> 00:33:55,919
string concatenation we're adding
991
00:33:55,919 --> 00:33:57,039
strings together
992
00:33:57,039 --> 00:33:59,200
we would need to convert this variable
993
00:33:59,200 --> 00:34:00,240
back to the string
994
00:34:00,240 --> 00:34:02,880
data type so we can cast it we'll
995
00:34:02,880 --> 00:34:03,440
surround
996
00:34:03,440 --> 00:34:05,679
our edge variable with a cast now this
997
00:34:05,679 --> 00:34:07,440
is what's going to happen we will accept
998
00:34:07,440 --> 00:34:09,280
some user input it will be of the string
999
00:34:09,280 --> 00:34:10,879
data type and then we will
1000
00:34:10,879 --> 00:34:13,440
immediately cast it to be of the integer
1001
00:34:13,440 --> 00:34:15,359
data type so that we can treat it
1002
00:34:15,359 --> 00:34:17,918
as a number instead of a character and
1003
00:34:17,918 --> 00:34:19,679
then if we need to display this
1004
00:34:19,679 --> 00:34:21,760
edge we will need to convert it back to
1005
00:34:21,760 --> 00:34:23,440
a string so that we can use
1006
00:34:23,440 --> 00:34:25,599
some string concatenation and display
1007
00:34:25,599 --> 00:34:27,040
all of these strings together
1008
00:34:27,040 --> 00:34:29,199
and now let's try this theory so what is
1009
00:34:29,199 --> 00:34:30,719
your name bro
1010
00:34:30,719 --> 00:34:33,359
how old are you let's say i'm 21 hello
1011
00:34:33,359 --> 00:34:34,000
bro you
1012
00:34:34,000 --> 00:34:36,719
are 22 years old here's a situation what
1013
00:34:36,719 --> 00:34:37,440
if we enter
1014
00:34:37,440 --> 00:34:39,918
a number that contains a decimal portion
1015
00:34:39,918 --> 00:34:41,280
so let's try this again
1016
00:34:41,280 --> 00:34:44,879
let's say i'm 21.5 years old
1017
00:34:44,879 --> 00:34:47,199
well now we ran into a different issue a
1018
00:34:47,199 --> 00:34:48,320
value error
1019
00:34:48,320 --> 00:34:51,359
invalid literal for int with base 10
1020
00:34:51,359 --> 00:34:54,239
that means if we cast a string as an
1021
00:34:54,239 --> 00:34:54,879
integer
1022
00:34:54,879 --> 00:34:57,359
it can only be a whole number a portion
1023
00:34:57,359 --> 00:34:58,560
that does not contain
1024
00:34:58,560 --> 00:35:01,440
a decimal and that is where the float
1025
00:35:01,440 --> 00:35:02,880
data type would come in
1026
00:35:02,880 --> 00:35:05,280
it is a data type that can contain a
1027
00:35:05,280 --> 00:35:06,560
decimal portion
1028
00:35:06,560 --> 00:35:09,280
for this example let's create a third
1029
00:35:09,280 --> 00:35:11,200
variable called height and we will ask
1030
00:35:11,200 --> 00:35:12,880
for somebody's height
1031
00:35:12,880 --> 00:35:16,720
input we'll create a prompt that says
1032
00:35:16,720 --> 00:35:21,280
how tall are you
1033
00:35:21,280 --> 00:35:24,160
and we will cast this to be of the float
1034
00:35:24,160 --> 00:35:25,280
data type
1035
00:35:25,280 --> 00:35:28,320
so that our string our user input
1036
00:35:28,320 --> 00:35:31,119
can contain a decimal portion and then
1037
00:35:31,119 --> 00:35:33,359
we can treat it as a number instead of a
1038
00:35:33,359 --> 00:35:34,800
series of characters
1039
00:35:34,800 --> 00:35:36,640
and i think i will delete this line we
1040
00:35:36,640 --> 00:35:38,160
won't really need it anymore
1041
00:35:38,160 --> 00:35:40,560
and then let's print their height along
1042
00:35:40,560 --> 00:35:42,160
with a message
1043
00:35:42,160 --> 00:35:45,200
u r plus then
1044
00:35:45,200 --> 00:35:47,520
height but we need to cast this back to
1045
00:35:47,520 --> 00:35:50,000
being a string
1046
00:35:50,000 --> 00:35:54,240
so we'll surround our height with a cast
1047
00:35:54,400 --> 00:35:57,520
plus let's say cm
1048
00:35:57,520 --> 00:36:00,800
tall all right let's test this
1049
00:36:00,800 --> 00:36:03,839
what is your name bro how old are you
1050
00:36:03,839 --> 00:36:06,800
let's say i'm 21 how tall are you let's
1051
00:36:06,800 --> 00:36:12,240
pretend that i'm 250.5 centimeters tall
1052
00:36:12,240 --> 00:36:14,320
i'm adding the 0.5 just to test to see
1053
00:36:14,320 --> 00:36:15,920
if this will accept a floating point
1054
00:36:15,920 --> 00:36:17,680
number
1055
00:36:17,680 --> 00:36:21,000
hello bro you are 21 years old you are
1056
00:36:21,000 --> 00:36:23,200
250.5 centimeters tall
1057
00:36:23,200 --> 00:36:25,119
not really all right everybody so that
1058
00:36:25,119 --> 00:36:26,480
is the basics of
1059
00:36:26,480 --> 00:36:29,359
user input in python normally with user
1060
00:36:29,359 --> 00:36:30,640
input it will give you
1061
00:36:30,640 --> 00:36:33,200
a value of the string data type if you
1062
00:36:33,200 --> 00:36:35,520
need to use that value for any sort of
1063
00:36:35,520 --> 00:36:36,240
math
1064
00:36:36,240 --> 00:36:38,240
you'll probably need to cast it to be of
1065
00:36:38,240 --> 00:36:40,000
the integer or the float data
1066
00:36:40,000 --> 00:36:42,400
type so if you would like a copy of this
1067
00:36:42,400 --> 00:36:44,160
code i will post this in the comment
1068
00:36:44,160 --> 00:36:45,440
section down below
1069
00:36:45,440 --> 00:36:47,920
but yeah that's how to accept user input
1070
00:36:47,920 --> 00:36:49,920
in python
1071
00:36:49,920 --> 00:36:52,079
hey what's going on everybody it's bro
1072
00:36:52,079 --> 00:36:53,839
hope you're doing well and in this video
1073
00:36:53,839 --> 00:36:55,760
i'm going to demonstrate a few useful
1074
00:36:55,760 --> 00:36:57,920
functions related to numbers in python
1075
00:36:57,920 --> 00:36:58,320
so
1076
00:36:58,320 --> 00:37:03,200
sit back relax and enjoy the show
1077
00:37:03,200 --> 00:37:05,760
okay let's begin in this video i'm going
1078
00:37:05,760 --> 00:37:07,760
to be demonstrating a bunch of useful
1079
00:37:07,760 --> 00:37:09,599
functions related to numbers
1080
00:37:09,599 --> 00:37:11,359
and a bunch of these functions are
1081
00:37:11,359 --> 00:37:13,119
located within the math
1082
00:37:13,119 --> 00:37:15,839
module now let's create a variable for
1083
00:37:15,839 --> 00:37:17,119
this example let's say
1084
00:37:17,119 --> 00:37:21,440
pi equals 3.14 just the first few digits
1085
00:37:21,440 --> 00:37:23,520
of pi is fine for this example
1086
00:37:23,520 --> 00:37:25,200
and now here's a bunch of useful
1087
00:37:25,200 --> 00:37:26,880
functions that you guys would probably
1088
00:37:26,880 --> 00:37:28,160
be interested in
1089
00:37:28,160 --> 00:37:30,560
the first is round round is a built-in
1090
00:37:30,560 --> 00:37:32,880
function and all we do is type round
1091
00:37:32,880 --> 00:37:35,119
then add a set of parenthesis and then
1092
00:37:35,119 --> 00:37:36,400
we can pass in
1093
00:37:36,400 --> 00:37:39,200
a value or a variable as an argument and
1094
00:37:39,200 --> 00:37:40,240
the round function
1095
00:37:40,240 --> 00:37:43,119
will round that number for us so pi
1096
00:37:43,119 --> 00:37:45,280
rounded to the nearest whole integer
1097
00:37:45,280 --> 00:37:48,480
is you guessed it three now we can round
1098
00:37:48,480 --> 00:37:49,440
a number up
1099
00:37:49,440 --> 00:37:51,760
by using the seal function of the math
1100
00:37:51,760 --> 00:37:52,800
class seal
1101
00:37:52,800 --> 00:37:55,839
short for ceiling so let's print
1102
00:37:55,839 --> 00:37:58,480
our result and in order to access this
1103
00:37:58,480 --> 00:38:00,160
function within the math module
1104
00:38:00,160 --> 00:38:02,480
we would type the name of the module dot
1105
00:38:02,480 --> 00:38:04,400
and here's a bunch of functions that we
1106
00:38:04,400 --> 00:38:05,599
have access to
1107
00:38:05,599 --> 00:38:07,520
so we are looking for seal and it's
1108
00:38:07,520 --> 00:38:08,960
actually at the top here for me
1109
00:38:08,960 --> 00:38:11,359
and at seal we'll round a number up to
1110
00:38:11,359 --> 00:38:12,880
the nearest whole integer
1111
00:38:12,880 --> 00:38:16,320
so pi rounded up is four
1112
00:38:16,320 --> 00:38:18,160
then on the flip side we can round a
1113
00:38:18,160 --> 00:38:19,520
number down by using
1114
00:38:19,520 --> 00:38:21,599
floor and that is also located within
1115
00:38:21,599 --> 00:38:22,560
the math module
1116
00:38:22,560 --> 00:38:25,760
so let's type math dot floor and
1117
00:38:25,760 --> 00:38:29,599
pi rounded down would be three
1118
00:38:29,599 --> 00:38:32,320
up next i have the absolute value
1119
00:38:32,320 --> 00:38:33,839
function but it's abs
1120
00:38:33,839 --> 00:38:36,960
for short abs will give you the absolute
1121
00:38:36,960 --> 00:38:38,480
value of a number
1122
00:38:38,480 --> 00:38:40,720
so the absolute value of pi actually
1123
00:38:40,720 --> 00:38:42,880
let's make this negative 3.14
1124
00:38:42,880 --> 00:38:45,520
the absolute value will tell you how far
1125
00:38:45,520 --> 00:38:46,160
a number
1126
00:38:46,160 --> 00:38:48,800
is away from zero so if you pass in a
1127
00:38:48,800 --> 00:38:49,680
negative number
1128
00:38:49,680 --> 00:38:52,160
it will give you a positive number so
1129
00:38:52,160 --> 00:38:52,800
that is
1130
00:38:52,800 --> 00:38:55,599
abs short for absolute value next we
1131
00:38:55,599 --> 00:38:56,240
have
1132
00:38:56,240 --> 00:38:59,359
the pow function pow will raise
1133
00:38:59,359 --> 00:39:03,040
a base number to a power let's raise
1134
00:39:03,040 --> 00:39:06,160
pi to the power of two so we pass in
1135
00:39:06,160 --> 00:39:09,200
two arguments a base and an exponent
1136
00:39:09,200 --> 00:39:11,920
pi to the power of two is nine point
1137
00:39:11,920 --> 00:39:12,480
eight
1138
00:39:12,480 --> 00:39:15,200
and some change so that is the pow
1139
00:39:15,200 --> 00:39:16,240
function
1140
00:39:16,240 --> 00:39:19,680
next we have square root
1141
00:39:20,800 --> 00:39:22,640
so this is located within the math
1142
00:39:22,640 --> 00:39:24,079
module we'll type
1143
00:39:24,079 --> 00:39:27,520
math dot s q r
1144
00:39:27,520 --> 00:39:29,680
t short for square root and the square
1145
00:39:29,680 --> 00:39:31,119
root of pi
1146
00:39:31,119 --> 00:39:34,240
is let's find out oh i guess we can't
1147
00:39:34,240 --> 00:39:35,119
pass in pi
1148
00:39:35,119 --> 00:39:38,000
uh let's round a different number like
1149
00:39:38,000 --> 00:39:39,599
420
1150
00:39:39,599 --> 00:39:41,119
oh i guess that's because i made pi
1151
00:39:41,119 --> 00:39:43,839
negative okay so the square root of 420
1152
00:39:43,839 --> 00:39:44,320
is
1153
00:39:44,320 --> 00:39:47,200
20.4 and some change so that is the
1154
00:39:47,200 --> 00:39:47,839
square
1155
00:39:47,839 --> 00:39:50,160
root function all right we have two left
1156
00:39:50,160 --> 00:39:52,000
we have the max function
1157
00:39:52,000 --> 00:39:54,560
the max function will find the largest
1158
00:39:54,560 --> 00:39:56,000
of a varying amount
1159
00:39:56,000 --> 00:39:58,400
of values so let's create three
1160
00:39:58,400 --> 00:40:00,079
variables for this example
1161
00:40:00,079 --> 00:40:03,599
let's say x equals one y equals two
1162
00:40:03,599 --> 00:40:07,040
and z equals three we can use the max
1163
00:40:07,040 --> 00:40:08,079
function to find
1164
00:40:08,079 --> 00:40:10,800
the largest of these values so within a
1165
00:40:10,800 --> 00:40:11,839
print statement
1166
00:40:11,839 --> 00:40:15,040
let's use the max function and pass in
1167
00:40:15,040 --> 00:40:17,200
all of the different values or variables
1168
00:40:17,200 --> 00:40:18,720
that you would like to compare
1169
00:40:18,720 --> 00:40:21,119
so i would like to find the largest
1170
00:40:21,119 --> 00:40:22,160
value between
1171
00:40:22,160 --> 00:40:26,160
x y and z and the result in this example
1172
00:40:26,160 --> 00:40:29,520
is three now the min function will find
1173
00:40:29,520 --> 00:40:30,720
the lowest
1174
00:40:30,720 --> 00:40:34,160
so min and then pass in your values or
1175
00:40:34,160 --> 00:40:35,040
variables
1176
00:40:35,040 --> 00:40:36,880
so i would like to find the minimum
1177
00:40:36,880 --> 00:40:38,319
value between x
1178
00:40:38,319 --> 00:40:42,560
y and z and in this case it is one
1179
00:40:42,560 --> 00:40:45,280
well everybody those are a few useful
1180
00:40:45,280 --> 00:40:46,640
functions that you guys might be
1181
00:40:46,640 --> 00:40:47,599
interested in
1182
00:40:47,599 --> 00:40:49,119
if you guys would like a copy of this
1183
00:40:49,119 --> 00:40:50,960
code i will post this in the comments
1184
00:40:50,960 --> 00:40:52,319
section down below
1185
00:40:52,319 --> 00:40:54,560
but yeah those are a few useful
1186
00:40:54,560 --> 00:40:56,400
functions related to numbers
1187
00:40:56,400 --> 00:41:00,240
in python hey what's going on everybody
1188
00:41:00,240 --> 00:41:02,400
it's bro hope you're doing well and in
1189
00:41:02,400 --> 00:41:04,000
this video i'm going to explain string
1190
00:41:04,000 --> 00:41:06,319
slicing in python so sit back
1191
00:41:06,319 --> 00:41:10,240
relax and enjoy the show
1192
00:41:10,240 --> 00:41:12,720
all right then let's talk about slicing
1193
00:41:12,720 --> 00:41:14,160
slicing can be used to create a
1194
00:41:14,160 --> 00:41:16,240
substring by extracting elements from
1195
00:41:16,240 --> 00:41:17,280
another string
1196
00:41:17,280 --> 00:41:19,200
the same principle can also be applied
1197
00:41:19,200 --> 00:41:21,280
to different collections as well
1198
00:41:21,280 --> 00:41:22,800
but in this video we're going to be
1199
00:41:22,800 --> 00:41:24,960
focusing on slicing strings
1200
00:41:24,960 --> 00:41:27,440
now to slice a string we can either use
1201
00:41:27,440 --> 00:41:29,040
the indexing operator
1202
00:41:29,040 --> 00:41:31,520
which is a set of square brackets or by
1203
00:41:31,520 --> 00:41:33,119
use of the slice function
1204
00:41:33,119 --> 00:41:35,440
to create a slice object this is a
1205
00:41:35,440 --> 00:41:36,640
little more complex
1206
00:41:36,640 --> 00:41:38,480
we'll discuss the slice function near
1207
00:41:38,480 --> 00:41:39,760
the end of this video
1208
00:41:39,760 --> 00:41:43,119
so with slicing there are three optional
1209
00:41:43,119 --> 00:41:43,839
arguments
1210
00:41:43,839 --> 00:41:45,280
three fields that we can fill in
1211
00:41:45,280 --> 00:41:46,880
depending on where
1212
00:41:46,880 --> 00:41:49,200
and how we want to slice our string a
1213
00:41:49,200 --> 00:41:50,240
starting index
1214
00:41:50,240 --> 00:41:53,280
a stopping index and a step let's begin
1215
00:41:53,280 --> 00:41:54,880
with the starting index
1216
00:41:54,880 --> 00:41:57,520
now for this example let's say we have a
1217
00:41:57,520 --> 00:41:58,160
string
1218
00:41:58,160 --> 00:42:00,319
variable called name and this will store
1219
00:42:00,319 --> 00:42:02,000
a value of whatever your
1220
00:42:02,000 --> 00:42:04,720
own full name is yes i legally change my
1221
00:42:04,720 --> 00:42:06,240
name to bro code you can use your own
1222
00:42:06,240 --> 00:42:07,520
name for this if you want
1223
00:42:07,520 --> 00:42:09,359
so what i would like to do is to create
1224
00:42:09,359 --> 00:42:10,560
a substring
1225
00:42:10,560 --> 00:42:13,440
based off a sliced portion of my full
1226
00:42:13,440 --> 00:42:14,000
name
1227
00:42:14,000 --> 00:42:16,160
so what i would like to do is create a
1228
00:42:16,160 --> 00:42:18,480
substring based off of just the first
1229
00:42:18,480 --> 00:42:20,079
part of my name just bro
1230
00:42:20,079 --> 00:42:23,520
so let's begin by creating a substring
1231
00:42:23,520 --> 00:42:26,720
maybe i'll call this first
1232
00:42:26,720 --> 00:42:30,480
name equals now to slice a string
1233
00:42:30,480 --> 00:42:33,040
type in a string or the name of a
1234
00:42:33,040 --> 00:42:34,720
variable that contains your string
1235
00:42:34,720 --> 00:42:36,480
and follow this with the indexing
1236
00:42:36,480 --> 00:42:38,560
operator and like i said we'll discuss
1237
00:42:38,560 --> 00:42:41,359
the slice function a little bit later so
1238
00:42:41,359 --> 00:42:42,079
we need
1239
00:42:42,079 --> 00:42:45,119
a starting index and a stopping index
1240
00:42:45,119 --> 00:42:47,599
but we'll begin with the starting index
1241
00:42:47,599 --> 00:42:49,040
where do we want to begin
1242
00:42:49,040 --> 00:42:51,839
our slice so computers they always start
1243
00:42:51,839 --> 00:42:52,800
with zero
1244
00:42:52,800 --> 00:42:54,880
so the first character in your string
1245
00:42:54,880 --> 00:42:56,079
will have an index
1246
00:42:56,079 --> 00:42:58,640
of zero i would like my slice to begin
1247
00:42:58,640 --> 00:42:59,920
at index 0
1248
00:42:59,920 --> 00:43:01,760
so i'll list that for the starting
1249
00:43:01,760 --> 00:43:03,520
position the starting index
1250
00:43:03,520 --> 00:43:05,280
so let's test this just to be sure it
1251
00:43:05,280 --> 00:43:07,119
works i'll just print
1252
00:43:07,119 --> 00:43:10,560
first name and let's see if this prints
1253
00:43:10,560 --> 00:43:11,520
capital b
1254
00:43:11,520 --> 00:43:13,839
which it does so following that same
1255
00:43:13,839 --> 00:43:15,760
pattern the next character
1256
00:43:15,760 --> 00:43:18,160
within my string would have an index of
1257
00:43:18,160 --> 00:43:19,520
one that would be
1258
00:43:19,520 --> 00:43:22,720
r then the next index two would be
1259
00:43:22,720 --> 00:43:26,240
o and three should be an empty space
1260
00:43:26,240 --> 00:43:29,280
all right so that is the starting index
1261
00:43:29,280 --> 00:43:31,680
within string slicing if we would like
1262
00:43:31,680 --> 00:43:34,400
to slice an entire portion of our string
1263
00:43:34,400 --> 00:43:37,040
not just one letter we need a stopping
1264
00:43:37,040 --> 00:43:38,240
index as well
1265
00:43:38,240 --> 00:43:40,560
and both of these fields are separated
1266
00:43:40,560 --> 00:43:41,680
with a colon
1267
00:43:41,680 --> 00:43:43,839
so let's say i would like to slice the
1268
00:43:43,839 --> 00:43:46,720
first three characters of my string
1269
00:43:46,720 --> 00:43:50,000
so this would have an index of zero one
1270
00:43:50,000 --> 00:43:52,720
two and this isn't going to work as you
1271
00:43:52,720 --> 00:43:54,079
would imagine and i'll explain
1272
00:43:54,079 --> 00:43:57,359
why so you would think that indexes
1273
00:43:57,359 --> 00:43:59,920
0 through 2 would give us three letters
1274
00:43:59,920 --> 00:44:01,839
right but it only gives us two
1275
00:44:01,839 --> 00:44:05,200
so this first index is inclusive
1276
00:44:05,200 --> 00:44:08,720
the stopping index is exclusive so if i
1277
00:44:08,720 --> 00:44:09,920
want the first
1278
00:44:09,920 --> 00:44:12,079
three characters the first three letters
1279
00:44:12,079 --> 00:44:12,960
in the string
1280
00:44:12,960 --> 00:44:15,680
i would need the starting index to be
1281
00:44:15,680 --> 00:44:16,160
zero
1282
00:44:16,160 --> 00:44:18,240
and the stopping index to be three and
1283
00:44:18,240 --> 00:44:19,839
now this should print
1284
00:44:19,839 --> 00:44:22,240
my first name and not anything else
1285
00:44:22,240 --> 00:44:24,160
there is a shorthand way of writing this
1286
00:44:24,160 --> 00:44:24,720
too
1287
00:44:24,720 --> 00:44:27,680
so if you were to leave this first index
1288
00:44:27,680 --> 00:44:28,160
blank
1289
00:44:28,160 --> 00:44:30,880
for start well python is going to assume
1290
00:44:30,880 --> 00:44:31,920
that this is zero
1291
00:44:31,920 --> 00:44:34,560
the beginning of your string so this
1292
00:44:34,560 --> 00:44:35,200
would be
1293
00:44:35,200 --> 00:44:37,599
no different than what we had previously
1294
00:44:37,599 --> 00:44:38,880
just having colon
1295
00:44:38,880 --> 00:44:41,040
three now let's try and create a
1296
00:44:41,040 --> 00:44:42,160
separate substring
1297
00:44:42,160 --> 00:44:45,280
for a last name so last
1298
00:44:45,280 --> 00:44:49,280
name name now we need a
1299
00:44:49,280 --> 00:44:52,319
starting index and a stopping index so
1300
00:44:52,319 --> 00:44:54,880
we need the starting index at least so
1301
00:44:54,880 --> 00:44:56,000
this would be
1302
00:44:56,000 --> 00:44:59,119
zero one two three four
1303
00:44:59,119 --> 00:45:01,599
four colon and where do we want this to
1304
00:45:01,599 --> 00:45:02,640
stop
1305
00:45:02,640 --> 00:45:05,680
zero one two three four five
1306
00:45:05,680 --> 00:45:08,240
six seven and remember that this is
1307
00:45:08,240 --> 00:45:09,119
exclusive
1308
00:45:09,119 --> 00:45:11,599
so we're going to place eight here and
1309
00:45:11,599 --> 00:45:13,440
now i would like to display
1310
00:45:13,440 --> 00:45:16,079
last name and this should just display
1311
00:45:16,079 --> 00:45:16,960
code
1312
00:45:16,960 --> 00:45:19,359
and a shortcut is that if you were to
1313
00:45:19,359 --> 00:45:20,079
leave
1314
00:45:20,079 --> 00:45:22,400
stop blank but you still have that colon
1315
00:45:22,400 --> 00:45:24,560
this is a shorthand way of writing
1316
00:45:24,560 --> 00:45:27,200
i would like every character starting at
1317
00:45:27,200 --> 00:45:28,160
index four
1318
00:45:28,160 --> 00:45:30,560
and everything after until the end so
1319
00:45:30,560 --> 00:45:32,000
there would be no change to this
1320
00:45:32,000 --> 00:45:34,319
as well you can write it either way okay
1321
00:45:34,319 --> 00:45:36,079
so we still have to discuss
1322
00:45:36,079 --> 00:45:38,640
step step is an optional field that we
1323
00:45:38,640 --> 00:45:40,079
can set a value to
1324
00:45:40,079 --> 00:45:42,800
so step is how much we're increasing our
1325
00:45:42,800 --> 00:45:43,680
index by
1326
00:45:43,680 --> 00:45:46,720
between starting and stopping so it is
1327
00:45:46,720 --> 00:45:48,000
entirely possible
1328
00:45:48,000 --> 00:45:50,640
to create a substring that will count
1329
00:45:50,640 --> 00:45:51,119
only
1330
00:45:51,119 --> 00:45:53,440
every second character after the first
1331
00:45:53,440 --> 00:45:55,440
normally step is one by default
1332
00:45:55,440 --> 00:45:57,520
but if we were to set that to two we're
1333
00:45:57,520 --> 00:45:58,640
going to count only
1334
00:45:58,640 --> 00:46:01,440
every second character so let's try this
1335
00:46:01,440 --> 00:46:02,880
maybe i'll create a new
1336
00:46:02,880 --> 00:46:05,200
variable called funky name because i
1337
00:46:05,200 --> 00:46:06,480
really don't know what else to call this
1338
00:46:06,480 --> 00:46:07,680
for this example
1339
00:46:07,680 --> 00:46:10,560
let's say funky name equals name and i'm
1340
00:46:10,560 --> 00:46:11,440
going to count
1341
00:46:11,440 --> 00:46:14,000
only every second character including
1342
00:46:14,000 --> 00:46:14,880
the first
1343
00:46:14,880 --> 00:46:18,480
so the indexes would be zero colon
1344
00:46:18,480 --> 00:46:20,640
eight for my example but if you're using
1345
00:46:20,640 --> 00:46:21,839
your own name that's probably going to
1346
00:46:21,839 --> 00:46:23,200
be something different
1347
00:46:23,200 --> 00:46:26,240
so the default first step is one and if
1348
00:46:26,240 --> 00:46:28,000
i were to display funky name as it
1349
00:46:28,000 --> 00:46:30,480
is currently this will just display my
1350
00:46:30,480 --> 00:46:31,119
full name
1351
00:46:31,119 --> 00:46:33,920
but if we set step to two this will only
1352
00:46:33,920 --> 00:46:34,560
display
1353
00:46:34,560 --> 00:46:36,400
every second character including the
1354
00:46:36,400 --> 00:46:38,640
first so funky name is now
1355
00:46:38,640 --> 00:46:42,319
for me at least b o c d now if step
1356
00:46:42,319 --> 00:46:44,960
was three this will print only every
1357
00:46:44,960 --> 00:46:46,880
third character including the first
1358
00:46:46,880 --> 00:46:50,160
where it's b space d a shorthand way of
1359
00:46:50,160 --> 00:46:50,800
writing this
1360
00:46:50,800 --> 00:46:53,920
is that you could leave start and stop
1361
00:46:53,920 --> 00:46:55,920
empty but the important thing is to have
1362
00:46:55,920 --> 00:46:57,280
these two colons
1363
00:46:57,280 --> 00:46:59,119
python is going to assume that if you
1364
00:46:59,119 --> 00:47:00,720
leave your starting index
1365
00:47:00,720 --> 00:47:03,520
empty you're referring to index 0 the
1366
00:47:03,520 --> 00:47:04,240
beginning
1367
00:47:04,240 --> 00:47:06,800
if your second index your stopping index
1368
00:47:06,800 --> 00:47:07,599
is empty
1369
00:47:07,599 --> 00:47:10,319
python will assume this will be the very
1370
00:47:10,319 --> 00:47:11,440
end of your string
1371
00:47:11,440 --> 00:47:13,520
so just having two colons and then step
1372
00:47:13,520 --> 00:47:14,560
three would be
1373
00:47:14,560 --> 00:47:16,880
no different than what we had previously
1374
00:47:16,880 --> 00:47:19,040
or this next part i'm going to explain
1375
00:47:19,040 --> 00:47:22,000
how we can reverse a string in python so
1376
00:47:22,000 --> 00:47:24,240
let's create a new substring variable
1377
00:47:24,240 --> 00:47:24,720
called
1378
00:47:24,720 --> 00:47:27,680
reversed name equals name i would like
1379
00:47:27,680 --> 00:47:29,200
to use the entire string
1380
00:47:29,200 --> 00:47:32,240
so i can just use two colons python will
1381
00:47:32,240 --> 00:47:33,359
fill in the rest
1382
00:47:33,359 --> 00:47:35,680
so for the step this is going to be
1383
00:47:35,680 --> 00:47:36,800
negative one
1384
00:47:36,800 --> 00:47:38,319
it's kind of like we're counting
1385
00:47:38,319 --> 00:47:40,240
backwards and we're going to create
1386
00:47:40,240 --> 00:47:43,440
a new sub string based off my name but
1387
00:47:43,440 --> 00:47:44,240
in reverse
1388
00:47:44,240 --> 00:47:47,200
so if i were to print my reversed name
1389
00:47:47,200 --> 00:47:48,400
this is now
1390
00:47:48,400 --> 00:47:50,640
edok orb i think that's how you
1391
00:47:50,640 --> 00:47:51,440
pronounce it
1392
00:47:51,440 --> 00:47:53,920
so that's how to reverse a string in
1393
00:47:53,920 --> 00:47:55,760
python using slicing
1394
00:47:55,760 --> 00:47:57,760
now let's move on to part two of this
1395
00:47:57,760 --> 00:47:59,680
video i'm going to be explaining the
1396
00:47:59,680 --> 00:48:01,760
slice function and we can use the slice
1397
00:48:01,760 --> 00:48:03,520
function to create a slice
1398
00:48:03,520 --> 00:48:05,599
object which is actually reusable so
1399
00:48:05,599 --> 00:48:07,040
that's somewhat convenient
1400
00:48:07,040 --> 00:48:08,640
let's take a different scenario this
1401
00:48:08,640 --> 00:48:11,440
time let's say we have a website url
1402
00:48:11,440 --> 00:48:16,000
website equals http colon2forwardslashes
1403
00:48:16,000 --> 00:48:18,160
google.com but use whatever website you
1404
00:48:18,160 --> 00:48:20,079
want what i would like to do for this
1405
00:48:20,079 --> 00:48:20,720
program
1406
00:48:20,720 --> 00:48:23,599
is remove and create a substring based
1407
00:48:23,599 --> 00:48:24,960
off the website name
1408
00:48:24,960 --> 00:48:27,040
and not anything else i would like to
1409
00:48:27,040 --> 00:48:30,480
exclude the http portion as well as the
1410
00:48:30,480 --> 00:48:31,760
dot-com portion
1411
00:48:31,760 --> 00:48:34,160
of this website url so let's break it
1412
00:48:34,160 --> 00:48:35,359
down step by step
1413
00:48:35,359 --> 00:48:37,599
our first step is to create a slice
1414
00:48:37,599 --> 00:48:40,079
object so i will name the slice object
1415
00:48:40,079 --> 00:48:40,880
as slice
1416
00:48:40,880 --> 00:48:43,520
slice equals and i will invoke the slice
1417
00:48:43,520 --> 00:48:44,400
function
1418
00:48:44,400 --> 00:48:46,559
so within the parentheses of the slice
1419
00:48:46,559 --> 00:48:48,400
function we can add up to
1420
00:48:48,400 --> 00:48:52,079
three values a start a stop and a step
1421
00:48:52,079 --> 00:48:53,839
exactly the same as what we did with
1422
00:48:53,839 --> 00:48:56,480
indexing however with the slice function
1423
00:48:56,480 --> 00:48:59,040
we separate each of these values with a
1424
00:48:59,040 --> 00:48:59,520
comma
1425
00:48:59,520 --> 00:49:01,520
instead of a colon but it works the same
1426
00:49:01,520 --> 00:49:03,359
way so we would like
1427
00:49:03,359 --> 00:49:05,520
our substring to begin where the name of
1428
00:49:05,520 --> 00:49:06,800
the website begins
1429
00:49:06,800 --> 00:49:10,400
so we would like our start to be zero
1430
00:49:10,400 --> 00:49:13,599
one two three four five six seven
1431
00:49:13,599 --> 00:49:15,839
so i will pass in seven for where my
1432
00:49:15,839 --> 00:49:17,200
substring will begin
1433
00:49:17,200 --> 00:49:19,680
and now each value within the slice
1434
00:49:19,680 --> 00:49:21,359
function will be separated with
1435
00:49:21,359 --> 00:49:23,839
a comma instead of a colon like what we
1436
00:49:23,839 --> 00:49:25,920
did with the indexing operator
1437
00:49:25,920 --> 00:49:28,400
now for the stop index that's going to
1438
00:49:28,400 --> 00:49:29,520
be a little bit tricky
1439
00:49:29,520 --> 00:49:31,520
and let me explain why one issue that
1440
00:49:31,520 --> 00:49:32,800
we're going to run into
1441
00:49:32,800 --> 00:49:34,880
is that not all website names are
1442
00:49:34,880 --> 00:49:36,559
consistent with their length they can
1443
00:49:36,559 --> 00:49:37,599
really vary
1444
00:49:37,599 --> 00:49:40,079
so it is not possible for us to use the
1445
00:49:40,079 --> 00:49:41,599
same stopping index
1446
00:49:41,599 --> 00:49:44,319
for each of these website urls so one
1447
00:49:44,319 --> 00:49:46,240
way in which we can account for that
1448
00:49:46,240 --> 00:49:48,400
is to use what is called a negative
1449
00:49:48,400 --> 00:49:51,119
index each character within a string
1450
00:49:51,119 --> 00:49:53,760
has a positive index as well as a
1451
00:49:53,760 --> 00:49:54,880
negative index
1452
00:49:54,880 --> 00:49:57,440
and a negative index works almost
1453
00:49:57,440 --> 00:49:58,880
exactly the same way
1454
00:49:58,880 --> 00:50:01,440
except the character most on the right
1455
00:50:01,440 --> 00:50:03,520
begins with a negative index
1456
00:50:03,520 --> 00:50:06,240
of minus one and the character on the
1457
00:50:06,240 --> 00:50:06,800
left
1458
00:50:06,800 --> 00:50:09,200
of that would be negative two negative
1459
00:50:09,200 --> 00:50:10,640
three and negative four
1460
00:50:10,640 --> 00:50:12,720
it's as if you're counting backwards so
1461
00:50:12,720 --> 00:50:14,640
with indexing we can use some
1462
00:50:14,640 --> 00:50:15,920
combination of
1463
00:50:15,920 --> 00:50:18,640
positive and negative indexing so i
1464
00:50:18,640 --> 00:50:19,520
would like
1465
00:50:19,520 --> 00:50:22,400
my substring to end where this dot is so
1466
00:50:22,400 --> 00:50:23,200
this would have
1467
00:50:23,200 --> 00:50:26,240
a index of minus one minus two
1468
00:50:26,240 --> 00:50:28,880
minus three minus four and remember that
1469
00:50:28,880 --> 00:50:30,400
this is exclusive
1470
00:50:30,400 --> 00:50:32,880
so i would like my slice to begin at
1471
00:50:32,880 --> 00:50:34,079
index seven
1472
00:50:34,079 --> 00:50:37,280
and a negative index of four for the
1473
00:50:37,280 --> 00:50:38,559
stopping position
1474
00:50:38,559 --> 00:50:40,480
so that will give me just the website
1475
00:50:40,480 --> 00:50:41,920
name and remove
1476
00:50:41,920 --> 00:50:44,960
the http portion as well as the dot com
1477
00:50:44,960 --> 00:50:47,119
portion of my website url
1478
00:50:47,119 --> 00:50:49,359
and now we have a slice object and we
1479
00:50:49,359 --> 00:50:50,559
can reuse this
1480
00:50:50,559 --> 00:50:53,520
so to apply your slice object type in
1481
00:50:53,520 --> 00:50:54,880
the name your string
1482
00:50:54,880 --> 00:50:57,280
index operator and place your slice
1483
00:50:57,280 --> 00:50:58,319
right within here
1484
00:50:58,319 --> 00:51:01,599
and let's print this so print
1485
00:51:01,599 --> 00:51:03,920
our website and apply the slice to it
1486
00:51:03,920 --> 00:51:05,359
and now this should give us
1487
00:51:05,359 --> 00:51:07,520
just the website name and now let's test
1488
00:51:07,520 --> 00:51:09,680
this with maybe another website
1489
00:51:09,680 --> 00:51:11,760
i'll rename this as website one and
1490
00:51:11,760 --> 00:51:14,960
let's create website2
1491
00:51:14,960 --> 00:51:17,760
and let's try a different website name
1492
00:51:17,760 --> 00:51:18,640
for goodmeasure
1493
00:51:18,640 --> 00:51:22,400
so let's try wikipedia so we can reuse
1494
00:51:22,400 --> 00:51:23,680
our slice object
1495
00:51:23,680 --> 00:51:26,319
and we will apply our slice to website
1496
00:51:26,319 --> 00:51:27,359
two this time
1497
00:51:27,359 --> 00:51:30,400
and this should remove the url portions
1498
00:51:30,400 --> 00:51:32,240
http as well as
1499
00:51:32,240 --> 00:51:34,960
com all right everybody so that is
1500
00:51:34,960 --> 00:51:36,240
slicing strings
1501
00:51:36,240 --> 00:51:38,240
in python you can either use the
1502
00:51:38,240 --> 00:51:39,359
indexing operator
1503
00:51:39,359 --> 00:51:42,160
or the slice function to create a slice
1504
00:51:42,160 --> 00:51:43,119
object
1505
00:51:43,119 --> 00:51:44,480
if you would like a copy of all this
1506
00:51:44,480 --> 00:51:46,480
code i will post all of this in the
1507
00:51:46,480 --> 00:51:48,079
comments section down below
1508
00:51:48,079 --> 00:51:50,160
but yeah that is how string slicing
1509
00:51:50,160 --> 00:51:52,880
works in python
1510
00:51:52,880 --> 00:51:54,800
hey what's going on people it's you bro
1511
00:51:54,800 --> 00:51:56,559
hope you're doing well and in this video
1512
00:51:56,559 --> 00:51:57,599
i'm going to explain
1513
00:51:57,599 --> 00:51:59,760
if statements else if statements and
1514
00:51:59,760 --> 00:52:01,520
else statements and python so
1515
00:52:01,520 --> 00:52:06,160
sit back relax and enjoy the show
1516
00:52:06,480 --> 00:52:08,240
all right people let's talk about if
1517
00:52:08,240 --> 00:52:10,640
statements an if statement is a block of
1518
00:52:10,640 --> 00:52:11,119
code
1519
00:52:11,119 --> 00:52:13,680
that will execute only if its condition
1520
00:52:13,680 --> 00:52:14,640
is true
1521
00:52:14,640 --> 00:52:16,640
it's a very basic form of decision
1522
00:52:16,640 --> 00:52:18,400
making with programming
1523
00:52:18,400 --> 00:52:21,119
for this example let's create a prompt
1524
00:52:21,119 --> 00:52:22,640
where we will ask a user
1525
00:52:22,640 --> 00:52:24,400
for their age and depending on what
1526
00:52:24,400 --> 00:52:26,960
their age is we will print a response
1527
00:52:26,960 --> 00:52:29,599
so let's say age equals input and then
1528
00:52:29,599 --> 00:52:30,800
we will add a prompt
1529
00:52:30,800 --> 00:52:33,200
how old are you now this will return a
1530
00:52:33,200 --> 00:52:33,920
string
1531
00:52:33,920 --> 00:52:36,079
so we should probably cast this as an
1532
00:52:36,079 --> 00:52:37,599
integer data type
1533
00:52:37,599 --> 00:52:39,839
so we now have a variable called age
1534
00:52:39,839 --> 00:52:42,000
where we will accept some user input
1535
00:52:42,000 --> 00:52:43,839
let's check somebody's age with a
1536
00:52:43,839 --> 00:52:45,599
variety of if statements
1537
00:52:45,599 --> 00:52:47,680
so let's check to see if somebody's age
1538
00:52:47,680 --> 00:52:49,680
is greater than or equal to 18
1539
00:52:49,680 --> 00:52:51,839
so to create an if statement type if and
1540
00:52:51,839 --> 00:52:53,200
then some sort of condition
1541
00:52:53,200 --> 00:52:56,400
if age is greater than or equal to 18
1542
00:52:56,400 --> 00:52:58,720
to finish this if statement add a colon
1543
00:52:58,720 --> 00:52:59,599
at the end
1544
00:52:59,599 --> 00:53:01,359
now pay attention to this if i move down
1545
00:53:01,359 --> 00:53:03,520
to the next line you'll notice that my
1546
00:53:03,520 --> 00:53:04,400
cursor is now
1547
00:53:04,400 --> 00:53:07,440
indented any indented code underneath an
1548
00:53:07,440 --> 00:53:10,240
if statement is the block of code for
1549
00:53:10,240 --> 00:53:11,520
that if statement
1550
00:53:11,520 --> 00:53:13,760
so if this condition is true we will
1551
00:53:13,760 --> 00:53:14,960
execute some
1552
00:53:14,960 --> 00:53:17,200
block of code if it's false we'll skip
1553
00:53:17,200 --> 00:53:19,440
over it so if somebody's age is greater
1554
00:53:19,440 --> 00:53:20,880
than or equal to 18
1555
00:53:20,880 --> 00:53:24,319
let's print you are an
1556
00:53:24,319 --> 00:53:27,680
adult and let's try this program how old
1557
00:53:27,680 --> 00:53:28,160
are you
1558
00:53:28,160 --> 00:53:31,040
let's say i'm 21 so my condition when we
1559
00:53:31,040 --> 00:53:32,319
get to the if statement
1560
00:53:32,319 --> 00:53:33,599
is going to check to see if this
1561
00:53:33,599 --> 00:53:35,599
condition is true if it is true we're
1562
00:53:35,599 --> 00:53:36,079
going to
1563
00:53:36,079 --> 00:53:38,559
execute this block of code if it's not
1564
00:53:38,559 --> 00:53:39,680
we skip over it
1565
00:53:39,680 --> 00:53:42,319
so how old are you let's say i'm 21 i'm
1566
00:53:42,319 --> 00:53:43,359
going to hit enter
1567
00:53:43,359 --> 00:53:45,680
and my if statement is true so we
1568
00:53:45,680 --> 00:53:48,000
execute this block of code which prints
1569
00:53:48,000 --> 00:53:49,839
you are an adult so what if this
1570
00:53:49,839 --> 00:53:51,280
condition is false
1571
00:53:51,280 --> 00:53:53,200
let's say i'm 12. well then we're just
1572
00:53:53,200 --> 00:53:54,880
going to skip over this block of code
1573
00:53:54,880 --> 00:53:56,240
and continue on with the rest of the
1574
00:53:56,240 --> 00:53:56,800
program
1575
00:53:56,800 --> 00:53:58,480
if you would like your program to take
1576
00:53:58,480 --> 00:54:00,000
some other course of action
1577
00:54:00,000 --> 00:54:02,960
if this condition is false we can add an
1578
00:54:02,960 --> 00:54:03,359
else
1579
00:54:03,359 --> 00:54:06,160
statement so after the if statement add
1580
00:54:06,160 --> 00:54:06,800
else
1581
00:54:06,800 --> 00:54:08,640
make sure you get the indentation right
1582
00:54:08,640 --> 00:54:10,240
you don't want this within the block of
1583
00:54:10,240 --> 00:54:11,839
code for the if statement
1584
00:54:11,839 --> 00:54:14,800
so if this condition is false we can
1585
00:54:14,800 --> 00:54:15,200
take
1586
00:54:15,200 --> 00:54:17,359
some other course of action so if
1587
00:54:17,359 --> 00:54:18,319
somebody's age
1588
00:54:18,319 --> 00:54:22,960
is less than 18 let's print
1589
00:54:22,960 --> 00:54:26,400
you are a child
1590
00:54:26,400 --> 00:54:28,400
so let's try this again i'm going to
1591
00:54:28,400 --> 00:54:29,440
enter that i
1592
00:54:29,440 --> 00:54:31,520
am 12 years old not really but let's
1593
00:54:31,520 --> 00:54:33,119
just pretend i am
1594
00:54:33,119 --> 00:54:35,760
so this will now print you are a child
1595
00:54:35,760 --> 00:54:36,160
if
1596
00:54:36,160 --> 00:54:38,480
this condition is false perform this
1597
00:54:38,480 --> 00:54:40,799
block of code for the else statement
1598
00:54:40,799 --> 00:54:43,280
so with if statements we can check more
1599
00:54:43,280 --> 00:54:45,440
than one condition before reaching the
1600
00:54:45,440 --> 00:54:46,960
else statement by using an
1601
00:54:46,960 --> 00:54:50,240
else if statement and we add that after
1602
00:54:50,240 --> 00:54:53,280
an if statement so it's shortened to e l
1603
00:54:53,280 --> 00:54:56,240
i f for else if so let's check to see if
1604
00:54:56,240 --> 00:54:57,520
somebody's age
1605
00:54:57,520 --> 00:55:00,799
is less than zero for some reason
1606
00:55:00,799 --> 00:55:03,839
so let's print
1607
00:55:03,839 --> 00:55:08,880
you haven't been born yet
1608
00:55:08,880 --> 00:55:11,359
so with if statements we start at the
1609
00:55:11,359 --> 00:55:12,480
top we first check
1610
00:55:12,480 --> 00:55:14,640
the if statement if this is false we
1611
00:55:14,640 --> 00:55:15,839
move down to the next
1612
00:55:15,839 --> 00:55:18,000
statement to check then we check our
1613
00:55:18,000 --> 00:55:19,280
else if statement
1614
00:55:19,280 --> 00:55:21,920
if this is also false then we resort to
1615
00:55:21,920 --> 00:55:23,119
the else statement
1616
00:55:23,119 --> 00:55:25,119
the else statement is sort of like a
1617
00:55:25,119 --> 00:55:27,599
last resort if all conditions
1618
00:55:27,599 --> 00:55:30,160
evaluate to be false then we execute the
1619
00:55:30,160 --> 00:55:31,040
else statement
1620
00:55:31,040 --> 00:55:33,760
so let's try this how old are you let's
1621
00:55:33,760 --> 00:55:34,880
say i'm negative
1622
00:55:34,880 --> 00:55:38,400
1 years old you haven't been born yet
1623
00:55:38,400 --> 00:55:40,720
so with if statements we always begin
1624
00:55:40,720 --> 00:55:42,880
with an if statement and we check
1625
00:55:42,880 --> 00:55:44,799
that condition if that condition
1626
00:55:44,799 --> 00:55:46,400
evaluates to be false
1627
00:55:46,400 --> 00:55:48,640
then we will skip this block of code and
1628
00:55:48,640 --> 00:55:50,880
move down to the else if statement
1629
00:55:50,880 --> 00:55:53,359
if there is one it's optional if this
1630
00:55:53,359 --> 00:55:54,319
condition
1631
00:55:54,319 --> 00:55:56,960
is also false then we will use our else
1632
00:55:56,960 --> 00:55:58,880
statement as a last resort
1633
00:55:58,880 --> 00:56:00,799
if there is one if there is no else
1634
00:56:00,799 --> 00:56:02,720
statement then we just skip over all of
1635
00:56:02,720 --> 00:56:04,160
this then and continue on with the rest
1636
00:56:04,160 --> 00:56:05,359
of the program
1637
00:56:05,359 --> 00:56:07,680
so you can add more than one else if
1638
00:56:07,680 --> 00:56:10,240
statement so let's check something else
1639
00:56:10,240 --> 00:56:14,160
let's check to see else if age
1640
00:56:14,160 --> 00:56:17,599
is equal to 100 so if you need to check
1641
00:56:17,599 --> 00:56:18,160
to see
1642
00:56:18,160 --> 00:56:21,119
if a value is equal to a particular
1643
00:56:21,119 --> 00:56:21,839
value
1644
00:56:21,839 --> 00:56:24,319
make sure you use double equal signs
1645
00:56:24,319 --> 00:56:25,760
this is the comparison
1646
00:56:25,760 --> 00:56:29,200
operator for equality if you use just
1647
00:56:29,200 --> 00:56:31,440
one equal sign that's the assignment
1648
00:56:31,440 --> 00:56:32,319
operator and
1649
00:56:32,319 --> 00:56:34,319
python thinks you're attempting to set
1650
00:56:34,319 --> 00:56:35,839
age equal to 100
1651
00:56:35,839 --> 00:56:38,079
so if you want to check to see if age is
1652
00:56:38,079 --> 00:56:39,119
equal to 100
1653
00:56:39,119 --> 00:56:42,400
then use double equals so let's check to
1654
00:56:42,400 --> 00:56:44,480
see if somebody's age is
1655
00:56:44,480 --> 00:56:48,240
equal to 100 so let's print
1656
00:56:48,240 --> 00:56:52,400
you are a century
1657
00:56:52,400 --> 00:56:54,960
old now pay attention to this if i were
1658
00:56:54,960 --> 00:56:56,400
to run this program
1659
00:56:56,400 --> 00:56:59,599
how old are you let's say i'm 100. so
1660
00:56:59,599 --> 00:57:02,000
this is printing you are an adult that's
1661
00:57:02,000 --> 00:57:03,520
because we first check
1662
00:57:03,520 --> 00:57:06,079
our if statement and well we're 100
1663
00:57:06,079 --> 00:57:07,520
years old and 100
1664
00:57:07,520 --> 00:57:10,079
is greater than or equal to 18. so we
1665
00:57:10,079 --> 00:57:10,720
print
1666
00:57:10,720 --> 00:57:12,720
this block of code we execute this block
1667
00:57:12,720 --> 00:57:14,480
of code and then we skip
1668
00:57:14,480 --> 00:57:17,839
everything else even though age is equal
1669
00:57:17,839 --> 00:57:19,119
to 100 within
1670
00:57:19,119 --> 00:57:22,079
our else if statement so the order of
1671
00:57:22,079 --> 00:57:23,119
your if statements
1672
00:57:23,119 --> 00:57:25,680
does matter so let's change this around
1673
00:57:25,680 --> 00:57:26,640
let's add this
1674
00:57:26,640 --> 00:57:30,160
to the very beginning and this will be
1675
00:57:30,160 --> 00:57:30,559
our
1676
00:57:30,559 --> 00:57:33,599
if statement and we will set age
1677
00:57:33,599 --> 00:57:35,520
is greater than or equal to 18 to be an
1678
00:57:35,520 --> 00:57:36,720
else if statement
1679
00:57:36,720 --> 00:57:38,720
so we're first going to check to see if
1680
00:57:38,720 --> 00:57:40,559
age is equal to 100
1681
00:57:40,559 --> 00:57:43,440
if not we will check to see if age is
1682
00:57:43,440 --> 00:57:45,359
greater than or equal to 18
1683
00:57:45,359 --> 00:57:47,040
and we just follow that order so let's
1684
00:57:47,040 --> 00:57:48,880
try this again how old are you
1685
00:57:48,880 --> 00:57:52,000
i am 100 years old you are
1686
00:57:52,000 --> 00:57:54,960
a century old so we first check our if
1687
00:57:54,960 --> 00:57:55,599
statement
1688
00:57:55,599 --> 00:57:57,359
if this condition is true we will
1689
00:57:57,359 --> 00:57:59,119
execute this block of code
1690
00:57:59,119 --> 00:58:01,119
if it's false we just go down the order
1691
00:58:01,119 --> 00:58:03,520
till we reach our else statement
1692
00:58:03,520 --> 00:58:06,160
well everybody that is the basics of if
1693
00:58:06,160 --> 00:58:08,000
statements we have if statements
1694
00:58:08,000 --> 00:58:10,640
else if statements and else statements
1695
00:58:10,640 --> 00:58:12,880
so if you would like a copy of this code
1696
00:58:12,880 --> 00:58:14,720
i will post this in the comments down
1697
00:58:14,720 --> 00:58:15,280
below
1698
00:58:15,280 --> 00:58:17,599
but yeah that is how if statements work
1699
00:58:17,599 --> 00:58:19,680
in python
1700
00:58:19,680 --> 00:58:21,680
hey what's going on everybody it's bro
1701
00:58:21,680 --> 00:58:23,280
hope you're doing well in this video i'm
1702
00:58:23,280 --> 00:58:24,160
going to explain
1703
00:58:24,160 --> 00:58:26,880
logical operators in python so sit back
1704
00:58:26,880 --> 00:58:27,680
relax
1705
00:58:27,680 --> 00:58:30,559
and enjoy the show
1706
00:58:30,720 --> 00:58:32,480
all right everybody let's talk about
1707
00:58:32,480 --> 00:58:33,760
logical operators
1708
00:58:33,760 --> 00:58:36,640
these are used to check if two or more
1709
00:58:36,640 --> 00:58:37,920
conditional statements
1710
00:58:37,920 --> 00:58:40,640
are true so we'll be discussing the and
1711
00:58:40,640 --> 00:58:41,760
as well as the or
1712
00:58:41,760 --> 00:58:44,000
logical operators there's also a third
1713
00:58:44,000 --> 00:58:44,799
one called
1714
00:58:44,799 --> 00:58:46,240
not but that works a little bit
1715
00:58:46,240 --> 00:58:48,000
different and i'll explain why
1716
00:58:48,000 --> 00:58:50,240
so let's create a program to demonstrate
1717
00:58:50,240 --> 00:58:51,680
this let's create a program
1718
00:58:51,680 --> 00:58:53,119
where we will ask somebody for the
1719
00:58:53,119 --> 00:58:55,119
temperature outside and then depending
1720
00:58:55,119 --> 00:58:56,640
on the temperature if it falls within a
1721
00:58:56,640 --> 00:58:57,440
certain range
1722
00:58:57,440 --> 00:58:59,599
we will print a message so we'll need to
1723
00:58:59,599 --> 00:59:01,839
use and as well as or logical operators
1724
00:59:01,839 --> 00:59:02,640
to do this
1725
00:59:02,640 --> 00:59:04,559
so here i have a prompt temp for
1726
00:59:04,559 --> 00:59:06,640
temperature equals input what is the
1727
00:59:06,640 --> 00:59:08,319
temperature outside
1728
00:59:08,319 --> 00:59:10,640
so i'm going to cast this as an integer
1729
00:59:10,640 --> 00:59:11,839
data type
1730
00:59:11,839 --> 00:59:13,839
so what if we want to check to see if
1731
00:59:13,839 --> 00:59:15,440
our temperature is within a certain
1732
00:59:15,440 --> 00:59:16,160
range
1733
00:59:16,160 --> 00:59:19,040
we can use the and logical operator to
1734
00:59:19,040 --> 00:59:19,680
check
1735
00:59:19,680 --> 00:59:21,839
two or more conditional statements this
1736
00:59:21,839 --> 00:59:23,040
is how we might do that
1737
00:59:23,040 --> 00:59:26,400
let's check if temp
1738
00:59:26,400 --> 00:59:28,960
is greater than or equal to zero zero
1739
00:59:28,960 --> 00:59:30,559
degrees celsius
1740
00:59:30,559 --> 00:59:32,559
and we can check another condition too
1741
00:59:32,559 --> 00:59:34,079
if temp
1742
00:59:34,079 --> 00:59:36,720
is less than or equal to 30 degrees
1743
00:59:36,720 --> 00:59:38,000
celsius
1744
00:59:38,000 --> 00:59:40,559
so we need to add a colon at the end to
1745
00:59:40,559 --> 00:59:42,720
finish this if statement
1746
00:59:42,720 --> 00:59:44,960
and let me just fix some of the spacing
1747
00:59:44,960 --> 00:59:46,319
okay so
1748
00:59:46,319 --> 00:59:48,720
with the and logical operator in order
1749
00:59:48,720 --> 00:59:51,280
for this entire condition to be true
1750
00:59:51,280 --> 00:59:53,920
both conditions must be true this needs
1751
00:59:53,920 --> 00:59:54,880
to be true
1752
00:59:54,880 --> 00:59:57,200
and this needs to be true so if our
1753
00:59:57,200 --> 00:59:57,920
temperature
1754
00:59:57,920 --> 01:00:01,440
is equal to or above 0 as well
1755
01:00:01,440 --> 01:00:04,480
as being less than or equal to 30 that
1756
01:00:04,480 --> 01:00:05,680
means our temperature
1757
01:00:05,680 --> 01:00:07,680
falls within a certain range so let's
1758
01:00:07,680 --> 01:00:08,720
print a message
1759
01:00:08,720 --> 01:00:12,799
such as the temperature
1760
01:00:12,799 --> 01:00:16,160
is good today
1761
01:00:16,319 --> 01:00:19,760
print go
1762
01:00:19,760 --> 01:00:23,440
outside all right let's try it
1763
01:00:23,440 --> 01:00:25,280
so what is the temperature outside let's
1764
01:00:25,280 --> 01:00:27,680
say it's 20 degrees celsius
1765
01:00:27,680 --> 01:00:30,000
well the temperature is good today go
1766
01:00:30,000 --> 01:00:30,880
outside
1767
01:00:30,880 --> 01:00:33,359
this condition is true 20 is greater
1768
01:00:33,359 --> 01:00:34,880
than or equal to zero
1769
01:00:34,880 --> 01:00:37,599
and temperature is less than or equal to
1770
01:00:37,599 --> 01:00:38,319
30.
1771
01:00:38,319 --> 01:00:40,240
let's try a different temperature like
1772
01:00:40,240 --> 01:00:42,079
negative 20 degrees celsius so it's
1773
01:00:42,079 --> 01:00:44,640
really freaking cold outside right now
1774
01:00:44,640 --> 01:00:46,640
as you can see nothing happened because
1775
01:00:46,640 --> 01:00:48,000
our first condition
1776
01:00:48,000 --> 01:00:50,880
was false our temperature is not greater
1777
01:00:50,880 --> 01:00:52,559
than or equal to zero
1778
01:00:52,559 --> 01:00:54,880
but our temperature is less than or
1779
01:00:54,880 --> 01:00:56,079
equal to 30 so
1780
01:00:56,079 --> 01:00:58,000
our second condition is true but our
1781
01:00:58,000 --> 01:00:59,520
first condition was false
1782
01:00:59,520 --> 01:01:01,119
and in order for this entire statement
1783
01:01:01,119 --> 01:01:03,119
to be true since we're using the and
1784
01:01:03,119 --> 01:01:04,400
logical operator
1785
01:01:04,400 --> 01:01:06,640
both conditions must be true in order
1786
01:01:06,640 --> 01:01:07,680
for this statement
1787
01:01:07,680 --> 01:01:10,000
to be true now let's discuss the or
1788
01:01:10,000 --> 01:01:11,119
logical operator
1789
01:01:11,119 --> 01:01:13,200
we can check to see if our temperature
1790
01:01:13,200 --> 01:01:14,160
is below
1791
01:01:14,160 --> 01:01:16,720
or above a certain range so let's write
1792
01:01:16,720 --> 01:01:17,920
else if
1793
01:01:17,920 --> 01:01:21,839
temp is less than zero or
1794
01:01:21,839 --> 01:01:25,359
temp is greater than 30. so that means
1795
01:01:25,359 --> 01:01:26,720
it's going to be either
1796
01:01:26,720 --> 01:01:29,440
very cold outside or very hot outside so
1797
01:01:29,440 --> 01:01:31,920
let's print a message
1798
01:01:31,920 --> 01:01:35,440
the temperature
1799
01:01:35,599 --> 01:01:39,839
is bad today
1800
01:01:39,920 --> 01:01:44,000
print stay
1801
01:01:44,000 --> 01:01:48,240
inside all right let's try this again
1802
01:01:49,040 --> 01:01:50,880
what is the temperature outside it is
1803
01:01:50,880 --> 01:01:53,359
negative 20 degrees outside
1804
01:01:53,359 --> 01:01:55,839
the temperature is bad today stay inside
1805
01:01:55,839 --> 01:01:56,559
so with the
1806
01:01:56,559 --> 01:01:59,039
orological operator as long as one of
1807
01:01:59,039 --> 01:02:00,000
these conditions
1808
01:02:00,000 --> 01:02:02,480
is true then the entire statement is
1809
01:02:02,480 --> 01:02:04,160
true it doesn't matter if one of them is
1810
01:02:04,160 --> 01:02:05,920
false as long as one of them is true
1811
01:02:05,920 --> 01:02:07,119
then the entire statement
1812
01:02:07,119 --> 01:02:09,440
is true last but not least we have the
1813
01:02:09,440 --> 01:02:11,599
not logical operator this works a little
1814
01:02:11,599 --> 01:02:12,720
bit different from the
1815
01:02:12,720 --> 01:02:14,960
and as well as the or logical operator
1816
01:02:14,960 --> 01:02:16,720
because we can check
1817
01:02:16,720 --> 01:02:18,400
one or more conditional statements
1818
01:02:18,400 --> 01:02:20,160
instead of two or more conditional
1819
01:02:20,160 --> 01:02:21,039
statements
1820
01:02:21,039 --> 01:02:23,359
so what the not logical operator will do
1821
01:02:23,359 --> 01:02:24,400
is that it'll take
1822
01:02:24,400 --> 01:02:26,480
a conditional statement if it's true
1823
01:02:26,480 --> 01:02:28,000
it's going to flip it to false
1824
01:02:28,000 --> 01:02:29,520
if it's normally false it's going to
1825
01:02:29,520 --> 01:02:31,920
flip it to true so for this example we
1826
01:02:31,920 --> 01:02:33,599
have another way in which we could write
1827
01:02:33,599 --> 01:02:34,160
this
1828
01:02:34,160 --> 01:02:36,319
although it's a little more complex so
1829
01:02:36,319 --> 01:02:37,440
if our statement
1830
01:02:37,440 --> 01:02:39,520
is true what we can do is that we can
1831
01:02:39,520 --> 01:02:41,200
flip it to false using the
1832
01:02:41,200 --> 01:02:43,520
not logical operator if it's normally
1833
01:02:43,520 --> 01:02:45,119
false we can change it to true
1834
01:02:45,119 --> 01:02:47,599
so what we'll do is surround one or more
1835
01:02:47,599 --> 01:02:48,960
conditional statements with a set of
1836
01:02:48,960 --> 01:02:49,839
parentheses
1837
01:02:49,839 --> 01:02:51,280
and precede everything within the
1838
01:02:51,280 --> 01:02:52,960
parentheses with the not
1839
01:02:52,960 --> 01:02:55,599
logical operator so if the statement is
1840
01:02:55,599 --> 01:02:56,480
normally true
1841
01:02:56,480 --> 01:02:58,400
it's going to become false using the not
1842
01:02:58,400 --> 01:02:59,520
logical operator
1843
01:02:59,520 --> 01:03:01,280
if it's normally false it's going to be
1844
01:03:01,280 --> 01:03:03,039
true then let's do the same with the
1845
01:03:03,039 --> 01:03:04,799
else if statement
1846
01:03:04,799 --> 01:03:06,720
so surround one or more conditional
1847
01:03:06,720 --> 01:03:08,000
statements with the not
1848
01:03:08,000 --> 01:03:10,400
logical operator so now these roles are
1849
01:03:10,400 --> 01:03:12,079
going to be kind of reversed so let's
1850
01:03:12,079 --> 01:03:13,680
try this again what is the temperature
1851
01:03:13,680 --> 01:03:15,359
outside it is negative 20 degrees
1852
01:03:15,359 --> 01:03:16,079
outside
1853
01:03:16,079 --> 01:03:18,240
the temperature is good today go outside
1854
01:03:18,240 --> 01:03:19,760
so what we should probably do
1855
01:03:19,760 --> 01:03:22,720
is kind of reverse these rolls so i'm
1856
01:03:22,720 --> 01:03:23,680
just going to swap
1857
01:03:23,680 --> 01:03:25,359
everything within these if statements
1858
01:03:25,359 --> 01:03:27,680
with each other
1859
01:03:27,680 --> 01:03:29,119
and now this should work the same as it
1860
01:03:29,119 --> 01:03:30,880
did before but we just wrote it a little
1861
01:03:30,880 --> 01:03:31,599
bit different
1862
01:03:31,599 --> 01:03:34,559
what is the temperature outside it is 15
1863
01:03:34,559 --> 01:03:35,440
degrees outside
1864
01:03:35,440 --> 01:03:37,440
15 degrees celsius the temperature is
1865
01:03:37,440 --> 01:03:38,960
good today go outside
1866
01:03:38,960 --> 01:03:41,280
so with the not logical operator you can
1867
01:03:41,280 --> 01:03:42,319
surround one
1868
01:03:42,319 --> 01:03:44,640
or more conditional statements with the
1869
01:03:44,640 --> 01:03:46,480
not logical operator and what this will
1870
01:03:46,480 --> 01:03:47,039
do
1871
01:03:47,039 --> 01:03:49,440
is flip it from being false to true or
1872
01:03:49,440 --> 01:03:50,799
from true to false
1873
01:03:50,799 --> 01:03:52,880
well everybody that's the basics of
1874
01:03:52,880 --> 01:03:55,119
logical operators in python if you would
1875
01:03:55,119 --> 01:03:57,280
like a copy of all this code i will post
1876
01:03:57,280 --> 01:03:58,160
this in the comments
1877
01:03:58,160 --> 01:04:00,799
down below but yeah that's the basics of
1878
01:04:00,799 --> 01:04:02,079
logical operators
1879
01:04:02,079 --> 01:04:05,280
in python hey what's going on everybody
1880
01:04:05,280 --> 01:04:07,119
it's bro hope you're doing well and in
1881
01:04:07,119 --> 01:04:08,720
this video i'm going to explain while
1882
01:04:08,720 --> 01:04:10,799
loops in python so sit back
1883
01:04:10,799 --> 01:04:14,640
relax and enjoy the show
1884
01:04:15,119 --> 01:04:17,760
so while loops a while loop is a
1885
01:04:17,760 --> 01:04:19,839
statement that will execute its block of
1886
01:04:19,839 --> 01:04:20,319
code
1887
01:04:20,319 --> 01:04:23,119
as long as its condition remains true
1888
01:04:23,119 --> 01:04:25,200
later on in this video for this example
1889
01:04:25,200 --> 01:04:26,000
we're going to create
1890
01:04:26,000 --> 01:04:28,400
a program where we will prompt a user to
1891
01:04:28,400 --> 01:04:29,520
enter their name
1892
01:04:29,520 --> 01:04:31,760
if they attempt to skip that prompt then
1893
01:04:31,760 --> 01:04:33,839
we will continually ask the user to
1894
01:04:33,839 --> 01:04:34,799
enter their name
1895
01:04:34,799 --> 01:04:36,160
and they can't continue on with the rest
1896
01:04:36,160 --> 01:04:37,920
of the program until they do so the key
1897
01:04:37,920 --> 01:04:39,680
thing to keep in mind with while loops
1898
01:04:39,680 --> 01:04:41,039
is that you'll want some way to
1899
01:04:41,039 --> 01:04:42,880
eventually escape the while loop if you
1900
01:04:42,880 --> 01:04:44,400
don't have any way to escape the while
1901
01:04:44,400 --> 01:04:46,000
loop that's referred to as an
1902
01:04:46,000 --> 01:04:48,240
infinite loop so let's create an example
1903
01:04:48,240 --> 01:04:50,319
of an infinite loop because it's fun
1904
01:04:50,319 --> 01:04:52,079
so to create a while loop type while and
1905
01:04:52,079 --> 01:04:53,359
then some sort of condition
1906
01:04:53,359 --> 01:04:55,680
so for our condition let's say while one
1907
01:04:55,680 --> 01:04:56,640
is equal to one
1908
01:04:56,640 --> 01:04:59,039
so this will always be true now what do
1909
01:04:59,039 --> 01:05:00,000
we want to do
1910
01:05:00,000 --> 01:05:02,160
when we execute this block of code let's
1911
01:05:02,160 --> 01:05:03,119
print something
1912
01:05:03,119 --> 01:05:06,400
print help i'm
1913
01:05:06,400 --> 01:05:10,480
stuck in a loop all right so let's try
1914
01:05:10,480 --> 01:05:10,880
this
1915
01:05:10,880 --> 01:05:13,119
while one is equal to one print this
1916
01:05:13,119 --> 01:05:15,599
statement
1917
01:05:15,599 --> 01:05:17,520
and we will just continue on and on
1918
01:05:17,520 --> 01:05:19,440
within this while loop because we have
1919
01:05:19,440 --> 01:05:21,680
no way to escape this while loop
1920
01:05:21,680 --> 01:05:24,079
so now let's use this concept and write
1921
01:05:24,079 --> 01:05:26,400
a program where we will prompt a user to
1922
01:05:26,400 --> 01:05:27,520
type in their name
1923
01:05:27,520 --> 01:05:29,280
if they don't type in anything then we
1924
01:05:29,280 --> 01:05:31,119
will continue to prompt them to type in
1925
01:05:31,119 --> 01:05:32,240
at least something
1926
01:05:32,240 --> 01:05:35,599
so let's say name is equal to
1927
01:05:35,599 --> 01:05:36,960
and then to just keep this blank we'll
1928
01:05:36,960 --> 01:05:39,920
just set this to a set of quotes
1929
01:05:39,920 --> 01:05:41,520
and then let's write a while loop here
1930
01:05:41,520 --> 01:05:43,119
while name
1931
01:05:43,119 --> 01:05:45,599
and let's check the length of name if
1932
01:05:45,599 --> 01:05:46,319
the length
1933
01:05:46,319 --> 01:05:49,359
of our name is equal
1934
01:05:49,359 --> 01:05:52,319
to zero then we will ask the user to
1935
01:05:52,319 --> 01:05:53,520
type in their name
1936
01:05:53,520 --> 01:05:57,280
name equals input
1937
01:05:57,280 --> 01:06:00,319
enter your name
1938
01:06:00,319 --> 01:06:02,160
so then once we escape the while loop
1939
01:06:02,160 --> 01:06:04,160
let's print something
1940
01:06:04,160 --> 01:06:07,280
print hello plus
1941
01:06:07,280 --> 01:06:10,720
name whoops hello plus name
1942
01:06:10,720 --> 01:06:12,799
alright so when i run this we are giving
1943
01:06:12,799 --> 01:06:14,880
the user a way to escape the while loop
1944
01:06:14,880 --> 01:06:16,400
they have to type in something for their
1945
01:06:16,400 --> 01:06:16,960
name
1946
01:06:16,960 --> 01:06:19,920
as long as the length of my name is
1947
01:06:19,920 --> 01:06:20,880
equal to zero
1948
01:06:20,880 --> 01:06:23,440
keep on printing this prompt enter your
1949
01:06:23,440 --> 01:06:23,920
name
1950
01:06:23,920 --> 01:06:25,359
so i'm just going to hit enter a bunch
1951
01:06:25,359 --> 01:06:26,960
of times enter your name
1952
01:06:26,960 --> 01:06:30,240
no enter your name no enter your name no
1953
01:06:30,240 --> 01:06:32,160
so we are stuck within this loop until
1954
01:06:32,160 --> 01:06:34,000
we enter something for our name
1955
01:06:34,000 --> 01:06:36,319
so this time let me type in a name then
1956
01:06:36,319 --> 01:06:37,280
hit enter
1957
01:06:37,280 --> 01:06:39,599
so therefore our condition is now false
1958
01:06:39,599 --> 01:06:40,400
the length
1959
01:06:40,400 --> 01:06:43,520
of my name is now one two three so
1960
01:06:43,520 --> 01:06:46,000
three is not equal to zero therefore our
1961
01:06:46,000 --> 01:06:48,240
while statement our while loop is false
1962
01:06:48,240 --> 01:06:49,520
and we continue on with the rest of the
1963
01:06:49,520 --> 01:06:51,440
program there are a few variations in
1964
01:06:51,440 --> 01:06:53,359
which you could write the same program
1965
01:06:53,359 --> 01:06:54,000
so
1966
01:06:54,000 --> 01:06:55,839
let's write this a different way what we
1967
01:06:55,839 --> 01:06:57,039
could do is say
1968
01:06:57,039 --> 01:07:00,480
name is equal to none while
1969
01:07:00,480 --> 01:07:03,440
not name and this will pretty much do
1970
01:07:03,440 --> 01:07:05,440
the same thing it's just another way of
1971
01:07:05,440 --> 01:07:06,720
writing this
1972
01:07:06,720 --> 01:07:08,400
well everybody that's the basics of
1973
01:07:08,400 --> 01:07:10,559
while loops a while loop is a statement
1974
01:07:10,559 --> 01:07:11,599
that will execute
1975
01:07:11,599 --> 01:07:13,920
its block of code as long as its
1976
01:07:13,920 --> 01:07:15,599
condition remains true
1977
01:07:15,599 --> 01:07:17,599
for our example we asked the user to
1978
01:07:17,599 --> 01:07:18,720
type in their name
1979
01:07:18,720 --> 01:07:20,559
if they were to leave it empty or blank
1980
01:07:20,559 --> 01:07:22,480
we would keep on asking the user to type
1981
01:07:22,480 --> 01:07:23,440
in their name
1982
01:07:23,440 --> 01:07:25,039
so if you would like a copy of this code
1983
01:07:25,039 --> 01:07:26,799
i will post this in the comments section
1984
01:07:26,799 --> 01:07:27,520
down below
1985
01:07:27,520 --> 01:07:29,440
but yeah that's the basics of while
1986
01:07:29,440 --> 01:07:31,599
loops in python
1987
01:07:31,599 --> 01:07:33,760
hey what's going on everybody it's bro
1988
01:07:33,760 --> 01:07:35,359
hope you're doing well and in this video
1989
01:07:35,359 --> 01:07:37,520
i'm going to explain for loops in python
1990
01:07:37,520 --> 01:07:38,799
and at the end of this video we're going
1991
01:07:38,799 --> 01:07:40,880
to create a countdown timer so
1992
01:07:40,880 --> 01:07:45,359
sit back relax and enjoy the show
1993
01:07:45,359 --> 01:07:47,359
all right guys and gals let's talk about
1994
01:07:47,359 --> 01:07:49,920
for loops a for loop is a statement that
1995
01:07:49,920 --> 01:07:52,240
will execute its block of code a
1996
01:07:52,240 --> 01:07:54,880
limited amount of times it's similar but
1997
01:07:54,880 --> 01:07:56,480
different from a while loop because a
1998
01:07:56,480 --> 01:07:57,359
while loop
1999
01:07:57,359 --> 01:08:00,640
could iterate an unlimited or infinite
2000
01:08:00,640 --> 01:08:02,319
amount of times depending on the
2001
01:08:02,319 --> 01:08:04,240
condition a for loop
2002
01:08:04,240 --> 01:08:06,480
will only iterate a limited amount of
2003
01:08:06,480 --> 01:08:08,799
times and before we start executing
2004
01:08:08,799 --> 01:08:10,319
its block of code we already know how
2005
01:08:10,319 --> 01:08:11,760
many times we're going to repeat this
2006
01:08:11,760 --> 01:08:12,799
block of code
2007
01:08:12,799 --> 01:08:14,480
so for this example let's create a for
2008
01:08:14,480 --> 01:08:16,319
loop that will simply count it to 10
2009
01:08:16,319 --> 01:08:17,759
and then we'll create a few more
2010
01:08:17,759 --> 01:08:19,439
sophisticated examples
2011
01:08:19,439 --> 01:08:20,960
so to create a for loop that will count
2012
01:08:20,960 --> 01:08:22,719
up to 10 this is what we'll type
2013
01:08:22,719 --> 01:08:24,479
four and then we need some sort of
2014
01:08:24,479 --> 01:08:26,640
counter people usually either write
2015
01:08:26,640 --> 01:08:29,359
index or they shorten it to just i i for
2016
01:08:29,359 --> 01:08:30,158
index
2017
01:08:30,158 --> 01:08:34,000
four i in and we can set a range range
2018
01:08:34,000 --> 01:08:36,799
ten so we will execute this for loop ten
2019
01:08:36,799 --> 01:08:37,279
times
2020
01:08:37,279 --> 01:08:39,198
and to finish this for loop just add a
2021
01:08:39,198 --> 01:08:40,319
colon at the end
2022
01:08:40,319 --> 01:08:43,279
so the next line or lines will be
2023
01:08:43,279 --> 01:08:45,198
indented so that's the block of code for
2024
01:08:45,198 --> 01:08:46,000
the for loop
2025
01:08:46,000 --> 01:08:47,359
what do we want to do during each
2026
01:08:47,359 --> 01:08:49,359
iteration let's just print what
2027
01:08:49,359 --> 01:08:51,600
i is and let's take a look to see what
2028
01:08:51,600 --> 01:08:52,880
would happen
2029
01:08:52,880 --> 01:08:55,359
so we executed this for loop ten times
2030
01:08:55,359 --> 01:08:57,279
except with computers computers always
2031
01:08:57,279 --> 01:08:58,238
start with zero
2032
01:08:58,238 --> 01:09:00,960
so when we said four i in range ten
2033
01:09:00,960 --> 01:09:02,080
we're counting numbers
2034
01:09:02,080 --> 01:09:04,799
zero through nine so it's as if this
2035
01:09:04,799 --> 01:09:05,279
number
2036
01:09:05,279 --> 01:09:07,520
is exclusive so there's two ways we can
2037
01:09:07,520 --> 01:09:09,439
change this we could say range ten
2038
01:09:09,439 --> 01:09:12,080
plus one or what i would do is say print
2039
01:09:12,080 --> 01:09:12,799
i
2040
01:09:12,799 --> 01:09:15,279
plus one so this will give us numbers
2041
01:09:15,279 --> 01:09:15,920
one
2042
01:09:15,920 --> 01:09:18,158
through ten so that's a for loop it will
2043
01:09:18,158 --> 01:09:20,238
execute a limited amount of times
2044
01:09:20,238 --> 01:09:22,479
this time let's count a range between
2045
01:09:22,479 --> 01:09:23,439
two numbers
2046
01:09:23,439 --> 01:09:25,600
not necessarily 0 to 10 but something
2047
01:09:25,600 --> 01:09:27,679
else so let's say 4i
2048
01:09:27,679 --> 01:09:31,279
in range maybe the numbers 50 through
2049
01:09:31,279 --> 01:09:31,920
100
2050
01:09:31,920 --> 01:09:34,158
so within my range function i will pass
2051
01:09:34,158 --> 01:09:35,439
in two numbers
2052
01:09:35,439 --> 01:09:37,198
the first number will be the starting
2053
01:09:37,198 --> 01:09:38,880
point and the second number is the
2054
01:09:38,880 --> 01:09:39,759
ending point
2055
01:09:39,759 --> 01:09:41,920
this first number is inclusive and the
2056
01:09:41,920 --> 01:09:43,920
second number is exclusive
2057
01:09:43,920 --> 01:09:46,880
so let's print this print whatever i is
2058
01:09:46,880 --> 01:09:48,238
so this will count
2059
01:09:48,238 --> 01:09:51,359
50 through 99 actually if you wanted to
2060
01:09:51,359 --> 01:09:53,198
include this last number since it's
2061
01:09:53,198 --> 01:09:55,360
exclusive what we could do is just add
2062
01:09:55,360 --> 01:09:56,960
one to the end so this would give
2063
01:09:56,960 --> 01:10:00,320
us 50 through 100 except that
2064
01:10:00,320 --> 01:10:02,800
this will iterate 51 times since we're
2065
01:10:02,800 --> 01:10:04,000
counting 50.
2066
01:10:04,000 --> 01:10:05,360
so you just have to pay attention to
2067
01:10:05,360 --> 01:10:06,800
your range and the numbers that it's
2068
01:10:06,800 --> 01:10:08,480
counting now one thing that you can do
2069
01:10:08,480 --> 01:10:10,239
with the range function is that you can
2070
01:10:10,239 --> 01:10:12,000
add a third argument and this will
2071
01:10:12,000 --> 01:10:13,360
function as the step
2072
01:10:13,360 --> 01:10:15,600
how much you want to count up or down by
2073
01:10:15,600 --> 01:10:17,600
so this time let's count up by two
2074
01:10:17,600 --> 01:10:20,640
so i'll add comma two we're passing in a
2075
01:10:20,640 --> 01:10:22,080
third argument this time
2076
01:10:22,080 --> 01:10:24,960
so this program will now count up by two
2077
01:10:24,960 --> 01:10:27,120
starting at fifty and go all the way
2078
01:10:27,120 --> 01:10:30,320
to one hundred a benefit of for loops is
2079
01:10:30,320 --> 01:10:32,080
that we can iterate through anything
2080
01:10:32,080 --> 01:10:33,040
that is considered
2081
01:10:33,040 --> 01:10:35,679
iterable this could include a string the
2082
01:10:35,679 --> 01:10:36,719
letters in a string
2083
01:10:36,719 --> 01:10:39,280
or any sort of collection so this time
2084
01:10:39,280 --> 01:10:40,800
let's create a for loop that will
2085
01:10:40,800 --> 01:10:41,360
iterate
2086
01:10:41,360 --> 01:10:44,320
once through each letter in maybe a name
2087
01:10:44,320 --> 01:10:44,880
so for
2088
01:10:44,880 --> 01:10:47,840
i in and let's type a name here type in
2089
01:10:47,840 --> 01:10:49,360
whatever your first name and last name
2090
01:10:49,360 --> 01:10:50,640
is
2091
01:10:50,640 --> 01:10:53,760
and then we will print whatever i is
2092
01:10:53,760 --> 01:10:56,640
for i in whatever your name is print
2093
01:10:56,640 --> 01:10:57,520
each letter
2094
01:10:57,520 --> 01:11:00,480
in the string so when i run this each
2095
01:11:00,480 --> 01:11:01,040
letter
2096
01:11:01,040 --> 01:11:03,360
within my name will be printed to a new
2097
01:11:03,360 --> 01:11:04,239
line
2098
01:11:04,239 --> 01:11:06,320
all right people for my last trick we're
2099
01:11:06,320 --> 01:11:07,840
going to create a program
2100
01:11:07,840 --> 01:11:09,760
where we will simulate a countdown
2101
01:11:09,760 --> 01:11:11,760
starting at 10 and count down to zero
2102
01:11:11,760 --> 01:11:13,760
and then maybe once we reach zero we can
2103
01:11:13,760 --> 01:11:15,280
print something such as
2104
01:11:15,280 --> 01:11:17,440
happy new year so to create this program
2105
01:11:17,440 --> 01:11:19,280
we'll need an import so at the top
2106
01:11:19,280 --> 01:11:21,199
we're going to import the time module
2107
01:11:21,199 --> 01:11:22,560
because we'll be waiting
2108
01:11:22,560 --> 01:11:24,640
one second after each iteration of this
2109
01:11:24,640 --> 01:11:25,600
for loop
2110
01:11:25,600 --> 01:11:27,840
so to create this program let's say four
2111
01:11:27,840 --> 01:11:29,600
you could say i but you can really write
2112
01:11:29,600 --> 01:11:30,719
anything here and
2113
01:11:30,719 --> 01:11:32,800
to demonstrate that let's say four
2114
01:11:32,800 --> 01:11:35,040
seconds four seconds
2115
01:11:35,040 --> 01:11:38,080
in range so the starting point is going
2116
01:11:38,080 --> 01:11:38,960
to be ten
2117
01:11:38,960 --> 01:11:41,120
the ending point will be zero and we'll
2118
01:11:41,120 --> 01:11:42,159
add a step
2119
01:11:42,159 --> 01:11:44,320
what do we want to count up or down by
2120
01:11:44,320 --> 01:11:46,000
let's set this to negative one so this
2121
01:11:46,000 --> 01:11:47,120
will be a countdown
2122
01:11:47,120 --> 01:11:49,360
starting at ten and ending at zero so
2123
01:11:49,360 --> 01:11:51,440
after each iteration
2124
01:11:51,440 --> 01:11:54,320
let's print whatever i is or in this
2125
01:11:54,320 --> 01:11:55,040
case
2126
01:11:55,040 --> 01:11:58,000
seconds and then we can sleep have our
2127
01:11:58,000 --> 01:11:58,800
thread sleep
2128
01:11:58,800 --> 01:12:01,280
for a number of seconds too so after we
2129
01:12:01,280 --> 01:12:03,120
print seconds
2130
01:12:03,120 --> 01:12:06,400
type time dot sleep and pass in
2131
01:12:06,400 --> 01:12:08,159
how long you want to sleep for i would
2132
01:12:08,159 --> 01:12:10,159
like to sleep for one second
2133
01:12:10,159 --> 01:12:12,400
and then at the end let's print happy
2134
01:12:12,400 --> 01:12:14,159
new year make sure this isn't within the
2135
01:12:14,159 --> 01:12:15,120
for loop
2136
01:12:15,120 --> 01:12:18,719
print happy new year and well
2137
01:12:18,719 --> 01:12:21,199
that's it let's test this so we'll begin
2138
01:12:21,199 --> 01:12:23,120
at 10 and count down to zero and you can
2139
01:12:23,120 --> 01:12:25,120
see that my thread that is running this
2140
01:12:25,120 --> 01:12:25,760
program
2141
01:12:25,760 --> 01:12:28,159
is sleeping for one second after each
2142
01:12:28,159 --> 01:12:29,920
iteration of this for loop
2143
01:12:29,920 --> 01:12:32,159
then once we reach zero it will display
2144
01:12:32,159 --> 01:12:33,520
happy new year
2145
01:12:33,520 --> 01:12:35,760
well everybody that's the basics of for
2146
01:12:35,760 --> 01:12:37,760
loops a for loop is a statement
2147
01:12:37,760 --> 01:12:40,239
that will execute its block of code a
2148
01:12:40,239 --> 01:12:41,840
limited amount of times
2149
01:12:41,840 --> 01:12:43,520
it's different from a while loop because
2150
01:12:43,520 --> 01:12:45,440
a while loop could execute
2151
01:12:45,440 --> 01:12:47,920
an infinite or unlimited amount of times
2152
01:12:47,920 --> 01:12:50,239
with for loops they execute limited
2153
01:12:50,239 --> 01:12:51,280
amounts of times
2154
01:12:51,280 --> 01:12:53,280
and before we enter the for loop we
2155
01:12:53,280 --> 01:12:54,640
already know how many times we're going
2156
01:12:54,640 --> 01:12:56,400
to iterate this for loop
2157
01:12:56,400 --> 01:12:58,159
if you'd like a copy of all this code i
2158
01:12:58,159 --> 01:12:59,679
will post this in the comments down
2159
01:12:59,679 --> 01:13:00,400
below
2160
01:13:00,400 --> 01:13:02,840
but yeah that's how for loops work in
2161
01:13:02,840 --> 01:13:04,400
python
2162
01:13:04,400 --> 01:13:06,239
hey what's going on everybody it's you
2163
01:13:06,239 --> 01:13:07,920
bro hope you're doing well and in this
2164
01:13:07,920 --> 01:13:09,760
video i'm going to explain nested loops
2165
01:13:09,760 --> 01:13:10,880
in python so
2166
01:13:10,880 --> 01:13:15,440
sit back relax and enjoy the show
2167
01:13:15,520 --> 01:13:17,280
hey all you people let's talk about
2168
01:13:17,280 --> 01:13:19,840
nested loops a nested loop is a general
2169
01:13:19,840 --> 01:13:20,960
concept of having
2170
01:13:20,960 --> 01:13:22,640
one loop inside of another loop and
2171
01:13:22,640 --> 01:13:24,000
that's really it it doesn't matter if
2172
01:13:24,000 --> 01:13:26,000
it's a for loop or a while loop
2173
01:13:26,000 --> 01:13:28,560
so the inner loop will finish all of its
2174
01:13:28,560 --> 01:13:29,280
iterations
2175
01:13:29,280 --> 01:13:31,679
before we finish one iteration of the
2176
01:13:31,679 --> 01:13:32,480
outer loop
2177
01:13:32,480 --> 01:13:34,000
and to best demonstrate this we're going
2178
01:13:34,000 --> 01:13:36,239
to create a program where we will draw a
2179
01:13:36,239 --> 01:13:38,320
rectangle made out of a certain symbol
2180
01:13:38,320 --> 01:13:39,199
that we choose
2181
01:13:39,199 --> 01:13:41,440
we need to set a width and a height and
2182
01:13:41,440 --> 01:13:43,440
to best do this we'll need to use a
2183
01:13:43,440 --> 01:13:45,679
nested loop we'll create a few prompts
2184
01:13:45,679 --> 01:13:46,560
one for rows
2185
01:13:46,560 --> 01:13:48,320
columns and a symbol that we want to
2186
01:13:48,320 --> 01:13:49,600
make a rectangle of
2187
01:13:49,600 --> 01:13:52,880
so let's begin with rows rows equals
2188
01:13:52,880 --> 01:13:56,480
input how many
2189
01:13:56,480 --> 01:14:00,159
rows and we'll need to cast this as an
2190
01:14:00,159 --> 01:14:01,440
integer data type
2191
01:14:01,440 --> 01:14:04,000
because we're working with numbers okay
2192
01:14:04,000 --> 01:14:07,760
let's do the same thing for columns
2193
01:14:08,159 --> 01:14:10,159
so replace rows with columns for this
2194
01:14:10,159 --> 01:14:12,560
next line
2195
01:14:12,560 --> 01:14:15,600
input how many columns
2196
01:14:15,600 --> 01:14:18,159
and we'll create a symbol variable
2197
01:14:18,159 --> 01:14:18,960
symbol
2198
01:14:18,960 --> 01:14:21,679
equals input and this we don't have to
2199
01:14:21,679 --> 01:14:23,600
cast
2200
01:14:23,600 --> 01:14:26,840
enter a symbol to
2201
01:14:26,840 --> 01:14:29,679
use now it's time for the nested loops
2202
01:14:29,679 --> 01:14:31,679
we're going to create an outer for loop
2203
01:14:31,679 --> 01:14:33,360
as well as an inner for loop
2204
01:14:33,360 --> 01:14:35,280
the outer for loop will be in charge of
2205
01:14:35,280 --> 01:14:37,440
the rows the inner for loop will be in
2206
01:14:37,440 --> 01:14:38,719
charge of the columns
2207
01:14:38,719 --> 01:14:41,120
so let's begin by creating our outer
2208
01:14:41,120 --> 01:14:42,080
loop so
2209
01:14:42,080 --> 01:14:46,320
we'll write 4 i in range
2210
01:14:46,320 --> 01:14:49,440
rows we would like our outer for loop to
2211
01:14:49,440 --> 01:14:50,800
iterate as many times
2212
01:14:50,800 --> 01:14:52,640
as we have rows and we're going to
2213
01:14:52,640 --> 01:14:54,400
create an inner for loop that will
2214
01:14:54,400 --> 01:14:56,960
iterate as many times as we have columns
2215
01:14:56,960 --> 01:14:58,800
so within the block of code for the
2216
01:14:58,800 --> 01:15:00,480
outer for loop we're going to create
2217
01:15:00,480 --> 01:15:01,840
another for loop
2218
01:15:01,840 --> 01:15:04,239
this time it'll be four and a common
2219
01:15:04,239 --> 01:15:05,040
convention
2220
01:15:05,040 --> 01:15:07,920
for inner loops is to write j as an
2221
01:15:07,920 --> 01:15:09,120
index because
2222
01:15:09,120 --> 01:15:12,719
j comes after i i guess so for j
2223
01:15:12,719 --> 01:15:17,920
in range columns
2224
01:15:18,159 --> 01:15:21,040
so our inner for loop will iterate for
2225
01:15:21,040 --> 01:15:22,080
as many times
2226
01:15:22,080 --> 01:15:24,800
as we have columns all we'll do within
2227
01:15:24,800 --> 01:15:25,120
the
2228
01:15:25,120 --> 01:15:27,760
inner for loop is print our symbol that
2229
01:15:27,760 --> 01:15:28,719
we have
2230
01:15:28,719 --> 01:15:31,120
except we'll have one issue using a
2231
01:15:31,120 --> 01:15:32,480
print statement
2232
01:15:32,480 --> 01:15:34,560
after we use the print statement we will
2233
01:15:34,560 --> 01:15:36,800
enter a new line character and move down
2234
01:15:36,800 --> 01:15:37,679
to the next line
2235
01:15:37,679 --> 01:15:39,840
we can actually prevent that by adding
2236
01:15:39,840 --> 01:15:40,880
comma
2237
01:15:40,880 --> 01:15:45,040
and equals quotes so after using a print
2238
01:15:45,040 --> 01:15:45,520
statement
2239
01:15:45,520 --> 01:15:47,679
this will prevent our cursor from moving
2240
01:15:47,679 --> 01:15:49,280
down to the next line
2241
01:15:49,280 --> 01:15:52,080
so pay attention to the indentation too
2242
01:15:52,080 --> 01:15:53,600
so there's kind of two levels of
2243
01:15:53,600 --> 01:15:54,400
indentation
2244
01:15:54,400 --> 01:15:56,320
we're currently within the inner for
2245
01:15:56,320 --> 01:15:58,159
loop so that's pretty much it for the
2246
01:15:58,159 --> 01:16:00,000
inner for loop now we're going to
2247
01:16:00,000 --> 01:16:02,800
print a new line once we exit the inner
2248
01:16:02,800 --> 01:16:03,760
for loop
2249
01:16:03,760 --> 01:16:05,920
and you can see that the indentation is
2250
01:16:05,920 --> 01:16:07,040
on
2251
01:16:07,040 --> 01:16:08,880
this line right here we're within the
2252
01:16:08,880 --> 01:16:10,239
outer for loop now
2253
01:16:10,239 --> 01:16:12,080
and that's pretty much it so let's try
2254
01:16:12,080 --> 01:16:13,360
this program
2255
01:16:13,360 --> 01:16:16,480
how many rows let's say five rows how
2256
01:16:16,480 --> 01:16:19,120
many columns perhaps six
2257
01:16:19,120 --> 01:16:21,040
what symbol do we want to use let's say
2258
01:16:21,040 --> 01:16:22,480
the dollar sign
2259
01:16:22,480 --> 01:16:25,760
and let's try it there we go there's our
2260
01:16:25,760 --> 01:16:26,719
rectangle
2261
01:16:26,719 --> 01:16:30,000
so we have let's see six columns one
2262
01:16:30,000 --> 01:16:32,800
two three four five six and five rows
2263
01:16:32,800 --> 01:16:33,199
one
2264
01:16:33,199 --> 01:16:36,719
two three four five in summary
2265
01:16:36,719 --> 01:16:38,640
a nested loop is really just this
2266
01:16:38,640 --> 01:16:39,840
concept of having
2267
01:16:39,840 --> 01:16:41,440
one loop inside of another loop it
2268
01:16:41,440 --> 01:16:43,440
doesn't matter if it's a while loop or a
2269
01:16:43,440 --> 01:16:45,600
for a loop it's really situational
2270
01:16:45,600 --> 01:16:47,520
the inner loop will finish all of its
2271
01:16:47,520 --> 01:16:49,360
iterations before finishing
2272
01:16:49,360 --> 01:16:51,440
one iteration of the outer loop so what
2273
01:16:51,440 --> 01:16:53,440
we did to demonstrate that is that we
2274
01:16:53,440 --> 01:16:54,159
had
2275
01:16:54,159 --> 01:16:56,400
the outer loop in charge the rows and an
2276
01:16:56,400 --> 01:16:58,000
inner loop in charge of the columns for
2277
01:16:58,000 --> 01:16:59,600
our rectangle program
2278
01:16:59,600 --> 01:17:01,280
so if you would like a copy of this
2279
01:17:01,280 --> 01:17:03,280
program i will post this in the comment
2280
01:17:03,280 --> 01:17:04,560
section down below
2281
01:17:04,560 --> 01:17:06,800
but yeah that's the basics of nested
2282
01:17:06,800 --> 01:17:09,120
loops in python
2283
01:17:09,120 --> 01:17:11,520
hey what's going on everybody it's bro
2284
01:17:11,520 --> 01:17:12,480
hope you're doing well
2285
01:17:12,480 --> 01:17:14,320
and in this video i'm going to explain
2286
01:17:14,320 --> 01:17:16,000
loop control statements in python
2287
01:17:16,000 --> 01:17:21,199
so sit back relax and enjoy the show
2288
01:17:21,360 --> 01:17:23,040
all right ladies and gentlemen let's
2289
01:17:23,040 --> 01:17:24,960
talk about loop control statements
2290
01:17:24,960 --> 01:17:26,800
these are used to change a loop's
2291
01:17:26,800 --> 01:17:29,679
execution from its normal sequence
2292
01:17:29,679 --> 01:17:30,800
and there are three we're going to
2293
01:17:30,800 --> 01:17:32,800
discuss break continue
2294
01:17:32,800 --> 01:17:35,360
and pass let's begin with break break is
2295
01:17:35,360 --> 01:17:37,600
used to terminate the loop entirely
2296
01:17:37,600 --> 01:17:39,360
now here's a good example of where a
2297
01:17:39,360 --> 01:17:40,640
break would be useful
2298
01:17:40,640 --> 01:17:43,520
let's say while true we're going to
2299
01:17:43,520 --> 01:17:44,640
continually ask
2300
01:17:44,640 --> 01:17:46,719
for somebody's name if they don't type
2301
01:17:46,719 --> 01:17:48,400
in anything then we'll continue the
2302
01:17:48,400 --> 01:17:49,920
while loop over and over again
2303
01:17:49,920 --> 01:17:55,040
so let's say name equals input
2304
01:17:55,199 --> 01:17:58,960
enter your name now
2305
01:17:58,960 --> 01:18:02,480
if name does not equal
2306
01:18:02,480 --> 01:18:04,080
a set of quotes that means they don't
2307
01:18:04,080 --> 01:18:05,840
type in anything then
2308
01:18:05,840 --> 01:18:08,000
we will break and break out of this
2309
01:18:08,000 --> 01:18:10,400
while loop and let's test it
2310
01:18:10,400 --> 01:18:12,800
enter your name nah i don't think so i'm
2311
01:18:12,800 --> 01:18:14,480
too lazy to enter my name
2312
01:18:14,480 --> 01:18:15,920
all right i can see that i'm not going
2313
01:18:15,920 --> 01:18:17,520
to escape this while loop so i'm going
2314
01:18:17,520 --> 01:18:19,600
to type in my name because i give up
2315
01:18:19,600 --> 01:18:22,239
and hit enter therefore i will break and
2316
01:18:22,239 --> 01:18:23,760
exit out of this while loop
2317
01:18:23,760 --> 01:18:26,080
so a break is used to terminate the loop
2318
01:18:26,080 --> 01:18:28,000
entirely when it's encountered
2319
01:18:28,000 --> 01:18:30,480
next we have continue continue skips to
2320
01:18:30,480 --> 01:18:32,480
the next iteration of the loop
2321
01:18:32,480 --> 01:18:35,040
now let's say we have a phone number
2322
01:18:35,040 --> 01:18:35,840
phone
2323
01:18:35,840 --> 01:18:38,400
underscore number and i will set the
2324
01:18:38,400 --> 01:18:39,199
sequel to
2325
01:18:39,199 --> 01:18:40,480
let's say a random phone number
2326
01:18:40,480 --> 01:18:45,679
including dashes 123-456-7890
2327
01:18:45,679 --> 01:18:47,679
what i would like to do is to display
2328
01:18:47,679 --> 01:18:49,760
this number within the console window
2329
01:18:49,760 --> 01:18:51,360
without these dashes
2330
01:18:51,360 --> 01:18:53,520
this is how to do that using a continue
2331
01:18:53,520 --> 01:18:54,880
control statement
2332
01:18:54,880 --> 01:19:00,159
for i in phone number
2333
01:19:00,640 --> 01:19:03,199
so for each character within our string
2334
01:19:03,199 --> 01:19:04,000
of phone number
2335
01:19:04,000 --> 01:19:06,239
what we'll do is check to see if we
2336
01:19:06,239 --> 01:19:08,560
encounter a dash character
2337
01:19:08,560 --> 01:19:11,679
if i i is our index is
2338
01:19:11,679 --> 01:19:15,679
equal to a dash then we want to
2339
01:19:15,679 --> 01:19:18,400
continue and skip over this iteration of
2340
01:19:18,400 --> 01:19:19,520
the loop
2341
01:19:19,520 --> 01:19:23,120
so continue
2342
01:19:23,120 --> 01:19:26,320
then i would like to print
2343
01:19:26,320 --> 01:19:29,360
whatever our indexes whatever character
2344
01:19:29,360 --> 01:19:30,640
we're working on
2345
01:19:30,640 --> 01:19:32,640
so if i were to run this as it is it's
2346
01:19:32,640 --> 01:19:34,400
going to print each digit
2347
01:19:34,400 --> 01:19:37,199
on a new line so with print statements
2348
01:19:37,199 --> 01:19:39,280
they will add a new line character to
2349
01:19:39,280 --> 01:19:40,719
the end of your string
2350
01:19:40,719 --> 01:19:42,080
and you can actually change that by
2351
01:19:42,080 --> 01:19:44,480
adding to the end comma
2352
01:19:44,480 --> 01:19:47,440
end equals and then a set of quotes so
2353
01:19:47,440 --> 01:19:49,440
this will print my phone number without
2354
01:19:49,440 --> 01:19:50,480
any dashes
2355
01:19:50,480 --> 01:19:51,920
one two three four five six seven eight
2356
01:19:51,920 --> 01:19:54,239
nine zero so that's a use of
2357
01:19:54,239 --> 01:19:56,480
the continue control statement it skips
2358
01:19:56,480 --> 01:19:58,719
to the next iteration of the loop
2359
01:19:58,719 --> 01:20:01,199
last but not least we have pass pass
2360
01:20:01,199 --> 01:20:01,920
does nothing
2361
01:20:01,920 --> 01:20:04,080
it acts as a placeholder let's say that
2362
01:20:04,080 --> 01:20:05,760
i would like to print the numbers
2363
01:20:05,760 --> 01:20:09,360
1 through 20 using a for loop for i
2364
01:20:09,360 --> 01:20:13,280
in range 1 through 21 and remember that
2365
01:20:13,280 --> 01:20:14,560
the second digit is
2366
01:20:14,560 --> 01:20:16,880
exclusive so this will iterate once
2367
01:20:16,880 --> 01:20:18,080
through the digits of 1
2368
01:20:18,080 --> 01:20:20,320
through 20. let's pretend that i'm
2369
01:20:20,320 --> 01:20:22,320
highly superstitious and i don't want to
2370
01:20:22,320 --> 01:20:22,639
print
2371
01:20:22,639 --> 01:20:24,960
the number 13 because 13 is considered
2372
01:20:24,960 --> 01:20:26,080
an unlucky number
2373
01:20:26,080 --> 01:20:29,760
if i is equal to 13
2374
01:20:29,760 --> 01:20:31,760
what we'll do is pass and pass acts as a
2375
01:20:31,760 --> 01:20:34,159
placeholder and it won't do anything
2376
01:20:34,159 --> 01:20:37,360
else print
2377
01:20:37,360 --> 01:20:40,560
i that's one i there we go
2378
01:20:40,560 --> 01:20:42,639
this will print the numbers 1 through 20
2379
01:20:42,639 --> 01:20:44,800
and skip 13 because we used
2380
01:20:44,800 --> 01:20:47,040
the past control statement which does
2381
01:20:47,040 --> 01:20:49,280
nothing it acts as a placeholder
2382
01:20:49,280 --> 01:20:51,280
so everybody those are loop control
2383
01:20:51,280 --> 01:20:52,639
statements they change
2384
01:20:52,639 --> 01:20:54,719
a loop's execution from its normal
2385
01:20:54,719 --> 01:20:56,480
sequence we have break
2386
01:20:56,480 --> 01:20:59,040
continue and pass so if you would like a
2387
01:20:59,040 --> 01:21:00,080
copy of all this code
2388
01:21:00,080 --> 01:21:01,679
i will post all of this in the comments
2389
01:21:01,679 --> 01:21:03,920
section down below but yeah those are a
2390
01:21:03,920 --> 01:21:04,320
few
2391
01:21:04,320 --> 01:21:07,360
loop control statements in python what's
2392
01:21:07,360 --> 01:21:08,639
going on everybody
2393
01:21:08,639 --> 01:21:10,719
it's bro hope you're doing well and in
2394
01:21:10,719 --> 01:21:12,320
this video i'm going to explain
2395
01:21:12,320 --> 01:21:15,920
lists in python so sit back relax
2396
01:21:15,920 --> 01:21:18,880
and enjoy the show
2397
01:21:19,120 --> 01:21:21,040
all right ladies and gentlemen let's get
2398
01:21:21,040 --> 01:21:23,120
down to business i got to explain
2399
01:21:23,120 --> 01:21:25,920
lists to you all now a list is used to
2400
01:21:25,920 --> 01:21:26,560
store
2401
01:21:26,560 --> 01:21:29,920
multiple items within a single variable
2402
01:21:29,920 --> 01:21:32,080
for example let's say we have a variable
2403
01:21:32,080 --> 01:21:33,120
called food
2404
01:21:33,120 --> 01:21:36,320
and i will store a string value of pizza
2405
01:21:36,320 --> 01:21:37,920
because i like pizza
2406
01:21:37,920 --> 01:21:40,159
what we could do is that we can store
2407
01:21:40,159 --> 01:21:42,000
multiple items within this
2408
01:21:42,000 --> 01:21:44,719
variable by turning this variable into a
2409
01:21:44,719 --> 01:21:45,280
list
2410
01:21:45,280 --> 01:21:47,280
and in order to do so we're going to
2411
01:21:47,280 --> 01:21:49,199
surround all of the values that we would
2412
01:21:49,199 --> 01:21:50,080
like to add
2413
01:21:50,080 --> 01:21:52,960
to our list with a set of square
2414
01:21:52,960 --> 01:21:53,920
brackets
2415
01:21:53,920 --> 01:21:56,480
and boom there you have it we now have a
2416
01:21:56,480 --> 01:21:58,719
list called food it is no longer
2417
01:21:58,719 --> 01:22:01,920
a variable so what we could do is add
2418
01:22:01,920 --> 01:22:04,560
multiple items to this list of food so
2419
01:22:04,560 --> 01:22:06,320
let's add some other food items that we
2420
01:22:06,320 --> 01:22:07,199
might like
2421
01:22:07,199 --> 01:22:09,840
let's see i also like maybe some
2422
01:22:09,840 --> 01:22:11,760
hamburgers
2423
01:22:11,760 --> 01:22:15,360
and a hot dog
2424
01:22:15,679 --> 01:22:18,719
and maybe some spaghetti i think i
2425
01:22:18,719 --> 01:22:19,920
smelled spaghetti right
2426
01:22:19,920 --> 01:22:22,960
okay so we now have a list called food
2427
01:22:22,960 --> 01:22:25,280
now what happens if we attempt to print
2428
01:22:25,280 --> 01:22:26,800
this list of food
2429
01:22:26,800 --> 01:22:30,719
so print food so this will
2430
01:22:30,719 --> 01:22:34,000
print all of the elements found in this
2431
01:22:34,000 --> 01:22:34,960
list
2432
01:22:34,960 --> 01:22:37,760
each item in a list is referred to as an
2433
01:22:37,760 --> 01:22:38,400
element
2434
01:22:38,400 --> 01:22:41,280
if we need to access a certain element
2435
01:22:41,280 --> 01:22:41,679
of
2436
01:22:41,679 --> 01:22:44,400
this list we have to list the index so
2437
01:22:44,400 --> 01:22:46,239
next to our list we'll add a set of
2438
01:22:46,239 --> 01:22:47,520
square brackets again
2439
01:22:47,520 --> 01:22:49,920
and we need to list the numbered index
2440
01:22:49,920 --> 01:22:51,520
of the element that we're trying to
2441
01:22:51,520 --> 01:22:52,320
access
2442
01:22:52,320 --> 01:22:53,760
now computers they always start with
2443
01:22:53,760 --> 01:22:55,360
zero so the first
2444
01:22:55,360 --> 01:22:58,320
element in our list would be element
2445
01:22:58,320 --> 01:22:59,360
number zero
2446
01:22:59,360 --> 01:23:02,639
the next one would be one two and three
2447
01:23:02,639 --> 01:23:06,239
so if we need to access element zero
2448
01:23:06,239 --> 01:23:07,920
which is in the first position we would
2449
01:23:07,920 --> 01:23:10,159
say food square brackets
2450
01:23:10,159 --> 01:23:12,320
and then within here list the index so
2451
01:23:12,320 --> 01:23:13,440
the first position
2452
01:23:13,440 --> 01:23:15,520
is zero and this will now print the
2453
01:23:15,520 --> 01:23:17,120
first element of our list
2454
01:23:17,120 --> 01:23:19,280
which is pizza let's attempt to access
2455
01:23:19,280 --> 01:23:20,560
some of these other elements
2456
01:23:20,560 --> 01:23:24,159
so the element at index one would be
2457
01:23:24,159 --> 01:23:28,159
you guessed it hamburger two
2458
01:23:28,159 --> 01:23:30,960
would be hot dog and three would be
2459
01:23:30,960 --> 01:23:31,920
spaghetti
2460
01:23:31,920 --> 01:23:34,719
now what happens if we attempt to access
2461
01:23:34,719 --> 01:23:36,880
the element at index four
2462
01:23:36,880 --> 01:23:38,719
well currently that's out of range
2463
01:23:38,719 --> 01:23:40,159
because we did not assign
2464
01:23:40,159 --> 01:23:42,800
a value so we're experiencing an error
2465
01:23:42,800 --> 01:23:43,600
an index
2466
01:23:43,600 --> 01:23:46,239
out of range error but if i were to add
2467
01:23:46,239 --> 01:23:47,600
another element
2468
01:23:47,600 --> 01:23:51,440
let's say pudding alright well then
2469
01:23:51,440 --> 01:23:54,560
the element at index 4 would be putting
2470
01:23:54,560 --> 01:23:55,199
then
2471
01:23:55,199 --> 01:23:57,600
one important concept with lists is that
2472
01:23:57,600 --> 01:23:58,480
you can always
2473
01:23:58,480 --> 01:24:00,719
update and change the elements found
2474
01:24:00,719 --> 01:24:01,600
within a list
2475
01:24:01,600 --> 01:24:03,440
later on in the program after you
2476
01:24:03,440 --> 01:24:05,600
declare one so let's say we would like
2477
01:24:05,600 --> 01:24:05,920
to
2478
01:24:05,920 --> 01:24:08,320
immediately change one of these elements
2479
01:24:08,320 --> 01:24:09,040
let's say
2480
01:24:09,040 --> 01:24:12,320
food at index zero i want to
2481
01:24:12,320 --> 01:24:15,360
replace with maybe sushi because sushi
2482
01:24:15,360 --> 01:24:16,080
is great
2483
01:24:16,080 --> 01:24:18,880
so if i were to print the element at
2484
01:24:18,880 --> 01:24:19,440
index
2485
01:24:19,440 --> 01:24:22,719
0 this would no longer be pizza because
2486
01:24:22,719 --> 01:24:25,760
we updated element 0 to be
2487
01:24:25,760 --> 01:24:28,320
sushi then and this prints sushi now if
2488
01:24:28,320 --> 01:24:29,600
you need to display
2489
01:24:29,600 --> 01:24:31,920
all of the elements found within a list
2490
01:24:31,920 --> 01:24:34,400
you can easily do so with a standard for
2491
01:24:34,400 --> 01:24:34,880
loop
2492
01:24:34,880 --> 01:24:38,239
what we'll do is say for x in
2493
01:24:38,239 --> 01:24:41,920
the name of our list which is food
2494
01:24:41,920 --> 01:24:45,600
print x and let me turn this line into a
2495
01:24:45,600 --> 01:24:46,639
comment
2496
01:24:46,639 --> 01:24:49,360
all right so this for loop will print
2497
01:24:49,360 --> 01:24:51,360
all of the elements found within our
2498
01:24:51,360 --> 01:24:51,760
list
2499
01:24:51,760 --> 01:24:55,040
of food and the results are sushi
2500
01:24:55,040 --> 01:24:57,600
hamburger hot dog spaghetti and pudding
2501
01:24:57,600 --> 01:24:59,440
all right people for this next portion
2502
01:24:59,440 --> 01:25:01,520
i'm going to demonstrate a few useful
2503
01:25:01,520 --> 01:25:02,400
functions of
2504
01:25:02,400 --> 01:25:04,159
lists and to access some of these
2505
01:25:04,159 --> 01:25:06,320
functions type the name of your list
2506
01:25:06,320 --> 01:25:08,719
dot and there's a bunch to choose from
2507
01:25:08,719 --> 01:25:10,080
here let's begin with
2508
01:25:10,080 --> 01:25:12,560
event we can add an element to this list
2509
01:25:12,560 --> 01:25:14,480
let's say at the end i would like to add
2510
01:25:14,480 --> 01:25:17,679
ice cream so then if i were to display
2511
01:25:17,679 --> 01:25:20,400
my list we now have ice cream at the end
2512
01:25:20,400 --> 01:25:22,000
because we appended
2513
01:25:22,000 --> 01:25:24,800
this value to my list of food so let's
2514
01:25:24,800 --> 01:25:26,320
go over a few others
2515
01:25:26,320 --> 01:25:29,600
so we can also remove a value food
2516
01:25:29,600 --> 01:25:32,159
dot remove let's say i would like to
2517
01:25:32,159 --> 01:25:32,800
remove
2518
01:25:32,800 --> 01:25:36,239
hotdog so i'm going to type that within
2519
01:25:36,239 --> 01:25:39,520
my function and this will remove hotdog
2520
01:25:39,520 --> 01:25:42,880
and that is no longer here
2521
01:25:42,880 --> 01:25:46,719
next we can pop food.pop
2522
01:25:46,719 --> 01:25:50,000
pop will remove the last element so this
2523
01:25:50,000 --> 01:25:52,560
will remove
2524
01:25:52,840 --> 01:25:54,560
pudding
2525
01:25:54,560 --> 01:25:57,760
we can insert a value at a given index
2526
01:25:57,760 --> 01:26:01,360
food dot insert we need to list an index
2527
01:26:01,360 --> 01:26:03,760
let's say zero that would be the first
2528
01:26:03,760 --> 01:26:05,520
position technically that's where pizza
2529
01:26:05,520 --> 01:26:06,560
is currently
2530
01:26:06,560 --> 01:26:10,000
and i would like to add cake
2531
01:26:10,000 --> 01:26:14,639
and at index 0 we now have cake
2532
01:26:14,639 --> 01:26:17,840
so next up we have sort
2533
01:26:17,840 --> 01:26:20,880
food dot sort this will sort a list
2534
01:26:20,880 --> 01:26:22,320
alphabetically
2535
01:26:22,320 --> 01:26:24,480
so in the first position we have
2536
01:26:24,480 --> 01:26:26,719
hamburger hot dog pudding spaghetti
2537
01:26:26,719 --> 01:26:31,120
then sushi we can also clear a list
2538
01:26:31,120 --> 01:26:34,639
food dot clear and this will remove
2539
01:26:34,639 --> 01:26:37,600
all of the elements of a list so this
2540
01:26:37,600 --> 01:26:39,280
will not print anything because we
2541
01:26:39,280 --> 01:26:40,239
cleared the list
2542
01:26:40,239 --> 01:26:42,880
well everyone in conclusion a list is
2543
01:26:42,880 --> 01:26:45,600
really just a variable that can store
2544
01:26:45,600 --> 01:26:48,560
multiple values think of it that way so
2545
01:26:48,560 --> 01:26:50,000
if you would like a copy
2546
01:26:50,000 --> 01:26:51,360
of all the code that we have written
2547
01:26:51,360 --> 01:26:53,199
here today i will post all of this in
2548
01:26:53,199 --> 01:26:54,560
the comments down below
2549
01:26:54,560 --> 01:26:58,239
but yeah that's how lists work in python
2550
01:26:58,239 --> 01:27:00,480
how's it going everybody it's bro hope
2551
01:27:00,480 --> 01:27:01,280
you're doing well
2552
01:27:01,280 --> 01:27:02,960
and in this video i'm going to explain
2553
01:27:02,960 --> 01:27:05,760
2d lists in python so sit back
2554
01:27:05,760 --> 01:27:09,600
relax and enjoy the show
2555
01:27:10,000 --> 01:27:12,239
i have a super quick video for you all
2556
01:27:12,239 --> 01:27:13,120
today on
2557
01:27:13,120 --> 01:27:15,639
2d lists also referred to as
2558
01:27:15,639 --> 01:27:17,280
multi-dimensional lists
2559
01:27:17,280 --> 01:27:20,400
all it is is a list of separate lists
2560
01:27:20,400 --> 01:27:22,239
so let's begin by creating a few
2561
01:27:22,239 --> 01:27:24,719
separate lists let's say we have a list
2562
01:27:24,719 --> 01:27:27,520
called drinks and let's assign a few
2563
01:27:27,520 --> 01:27:28,400
values
2564
01:27:28,400 --> 01:27:32,000
let's say we have coffee as well
2565
01:27:32,000 --> 01:27:35,440
as soda and maybe some tea
2566
01:27:35,440 --> 01:27:37,760
all right so we currently have one list
2567
01:27:37,760 --> 01:27:39,679
let's make maybe two more let's make a
2568
01:27:39,679 --> 01:27:40,400
list called
2569
01:27:40,400 --> 01:27:44,840
dinner and maybe in here we'll place
2570
01:27:44,840 --> 01:27:49,199
pizza then hamburger
2571
01:27:50,560 --> 01:27:53,679
and hot dog okay
2572
01:27:53,679 --> 01:27:55,040
then let's create one more for this
2573
01:27:55,040 --> 01:27:58,400
example let's say dessert
2574
01:27:59,280 --> 01:28:02,320
and maybe we'll add two items
2575
01:28:02,320 --> 01:28:03,920
they all don't need the same amount of
2576
01:28:03,920 --> 01:28:06,239
items or elements so we have cake
2577
01:28:06,239 --> 01:28:09,120
and ice cream
2578
01:28:09,440 --> 01:28:12,000
all right so we have three lists one
2579
01:28:12,000 --> 01:28:13,040
called drinks
2580
01:28:13,040 --> 01:28:16,080
dinner and dessert so what we can do is
2581
01:28:16,080 --> 01:28:16,560
add
2582
01:28:16,560 --> 01:28:19,600
all of these lists to one list
2583
01:28:19,600 --> 01:28:22,480
and let's create a list called food food
2584
01:28:22,480 --> 01:28:24,480
equals and for each of these elements
2585
01:28:24,480 --> 01:28:25,600
we're going to place
2586
01:28:25,600 --> 01:28:29,679
our lists so we have drinks dinner
2587
01:28:29,679 --> 01:28:32,480
and dessert check this out what if i
2588
01:28:32,480 --> 01:28:33,040
were to
2589
01:28:33,040 --> 01:28:36,400
print my 2d list of food
2590
01:28:36,400 --> 01:28:38,480
so what ends up happening is that this
2591
01:28:38,480 --> 01:28:40,239
will print all of the elements
2592
01:28:40,239 --> 01:28:42,880
found within each individual list and
2593
01:28:42,880 --> 01:28:44,480
they're all grouped together
2594
01:28:44,480 --> 01:28:48,159
so this first portion is my drinks list
2595
01:28:48,159 --> 01:28:51,199
which contains coffee soda tea this next
2596
01:28:51,199 --> 01:28:51,920
element
2597
01:28:51,920 --> 01:28:54,400
is my dinner list which contains pizza
2598
01:28:54,400 --> 01:28:57,600
hamburger hot dog and the last element
2599
01:28:57,600 --> 01:29:00,880
dessert contains cake and ice cream if i
2600
01:29:00,880 --> 01:29:02,080
need to access
2601
01:29:02,080 --> 01:29:04,960
just one of these lists i will add an
2602
01:29:04,960 --> 01:29:05,600
index
2603
01:29:05,600 --> 01:29:08,719
after my food 2d list and
2604
01:29:08,719 --> 01:29:11,840
set an index number so index zero is
2605
01:29:11,840 --> 01:29:13,360
referring to my first
2606
01:29:13,360 --> 01:29:15,840
list of drinks and this will display all
2607
01:29:15,840 --> 01:29:17,760
of the elements found within my first
2608
01:29:17,760 --> 01:29:18,560
list
2609
01:29:18,560 --> 01:29:21,199
and if i need just one of these elements
2610
01:29:21,199 --> 01:29:22,159
i will add
2611
01:29:22,159 --> 01:29:24,960
a second set of square brackets and list
2612
01:29:24,960 --> 01:29:27,199
the index of the item that i'm trying to
2613
01:29:27,199 --> 01:29:27,920
access
2614
01:29:27,920 --> 01:29:31,040
so this very first item coffee would be
2615
01:29:31,040 --> 01:29:33,920
index zero and then index zero again so
2616
01:29:33,920 --> 01:29:34,400
we need
2617
01:29:34,400 --> 01:29:36,639
two sets of square brackets and if i
2618
01:29:36,639 --> 01:29:37,679
were to print this
2619
01:29:37,679 --> 01:29:40,320
this will display coffee so if i go down
2620
01:29:40,320 --> 01:29:40,960
the list
2621
01:29:40,960 --> 01:29:44,000
the next element would be soda
2622
01:29:44,000 --> 01:29:47,199
and then t if i were to change the index
2623
01:29:47,199 --> 01:29:47,760
within
2624
01:29:47,760 --> 01:29:50,320
this first set of square brackets well
2625
01:29:50,320 --> 01:29:51,360
now we're working with
2626
01:29:51,360 --> 01:29:53,360
a different list this time we're working
2627
01:29:53,360 --> 01:29:54,960
with our dinner list
2628
01:29:54,960 --> 01:29:58,080
so the element at index 1
2629
01:29:58,080 --> 01:30:01,280
index 2 that would be our dinner list
2630
01:30:01,280 --> 01:30:03,920
and this will display the element of hot
2631
01:30:03,920 --> 01:30:04,560
dog
2632
01:30:04,560 --> 01:30:06,719
if i change this to the next list which
2633
01:30:06,719 --> 01:30:08,239
would be dessert
2634
01:30:08,239 --> 01:30:10,159
well we're going to receive an index out
2635
01:30:10,159 --> 01:30:11,840
of range error because
2636
01:30:11,840 --> 01:30:14,880
we only have two values within this list
2637
01:30:14,880 --> 01:30:18,239
cake and ice cream there is no element
2638
01:30:18,239 --> 01:30:21,760
at index 2 because we only added two
2639
01:30:21,760 --> 01:30:22,320
elements
2640
01:30:22,320 --> 01:30:24,960
to this list of dessert all right
2641
01:30:24,960 --> 01:30:26,159
everybody so that
2642
01:30:26,159 --> 01:30:29,080
is 2d lists also referred to as
2643
01:30:29,080 --> 01:30:30,880
multi-dimensional lists
2644
01:30:30,880 --> 01:30:33,120
it's a list of separate lists if you
2645
01:30:33,120 --> 01:30:34,400
need to access
2646
01:30:34,400 --> 01:30:36,880
one of the elements within your 2d list
2647
01:30:36,880 --> 01:30:37,520
you need
2648
01:30:37,520 --> 01:30:40,159
two sets of square brackets so if you
2649
01:30:40,159 --> 01:30:41,520
would like a copy of this code
2650
01:30:41,520 --> 01:30:43,120
i will post this in the comments down
2651
01:30:43,120 --> 01:30:44,880
below but yeah that's how
2652
01:30:44,880 --> 01:30:48,000
2d lists work in python hey what's going
2653
01:30:48,000 --> 01:30:48,639
on people
2654
01:30:48,639 --> 01:30:50,400
it's you bro hope you're doing well and
2655
01:30:50,400 --> 01:30:51,920
in this video i'm going to explain
2656
01:30:51,920 --> 01:30:53,440
tuples in python so
2657
01:30:53,440 --> 01:30:57,920
sit back relax and enjoy the show
2658
01:30:58,239 --> 01:31:01,120
so tuples they are collections which are
2659
01:31:01,120 --> 01:31:01,679
ordered
2660
01:31:01,679 --> 01:31:03,840
and unchangeable they're very similar to
2661
01:31:03,840 --> 01:31:05,040
lists but they're ordered
2662
01:31:05,040 --> 01:31:07,120
and we can't change them they're useful
2663
01:31:07,120 --> 01:31:09,199
for grouping together related data
2664
01:31:09,199 --> 01:31:10,880
let's say we would like to create some
2665
01:31:10,880 --> 01:31:12,719
sort of student record
2666
01:31:12,719 --> 01:31:14,880
so we can create a tuple to take care of
2667
01:31:14,880 --> 01:31:16,719
that for us so let's say we would like
2668
01:31:16,719 --> 01:31:18,560
to create a tuple called student
2669
01:31:18,560 --> 01:31:20,719
the process of creating a tuple is very
2670
01:31:20,719 --> 01:31:22,000
similar to lists
2671
01:31:22,000 --> 01:31:24,480
but instead of using a set of square
2672
01:31:24,480 --> 01:31:26,400
brackets to place all of the values
2673
01:31:26,400 --> 01:31:26,960
within
2674
01:31:26,960 --> 01:31:29,360
we're going to use a set of parentheses
2675
01:31:29,360 --> 01:31:31,360
and now we can add a bunch of values
2676
01:31:31,360 --> 01:31:33,840
related to this collection of student
2677
01:31:33,840 --> 01:31:35,760
so let's add some student information
2678
01:31:35,760 --> 01:31:37,600
let's say that this student's name is
2679
01:31:37,600 --> 01:31:38,159
bro
2680
01:31:38,159 --> 01:31:40,719
let's give him an age as well as a
2681
01:31:40,719 --> 01:31:41,440
gender
2682
01:31:41,440 --> 01:31:43,360
all right so there you have it we now
2683
01:31:43,360 --> 01:31:45,360
have a tuple called student
2684
01:31:45,360 --> 01:31:46,800
and let's take a look at some of the
2685
01:31:46,800 --> 01:31:48,400
functions related to tuples
2686
01:31:48,400 --> 01:31:49,920
and you can access them by typing
2687
01:31:49,920 --> 01:31:51,679
student dot
2688
01:31:51,679 --> 01:31:53,280
and then there are a few here not as
2689
01:31:53,280 --> 01:31:55,280
many as lists we have count and we have
2690
01:31:55,280 --> 01:31:56,080
index
2691
01:31:56,080 --> 01:31:58,080
so we can find the count of how many
2692
01:31:58,080 --> 01:32:00,159
times a value appears
2693
01:32:00,159 --> 01:32:03,199
let's count how many times the value of
2694
01:32:03,199 --> 01:32:04,480
bro appears
2695
01:32:04,480 --> 01:32:06,320
so we'll need to put this within a print
2696
01:32:06,320 --> 01:32:08,960
statement so we can actually see it
2697
01:32:08,960 --> 01:32:12,000
so bro appears
2698
01:32:12,000 --> 01:32:14,320
one time within this tuple there is also
2699
01:32:14,320 --> 01:32:16,000
an index method as well
2700
01:32:16,000 --> 01:32:19,600
so type the name of your tuple dot
2701
01:32:19,600 --> 01:32:22,080
and we will use the index method we can
2702
01:32:22,080 --> 01:32:24,639
find the index of a certain value
2703
01:32:24,639 --> 01:32:26,239
let's say we would like to find the
2704
01:32:26,239 --> 01:32:27,760
index of mail
2705
01:32:27,760 --> 01:32:29,360
and then i will just print this to the
2706
01:32:29,360 --> 01:32:31,120
console window
2707
01:32:31,120 --> 01:32:34,560
so the index of the value mail would be
2708
01:32:34,560 --> 01:32:37,600
at two zero one
2709
01:32:37,600 --> 01:32:40,159
two that's it for the methods available
2710
01:32:40,159 --> 01:32:41,199
to tuples
2711
01:32:41,199 --> 01:32:42,960
a few tricks that you can do with tuples
2712
01:32:42,960 --> 01:32:44,880
is that you can display
2713
01:32:44,880 --> 01:32:47,760
all of the contents within a tuple using
2714
01:32:47,760 --> 01:32:48,560
a for loop
2715
01:32:48,560 --> 01:32:51,760
let's say 4x in student
2716
01:32:51,760 --> 01:32:55,199
we will print x so
2717
01:32:55,199 --> 01:32:57,199
this will iterate once through all the
2718
01:32:57,199 --> 01:32:59,520
values found within our tuple
2719
01:32:59,520 --> 01:33:02,080
of student and we can also check to see
2720
01:33:02,080 --> 01:33:03,440
if a certain value
2721
01:33:03,440 --> 01:33:05,280
exists within our tuple using an if
2722
01:33:05,280 --> 01:33:07,679
statement let's say if
2723
01:33:07,679 --> 01:33:11,120
bro in student
2724
01:33:11,120 --> 01:33:13,280
if this is true then what do we want to
2725
01:33:13,280 --> 01:33:14,480
do let's say
2726
01:33:14,480 --> 01:33:17,840
print bro is here
2727
01:33:17,840 --> 01:33:21,199
all right so since bro is within here
2728
01:33:21,199 --> 01:33:23,920
this will be true and this will print
2729
01:33:23,920 --> 01:33:24,480
our
2730
01:33:24,480 --> 01:33:27,120
statement of bro is here all right
2731
01:33:27,120 --> 01:33:29,280
everybody like i said this is a super
2732
01:33:29,280 --> 01:33:31,120
quick video on tuples
2733
01:33:31,120 --> 01:33:34,159
they're very similar to lists except
2734
01:33:34,159 --> 01:33:36,480
they are ordered and unchangeable if
2735
01:33:36,480 --> 01:33:38,239
you'd like a copy of all this code i
2736
01:33:38,239 --> 01:33:39,840
will post all of this in the comments
2737
01:33:39,840 --> 01:33:40,719
down below
2738
01:33:40,719 --> 01:33:42,639
and don't forget to smash that like
2739
01:33:42,639 --> 01:33:44,480
button drop a comment down below
2740
01:33:44,480 --> 01:33:46,080
and subscribe if you'd like to become a
2741
01:33:46,080 --> 01:33:47,600
fellow bro
2742
01:33:47,600 --> 01:33:49,760
hey what's going on everybody it's you
2743
01:33:49,760 --> 01:33:51,120
bro hope you're doing well
2744
01:33:51,120 --> 01:33:52,880
and in this video i'm going to explain
2745
01:33:52,880 --> 01:33:55,040
what a set is in python so
2746
01:33:55,040 --> 01:33:59,920
sit back relax and enjoy the show
2747
01:34:00,080 --> 01:34:03,280
a set is a collection which is unordered
2748
01:34:03,280 --> 01:34:04,320
as well as being
2749
01:34:04,320 --> 01:34:06,480
unindexed they do not allow any
2750
01:34:06,480 --> 01:34:07,760
duplicate values
2751
01:34:07,760 --> 01:34:10,159
for example let's create a set of
2752
01:34:10,159 --> 01:34:10,960
silverware
2753
01:34:10,960 --> 01:34:12,960
so in order to create a set we need a
2754
01:34:12,960 --> 01:34:14,080
name for this set
2755
01:34:14,080 --> 01:34:17,199
let's call it utensils equals
2756
01:34:17,199 --> 01:34:19,120
and in order to create a set we need to
2757
01:34:19,120 --> 01:34:21,280
surround all values with a set
2758
01:34:21,280 --> 01:34:23,920
of curly braces now let's add a few
2759
01:34:23,920 --> 01:34:24,719
utensils
2760
01:34:24,719 --> 01:34:27,760
to the set called utensils let's add a
2761
01:34:27,760 --> 01:34:29,440
fork
2762
01:34:29,440 --> 01:34:32,320
a spoon and maybe a knife and that
2763
01:34:32,320 --> 01:34:33,440
should be good
2764
01:34:33,440 --> 01:34:36,560
and let's display all of the values for
2765
01:34:36,560 --> 01:34:40,880
x in utensils
2766
01:34:41,119 --> 01:34:44,560
print x now with sets
2767
01:34:44,560 --> 01:34:46,560
they're different from lists because
2768
01:34:46,560 --> 01:34:48,400
they're unordered as well as being
2769
01:34:48,400 --> 01:34:49,360
unindexed
2770
01:34:49,360 --> 01:34:51,600
if i were to print all of the elements
2771
01:34:51,600 --> 01:34:52,639
in utensils
2772
01:34:52,639 --> 01:34:54,639
they might not necessarily be in the
2773
01:34:54,639 --> 01:34:56,719
same order in which we placed them
2774
01:34:56,719 --> 01:34:59,199
for example when we printed all of the
2775
01:34:59,199 --> 01:35:00,880
elements found within utensils
2776
01:35:00,880 --> 01:35:03,280
first we had a knife a fork and then a
2777
01:35:03,280 --> 01:35:05,199
spoon if i were to run this again
2778
01:35:05,199 --> 01:35:07,840
they might be in a different order kind
2779
01:35:07,840 --> 01:35:08,880
of like that
2780
01:35:08,880 --> 01:35:11,840
so a set is actually faster than a list
2781
01:35:11,840 --> 01:35:14,000
if you need to check to see if something
2782
01:35:14,000 --> 01:35:16,960
is within a set compared to a list and
2783
01:35:16,960 --> 01:35:19,520
they do not allow any duplicate values
2784
01:35:19,520 --> 01:35:21,280
let me add a bunch of knives and let's
2785
01:35:21,280 --> 01:35:22,960
see what happens
2786
01:35:22,960 --> 01:35:26,159
so i'll have a total of three knives but
2787
01:35:26,159 --> 01:35:28,000
when we print all of the elements
2788
01:35:28,000 --> 01:35:30,800
of the set only one knife appears so
2789
01:35:30,800 --> 01:35:34,000
here's a few useful methods of sets
2790
01:35:34,000 --> 01:35:36,560
the first method is that we can add an
2791
01:35:36,560 --> 01:35:37,920
item to our set
2792
01:35:37,920 --> 01:35:40,320
so type in the name of the set in this
2793
01:35:40,320 --> 01:35:42,960
case it's utensils dot and here's a
2794
01:35:42,960 --> 01:35:44,960
bunch of methods that you can use
2795
01:35:44,960 --> 01:35:47,360
i am looking for add and we can add an
2796
01:35:47,360 --> 01:35:48,639
element to the set
2797
01:35:48,639 --> 01:35:52,159
i would like to add a napkin
2798
01:35:52,159 --> 01:35:54,800
and then if we were to print the set we
2799
01:35:54,800 --> 01:35:56,080
do have a napkin
2800
01:35:56,080 --> 01:35:58,719
within the set we can also remove an
2801
01:35:58,719 --> 01:35:59,760
element
2802
01:35:59,760 --> 01:36:02,880
utensils dot remove
2803
01:36:02,880 --> 01:36:07,600
and i would like to remove the fork
2804
01:36:08,239 --> 01:36:11,199
and now our fork is gone we can also
2805
01:36:11,199 --> 01:36:12,800
clear
2806
01:36:12,800 --> 01:36:16,480
utensils dot clear
2807
01:36:16,480 --> 01:36:18,480
and all the elements within our set
2808
01:36:18,480 --> 01:36:20,080
should be gone
2809
01:36:20,080 --> 01:36:22,239
so let me turn these lines into a
2810
01:36:22,239 --> 01:36:23,440
comment
2811
01:36:23,440 --> 01:36:25,520
all right for this next example we'll
2812
01:36:25,520 --> 01:36:26,719
need a second set
2813
01:36:26,719 --> 01:36:30,560
let's create a second set called dishes
2814
01:36:30,560 --> 01:36:33,280
and then we need a set of curly braces
2815
01:36:33,280 --> 01:36:33,600
and
2816
01:36:33,600 --> 01:36:36,639
let's add a bowl
2817
01:36:36,960 --> 01:36:40,960
a plate and a cup
2818
01:36:40,960 --> 01:36:43,119
our next method is that we're going to
2819
01:36:43,119 --> 01:36:45,600
add one set to another by using the
2820
01:36:45,600 --> 01:36:46,800
update method
2821
01:36:46,800 --> 01:36:48,719
let's say we would like to add our
2822
01:36:48,719 --> 01:36:51,040
dishes set to our utensil set
2823
01:36:51,040 --> 01:36:54,000
so let's type utensils dot and use the
2824
01:36:54,000 --> 01:36:54,960
update method
2825
01:36:54,960 --> 01:36:57,679
and within this method we will pass in
2826
01:36:57,679 --> 01:36:59,360
the set that we would like to add
2827
01:36:59,360 --> 01:37:02,639
so utensils update dishes this will add
2828
01:37:02,639 --> 01:37:04,639
all of the elements found within dishes
2829
01:37:04,639 --> 01:37:07,119
to our utensil set and you can see that
2830
01:37:07,119 --> 01:37:08,800
when we print our utensils
2831
01:37:08,800 --> 01:37:12,000
we have a bowl a plate and a cup in here
2832
01:37:12,000 --> 01:37:12,639
as well
2833
01:37:12,639 --> 01:37:14,000
and then if i were to switch these
2834
01:37:14,000 --> 01:37:16,400
around let's say dishes
2835
01:37:16,400 --> 01:37:20,560
update utensils and then i will
2836
01:37:20,560 --> 01:37:23,679
print everything found within dishes now
2837
01:37:23,679 --> 01:37:25,360
there's a fork a spoon
2838
01:37:25,360 --> 01:37:28,000
and a knife in here as well we can also
2839
01:37:28,000 --> 01:37:28,800
join
2840
01:37:28,800 --> 01:37:31,199
two sets together and create a new set
2841
01:37:31,199 --> 01:37:32,239
entirely
2842
01:37:32,239 --> 01:37:34,719
so let's create a set called dinner
2843
01:37:34,719 --> 01:37:36,560
table like we're setting up a dinner
2844
01:37:36,560 --> 01:37:39,040
table and we need a fork a spoon a knife
2845
01:37:39,040 --> 01:37:40,960
a bowl a plate and a cup
2846
01:37:40,960 --> 01:37:44,320
so we have a dinner table set
2847
01:37:44,320 --> 01:37:46,960
we're going to set the sequel to either
2848
01:37:46,960 --> 01:37:48,719
utensils
2849
01:37:48,719 --> 01:37:51,760
dot union
2850
01:37:51,760 --> 01:37:54,880
dishes or we could do dishes
2851
01:37:54,880 --> 01:37:57,840
union utensils either way would work and
2852
01:37:57,840 --> 01:37:59,520
let's display all the elements found
2853
01:37:59,520 --> 01:38:00,800
within dinner table
2854
01:38:00,800 --> 01:38:02,719
and we should now have elements from
2855
01:38:02,719 --> 01:38:04,400
both sets utensils
2856
01:38:04,400 --> 01:38:07,040
and dishes as well there are also some
2857
01:38:07,040 --> 01:38:09,280
methods in which we can compare
2858
01:38:09,280 --> 01:38:11,040
the similarities as well as the
2859
01:38:11,040 --> 01:38:12,320
differences between
2860
01:38:12,320 --> 01:38:14,960
the elements found within two sets let's
2861
01:38:14,960 --> 01:38:16,880
say i would like to check to see
2862
01:38:16,880 --> 01:38:19,679
what utensils has that dishes doesn't
2863
01:38:19,679 --> 01:38:21,760
and for this example i think i'll add a
2864
01:38:21,760 --> 01:38:22,480
knife
2865
01:38:22,480 --> 01:38:24,800
to my dishes set just so that they have
2866
01:38:24,800 --> 01:38:26,639
at least one thing in common
2867
01:38:26,639 --> 01:38:29,679
so i would like to see what utensils has
2868
01:38:29,679 --> 01:38:31,920
that dishes doesn't so we can do so
2869
01:38:31,920 --> 01:38:32,800
using the
2870
01:38:32,800 --> 01:38:35,040
difference method and i will print the
2871
01:38:35,040 --> 01:38:36,880
results with a print statement
2872
01:38:36,880 --> 01:38:40,480
so i'm going to compare utensils against
2873
01:38:40,480 --> 01:38:45,040
dishes utensils
2874
01:38:45,040 --> 01:38:48,639
dishes and this will print what utensils
2875
01:38:48,639 --> 01:38:49,199
has
2876
01:38:49,199 --> 01:38:52,320
that dishes doesn't which is a fork as
2877
01:38:52,320 --> 01:38:54,159
well as a spoon
2878
01:38:54,159 --> 01:38:56,000
and we can reverse the rolls too we
2879
01:38:56,000 --> 01:38:58,639
could say dishes difference
2880
01:38:58,639 --> 01:39:01,119
utensils what do dishes have that
2881
01:39:01,119 --> 01:39:02,719
utensils doesn't
2882
01:39:02,719 --> 01:39:06,480
and dishes has a cup a bowl and a plate
2883
01:39:06,480 --> 01:39:08,880
they both have knives so that's why
2884
01:39:08,880 --> 01:39:10,480
knife isn't appearing
2885
01:39:10,480 --> 01:39:12,159
we can also check to see if there's
2886
01:39:12,159 --> 01:39:13,679
anything that they have in common
2887
01:39:13,679 --> 01:39:16,880
using the intersection method so i'll
2888
01:39:16,880 --> 01:39:20,320
turn this line into a comment
2889
01:39:20,400 --> 01:39:23,920
we'll print utensils
2890
01:39:23,920 --> 01:39:27,199
dot intersection
2891
01:39:27,199 --> 01:39:30,159
dishes and this will return whatever
2892
01:39:30,159 --> 01:39:32,159
element that they have in common
2893
01:39:32,159 --> 01:39:35,280
which is a knife all right everybody in
2894
01:39:35,280 --> 01:39:36,560
conclusion a set
2895
01:39:36,560 --> 01:39:39,119
is a collection which is unordered as
2896
01:39:39,119 --> 01:39:40,080
well as being
2897
01:39:40,080 --> 01:39:42,320
unindexed they do not allow any
2898
01:39:42,320 --> 01:39:43,520
duplicate values
2899
01:39:43,520 --> 01:39:45,920
and you can do things like compare two
2900
01:39:45,920 --> 01:39:47,600
sets see if there's anything that they
2901
01:39:47,600 --> 01:39:49,440
have in common any differences
2902
01:39:49,440 --> 01:39:51,440
and we can add or remove elements to a
2903
01:39:51,440 --> 01:39:53,520
set if you'd like a copy of all this
2904
01:39:53,520 --> 01:39:55,360
code i will post all of this in the
2905
01:39:55,360 --> 01:39:56,639
comments down below
2906
01:39:56,639 --> 01:39:58,480
don't be afraid to smash that like
2907
01:39:58,480 --> 01:40:00,560
button drop a comment down below
2908
01:40:00,560 --> 01:40:02,159
and subscribe if you'd like to become a
2909
01:40:02,159 --> 01:40:05,119
fellow bro hey what's going on everybody
2910
01:40:05,119 --> 01:40:06,960
it's you bro hope you're doing well and
2911
01:40:06,960 --> 01:40:08,480
in this video i'm going to explain
2912
01:40:08,480 --> 01:40:11,199
how dictionaries work in python so sit
2913
01:40:11,199 --> 01:40:11,840
back
2914
01:40:11,840 --> 01:40:15,679
relax and enjoy the show
2915
01:40:15,760 --> 01:40:17,760
all right let's get down to business a
2916
01:40:17,760 --> 01:40:19,119
dictionary is a
2917
01:40:19,119 --> 01:40:22,159
changeable unordered collection of
2918
01:40:22,159 --> 01:40:25,679
unique key value pairs they're fast
2919
01:40:25,679 --> 01:40:27,199
because they use hashing
2920
01:40:27,199 --> 01:40:29,440
and they allow us to access a value
2921
01:40:29,440 --> 01:40:30,239
quickly
2922
01:40:30,239 --> 01:40:32,320
now to create a dictionary it's very
2923
01:40:32,320 --> 01:40:33,679
similar to creating a
2924
01:40:33,679 --> 01:40:36,159
set based on the last video except we're
2925
01:40:36,159 --> 01:40:36,880
going to store
2926
01:40:36,880 --> 01:40:39,679
unique key value pairs let's create a
2927
01:40:39,679 --> 01:40:40,639
dictionary
2928
01:40:40,639 --> 01:40:43,440
of countries and their capitals we can
2929
01:40:43,440 --> 01:40:44,159
store those
2930
01:40:44,159 --> 01:40:46,960
as key value pairs so let's call this
2931
01:40:46,960 --> 01:40:48,159
dictionary
2932
01:40:48,159 --> 01:40:51,040
capitals capitals equals then we need a
2933
01:40:51,040 --> 01:40:53,040
set of curly braces much like what we
2934
01:40:53,040 --> 01:40:53,360
use
2935
01:40:53,360 --> 01:40:56,480
with sets now we need a key
2936
01:40:56,480 --> 01:40:59,600
and a value let's say we have the usa as
2937
01:40:59,600 --> 01:41:00,560
a key
2938
01:41:00,560 --> 01:41:03,119
and the capital of the usa would be
2939
01:41:03,119 --> 01:41:04,480
washington dc
2940
01:41:04,480 --> 01:41:06,800
so in order to associate a value with
2941
01:41:06,800 --> 01:41:08,639
the key we'll follow the key with a
2942
01:41:08,639 --> 01:41:09,119
colon
2943
01:41:09,119 --> 01:41:11,360
and then add some value and the data
2944
01:41:11,360 --> 01:41:12,719
type really doesn't matter but
2945
01:41:12,719 --> 01:41:14,800
in this case we're just using strings so
2946
01:41:14,800 --> 01:41:16,320
the capital of the usa
2947
01:41:16,320 --> 01:41:19,360
would be washington dc
2948
01:41:19,360 --> 01:41:21,760
and to add another key value pair we'll
2949
01:41:21,760 --> 01:41:23,600
separate each with a comma
2950
01:41:23,600 --> 01:41:25,520
and let's add a few others for good
2951
01:41:25,520 --> 01:41:28,159
measure so let's say we have india next
2952
01:41:28,159 --> 01:41:31,520
as a key and the value would be
2953
01:41:31,520 --> 01:41:35,040
new delhi and we'll add two more
2954
01:41:35,040 --> 01:41:38,080
let's say we have china
2955
01:41:38,080 --> 01:41:41,679
and the capital of china is beijing
2956
01:41:41,679 --> 01:41:46,000
and lastly let's add russia
2957
01:41:46,000 --> 01:41:49,920
and the capital of russia is moscow
2958
01:41:49,920 --> 01:41:51,199
and there you have it ladies and
2959
01:41:51,199 --> 01:41:53,520
gentlemen we now have a dictionary
2960
01:41:53,520 --> 01:41:54,639
called capitals
2961
01:41:54,639 --> 01:41:57,760
that has unique key value pairs so
2962
01:41:57,760 --> 01:41:58,560
remember with
2963
01:41:58,560 --> 01:42:00,800
dictionaries they are unordered in order
2964
01:42:00,800 --> 01:42:02,480
to access one of these values
2965
01:42:02,480 --> 01:42:04,560
instead of using a numbered index we're
2966
01:42:04,560 --> 01:42:06,639
going to use the associated key with
2967
01:42:06,639 --> 01:42:07,360
that value
2968
01:42:07,360 --> 01:42:09,199
here's an example let's say i would like
2969
01:42:09,199 --> 01:42:10,960
to print the capital
2970
01:42:10,960 --> 01:42:14,159
of russia so i need to use this key of
2971
01:42:14,159 --> 01:42:14,880
russia
2972
01:42:14,880 --> 01:42:16,960
so in order to do this type in the name
2973
01:42:16,960 --> 01:42:18,000
of the dictionary
2974
01:42:18,000 --> 01:42:20,320
followed by a set of straight brackets
2975
01:42:20,320 --> 01:42:21,520
and then instead of an
2976
01:42:21,520 --> 01:42:25,040
index number like 0 or 1 2 3
2977
01:42:25,040 --> 01:42:27,600
i'm going to use the key so i would like
2978
01:42:27,600 --> 01:42:28,400
to print
2979
01:42:28,400 --> 01:42:31,040
the value that's stored or associated
2980
01:42:31,040 --> 01:42:31,920
with the key
2981
01:42:31,920 --> 01:42:35,119
of russia so when i run this this will
2982
01:42:35,119 --> 01:42:36,159
print
2983
01:42:36,159 --> 01:42:39,280
that value which is moscow so this isn't
2984
01:42:39,280 --> 01:42:40,080
always safe
2985
01:42:40,080 --> 01:42:41,840
let's say we have a key that doesn't
2986
01:42:41,840 --> 01:42:43,199
exist like germany
2987
01:42:43,199 --> 01:42:44,960
germany is not currently within my
2988
01:42:44,960 --> 01:42:47,679
dictionary so if i were to run this
2989
01:42:47,679 --> 01:42:49,440
well then my program is going to
2990
01:42:49,440 --> 01:42:51,040
encounter an error
2991
01:42:51,040 --> 01:42:52,880
and this will interrupt the normal flow
2992
01:42:52,880 --> 01:42:54,080
of my program
2993
01:42:54,080 --> 01:42:57,520
a much safer way to access a key
2994
01:42:57,520 --> 01:42:59,360
to check to see if it's there or not is
2995
01:42:59,360 --> 01:43:01,040
to use the get method
2996
01:43:01,040 --> 01:43:03,520
of dictionaries so i'm going to turn
2997
01:43:03,520 --> 01:43:04,960
this line into a comment and let's try
2998
01:43:04,960 --> 01:43:06,560
this again but this time we'll use the
2999
01:43:06,560 --> 01:43:07,360
get method
3000
01:43:07,360 --> 01:43:09,360
so type in the name of the dictionary
3001
01:43:09,360 --> 01:43:12,000
dot get
3002
01:43:12,000 --> 01:43:15,199
and list the key that you want to use
3003
01:43:15,199 --> 01:43:18,560
so i would like to see if germany
3004
01:43:18,560 --> 01:43:21,679
is within my dictionary currently there
3005
01:43:21,679 --> 01:43:23,040
isn't so this will return
3006
01:43:23,040 --> 01:43:25,199
none and we will not encounter an error
3007
01:43:25,199 --> 01:43:26,320
so this is a much
3008
01:43:26,320 --> 01:43:28,800
safer way of checking to see if there is
3009
01:43:28,800 --> 01:43:30,719
a key within your dictionary
3010
01:43:30,719 --> 01:43:32,639
here's a few other useful methods there
3011
01:43:32,639 --> 01:43:35,199
is a method to print only the keys
3012
01:43:35,199 --> 01:43:38,000
so we type in the name of the dictionary
3013
01:43:38,000 --> 01:43:39,119
capitals
3014
01:43:39,119 --> 01:43:41,119
and here's a listing of all of the
3015
01:43:41,119 --> 01:43:42,880
methods that we have access to
3016
01:43:42,880 --> 01:43:45,440
i'm looking for the keys method and this
3017
01:43:45,440 --> 01:43:46,560
will print
3018
01:43:46,560 --> 01:43:49,360
only the keys and not the values we can
3019
01:43:49,360 --> 01:43:50,239
also print
3020
01:43:50,239 --> 01:43:53,360
just the values and not the keys so type
3021
01:43:53,360 --> 01:43:55,119
in the name of the dictionary again
3022
01:43:55,119 --> 01:43:59,360
capitals dot values
3023
01:43:59,360 --> 01:44:02,239
and this will print only the values or
3024
01:44:02,239 --> 01:44:03,679
you could print everything
3025
01:44:03,679 --> 01:44:06,320
both the keys and the values and you do
3026
01:44:06,320 --> 01:44:07,199
so by using
3027
01:44:07,199 --> 01:44:10,719
the items method capitals
3028
01:44:10,719 --> 01:44:14,719
dot items
3029
01:44:15,199 --> 01:44:16,560
and this will print your entire
3030
01:44:16,560 --> 01:44:18,880
dictionary one other way in which we can
3031
01:44:18,880 --> 01:44:19,440
display
3032
01:44:19,440 --> 01:44:21,040
all of the key value pairs in a
3033
01:44:21,040 --> 01:44:23,199
dictionary is to use a for loop
3034
01:44:23,199 --> 01:44:25,360
so at the end let's create a for loop
3035
01:44:25,360 --> 01:44:26,400
for
3036
01:44:26,400 --> 01:44:30,320
key comma value in
3037
01:44:30,320 --> 01:44:33,360
capitals dot items
3038
01:44:33,360 --> 01:44:36,000
this is going to iterate once for each
3039
01:44:36,000 --> 01:44:37,119
key value pair
3040
01:44:37,119 --> 01:44:40,000
in my dictionary for each key value in
3041
01:44:40,000 --> 01:44:40,800
capitals
3042
01:44:40,800 --> 01:44:44,000
dot items print
3043
01:44:44,000 --> 01:44:47,520
key comma value
3044
01:44:47,520 --> 01:44:50,080
and this will print my entire dictionary
3045
01:44:50,080 --> 01:44:51,840
a feature of dictionaries
3046
01:44:51,840 --> 01:44:54,000
is that they are mutable that means we
3047
01:44:54,000 --> 01:44:55,840
can change them or alter them
3048
01:44:55,840 --> 01:44:58,320
after the program is already running so
3049
01:44:58,320 --> 01:44:59,760
one way in which we can do that
3050
01:44:59,760 --> 01:45:01,840
is to use the update method of
3051
01:45:01,840 --> 01:45:02,960
dictionaries
3052
01:45:02,960 --> 01:45:06,000
so let's add germany as a key and give
3053
01:45:06,000 --> 01:45:06,800
it a value
3054
01:45:06,800 --> 01:45:09,840
of berlin so type in the name of the
3055
01:45:09,840 --> 01:45:11,280
dictionary capitals
3056
01:45:11,280 --> 01:45:13,760
and we'll use the update method and
3057
01:45:13,760 --> 01:45:15,679
within the parentheses of this method
3058
01:45:15,679 --> 01:45:18,159
we'll add a set of curly braces and now
3059
01:45:18,159 --> 01:45:19,360
we can add a new
3060
01:45:19,360 --> 01:45:23,440
key value pair so let's add germany
3061
01:45:23,440 --> 01:45:27,280
and a value of berlin so if i were to
3062
01:45:27,280 --> 01:45:28,480
run this and display
3063
01:45:28,480 --> 01:45:30,480
all of the key value pairs found within
3064
01:45:30,480 --> 01:45:32,320
my dictionary we now have
3065
01:45:32,320 --> 01:45:35,760
germany as a key and berlin as its value
3066
01:45:35,760 --> 01:45:38,080
not only can you use the update method
3067
01:45:38,080 --> 01:45:39,199
to add a new
3068
01:45:39,199 --> 01:45:41,199
key value pair but you can update an
3069
01:45:41,199 --> 01:45:43,280
existing one let's say we would like to
3070
01:45:43,280 --> 01:45:44,000
change the
3071
01:45:44,000 --> 01:45:47,440
capital of usa so capitals
3072
01:45:47,440 --> 01:45:51,520
dot update parentheses curly braces
3073
01:45:51,520 --> 01:45:54,639
list the key usa colon
3074
01:45:54,639 --> 01:45:57,679
and then we can give the usa a new value
3075
01:45:57,679 --> 01:45:59,440
let's pretend that the united states
3076
01:45:59,440 --> 01:46:01,760
government has decided to relocate
3077
01:46:01,760 --> 01:46:05,199
their capital to las vegas
3078
01:46:05,199 --> 01:46:08,719
so this will now update our key of usa
3079
01:46:08,719 --> 01:46:11,600
with a new value and if i were to print
3080
01:46:11,600 --> 01:46:14,000
all of the key value pairs found within
3081
01:46:14,000 --> 01:46:16,960
my dictionary of capitals the usa has a
3082
01:46:16,960 --> 01:46:17,760
new value
3083
01:46:17,760 --> 01:46:21,119
of las vegas so the last two methods i'm
3084
01:46:21,119 --> 01:46:22,719
going to cover are both pop
3085
01:46:22,719 --> 01:46:25,040
and clear you can use the pop method to
3086
01:46:25,040 --> 01:46:25,760
remove
3087
01:46:25,760 --> 01:46:29,040
a key value pair so capitals
3088
01:46:29,040 --> 01:46:32,800
dot pop parentheses and list the key
3089
01:46:32,800 --> 01:46:34,480
of the key value pair that you would
3090
01:46:34,480 --> 01:46:36,239
like to remove so let's say we would
3091
01:46:36,239 --> 01:46:37,600
like to remove china
3092
01:46:37,600 --> 01:46:39,920
so we'll list that key and then when we
3093
01:46:39,920 --> 01:46:41,360
use the pop method
3094
01:46:41,360 --> 01:46:44,000
that will remove this key value pair
3095
01:46:44,000 --> 01:46:45,280
from my dictionary
3096
01:46:45,280 --> 01:46:47,440
and then of course we have clear as well
3097
01:46:47,440 --> 01:46:49,119
which will just remove everything
3098
01:46:49,119 --> 01:46:52,560
capitals dot clear
3099
01:46:52,560 --> 01:46:55,199
and this will clear my dictionary all
3100
01:46:55,199 --> 01:46:56,080
right everybody
3101
01:46:56,080 --> 01:46:58,880
so in conclusion a dictionary is a
3102
01:46:58,880 --> 01:46:59,840
changeable
3103
01:46:59,840 --> 01:47:02,800
unordered collection of unique key value
3104
01:47:02,800 --> 01:47:03,520
pairs
3105
01:47:03,520 --> 01:47:05,600
they're fast because they use hashing
3106
01:47:05,600 --> 01:47:07,760
and they allow us to access a value
3107
01:47:07,760 --> 01:47:08,400
quickly
3108
01:47:08,400 --> 01:47:10,800
well that's the basics of dictionaries
3109
01:47:10,800 --> 01:47:11,679
in python
3110
01:47:11,679 --> 01:47:13,199
if you would like a copy of all this
3111
01:47:13,199 --> 01:47:15,119
code i will post all of this in the
3112
01:47:15,119 --> 01:47:16,400
comments down below
3113
01:47:16,400 --> 01:47:18,719
but yeah that's how dictionaries work in
3114
01:47:18,719 --> 01:47:20,480
python
3115
01:47:20,480 --> 01:47:22,639
hey what's going on everybody it's bro
3116
01:47:22,639 --> 01:47:24,320
hope you're doing well and in this video
3117
01:47:24,320 --> 01:47:25,679
i'm going to teach you guys all about
3118
01:47:25,679 --> 01:47:26,560
the index
3119
01:47:26,560 --> 01:47:29,840
operator in python so sit back relax
3120
01:47:29,840 --> 01:47:32,800
and enjoy the show
3121
01:47:32,880 --> 01:47:34,800
all right people let's begin i'm going
3122
01:47:34,800 --> 01:47:36,719
to be explaining the index operator
3123
01:47:36,719 --> 01:47:38,159
which is represented by
3124
01:47:38,159 --> 01:47:41,040
a set of square brackets now they give
3125
01:47:41,040 --> 01:47:41,760
access
3126
01:47:41,760 --> 01:47:44,719
to a sequences elements they include but
3127
01:47:44,719 --> 01:47:46,000
are not limited to
3128
01:47:46,000 --> 01:47:48,800
strings lists and tuples for this
3129
01:47:48,800 --> 01:47:50,320
example we're going to be working with
3130
01:47:50,320 --> 01:47:52,080
strings because strings are easy to work
3131
01:47:52,080 --> 01:47:52,560
with
3132
01:47:52,560 --> 01:47:55,199
let's say we have a name and name equals
3133
01:47:55,199 --> 01:47:56,159
whatever your first
3134
01:47:56,159 --> 01:47:58,880
and last name is we can use the index
3135
01:47:58,880 --> 01:48:00,239
operator to access
3136
01:48:00,239 --> 01:48:03,520
an element of the sequence our string
3137
01:48:03,520 --> 01:48:05,840
so let's check to see if the first
3138
01:48:05,840 --> 01:48:07,280
letter in our name
3139
01:48:07,280 --> 01:48:09,440
is lowercase and i should probably
3140
01:48:09,440 --> 01:48:11,040
change that to lowercase for this
3141
01:48:11,040 --> 01:48:11,840
example
3142
01:48:11,840 --> 01:48:14,960
so within an if statement if name and we
3143
01:48:14,960 --> 01:48:16,320
can add a set
3144
01:48:16,320 --> 01:48:19,360
of square brackets after our sequence
3145
01:48:19,360 --> 01:48:22,400
which is a string and we can check
3146
01:48:22,400 --> 01:48:25,199
a given element of our sequence let's
3147
01:48:25,199 --> 01:48:26,000
check to see
3148
01:48:26,000 --> 01:48:29,119
if the first letter is lowercase
3149
01:48:29,119 --> 01:48:31,119
now with computers they always start at
3150
01:48:31,119 --> 01:48:33,199
zero if you need to access the first
3151
01:48:33,199 --> 01:48:33,760
element
3152
01:48:33,760 --> 01:48:36,800
in a sequence that would be index zero
3153
01:48:36,800 --> 01:48:38,639
and then the next sequence well the next
3154
01:48:38,639 --> 01:48:40,719
element in the sequence would be one
3155
01:48:40,719 --> 01:48:42,880
then two then you followed that pattern
3156
01:48:42,880 --> 01:48:44,719
so i would like to check to see if
3157
01:48:44,719 --> 01:48:47,040
name at index zero that would be the
3158
01:48:47,040 --> 01:48:48,639
first character
3159
01:48:48,639 --> 01:48:51,119
is lowercase so there is a method to
3160
01:48:51,119 --> 01:48:52,239
check that
3161
01:48:52,239 --> 01:48:55,440
dot is lower this will return
3162
01:48:55,440 --> 01:48:58,239
true or false if the first letter is
3163
01:48:58,239 --> 01:48:59,119
lowercase
3164
01:48:59,119 --> 01:49:00,960
then our if statement is going to be
3165
01:49:00,960 --> 01:49:03,360
resolved to true
3166
01:49:03,360 --> 01:49:06,239
and what i could do is that i can
3167
01:49:06,239 --> 01:49:07,360
perhaps change it
3168
01:49:07,360 --> 01:49:10,800
to uppercase then so let's reassign
3169
01:49:10,800 --> 01:49:14,239
name to equal name dot
3170
01:49:14,239 --> 01:49:17,520
and there is a capitalize function here
3171
01:49:17,520 --> 01:49:21,520
and then let's print our name print
3172
01:49:21,520 --> 01:49:24,880
name so when we run this
3173
01:49:24,880 --> 01:49:27,520
the first letter in my name is now
3174
01:49:27,520 --> 01:49:28,639
capitalized
3175
01:49:28,639 --> 01:49:30,960
so if you need to access an element
3176
01:49:30,960 --> 01:49:32,880
within a sequence a string or
3177
01:49:32,880 --> 01:49:36,000
list or a tuple you would add a set
3178
01:49:36,000 --> 01:49:38,320
of square brackets afterwards and then
3179
01:49:38,320 --> 01:49:39,599
you list an integer
3180
01:49:39,599 --> 01:49:41,119
of the element that you're trying to
3181
01:49:41,119 --> 01:49:43,599
access so here's a few other examples
3182
01:49:43,599 --> 01:49:44,480
i'm going to turn
3183
01:49:44,480 --> 01:49:47,679
this as well as this into a comment
3184
01:49:47,679 --> 01:49:49,199
and let's say we would like to create
3185
01:49:49,199 --> 01:49:51,360
some substrings and we can do so
3186
01:49:51,360 --> 01:49:54,080
using our index operator so i would like
3187
01:49:54,080 --> 01:49:54,400
to
3188
01:49:54,400 --> 01:49:57,040
create a substring from the first part
3189
01:49:57,040 --> 01:49:57,920
of my name
3190
01:49:57,920 --> 01:50:00,320
so what i'll do is create a new variable
3191
01:50:00,320 --> 01:50:01,440
first
3192
01:50:01,440 --> 01:50:05,599
name equals name index operator
3193
01:50:05,599 --> 01:50:08,880
so you can specify a range for an index
3194
01:50:08,880 --> 01:50:09,679
operator
3195
01:50:09,679 --> 01:50:12,480
so you would set the starting position
3196
01:50:12,480 --> 01:50:14,239
on the left hand side followed by a
3197
01:50:14,239 --> 01:50:16,400
colon and then your ending position
3198
01:50:16,400 --> 01:50:18,400
so i would like the substring to begin
3199
01:50:18,400 --> 01:50:19,679
at index 0 and
3200
01:50:19,679 --> 01:50:22,880
end at 3 so that would be 0 colon
3201
01:50:22,880 --> 01:50:25,679
then three all right and then i would
3202
01:50:25,679 --> 01:50:26,639
like to make this all
3203
01:50:26,639 --> 01:50:29,199
uppercase for some reason and then let's
3204
01:50:29,199 --> 01:50:32,320
print my first name
3205
01:50:33,040 --> 01:50:35,920
so with the index operator we specified
3206
01:50:35,920 --> 01:50:36,639
a range
3207
01:50:36,639 --> 01:50:39,199
we would like to access elements 0
3208
01:50:39,199 --> 01:50:39,679
through
3209
01:50:39,679 --> 01:50:42,080
3. turn them up your case and assign
3210
01:50:42,080 --> 01:50:42,960
them to a new
3211
01:50:42,960 --> 01:50:45,679
variable of first name here's a shortcut
3212
01:50:45,679 --> 01:50:46,800
that you might find useful
3213
01:50:46,800 --> 01:50:49,440
if the index for your range begins with
3214
01:50:49,440 --> 01:50:50,960
zero you can actually delete that you
3215
01:50:50,960 --> 01:50:52,880
don't even necessarily need that zero
3216
01:50:52,880 --> 01:50:55,119
now we just have a colon three and this
3217
01:50:55,119 --> 01:50:57,119
would work the same as it did before
3218
01:50:57,119 --> 01:50:59,119
so that's a useful shortcut and we're
3219
01:50:59,119 --> 01:51:00,960
going to bring it up later if we need to
3220
01:51:00,960 --> 01:51:03,920
access the last element in a sequence
3221
01:51:03,920 --> 01:51:05,760
this time let's create a
3222
01:51:05,760 --> 01:51:09,599
substring of last name so last
3223
01:51:09,599 --> 01:51:13,599
name equals name index operator
3224
01:51:13,599 --> 01:51:16,719
this time i would like to access the
3225
01:51:16,719 --> 01:51:20,239
element at index let's see 0 1
3226
01:51:20,239 --> 01:51:24,080
2 3 4. all right so i'm going to say
3227
01:51:24,080 --> 01:51:27,360
for colon and we're not really sure
3228
01:51:27,360 --> 01:51:29,440
where our name our last name is going to
3229
01:51:29,440 --> 01:51:30,080
end
3230
01:51:30,080 --> 01:51:32,639
so what you could do is just say colon
3231
01:51:32,639 --> 01:51:33,360
and then leave
3232
01:51:33,360 --> 01:51:36,800
the next index blank so that will be
3233
01:51:36,800 --> 01:51:38,639
the index beginning at four and
3234
01:51:38,639 --> 01:51:40,159
everything afterwards
3235
01:51:40,159 --> 01:51:42,960
and let's say two lower to make this all
3236
01:51:42,960 --> 01:51:44,639
lowercase for some reason
3237
01:51:44,639 --> 01:51:48,880
so let's print our last name print
3238
01:51:48,880 --> 01:51:52,400
last name so this should
3239
01:51:52,400 --> 01:51:56,480
take everything starting at index 4
3240
01:51:56,480 --> 01:51:58,880
and after that and turn it into its own
3241
01:51:58,880 --> 01:52:01,040
substring of last name
3242
01:52:01,040 --> 01:52:03,840
now you can access the last element in a
3243
01:52:03,840 --> 01:52:05,679
sequence by using what's called
3244
01:52:05,679 --> 01:52:08,880
negative indexing so let's add
3245
01:52:08,880 --> 01:52:11,119
a exclamation point to the end of our
3246
01:52:11,119 --> 01:52:13,040
name so let's say
3247
01:52:13,040 --> 01:52:16,960
we have a new variable called last
3248
01:52:16,960 --> 01:52:21,040
character and i would like to store
3249
01:52:21,040 --> 01:52:23,599
the last character in my name to this
3250
01:52:23,599 --> 01:52:24,080
new
3251
01:52:24,080 --> 01:52:27,040
variable this new substring so to access
3252
01:52:27,040 --> 01:52:27,840
the last
3253
01:52:27,840 --> 01:52:31,199
element in a sequence within the index
3254
01:52:31,199 --> 01:52:32,320
operator we can use
3255
01:52:32,320 --> 01:52:35,520
negative indexing so the last element
3256
01:52:35,520 --> 01:52:36,159
would be
3257
01:52:36,159 --> 01:52:38,880
negative one all right and then let's
3258
01:52:38,880 --> 01:52:39,360
print
3259
01:52:39,360 --> 01:52:42,400
whatever our last character is so this
3260
01:52:42,400 --> 01:52:45,679
should print an exclamation point
3261
01:52:45,679 --> 01:52:48,080
yep which did so then if this was
3262
01:52:48,080 --> 01:52:48,880
negative 2
3263
01:52:48,880 --> 01:52:51,440
that would be the second to last element
3264
01:52:51,440 --> 01:52:52,800
in a sequence
3265
01:52:52,800 --> 01:52:55,599
so now that would be e because e is in
3266
01:52:55,599 --> 01:52:56,719
the second to last
3267
01:52:56,719 --> 01:52:59,199
element in my sequence all right
3268
01:52:59,199 --> 01:53:01,119
everybody so that's the basics of the
3269
01:53:01,119 --> 01:53:02,159
index operator
3270
01:53:02,159 --> 01:53:04,880
you can add a set of square brackets
3271
01:53:04,880 --> 01:53:05,920
after a string
3272
01:53:05,920 --> 01:53:08,480
a list or a tuple and then list an
3273
01:53:08,480 --> 01:53:10,080
integer or a range
3274
01:53:10,080 --> 01:53:11,280
of the elements that you're trying to
3275
01:53:11,280 --> 01:53:13,520
access so if you would like a copy of
3276
01:53:13,520 --> 01:53:15,280
all this code i will post everything in
3277
01:53:15,280 --> 01:53:16,560
the comments down below
3278
01:53:16,560 --> 01:53:18,480
don't be afraid to help me out and smash
3279
01:53:18,480 --> 01:53:20,320
that like button drop a comment down
3280
01:53:20,320 --> 01:53:20,960
below
3281
01:53:20,960 --> 01:53:22,480
and subscribe if you'd like to become a
3282
01:53:22,480 --> 01:53:24,159
fellow bro
3283
01:53:24,159 --> 01:53:26,000
hey what's going on everybody it's you
3284
01:53:26,000 --> 01:53:27,199
bro hope you're doing well
3285
01:53:27,199 --> 01:53:28,880
and in this video i'm going to explain
3286
01:53:28,880 --> 01:53:31,199
how functions work in python so
3287
01:53:31,199 --> 01:53:35,679
sit back relax and enjoy the show
3288
01:53:36,080 --> 01:53:37,679
if you find this video helpful please
3289
01:53:37,679 --> 01:53:39,119
remember to like
3290
01:53:39,119 --> 01:53:41,599
comment and subscribe your support will
3291
01:53:41,599 --> 01:53:43,440
help keep this channel running
3292
01:53:43,440 --> 01:53:46,480
okay let's do this thing a function
3293
01:53:46,480 --> 01:53:49,119
is a block of code which is executed
3294
01:53:49,119 --> 01:53:50,560
only when it is called
3295
01:53:50,560 --> 01:53:53,280
this process is also known as invoking a
3296
01:53:53,280 --> 01:53:54,159
function
3297
01:53:54,159 --> 01:53:56,000
with programming we try not to repeat
3298
01:53:56,000 --> 01:53:57,280
code if we don't have to
3299
01:53:57,280 --> 01:53:59,599
that's why functions are useful because
3300
01:53:59,599 --> 01:54:01,040
our function can perform
3301
01:54:01,040 --> 01:54:03,599
some specific task for us some block of
3302
01:54:03,599 --> 01:54:04,480
code for us
3303
01:54:04,480 --> 01:54:06,800
whenever it is called so we only need to
3304
01:54:06,800 --> 01:54:08,159
write that block of code
3305
01:54:08,159 --> 01:54:10,159
once and if we need to repeat it we just
3306
01:54:10,159 --> 01:54:12,239
call this function one more time
3307
01:54:12,239 --> 01:54:13,840
so let's define and create a function
3308
01:54:13,840 --> 01:54:15,920
we'll create a function called hello
3309
01:54:15,920 --> 01:54:17,520
and in order to define a function we'll
3310
01:54:17,520 --> 01:54:19,040
type def
3311
01:54:19,040 --> 01:54:21,760
and then a unique function name so let's
3312
01:54:21,760 --> 01:54:23,280
call this function hello
3313
01:54:23,280 --> 01:54:25,119
and with functions they always end with
3314
01:54:25,119 --> 01:54:26,960
a set of parentheses
3315
01:54:26,960 --> 01:54:29,040
and lastly to finish defining this
3316
01:54:29,040 --> 01:54:30,159
function we'll add
3317
01:54:30,159 --> 01:54:33,040
a colon at the end so if you go down to
3318
01:54:33,040 --> 01:54:33,840
the next line
3319
01:54:33,840 --> 01:54:36,320
take notice that my cursor is now
3320
01:54:36,320 --> 01:54:37,280
indented
3321
01:54:37,280 --> 01:54:39,679
any code underneath a function that is
3322
01:54:39,679 --> 01:54:40,480
indented
3323
01:54:40,480 --> 01:54:42,880
belongs to that specific function and
3324
01:54:42,880 --> 01:54:44,320
will only execute
3325
01:54:44,320 --> 01:54:47,119
whenever that function is called so for
3326
01:54:47,119 --> 01:54:48,880
now we need to type in something
3327
01:54:48,880 --> 01:54:50,080
if you don't know what you want your
3328
01:54:50,080 --> 01:54:52,000
function to do you can always type pass
3329
01:54:52,000 --> 01:54:52,719
for now
3330
01:54:52,719 --> 01:54:56,320
but let's print something print the word
3331
01:54:56,320 --> 01:55:00,080
hello and in order to call this function
3332
01:55:00,080 --> 01:55:02,639
all we need to do within our program is
3333
01:55:02,639 --> 01:55:04,560
type the name of the function which is
3334
01:55:04,560 --> 01:55:05,360
hello
3335
01:55:05,360 --> 01:55:07,280
and then add a set of parentheses
3336
01:55:07,280 --> 01:55:09,199
because functions always end with a set
3337
01:55:09,199 --> 01:55:10,320
of parentheses
3338
01:55:10,320 --> 01:55:13,199
so what this will do is execute my
3339
01:55:13,199 --> 01:55:13,840
function
3340
01:55:13,840 --> 01:55:15,679
once and perform this block of code
3341
01:55:15,679 --> 01:55:17,040
whenever it is called
3342
01:55:17,040 --> 01:55:19,599
if i were to remove this well our
3343
01:55:19,599 --> 01:55:21,119
program currently isn't going to do
3344
01:55:21,119 --> 01:55:21,760
anything
3345
01:55:21,760 --> 01:55:24,080
because functions only execute their
3346
01:55:24,080 --> 01:55:25,119
block of code
3347
01:55:25,119 --> 01:55:27,840
only when it is called so this time
3348
01:55:27,840 --> 01:55:28,960
let's print the word
3349
01:55:28,960 --> 01:55:31,920
hello three times so i will call this
3350
01:55:31,920 --> 01:55:32,480
function
3351
01:55:32,480 --> 01:55:35,599
three separate times hello hello hello
3352
01:55:35,599 --> 01:55:38,320
so when i run this our hello function
3353
01:55:38,320 --> 01:55:39,199
will be called
3354
01:55:39,199 --> 01:55:41,840
three separate times now with functions
3355
01:55:41,840 --> 01:55:44,400
they are not limited to just one line
3356
01:55:44,400 --> 01:55:46,719
this is an entire block of code so maybe
3357
01:55:46,719 --> 01:55:48,880
we can do something else too
3358
01:55:48,880 --> 01:55:52,000
let's print have a nice
3359
01:55:52,000 --> 01:55:55,280
day now if i were to run this my hello
3360
01:55:55,280 --> 01:55:56,800
function is going to execute
3361
01:55:56,800 --> 01:55:59,199
this entire block of code three separate
3362
01:55:59,199 --> 01:56:00,400
times hello
3363
01:56:00,400 --> 01:56:03,119
have a nice day now an important feature
3364
01:56:03,119 --> 01:56:04,000
of functions
3365
01:56:04,000 --> 01:56:06,320
is that we can send our function some
3366
01:56:06,320 --> 01:56:07,040
information
3367
01:56:07,040 --> 01:56:08,880
and our function can do something with
3368
01:56:08,880 --> 01:56:10,480
that information that it receives
3369
01:56:10,480 --> 01:56:11,920
let's say this time i would like to
3370
01:56:11,920 --> 01:56:14,400
print hello plus somebody's name
3371
01:56:14,400 --> 01:56:16,320
currently name is an unresolved
3372
01:56:16,320 --> 01:56:17,920
reference what we can do
3373
01:56:17,920 --> 01:56:19,920
when we call this function is send our
3374
01:56:19,920 --> 01:56:21,599
function some information
3375
01:56:21,599 --> 01:56:23,920
this can be a value a variable a
3376
01:56:23,920 --> 01:56:24,719
collection
3377
01:56:24,719 --> 01:56:26,560
all sorts of different things so if you
3378
01:56:26,560 --> 01:56:27,920
need to send your function some
3379
01:56:27,920 --> 01:56:28,880
information
3380
01:56:28,880 --> 01:56:30,320
within the parentheses when you call
3381
01:56:30,320 --> 01:56:32,639
that function just list the data that
3382
01:56:32,639 --> 01:56:34,080
you want to send your function
3383
01:56:34,080 --> 01:56:36,000
so let's say i would like to send my
3384
01:56:36,000 --> 01:56:37,520
function a string
3385
01:56:37,520 --> 01:56:40,239
value of bro whatever your first name is
3386
01:56:40,239 --> 01:56:41,760
when you send information
3387
01:56:41,760 --> 01:56:45,040
to a function these are called arguments
3388
01:56:45,040 --> 01:56:46,880
they are the information that you're
3389
01:56:46,880 --> 01:56:48,800
sending to a function
3390
01:56:48,800 --> 01:56:50,880
and when you define that function you
3391
01:56:50,880 --> 01:56:52,239
need a matching
3392
01:56:52,239 --> 01:56:55,360
set of what is known as parameters
3393
01:56:55,360 --> 01:56:57,520
so our hello function is going to
3394
01:56:57,520 --> 01:57:00,320
receive one argument a string value
3395
01:57:00,320 --> 01:57:03,360
we need a matching number of parameters
3396
01:57:03,360 --> 01:57:05,199
currently this isn't going to work you
3397
01:57:05,199 --> 01:57:06,719
can see here that hello
3398
01:57:06,719 --> 01:57:09,520
takes zero positional arguments but one
3399
01:57:09,520 --> 01:57:10,480
was given
3400
01:57:10,480 --> 01:57:13,360
our function needs a matching number of
3401
01:57:13,360 --> 01:57:14,400
parameters
3402
01:57:14,400 --> 01:57:17,040
so think of it this way when we receive
3403
01:57:17,040 --> 01:57:17,920
this value
3404
01:57:17,920 --> 01:57:20,400
we're going to give a nickname to this
3405
01:57:20,400 --> 01:57:21,040
value
3406
01:57:21,040 --> 01:57:24,159
so when we receive the string value what
3407
01:57:24,159 --> 01:57:25,199
do we want to call it
3408
01:57:25,199 --> 01:57:27,840
well let's call it name so in order to
3409
01:57:27,840 --> 01:57:29,280
add parameters
3410
01:57:29,280 --> 01:57:31,280
to your function just list the
3411
01:57:31,280 --> 01:57:32,719
parameters within
3412
01:57:32,719 --> 01:57:35,040
the parentheses of that function and
3413
01:57:35,040 --> 01:57:36,639
well this should work now
3414
01:57:36,639 --> 01:57:40,000
we have a matching set of arguments and
3415
01:57:40,000 --> 01:57:42,080
parameters when we call the hello
3416
01:57:42,080 --> 01:57:44,159
function this time we are sending
3417
01:57:44,159 --> 01:57:46,800
one argument over and when our hello
3418
01:57:46,800 --> 01:57:47,440
function
3419
01:57:47,440 --> 01:57:49,360
receives this argument we're going to
3420
01:57:49,360 --> 01:57:52,239
give it a temporary nickname of
3421
01:57:52,239 --> 01:57:53,760
name or you can call this whatever you
3422
01:57:53,760 --> 01:57:55,599
want to and then we can use
3423
01:57:55,599 --> 01:57:58,000
this value for whatever we want within
3424
01:57:58,000 --> 01:57:58,960
our function
3425
01:57:58,960 --> 01:58:00,880
now if we were to run this this will
3426
01:58:00,880 --> 01:58:02,320
print hello bro
3427
01:58:02,320 --> 01:58:04,880
have a nice day now i can send maybe a
3428
01:58:04,880 --> 01:58:05,760
different value
3429
01:58:05,760 --> 01:58:09,280
let's say hello and then i will send a
3430
01:58:09,280 --> 01:58:11,520
string value of dude
3431
01:58:11,520 --> 01:58:14,719
hello dude have a nice day so this isn't
3432
01:58:14,719 --> 01:58:15,599
limited to
3433
01:58:15,599 --> 01:58:18,159
just values we can send variables to
3434
01:58:18,159 --> 01:58:19,440
let's say we have a
3435
01:58:19,440 --> 01:58:23,360
variable called name equals
3436
01:58:23,360 --> 01:58:26,880
bro and this time i'm going to send
3437
01:58:26,880 --> 01:58:30,719
this variable over hello and then send
3438
01:58:30,719 --> 01:58:34,000
name so these don't need to be the
3439
01:58:34,000 --> 01:58:37,760
exact same let's say this is
3440
01:58:37,760 --> 01:58:40,960
my name and i'm going to send
3441
01:58:40,960 --> 01:58:45,440
my name over and i will temporarily give
3442
01:58:45,440 --> 01:58:48,960
this value a nickname of just name
3443
01:58:48,960 --> 01:58:50,880
and then i can use this variable for
3444
01:58:50,880 --> 01:58:52,560
something hello bro
3445
01:58:52,560 --> 01:58:55,040
have a nice day now with arguments you
3446
01:58:55,040 --> 01:58:55,760
can send
3447
01:58:55,760 --> 01:58:58,480
more than one value over let's say this
3448
01:58:58,480 --> 01:58:59,679
time i would like to send
3449
01:58:59,679 --> 01:59:02,800
two values over so let's send pro
3450
01:59:02,800 --> 01:59:05,679
maybe for a first name and code for a
3451
01:59:05,679 --> 01:59:06,719
second name but
3452
01:59:06,719 --> 01:59:08,239
you can use your first name and last
3453
01:59:08,239 --> 01:59:10,800
name so this isn't going to work because
3454
01:59:10,800 --> 01:59:13,119
we do not have a matching set
3455
01:59:13,119 --> 01:59:15,520
of arguments and parameters we're
3456
01:59:15,520 --> 01:59:16,159
sending
3457
01:59:16,159 --> 01:59:19,040
two arguments over but our function is
3458
01:59:19,040 --> 01:59:20,159
only designed
3459
01:59:20,159 --> 01:59:23,199
to accept one argument because we have
3460
01:59:23,199 --> 01:59:26,400
one parameter so let's rename
3461
01:59:26,400 --> 01:59:30,159
name as first name and we need
3462
01:59:30,159 --> 01:59:32,400
two parameters this time so what are we
3463
01:59:32,400 --> 01:59:33,280
going to call
3464
01:59:33,280 --> 01:59:35,599
this value when we receive it let's call
3465
01:59:35,599 --> 01:59:38,320
this last name
3466
01:59:38,320 --> 01:59:40,960
so this time we are going to receive two
3467
01:59:40,960 --> 01:59:42,080
values
3468
01:59:42,080 --> 01:59:44,080
they will be nicknamed first name and
3469
01:59:44,080 --> 01:59:45,679
last name so this time
3470
01:59:45,679 --> 01:59:48,719
let's print hello first name
3471
01:59:48,719 --> 01:59:52,400
maybe i'll add a space plus
3472
01:59:52,400 --> 01:59:56,480
last name and this is now valid
3473
01:59:56,480 --> 01:59:59,040
hello bro code have a nice day with
3474
01:59:59,040 --> 01:59:59,760
arguments
3475
01:59:59,760 --> 02:00:02,000
you can mix and match the data types
3476
02:00:02,000 --> 02:00:03,040
that you're sending
3477
02:00:03,040 --> 02:00:05,840
as arguments so currently we're sending
3478
02:00:05,840 --> 02:00:06,239
two
3479
02:00:06,239 --> 02:00:08,880
string values as arguments this time
3480
02:00:08,880 --> 02:00:10,960
let's send an integer value
3481
02:00:10,960 --> 02:00:13,440
let's say we would like to send an edge
3482
02:00:13,440 --> 02:00:15,280
over to our hello function
3483
02:00:15,280 --> 02:00:18,239
so i'll just send maybe the value of 21.
3484
02:00:18,239 --> 02:00:20,080
so currently this isn't going to work
3485
02:00:20,080 --> 02:00:22,639
because the hello function takes two
3486
02:00:22,639 --> 02:00:23,920
positional arguments
3487
02:00:23,920 --> 02:00:26,159
but three were given when we call this
3488
02:00:26,159 --> 02:00:27,840
hello function we're sending
3489
02:00:27,840 --> 02:00:30,000
this argument over but our function
3490
02:00:30,000 --> 02:00:31,440
doesn't know what to do with this
3491
02:00:31,440 --> 02:00:32,159
argument
3492
02:00:32,159 --> 02:00:34,880
so we need a matching parameter for this
3493
02:00:34,880 --> 02:00:36,239
value that it receives
3494
02:00:36,239 --> 02:00:39,040
so let's call this value maybe edge as a
3495
02:00:39,040 --> 02:00:40,400
temporary nickname
3496
02:00:40,400 --> 02:00:42,239
and then we can do something with this
3497
02:00:42,239 --> 02:00:43,920
value when we receive it
3498
02:00:43,920 --> 02:00:47,040
so let's print an additional message
3499
02:00:47,040 --> 02:00:50,880
perhaps u r plus
3500
02:00:50,880 --> 02:00:54,719
age plus years
3501
02:00:54,719 --> 02:00:57,760
old so if you need to display an
3502
02:00:57,760 --> 02:00:59,840
integer or number along with the string
3503
02:00:59,840 --> 02:01:01,920
you do need to convert it to a string
3504
02:01:01,920 --> 02:01:04,800
so let's just cast our edge as a string
3505
02:01:04,800 --> 02:01:06,239
when we display this
3506
02:01:06,239 --> 02:01:08,320
so this should work now we now have a
3507
02:01:08,320 --> 02:01:09,599
matching number
3508
02:01:09,599 --> 02:01:13,119
of arguments and parameters so this will
3509
02:01:13,119 --> 02:01:14,000
display
3510
02:01:14,000 --> 02:01:16,880
hello bro code you are 21 years old have
3511
02:01:16,880 --> 02:01:17,840
a nice day
3512
02:01:17,840 --> 02:01:20,400
so ladies and gentlemen in conclusion a
3513
02:01:20,400 --> 02:01:20,960
function
3514
02:01:20,960 --> 02:01:23,520
is a block of code which is executed
3515
02:01:23,520 --> 02:01:24,880
only when it is called
3516
02:01:24,880 --> 02:01:27,520
if i were to remove this function call
3517
02:01:27,520 --> 02:01:28,239
well then
3518
02:01:28,239 --> 02:01:30,320
our program is just going to completely
3519
02:01:30,320 --> 02:01:31,599
ignore this function and
3520
02:01:31,599 --> 02:01:34,400
this block of code would not be executed
3521
02:01:34,400 --> 02:01:36,480
when we call this function we can pass
3522
02:01:36,480 --> 02:01:39,360
information known as arguments but we
3523
02:01:39,360 --> 02:01:40,080
need a
3524
02:01:40,080 --> 02:01:42,880
matching number of parameters set up to
3525
02:01:42,880 --> 02:01:43,599
receive
3526
02:01:43,599 --> 02:01:46,239
these arguments there are a few advanced
3527
02:01:46,239 --> 02:01:48,159
features of parameters but
3528
02:01:48,159 --> 02:01:50,000
i'll hopefully get to that in future
3529
02:01:50,000 --> 02:01:51,920
videos we'll just stick with the basics
3530
02:01:51,920 --> 02:01:53,599
for now of functions
3531
02:01:53,599 --> 02:01:55,920
so that's the basics of functions if you
3532
02:01:55,920 --> 02:01:57,520
would like a copy of this code
3533
02:01:57,520 --> 02:01:59,280
i will post this in the comments down
3534
02:01:59,280 --> 02:02:01,679
below but yeah that is how functions
3535
02:02:01,679 --> 02:02:04,639
work in python hey what's going on
3536
02:02:04,639 --> 02:02:06,560
everybody it's you bro hope you're doing
3537
02:02:06,560 --> 02:02:06,880
well
3538
02:02:06,880 --> 02:02:08,480
and in this video i'm going to explain
3539
02:02:08,480 --> 02:02:10,400
the return statement in python so
3540
02:02:10,400 --> 02:02:15,119
sit back relax and enjoy the show
3541
02:02:15,520 --> 02:02:18,000
all right people let's begin the return
3542
02:02:18,000 --> 02:02:19,920
statement is used within functions
3543
02:02:19,920 --> 02:02:23,119
to send python values or objects back to
3544
02:02:23,119 --> 02:02:23,920
the caller
3545
02:02:23,920 --> 02:02:26,320
these values or objects are known as the
3546
02:02:26,320 --> 02:02:26,960
function's
3547
02:02:26,960 --> 02:02:29,040
return value so do you know how with
3548
02:02:29,040 --> 02:02:30,239
functions we can pass
3549
02:02:30,239 --> 02:02:32,560
arguments to a function and then our
3550
02:02:32,560 --> 02:02:34,000
function can do something with those
3551
02:02:34,000 --> 02:02:34,800
arguments
3552
02:02:34,800 --> 02:02:36,880
well not only that but our function can
3553
02:02:36,880 --> 02:02:38,639
pass some value or
3554
02:02:38,639 --> 02:02:41,040
object back to the caller so what we'll
3555
02:02:41,040 --> 02:02:43,119
do for this example is create
3556
02:02:43,119 --> 02:02:45,440
a function that will simply multiply two
3557
02:02:45,440 --> 02:02:46,480
numbers together
3558
02:02:46,480 --> 02:02:49,119
and return the result back to the caller
3559
02:02:49,119 --> 02:02:50,400
so let's define
3560
02:02:50,400 --> 02:02:52,639
a function called multiply and we'll set
3561
02:02:52,639 --> 02:02:54,560
up two parameters for this function
3562
02:02:54,560 --> 02:02:58,239
let's say number one and number two
3563
02:02:58,239 --> 02:03:01,760
and what we'll do is say result
3564
02:03:01,760 --> 02:03:05,679
equals number one times number two
3565
02:03:05,679 --> 02:03:07,199
and then at the end we can return
3566
02:03:07,199 --> 02:03:08,960
something what do we want to return
3567
02:03:08,960 --> 02:03:11,199
let's return whatever the result is now
3568
02:03:11,199 --> 02:03:13,119
when we call this function we will pass
3569
02:03:13,119 --> 02:03:16,080
in two values as arguments let's say we
3570
02:03:16,080 --> 02:03:17,199
would like to multiply
3571
02:03:17,199 --> 02:03:20,320
six times eight so when we run this well
3572
02:03:20,320 --> 02:03:22,239
we can't actually see anything
3573
02:03:22,239 --> 02:03:24,000
when we return the result after
3574
02:03:24,000 --> 02:03:25,360
multiplying numbers
3575
02:03:25,360 --> 02:03:27,599
one and two it's going to return the
3576
02:03:27,599 --> 02:03:29,520
result back to the caller
3577
02:03:29,520 --> 02:03:31,280
in order to see the value that is
3578
02:03:31,280 --> 02:03:32,880
returned back to the caller
3579
02:03:32,880 --> 02:03:35,040
one way in which we can do this is to
3580
02:03:35,040 --> 02:03:35,920
print
3581
02:03:35,920 --> 02:03:38,000
the value that is returned within a
3582
02:03:38,000 --> 02:03:39,040
print statement
3583
02:03:39,040 --> 02:03:41,520
so what we'll do is print the value that
3584
02:03:41,520 --> 02:03:44,000
is returned after calling the multiply
3585
02:03:44,000 --> 02:03:44,800
function
3586
02:03:44,800 --> 02:03:47,920
so 6 times 8 is really great 6 times 8
3587
02:03:47,920 --> 02:03:48,960
is 48
3588
02:03:48,960 --> 02:03:51,280
now one other thing that we can do is
3589
02:03:51,280 --> 02:03:53,599
store the returned value within a
3590
02:03:53,599 --> 02:03:54,480
variable
3591
02:03:54,480 --> 02:03:58,159
so let's say x equals multiply
3592
02:03:58,159 --> 02:04:00,320
6 times 8 and then we will print
3593
02:04:00,320 --> 02:04:02,400
whatever x is
3594
02:04:02,400 --> 02:04:05,599
so the result is still the same of 48
3595
02:04:05,599 --> 02:04:07,599
now with the return value there is
3596
02:04:07,599 --> 02:04:09,520
another way in which we could write this
3597
02:04:09,520 --> 02:04:10,239
that uses
3598
02:04:10,239 --> 02:04:12,159
less lines of code so we'll delete this
3599
02:04:12,159 --> 02:04:14,560
first line and instead of returning
3600
02:04:14,560 --> 02:04:16,800
a variable called result what we'll do
3601
02:04:16,800 --> 02:04:18,159
is just return
3602
02:04:18,159 --> 02:04:20,960
number one times number two and this
3603
02:04:20,960 --> 02:04:23,119
would work the same as it does before
3604
02:04:23,119 --> 02:04:25,280
but it's written in less lines of code
3605
02:04:25,280 --> 02:04:26,880
so that's another way in which you could
3606
02:04:26,880 --> 02:04:27,840
write this
3607
02:04:27,840 --> 02:04:29,360
all right everybody so that is the
3608
02:04:29,360 --> 02:04:31,520
return statement functions can send
3609
02:04:31,520 --> 02:04:34,159
python values or objects back to the
3610
02:04:34,159 --> 02:04:34,960
caller
3611
02:04:34,960 --> 02:04:36,880
these values or objects are known as the
3612
02:04:36,880 --> 02:04:38,000
functions return
3613
02:04:38,000 --> 02:04:40,400
value you can either print this value
3614
02:04:40,400 --> 02:04:42,400
directly to the console window
3615
02:04:42,400 --> 02:04:44,960
or you can store it within a variable or
3616
02:04:44,960 --> 02:04:46,159
some other location
3617
02:04:46,159 --> 02:04:48,159
and well ladies and gentlemen that is
3618
02:04:48,159 --> 02:04:51,360
how the return statement works in python
3619
02:04:51,360 --> 02:04:53,920
what's going on everybody it's bro hope
3620
02:04:53,920 --> 02:04:54,719
you're doing well
3621
02:04:54,719 --> 02:04:56,239
and in this video i'm going to explain
3622
02:04:56,239 --> 02:04:58,239
keyword arguments in python so
3623
02:04:58,239 --> 02:05:02,800
sit back relax and enjoy the show
3624
02:05:02,960 --> 02:05:05,199
all right everybody let's discuss
3625
02:05:05,199 --> 02:05:06,400
keyword arguments
3626
02:05:06,400 --> 02:05:09,280
these are arguments that are preceded by
3627
02:05:09,280 --> 02:05:11,599
an identifier when we pass them to a
3628
02:05:11,599 --> 02:05:12,480
function
3629
02:05:12,480 --> 02:05:14,320
the order of the arguments doesn't
3630
02:05:14,320 --> 02:05:16,560
matter unlike positional arguments
3631
02:05:16,560 --> 02:05:18,400
which do matter and we've been working
3632
02:05:18,400 --> 02:05:20,320
with positional arguments already
3633
02:05:20,320 --> 02:05:21,920
and python knows the names of the
3634
02:05:21,920 --> 02:05:24,159
arguments that our function receives
3635
02:05:24,159 --> 02:05:26,639
when we use keyword arguments here's an
3636
02:05:26,639 --> 02:05:27,520
example of us
3637
02:05:27,520 --> 02:05:29,199
using positional arguments and then i'll
3638
02:05:29,199 --> 02:05:30,800
demonstrate the benefits of
3639
02:05:30,800 --> 02:05:32,960
keyword arguments let's say we have a
3640
02:05:32,960 --> 02:05:34,239
function called hello
3641
02:05:34,239 --> 02:05:36,560
that accepts three arguments a first
3642
02:05:36,560 --> 02:05:38,320
name a middle name and a last name
3643
02:05:38,320 --> 02:05:40,480
and the output will be hello first name
3644
02:05:40,480 --> 02:05:41,840
middle name last name
3645
02:05:41,840 --> 02:05:43,440
so if i were to run this this will
3646
02:05:43,440 --> 02:05:45,360
display hello bro
3647
02:05:45,360 --> 02:05:47,840
dude code so we're using positional
3648
02:05:47,840 --> 02:05:48,560
arguments
3649
02:05:48,560 --> 02:05:50,719
the order of the arguments does matter
3650
02:05:50,719 --> 02:05:52,000
what do you think's gonna happen if i
3651
02:05:52,000 --> 02:05:54,079
were to replace some of these values
3652
02:05:54,079 --> 02:05:56,639
let's say i will swap the first argument
3653
02:05:56,639 --> 02:05:57,840
with the
3654
02:05:57,840 --> 02:06:01,040
third argument so let's try it again
3655
02:06:01,040 --> 02:06:04,320
hello code dude bro so obviously the
3656
02:06:04,320 --> 02:06:05,840
order of the arguments matters
3657
02:06:05,840 --> 02:06:07,920
and now if we were to use keyword
3658
02:06:07,920 --> 02:06:09,440
arguments then the order of the
3659
02:06:09,440 --> 02:06:10,320
arguments
3660
02:06:10,320 --> 02:06:12,800
doesn't matter but with each argument we
3661
02:06:12,800 --> 02:06:15,040
need to precede each argument with
3662
02:06:15,040 --> 02:06:18,159
a unique identifier and that identifier
3663
02:06:18,159 --> 02:06:20,239
is the name of the parameter we want to
3664
02:06:20,239 --> 02:06:22,239
associate each argument with
3665
02:06:22,239 --> 02:06:25,040
so for my intended last name i will use
3666
02:06:25,040 --> 02:06:26,400
an identifier of
3667
02:06:26,400 --> 02:06:29,520
last to match this parameter so
3668
02:06:29,520 --> 02:06:32,400
dude will be middle middle equals dude
3669
02:06:32,400 --> 02:06:33,119
and bro
3670
02:06:33,119 --> 02:06:35,040
will be first even though they're not in
3671
02:06:35,040 --> 02:06:36,880
the correct order so let's try this
3672
02:06:36,880 --> 02:06:37,440
again
3673
02:06:37,440 --> 02:06:40,880
and my output is hello bro dude code
3674
02:06:40,880 --> 02:06:43,119
so that's all about keyword arguments
3675
02:06:43,119 --> 02:06:44,239
they are arguments
3676
02:06:44,239 --> 02:06:46,719
preceded with an identifier when we pass
3677
02:06:46,719 --> 02:06:48,000
them to a function
3678
02:06:48,000 --> 02:06:49,679
the order of the arguments doesn't
3679
02:06:49,679 --> 02:06:51,520
matter unlike positional arguments
3680
02:06:51,520 --> 02:06:53,599
which do matter and then python knows
3681
02:06:53,599 --> 02:06:54,800
the names of the arguments
3682
02:06:54,800 --> 02:06:56,800
that our function receives when we use
3683
02:06:56,800 --> 02:06:58,079
keyword arguments
3684
02:06:58,079 --> 02:07:00,239
so that's it for keyword arguments if
3685
02:07:00,239 --> 02:07:02,079
you would like a copy of this code
3686
02:07:02,079 --> 02:07:03,920
i will post this in the comment section
3687
02:07:03,920 --> 02:07:06,159
down below but yeah that's how keyword
3688
02:07:06,159 --> 02:07:06,800
arguments
3689
02:07:06,800 --> 02:07:10,239
work in python hey what's going on
3690
02:07:10,239 --> 02:07:12,079
everybody it's you bro hope you're doing
3691
02:07:12,079 --> 02:07:13,520
well and in this video i'm going to
3692
02:07:13,520 --> 02:07:14,159
explain
3693
02:07:14,159 --> 02:07:16,719
nested function calls in python so sit
3694
02:07:16,719 --> 02:07:17,280
back
3695
02:07:17,280 --> 02:07:21,199
relax and enjoy the show
3696
02:07:21,520 --> 02:07:24,000
all right people nested function calls
3697
02:07:24,000 --> 02:07:25,520
these are function calls
3698
02:07:25,520 --> 02:07:27,920
inside of other function calls and this
3699
02:07:27,920 --> 02:07:29,119
is possible because
3700
02:07:29,119 --> 02:07:31,520
certain functions will return a value
3701
02:07:31,520 --> 02:07:33,679
and we can immediately use that value as
3702
02:07:33,679 --> 02:07:35,920
an argument for the next function
3703
02:07:35,920 --> 02:07:37,760
so here's an example let's say that we
3704
02:07:37,760 --> 02:07:39,440
have the small program
3705
02:07:39,440 --> 02:07:41,679
where we will ask a user to type in a
3706
02:07:41,679 --> 02:07:43,199
whole positive number
3707
02:07:43,199 --> 02:07:45,119
and let's say i don't let's say i type
3708
02:07:45,119 --> 02:07:47,599
in negative 3.14
3709
02:07:47,599 --> 02:07:50,000
and then i'm storing that value within a
3710
02:07:50,000 --> 02:07:50,639
variable
3711
02:07:50,639 --> 02:07:52,719
named num what i'm going to do is
3712
02:07:52,719 --> 02:07:54,320
convert that number
3713
02:07:54,320 --> 02:07:55,920
to a floating point number because
3714
02:07:55,920 --> 02:07:57,360
currently it's a string when you accept
3715
02:07:57,360 --> 02:07:58,400
user input
3716
02:07:58,400 --> 02:08:01,280
i will find the absolute value of num
3717
02:08:01,280 --> 02:08:03,119
and then round it to the nearest whole
3718
02:08:03,119 --> 02:08:03,599
number
3719
02:08:03,599 --> 02:08:05,440
and then print the value so we can
3720
02:08:05,440 --> 02:08:07,199
actually do this using less lines of
3721
02:08:07,199 --> 02:08:09,360
code with nested function calls
3722
02:08:09,360 --> 02:08:11,440
so let's start with the first function
3723
02:08:11,440 --> 02:08:13,520
which will be accepting some user input
3724
02:08:13,520 --> 02:08:16,159
the input function so this will be in
3725
02:08:16,159 --> 02:08:17,199
the very center
3726
02:08:17,199 --> 02:08:19,760
of our nested function calls and then
3727
02:08:19,760 --> 02:08:21,760
the next function we would like to apply
3728
02:08:21,760 --> 02:08:22,239
to
3729
02:08:22,239 --> 02:08:24,480
the variable that is returned the value
3730
02:08:24,480 --> 02:08:25,679
that is returned
3731
02:08:25,679 --> 02:08:27,520
we will pass in as an argument to our
3732
02:08:27,520 --> 02:08:29,360
next function of casting
3733
02:08:29,360 --> 02:08:31,760
this data type to a float so we'll
3734
02:08:31,760 --> 02:08:32,400
surround
3735
02:08:32,400 --> 02:08:34,880
our function with another function and
3736
02:08:34,880 --> 02:08:35,599
use
3737
02:08:35,599 --> 02:08:38,079
the float function and then we'll just
3738
02:08:38,079 --> 02:08:39,440
keep on adding layers to this
3739
02:08:39,440 --> 02:08:42,079
so we have the absolute value function
3740
02:08:42,079 --> 02:08:42,800
next
3741
02:08:42,800 --> 02:08:45,520
so we'll add another layer and we have
3742
02:08:45,520 --> 02:08:47,280
the round function
3743
02:08:47,280 --> 02:08:50,560
so we'll add a layer to that and
3744
02:08:50,560 --> 02:08:52,400
lastly we're going to print and that'll
3745
02:08:52,400 --> 02:08:55,119
be the outermost layer
3746
02:08:55,119 --> 02:08:57,280
so this program will do the exact same
3747
02:08:57,280 --> 02:08:58,320
thing
3748
02:08:58,320 --> 02:09:01,679
let's say negative 3.14 except we've
3749
02:09:01,679 --> 02:09:02,639
written this with
3750
02:09:02,639 --> 02:09:05,520
one line of code so basically we start
3751
02:09:05,520 --> 02:09:07,520
with the innermost function and resolve
3752
02:09:07,520 --> 02:09:08,480
that first
3753
02:09:08,480 --> 02:09:10,960
whatever value is returned we use as an
3754
02:09:10,960 --> 02:09:13,520
argument to the next outermost function
3755
02:09:13,520 --> 02:09:15,679
and in this case it would be float then
3756
02:09:15,679 --> 02:09:18,480
we resolve that then move on to absolute
3757
02:09:18,480 --> 02:09:20,480
value then round and then print
3758
02:09:20,480 --> 02:09:22,719
so it's just another way of writing code
3759
02:09:22,719 --> 02:09:23,599
that takes up less
3760
02:09:23,599 --> 02:09:25,440
lines but this is more of an extreme
3761
02:09:25,440 --> 02:09:27,280
example you probably won't see
3762
02:09:27,280 --> 02:09:29,840
like four or five nested functions well
3763
02:09:29,840 --> 02:09:31,760
everybody those are nested function
3764
02:09:31,760 --> 02:09:32,239
calls
3765
02:09:32,239 --> 02:09:34,000
if you would like a copy of this code i
3766
02:09:34,000 --> 02:09:35,599
will post all of this to the comment
3767
02:09:35,599 --> 02:09:36,800
section down below
3768
02:09:36,800 --> 02:09:39,199
but yeah those are nested function calls
3769
02:09:39,199 --> 02:09:40,719
in python
3770
02:09:40,719 --> 02:09:42,560
hey what's going on people it's your bro
3771
02:09:42,560 --> 02:09:44,159
hope you're doing well and in this video
3772
02:09:44,159 --> 02:09:46,159
i'm going to show you how variable scope
3773
02:09:46,159 --> 02:09:47,760
works in python so
3774
02:09:47,760 --> 02:09:52,079
sit back relax and enjoy the show
3775
02:09:52,159 --> 02:09:54,400
all right let's do a thing people let's
3776
02:09:54,400 --> 02:09:56,560
discuss the scope of a variable
3777
02:09:56,560 --> 02:09:58,639
now the scope of a variable is the
3778
02:09:58,639 --> 02:10:00,400
region that a variable
3779
02:10:00,400 --> 02:10:02,960
is recognized a variable is only
3780
02:10:02,960 --> 02:10:05,040
available from inside the region
3781
02:10:05,040 --> 02:10:06,960
that it is created let's say that we
3782
02:10:06,960 --> 02:10:08,800
have a function and i just gave this
3783
02:10:08,800 --> 02:10:10,880
function a name of display name
3784
02:10:10,880 --> 02:10:12,960
i created a variable inside of this
3785
02:10:12,960 --> 02:10:14,400
function named name
3786
02:10:14,400 --> 02:10:16,480
and you can assign this a value of
3787
02:10:16,480 --> 02:10:17,599
whatever your name is
3788
02:10:17,599 --> 02:10:19,679
preferably a last name for this example
3789
02:10:19,679 --> 02:10:21,520
and what i'll do is just print
3790
02:10:21,520 --> 02:10:24,400
whatever my name is so this variable is
3791
02:10:24,400 --> 02:10:25,119
known to have
3792
02:10:25,119 --> 02:10:28,079
a local scope because it's declared
3793
02:10:28,079 --> 02:10:29,599
inside of a function
3794
02:10:29,599 --> 02:10:32,400
and a variable is only recognized inside
3795
02:10:32,400 --> 02:10:33,520
of the region
3796
02:10:33,520 --> 02:10:36,079
that it is created this variable of name
3797
02:10:36,079 --> 02:10:37,679
since it's created inside
3798
02:10:37,679 --> 02:10:40,560
of this function it is known as a local
3799
02:10:40,560 --> 02:10:41,280
variable
3800
02:10:41,280 --> 02:10:44,079
that has a local scope it's only
3801
02:10:44,079 --> 02:10:44,800
available
3802
02:10:44,800 --> 02:10:46,560
inside of this function that it's
3803
02:10:46,560 --> 02:10:49,119
created now let's attempt to access this
3804
02:10:49,119 --> 02:10:50,000
variable from
3805
02:10:50,000 --> 02:10:52,320
outside of our function so i'm going to
3806
02:10:52,320 --> 02:10:53,280
print my
3807
02:10:53,280 --> 02:10:55,679
name variable but we'll have an issue
3808
02:10:55,679 --> 02:10:56,480
though
3809
02:10:56,480 --> 02:10:59,840
so name name is not defined so
3810
02:10:59,840 --> 02:11:02,480
local variables are declared inside of a
3811
02:11:02,480 --> 02:11:03,040
function
3812
02:11:03,040 --> 02:11:05,760
and they have a local scope they are
3813
02:11:05,760 --> 02:11:06,159
only
3814
02:11:06,159 --> 02:11:07,920
available inside of this function in
3815
02:11:07,920 --> 02:11:09,360
which they're created
3816
02:11:09,360 --> 02:11:12,320
on the other hand a global variable is a
3817
02:11:12,320 --> 02:11:14,079
variable that is declared
3818
02:11:14,079 --> 02:11:16,320
outside of any function but within your
3819
02:11:16,320 --> 02:11:17,920
module that you're working with
3820
02:11:17,920 --> 02:11:20,239
a global variable is known to have a
3821
02:11:20,239 --> 02:11:21,280
global scope
3822
02:11:21,280 --> 02:11:23,760
they are available inside and outside of
3823
02:11:23,760 --> 02:11:24,880
any functions
3824
02:11:24,880 --> 02:11:27,599
now if i was to print my name variable
3825
02:11:27,599 --> 02:11:29,119
this is going to print
3826
02:11:29,119 --> 02:11:31,280
bro or whatever your first name is and
3827
02:11:31,280 --> 02:11:33,199
it is entirely possible to have both
3828
02:11:33,199 --> 02:11:35,920
a global and local version of the same
3829
02:11:35,920 --> 02:11:37,199
named variable
3830
02:11:37,199 --> 02:11:39,760
so we have both a global version of name
3831
02:11:39,760 --> 02:11:42,239
as well as a local version of name
3832
02:11:42,239 --> 02:11:45,040
if i was to print my name variable from
3833
02:11:45,040 --> 02:11:46,400
outside of this function
3834
02:11:46,400 --> 02:11:48,960
it's going to print my global version
3835
02:11:48,960 --> 02:11:50,639
however if i print
3836
02:11:50,639 --> 02:11:52,719
my version of name inside of that
3837
02:11:52,719 --> 02:11:54,159
function in which it's created
3838
02:11:54,159 --> 02:11:56,079
we're going to first use the local
3839
02:11:56,079 --> 02:11:57,599
version before the
3840
02:11:57,599 --> 02:11:59,920
global version of name and let's try
3841
02:11:59,920 --> 02:12:01,520
that so i'm going to call
3842
02:12:01,520 --> 02:12:04,960
my display name function
3843
02:12:04,960 --> 02:12:08,239
and this will print my last name
3844
02:12:08,239 --> 02:12:11,040
code and then my first name of bro so it
3845
02:12:11,040 --> 02:12:12,800
is possible to have a global
3846
02:12:12,800 --> 02:12:15,119
and local version of the same named
3847
02:12:15,119 --> 02:12:16,159
variable
3848
02:12:16,159 --> 02:12:18,239
now if i was to remove this line where
3849
02:12:18,239 --> 02:12:20,079
we declare a local version
3850
02:12:20,079 --> 02:12:22,239
of name well then the next best
3851
02:12:22,239 --> 02:12:24,079
available name to us would be
3852
02:12:24,079 --> 02:12:26,800
the global version of name so if you use
3853
02:12:26,800 --> 02:12:28,719
a variable inside of a function
3854
02:12:28,719 --> 02:12:31,440
you'll first use a local version of that
3855
02:12:31,440 --> 02:12:32,079
variable
3856
02:12:32,079 --> 02:12:34,719
if it's available if not you'll use a
3857
02:12:34,719 --> 02:12:36,079
global version instead
3858
02:12:36,079 --> 02:12:38,079
and python will follow this rule known
3859
02:12:38,079 --> 02:12:40,000
as the legb
3860
02:12:40,000 --> 02:12:42,880
rule you use any local variables first
3861
02:12:42,880 --> 02:12:43,199
then
3862
02:12:43,199 --> 02:12:45,840
enclosed variables then global and then
3863
02:12:45,840 --> 02:12:48,320
any built-in variables in that order
3864
02:12:48,320 --> 02:12:50,400
so that's the main difference between
3865
02:12:50,400 --> 02:12:51,520
local scope and
3866
02:12:51,520 --> 02:12:54,320
global scope the scope of a variable is
3867
02:12:54,320 --> 02:12:56,400
the region in which it's available
3868
02:12:56,400 --> 02:12:58,159
and it's limited to the region in which
3869
02:12:58,159 --> 02:13:00,159
it's created a local scope
3870
02:13:00,159 --> 02:13:02,480
is available only inside of the function
3871
02:13:02,480 --> 02:13:04,480
in which that variable is created
3872
02:13:04,480 --> 02:13:07,280
a global scoped variable is a variable
3873
02:13:07,280 --> 02:13:08,639
that is available
3874
02:13:08,639 --> 02:13:10,960
inside and outside of any functions but
3875
02:13:10,960 --> 02:13:12,719
within your module that you're working
3876
02:13:12,719 --> 02:13:13,119
with
3877
02:13:13,119 --> 02:13:15,360
so that's variable scope in python if
3878
02:13:15,360 --> 02:13:17,040
you would like a copy of this code i
3879
02:13:17,040 --> 02:13:18,639
will post this code to the comment
3880
02:13:18,639 --> 02:13:19,840
section down below
3881
02:13:19,840 --> 02:13:22,320
and well yeah that's how variable scope
3882
02:13:22,320 --> 02:13:24,480
works in python
3883
02:13:24,480 --> 02:13:26,560
hey what's going on everybody it's bro
3884
02:13:26,560 --> 02:13:27,520
hope you're doing well
3885
02:13:27,520 --> 02:13:29,119
and in this video i'm going to explain
3886
02:13:29,119 --> 02:13:32,239
the arcs parameter in python so sit back
3887
02:13:32,239 --> 02:13:36,079
relax and enjoy the show
3888
02:13:36,079 --> 02:13:38,560
all right guys and gals let's talk about
3889
02:13:38,560 --> 02:13:39,440
args
3890
02:13:39,440 --> 02:13:42,000
now args is a parameter that will pack
3891
02:13:42,000 --> 02:13:44,079
all arguments into a tuple
3892
02:13:44,079 --> 02:13:45,920
it's useful so that a function can
3893
02:13:45,920 --> 02:13:48,400
accept a varying amount of arguments
3894
02:13:48,400 --> 02:13:50,000
here's an example of why it would be
3895
02:13:50,000 --> 02:13:52,079
useful let's say we have a function
3896
02:13:52,079 --> 02:13:54,159
called add that will accept two numbers
3897
02:13:54,159 --> 02:13:55,199
as arguments
3898
02:13:55,199 --> 02:13:57,440
and add them together and return the sum
3899
02:13:57,440 --> 02:13:59,440
so this works as intended right as long
3900
02:13:59,440 --> 02:14:00,320
as we pass in
3901
02:14:00,320 --> 02:14:02,239
only two numbers but what if we need to
3902
02:14:02,239 --> 02:14:04,639
pass in three numbers as arguments
3903
02:14:04,639 --> 02:14:06,320
well we can no longer use this add
3904
02:14:06,320 --> 02:14:08,079
function because we have two parameters
3905
02:14:08,079 --> 02:14:08,639
set up
3906
02:14:08,639 --> 02:14:10,960
but we're passing in three arguments you
3907
02:14:10,960 --> 02:14:12,639
can see here within my console window
3908
02:14:12,639 --> 02:14:14,239
that we have a type error
3909
02:14:14,239 --> 02:14:16,639
add takes two positional arguments but
3910
02:14:16,639 --> 02:14:17,679
three were given
3911
02:14:17,679 --> 02:14:19,440
and one way in which we can solve this
3912
02:14:19,440 --> 02:14:20,800
issue is to use this
3913
02:14:20,800 --> 02:14:22,719
args parameter so what we're going to do
3914
02:14:22,719 --> 02:14:24,560
is replace all of our parameters
3915
02:14:24,560 --> 02:14:27,599
with asterisk args and actually
3916
02:14:27,599 --> 02:14:30,000
the args portion isn't as important as
3917
02:14:30,000 --> 02:14:31,119
the asterisk
3918
02:14:31,119 --> 02:14:32,800
args is short for arguments and you can
3919
02:14:32,800 --> 02:14:34,239
really name this whatever you want just
3920
02:14:34,239 --> 02:14:36,239
be sure that you have this asterisk
3921
02:14:36,239 --> 02:14:38,239
because with this asterisk we're doing a
3922
02:14:38,239 --> 02:14:39,440
form of packing
3923
02:14:39,440 --> 02:14:41,520
what we're doing is passing all of these
3924
02:14:41,520 --> 02:14:43,199
arguments and packing them
3925
02:14:43,199 --> 02:14:45,520
into a tuple so let's rewrite this
3926
02:14:45,520 --> 02:14:47,920
program so let's say maybe sum
3927
02:14:47,920 --> 02:14:51,199
equals zero and then we will need to
3928
02:14:51,199 --> 02:14:53,199
iterate through all of the items within
3929
02:14:53,199 --> 02:14:54,079
our tuple because
3930
02:14:54,079 --> 02:14:57,599
tuples are iterable so let's say for i
3931
02:14:57,599 --> 02:15:00,719
in args what we'll do is say sum
3932
02:15:00,719 --> 02:15:03,360
plus equals whatever i is and at the end
3933
02:15:03,360 --> 02:15:05,040
when we escape the for loop we will
3934
02:15:05,040 --> 02:15:06,000
return
3935
02:15:06,000 --> 02:15:08,239
our sum so let's try it again and see if
3936
02:15:08,239 --> 02:15:09,360
it works
3937
02:15:09,360 --> 02:15:11,199
well would you look at that it works
3938
02:15:11,199 --> 02:15:13,199
let's pass in a few other arguments for
3939
02:15:13,199 --> 02:15:14,079
good measure
3940
02:15:14,079 --> 02:15:16,320
let's pass in six arguments this time
3941
02:15:16,320 --> 02:15:17,520
all right look at that
3942
02:15:17,520 --> 02:15:19,520
this does in fact work so you can
3943
02:15:19,520 --> 02:15:20,960
actually name this whatever you want the
3944
02:15:20,960 --> 02:15:23,119
important thing is to have this asterisk
3945
02:15:23,119 --> 02:15:25,119
so let's rename this as i don't know
3946
02:15:25,119 --> 02:15:26,400
stuff
3947
02:15:26,400 --> 02:15:29,440
and let's try this again yep
3948
02:15:29,440 --> 02:15:32,320
it still works with tuples they are
3949
02:15:32,320 --> 02:15:32,880
ordered
3950
02:15:32,880 --> 02:15:34,960
and unchangeable let's attempt to edit
3951
02:15:34,960 --> 02:15:36,480
one of these values
3952
02:15:36,480 --> 02:15:39,599
so let's say stuff at
3953
02:15:39,599 --> 02:15:42,960
index zero is equal to
3954
02:15:42,960 --> 02:15:45,920
zero and let's try and change it so you
3955
02:15:45,920 --> 02:15:47,679
can see that tuple object does not
3956
02:15:47,679 --> 02:15:48,159
support
3957
02:15:48,159 --> 02:15:50,800
item assignment if you need to change
3958
02:15:50,800 --> 02:15:51,840
one of the values
3959
02:15:51,840 --> 02:15:54,239
within your tuple after you pack all of
3960
02:15:54,239 --> 02:15:55,119
these arguments
3961
02:15:55,119 --> 02:15:56,800
you might need to convert this to a
3962
02:15:56,800 --> 02:15:58,639
different collection one way in which we
3963
02:15:58,639 --> 02:16:00,400
can do that is to cast it
3964
02:16:00,400 --> 02:16:03,840
let's say stuff equals and we'll cast
3965
02:16:03,840 --> 02:16:06,880
our tuple as a list because a list
3966
02:16:06,880 --> 02:16:09,199
can be changeable it's mutable so let's
3967
02:16:09,199 --> 02:16:11,280
attempt this again we'll cast
3968
02:16:11,280 --> 02:16:13,679
our tuple as a list and then edit one of
3969
02:16:13,679 --> 02:16:14,320
the values
3970
02:16:14,320 --> 02:16:16,560
and then add all the values together so
3971
02:16:16,560 --> 02:16:18,079
to change one of the values
3972
02:16:18,079 --> 02:16:20,159
say the name your collection stuff in
3973
02:16:20,159 --> 02:16:22,480
this case let's say at index zero
3974
02:16:22,480 --> 02:16:25,119
this now equals zero so now if we were
3975
02:16:25,119 --> 02:16:26,880
to add all these items together
3976
02:16:26,880 --> 02:16:29,920
they should equal 20. there we go so
3977
02:16:29,920 --> 02:16:32,558
yeah that's basically the args parameter
3978
02:16:32,558 --> 02:16:34,478
the name isn't as important as the
3979
02:16:34,478 --> 02:16:36,160
asterisk because you can really name it
3980
02:16:36,160 --> 02:16:37,120
whatever you want
3981
02:16:37,120 --> 02:16:39,359
a common convention is to just name this
3982
02:16:39,359 --> 02:16:41,679
args it's a parameter that will pack
3983
02:16:41,679 --> 02:16:44,398
all arguments into a tuple it's useful
3984
02:16:44,398 --> 02:16:46,000
so that a function can accept
3985
02:16:46,000 --> 02:16:48,558
a varying amount of arguments well that
3986
02:16:48,558 --> 02:16:50,000
is all about args
3987
02:16:50,000 --> 02:16:51,920
if you would like a copy of this code i
3988
02:16:51,920 --> 02:16:53,599
will post this in the comments section
3989
02:16:53,599 --> 02:16:54,240
down below
3990
02:16:54,240 --> 02:16:56,638
but yeah that is how the args parameter
3991
02:16:56,638 --> 02:16:58,718
works in python
3992
02:16:58,718 --> 02:17:00,879
hey what's going on everybody it's bro
3993
02:17:00,879 --> 02:17:02,318
hope you're doing well and in this video
3994
02:17:02,318 --> 02:17:04,080
i'm going to be explaining quarks
3995
02:17:04,080 --> 02:17:07,120
in python so sit back relax
3996
02:17:07,120 --> 02:17:10,000
and enjoy the show
3997
02:17:10,318 --> 02:17:12,000
all right everybody let's talk about
3998
02:17:12,000 --> 02:17:14,718
quarkx now this is a parameter that will
3999
02:17:14,718 --> 02:17:15,120
pack
4000
02:17:15,120 --> 02:17:17,760
all arguments into a dictionary it's
4001
02:17:17,760 --> 02:17:20,160
useful so that a function can accept a
4002
02:17:20,160 --> 02:17:22,959
varying amount of keyword arguments it's
4003
02:17:22,959 --> 02:17:23,760
identical
4004
02:17:23,760 --> 02:17:26,478
to args except with args args will
4005
02:17:26,478 --> 02:17:28,718
accept a varying amount of positional
4006
02:17:28,718 --> 02:17:29,439
arguments
4007
02:17:29,439 --> 02:17:31,840
and pack them into a tuple with quarks
4008
02:17:31,840 --> 02:17:33,679
this will accept a varying amount of
4009
02:17:33,679 --> 02:17:34,799
keyword arguments
4010
02:17:34,799 --> 02:17:36,718
and pack them into a dictionary so
4011
02:17:36,718 --> 02:17:38,240
here's an example of why this would be
4012
02:17:38,240 --> 02:17:38,959
useful
4013
02:17:38,959 --> 02:17:40,718
let's say we have a function called
4014
02:17:40,718 --> 02:17:43,200
hello that accepts two keyword arguments
4015
02:17:43,200 --> 02:17:44,080
a first name
4016
02:17:44,080 --> 02:17:46,160
and a last name all this will do is
4017
02:17:46,160 --> 02:17:47,120
output hello
4018
02:17:47,120 --> 02:17:49,439
your first name and last name but what
4019
02:17:49,439 --> 02:17:51,200
if somebody has more than two names
4020
02:17:51,200 --> 02:17:53,120
let's say somebody has a middle name
4021
02:17:53,120 --> 02:17:54,718
and for this example i'll just say my
4022
02:17:54,718 --> 02:17:56,558
middle name is dude
4023
02:17:56,558 --> 02:17:59,280
so if i were to run this well this hello
4024
02:17:59,280 --> 02:18:01,120
function is no longer going to work
4025
02:18:01,120 --> 02:18:03,040
as you can see in the console window we
4026
02:18:03,040 --> 02:18:04,398
have a type error
4027
02:18:04,398 --> 02:18:06,558
the hello function got an unexpected
4028
02:18:06,558 --> 02:18:08,160
keyword argument middle
4029
02:18:08,160 --> 02:18:10,080
one way in which we can solve this we
4030
02:18:10,080 --> 02:18:11,760
can use this quark's
4031
02:18:11,760 --> 02:18:14,558
parameter so we can accept a varying
4032
02:18:14,558 --> 02:18:15,120
amount
4033
02:18:15,120 --> 02:18:17,280
of keyword arguments so what we'll do is
4034
02:18:17,280 --> 02:18:18,318
replace
4035
02:18:18,318 --> 02:18:21,359
our parameters with two asterisks then
4036
02:18:21,359 --> 02:18:23,760
quarks now if i would like the same
4037
02:18:23,760 --> 02:18:25,519
output as i had before i'm going to need
4038
02:18:25,519 --> 02:18:26,879
to change a few things around
4039
02:18:26,879 --> 02:18:29,040
in order to access a value within a
4040
02:18:29,040 --> 02:18:30,398
dictionary you type the name of the
4041
02:18:30,398 --> 02:18:31,519
dictionary
4042
02:18:31,519 --> 02:18:34,318
square brackets and then within quotes
4043
02:18:34,318 --> 02:18:36,398
list your key
4044
02:18:36,398 --> 02:18:38,799
so i would like to access the value at
4045
02:18:38,799 --> 02:18:40,558
this key of first
4046
02:18:40,558 --> 02:18:42,799
and let's repeat the process for last
4047
02:18:42,799 --> 02:18:44,879
name
4048
02:18:44,879 --> 02:18:46,959
so the key is going to be last we'll
4049
02:18:46,959 --> 02:18:48,718
access this value
4050
02:18:48,718 --> 02:18:50,879
at this key and this should work the
4051
02:18:50,879 --> 02:18:52,398
same as it did before
4052
02:18:52,398 --> 02:18:54,478
now let's take this a step further let's
4053
02:18:54,478 --> 02:18:55,920
say we would like to display
4054
02:18:55,920 --> 02:18:58,160
somebody's full name based on the amount
4055
02:18:58,160 --> 02:19:00,080
of keyword arguments that they pass into
4056
02:19:00,080 --> 02:19:01,040
this function
4057
02:19:01,040 --> 02:19:02,558
well there's a few different ways in
4058
02:19:02,558 --> 02:19:05,120
which we could do this one easy way is
4059
02:19:05,120 --> 02:19:06,879
that we could write our program like
4060
02:19:06,879 --> 02:19:08,000
this
4061
02:19:08,000 --> 02:19:11,599
let's print the word hello
4062
02:19:11,599 --> 02:19:14,080
and we will need to iterate once through
4063
02:19:14,080 --> 02:19:16,000
each key value pair within this
4064
02:19:16,000 --> 02:19:17,040
dictionary
4065
02:19:17,040 --> 02:19:19,359
and we can use a for loop to do that so
4066
02:19:19,359 --> 02:19:20,080
to iterate
4067
02:19:20,080 --> 02:19:22,318
once through each key value pair in your
4068
02:19:22,318 --> 02:19:23,120
dictionary
4069
02:19:23,120 --> 02:19:26,478
we'll type for key value
4070
02:19:26,478 --> 02:19:28,959
in the name of our dictionary quarks in
4071
02:19:28,959 --> 02:19:29,599
this case
4072
02:19:29,599 --> 02:19:33,679
dot items and then we will print
4073
02:19:33,679 --> 02:19:36,719
each value
4074
02:19:37,200 --> 02:19:38,558
so there's one more thing that we need
4075
02:19:38,558 --> 02:19:40,558
to do this kind of works as intended but
4076
02:19:40,558 --> 02:19:41,280
it's going to
4077
02:19:41,280 --> 02:19:44,318
print each value including our word
4078
02:19:44,318 --> 02:19:44,959
hello
4079
02:19:44,959 --> 02:19:48,160
on a new line so with a standard print
4080
02:19:48,160 --> 02:19:48,960
statement
4081
02:19:48,960 --> 02:19:50,960
this will print a new line character
4082
02:19:50,960 --> 02:19:52,720
after each print statement as if we're
4083
02:19:52,720 --> 02:19:53,520
hitting enter
4084
02:19:53,520 --> 02:19:56,000
we can actually change that so after
4085
02:19:56,000 --> 02:19:56,800
your string
4086
02:19:56,800 --> 02:19:59,680
add and equals and we can replace that
4087
02:19:59,680 --> 02:20:01,120
new line character with something else
4088
02:20:01,120 --> 02:20:02,880
maybe a space
4089
02:20:02,880 --> 02:20:04,800
and let's do the same thing for our for
4090
02:20:04,800 --> 02:20:06,319
loop so value
4091
02:20:06,319 --> 02:20:08,800
comma end and then just add space now
4092
02:20:08,800 --> 02:20:09,920
this will print
4093
02:20:09,920 --> 02:20:12,720
all of your names on the same line hello
4094
02:20:12,720 --> 02:20:13,200
bro
4095
02:20:13,200 --> 02:20:15,840
dude code and now we can pass in a
4096
02:20:15,840 --> 02:20:17,600
varying amount of keyword arguments
4097
02:20:17,600 --> 02:20:19,280
based on how long our name is
4098
02:20:19,280 --> 02:20:22,000
perhaps we have like a title title
4099
02:20:22,000 --> 02:20:23,040
equals
4100
02:20:23,040 --> 02:20:26,800
mr and we can print that as well
4101
02:20:26,800 --> 02:20:30,000
hello mr bro dude code here's a fun
4102
02:20:30,000 --> 02:20:31,760
fact of the day you don't necessarily
4103
02:20:31,760 --> 02:20:34,000
need to name this parameter as quarks
4104
02:20:34,000 --> 02:20:35,280
the important thing is to have
4105
02:20:35,280 --> 02:20:37,760
these double asterisks that precede it
4106
02:20:37,760 --> 02:20:39,120
quarks is just a
4107
02:20:39,120 --> 02:20:41,439
common naming convention it's short for
4108
02:20:41,439 --> 02:20:42,640
keyword arguments
4109
02:20:42,640 --> 02:20:44,160
you could name this as something else
4110
02:20:44,160 --> 02:20:45,920
perhaps something more descriptive like
4111
02:20:45,920 --> 02:20:47,840
names since we're passing in a bunch of
4112
02:20:47,840 --> 02:20:49,840
names so the name of our dictionary
4113
02:20:49,840 --> 02:20:52,080
is now names and this would work the
4114
02:20:52,080 --> 02:20:53,359
same as it did before
4115
02:20:53,359 --> 02:20:55,040
the important thing is to have these
4116
02:20:55,040 --> 02:20:57,120
double asterisks that precede the name
4117
02:20:57,120 --> 02:20:58,160
of your dictionary
4118
02:20:58,160 --> 02:21:00,319
but quarks is the common naming
4119
02:21:00,319 --> 02:21:01,280
convention
4120
02:21:01,280 --> 02:21:03,280
well everybody that's all about quarkx
4121
02:21:03,280 --> 02:21:05,040
it's a parameter that will pack
4122
02:21:05,040 --> 02:21:07,680
a varying amount of keyword arguments
4123
02:21:07,680 --> 02:21:08,399
into a
4124
02:21:08,399 --> 02:21:10,640
dictionary if you would like a copy of
4125
02:21:10,640 --> 02:21:12,000
all this code i will post
4126
02:21:12,000 --> 02:21:13,920
this in the comments section down below
4127
02:21:13,920 --> 02:21:15,920
but yeah that is how quarkx
4128
02:21:15,920 --> 02:21:18,479
works in python hey what's going on
4129
02:21:18,479 --> 02:21:19,040
everybody
4130
02:21:19,040 --> 02:21:20,880
it's your bro hope you're doing well and
4131
02:21:20,880 --> 02:21:22,080
in this video i'm going to teach you
4132
02:21:22,080 --> 02:21:23,920
guys all about the format method
4133
02:21:23,920 --> 02:21:27,040
in python so sit back relax and enjoy
4134
02:21:27,040 --> 02:21:29,439
the show
4135
02:21:29,680 --> 02:21:32,240
okay ladies and gentlemen let's begin
4136
02:21:32,240 --> 02:21:33,359
the format method
4137
02:21:33,359 --> 02:21:35,920
is a method available to strings it's
4138
02:21:35,920 --> 02:21:37,680
optional and it gives users
4139
02:21:37,680 --> 02:21:40,479
more control when displaying output to
4140
02:21:40,479 --> 02:21:42,560
best demonstrate this i'm going to use a
4141
02:21:42,560 --> 02:21:42,960
line
4142
02:21:42,960 --> 02:21:45,760
from a popular nursery rhyme titled hey
4143
02:21:45,760 --> 02:21:47,439
diddle diddle because well
4144
02:21:47,439 --> 02:21:49,680
you know i'm a mature adult like that
4145
02:21:49,680 --> 02:21:50,640
i'll be printing
4146
02:21:50,640 --> 02:21:53,439
this line to my console window the cow
4147
02:21:53,439 --> 02:21:54,080
jumped over
4148
02:21:54,080 --> 02:21:56,640
the moon let's do so using a standard
4149
02:21:56,640 --> 02:21:57,680
print statement
4150
02:21:57,680 --> 02:21:59,200
and then i'll show you the benefits of
4151
02:21:59,200 --> 02:22:01,359
using the format method later
4152
02:22:01,359 --> 02:22:04,080
for this example i have two variables a
4153
02:22:04,080 --> 02:22:05,840
variable called animal and i'm going to
4154
02:22:05,840 --> 02:22:06,399
store
4155
02:22:06,399 --> 02:22:09,359
a string of cow and item and i'll be
4156
02:22:09,359 --> 02:22:10,720
storing an item name
4157
02:22:10,720 --> 02:22:12,720
which is moon when we compile and run
4158
02:22:12,720 --> 02:22:14,160
this as you would expect
4159
02:22:14,160 --> 02:22:16,800
the output is the cow jumped over the
4160
02:22:16,800 --> 02:22:17,439
moon
4161
02:22:17,439 --> 02:22:19,760
there's a more elegant way of writing
4162
02:22:19,760 --> 02:22:20,720
this line of code
4163
02:22:20,720 --> 02:22:22,800
and that is by using the format method
4164
02:22:22,800 --> 02:22:24,479
available to strings
4165
02:22:24,479 --> 02:22:26,000
i'm going to turn this line into a
4166
02:22:26,000 --> 02:22:27,520
comment and we're instead
4167
02:22:27,520 --> 02:22:29,840
going to produce the same output but use
4168
02:22:29,840 --> 02:22:31,040
the format method
4169
02:22:31,040 --> 02:22:33,439
instead so we need a standard print
4170
02:22:33,439 --> 02:22:34,640
statement like usual
4171
02:22:34,640 --> 02:22:36,479
and then a string that we would like to
4172
02:22:36,479 --> 02:22:37,760
format so
4173
02:22:37,760 --> 02:22:39,600
we're going to write the same thing but
4174
02:22:39,600 --> 02:22:41,120
in place of the variables we're going to
4175
02:22:41,120 --> 02:22:42,880
add a placeholder
4176
02:22:42,880 --> 02:22:45,680
the instead of the variable name or
4177
02:22:45,680 --> 02:22:47,359
value name i'm going to add
4178
02:22:47,359 --> 02:22:50,319
a set of curly braces so that functions
4179
02:22:50,319 --> 02:22:51,760
as a placeholder
4180
02:22:51,760 --> 02:22:55,359
for a value or a variable the
4181
02:22:55,359 --> 02:22:59,040
this should be cow jumped over
4182
02:22:59,040 --> 02:23:01,359
the and i'm going to insert another
4183
02:23:01,359 --> 02:23:02,560
placeholder
4184
02:23:02,560 --> 02:23:04,960
now this is a string we can format the
4185
02:23:04,960 --> 02:23:06,240
string by following
4186
02:23:06,240 --> 02:23:10,000
this string with dot format parenthesis
4187
02:23:10,000 --> 02:23:12,240
and within the parentheses of the format
4188
02:23:12,240 --> 02:23:14,080
method we can insert
4189
02:23:14,080 --> 02:23:16,800
some values or variables let's begin
4190
02:23:16,800 --> 02:23:17,439
with some
4191
02:23:17,439 --> 02:23:20,640
values i would like to insert cow
4192
02:23:20,640 --> 02:23:23,680
as well as moon so this will produce
4193
02:23:23,680 --> 02:23:26,080
the same output but it's written a
4194
02:23:26,080 --> 02:23:27,520
little more elegantly
4195
02:23:27,520 --> 02:23:30,160
we can also replace these with variables
4196
02:23:30,160 --> 02:23:31,120
because
4197
02:23:31,120 --> 02:23:33,680
they store these values so i'm going to
4198
02:23:33,680 --> 02:23:34,720
replace cow
4199
02:23:34,720 --> 02:23:37,920
with animal and moon with item
4200
02:23:37,920 --> 02:23:40,399
and this will create the same output as
4201
02:23:40,399 --> 02:23:41,359
it did before
4202
02:23:41,359 --> 02:23:43,600
these curly braces are what's known as
4203
02:23:43,600 --> 02:23:44,560
format fields
4204
02:23:44,560 --> 02:23:46,800
they function as a placeholder for a
4205
02:23:46,800 --> 02:23:47,600
value or
4206
02:23:47,600 --> 02:23:50,000
a variable and they work in order the
4207
02:23:50,000 --> 02:23:52,560
first format field will insert the first
4208
02:23:52,560 --> 02:23:53,280
value
4209
02:23:53,280 --> 02:23:56,560
at this location if you have another set
4210
02:23:56,560 --> 02:23:59,280
or more sets of this format field you'll
4211
02:23:59,280 --> 02:24:00,000
insert
4212
02:24:00,000 --> 02:24:02,640
the next value that's listed within the
4213
02:24:02,640 --> 02:24:03,760
format method
4214
02:24:03,760 --> 02:24:06,080
so if we were to switch these around
4215
02:24:06,080 --> 02:24:08,319
we'll switch the position of animal with
4216
02:24:08,319 --> 02:24:09,920
item
4217
02:24:09,920 --> 02:24:12,240
well then the first format field is
4218
02:24:12,240 --> 02:24:13,280
going to insert
4219
02:24:13,280 --> 02:24:15,840
our item and the second format field
4220
02:24:15,840 --> 02:24:17,359
will insert our animal
4221
02:24:17,359 --> 02:24:20,000
this time the moon jumped over the cow
4222
02:24:20,000 --> 02:24:22,319
an additional way of inserting values at
4223
02:24:22,319 --> 02:24:24,240
a given place holder would be to use
4224
02:24:24,240 --> 02:24:26,840
what's referred to as a
4225
02:24:26,840 --> 02:24:29,600
positional argument
4226
02:24:29,600 --> 02:24:32,080
within these format fields we will list
4227
02:24:32,080 --> 02:24:33,920
the index of the value that we would
4228
02:24:33,920 --> 02:24:36,319
like to insert at this location
4229
02:24:36,319 --> 02:24:38,720
if you need to insert the first value
4230
02:24:38,720 --> 02:24:40,000
well that has an index
4231
02:24:40,000 --> 02:24:41,840
of zero because computers always start
4232
02:24:41,840 --> 02:24:44,160
with zero and following that pattern
4233
02:24:44,160 --> 02:24:47,200
the next index would be one if you had
4234
02:24:47,200 --> 02:24:49,280
let's say three values that you need to
4235
02:24:49,280 --> 02:24:51,680
insert if you had another placeholder
4236
02:24:51,680 --> 02:24:53,760
that would be two then so you would just
4237
02:24:53,760 --> 02:24:54,800
follow that pattern
4238
02:24:54,800 --> 02:24:57,200
so there's going to be no visible change
4239
02:24:57,200 --> 02:24:58,319
but what we could do
4240
02:24:58,319 --> 02:25:01,280
is reverse the positional arguments of
4241
02:25:01,280 --> 02:25:02,720
these placeholders
4242
02:25:02,720 --> 02:25:04,240
so what do you think would happen if i
4243
02:25:04,240 --> 02:25:06,000
switched one with zero at these
4244
02:25:06,000 --> 02:25:07,120
locations
4245
02:25:07,120 --> 02:25:09,359
well this format field is going to
4246
02:25:09,359 --> 02:25:11,680
insert the value found at index
4247
02:25:11,680 --> 02:25:14,720
one this first value is index zero and
4248
02:25:14,720 --> 02:25:15,840
the second one
4249
02:25:15,840 --> 02:25:17,920
is index one so now the roles are
4250
02:25:17,920 --> 02:25:19,920
reversed this time the moon
4251
02:25:19,920 --> 02:25:22,640
jumped over the cow our last way of
4252
02:25:22,640 --> 02:25:25,040
inserting some values at a given format
4253
02:25:25,040 --> 02:25:27,280
field would be to use what's referred to
4254
02:25:27,280 --> 02:25:27,840
as a
4255
02:25:27,840 --> 02:25:31,200
keyword argument within
4256
02:25:31,200 --> 02:25:33,200
the format method we're going to list
4257
02:25:33,200 --> 02:25:35,359
some keyword argument pairs
4258
02:25:35,359 --> 02:25:37,840
we need a keyword name followed by a
4259
02:25:37,840 --> 02:25:38,560
value
4260
02:25:38,560 --> 02:25:40,319
so let's say animal is going to be the
4261
02:25:40,319 --> 02:25:42,479
keyword we're going to say equals
4262
02:25:42,479 --> 02:25:45,680
some value animal equals cow and
4263
02:25:45,680 --> 02:25:48,000
item equals moon and you may have
4264
02:25:48,000 --> 02:25:50,080
noticed that the color of your text
4265
02:25:50,080 --> 02:25:51,040
might have changed
4266
02:25:51,040 --> 02:25:53,040
so we no longer need these variables
4267
02:25:53,040 --> 02:25:55,760
actually um but i'm going to comment out
4268
02:25:55,760 --> 02:25:57,359
these two print statements because we
4269
02:25:57,359 --> 02:25:58,800
will get an error then
4270
02:25:58,800 --> 02:26:02,000
so these are in keyword argument pairs
4271
02:26:02,000 --> 02:26:04,880
and within our format field instead of
4272
02:26:04,880 --> 02:26:06,000
keeping these empty
4273
02:26:06,000 --> 02:26:08,319
or using an index we can use the keyword
4274
02:26:08,319 --> 02:26:10,880
name at my first format field i'm going
4275
02:26:10,880 --> 02:26:12,720
to insert my keyword
4276
02:26:12,720 --> 02:26:15,520
of animal and at my second format field
4277
02:26:15,520 --> 02:26:17,040
i would like to insert
4278
02:26:17,040 --> 02:26:20,000
the item keyword so the result is going
4279
02:26:20,000 --> 02:26:20,479
to be
4280
02:26:20,479 --> 02:26:22,640
the cow jumped over the moon so let's
4281
02:26:22,640 --> 02:26:23,760
reverse these now
4282
02:26:23,760 --> 02:26:26,319
we'll place our item keyword here and
4283
02:26:26,319 --> 02:26:28,319
then at the second format field we will
4284
02:26:28,319 --> 02:26:28,960
insert
4285
02:26:28,960 --> 02:26:32,080
our animal and this time the moon
4286
02:26:32,080 --> 02:26:33,680
jumped over the cow here's an important
4287
02:26:33,680 --> 02:26:35,120
concept that i'd like to bring to your
4288
02:26:35,120 --> 02:26:35,760
attention
4289
02:26:35,760 --> 02:26:37,359
with these values found within the
4290
02:26:37,359 --> 02:26:39,680
format method you can actually reuse
4291
02:26:39,680 --> 02:26:40,319
some of these
4292
02:26:40,319 --> 02:26:42,560
more than once so let's say we would
4293
02:26:42,560 --> 02:26:43,840
like to state
4294
02:26:43,840 --> 02:26:46,240
the animal jumped over the animal we're
4295
02:26:46,240 --> 02:26:46,960
going to use
4296
02:26:46,960 --> 02:26:49,359
this animal keyword twice and our output
4297
02:26:49,359 --> 02:26:50,240
is going to be
4298
02:26:50,240 --> 02:26:52,800
the cow jumped over the cow if we're
4299
02:26:52,800 --> 02:26:54,479
doing the same thing with positional
4300
02:26:54,479 --> 02:26:55,920
arguments
4301
02:26:55,920 --> 02:26:58,560
we can reuse the same index this time
4302
02:26:58,560 --> 02:26:59,439
let's say
4303
02:26:59,439 --> 02:27:02,319
that the moon jumped over the moon so
4304
02:27:02,319 --> 02:27:03,439
our item has an
4305
02:27:03,439 --> 02:27:06,479
index of one so within our first format
4306
02:27:06,479 --> 02:27:08,080
field we'll state
4307
02:27:08,080 --> 02:27:10,479
the index will be one and our second
4308
02:27:10,479 --> 02:27:12,319
format field will also be one
4309
02:27:12,319 --> 02:27:14,399
so our output's going to be the moon
4310
02:27:14,399 --> 02:27:16,080
jumped over the moon
4311
02:27:16,080 --> 02:27:17,840
all right let's take things up a level
4312
02:27:17,840 --> 02:27:19,600
there's an even more elegant way in
4313
02:27:19,600 --> 02:27:21,280
which we could write all of this
4314
02:27:21,280 --> 02:27:23,760
what we'll do is store our string that
4315
02:27:23,760 --> 02:27:25,200
we would like to format
4316
02:27:25,200 --> 02:27:27,760
within a variable let's turn this line
4317
02:27:27,760 --> 02:27:29,359
into a comment and let's create a
4318
02:27:29,359 --> 02:27:30,319
variable called
4319
02:27:30,319 --> 02:27:32,800
text text equals the string that we
4320
02:27:32,800 --> 02:27:33,840
would like to format
4321
02:27:33,840 --> 02:27:37,280
which is the format field
4322
02:27:37,280 --> 02:27:41,680
jumped over the format field
4323
02:27:41,680 --> 02:27:44,720
and if we need to format this variable
4324
02:27:44,720 --> 02:27:47,120
we can just call the format method on it
4325
02:27:47,120 --> 02:27:50,399
so within a print statement let's say
4326
02:27:50,399 --> 02:27:54,080
text dot and then use the format method
4327
02:27:54,080 --> 02:27:54,640
and pass
4328
02:27:54,640 --> 02:27:56,479
in the values that we would like to
4329
02:27:56,479 --> 02:27:58,720
insert at these placeholders
4330
02:27:58,720 --> 02:28:02,399
so we're going to insert animal as well
4331
02:28:02,399 --> 02:28:05,040
as item so our result as you would
4332
02:28:05,040 --> 02:28:05,680
imagine
4333
02:28:05,680 --> 02:28:08,319
is the cow jumped over the moon this
4334
02:28:08,319 --> 02:28:10,080
next section i'm going to explain how we
4335
02:28:10,080 --> 02:28:11,200
can add some padding
4336
02:28:11,200 --> 02:28:13,359
to a string when we display it using the
4337
02:28:13,359 --> 02:28:14,479
format method
4338
02:28:14,479 --> 02:28:16,720
let's say we have a name variable and
4339
02:28:16,720 --> 02:28:18,479
assign this a value of whatever your
4340
02:28:18,479 --> 02:28:19,200
name is
4341
02:28:19,200 --> 02:28:21,200
so i would like to display my name along
4342
02:28:21,200 --> 02:28:22,479
with the custom message
4343
02:28:22,479 --> 02:28:25,520
let's say hello my name
4344
02:28:25,520 --> 02:28:28,080
is and then i'll use a format field here
4345
02:28:28,080 --> 02:28:30,240
so let's follow this string by using the
4346
02:28:30,240 --> 02:28:31,200
format method
4347
02:28:31,200 --> 02:28:33,600
and i would like to insert my name at
4348
02:28:33,600 --> 02:28:34,800
this location
4349
02:28:34,800 --> 02:28:37,520
so currently this will display hello my
4350
02:28:37,520 --> 02:28:38,399
name is bro
4351
02:28:38,399 --> 02:28:40,640
or whatever else your name is so we can
4352
02:28:40,640 --> 02:28:41,840
add some padding
4353
02:28:41,840 --> 02:28:44,960
either before our name after or we can
4354
02:28:44,960 --> 02:28:46,560
add some padding to the left and the
4355
02:28:46,560 --> 02:28:47,520
right hand side
4356
02:28:47,520 --> 02:28:49,920
of our value that we have so what we'll
4357
02:28:49,920 --> 02:28:51,200
do to begin
4358
02:28:51,200 --> 02:28:53,280
is let's add some padding to the right
4359
02:28:53,280 --> 02:28:54,960
hand side of
4360
02:28:54,960 --> 02:28:56,880
our name that we have so within our
4361
02:28:56,880 --> 02:28:58,560
format field if we need to add some
4362
02:28:58,560 --> 02:28:59,200
padding
4363
02:28:59,200 --> 02:29:01,520
within the format field add a colon and
4364
02:29:01,520 --> 02:29:03,200
then the amount of space you would like
4365
02:29:03,200 --> 02:29:04,240
to allocate
4366
02:29:04,240 --> 02:29:07,600
to displaying your value so let's say 10
4367
02:29:07,600 --> 02:29:10,720
so i'm going to allocate 10 spaces worth
4368
02:29:10,720 --> 02:29:11,200
of room
4369
02:29:11,200 --> 02:29:13,439
to display my name you can't really see
4370
02:29:13,439 --> 02:29:15,520
it right now but let's take a look
4371
02:29:15,520 --> 02:29:18,720
so i can't move my cursor past this line
4372
02:29:18,720 --> 02:29:20,000
at the end of my name
4373
02:29:20,000 --> 02:29:22,000
but with my second line i have all of
4374
02:29:22,000 --> 02:29:23,600
this room to work with
4375
02:29:23,600 --> 02:29:25,840
so if i were to change my string that i
4376
02:29:25,840 --> 02:29:28,000
have let's say
4377
02:29:28,000 --> 02:29:31,120
nice to meet you well then
4378
02:29:31,120 --> 02:29:32,880
all of this padding that i have is going
4379
02:29:32,880 --> 02:29:34,880
to be more evident
4380
02:29:34,880 --> 02:29:38,479
so we can add a number preceding with a
4381
02:29:38,479 --> 02:29:40,319
colon to add some padding
4382
02:29:40,319 --> 02:29:42,960
to a value and then you can left align
4383
02:29:42,960 --> 02:29:45,439
this right align it or center align it
4384
02:29:45,439 --> 02:29:47,439
so what i'll do is copy this line that i
4385
02:29:47,439 --> 02:29:48,880
have
4386
02:29:48,880 --> 02:29:51,280
if you need to left align it you use the
4387
02:29:51,280 --> 02:29:52,319
less than sign
4388
02:29:52,319 --> 02:29:53,920
but there's going to be no visible
4389
02:29:53,920 --> 02:29:55,600
change because that's already the
4390
02:29:55,600 --> 02:29:56,399
default
4391
02:29:56,399 --> 02:29:59,520
if you need to write a line precede your
4392
02:29:59,520 --> 02:30:01,760
number with a greater than sign
4393
02:30:01,760 --> 02:30:04,319
so your value is going to be right
4394
02:30:04,319 --> 02:30:05,120
aligned
4395
02:30:05,120 --> 02:30:07,920
if you need to center this value then
4396
02:30:07,920 --> 02:30:08,479
use
4397
02:30:08,479 --> 02:30:11,840
the carrot so this will center align
4398
02:30:11,840 --> 02:30:14,240
your value within the padding that you
4399
02:30:14,240 --> 02:30:15,280
have allotted
4400
02:30:15,280 --> 02:30:16,880
one question that you might have for me
4401
02:30:16,880 --> 02:30:18,399
and i'll try and answer that now because
4402
02:30:18,399 --> 02:30:20,560
it's relevant what if we need to add a
4403
02:30:20,560 --> 02:30:22,319
positional argument or a
4404
02:30:22,319 --> 02:30:24,800
keyword argument to our format field if
4405
02:30:24,800 --> 02:30:26,720
there's already some text within here
4406
02:30:26,720 --> 02:30:28,640
so what you would do before the colon
4407
02:30:28,640 --> 02:30:30,479
you would just precede the colon
4408
02:30:30,479 --> 02:30:32,880
with either your positional argument or
4409
02:30:32,880 --> 02:30:34,160
your keyword argument
4410
02:30:34,160 --> 02:30:36,240
followed by colon and then whatever you
4411
02:30:36,240 --> 02:30:38,240
want to add afterwards to format
4412
02:30:38,240 --> 02:30:40,399
your value that you have alright ladies
4413
02:30:40,399 --> 02:30:42,319
and gentlemen we are near the end and i
4414
02:30:42,319 --> 02:30:44,240
promise that this is the last section
4415
02:30:44,240 --> 02:30:46,479
how can we format some numbers let's say
4416
02:30:46,479 --> 02:30:47,600
we have a
4417
02:30:47,600 --> 02:30:50,000
number variable and this will equal the
4418
02:30:50,000 --> 02:30:52,600
first few digits of pi
4419
02:30:52,600 --> 02:30:55,680
3.14159 let's pretend that we would like
4420
02:30:55,680 --> 02:30:56,000
to
4421
02:30:56,000 --> 02:30:59,359
display only the first two digits after
4422
02:30:59,359 --> 02:31:02,000
the decimal so we can do so using the
4423
02:31:02,000 --> 02:31:03,040
format method
4424
02:31:03,040 --> 02:31:06,319
let's print the number
4425
02:31:06,319 --> 02:31:09,760
pi is and then i'll use a format field
4426
02:31:09,760 --> 02:31:10,560
here
4427
02:31:10,560 --> 02:31:12,479
and we will call the format method and
4428
02:31:12,479 --> 02:31:13,600
insert our
4429
02:31:13,600 --> 02:31:17,439
number that we have so if i need to
4430
02:31:17,439 --> 02:31:20,560
display only the first two digits after
4431
02:31:20,560 --> 02:31:21,359
the decimal
4432
02:31:21,359 --> 02:31:24,960
within the format field i will add
4433
02:31:24,960 --> 02:31:28,000
colon dot to f
4434
02:31:28,000 --> 02:31:30,160
f is for floating point numbers that's
4435
02:31:30,160 --> 02:31:32,399
anything with a decimal portion
4436
02:31:32,399 --> 02:31:35,280
so the result is that this will display
4437
02:31:35,280 --> 02:31:36,640
only the first
4438
02:31:36,640 --> 02:31:39,840
two digits after my decimal because we
4439
02:31:39,840 --> 02:31:41,200
placed two within
4440
02:31:41,200 --> 02:31:44,240
the format field so if this was three
4441
02:31:44,240 --> 02:31:47,040
point three f this would display three
4442
02:31:47,040 --> 02:31:47,600
digits
4443
02:31:47,600 --> 02:31:50,160
after the decimal portion oh and
4444
02:31:50,160 --> 02:31:51,760
apparently this will round your number
4445
02:31:51,760 --> 02:31:53,120
so that's something that you should keep
4446
02:31:53,120 --> 02:31:53,760
in mind
4447
02:31:53,760 --> 02:31:55,600
so this time let's change our number to
4448
02:31:55,600 --> 02:31:56,800
something larger like
4449
02:31:56,800 --> 02:31:59,920
1000 and i will copy this line and make
4450
02:31:59,920 --> 02:32:00,800
a few changes
4451
02:32:00,800 --> 02:32:04,160
let's say the number is
4452
02:32:04,160 --> 02:32:06,640
and i would like to add a comma at the
4453
02:32:06,640 --> 02:32:07,680
thousands place
4454
02:32:07,680 --> 02:32:09,600
so after the colon within the format
4455
02:32:09,600 --> 02:32:11,439
field i will add a comma
4456
02:32:11,439 --> 02:32:13,680
so this will automatically add a comma
4457
02:32:13,680 --> 02:32:15,680
to all 1000s places
4458
02:32:15,680 --> 02:32:17,280
another cool thing that you can do is
4459
02:32:17,280 --> 02:32:19,359
that you can display your number as
4460
02:32:19,359 --> 02:32:21,680
binary so within the format field add
4461
02:32:21,680 --> 02:32:22,880
colon b
4462
02:32:22,880 --> 02:32:25,040
this will display a binary
4463
02:32:25,040 --> 02:32:27,359
representation of your number
4464
02:32:27,359 --> 02:32:30,080
you can do the same thing with octal by
4465
02:32:30,080 --> 02:32:31,359
adding colon
4466
02:32:31,359 --> 02:32:34,560
o so our number is now being displayed
4467
02:32:34,560 --> 02:32:35,840
as an octal number
4468
02:32:35,840 --> 02:32:37,040
and you can do the same thing with
4469
02:32:37,040 --> 02:32:39,200
hexadecimal lowercase x
4470
02:32:39,200 --> 02:32:42,000
for lowercase or uppercase x for all
4471
02:32:42,000 --> 02:32:42,880
uppercase
4472
02:32:42,880 --> 02:32:46,720
so my number 1000 is now in hexadecimal
4473
02:32:46,720 --> 02:32:49,920
and lastly we can display this number
4474
02:32:49,920 --> 02:32:53,040
in scientific notation using either
4475
02:32:53,040 --> 02:32:55,760
lowercase e for lowercase or capital e
4476
02:32:55,760 --> 02:32:56,880
for uppercase
4477
02:32:56,880 --> 02:33:00,000
and mynumber 1000 is now in scientific
4478
02:33:00,000 --> 02:33:01,359
notation
4479
02:33:01,359 --> 02:33:03,280
all right everybody so that's a few
4480
02:33:03,280 --> 02:33:05,200
things that you can do with the format
4481
02:33:05,200 --> 02:33:07,280
method it's an optional method that
4482
02:33:07,280 --> 02:33:08,399
gives users
4483
02:33:08,399 --> 02:33:10,720
more control when displaying output if
4484
02:33:10,720 --> 02:33:12,240
you would like a copy of all the code
4485
02:33:12,240 --> 02:33:13,680
that we've written here today i will
4486
02:33:13,680 --> 02:33:15,280
post everything in the comments down
4487
02:33:15,280 --> 02:33:16,000
below
4488
02:33:16,000 --> 02:33:17,840
don't be afraid to smash that like
4489
02:33:17,840 --> 02:33:20,160
button drop a comment down below and
4490
02:33:20,160 --> 02:33:21,439
subscribe if you'd like to become a
4491
02:33:21,439 --> 02:33:22,399
fellow bro
4492
02:33:22,399 --> 02:33:24,479
hey what's going on everybody it's bro
4493
02:33:24,479 --> 02:33:26,000
hope you're doing well and in this video
4494
02:33:26,000 --> 02:33:27,760
i'm going to explain a few useful
4495
02:33:27,760 --> 02:33:29,359
methods of the random module
4496
02:33:29,359 --> 02:33:34,720
so sit back relax and enjoy the show
4497
02:33:34,720 --> 02:33:36,800
okay people in this video i'm going to
4498
02:33:36,800 --> 02:33:38,640
explain how we can generate some random
4499
02:33:38,640 --> 02:33:39,200
numbers
4500
02:33:39,200 --> 02:33:41,760
as well as a few other useful methods of
4501
02:33:41,760 --> 02:33:43,120
the random module
4502
02:33:43,120 --> 02:33:45,040
now we're not going to be creating true
4503
02:33:45,040 --> 02:33:47,120
random numbers but something called
4504
02:33:47,120 --> 02:33:49,040
pseudorandom numbers which are fairly
4505
02:33:49,040 --> 02:33:51,280
darn close so the first step
4506
02:33:51,280 --> 02:33:53,600
is to import the random module at the
4507
02:33:53,600 --> 02:33:54,960
top import
4508
02:33:54,960 --> 02:33:57,120
random we now have access to everything
4509
02:33:57,120 --> 02:33:59,439
that the random module has to offer
4510
02:33:59,439 --> 02:34:01,439
let's begin by generating a random
4511
02:34:01,439 --> 02:34:02,880
number between one and six
4512
02:34:02,880 --> 02:34:04,640
like we're rolling a dice so what i'm
4513
02:34:04,640 --> 02:34:06,399
going to do is say x
4514
02:34:06,399 --> 02:34:10,319
equals random dot rand
4515
02:34:10,319 --> 02:34:12,319
and we can generate a random integer
4516
02:34:12,319 --> 02:34:13,760
between a certain range
4517
02:34:13,760 --> 02:34:16,640
if i want one through six i will state
4518
02:34:16,640 --> 02:34:17,200
one
4519
02:34:17,200 --> 02:34:19,600
comma six and then we can generate a
4520
02:34:19,600 --> 02:34:21,439
random number between one and six like
4521
02:34:21,439 --> 02:34:22,640
we're rolling a dice
4522
02:34:22,640 --> 02:34:26,080
so if i were to print the value of x
4523
02:34:26,080 --> 02:34:27,840
then we will get a random number between
4524
02:34:27,840 --> 02:34:30,160
one and six for example i just got a one
4525
02:34:30,160 --> 02:34:33,680
a two a three and a six all right
4526
02:34:33,680 --> 02:34:35,920
we can also generate a random floating
4527
02:34:35,920 --> 02:34:37,120
point number two
4528
02:34:37,120 --> 02:34:40,560
let's say y equals random dot
4529
02:34:40,560 --> 02:34:43,760
random so this will give us a random
4530
02:34:43,760 --> 02:34:44,640
number between
4531
02:34:44,640 --> 02:34:47,840
0 and 1. this time i'm going to print
4532
02:34:47,840 --> 02:34:52,760
y so we have 0.49 and some change
4533
02:34:52,760 --> 02:34:56,000
0.145 and 0.858
4534
02:34:56,000 --> 02:34:58,399
we can also generate a random choice
4535
02:34:58,399 --> 02:35:00,479
from a list or other collection
4536
02:35:00,479 --> 02:35:02,080
let's say we're playing a game of rock
4537
02:35:02,080 --> 02:35:04,640
paper scissors so let's create a list
4538
02:35:04,640 --> 02:35:08,080
i'll just call this my list equals
4539
02:35:08,080 --> 02:35:10,720
square brackets and we will have three
4540
02:35:10,720 --> 02:35:11,439
choices
4541
02:35:11,439 --> 02:35:14,720
rock paper and
4542
02:35:14,720 --> 02:35:17,200
scissors
4543
02:35:18,560 --> 02:35:21,680
so let's say z
4544
02:35:21,680 --> 02:35:24,479
will be the random string that we
4545
02:35:24,479 --> 02:35:25,359
generate
4546
02:35:25,359 --> 02:35:28,479
z equals random dot
4547
02:35:28,479 --> 02:35:33,040
choice and we will pass in my list
4548
02:35:33,040 --> 02:35:36,880
and let's print z so this will generate
4549
02:35:36,880 --> 02:35:37,359
a
4550
02:35:37,359 --> 02:35:40,080
random choice from my list called my
4551
02:35:40,080 --> 02:35:40,560
list
4552
02:35:40,560 --> 02:35:43,600
so we got scissors paper and
4553
02:35:43,600 --> 02:35:46,399
rock we can also use the shuffle method
4554
02:35:46,399 --> 02:35:47,840
of the random module to
4555
02:35:47,840 --> 02:35:50,399
shuffle a list or other collection let's
4556
02:35:50,399 --> 02:35:52,720
say we're working with a deck of cards
4557
02:35:52,720 --> 02:35:56,479
cards equals let's say one
4558
02:35:56,479 --> 02:35:59,040
through nine
4559
02:36:00,240 --> 02:36:04,640
and we have a jack a queen
4560
02:36:04,640 --> 02:36:08,160
a king and an ace this won't be a full
4561
02:36:08,160 --> 02:36:10,399
deck of cards it'll just be one suit
4562
02:36:10,399 --> 02:36:13,200
so we can actually shuffle this list by
4563
02:36:13,200 --> 02:36:14,880
using the shuffle method
4564
02:36:14,880 --> 02:36:18,479
random dot shuffle what do we want to
4565
02:36:18,479 --> 02:36:18,960
shuffle
4566
02:36:18,960 --> 02:36:22,560
we want to shuffle our cards and i will
4567
02:36:22,560 --> 02:36:23,200
display
4568
02:36:23,200 --> 02:36:26,319
my cards with a print statement so the
4569
02:36:26,319 --> 02:36:28,399
shuffle method will shuffle a list or
4570
02:36:28,399 --> 02:36:29,680
other collection for you
4571
02:36:29,680 --> 02:36:31,520
well everybody that's a few useful
4572
02:36:31,520 --> 02:36:33,280
methods of the random module
4573
02:36:33,280 --> 02:36:34,560
if you would like a copy of all this
4574
02:36:34,560 --> 02:36:36,399
code i will post this in the comments
4575
02:36:36,399 --> 02:36:37,280
down below
4576
02:36:37,280 --> 02:36:39,120
don't be afraid to smash that like
4577
02:36:39,120 --> 02:36:40,960
button drop a comment down below
4578
02:36:40,960 --> 02:36:42,560
and subscribe if you'd like to become a
4579
02:36:42,560 --> 02:36:45,439
fellow bro hey what's going on everybody
4580
02:36:45,439 --> 02:36:45,840
it's
4581
02:36:45,840 --> 02:36:47,600
bro hope you're doing well and in this
4582
02:36:47,600 --> 02:36:49,120
video i'm going to explain exception
4583
02:36:49,120 --> 02:36:50,240
handling in python
4584
02:36:50,240 --> 02:36:55,439
so sit back relax and enjoy the show
4585
02:36:55,680 --> 02:36:57,439
well everyone let's talk about
4586
02:36:57,439 --> 02:36:59,680
exceptions an exception is an event
4587
02:36:59,680 --> 02:37:00,319
detected
4588
02:37:00,319 --> 02:37:02,720
during execution that interrupts the
4589
02:37:02,720 --> 02:37:04,319
normal flow of a program
4590
02:37:04,319 --> 02:37:06,319
here's a program that i've written and
4591
02:37:06,319 --> 02:37:07,840
we're intentionally going to cause an
4592
02:37:07,840 --> 02:37:08,640
exception
4593
02:37:08,640 --> 02:37:10,399
so this program that i've written we
4594
02:37:10,399 --> 02:37:12,319
will accept a numerator and a
4595
02:37:12,319 --> 02:37:13,920
denominator from the user
4596
02:37:13,920 --> 02:37:15,439
we'll divide the numerator by the
4597
02:37:15,439 --> 02:37:17,120
denominator we'll get a result
4598
02:37:17,120 --> 02:37:19,120
and print the result so what if i were
4599
02:37:19,120 --> 02:37:21,439
to divide a number such as 5
4600
02:37:21,439 --> 02:37:23,439
divided by 0 which we cannot
4601
02:37:23,439 --> 02:37:24,720
mathematically do
4602
02:37:24,720 --> 02:37:26,800
well this will cause an exception an
4603
02:37:26,800 --> 02:37:27,840
event detected
4604
02:37:27,840 --> 02:37:30,080
during execution that interrupts the
4605
02:37:30,080 --> 02:37:31,359
flow of a program
4606
02:37:31,359 --> 02:37:33,680
so our program was interrupted and
4607
02:37:33,680 --> 02:37:35,439
within our console window we have this
4608
02:37:35,439 --> 02:37:36,800
traceback message
4609
02:37:36,800 --> 02:37:39,439
which states what went wrong and a name
4610
02:37:39,439 --> 02:37:41,120
for this exception that occurred
4611
02:37:41,120 --> 02:37:43,040
this specific exception that occurred is
4612
02:37:43,040 --> 02:37:44,640
a zero division error
4613
02:37:44,640 --> 02:37:47,040
so this video is all about handling
4614
02:37:47,040 --> 02:37:48,960
these exceptions so they do not
4615
02:37:48,960 --> 02:37:51,040
interrupt the normal flow of a program
4616
02:37:51,040 --> 02:37:53,200
now a very basic form of exception
4617
02:37:53,200 --> 02:37:53,840
handling
4618
02:37:53,840 --> 02:37:55,439
is to surround any code that is
4619
02:37:55,439 --> 02:37:57,359
considered dangerous as in it might
4620
02:37:57,359 --> 02:37:58,479
cause an exception
4621
02:37:58,479 --> 02:38:01,120
within a try block anytime you accept
4622
02:38:01,120 --> 02:38:03,359
user input would be a good indicator too
4623
02:38:03,359 --> 02:38:05,359
so this code is considered dangerous
4624
02:38:05,359 --> 02:38:06,800
because we don't know what the user is
4625
02:38:06,800 --> 02:38:07,920
going to type in
4626
02:38:07,920 --> 02:38:10,399
so we will try all of this code if an
4627
02:38:10,399 --> 02:38:11,439
exception occurs
4628
02:38:11,439 --> 02:38:13,840
we can catch it and handle it so to do
4629
02:38:13,840 --> 02:38:14,479
that
4630
02:38:14,479 --> 02:38:16,640
we'll add an accept block accept and
4631
02:38:16,640 --> 02:38:18,319
then we will catch
4632
02:38:18,319 --> 02:38:21,359
exception normally having this block by
4633
02:38:21,359 --> 02:38:21,840
itself
4634
02:38:21,840 --> 02:38:23,520
isn't considered good practice but i'll
4635
02:38:23,520 --> 02:38:25,120
explain why later so
4636
02:38:25,120 --> 02:38:27,600
this accept exception block will catch
4637
02:38:27,600 --> 02:38:29,680
all sorts of different exceptions
4638
02:38:29,680 --> 02:38:31,280
and it will prevent our program from
4639
02:38:31,280 --> 02:38:32,880
being interrupted and we can do
4640
02:38:32,880 --> 02:38:34,880
something else entirely such as print a
4641
02:38:34,880 --> 02:38:35,680
message
4642
02:38:35,680 --> 02:38:39,359
something went wrong so let's try this
4643
02:38:39,359 --> 02:38:40,160
again
4644
02:38:40,160 --> 02:38:42,560
we will divide five by zero which we
4645
02:38:42,560 --> 02:38:43,439
cannot do
4646
02:38:43,439 --> 02:38:45,600
we no longer have that traceback message
4647
02:38:45,600 --> 02:38:46,960
within our console window
4648
02:38:46,960 --> 02:38:48,640
we caught the exception and we did
4649
02:38:48,640 --> 02:38:50,399
something else we printed
4650
02:38:50,399 --> 02:38:53,280
something went wrong like i said before
4651
02:38:53,280 --> 02:38:55,040
it's not considered good practice to
4652
02:38:55,040 --> 02:38:56,080
have a single
4653
02:38:56,080 --> 02:38:58,000
accept block that will handle all
4654
02:38:58,000 --> 02:39:00,240
exceptions it's much better to first
4655
02:39:00,240 --> 02:39:02,399
handle specific exceptions when they
4656
02:39:02,399 --> 02:39:02,960
occur
4657
02:39:02,960 --> 02:39:05,040
and we can do so by writing additional
4658
02:39:05,040 --> 02:39:06,319
accept blocks
4659
02:39:06,319 --> 02:39:08,560
so let's create an additional accept
4660
02:39:08,560 --> 02:39:10,000
block that will catch
4661
02:39:10,000 --> 02:39:13,120
any zero division error exceptions
4662
02:39:13,120 --> 02:39:14,479
that occurs when somebody attempts to
4663
02:39:14,479 --> 02:39:16,640
divide by zero so if
4664
02:39:16,640 --> 02:39:18,479
this exception occurs how do we want to
4665
02:39:18,479 --> 02:39:20,479
handle it let's just print the message
4666
02:39:20,479 --> 02:39:24,319
let's print you can't divide
4667
02:39:24,319 --> 02:39:27,359
by zero idiot
4668
02:39:27,359 --> 02:39:30,240
and let's try this again so five divided
4669
02:39:30,240 --> 02:39:31,520
by zero
4670
02:39:31,520 --> 02:39:33,840
you can't divide by zero idiot here's
4671
02:39:33,840 --> 02:39:35,439
another one i'm going to turn
4672
02:39:35,439 --> 02:39:38,000
this accept block into a comment so we
4673
02:39:38,000 --> 02:39:39,359
can take a look at something
4674
02:39:39,359 --> 02:39:41,359
what if i divide by something that's not
4675
02:39:41,359 --> 02:39:42,720
a number like we take
4676
02:39:42,720 --> 02:39:45,200
5 and divide it by the word pizza well
4677
02:39:45,200 --> 02:39:46,560
pizza is not a number
4678
02:39:46,560 --> 02:39:50,160
and we ran into a value error exception
4679
02:39:50,160 --> 02:39:52,399
so we should probably handle this too so
4680
02:39:52,399 --> 02:39:54,399
let's create another accept block that
4681
02:39:54,399 --> 02:39:55,680
will handle that
4682
02:39:55,680 --> 02:39:59,840
accept value error
4683
02:39:59,840 --> 02:40:02,000
and how do we want to handle this let's
4684
02:40:02,000 --> 02:40:03,920
print something
4685
02:40:03,920 --> 02:40:06,960
enter only numbers
4686
02:40:06,960 --> 02:40:10,399
please okay let's try this again and see
4687
02:40:10,399 --> 02:40:10,880
if
4688
02:40:10,880 --> 02:40:13,680
this exception is caught five divided by
4689
02:40:13,680 --> 02:40:14,240
the word
4690
02:40:14,240 --> 02:40:17,920
pizza enter only numbers please
4691
02:40:17,920 --> 02:40:20,960
not only that but you can also add the
4692
02:40:20,960 --> 02:40:23,200
accept exception block at the very end
4693
02:40:23,200 --> 02:40:24,880
just in case there's something you won't
4694
02:40:24,880 --> 02:40:25,840
anticipate
4695
02:40:25,840 --> 02:40:27,520
but it's always considered good practice
4696
02:40:27,520 --> 02:40:30,160
to first catch any specific exceptions
4697
02:40:30,160 --> 02:40:31,920
and let the user know exactly what went
4698
02:40:31,920 --> 02:40:34,479
wrong an additional way in which we can
4699
02:40:34,479 --> 02:40:35,760
handle these exceptions
4700
02:40:35,760 --> 02:40:38,000
is that we could display the exception
4701
02:40:38,000 --> 02:40:39,840
that occurs although it is completely
4702
02:40:39,840 --> 02:40:40,560
optional
4703
02:40:40,560 --> 02:40:42,319
so to do that what we'll do is that
4704
02:40:42,319 --> 02:40:44,160
after each of these accept block
4705
02:40:44,160 --> 02:40:45,120
definitions
4706
02:40:45,120 --> 02:40:48,319
let's add as e so we're calling the
4707
02:40:48,319 --> 02:40:49,279
exception as
4708
02:40:49,279 --> 02:40:50,960
e that's just standard practice although
4709
02:40:50,960 --> 02:40:52,319
it's not necessary
4710
02:40:52,319 --> 02:40:54,479
so let's do that for each of these
4711
02:40:54,479 --> 02:40:55,760
except blocks
4712
02:40:55,760 --> 02:40:58,720
and then we can print whatever e is so
4713
02:40:58,720 --> 02:40:59,920
let's add that
4714
02:40:59,920 --> 02:41:02,960
to each of these blocks so
4715
02:41:02,960 --> 02:41:04,800
it should now look something like this
4716
02:41:04,800 --> 02:41:06,800
so when we encounter an exception
4717
02:41:06,800 --> 02:41:09,200
we can display what exception occurred
4718
02:41:09,200 --> 02:41:10,080
too as well
4719
02:41:10,080 --> 02:41:13,600
so let's divide five by pizza invalid
4720
02:41:13,600 --> 02:41:16,800
literal for int with base 10 pizza
4721
02:41:16,800 --> 02:41:19,680
enter only numbers please so like i said
4722
02:41:19,680 --> 02:41:20,080
this
4723
02:41:20,080 --> 02:41:21,680
is an additional way in which you could
4724
02:41:21,680 --> 02:41:23,279
handle an exception although it's
4725
02:41:23,279 --> 02:41:24,640
entirely optional
4726
02:41:24,640 --> 02:41:26,319
another thing that you can do is that
4727
02:41:26,319 --> 02:41:28,160
you can add an else statement
4728
02:41:28,160 --> 02:41:30,800
to the end of your accept blocks so
4729
02:41:30,800 --> 02:41:32,319
what's happening here is that we're
4730
02:41:32,319 --> 02:41:32,960
going to
4731
02:41:32,960 --> 02:41:35,279
try all of this code if an exception
4732
02:41:35,279 --> 02:41:36,080
happens
4733
02:41:36,080 --> 02:41:38,880
then catch it and handle it if not then
4734
02:41:38,880 --> 02:41:40,000
do something else
4735
02:41:40,000 --> 02:41:42,960
so why don't we print our result only if
4736
02:41:42,960 --> 02:41:43,600
there are no
4737
02:41:43,600 --> 02:41:46,720
exceptions that occur so if there are no
4738
02:41:46,720 --> 02:41:48,399
exceptions we will execute
4739
02:41:48,399 --> 02:41:51,279
this else statement if not then we won't
4740
02:41:51,279 --> 02:41:51,840
so
4741
02:41:51,840 --> 02:41:53,840
let's divide five by an actual number
4742
02:41:53,840 --> 02:41:54,960
this time like
4743
02:41:54,960 --> 02:41:57,439
two so there are no exceptions that
4744
02:41:57,439 --> 02:41:58,319
occurred so
4745
02:41:58,319 --> 02:42:00,720
we executed this else statement whatever
4746
02:42:00,720 --> 02:42:01,680
code is within
4747
02:42:01,680 --> 02:42:03,439
our else block and this printed the
4748
02:42:03,439 --> 02:42:05,200
result of 2.5
4749
02:42:05,200 --> 02:42:07,200
but if an exception were to occur like
4750
02:42:07,200 --> 02:42:08,479
we divide 5 by
4751
02:42:08,479 --> 02:42:11,840
0 then we will catch our exception and
4752
02:42:11,840 --> 02:42:12,479
handle it
4753
02:42:12,479 --> 02:42:15,040
and we do not execute whatever block of
4754
02:42:15,040 --> 02:42:16,080
code is within
4755
02:42:16,080 --> 02:42:18,319
our else statement there is one other
4756
02:42:18,319 --> 02:42:20,000
clause that you should be aware of that
4757
02:42:20,000 --> 02:42:20,560
is the
4758
02:42:20,560 --> 02:42:22,960
final clause this is always at the end
4759
02:42:22,960 --> 02:42:23,840
how this works
4760
02:42:23,840 --> 02:42:25,920
is that whether or not we catch an
4761
02:42:25,920 --> 02:42:28,319
exception we will always execute any
4762
02:42:28,319 --> 02:42:28,720
code
4763
02:42:28,720 --> 02:42:30,720
that is within the block of code for our
4764
02:42:30,720 --> 02:42:32,000
final clause
4765
02:42:32,000 --> 02:42:34,160
so this is a good opportunity if you
4766
02:42:34,160 --> 02:42:35,120
open files
4767
02:42:35,120 --> 02:42:38,000
to close them within the finally block
4768
02:42:38,000 --> 02:42:38,399
but
4769
02:42:38,399 --> 02:42:40,399
we haven't dealt with opening files yet
4770
02:42:40,399 --> 02:42:41,600
so we won't do that
4771
02:42:41,600 --> 02:42:43,120
all we'll do is just print something
4772
02:42:43,120 --> 02:42:45,040
print this
4773
02:42:45,040 --> 02:42:48,399
will always execute so
4774
02:42:48,399 --> 02:42:50,640
whether or not we encounter an exception
4775
02:42:50,640 --> 02:42:52,720
this finally block will always execute
4776
02:42:52,720 --> 02:42:53,359
at the end
4777
02:42:53,359 --> 02:42:55,520
so let's try this again so this time
4778
02:42:55,520 --> 02:42:56,880
let's cause an exception
4779
02:42:56,880 --> 02:42:59,600
let's divide five by zero you can't
4780
02:42:59,600 --> 02:43:00,240
divide by
4781
02:43:00,240 --> 02:43:02,720
zero this will always execute and this
4782
02:43:02,720 --> 02:43:04,479
time let's take this seriously and
4783
02:43:04,479 --> 02:43:04,960
divide
4784
02:43:04,960 --> 02:43:07,760
five by another number such as three so
4785
02:43:07,760 --> 02:43:09,359
this will give us a result
4786
02:43:09,359 --> 02:43:11,680
and like i said the finally clause the
4787
02:43:11,680 --> 02:43:13,279
code within the finally clause will
4788
02:43:13,279 --> 02:43:14,399
always execute
4789
02:43:14,399 --> 02:43:16,240
but we won't really be working with this
4790
02:43:16,240 --> 02:43:18,319
until we get to the section on file
4791
02:43:18,319 --> 02:43:19,359
handling
4792
02:43:19,359 --> 02:43:21,680
well everybody those are exceptions in
4793
02:43:21,680 --> 02:43:24,080
python they are events detected
4794
02:43:24,080 --> 02:43:26,479
during execution that interrupt the flow
4795
02:43:26,479 --> 02:43:27,520
of a program
4796
02:43:27,520 --> 02:43:29,520
and we can handle these exceptions by
4797
02:43:29,520 --> 02:43:31,680
using try and accept blocks
4798
02:43:31,680 --> 02:43:33,359
so if you would like a copy of all this
4799
02:43:33,359 --> 02:43:34,960
code i will post all of this in the
4800
02:43:34,960 --> 02:43:36,319
comments down below
4801
02:43:36,319 --> 02:43:38,800
but yeah that is how exceptions work in
4802
02:43:38,800 --> 02:43:40,319
python
4803
02:43:40,319 --> 02:43:42,880
hey what's going on everybody it's bro
4804
02:43:42,880 --> 02:43:43,920
hope you're doing well
4805
02:43:43,920 --> 02:43:45,200
and in this video we're going to be
4806
02:43:45,200 --> 02:43:47,439
covering some basic file detection using
4807
02:43:47,439 --> 02:43:48,399
python so
4808
02:43:48,399 --> 02:43:52,960
sit back relax and enjoy the show
4809
02:43:53,040 --> 02:43:55,040
welcome ladies and gentlemen to the
4810
02:43:55,040 --> 02:43:57,120
beginning of a mini series involving
4811
02:43:57,120 --> 02:43:59,439
doing stuff with files using python and
4812
02:43:59,439 --> 02:44:00,880
in this video we're going to be doing
4813
02:44:00,880 --> 02:44:01,680
some basic
4814
02:44:01,680 --> 02:44:04,240
file detection and in order to do so i
4815
02:44:04,240 --> 02:44:04,800
recommend
4816
02:44:04,800 --> 02:44:07,120
importing the os module it's already
4817
02:44:07,120 --> 02:44:08,800
included with the standard python
4818
02:44:08,800 --> 02:44:09,359
library
4819
02:44:09,359 --> 02:44:11,359
all you need to do is import it all
4820
02:44:11,359 --> 02:44:13,520
we'll be doing is checking to see if a
4821
02:44:13,520 --> 02:44:14,000
file
4822
02:44:14,000 --> 02:44:16,080
exists someplace on our computer so
4823
02:44:16,080 --> 02:44:17,840
we'll probably need a file to work with
4824
02:44:17,840 --> 02:44:20,000
what i'm going to do is go to my desktop
4825
02:44:20,000 --> 02:44:21,120
for convenience
4826
02:44:21,120 --> 02:44:23,520
and just create a new text document i
4827
02:44:23,520 --> 02:44:25,760
will call this test.txt
4828
02:44:25,760 --> 02:44:28,000
now i probably need the file location so
4829
02:44:28,000 --> 02:44:30,319
i'm going to copy that
4830
02:44:30,319 --> 02:44:32,960
and then we can use this so let's create
4831
02:44:32,960 --> 02:44:34,479
a variable called path
4832
02:44:34,479 --> 02:44:36,640
that will include the path to the file
4833
02:44:36,640 --> 02:44:38,160
that i would like to detect
4834
02:44:38,160 --> 02:44:41,760
so that was test.txt now if you have
4835
02:44:41,760 --> 02:44:44,319
backslashes in your file path you'll
4836
02:44:44,319 --> 02:44:45,200
probably need
4837
02:44:45,200 --> 02:44:47,359
double backslashes because that's the
4838
02:44:47,359 --> 02:44:49,840
escape sequence for a backslash within a
4839
02:44:49,840 --> 02:44:50,560
string
4840
02:44:50,560 --> 02:44:53,120
so we now have this variable path that
4841
02:44:53,120 --> 02:44:54,160
we can work with
4842
02:44:54,160 --> 02:44:56,720
now i would like to check to see if this
4843
02:44:56,720 --> 02:44:57,520
location
4844
02:44:57,520 --> 02:45:00,560
exists on my computer so if os
4845
02:45:00,560 --> 02:45:04,160
dot path dot exists and you can see that
4846
02:45:04,160 --> 02:45:05,120
we can pass in
4847
02:45:05,120 --> 02:45:07,600
our path as an argument so i will pass
4848
02:45:07,600 --> 02:45:08,560
in my path
4849
02:45:08,560 --> 02:45:10,880
and if this returns true if that
4850
02:45:10,880 --> 02:45:14,319
location exists i will print
4851
02:45:14,319 --> 02:45:18,960
that location exists
4852
02:45:18,960 --> 02:45:21,279
so this will not tell me if this is a
4853
02:45:21,279 --> 02:45:22,319
file or not
4854
02:45:22,319 --> 02:45:25,359
we'll be covering that later else
4855
02:45:25,359 --> 02:45:29,680
let's print that location
4856
02:45:29,680 --> 02:45:32,800
doesn't exist
4857
02:45:32,800 --> 02:45:35,439
all right so if this location exists
4858
02:45:35,439 --> 02:45:37,040
this will return true
4859
02:45:37,040 --> 02:45:39,359
if not this will execute our else
4860
02:45:39,359 --> 02:45:40,399
statement
4861
02:45:40,399 --> 02:45:43,200
that location exists so what if i were
4862
02:45:43,200 --> 02:45:44,399
to delete this file
4863
02:45:44,399 --> 02:45:46,880
and we run this program again that
4864
02:45:46,880 --> 02:45:48,880
location doesn't exist
4865
02:45:48,880 --> 02:45:51,600
now this won't tell you if the location
4866
02:45:51,600 --> 02:45:52,399
you're dealing with
4867
02:45:52,399 --> 02:45:55,040
is a file or not there is a separate
4868
02:45:55,040 --> 02:45:56,399
function for that
4869
02:45:56,399 --> 02:45:59,840
it is if os
4870
02:45:59,840 --> 02:46:05,040
dot path dot is file
4871
02:46:05,200 --> 02:46:07,680
what we'll do is pass in our path and
4872
02:46:07,680 --> 02:46:09,760
print
4873
02:46:09,760 --> 02:46:12,880
that is a file
4874
02:46:12,880 --> 02:46:16,240
all right so let's create another file
4875
02:46:16,240 --> 02:46:18,359
because i deleted the previous one
4876
02:46:18,359 --> 02:46:21,279
test.txt it's in the same location let's
4877
02:46:21,279 --> 02:46:22,399
run this again
4878
02:46:22,399 --> 02:46:25,520
that location exists that is a file now
4879
02:46:25,520 --> 02:46:27,439
what if this was a folder
4880
02:46:27,439 --> 02:46:29,439
so i'm going to create a new folder i
4881
02:46:29,439 --> 02:46:30,880
will call this folder
4882
02:46:30,880 --> 02:46:34,640
i will delete my test document
4883
02:46:34,640 --> 02:46:39,200
and let's change the location to folder
4884
02:46:39,200 --> 02:46:41,279
that location exists but it is not a
4885
02:46:41,279 --> 02:46:43,600
file you can check to see if a location
4886
02:46:43,600 --> 02:46:44,880
is a directory
4887
02:46:44,880 --> 02:46:48,160
so let's write an else if statement os
4888
02:46:48,160 --> 02:46:52,319
dot path dot is directory and we will
4889
02:46:52,319 --> 02:46:54,479
pass in our path
4890
02:46:54,479 --> 02:46:57,840
and let's print that
4891
02:46:57,840 --> 02:47:01,840
is a directory
4892
02:47:01,840 --> 02:47:03,760
all right so we have our folder we're
4893
02:47:03,760 --> 02:47:05,359
going to check to see
4894
02:47:05,359 --> 02:47:08,479
if this location exists and if it's a
4895
02:47:08,479 --> 02:47:09,120
file or
4896
02:47:09,120 --> 02:47:12,240
a directory that location exists that is
4897
02:47:12,240 --> 02:47:13,359
a directory
4898
02:47:13,359 --> 02:47:15,760
well everyone that is the basics of file
4899
02:47:15,760 --> 02:47:16,399
detection
4900
02:47:16,399 --> 02:47:18,319
we'll be building upon this concept in
4901
02:47:18,319 --> 02:47:19,520
future videos
4902
02:47:19,520 --> 02:47:21,680
so if you would like a copy of this code
4903
02:47:21,680 --> 02:47:23,439
i will post this in the comment section
4904
02:47:23,439 --> 02:47:24,160
down below
4905
02:47:24,160 --> 02:47:26,640
but yeah that is the basics of simple
4906
02:47:26,640 --> 02:47:28,960
file detection using python
4907
02:47:28,960 --> 02:47:31,040
hey what's going on everybody it's bro
4908
02:47:31,040 --> 02:47:32,640
hope you're doing well and in this video
4909
02:47:32,640 --> 02:47:34,479
we're going to be reading a file in
4910
02:47:34,479 --> 02:47:35,439
python so
4911
02:47:35,439 --> 02:47:39,920
sit back relax and enjoy the show
4912
02:47:40,000 --> 02:47:42,240
welcome back guys and gals in this video
4913
02:47:42,240 --> 02:47:44,000
i'm going to explain how we can read the
4914
02:47:44,000 --> 02:47:46,319
contents of a file using python
4915
02:47:46,319 --> 02:47:48,240
so we'll probably need some sort of file
4916
02:47:48,240 --> 02:47:50,720
to work with i created a plain text file
4917
02:47:50,720 --> 02:47:51,720
called
4918
02:47:51,720 --> 02:47:54,640
test.txt it's a plain text file and all
4919
02:47:54,640 --> 02:47:55,279
this says
4920
02:47:55,279 --> 02:47:58,960
is omg you can read this have a nice day
4921
02:47:58,960 --> 02:48:01,359
like comment and subscribe so what i
4922
02:48:01,359 --> 02:48:02,560
would like to do is to read
4923
02:48:02,560 --> 02:48:04,960
the contents of that file line by line
4924
02:48:04,960 --> 02:48:06,640
and print it to the console window
4925
02:48:06,640 --> 02:48:08,479
and here's how and honestly we only need
4926
02:48:08,479 --> 02:48:10,080
two lines of code to do this
4927
02:48:10,080 --> 02:48:13,520
so what we'll do is type with open
4928
02:48:13,520 --> 02:48:15,840
then within the open function list the
4929
02:48:15,840 --> 02:48:17,680
name of your file
4930
02:48:17,680 --> 02:48:20,240
or the file path this file of mine is
4931
02:48:20,240 --> 02:48:21,920
within my project folder
4932
02:48:21,920 --> 02:48:24,560
so i only need to list the file name if
4933
02:48:24,560 --> 02:48:26,640
this was someplace else you'll probably
4934
02:48:26,640 --> 02:48:27,200
need
4935
02:48:27,200 --> 02:48:30,160
the file path so let's say this was on
4936
02:48:30,160 --> 02:48:31,120
my desktop
4937
02:48:31,120 --> 02:48:33,359
well i'll probably need to list the file
4938
02:48:33,359 --> 02:48:35,520
path of where that file is located
4939
02:48:35,520 --> 02:48:38,080
and i'll need to use double backslashes
4940
02:48:38,080 --> 02:48:40,319
because that's the escape sequence for a
4941
02:48:40,319 --> 02:48:41,200
backslash
4942
02:48:41,200 --> 02:48:43,359
but since this file is within my project
4943
02:48:43,359 --> 02:48:45,920
folder i only need to list the file name
4944
02:48:45,920 --> 02:48:49,200
so with open then the name your file or
4945
02:48:49,200 --> 02:48:50,720
the file path
4946
02:48:50,720 --> 02:48:54,560
as file then what we're going to do
4947
02:48:54,560 --> 02:48:58,720
is print file dot
4948
02:48:58,720 --> 02:49:02,319
read and that is it so let's test it
4949
02:49:02,319 --> 02:49:05,359
and it works omg you can read this have
4950
02:49:05,359 --> 02:49:06,240
a nice day
4951
02:49:06,240 --> 02:49:09,120
like comment and subscribe so this will
4952
02:49:09,120 --> 02:49:09,680
actually
4953
02:49:09,680 --> 02:49:12,399
close files automatically after opening
4954
02:49:12,399 --> 02:49:12,720
them
4955
02:49:12,720 --> 02:49:14,560
so that's kind of convenient and let's
4956
02:49:14,560 --> 02:49:15,840
just test that theory
4957
02:49:15,840 --> 02:49:19,120
so i'm going to print file dot
4958
02:49:19,120 --> 02:49:22,160
closed if my file is in fact closed this
4959
02:49:22,160 --> 02:49:22,800
will print
4960
02:49:22,800 --> 02:49:24,880
true if it's still open this will print
4961
02:49:24,880 --> 02:49:26,000
false
4962
02:49:26,000 --> 02:49:28,160
so normally when you read a file it's
4963
02:49:28,160 --> 02:49:29,680
normally open and you need to
4964
02:49:29,680 --> 02:49:31,680
close it manually but if you write your
4965
02:49:31,680 --> 02:49:32,880
program this way
4966
02:49:32,880 --> 02:49:35,279
with open this will close the file
4967
02:49:35,279 --> 02:49:36,880
automatically for you
4968
02:49:36,880 --> 02:49:39,840
so using with open will close any files
4969
02:49:39,840 --> 02:49:41,040
automatically for you
4970
02:49:41,040 --> 02:49:43,520
after they've been opened however this
4971
02:49:43,520 --> 02:49:44,640
does not catch
4972
02:49:44,640 --> 02:49:46,640
and handle any exceptions that might
4973
02:49:46,640 --> 02:49:49,359
occur like if we cannot locate this file
4974
02:49:49,359 --> 02:49:51,439
so let's say that i mistyped the
4975
02:49:51,439 --> 02:49:53,200
extension for this file
4976
02:49:53,200 --> 02:49:56,640
let's say that i type in test.tx
4977
02:49:56,640 --> 02:49:58,720
that's kind of funny because tx is the
4978
02:49:58,720 --> 02:49:59,760
abbreviation for
4979
02:49:59,760 --> 02:50:02,800
texas it's a texas file so this will
4980
02:50:02,800 --> 02:50:03,279
cause
4981
02:50:03,279 --> 02:50:05,920
a file not found error and this will
4982
02:50:05,920 --> 02:50:08,080
interrupt the flow of our program and we
4983
02:50:08,080 --> 02:50:09,760
would probably like to prevent that
4984
02:50:09,760 --> 02:50:12,240
so what we could do is write a try and
4985
02:50:12,240 --> 02:50:13,279
an accept block
4986
02:50:13,279 --> 02:50:15,520
so we will try all of this code if we
4987
02:50:15,520 --> 02:50:16,399
cannot find
4988
02:50:16,399 --> 02:50:19,200
this file we can handle this exception
4989
02:50:19,200 --> 02:50:20,640
file not found error
4990
02:50:20,640 --> 02:50:22,399
so we'll place all of this code within
4991
02:50:22,399 --> 02:50:23,760
the try block then
4992
02:50:23,760 --> 02:50:25,600
if an exception happens we will catch
4993
02:50:25,600 --> 02:50:26,800
that exception
4994
02:50:26,800 --> 02:50:29,760
except we would like to catch any file
4995
02:50:29,760 --> 02:50:30,800
not found air
4996
02:50:30,800 --> 02:50:33,359
exceptions and let's handle this by
4997
02:50:33,359 --> 02:50:35,600
printing
4998
02:50:35,600 --> 02:50:40,160
that file was not found
4999
02:50:40,160 --> 02:50:42,880
so if i were to run this again we will
5000
02:50:42,880 --> 02:50:44,880
catch and handle this exception and this
5001
02:50:44,880 --> 02:50:45,359
will not
5002
02:50:45,359 --> 02:50:48,160
interrupt the normal flow of our program
5003
02:50:48,160 --> 02:50:50,319
well everybody that's how to open and
5004
02:50:50,319 --> 02:50:50,640
read
5005
02:50:50,640 --> 02:50:53,359
a file in python if you'd like a copy of
5006
02:50:53,359 --> 02:50:55,120
all this code i will post this in the
5007
02:50:55,120 --> 02:50:56,640
comment section down below
5008
02:50:56,640 --> 02:50:58,960
but yeah that's how to open and read a
5009
02:50:58,960 --> 02:51:00,720
file using python
5010
02:51:00,720 --> 02:51:03,040
hey what's going on everybody it's bro
5011
02:51:03,040 --> 02:51:04,560
hope you're doing well and in this video
5012
02:51:04,560 --> 02:51:06,160
we're going to be writing files in
5013
02:51:06,160 --> 02:51:07,840
python so sit back
5014
02:51:07,840 --> 02:51:11,600
relax and enjoy the show
5015
02:51:11,760 --> 02:51:13,920
so to begin it's kind of similar to the
5016
02:51:13,920 --> 02:51:15,840
last video on reading files
5017
02:51:15,840 --> 02:51:18,880
with open within the open function list
5018
02:51:18,880 --> 02:51:21,359
the file name or the file path so what
5019
02:51:21,359 --> 02:51:22,720
do you want to call this file
5020
02:51:22,720 --> 02:51:26,399
i will call this file test dot txt
5021
02:51:26,399 --> 02:51:28,800
now normally with the open function
5022
02:51:28,800 --> 02:51:29,920
there is a mode
5023
02:51:29,920 --> 02:51:31,520
and you can actually change that there's
5024
02:51:31,520 --> 02:51:33,520
a second argument that you can pass in
5025
02:51:33,520 --> 02:51:36,560
by default this is r for read but if you
5026
02:51:36,560 --> 02:51:38,720
want to write a file this would be
5027
02:51:38,720 --> 02:51:41,520
w so now we're going to write a file
5028
02:51:41,520 --> 02:51:42,279
called
5029
02:51:42,279 --> 02:51:46,080
test.txt so to finish with open
5030
02:51:46,080 --> 02:51:49,600
as file what are we going to do file
5031
02:51:49,600 --> 02:51:52,560
dot write and within the write function
5032
02:51:52,560 --> 02:51:54,240
we can write some text
5033
02:51:54,240 --> 02:51:57,279
so let's create a text variable and we
5034
02:51:57,279 --> 02:51:58,880
will add a string
5035
02:51:58,880 --> 02:52:03,040
so let's say text equals
5036
02:52:03,040 --> 02:52:06,800
yo and if you need to go down to a new
5037
02:52:06,800 --> 02:52:07,200
line
5038
02:52:07,200 --> 02:52:09,600
be sure to use the new line character so
5039
02:52:09,600 --> 02:52:11,200
after this new line character
5040
02:52:11,200 --> 02:52:13,520
the text that comes after is going to be
5041
02:52:13,520 --> 02:52:15,200
on the next line as if we're hitting
5042
02:52:15,200 --> 02:52:16,000
enter
5043
02:52:16,000 --> 02:52:19,439
so yo this is some
5044
02:52:19,439 --> 02:52:22,640
text have
5045
02:52:22,640 --> 02:52:26,319
a good one
5046
02:52:26,319 --> 02:52:29,120
all right so after i run this program we
5047
02:52:29,120 --> 02:52:29,920
should have
5048
02:52:29,920 --> 02:52:32,640
a file a text file within our project
5049
02:52:32,640 --> 02:52:34,560
folder that appears
5050
02:52:34,560 --> 02:52:37,439
and it is right here and it says yo this
5051
02:52:37,439 --> 02:52:38,399
is some text
5052
02:52:38,399 --> 02:52:41,439
have a good one now this is in write
5053
02:52:41,439 --> 02:52:42,000
mode
5054
02:52:42,000 --> 02:52:44,399
if this text was something else it will
5055
02:52:44,399 --> 02:52:45,279
actually
5056
02:52:45,279 --> 02:52:47,520
overwrite your current file so if i were
5057
02:52:47,520 --> 02:52:49,760
to change this to
5058
02:52:49,760 --> 02:52:53,040
uh oh this text has
5059
02:52:53,040 --> 02:52:56,640
been over written well then
5060
02:52:56,640 --> 02:52:59,200
this will overwrite the text that we
5061
02:52:59,200 --> 02:52:59,760
have
5062
02:52:59,760 --> 02:53:02,560
uh oh this text has been overwritten so
5063
02:53:02,560 --> 02:53:04,640
you can actually append a file by
5064
02:53:04,640 --> 02:53:05,760
changing the mode
5065
02:53:05,760 --> 02:53:08,319
to a for append so let me change this
5066
02:53:08,319 --> 02:53:09,120
back
5067
02:53:09,120 --> 02:53:12,000
we will rewrite this this will save over
5068
02:53:12,000 --> 02:53:13,040
our current file
5069
02:53:13,040 --> 02:53:15,600
now we can append some text so let's add
5070
02:53:15,600 --> 02:53:17,439
maybe a new line
5071
02:53:17,439 --> 02:53:20,560
uh have a nice
5072
02:53:20,560 --> 02:53:25,120
day see ya so i would like to append
5073
02:53:25,120 --> 02:53:26,800
some text to this file i'm going to
5074
02:53:26,800 --> 02:53:28,399
change the mode to a
5075
02:53:28,399 --> 02:53:31,120
for append and we can now append some
5076
02:53:31,120 --> 02:53:33,279
text onto the end of this file
5077
02:53:33,279 --> 02:53:35,439
well everybody that's how to write to a
5078
02:53:35,439 --> 02:53:37,520
file and or append a file if you would
5079
02:53:37,520 --> 02:53:38,800
like a copy of this code
5080
02:53:38,800 --> 02:53:40,560
i will post this in the comment section
5081
02:53:40,560 --> 02:53:42,960
down below but yeah that's how to
5082
02:53:42,960 --> 02:53:46,080
write to and or append a file in python
5083
02:53:46,080 --> 02:53:48,399
hey what's going on everybody it's bro
5084
02:53:48,399 --> 02:53:49,359
hope you're doing well
5085
02:53:49,359 --> 02:53:50,720
and in this video we're going to be
5086
02:53:50,720 --> 02:53:52,560
copying files in python so
5087
02:53:52,560 --> 02:53:57,200
sit back relax and enjoy the show
5088
02:53:57,200 --> 02:53:59,200
welcome back to another video regarding
5089
02:53:59,200 --> 02:54:01,520
doing stuff with files using python and
5090
02:54:01,520 --> 02:54:02,880
in this video we're going to be
5091
02:54:02,880 --> 02:54:05,439
copying some files and in order to do so
5092
02:54:05,439 --> 02:54:06,240
i recommend
5093
02:54:06,240 --> 02:54:08,640
importing the shuttle module there's
5094
02:54:08,640 --> 02:54:10,000
other ways of doing this too
5095
02:54:10,000 --> 02:54:11,920
but i tend to like using the shuttle
5096
02:54:11,920 --> 02:54:13,040
module now
5097
02:54:13,040 --> 02:54:15,840
with this module there are three basic
5098
02:54:15,840 --> 02:54:16,479
functions
5099
02:54:16,479 --> 02:54:19,279
to copy a file starting at the top they
5100
02:54:19,279 --> 02:54:20,800
are copy file
5101
02:54:20,800 --> 02:54:24,000
copy and copy to so copy file is what
5102
02:54:24,000 --> 02:54:25,120
we'll be working with
5103
02:54:25,120 --> 02:54:27,760
this will copy the contents of a file
5104
02:54:27,760 --> 02:54:28,479
copy
5105
02:54:28,479 --> 02:54:31,040
will do everything that copy file does
5106
02:54:31,040 --> 02:54:32,000
plus copy
5107
02:54:32,000 --> 02:54:34,800
the permissions mode and the destination
5108
02:54:34,800 --> 02:54:36,160
can be a directory
5109
02:54:36,160 --> 02:54:38,560
and lastly copy 2 this will do
5110
02:54:38,560 --> 02:54:39,200
everything
5111
02:54:39,200 --> 02:54:41,600
that copy does plus it copies the
5112
02:54:41,600 --> 02:54:42,399
metadata
5113
02:54:42,399 --> 02:54:44,479
of that file including the files
5114
02:54:44,479 --> 02:54:46,479
creation and modification times
5115
02:54:46,479 --> 02:54:48,479
so depending on the project or the
5116
02:54:48,479 --> 02:54:49,760
program you're working with
5117
02:54:49,760 --> 02:54:52,479
you may need to use copy or copy too but
5118
02:54:52,479 --> 02:54:54,160
in this video we're going to be using
5119
02:54:54,160 --> 02:54:56,800
copy file just to copy the contents of a
5120
02:54:56,800 --> 02:54:57,439
file
5121
02:54:57,439 --> 02:55:00,080
now after you import the shuttle module
5122
02:55:00,080 --> 02:55:01,680
this is all you need to do to copy a
5123
02:55:01,680 --> 02:55:02,640
file
5124
02:55:02,640 --> 02:55:05,279
shuttle dot then use the appropriate
5125
02:55:05,279 --> 02:55:07,680
function that you need to copy your file
5126
02:55:07,680 --> 02:55:10,800
we're going to be using copy file copy
5127
02:55:10,800 --> 02:55:12,720
file and within this function there are
5128
02:55:12,720 --> 02:55:13,840
two arguments
5129
02:55:13,840 --> 02:55:17,439
a source and a destination so within my
5130
02:55:17,439 --> 02:55:19,359
project folder i have a plain
5131
02:55:19,359 --> 02:55:22,720
text file called test test.txt
5132
02:55:22,720 --> 02:55:25,920
all this says is yo this is some text
5133
02:55:25,920 --> 02:55:27,920
see ya what i would like to do is copy
5134
02:55:27,920 --> 02:55:29,439
this file because it's cool
5135
02:55:29,439 --> 02:55:32,000
now since this file is within my project
5136
02:55:32,000 --> 02:55:34,800
folder i only have to list the file name
5137
02:55:34,800 --> 02:55:36,800
otherwise if this is someplace else i
5138
02:55:36,800 --> 02:55:38,720
would list the file path here
5139
02:55:38,720 --> 02:55:42,560
so this file is test.txt
5140
02:55:42,560 --> 02:55:45,600
this is the source file and now i need a
5141
02:55:45,600 --> 02:55:46,640
destination
5142
02:55:46,640 --> 02:55:48,720
so the second argument will be separated
5143
02:55:48,720 --> 02:55:50,560
with the comma i'm going to list
5144
02:55:50,560 --> 02:55:53,200
a destination so you can actually rename
5145
02:55:53,200 --> 02:55:53,840
this to
5146
02:55:53,840 --> 02:55:57,040
i would like to name this as copy.txt
5147
02:55:57,040 --> 02:56:00,399
so the destination is my project folder
5148
02:56:00,399 --> 02:56:02,920
so when i create a copy of this file
5149
02:56:02,920 --> 02:56:04,160
test.txt
5150
02:56:04,160 --> 02:56:06,080
this copy will appear in my project
5151
02:56:06,080 --> 02:56:07,680
folder and say the same thing
5152
02:56:07,680 --> 02:56:11,040
so after running this we now have a
5153
02:56:11,040 --> 02:56:13,680
file named copy.txt that says the same
5154
02:56:13,680 --> 02:56:14,080
thing
5155
02:56:14,080 --> 02:56:16,640
as my test file and remember what i said
5156
02:56:16,640 --> 02:56:17,760
you can copy
5157
02:56:17,760 --> 02:56:19,920
this file to a different location on
5158
02:56:19,920 --> 02:56:22,000
your computer so you could list the file
5159
02:56:22,000 --> 02:56:22,560
path
5160
02:56:22,560 --> 02:56:25,359
so this file path would go to my desktop
5161
02:56:25,359 --> 02:56:26,399
and then when i run this
5162
02:56:26,399 --> 02:56:28,479
this will copy my original file my
5163
02:56:28,479 --> 02:56:30,640
source and then copy it to
5164
02:56:30,640 --> 02:56:32,800
wherever my destination is oh and i
5165
02:56:32,800 --> 02:56:34,800
almost forgot so if you need to use copy
5166
02:56:34,800 --> 02:56:35,920
or copy 2
5167
02:56:35,920 --> 02:56:38,479
the arguments are exactly the same so if
5168
02:56:38,479 --> 02:56:40,080
you needed to use copy
5169
02:56:40,080 --> 02:56:42,399
this would be a shuttle dot copy instead
5170
02:56:42,399 --> 02:56:43,439
of copy file
5171
02:56:43,439 --> 02:56:46,640
then copy 2 is shuttle dot copy 2.
5172
02:56:46,640 --> 02:56:48,880
the arguments are exactly the same but
5173
02:56:48,880 --> 02:56:50,720
each of these functions will copy
5174
02:56:50,720 --> 02:56:52,240
different things depending on what you
5175
02:56:52,240 --> 02:56:54,640
need for your own program or project
5176
02:56:54,640 --> 02:56:56,720
all right everybody so that's the basics
5177
02:56:56,720 --> 02:56:58,160
of copying files and
5178
02:56:58,160 --> 02:57:00,399
python i will post this in the comment
5179
02:57:00,399 --> 02:57:01,520
section down below
5180
02:57:01,520 --> 02:57:04,160
but yeah that is how to copy files using
5181
02:57:04,160 --> 02:57:05,760
python
5182
02:57:05,760 --> 02:57:08,080
hey what's going on everybody it's bro
5183
02:57:08,080 --> 02:57:09,840
hope you're doing well and in this video
5184
02:57:09,840 --> 02:57:11,520
i'm going to explain how we can move
5185
02:57:11,520 --> 02:57:15,120
files using python so sit back relax
5186
02:57:15,120 --> 02:57:18,080
and enjoy the show
5187
02:57:18,160 --> 02:57:20,479
welcome back everybody to another video
5188
02:57:20,479 --> 02:57:22,160
regarding that thing that we do called
5189
02:57:22,160 --> 02:57:23,920
programming and in this video we're
5190
02:57:23,920 --> 02:57:26,319
going to be moving files using python
5191
02:57:26,319 --> 02:57:28,319
and in order to do so i recommend
5192
02:57:28,319 --> 02:57:30,399
importing the os module
5193
02:57:30,399 --> 02:57:32,000
this is included with the standard
5194
02:57:32,000 --> 02:57:34,319
python library all you need to do is
5195
02:57:34,319 --> 02:57:35,040
import it
5196
02:57:35,040 --> 02:57:37,600
and now let's create two variables a
5197
02:57:37,600 --> 02:57:38,880
variable called source
5198
02:57:38,880 --> 02:57:41,120
that will hold the location of where our
5199
02:57:41,120 --> 02:57:42,880
source file is located
5200
02:57:42,880 --> 02:57:45,760
as well as a variable called destination
5201
02:57:45,760 --> 02:57:46,880
to function as well
5202
02:57:46,880 --> 02:57:49,760
the destination now for this video i'm
5203
02:57:49,760 --> 02:57:50,000
going
5204
02:57:50,000 --> 02:57:52,720
to move a file within my project folder
5205
02:57:52,720 --> 02:57:53,920
to my desktop
5206
02:57:53,920 --> 02:57:56,640
so to do so i'm going to create a new
5207
02:57:56,640 --> 02:57:57,359
file
5208
02:57:57,359 --> 02:58:00,160
i will name this test.txt but take the
5209
02:58:00,160 --> 02:58:02,399
liberty to name this whatever you want
5210
02:58:02,399 --> 02:58:05,120
so i'm going to list the location of
5211
02:58:05,120 --> 02:58:05,600
where
5212
02:58:05,600 --> 02:58:08,160
my test file is located since this is
5213
02:58:08,160 --> 02:58:10,160
within my project folder i only need
5214
02:58:10,160 --> 02:58:12,399
the file name if this was someplace else
5215
02:58:12,399 --> 02:58:14,080
on your computer you would probably need
5216
02:58:14,080 --> 02:58:15,200
the file path
5217
02:58:15,200 --> 02:58:16,960
and now where would i like to move this
5218
02:58:16,960 --> 02:58:19,279
file to i would like to move this file
5219
02:58:19,279 --> 02:58:20,399
to my desktop
5220
02:58:20,399 --> 02:58:22,479
so i just need the file path so i'm
5221
02:58:22,479 --> 02:58:24,560
going to go to my desktop
5222
02:58:24,560 --> 02:58:27,120
right click go to properties i'm going
5223
02:58:27,120 --> 02:58:28,080
to copy
5224
02:58:28,080 --> 02:58:30,560
this location and paste it within my
5225
02:58:30,560 --> 02:58:31,760
destination
5226
02:58:31,760 --> 02:58:34,960
and i will add slash desktop
5227
02:58:34,960 --> 02:58:38,319
slash test.txt you can also rename this
5228
02:58:38,319 --> 02:58:40,000
too if you want i will just keep it the
5229
02:58:40,000 --> 02:58:40,560
same
5230
02:58:40,560 --> 02:58:43,040
now if you have single backslashes
5231
02:58:43,040 --> 02:58:45,040
you'll likely need to change these
5232
02:58:45,040 --> 02:58:47,920
to double backslashes because that is
5233
02:58:47,920 --> 02:58:49,279
the escape sequence
5234
02:58:49,279 --> 02:58:52,080
to print a backslash within a string so
5235
02:58:52,080 --> 02:58:52,880
we have a
5236
02:58:52,880 --> 02:58:55,359
source location as well as a destination
5237
02:58:55,359 --> 02:58:56,080
location
5238
02:58:56,080 --> 02:58:58,399
now we can begin i would recommend
5239
02:58:58,399 --> 02:59:00,880
writing our code within a try and accept
5240
02:59:00,880 --> 02:59:01,680
block to
5241
02:59:01,680 --> 02:59:03,920
handle any exceptions so i would like to
5242
02:59:03,920 --> 02:59:05,120
handle any file
5243
02:59:05,120 --> 02:59:08,000
not found error exceptions just in case
5244
02:59:08,000 --> 02:59:10,080
our program cannot locate our source
5245
02:59:10,080 --> 02:59:12,880
file so let's print a message if that is
5246
02:59:12,880 --> 02:59:13,760
the case
5247
02:59:13,760 --> 02:59:17,600
perhaps source plus was not
5248
02:59:17,600 --> 02:59:20,080
found now within our try block let's
5249
02:59:20,080 --> 02:59:22,319
check to see if there is already a file
5250
02:59:22,319 --> 02:59:23,600
at this destination
5251
02:59:23,600 --> 02:59:25,680
because i would not like to save over it
5252
02:59:25,680 --> 02:59:27,600
if there's already a file there
5253
02:59:27,600 --> 02:59:30,800
so let's do some basic file detection if
5254
02:59:30,800 --> 02:59:31,920
os
5255
02:59:31,920 --> 02:59:35,200
dot path dot exists
5256
02:59:35,200 --> 02:59:37,760
and i will pass in my destination as an
5257
02:59:37,760 --> 02:59:39,200
argument to the exists
5258
02:59:39,200 --> 02:59:42,160
function so if there is already a file
5259
02:59:42,160 --> 02:59:44,479
here i should probably let the user know
5260
02:59:44,479 --> 02:59:47,760
print there is already a
5261
02:59:47,760 --> 02:59:51,120
file there else
5262
02:59:51,120 --> 02:59:53,600
we can replace our files honestly if you
5263
02:59:53,600 --> 02:59:56,080
do not care about saving over any files
5264
02:59:56,080 --> 02:59:59,840
you can omit this part so else os
5265
02:59:59,840 --> 03:00:02,880
dot replace and we will pass
5266
03:00:02,880 --> 03:00:06,160
in our source and our destination
5267
03:00:06,160 --> 03:00:09,439
as arguments and let's print a message
5268
03:00:09,439 --> 03:00:11,439
to the console window to let the user
5269
03:00:11,439 --> 03:00:11,760
know
5270
03:00:11,760 --> 03:00:16,319
that source plus was
5271
03:00:16,319 --> 03:00:19,200
moved all right let's try this oh i
5272
03:00:19,200 --> 03:00:21,120
accidentally deleted my
5273
03:00:21,120 --> 03:00:24,240
file test.txt all right
5274
03:00:24,240 --> 03:00:27,279
now this should disappear in three
5275
03:00:27,279 --> 03:00:31,000
two one and it's gone
5276
03:00:31,000 --> 03:00:33,840
test.txt was moved and it should now be
5277
03:00:33,840 --> 03:00:35,040
on my desktop
5278
03:00:35,040 --> 03:00:37,600
which it is now you can also use this to
5279
03:00:37,600 --> 03:00:39,120
move a directory as well
5280
03:00:39,120 --> 03:00:42,160
i'm going to create a new directory i
5281
03:00:42,160 --> 03:00:44,160
will name this folder
5282
03:00:44,160 --> 03:00:47,200
and i will change source to folder
5283
03:00:47,200 --> 03:00:50,840
and the destination from test.txt to
5284
03:00:50,840 --> 03:00:53,520
folder and now this should move my
5285
03:00:53,520 --> 03:00:55,600
folder
5286
03:00:55,600 --> 03:00:58,960
there is already a file there
5287
03:00:59,680 --> 03:01:02,880
oh let's delete that okay
5288
03:01:02,880 --> 03:01:05,439
folder was moved all right everybody so
5289
03:01:05,439 --> 03:01:06,000
that is how
5290
03:01:06,000 --> 03:01:08,880
to move a file and or directory using
5291
03:01:08,880 --> 03:01:09,520
python
5292
03:01:09,520 --> 03:01:11,920
if you would like a copy of this code i
5293
03:01:11,920 --> 03:01:13,600
will post this in the comment section
5294
03:01:13,600 --> 03:01:14,640
down below
5295
03:01:14,640 --> 03:01:17,279
but yeah that is how to move a file and
5296
03:01:17,279 --> 03:01:18,240
or directory
5297
03:01:18,240 --> 03:01:21,279
using python hey what's going on
5298
03:01:21,279 --> 03:01:21,920
everybody
5299
03:01:21,920 --> 03:01:23,920
it's bro hope you're doing well and in
5300
03:01:23,920 --> 03:01:25,279
this video i'm going to teach you guys
5301
03:01:25,279 --> 03:01:27,760
how we can delete files using python so
5302
03:01:27,760 --> 03:01:32,399
sit back relax and enjoy the show
5303
03:01:32,720 --> 03:01:35,120
all right everybody so in this video
5304
03:01:35,120 --> 03:01:37,040
we're going to be deleting files using
5305
03:01:37,040 --> 03:01:39,359
python and in order to do so i recommend
5306
03:01:39,359 --> 03:01:41,359
importing the os module
5307
03:01:41,359 --> 03:01:42,800
this is already included with the
5308
03:01:42,800 --> 03:01:44,800
standard python library all you need to
5309
03:01:44,800 --> 03:01:45,120
do
5310
03:01:45,120 --> 03:01:47,600
is import it and now we need a file to
5311
03:01:47,600 --> 03:01:48,240
work with
5312
03:01:48,240 --> 03:01:49,760
so let's right click on our projects
5313
03:01:49,760 --> 03:01:51,359
folder and go to new
5314
03:01:51,359 --> 03:01:54,399
file i will name this test.txt
5315
03:01:54,399 --> 03:01:56,240
and we now have a file that we can work
5316
03:01:56,240 --> 03:01:58,000
with but you know what i would like to
5317
03:01:58,000 --> 03:02:00,080
delete this file because it's lame
5318
03:02:00,080 --> 03:02:02,160
so let's do that in order to delete this
5319
03:02:02,160 --> 03:02:03,680
file let's type os
5320
03:02:03,680 --> 03:02:05,840
dot remove and we're going to list the
5321
03:02:05,840 --> 03:02:07,120
file path here
5322
03:02:07,120 --> 03:02:09,279
since this file is within my project
5323
03:02:09,279 --> 03:02:10,960
folder i only need to list
5324
03:02:10,960 --> 03:02:14,319
the file path test.txt if this was
5325
03:02:14,319 --> 03:02:16,160
someplace else on my computer i would
5326
03:02:16,160 --> 03:02:17,680
probably need the file path
5327
03:02:17,680 --> 03:02:19,920
like that but since this file is within
5328
03:02:19,920 --> 03:02:21,680
my project folder i only need
5329
03:02:21,680 --> 03:02:23,680
the file name another thing that you can
5330
03:02:23,680 --> 03:02:25,680
do too at least what i like to do
5331
03:02:25,680 --> 03:02:27,600
is that i like to pass in a variable
5332
03:02:27,600 --> 03:02:29,920
called path i will make this a variable
5333
03:02:29,920 --> 03:02:31,200
and assign it a
5334
03:02:31,200 --> 03:02:33,920
string value of whatever the path to my
5335
03:02:33,920 --> 03:02:34,960
file is
5336
03:02:34,960 --> 03:02:37,359
so i will assign path the name to my
5337
03:02:37,359 --> 03:02:38,200
file
5338
03:02:38,200 --> 03:02:40,960
test.txt now when i run this this will
5339
03:02:40,960 --> 03:02:42,200
delete my file
5340
03:02:42,200 --> 03:02:44,399
test.txt and you can see that it's no
5341
03:02:44,399 --> 03:02:45,359
longer there
5342
03:02:45,359 --> 03:02:47,760
now what if we attempt to delete a file
5343
03:02:47,760 --> 03:02:49,120
that doesn't exist
5344
03:02:49,120 --> 03:02:51,600
well we'll encounter an exception so if
5345
03:02:51,600 --> 03:02:52,160
you want
5346
03:02:52,160 --> 03:02:53,439
this is optional we can do some
5347
03:02:53,439 --> 03:02:56,160
exception handling so let's write
5348
03:02:56,160 --> 03:02:58,640
the remove function within a try block
5349
03:02:58,640 --> 03:02:59,600
and we will catch
5350
03:02:59,600 --> 03:03:02,479
this exception file not found error and
5351
03:03:02,479 --> 03:03:04,080
let's print something
5352
03:03:04,080 --> 03:03:08,080
print that file was not found
5353
03:03:08,080 --> 03:03:10,479
now if you attempt to delete a file that
5354
03:03:10,479 --> 03:03:11,439
doesn't exist
5355
03:03:11,439 --> 03:03:13,680
your program will not be interrupted and
5356
03:03:13,680 --> 03:03:15,279
instead you'll print something
5357
03:03:15,279 --> 03:03:18,000
that file was not found now this
5358
03:03:18,000 --> 03:03:18,560
function
5359
03:03:18,560 --> 03:03:21,120
does not remove empty folders this is
5360
03:03:21,120 --> 03:03:22,080
how to do so
5361
03:03:22,080 --> 03:03:23,920
and actually let's test that so let's
5362
03:03:23,920 --> 03:03:25,359
create a new directory
5363
03:03:25,359 --> 03:03:29,520
i'll call this empty underscore folder
5364
03:03:29,520 --> 03:03:31,680
now i will attempt to delete this empty
5365
03:03:31,680 --> 03:03:32,640
folder
5366
03:03:32,640 --> 03:03:35,920
empty folder
5367
03:03:36,240 --> 03:03:38,560
we encountered a permission error access
5368
03:03:38,560 --> 03:03:40,160
is denied so let's create an accept
5369
03:03:40,160 --> 03:03:41,439
block for that
5370
03:03:41,439 --> 03:03:44,560
accept permission error
5371
03:03:44,560 --> 03:03:48,000
print i don't know you do
5372
03:03:48,000 --> 03:03:51,359
not have permission to
5373
03:03:51,359 --> 03:03:55,200
delete that right to delete
5374
03:03:55,200 --> 03:03:57,200
an empty folder there is a different
5375
03:03:57,200 --> 03:03:58,800
function
5376
03:03:58,800 --> 03:04:02,160
os dot rmdir
5377
03:04:02,160 --> 03:04:04,080
short for remove directory we're going
5378
03:04:04,080 --> 03:04:05,200
to list the file path
5379
03:04:05,200 --> 03:04:07,920
and pass that in as an argument and
5380
03:04:07,920 --> 03:04:09,200
let's create an else block as well
5381
03:04:09,200 --> 03:04:10,479
because we can
5382
03:04:10,479 --> 03:04:12,800
else if there are no exceptions let's
5383
03:04:12,800 --> 03:04:14,160
print
5384
03:04:14,160 --> 03:04:17,279
path plus was
5385
03:04:17,279 --> 03:04:20,960
deleted all right so rmdir
5386
03:04:20,960 --> 03:04:23,279
remove directory will delete an empty
5387
03:04:23,279 --> 03:04:24,640
directory
5388
03:04:24,640 --> 03:04:27,279
empty folder was deleted now this
5389
03:04:27,279 --> 03:04:27,840
function
5390
03:04:27,840 --> 03:04:30,080
will not delete a folder that contains
5391
03:04:30,080 --> 03:04:31,760
files and let's try that
5392
03:04:31,760 --> 03:04:34,080
so let's create a new directory i'll
5393
03:04:34,080 --> 03:04:36,720
just call this folder
5394
03:04:36,720 --> 03:04:38,640
and within this folder i will create a
5395
03:04:38,640 --> 03:04:40,439
new file
5396
03:04:40,439 --> 03:04:43,040
test.txt
5397
03:04:43,040 --> 03:04:45,120
all right let's attempt to delete this
5398
03:04:45,120 --> 03:04:46,960
folder i will probably need to change
5399
03:04:46,960 --> 03:04:48,000
the file name
5400
03:04:48,000 --> 03:04:51,279
let's change that to folder and we
5401
03:04:51,279 --> 03:04:52,160
encountered an
5402
03:04:52,160 --> 03:04:55,439
os error that directory is not empty so
5403
03:04:55,439 --> 03:04:56,960
let's handle this exception we don't
5404
03:04:56,960 --> 03:04:58,640
need to but i like to
5405
03:04:58,640 --> 03:05:03,439
accept os air print
5406
03:05:03,439 --> 03:05:06,640
you cannot delete that
5407
03:05:06,640 --> 03:05:10,479
using that function
5408
03:05:10,479 --> 03:05:13,200
okay to delete a folder that contains
5409
03:05:13,200 --> 03:05:13,920
files
5410
03:05:13,920 --> 03:05:17,359
we'll need the shuttle module import
5411
03:05:17,359 --> 03:05:19,600
shuttle and the function that we're
5412
03:05:19,600 --> 03:05:20,800
looking for is
5413
03:05:20,800 --> 03:05:24,000
shuttle dot rm tree
5414
03:05:24,000 --> 03:05:26,080
short for remove tree and we will pass
5415
03:05:26,080 --> 03:05:27,359
in our path
5416
03:05:27,359 --> 03:05:29,200
now be careful with this function it is
5417
03:05:29,200 --> 03:05:30,479
considered dangerous
5418
03:05:30,479 --> 03:05:32,560
because it will delete a directory and
5419
03:05:32,560 --> 03:05:34,560
all files contained within
5420
03:05:34,560 --> 03:05:36,720
so now we can delete a folder that
5421
03:05:36,720 --> 03:05:38,399
contains files
5422
03:05:38,399 --> 03:05:41,359
folder was deleted in conclusion these
5423
03:05:41,359 --> 03:05:41,760
are
5424
03:05:41,760 --> 03:05:44,720
three basic functions to delete a file
5425
03:05:44,720 --> 03:05:45,920
or directory they
5426
03:05:45,920 --> 03:05:49,840
are remove which deletes a file rmdir
5427
03:05:49,840 --> 03:05:52,000
which is an abbreviation for remove
5428
03:05:52,000 --> 03:05:52,880
directory
5429
03:05:52,880 --> 03:05:55,439
which will delete an empty directory and
5430
03:05:55,439 --> 03:05:56,160
lastly
5431
03:05:56,160 --> 03:05:58,800
rmtree which is an abbreviation for
5432
03:05:58,800 --> 03:05:59,760
remove tree
5433
03:05:59,760 --> 03:06:01,920
and this is of the shuttle module and
5434
03:06:01,920 --> 03:06:04,080
this will delete a directory containing
5435
03:06:04,080 --> 03:06:04,960
files
5436
03:06:04,960 --> 03:06:06,960
so if you would like a copy of all this
5437
03:06:06,960 --> 03:06:08,880
code i will post this in the comments
5438
03:06:08,880 --> 03:06:10,080
section down below
5439
03:06:10,080 --> 03:06:12,560
but yeah that is how to delete files and
5440
03:06:12,560 --> 03:06:13,600
or directories
5441
03:06:13,600 --> 03:06:16,800
using python yo what's going on
5442
03:06:16,800 --> 03:06:18,399
everybody it's you bro hope you're doing
5443
03:06:18,399 --> 03:06:18,800
well
5444
03:06:18,800 --> 03:06:20,319
and in today's video i'm going to be
5445
03:06:20,319 --> 03:06:22,800
discussing modules in python so
5446
03:06:22,800 --> 03:06:27,279
sit back relax and enjoy the show
5447
03:06:27,439 --> 03:06:29,680
all right well welcome back we're
5448
03:06:29,680 --> 03:06:31,279
talking about modules today
5449
03:06:31,279 --> 03:06:33,920
a module is a file containing python
5450
03:06:33,920 --> 03:06:34,399
code
5451
03:06:34,399 --> 03:06:37,439
it may contain functions classes etc
5452
03:06:37,439 --> 03:06:39,760
it's used with modular programming which
5453
03:06:39,760 --> 03:06:42,000
is this concept of separating a program
5454
03:06:42,000 --> 03:06:44,240
into useful different parts we're
5455
03:06:44,240 --> 03:06:46,560
currently working within our main module
5456
03:06:46,560 --> 03:06:48,880
and mine is named hello youtube this is
5457
03:06:48,880 --> 03:06:50,720
how to create a separate module
5458
03:06:50,720 --> 03:06:52,640
right click on your project folder go to
5459
03:06:52,640 --> 03:06:54,399
new python file
5460
03:06:54,399 --> 03:06:56,560
i think i'll create a module filled with
5461
03:06:56,560 --> 03:06:58,000
maybe message functions
5462
03:06:58,000 --> 03:07:00,479
so i'll call this messages and click
5463
03:07:00,479 --> 03:07:01,680
python file
5464
03:07:01,680 --> 03:07:03,600
all right we have two tabs one for our
5465
03:07:03,600 --> 03:07:05,600
main module mine is called hello youtube
5466
03:07:05,600 --> 03:07:06,880
and a separate module
5467
03:07:06,880 --> 03:07:09,040
for messages and i can write whatever i
5468
03:07:09,040 --> 03:07:10,560
want within here since we're using
5469
03:07:10,560 --> 03:07:12,880
modular programming i'll place a bunch
5470
03:07:12,880 --> 03:07:15,040
of useful functions within this module
5471
03:07:15,040 --> 03:07:17,040
and i'll import them to the main module
5472
03:07:17,040 --> 03:07:18,560
that we're currently working with
5473
03:07:18,560 --> 03:07:21,439
let's define a function called hello all
5474
03:07:21,439 --> 03:07:24,160
this will do is print
5475
03:07:24,160 --> 03:07:27,520
hello have a nice
5476
03:07:27,520 --> 03:07:30,000
day and i'll create a second function
5477
03:07:30,000 --> 03:07:30,800
for good measure
5478
03:07:30,800 --> 03:07:33,840
let's call this pie
5479
03:07:35,200 --> 03:07:38,880
let's say bye have
5480
03:07:38,880 --> 03:07:42,240
a wonderful time
5481
03:07:42,240 --> 03:07:44,000
now if we would like access to this
5482
03:07:44,000 --> 03:07:46,000
module we need to import it
5483
03:07:46,000 --> 03:07:47,840
i will head back to my main module and
5484
03:07:47,840 --> 03:07:49,600
at the top write this
5485
03:07:49,600 --> 03:07:52,080
import then the name of the module and
5486
03:07:52,080 --> 03:07:54,160
for this example we used the name of
5487
03:07:54,160 --> 03:07:54,800
messages
5488
03:07:54,800 --> 03:07:57,359
for our separate module and to use a
5489
03:07:57,359 --> 03:07:58,800
function from that module
5490
03:07:58,800 --> 03:08:01,439
type the name of the module dot and then
5491
03:08:01,439 --> 03:08:02,640
the name of the function
5492
03:08:02,640 --> 03:08:05,520
so messages.hello will call the hello
5493
03:08:05,520 --> 03:08:06,000
function
5494
03:08:06,000 --> 03:08:08,720
found within the messages module and i
5495
03:08:08,720 --> 03:08:09,920
can also call the
5496
03:08:09,920 --> 03:08:13,600
buy function as well messages dot bye
5497
03:08:13,600 --> 03:08:16,080
and that will print bye have a wonderful
5498
03:08:16,080 --> 03:08:16,720
time
5499
03:08:16,720 --> 03:08:18,560
no it can be somewhat tedious to have to
5500
03:08:18,560 --> 03:08:20,479
write the name the module followed by
5501
03:08:20,479 --> 03:08:22,080
the function name what we could do to
5502
03:08:22,080 --> 03:08:22,960
shorten things up
5503
03:08:22,960 --> 03:08:25,680
is to give our messages module an alias
5504
03:08:25,680 --> 03:08:27,200
also known as a nickname
5505
03:08:27,200 --> 03:08:30,399
so at the top write import messages
5506
03:08:30,399 --> 03:08:32,640
as and what sort of nickname should we
5507
03:08:32,640 --> 03:08:33,680
give messages
5508
03:08:33,680 --> 03:08:37,120
let's say msg for short so we'll replace
5509
03:08:37,120 --> 03:08:38,319
messages with
5510
03:08:38,319 --> 03:08:41,520
msg msg hello and msg buy
5511
03:08:41,520 --> 03:08:42,960
and this would work the same and it
5512
03:08:42,960 --> 03:08:45,200
involves less typing there's also an
5513
03:08:45,200 --> 03:08:46,240
additional way of
5514
03:08:46,240 --> 03:08:47,920
importing a module and it goes a little
5515
03:08:47,920 --> 03:08:49,920
something like this from
5516
03:08:49,920 --> 03:08:52,479
the name of the module import and then
5517
03:08:52,479 --> 03:08:54,319
list all of the functions or classes you
5518
03:08:54,319 --> 03:08:55,359
would like to import
5519
03:08:55,359 --> 03:08:58,399
i would like to import hello comma buy
5520
03:08:58,399 --> 03:09:00,319
and we no longer need the module name
5521
03:09:00,319 --> 03:09:02,160
before calling one of these functions
5522
03:09:02,160 --> 03:09:04,160
i can simply just call hello and vi
5523
03:09:04,160 --> 03:09:06,000
directly another way in which this could
5524
03:09:06,000 --> 03:09:06,800
be written is
5525
03:09:06,800 --> 03:09:09,600
from messages import asterisk that means
5526
03:09:09,600 --> 03:09:10,000
to
5527
03:09:10,000 --> 03:09:11,680
import all and i actually would not
5528
03:09:11,680 --> 03:09:13,359
recommend using this if you're working
5529
03:09:13,359 --> 03:09:15,520
on a large program or something that
5530
03:09:15,520 --> 03:09:16,000
contains
5531
03:09:16,000 --> 03:09:18,240
many modules because you can run into a
5532
03:09:18,240 --> 03:09:19,520
naming conflict
5533
03:09:19,520 --> 03:09:21,279
because some of these modules may have
5534
03:09:21,279 --> 03:09:22,960
the same named function names or
5535
03:09:22,960 --> 03:09:24,000
variables
5536
03:09:24,000 --> 03:09:25,520
so if you're working on a small program
5537
03:09:25,520 --> 03:09:27,279
i really don't think it's a big deal but
5538
03:09:27,279 --> 03:09:28,800
if you have multiple modules i would
5539
03:09:28,800 --> 03:09:31,200
actually refrain from using import alt
5540
03:09:31,200 --> 03:09:33,439
it's much safer to write the name of the
5541
03:09:33,439 --> 03:09:35,200
module followed by the name of the
5542
03:09:35,200 --> 03:09:36,560
function that you want to use
5543
03:09:36,560 --> 03:09:38,399
now another cool thing is that python
5544
03:09:38,399 --> 03:09:40,000
has a bunch of pre-written modules that
5545
03:09:40,000 --> 03:09:41,520
you have access to if you would like a
5546
03:09:41,520 --> 03:09:44,000
comprehensive list of them type help
5547
03:09:44,000 --> 03:09:46,720
and pass in the word modules and within
5548
03:09:46,720 --> 03:09:48,720
my console window this will populate a
5549
03:09:48,720 --> 03:09:50,399
listing of all the modules available to
5550
03:09:50,399 --> 03:09:51,279
us
5551
03:09:51,279 --> 03:09:53,120
here they are let's see if math is in
5552
03:09:53,120 --> 03:09:54,560
here yep
5553
03:09:54,560 --> 03:09:56,880
there it is so yeah feel free to take a
5554
03:09:56,880 --> 03:09:58,160
look through some of these modules
5555
03:09:58,160 --> 03:09:59,920
because there are some fairly useful
5556
03:09:59,920 --> 03:10:01,680
tools within some of these modules
5557
03:10:01,680 --> 03:10:03,680
i suppose you can also go to python's
5558
03:10:03,680 --> 03:10:05,760
official documentation and search for
5559
03:10:05,760 --> 03:10:08,000
the python module index and here's a
5560
03:10:08,000 --> 03:10:09,520
listing of all the different modules too
5561
03:10:09,520 --> 03:10:10,880
they have access to
5562
03:10:10,880 --> 03:10:13,279
well everyone those are modules they are
5563
03:10:13,279 --> 03:10:15,040
files containing python code and they
5564
03:10:15,040 --> 03:10:17,680
may contain functions classes etc
5565
03:10:17,680 --> 03:10:18,960
if you would like a copy of all this
5566
03:10:18,960 --> 03:10:20,560
code i will post all of this to the
5567
03:10:20,560 --> 03:10:22,000
comments section down below
5568
03:10:22,000 --> 03:10:25,040
but yeah those are modules in python i
5569
03:10:25,040 --> 03:10:26,720
guess there's a module called pickle
5570
03:10:26,720 --> 03:10:28,960
hey what's going on everybody it's bro
5571
03:10:28,960 --> 03:10:29,920
hope you're doing well
5572
03:10:29,920 --> 03:10:31,120
and in this video we're going to be
5573
03:10:31,120 --> 03:10:33,120
creating a basic game of rock paper
5574
03:10:33,120 --> 03:10:34,720
scissors in python so
5575
03:10:34,720 --> 03:10:39,279
sit back relax and enjoy the show
5576
03:10:39,520 --> 03:10:42,000
let's begin by importing the random
5577
03:10:42,000 --> 03:10:42,720
module
5578
03:10:42,720 --> 03:10:44,080
because we're going to need the computer
5579
03:10:44,080 --> 03:10:46,560
to pick a random choice and we'll create
5580
03:10:46,560 --> 03:10:46,880
a
5581
03:10:46,880 --> 03:10:49,760
list of possible choices so we have
5582
03:10:49,760 --> 03:10:51,359
three strings within here
5583
03:10:51,359 --> 03:10:56,399
rock paper and scissors
5584
03:10:57,600 --> 03:11:00,000
i think i spelled scissors wrong there
5585
03:11:00,000 --> 03:11:01,359
we go
5586
03:11:01,359 --> 03:11:04,080
okay and we will have our computer pick
5587
03:11:04,080 --> 03:11:04,479
a
5588
03:11:04,479 --> 03:11:08,640
random dot choice from our list
5589
03:11:08,640 --> 03:11:12,800
of choices and let's test it by printing
5590
03:11:12,800 --> 03:11:16,319
whatever the computer picks
5591
03:11:16,640 --> 03:11:19,439
so the computer picked paper paper
5592
03:11:19,439 --> 03:11:20,239
scissors
5593
03:11:20,239 --> 03:11:23,040
and scissors okay what about the player
5594
03:11:23,040 --> 03:11:23,760
choice
5595
03:11:23,760 --> 03:11:27,200
let's say player equals
5596
03:11:27,200 --> 03:11:31,200
input rock
5597
03:11:31,200 --> 03:11:34,640
paper or
5598
03:11:34,840 --> 03:11:37,840
scissors
5599
03:11:38,160 --> 03:11:40,880
and let's print what the player has
5600
03:11:40,880 --> 03:11:41,600
print
5601
03:11:41,600 --> 03:11:46,880
player and let's print the computer too
5602
03:11:47,359 --> 03:11:49,359
maybe i'll add a string before these so
5603
03:11:49,359 --> 03:11:50,960
let's say
5604
03:11:50,960 --> 03:11:54,880
computer call in space
5605
03:11:54,880 --> 03:11:57,359
whatever the computer chooses and same
5606
03:11:57,359 --> 03:11:59,359
thing with the player
5607
03:11:59,359 --> 03:12:02,960
player all right
5608
03:12:02,960 --> 03:12:06,000
let's see if this works rock paper or
5609
03:12:06,000 --> 03:12:07,600
scissors let's say rock
5610
03:12:07,600 --> 03:12:10,160
the computer picks paper and i pick rock
5611
03:12:10,160 --> 03:12:12,720
so i would technically lose this round
5612
03:12:12,720 --> 03:12:14,479
now what if the player doesn't pick rock
5613
03:12:14,479 --> 03:12:16,720
paper or scissors what if they type in
5614
03:12:16,720 --> 03:12:19,120
gun so we would like some way to prevent
5615
03:12:19,120 --> 03:12:19,760
that
5616
03:12:19,760 --> 03:12:22,080
so let's put our input for our player
5617
03:12:22,080 --> 03:12:24,080
within a while loop
5618
03:12:24,080 --> 03:12:27,359
while player
5619
03:12:27,359 --> 03:12:32,319
not in choices
5620
03:12:32,319 --> 03:12:34,319
so we're going to need to initialize
5621
03:12:34,319 --> 03:12:36,239
player because right now it's an
5622
03:12:36,239 --> 03:12:38,880
unresolved reference
5623
03:12:38,880 --> 03:12:42,479
so let's say player equals none
5624
03:12:42,479 --> 03:12:45,359
for the time being okay now let's test
5625
03:12:45,359 --> 03:12:46,160
our choices
5626
03:12:46,160 --> 03:12:48,479
our player choices rock paper or
5627
03:12:48,479 --> 03:12:49,600
scissors i pick
5628
03:12:49,600 --> 03:12:52,080
the gun and it's going to keep on asking
5629
03:12:52,080 --> 03:12:53,040
me until i pick
5630
03:12:53,040 --> 03:12:56,239
something from my list of choices so
5631
03:12:56,239 --> 03:12:58,479
if i type in that still won't accept it
5632
03:12:58,479 --> 03:12:59,680
now let's type in
5633
03:12:59,680 --> 03:13:02,800
rock again there we go now what if
5634
03:13:02,800 --> 03:13:04,560
somebody types in one of these choices
5635
03:13:04,560 --> 03:13:05,920
either all caps or
5636
03:13:05,920 --> 03:13:07,920
capitalized well that technically
5637
03:13:07,920 --> 03:13:09,600
wouldn't be in choices because
5638
03:13:09,600 --> 03:13:12,000
these strings are case sensitive so why
5639
03:13:12,000 --> 03:13:14,080
don't we take our user input and make it
5640
03:13:14,080 --> 03:13:14,960
lowercase
5641
03:13:14,960 --> 03:13:17,600
and i'll just add on dot lower and use
5642
03:13:17,600 --> 03:13:18,960
the lower method
5643
03:13:18,960 --> 03:13:22,080
so therefore we can now type in any
5644
03:13:22,080 --> 03:13:24,399
input that is either all caps or
5645
03:13:24,399 --> 03:13:25,520
uppercase
5646
03:13:25,520 --> 03:13:27,040
and then that will still be accepted
5647
03:13:27,040 --> 03:13:28,640
because we're going to take our player
5648
03:13:28,640 --> 03:13:29,200
input
5649
03:13:29,200 --> 03:13:30,880
and then make it lower case so that it
5650
03:13:30,880 --> 03:13:33,040
matches one of these choices
5651
03:13:33,040 --> 03:13:34,960
okay let's work on the win conditions
5652
03:13:34,960 --> 03:13:36,319
next so first
5653
03:13:36,319 --> 03:13:39,040
let's check to see if we encountered a
5654
03:13:39,040 --> 03:13:39,760
tie
5655
03:13:39,760 --> 03:13:42,479
if player is equal to computer that
5656
03:13:42,479 --> 03:13:43,279
means that
5657
03:13:43,279 --> 03:13:46,160
we both chose the same thing so let's
5658
03:13:46,160 --> 03:13:48,479
print whatever the computer picked
5659
03:13:48,479 --> 03:13:52,640
what we picked and we will print
5660
03:13:52,800 --> 03:13:56,000
tie now let's write an else
5661
03:13:56,000 --> 03:13:59,120
if statement e l i
5662
03:13:59,120 --> 03:14:03,279
f player equals
5663
03:14:03,279 --> 03:14:06,960
rock so if we pick rock and then we will
5664
03:14:06,960 --> 03:14:07,359
create
5665
03:14:07,359 --> 03:14:11,520
a nested if statement if computer
5666
03:14:11,520 --> 03:14:15,040
equals paper
5667
03:14:15,040 --> 03:14:18,080
that means we lose so let's print
5668
03:14:18,080 --> 03:14:20,800
everything we have here except we're
5669
03:14:20,800 --> 03:14:21,520
going to change
5670
03:14:21,520 --> 03:14:25,120
tie to you
5671
03:14:25,120 --> 03:14:29,120
lose now if the computer picks
5672
03:14:29,120 --> 03:14:33,680
let's say scissors then we win
5673
03:14:33,680 --> 03:14:37,279
so scissors and you
5674
03:14:37,279 --> 03:14:40,399
win okay then let's just repeat this
5675
03:14:40,399 --> 03:14:41,120
process
5676
03:14:41,120 --> 03:14:44,720
for scissors and paper
5677
03:14:45,840 --> 03:14:47,520
so this will be another else if
5678
03:14:47,520 --> 03:14:48,960
statement
5679
03:14:48,960 --> 03:14:53,120
if we pick
5680
03:14:53,120 --> 03:14:56,239
scissors and the computer picks
5681
03:14:56,239 --> 03:15:00,319
rock then we lose if the computer picks
5682
03:15:00,319 --> 03:15:04,560
paper then you win
5683
03:15:04,560 --> 03:15:07,359
and lastly we have paper as the player
5684
03:15:07,359 --> 03:15:09,680
choice
5685
03:15:12,479 --> 03:15:16,080
so else if player equals paper
5686
03:15:16,080 --> 03:15:20,399
and the computer picks scissors then we
5687
03:15:20,399 --> 03:15:21,040
lose
5688
03:15:21,040 --> 03:15:23,840
if the computer picks rock well then
5689
03:15:23,840 --> 03:15:25,040
paper covers rock
5690
03:15:25,040 --> 03:15:29,279
and we win now let's test it
5691
03:15:29,279 --> 03:15:32,080
rock paper or scissors again i'll try
5692
03:15:32,080 --> 03:15:33,120
and pick the gun
5693
03:15:33,120 --> 03:15:35,279
which i can't let's take this seriously
5694
03:15:35,279 --> 03:15:36,479
i will pick rock
5695
03:15:36,479 --> 03:15:40,239
the computer picked rock so it's a tie
5696
03:15:40,239 --> 03:15:43,040
i'm feeling like paper today oh it's
5697
03:15:43,040 --> 03:15:44,880
another tie i guess
5698
03:15:44,880 --> 03:15:48,000
and how about scissors okay
5699
03:15:48,000 --> 03:15:50,479
so i win this round i picked uh scissors
5700
03:15:50,479 --> 03:15:51,760
and the computer picked paper
5701
03:15:51,760 --> 03:15:54,160
therefore i win let's try and lose this
5702
03:15:54,160 --> 03:15:55,920
time
5703
03:15:55,920 --> 03:15:58,960
just to verify it so i won again let's
5704
03:15:58,960 --> 03:16:00,080
try rock again
5705
03:16:00,080 --> 03:16:02,080
okay so i lost that round so we know
5706
03:16:02,080 --> 03:16:04,560
that the win conditions are working now
5707
03:16:04,560 --> 03:16:06,479
how about we add a feature where we will
5708
03:16:06,479 --> 03:16:08,000
ask the player if they would like to
5709
03:16:08,000 --> 03:16:08,640
play again
5710
03:16:08,640 --> 03:16:10,720
play another round i think the best way
5711
03:16:10,720 --> 03:16:12,239
to do that would be to write
5712
03:16:12,239 --> 03:16:14,640
all of this code within a while loop so
5713
03:16:14,640 --> 03:16:15,279
at the top
5714
03:16:15,279 --> 03:16:18,880
let's write while true and we'll need to
5715
03:16:18,880 --> 03:16:20,720
indent everything so that it's within
5716
03:16:20,720 --> 03:16:22,800
the while loop so let's just indent
5717
03:16:22,800 --> 03:16:31,840
everything here
5718
03:16:39,680 --> 03:16:41,840
now at the bottom we will ask the user
5719
03:16:41,840 --> 03:16:43,439
if they would like to play again and i
5720
03:16:43,439 --> 03:16:45,600
will store this within a variable
5721
03:16:45,600 --> 03:16:49,439
named play again play again equals input
5722
03:16:49,439 --> 03:16:54,319
and the prompt will be play again
5723
03:16:54,319 --> 03:16:58,319
yes slash no and depending on their
5724
03:16:58,319 --> 03:16:58,880
input
5725
03:16:58,880 --> 03:17:02,399
let's just make it all lower case
5726
03:17:02,479 --> 03:17:06,160
if play again
5727
03:17:06,160 --> 03:17:09,279
does not equal yes
5728
03:17:09,279 --> 03:17:11,600
that means they would like to quit so
5729
03:17:11,600 --> 03:17:14,000
let's use a break statement to break out
5730
03:17:14,000 --> 03:17:17,200
of our while loop that we're in
5731
03:17:17,200 --> 03:17:20,640
and at the end let's print by
5732
03:17:20,640 --> 03:17:23,040
and pay attention to the indentation
5733
03:17:23,040 --> 03:17:24,160
this input and
5734
03:17:24,160 --> 03:17:25,680
this if statement should be within the
5735
03:17:25,680 --> 03:17:27,680
while loop so they should have at least
5736
03:17:27,680 --> 03:17:30,880
one indentation and our print statement
5737
03:17:30,880 --> 03:17:32,399
here we'll just say bye
5738
03:17:32,399 --> 03:17:33,600
and you can see that there is no
5739
03:17:33,600 --> 03:17:35,359
indentation so that is outside of the
5740
03:17:35,359 --> 03:17:36,239
while loop
5741
03:17:36,239 --> 03:17:40,000
okay let's test this one last time
5742
03:17:40,000 --> 03:17:42,560
rock paper scissors i'm going to pick
5743
03:17:42,560 --> 03:17:43,279
the
5744
03:17:43,279 --> 03:17:46,160
cannon okay that's not valid input how
5745
03:17:46,160 --> 03:17:47,600
about rock
5746
03:17:47,600 --> 03:17:49,920
okay the computer picks rock i pick rock
5747
03:17:49,920 --> 03:17:52,080
so it's a tie do i want to play again
5748
03:17:52,080 --> 03:17:54,560
i'm going to select actually yes all
5749
03:17:54,560 --> 03:17:55,520
caps
5750
03:17:55,520 --> 03:17:57,840
that should still be acceptable elbow
5751
03:17:57,840 --> 03:17:59,840
paper this time
5752
03:17:59,840 --> 03:18:01,760
it's a tie again man the computer is
5753
03:18:01,760 --> 03:18:02,960
really good at this game
5754
03:18:02,960 --> 03:18:04,479
i'd like to play again i'll pick
5755
03:18:04,479 --> 03:18:06,720
scissors
5756
03:18:06,720 --> 03:18:08,399
okay so i lose i would like to play
5757
03:18:08,399 --> 03:18:11,840
again and i want to win this time
5758
03:18:11,840 --> 03:18:14,160
okay computer pick scissors i pick rock
5759
03:18:14,160 --> 03:18:16,560
you win do you want to play again
5760
03:18:16,560 --> 03:18:19,680
nah and it will print
5761
03:18:19,680 --> 03:18:22,720
bye well everybody that's a simple game
5762
03:18:22,720 --> 03:18:24,720
of rock paper scissors i'll post all of
5763
03:18:24,720 --> 03:18:26,239
this code to the comment section down
5764
03:18:26,239 --> 03:18:26,880
below
5765
03:18:26,880 --> 03:18:29,120
and well yeah that's a basic game of
5766
03:18:29,120 --> 03:18:32,720
rock paper scissors in python
5767
03:18:32,720 --> 03:18:34,800
hey what's going on everybody it's you
5768
03:18:34,800 --> 03:18:36,239
bro hope you're doing well
5769
03:18:36,239 --> 03:18:37,600
and in this video i'm going to show you
5770
03:18:37,600 --> 03:18:39,760
guys how we can build a basic quiz game
5771
03:18:39,760 --> 03:18:40,640
in python
5772
03:18:40,640 --> 03:18:45,520
so sit back relax and enjoy the show
5773
03:18:45,520 --> 03:18:47,279
to begin this project i think it would
5774
03:18:47,279 --> 03:18:48,960
be wise for us to create a skeletal
5775
03:18:48,960 --> 03:18:50,640
structure for how this program is going
5776
03:18:50,640 --> 03:18:51,439
to operate
5777
03:18:51,439 --> 03:18:52,880
and then we'll fill in the gaps later as
5778
03:18:52,880 --> 03:18:54,640
we go along so to begin
5779
03:18:54,640 --> 03:18:56,319
let's define all of the functions that
5780
03:18:56,319 --> 03:18:58,000
we'll need we'll need four
5781
03:18:58,000 --> 03:19:00,319
we'll create a function that is named
5782
03:19:00,319 --> 03:19:01,040
new game
5783
03:19:01,040 --> 03:19:02,479
whenever we call this function it will
5784
03:19:02,479 --> 03:19:05,040
create a new quiz game for us
5785
03:19:05,040 --> 03:19:07,040
and for the time being let's write pass
5786
03:19:07,040 --> 03:19:08,399
just as a placeholder
5787
03:19:08,399 --> 03:19:10,720
we'll have a total of four functions new
5788
03:19:10,720 --> 03:19:12,560
game
5789
03:19:12,560 --> 03:19:16,319
a function to check our answer
5790
03:19:16,479 --> 03:19:20,239
a function to display our score
5791
03:19:21,760 --> 03:19:24,960
and lastly a function to
5792
03:19:24,960 --> 03:19:27,439
play again
5793
03:19:30,319 --> 03:19:33,600
all right now i'm just going to add some
5794
03:19:33,600 --> 03:19:34,399
comments here
5795
03:19:34,399 --> 03:19:36,399
just to separate each of these functions
5796
03:19:36,399 --> 03:19:37,840
that we have just so that it's a little
5797
03:19:37,840 --> 03:19:38,800
bit easier to read
5798
03:19:38,800 --> 03:19:40,160
although this part is not really
5799
03:19:40,160 --> 03:19:42,960
necessary so i just want to separate
5800
03:19:42,960 --> 03:19:44,840
each of these functions for some
5801
03:19:44,840 --> 03:19:47,680
readability
5802
03:19:47,680 --> 03:19:49,439
okay those are all the functions that
5803
03:19:49,439 --> 03:19:51,439
we'll need what we'll need now
5804
03:19:51,439 --> 03:19:53,520
is some sort of collection to hold all
5805
03:19:53,520 --> 03:19:55,279
of the questions and answers that we
5806
03:19:55,279 --> 03:19:57,279
have and i think a dictionary would be
5807
03:19:57,279 --> 03:19:58,399
perfect for this
5808
03:19:58,399 --> 03:20:00,239
so what i did is that i created a
5809
03:20:00,239 --> 03:20:02,080
dictionary named questions
5810
03:20:02,080 --> 03:20:05,120
a dictionary has key value pairs
5811
03:20:05,120 --> 03:20:07,120
each key is a question that i would like
5812
03:20:07,120 --> 03:20:08,880
to ask and each question
5813
03:20:08,880 --> 03:20:11,359
has an associated value we'll have the
5814
03:20:11,359 --> 03:20:12,560
user guess between
5815
03:20:12,560 --> 03:20:16,080
answers of a b c or d so these would be
5816
03:20:16,080 --> 03:20:17,840
all of the correct answers all of the
5817
03:20:17,840 --> 03:20:19,600
values within this dictionary
5818
03:20:19,600 --> 03:20:20,880
so here are some of the questions that i
5819
03:20:20,880 --> 03:20:22,319
want to ask feel free to come up with
5820
03:20:22,319 --> 03:20:23,520
your own questions
5821
03:20:23,520 --> 03:20:25,200
if you would like you can copy the
5822
03:20:25,200 --> 03:20:26,880
questions that i have they should be
5823
03:20:26,880 --> 03:20:28,000
posted in the comment section
5824
03:20:28,000 --> 03:20:30,000
down below so the questions that i'm
5825
03:20:30,000 --> 03:20:31,120
going to ask are
5826
03:20:31,120 --> 03:20:33,520
who created python what year was python
5827
03:20:33,520 --> 03:20:34,399
created
5828
03:20:34,399 --> 03:20:36,560
python is attributed to which comedy
5829
03:20:36,560 --> 03:20:38,960
group and lastly is the earth round
5830
03:20:38,960 --> 03:20:40,319
i couldn't think of a fourth question so
5831
03:20:40,319 --> 03:20:43,040
i just threw in a random stupid question
5832
03:20:43,040 --> 03:20:45,120
and the correct answers to all of these
5833
03:20:45,120 --> 03:20:46,800
questions in order would be
5834
03:20:46,800 --> 03:20:50,640
a b c and a but feel free to come up
5835
03:20:50,640 --> 03:20:51,279
with your own
5836
03:20:51,279 --> 03:20:53,040
well we have our questions but we'll
5837
03:20:53,040 --> 03:20:55,279
need some sort of collection to hold all
5838
03:20:55,279 --> 03:20:57,120
of the different possible answers
5839
03:20:57,120 --> 03:20:59,920
to each of these questions and i think a
5840
03:20:59,920 --> 03:21:02,479
2d list would work perfect for this
5841
03:21:02,479 --> 03:21:05,359
so i have a list of lists a list of
5842
03:21:05,359 --> 03:21:07,279
tuples could work too i suppose
5843
03:21:07,279 --> 03:21:09,760
so here are all of the answers for the
5844
03:21:09,760 --> 03:21:10,880
first question
5845
03:21:10,880 --> 03:21:13,520
and the correct answer is a i believe
5846
03:21:13,520 --> 03:21:14,399
his name is
5847
03:21:14,399 --> 03:21:16,800
guido van rossum hey if you're listening
5848
03:21:16,800 --> 03:21:18,319
man i'm sorry if i'm mispronouncing your
5849
03:21:18,319 --> 03:21:20,000
name i'm really trying here
5850
03:21:20,000 --> 03:21:21,920
the second list corresponds to the
5851
03:21:21,920 --> 03:21:23,040
second question
5852
03:21:23,040 --> 03:21:24,960
what year was python created it was
5853
03:21:24,960 --> 03:21:26,640
created in 1991
5854
03:21:26,640 --> 03:21:29,120
at least according to wikipedia python
5855
03:21:29,120 --> 03:21:31,040
is attributed to which comedy group
5856
03:21:31,040 --> 03:21:34,319
the answer is c monty python like monty
5857
03:21:34,319 --> 03:21:35,680
python and the holy grail
5858
03:21:35,680 --> 03:21:38,160
good movie by the way and lastly is the
5859
03:21:38,160 --> 03:21:39,120
earth round
5860
03:21:39,120 --> 03:21:41,760
this is a highly debated topic but the
5861
03:21:41,760 --> 03:21:42,960
answer is hey
5862
03:21:42,960 --> 03:21:45,680
the earth is in fact round so i have a
5863
03:21:45,680 --> 03:21:47,120
list of lists
5864
03:21:47,120 --> 03:21:49,920
each list corresponds to a key value
5865
03:21:49,920 --> 03:21:52,560
pair within my dictionary of questions
5866
03:21:52,560 --> 03:21:54,399
and this is a lot to type if you want
5867
03:21:54,399 --> 03:21:56,399
you can just copy the code that i posted
5868
03:21:56,399 --> 03:21:57,920
in the comments down below
5869
03:21:57,920 --> 03:21:59,600
just copy and paste and you'll have all
5870
03:21:59,600 --> 03:22:01,120
of this or if you want you can pause the
5871
03:22:01,120 --> 03:22:02,000
video and type it
5872
03:22:02,000 --> 03:22:04,080
i don't care so now that you have your
5873
03:22:04,080 --> 03:22:05,680
questions and your options we're ready
5874
03:22:05,680 --> 03:22:06,479
to begin
5875
03:22:06,479 --> 03:22:07,920
so the first thing that we'll do within
5876
03:22:07,920 --> 03:22:09,760
our program is to call the
5877
03:22:09,760 --> 03:22:12,399
new game function to begin a new game so
5878
03:22:12,399 --> 03:22:12,960
after
5879
03:22:12,960 --> 03:22:14,720
we have our questions and options
5880
03:22:14,720 --> 03:22:16,720
created let's create a
5881
03:22:16,720 --> 03:22:19,760
new game by calling the new game
5882
03:22:19,760 --> 03:22:20,640
function
5883
03:22:20,640 --> 03:22:22,399
now when we run this program we'll
5884
03:22:22,399 --> 03:22:24,479
generate our dictionary of questions
5885
03:22:24,479 --> 03:22:27,200
and our 2d list of different options for
5886
03:22:27,200 --> 03:22:28,239
each question
5887
03:22:28,239 --> 03:22:29,920
and then we will call the new game
5888
03:22:29,920 --> 03:22:31,600
function to begin a new game
5889
03:22:31,600 --> 03:22:33,520
so let's head to the new game function
5890
03:22:33,520 --> 03:22:35,680
and fill in everything within our new
5891
03:22:35,680 --> 03:22:36,640
game function
5892
03:22:36,640 --> 03:22:39,040
so at the top let's declare a few things
5893
03:22:39,040 --> 03:22:40,080
let's say we have
5894
03:22:40,080 --> 03:22:43,520
a list named guesses and guesses will be
5895
03:22:43,520 --> 03:22:45,520
an empty list for now
5896
03:22:45,520 --> 03:22:48,640
i will declare a variable called correct
5897
03:22:48,640 --> 03:22:51,200
guesses set it equal to zero because we
5898
03:22:51,200 --> 03:22:52,960
haven't guessed anything yet
5899
03:22:52,960 --> 03:22:55,760
and we will set a current question
5900
03:22:55,760 --> 03:22:56,479
number
5901
03:22:56,479 --> 03:22:58,720
and set the sequel to one to represent
5902
03:22:58,720 --> 03:23:00,560
the first question
5903
03:23:00,560 --> 03:23:03,600
okay now we need to display all of the
5904
03:23:03,600 --> 03:23:05,760
questions within our dictionary of
5905
03:23:05,760 --> 03:23:06,479
questions
5906
03:23:06,479 --> 03:23:09,120
and we can use a for loop for that so
5907
03:23:09,120 --> 03:23:10,080
for
5908
03:23:10,080 --> 03:23:14,880
key in questions
5909
03:23:14,880 --> 03:23:20,479
i'm going to print my key
5910
03:23:20,560 --> 03:23:23,520
and let's just test this okay yeah
5911
03:23:23,520 --> 03:23:25,439
here's all my questions
5912
03:23:25,439 --> 03:23:28,319
i think what i'll do is actually print
5913
03:23:28,319 --> 03:23:30,880
something to separate each question
5914
03:23:30,880 --> 03:23:34,160
let's say one of these lines i think it
5915
03:23:34,160 --> 03:23:36,160
would look better with them
5916
03:23:36,160 --> 03:23:38,880
okay i'll print this line and then we'll
5917
03:23:38,880 --> 03:23:40,399
move on to a question
5918
03:23:40,399 --> 03:23:42,239
all right now after re-running this we
5919
03:23:42,239 --> 03:23:44,560
have each question and i added just a
5920
03:23:44,560 --> 03:23:46,160
line break between each of these to make
5921
03:23:46,160 --> 03:23:47,600
it look kind of fancy
5922
03:23:47,600 --> 03:23:49,600
now i need to display all of the
5923
03:23:49,600 --> 03:23:51,760
different options for each question
5924
03:23:51,760 --> 03:23:54,080
and we can do that with a nested for
5925
03:23:54,080 --> 03:23:55,120
loop
5926
03:23:55,120 --> 03:23:59,359
so if i were to write i in
5927
03:23:59,359 --> 03:24:02,399
options print i
5928
03:24:02,399 --> 03:24:05,359
let me show you what happens now with
5929
03:24:05,359 --> 03:24:07,040
the way this for loop is written now
5930
03:24:07,040 --> 03:24:08,239
it's going to display
5931
03:24:08,239 --> 03:24:10,000
all of the different options available
5932
03:24:10,000 --> 03:24:12,080
to us for the entire quiz game
5933
03:24:12,080 --> 03:24:14,960
what i would like instead is to display
5934
03:24:14,960 --> 03:24:16,160
only the first
5935
03:24:16,160 --> 03:24:18,720
list for the first question and then the
5936
03:24:18,720 --> 03:24:19,600
second list
5937
03:24:19,600 --> 03:24:21,760
for the second question and follow that
5938
03:24:21,760 --> 03:24:23,760
pattern so we're going to change our for
5939
03:24:23,760 --> 03:24:24,640
loop around
5940
03:24:24,640 --> 03:24:27,920
4i in options and we'll set an index for
5941
03:24:27,920 --> 03:24:28,640
options
5942
03:24:28,640 --> 03:24:30,720
the index is going to be our question
5943
03:24:30,720 --> 03:24:31,840
number
5944
03:24:31,840 --> 03:24:34,560
minus one now remember what i said in
5945
03:24:34,560 --> 03:24:36,080
previous videos where different
5946
03:24:36,080 --> 03:24:37,359
collections including
5947
03:24:37,359 --> 03:24:40,640
lists tuples etc the first element in a
5948
03:24:40,640 --> 03:24:41,439
collection
5949
03:24:41,439 --> 03:24:43,600
has an index of zero because computers
5950
03:24:43,600 --> 03:24:44,960
always start with zero
5951
03:24:44,960 --> 03:24:46,880
then the next element would therefore
5952
03:24:46,880 --> 03:24:48,239
have an index of one
5953
03:24:48,239 --> 03:24:50,479
then two three then you follow that
5954
03:24:50,479 --> 03:24:52,800
pattern we're using this question number
5955
03:24:52,800 --> 03:24:53,600
variable
5956
03:24:53,600 --> 03:24:55,760
as some sort of counter and since we
5957
03:24:55,760 --> 03:24:57,279
initially set this to one
5958
03:24:57,279 --> 03:24:59,520
i'm just going to subtract one so that
5959
03:24:59,520 --> 03:25:00,399
we effectively
5960
03:25:00,399 --> 03:25:03,040
receive zero as the index and now we
5961
03:25:03,040 --> 03:25:04,960
just need to increment our question
5962
03:25:04,960 --> 03:25:05,520
number
5963
03:25:05,520 --> 03:25:08,960
after each iteration so let's do that
5964
03:25:08,960 --> 03:25:11,680
i will add that to the end here after we
5965
03:25:11,680 --> 03:25:13,680
finish displaying all of the options
5966
03:25:13,680 --> 03:25:16,800
let's increment question number by one
5967
03:25:16,800 --> 03:25:20,960
by typing question num plus equals one
5968
03:25:20,960 --> 03:25:23,040
and if i were to run this currently
5969
03:25:23,040 --> 03:25:24,640
we'll display all of the different
5970
03:25:24,640 --> 03:25:26,239
options for each question
5971
03:25:26,239 --> 03:25:28,479
who created python here's all of the
5972
03:25:28,479 --> 03:25:30,160
associated options
5973
03:25:30,160 --> 03:25:32,560
what year is python created python is
5974
03:25:32,560 --> 03:25:34,319
attributed to which comedy group
5975
03:25:34,319 --> 03:25:37,279
and lastly is the earth round now it's
5976
03:25:37,279 --> 03:25:38,880
time for some user input
5977
03:25:38,880 --> 03:25:41,680
so i'm going to create a variable called
5978
03:25:41,680 --> 03:25:42,720
guess
5979
03:25:42,720 --> 03:25:44,479
and make sure you don't put it within
5980
03:25:44,479 --> 03:25:46,479
your inner for loop it should be within
5981
03:25:46,479 --> 03:25:48,080
the outer for loop
5982
03:25:48,080 --> 03:25:51,600
guess equals input and i'll create a
5983
03:25:51,600 --> 03:25:53,120
prompt
5984
03:25:53,120 --> 03:25:56,880
enter a b
5985
03:25:56,880 --> 03:26:00,560
c or d
5986
03:26:00,560 --> 03:26:02,080
here's something to consider we would
5987
03:26:02,080 --> 03:26:04,319
like the user to type in either capital
5988
03:26:04,319 --> 03:26:04,800
a
5989
03:26:04,800 --> 03:26:07,840
b c or d with strings they're case
5990
03:26:07,840 --> 03:26:09,760
sensitive what if the user typed in
5991
03:26:09,760 --> 03:26:12,000
one of these letters but lowercase if
5992
03:26:12,000 --> 03:26:13,439
they're correct we would still like to
5993
03:26:13,439 --> 03:26:14,560
give them their point
5994
03:26:14,560 --> 03:26:16,560
why don't we take our guess and make it
5995
03:26:16,560 --> 03:26:17,600
uppercase
5996
03:26:17,600 --> 03:26:20,560
so guess equals guess and we can make
5997
03:26:20,560 --> 03:26:21,439
this uppercase
5998
03:26:21,439 --> 03:26:24,560
using the upper method of strings at the
5999
03:26:24,560 --> 03:26:26,800
end of this game i'm going to compare
6000
03:26:26,800 --> 03:26:29,680
our guesses to the correct answers so we
6001
03:26:29,680 --> 03:26:30,800
have an empty
6002
03:26:30,800 --> 03:26:33,040
list named guesses and i'm going to
6003
03:26:33,040 --> 03:26:34,800
append our current guest that we're
6004
03:26:34,800 --> 03:26:35,600
working on
6005
03:26:35,600 --> 03:26:39,520
to our list of guesses so guesses
6006
03:26:39,520 --> 03:26:42,800
dot append and we will append our
6007
03:26:42,800 --> 03:26:45,840
guess this round now that we have our
6008
03:26:45,840 --> 03:26:46,239
guess
6009
03:26:46,239 --> 03:26:48,560
and we've appended our guess to our list
6010
03:26:48,560 --> 03:26:49,520
of guesses
6011
03:26:49,520 --> 03:26:51,120
let's check to see if it's the correct
6012
03:26:51,120 --> 03:26:53,359
answer or not so we're just about to
6013
03:26:53,359 --> 03:26:53,920
fill in
6014
03:26:53,920 --> 03:26:57,760
this check answer function next
6015
03:26:57,760 --> 03:26:59,920
so we are going to call this function
6016
03:26:59,920 --> 03:27:02,720
and pass in a few items as arguments
6017
03:27:02,720 --> 03:27:07,200
so let's use the check answer function
6018
03:27:07,200 --> 03:27:10,080
and we'll pass in the key for the
6019
03:27:10,080 --> 03:27:12,000
current question that we're on
6020
03:27:12,000 --> 03:27:14,560
so the key is the correct answer so that
6021
03:27:14,560 --> 03:27:15,760
would be
6022
03:27:15,760 --> 03:27:18,880
our questions
6023
03:27:18,880 --> 03:27:23,040
dot get key
6024
03:27:23,040 --> 03:27:25,279
this would be the answer and we'll also
6025
03:27:25,279 --> 03:27:26,479
pass in
6026
03:27:26,479 --> 03:27:30,080
our guess and we are going to fill in
6027
03:27:30,080 --> 03:27:32,720
the check answer function but we need to
6028
03:27:32,720 --> 03:27:34,239
set up the parameters
6029
03:27:34,239 --> 03:27:37,520
we're receiving effectively our answer
6030
03:27:37,520 --> 03:27:41,200
as well as our guess so i'm going to
6031
03:27:41,200 --> 03:27:42,160
name these as
6032
03:27:42,160 --> 03:27:45,760
answer and guess and we are going to
6033
03:27:45,760 --> 03:27:46,880
check to see if
6034
03:27:46,880 --> 03:27:50,239
our answer is equal to our guess
6035
03:27:50,239 --> 03:27:53,439
if answer is equal
6036
03:27:53,439 --> 03:27:57,760
to guess let's print something
6037
03:27:57,760 --> 03:28:02,239
print correct
6038
03:28:03,200 --> 03:28:04,640
and i think we should give the user a
6039
03:28:04,640 --> 03:28:06,640
point we'll have our check answer
6040
03:28:06,640 --> 03:28:08,479
function return
6041
03:28:08,479 --> 03:28:12,319
one for one point return one
6042
03:28:12,319 --> 03:28:15,200
else if this is not the correct answer
6043
03:28:15,200 --> 03:28:17,840
let's print
6044
03:28:17,840 --> 03:28:20,960
wrong and we will return
6045
03:28:20,960 --> 03:28:24,080
zero they do not get a point
6046
03:28:24,080 --> 03:28:26,319
and since this is returning a value we
6047
03:28:26,319 --> 03:28:27,600
should assign that
6048
03:28:27,600 --> 03:28:30,399
so let's assign the point we may or may
6049
03:28:30,399 --> 03:28:31,200
not receive
6050
03:28:31,200 --> 03:28:33,600
to our variable of correct guesses which
6051
03:28:33,600 --> 03:28:35,439
is initially set to zero
6052
03:28:35,439 --> 03:28:38,640
so we'll type correct guesses plus
6053
03:28:38,640 --> 03:28:41,520
equals check answer and check answer
6054
03:28:41,520 --> 03:28:42,399
will return
6055
03:28:42,399 --> 03:28:44,800
one if we scored a point or zero if we
6056
03:28:44,800 --> 03:28:46,640
did not score a point and make sure you
6057
03:28:46,640 --> 03:28:47,040
have
6058
03:28:47,040 --> 03:28:49,120
plus equals because if you just set this
6059
03:28:49,120 --> 03:28:50,160
to equals
6060
03:28:50,160 --> 03:28:52,479
then we cannot score more than one point
6061
03:28:52,479 --> 03:28:53,600
it will either be
6062
03:28:53,600 --> 03:28:56,319
zero or one so we are effectively
6063
03:28:56,319 --> 03:28:57,600
incrementing our correct
6064
03:28:57,600 --> 03:29:00,000
guesses by one for each point that we
6065
03:29:00,000 --> 03:29:01,040
score
6066
03:29:01,040 --> 03:29:03,120
and now we're going to work on the
6067
03:29:03,120 --> 03:29:04,399
display score
6068
03:29:04,399 --> 03:29:06,319
function and we'll call that at the end
6069
03:29:06,319 --> 03:29:08,239
after we finish all of our questions
6070
03:29:08,239 --> 03:29:09,120
that we have
6071
03:29:09,120 --> 03:29:10,960
so make sure that you do not write this
6072
03:29:10,960 --> 03:29:13,040
within the for loop it should be outside
6073
03:29:13,040 --> 03:29:13,600
of it
6074
03:29:13,600 --> 03:29:16,000
because once we finish iterating through
6075
03:29:16,000 --> 03:29:17,680
all of our questions we're going to
6076
03:29:17,680 --> 03:29:19,760
display a final score
6077
03:29:19,760 --> 03:29:22,880
so let's call the display score
6078
03:29:22,880 --> 03:29:25,200
function and we'll need to pass in some
6079
03:29:25,200 --> 03:29:26,160
arguments
6080
03:29:26,160 --> 03:29:29,439
our correct guesses as well
6081
03:29:29,439 --> 03:29:33,600
as our list of guesses
6082
03:29:33,600 --> 03:29:36,080
okay let's head to the display score
6083
03:29:36,080 --> 03:29:37,200
function
6084
03:29:37,200 --> 03:29:40,319
so we have as parameters
6085
03:29:40,319 --> 03:29:45,520
correct guesses as well as
6086
03:29:45,520 --> 03:29:48,399
guesses i need to remove this pass i'm
6087
03:29:48,399 --> 03:29:49,120
going to add
6088
03:29:49,120 --> 03:29:51,200
one of these fancy lines in just to
6089
03:29:51,200 --> 03:29:52,640
separate the questions
6090
03:29:52,640 --> 03:29:56,800
from the results and i will print
6091
03:29:56,800 --> 03:29:59,439
results
6092
03:29:59,840 --> 03:30:02,319
and maybe another one of these lines
6093
03:30:02,319 --> 03:30:02,960
okay
6094
03:30:02,960 --> 03:30:08,080
so i need to print all of the answers
6095
03:30:08,080 --> 03:30:10,640
answers
6096
03:30:11,760 --> 03:30:14,640
and i do not want to end on a new line
6097
03:30:14,640 --> 03:30:15,840
so i'm going to set
6098
03:30:15,840 --> 03:30:19,520
end equal to nothing and i need to
6099
03:30:19,520 --> 03:30:22,640
display all of the values within
6100
03:30:22,640 --> 03:30:25,279
our dictionary all of the answers i will
6101
03:30:25,279 --> 03:30:26,239
do that using
6102
03:30:26,239 --> 03:30:29,439
a for loop for i
6103
03:30:29,439 --> 03:30:33,359
in questions
6104
03:30:33,359 --> 03:30:37,359
print questions
6105
03:30:37,359 --> 03:30:41,359
dot get i
6106
03:30:41,359 --> 03:30:44,319
and i do not want to end on a new line
6107
03:30:44,319 --> 03:30:45,279
so i'm going to set
6108
03:30:45,279 --> 03:30:48,319
end equal to nothing effectively
6109
03:30:48,319 --> 03:30:50,560
and then i'll print a new line okay
6110
03:30:50,560 --> 03:30:52,319
let's work on the guesses i think i'm
6111
03:30:52,319 --> 03:30:53,840
just going to copy all this
6112
03:30:53,840 --> 03:30:56,239
and make some changes i'll replace
6113
03:30:56,239 --> 03:30:57,120
answers
6114
03:30:57,120 --> 03:31:00,479
with guesses for
6115
03:31:00,479 --> 03:31:04,239
i in guesses
6116
03:31:04,239 --> 03:31:07,760
print i
6117
03:31:07,760 --> 03:31:09,760
okay let's just be sure that
6118
03:31:09,760 --> 03:31:10,960
everything's working we're not
6119
03:31:10,960 --> 03:31:12,080
calculating a score
6120
03:31:12,080 --> 03:31:16,640
quite yet so i'm just going to answer a
6121
03:31:16,640 --> 03:31:19,760
then b c and d
6122
03:31:19,760 --> 03:31:22,080
on second thought after printing each of
6123
03:31:22,080 --> 03:31:23,520
these answers and guesses
6124
03:31:23,520 --> 03:31:25,439
i'm going to add a space after each of
6125
03:31:25,439 --> 03:31:26,800
these so
6126
03:31:26,800 --> 03:31:28,720
within each of these four loops at the
6127
03:31:28,720 --> 03:31:30,880
end i will add a space
6128
03:31:30,880 --> 03:31:33,920
let me try that again a b
6129
03:31:33,920 --> 03:31:37,040
c d okay not too bad
6130
03:31:37,040 --> 03:31:39,439
now let's calculate the final score and
6131
03:31:39,439 --> 03:31:41,359
we're still within our display score
6132
03:31:41,359 --> 03:31:43,120
function
6133
03:31:43,120 --> 03:31:46,399
let's set score equal to
6134
03:31:46,399 --> 03:31:48,960
correct
6135
03:31:49,600 --> 03:31:53,200
guesses divided by
6136
03:31:53,200 --> 03:31:57,600
the length of our questions
6137
03:31:58,319 --> 03:32:00,640
and let me add a set of parentheses
6138
03:32:00,640 --> 03:32:03,200
around here
6139
03:32:03,279 --> 03:32:06,840
then i'm going to multiply our score by
6140
03:32:06,840 --> 03:32:08,960
100
6141
03:32:08,960 --> 03:32:12,319
and if you don't want a decimal portion
6142
03:32:12,319 --> 03:32:14,080
because we'll display a percentage we
6143
03:32:14,080 --> 03:32:16,479
can cast this as an end
6144
03:32:16,479 --> 03:32:19,840
okay that all looks good and we'll print
6145
03:32:19,840 --> 03:32:21,439
the final score
6146
03:32:21,439 --> 03:32:25,760
print your score
6147
03:32:25,760 --> 03:32:29,680
is plus
6148
03:32:29,680 --> 03:32:31,439
we need to cast our score to a string
6149
03:32:31,439 --> 03:32:34,880
because we're using string concatenation
6150
03:32:34,880 --> 03:32:38,160
plus i'll add a percent sign
6151
03:32:38,160 --> 03:32:40,880
all right let's try it i'm intentionally
6152
03:32:40,880 --> 03:32:42,880
going to get the last question wrong
6153
03:32:42,880 --> 03:32:45,600
so we should have a 75 if we have four
6154
03:32:45,600 --> 03:32:46,960
questions
6155
03:32:46,960 --> 03:32:50,399
so that would be a b c
6156
03:32:50,399 --> 03:32:52,239
is the earth round what's earth i'm
6157
03:32:52,239 --> 03:32:54,319
gonna guess d
6158
03:32:54,319 --> 03:32:57,520
answers a b c a your guess is a b c
6159
03:32:57,520 --> 03:33:01,120
d your score is 75 okay let's test it
6160
03:33:01,120 --> 03:33:04,640
but getting all wrong answers d d d
6161
03:33:04,640 --> 03:33:07,120
d your score is zero percent
6162
03:33:07,120 --> 03:33:08,720
congratulations
6163
03:33:08,720 --> 03:33:10,640
and we'll test it one more time but get
6164
03:33:10,640 --> 03:33:12,000
all the right answers
6165
03:33:12,000 --> 03:33:14,720
we just want to be sure that we have 100
6166
03:33:14,720 --> 03:33:15,840
that's a
6167
03:33:15,840 --> 03:33:20,479
b c a your score is 100
6168
03:33:20,479 --> 03:33:22,479
now the last thing that you can add is
6169
03:33:22,479 --> 03:33:24,080
that we can play again if we want
6170
03:33:24,080 --> 03:33:26,319
and i have a separate function for that
6171
03:33:26,319 --> 03:33:27,680
if you would like to add this option
6172
03:33:27,680 --> 03:33:28,800
here's how to do so
6173
03:33:28,800 --> 03:33:32,160
let's remove that pass
6174
03:33:32,160 --> 03:33:34,720
let's set a variable named response
6175
03:33:34,720 --> 03:33:36,479
equal to
6176
03:33:36,479 --> 03:33:40,000
some input do you
6177
03:33:40,000 --> 03:33:45,359
want to play again
6178
03:33:45,600 --> 03:33:50,319
let's ask for yes or no
6179
03:33:50,319 --> 03:33:55,040
actually let me remove that okay
6180
03:33:55,040 --> 03:33:56,960
and depending on the user's response it
6181
03:33:56,960 --> 03:33:58,800
might be a lowercase so let's make it
6182
03:33:58,800 --> 03:33:59,600
uppercase
6183
03:33:59,600 --> 03:34:02,960
response equals response
6184
03:34:02,960 --> 03:34:06,479
and use the upper method
6185
03:34:07,040 --> 03:34:10,880
if response is equal to
6186
03:34:10,880 --> 03:34:14,960
yes all caps if response is equal to yes
6187
03:34:14,960 --> 03:34:18,560
then return true
6188
03:34:18,560 --> 03:34:22,560
else we will return
6189
03:34:22,560 --> 03:34:25,200
false and that is it for this function
6190
03:34:25,200 --> 03:34:26,880
the last thing we need to do
6191
03:34:26,880 --> 03:34:28,640
is to create a while loop that will
6192
03:34:28,640 --> 03:34:30,319
continue to ask the player if they want
6193
03:34:30,319 --> 03:34:31,279
to play again
6194
03:34:31,279 --> 03:34:33,200
this will be after we call the new game
6195
03:34:33,200 --> 03:34:34,479
function
6196
03:34:34,479 --> 03:34:38,080
while play again
6197
03:34:38,080 --> 03:34:40,399
and remember this will return either
6198
03:34:40,399 --> 03:34:41,439
true or
6199
03:34:41,439 --> 03:34:44,319
false depending on the user's response
6200
03:34:44,319 --> 03:34:44,800
while
6201
03:34:44,800 --> 03:34:48,479
play again new game
6202
03:34:48,479 --> 03:34:50,720
and call the new game function to create
6203
03:34:50,720 --> 03:34:52,640
a new game for us
6204
03:34:52,640 --> 03:34:54,560
if we escape the while loop that means
6205
03:34:54,560 --> 03:34:56,399
the user doesn't want to play again
6206
03:34:56,399 --> 03:34:59,520
so let's print the message
6207
03:34:59,520 --> 03:35:02,960
such as by e
6208
03:35:02,960 --> 03:35:06,640
all right let's test it i'm going to get
6209
03:35:06,640 --> 03:35:10,319
all of the correct answers this time a b
6210
03:35:10,319 --> 03:35:14,319
c a all right your score is 100
6211
03:35:14,319 --> 03:35:16,000
do you want to play again i'm going to
6212
03:35:16,000 --> 03:35:17,840
type
6213
03:35:17,840 --> 03:35:22,239
yes all right then we can play again
6214
03:35:22,239 --> 03:35:26,640
d d d d your score is zero percent do
6215
03:35:26,640 --> 03:35:27,840
you want to play again
6216
03:35:27,840 --> 03:35:30,880
no i think i mastered this game
6217
03:35:30,880 --> 03:35:33,760
bye all right everybody so that's how
6218
03:35:33,760 --> 03:35:36,239
you can use python to create a simple
6219
03:35:36,239 --> 03:35:37,120
quiz game
6220
03:35:37,120 --> 03:35:38,960
i will post all of this code to the
6221
03:35:38,960 --> 03:35:40,640
comment section down below
6222
03:35:40,640 --> 03:35:43,520
but well yeah that's a basic quiz game
6223
03:35:43,520 --> 03:35:46,399
in python for you all
6224
03:35:46,399 --> 03:35:48,720
hey what's going on everybody it's bro
6225
03:35:48,720 --> 03:35:49,760
hope you're doing well
6226
03:35:49,760 --> 03:35:51,439
and in this video i'm going to explain
6227
03:35:51,439 --> 03:35:53,840
object-oriented programming in python so
6228
03:35:53,840 --> 03:35:58,800
sit back relax and enjoy the show
6229
03:35:58,880 --> 03:36:01,600
welcome to today's topic on python
6230
03:36:01,600 --> 03:36:03,200
object oriented programming
6231
03:36:03,200 --> 03:36:06,160
also known as pop and in today's video
6232
03:36:06,160 --> 03:36:07,840
we're going to be creating objects
6233
03:36:07,840 --> 03:36:10,560
an object is an instance of a class by
6234
03:36:10,560 --> 03:36:12,319
using programming we can create
6235
03:36:12,319 --> 03:36:15,279
representations of real-life objects so
6236
03:36:15,279 --> 03:36:16,800
look around you wherever you're sitting
6237
03:36:16,800 --> 03:36:19,040
or standing right now you are surrounded
6238
03:36:19,040 --> 03:36:21,840
by objects next to me i have a phone
6239
03:36:21,840 --> 03:36:24,560
a television some snacks and i'm talking
6240
03:36:24,560 --> 03:36:25,439
into a microphone
6241
03:36:25,439 --> 03:36:27,439
point being we can use programming to
6242
03:36:27,439 --> 03:36:30,080
mimic real world objects by assigning a
6243
03:36:30,080 --> 03:36:32,160
combination of attributes
6244
03:36:32,160 --> 03:36:35,200
what an object is or has and methods
6245
03:36:35,200 --> 03:36:37,520
what an object can do now in order to
6246
03:36:37,520 --> 03:36:39,359
create an object we'll need to create a
6247
03:36:39,359 --> 03:36:40,000
class
6248
03:36:40,000 --> 03:36:41,840
a class can function as a blueprint that
6249
03:36:41,840 --> 03:36:43,600
will describe what attributes and
6250
03:36:43,600 --> 03:36:44,319
methods
6251
03:36:44,319 --> 03:36:46,239
that our distinct type of object will
6252
03:36:46,239 --> 03:36:48,080
have you can either create your class
6253
03:36:48,080 --> 03:36:49,680
within your main module
6254
03:36:49,680 --> 03:36:51,279
or you could create a separate file
6255
03:36:51,279 --> 03:36:53,359
dedicated solely for your class
6256
03:36:53,359 --> 03:36:55,279
now to create a class we would type
6257
03:36:55,279 --> 03:36:57,120
class and then the name of the object
6258
03:36:57,120 --> 03:36:58,880
that we would like to create so today i
6259
03:36:58,880 --> 03:37:00,479
would like to create some car objects
6260
03:37:00,479 --> 03:37:02,000
because i like cars
6261
03:37:02,000 --> 03:37:04,720
so we would type car and a common naming
6262
03:37:04,720 --> 03:37:05,359
convention
6263
03:37:05,359 --> 03:37:07,279
with class names is that they should be
6264
03:37:07,279 --> 03:37:09,279
capital so car is going to have a
6265
03:37:09,279 --> 03:37:10,239
capital c
6266
03:37:10,239 --> 03:37:11,680
and for the time being we need to type
6267
03:37:11,680 --> 03:37:13,600
in something i'll just type in pass as a
6268
03:37:13,600 --> 03:37:14,640
placeholder
6269
03:37:14,640 --> 03:37:16,880
so if you have a small program it may be
6270
03:37:16,880 --> 03:37:18,720
better to write your class within your
6271
03:37:18,720 --> 03:37:19,120
main
6272
03:37:19,120 --> 03:37:22,160
module but if your class is fairly large
6273
03:37:22,160 --> 03:37:24,000
you may want to consider placing your
6274
03:37:24,000 --> 03:37:26,080
class within a separate module
6275
03:37:26,080 --> 03:37:28,000
so if you were to take that route we
6276
03:37:28,000 --> 03:37:29,439
would go to file
6277
03:37:29,439 --> 03:37:32,000
new python file and we would name this
6278
03:37:32,000 --> 03:37:32,800
car
6279
03:37:32,800 --> 03:37:35,040
click python file and we would declare
6280
03:37:35,040 --> 03:37:36,000
our class within
6281
03:37:36,000 --> 03:37:39,200
the separate module so class car and for
6282
03:37:39,200 --> 03:37:40,560
now i'll type in pass
6283
03:37:40,560 --> 03:37:42,560
then we just need to import this class
6284
03:37:42,560 --> 03:37:44,720
so within our main module we would type
6285
03:37:44,720 --> 03:37:47,359
from the name of the module import the
6286
03:37:47,359 --> 03:37:48,399
name of the class
6287
03:37:48,399 --> 03:37:51,359
from car import car now objects can have
6288
03:37:51,359 --> 03:37:53,200
some combination of attributes and
6289
03:37:53,200 --> 03:37:55,200
methods attributes describe what an
6290
03:37:55,200 --> 03:37:56,560
object is or has
6291
03:37:56,560 --> 03:37:58,479
so what are a few attributes that cars
6292
03:37:58,479 --> 03:38:00,880
might have they might have a make so
6293
03:38:00,880 --> 03:38:02,720
let's create a few variables
6294
03:38:02,720 --> 03:38:04,319
for the time being i'm just going to use
6295
03:38:04,319 --> 03:38:06,000
none as a placeholder
6296
03:38:06,000 --> 03:38:10,479
cars can have a model a year
6297
03:38:10,479 --> 03:38:13,840
and a color
6298
03:38:14,080 --> 03:38:16,560
now objects can also have methods what
6299
03:38:16,560 --> 03:38:18,399
kinds of methods could cars perform
6300
03:38:18,399 --> 03:38:20,640
perhaps a drive method and a stop method
6301
03:38:20,640 --> 03:38:22,000
so let's define those
6302
03:38:22,000 --> 03:38:25,439
def let's say drive and we'll have one
6303
03:38:25,439 --> 03:38:26,239
argument
6304
03:38:26,239 --> 03:38:28,720
self self refers to the object that is
6305
03:38:28,720 --> 03:38:30,000
using this method
6306
03:38:30,000 --> 03:38:31,840
now what do we want to do when we call
6307
03:38:31,840 --> 03:38:33,120
this method
6308
03:38:33,120 --> 03:38:36,399
let's say this car
6309
03:38:36,399 --> 03:38:39,520
is driving let's create a stop method as
6310
03:38:39,520 --> 03:38:40,160
well
6311
03:38:40,160 --> 03:38:43,520
def stop
6312
03:38:43,520 --> 03:38:46,720
and we will print this
6313
03:38:46,720 --> 03:38:49,840
car is stopped we now have all of the
6314
03:38:49,840 --> 03:38:51,600
different attributes and methods we
6315
03:38:51,600 --> 03:38:53,359
would like our car objects to have but
6316
03:38:53,359 --> 03:38:54,880
there is one more thing that we need
6317
03:38:54,880 --> 03:38:57,199
it is a special method called the init
6318
03:38:57,199 --> 03:38:58,640
method that will construct
6319
03:38:58,640 --> 03:39:00,560
objects for us in other programming
6320
03:39:00,560 --> 03:39:02,160
languages this is known as the
6321
03:39:02,160 --> 03:39:03,040
constructor
6322
03:39:03,040 --> 03:39:05,040
so we need a special method that will
6323
03:39:05,040 --> 03:39:06,399
create objects for us
6324
03:39:06,399 --> 03:39:08,960
so we need to define this method def and
6325
03:39:08,960 --> 03:39:10,880
the syntax on this is somewhat strange
6326
03:39:10,880 --> 03:39:11,439
it is
6327
03:39:11,439 --> 03:39:14,080
two underscores init it's short for
6328
03:39:14,080 --> 03:39:14,880
initialize
6329
03:39:14,880 --> 03:39:16,800
200 scores again and then we need at
6330
03:39:16,800 --> 03:39:18,720
least self as an argument
6331
03:39:18,720 --> 03:39:21,760
and then we can actually assign our car
6332
03:39:21,760 --> 03:39:24,880
objects unique variables so take all of
6333
03:39:24,880 --> 03:39:26,239
these attributes that we have
6334
03:39:26,239 --> 03:39:27,680
and we're going to place them within
6335
03:39:27,680 --> 03:39:30,080
this init method now we can receive
6336
03:39:30,080 --> 03:39:30,960
arguments
6337
03:39:30,960 --> 03:39:33,040
when we create car objects but we need
6338
03:39:33,040 --> 03:39:34,239
to pass them in
6339
03:39:34,239 --> 03:39:36,960
as arguments to our init method so we
6340
03:39:36,960 --> 03:39:38,479
need to set up some parameters
6341
03:39:38,479 --> 03:39:39,920
let's say in order to create a car
6342
03:39:39,920 --> 03:39:42,720
object we need a make
6343
03:39:42,720 --> 03:39:45,920
a model a year and a color
6344
03:39:45,920 --> 03:39:47,600
then when we receive these arguments we
6345
03:39:47,600 --> 03:39:49,199
can actually assign them to
6346
03:39:49,199 --> 03:39:52,080
each car's specific attributes but we
6347
03:39:52,080 --> 03:39:54,160
need to precede each of these with self
6348
03:39:54,160 --> 03:39:56,160
self is referring to the object that
6349
03:39:56,160 --> 03:39:57,359
we're currently working on
6350
03:39:57,359 --> 03:40:01,279
or creating so self.make self.model
6351
03:40:01,279 --> 03:40:05,359
self.ear and self.color
6352
03:40:05,359 --> 03:40:07,199
and then when we assign these we're
6353
03:40:07,199 --> 03:40:08,880
going to say self.make
6354
03:40:08,880 --> 03:40:11,199
equals whatever make that we receive
6355
03:40:11,199 --> 03:40:12,960
when it's passed in as an argument
6356
03:40:12,960 --> 03:40:15,680
self.model equals model and continue
6357
03:40:15,680 --> 03:40:17,279
that pattern for year
6358
03:40:17,279 --> 03:40:20,720
and color all right that is all we need
6359
03:40:20,720 --> 03:40:23,279
for our class car we have a constructor
6360
03:40:23,279 --> 03:40:24,399
we're assigning
6361
03:40:24,399 --> 03:40:26,239
arguments that we receive to the
6362
03:40:26,239 --> 03:40:28,160
attributes of our car object
6363
03:40:28,160 --> 03:40:30,640
and we also have two methods one for
6364
03:40:30,640 --> 03:40:32,000
drive and one for stop
6365
03:40:32,000 --> 03:40:33,439
now we can start creating some car
6366
03:40:33,439 --> 03:40:36,000
objects so going back to my main file
6367
03:40:36,000 --> 03:40:36,479
here
6368
03:40:36,479 --> 03:40:38,319
to create an object we need a unique
6369
03:40:38,319 --> 03:40:40,239
name for it let's call this first car
6370
03:40:40,239 --> 03:40:41,920
just car one
6371
03:40:41,920 --> 03:40:44,800
equals the name of the class car then a
6372
03:40:44,800 --> 03:40:46,239
set of parentheses
6373
03:40:46,239 --> 03:40:48,479
now in order to construct a car object
6374
03:40:48,479 --> 03:40:50,160
we need to pass in a matching
6375
03:40:50,160 --> 03:40:52,239
set of arguments you can see here that
6376
03:40:52,239 --> 03:40:54,319
we need to pass in a make a model a year
6377
03:40:54,319 --> 03:40:55,120
and a color
6378
03:40:55,120 --> 03:40:57,040
so what kind of car should we make let's
6379
03:40:57,040 --> 03:40:59,520
say that the make is going to be
6380
03:40:59,520 --> 03:41:03,520
chevy we need a model
6381
03:41:03,520 --> 03:41:07,279
perhaps corvette a year
6382
03:41:07,279 --> 03:41:10,640
2021 is good and a color let's say blue
6383
03:41:10,640 --> 03:41:13,760
alright now let's actually access some
6384
03:41:13,760 --> 03:41:15,439
of these cars attributes
6385
03:41:15,439 --> 03:41:16,640
and i should probably put this one in a
6386
03:41:16,640 --> 03:41:19,680
print statement so print car ones
6387
03:41:19,680 --> 03:41:23,359
make and this should print chevy
6388
03:41:23,359 --> 03:41:25,600
then we can do the same thing for model
6389
03:41:25,600 --> 03:41:26,960
year and color
6390
03:41:26,960 --> 03:41:29,120
so let's change some of these around
6391
03:41:29,120 --> 03:41:30,720
model
6392
03:41:30,720 --> 03:41:35,359
year and color
6393
03:41:35,920 --> 03:41:38,720
yep this object is a chevy corvette 2021
6394
03:41:38,720 --> 03:41:39,920
and the color is blue
6395
03:41:39,920 --> 03:41:41,920
and we should have two methods as well
6396
03:41:41,920 --> 03:41:42,960
so car one
6397
03:41:42,960 --> 03:41:44,720
i would like this car to use its drive
6398
03:41:44,720 --> 03:41:46,399
method
6399
03:41:46,399 --> 03:41:48,800
this car is driving and let's have this
6400
03:41:48,800 --> 03:41:50,399
car use it stop method
6401
03:41:50,399 --> 03:41:54,479
car one dot stop
6402
03:41:55,040 --> 03:41:56,880
this car is stopped there's one thing
6403
03:41:56,880 --> 03:41:58,560
that i should mention real quick because
6404
03:41:58,560 --> 03:42:00,080
i forgot to mention it earlier
6405
03:42:00,080 --> 03:42:02,319
so within our init method you can see
6406
03:42:02,319 --> 03:42:03,520
that we need five
6407
03:42:03,520 --> 03:42:05,520
arguments in order to construct a car
6408
03:42:05,520 --> 03:42:06,720
object self
6409
03:42:06,720 --> 03:42:09,439
make model year in color but when we
6410
03:42:09,439 --> 03:42:10,000
pass in
6411
03:42:10,000 --> 03:42:12,160
our arguments we're not passing in
6412
03:42:12,160 --> 03:42:13,920
anything for self we only have
6413
03:42:13,920 --> 03:42:16,000
four arguments here that's because with
6414
03:42:16,000 --> 03:42:18,640
python we do not need to pass itself
6415
03:42:18,640 --> 03:42:20,960
that's done automatically for us we're
6416
03:42:20,960 --> 03:42:22,640
referring to the object that we're
6417
03:42:22,640 --> 03:42:23,439
dealing with
6418
03:42:23,439 --> 03:42:25,439
as you can see with our drive and stop
6419
03:42:25,439 --> 03:42:26,640
method we need
6420
03:42:26,640 --> 03:42:29,520
to pass in self in order to execute our
6421
03:42:29,520 --> 03:42:30,160
method
6422
03:42:30,160 --> 03:42:31,920
but when we call this method we do not
6423
03:42:31,920 --> 03:42:34,000
need to do so so just remember with
6424
03:42:34,000 --> 03:42:36,239
python with self we do not need to pass
6425
03:42:36,239 --> 03:42:38,239
in anything for this argument that is
6426
03:42:38,239 --> 03:42:38,720
all
6427
03:42:38,720 --> 03:42:40,720
now the nice thing about this is that we
6428
03:42:40,720 --> 03:42:42,239
can reuse this class
6429
03:42:42,239 --> 03:42:43,840
as a blueprint to create more car
6430
03:42:43,840 --> 03:42:45,279
objects we just call that
6431
03:42:45,279 --> 03:42:47,520
init method that constructor so this
6432
03:42:47,520 --> 03:42:49,120
time let's create a second car called
6433
03:42:49,120 --> 03:42:49,600
car
6434
03:42:49,600 --> 03:42:51,680
2 and this will be a different kind of
6435
03:42:51,680 --> 03:42:53,199
car
6436
03:42:53,199 --> 03:42:55,439
what kinds of arguments should we pass
6437
03:42:55,439 --> 03:42:58,720
it let's say this is a ford
6438
03:42:58,720 --> 03:43:01,760
mustang the year will be 2022 and the
6439
03:43:01,760 --> 03:43:03,359
color will be red
6440
03:43:03,359 --> 03:43:05,040
all right let's check card to use
6441
03:43:05,040 --> 03:43:07,840
attributes
6442
03:43:08,160 --> 03:43:10,640
and let's have car 2 use its drive and
6443
03:43:10,640 --> 03:43:12,640
stop method
6444
03:43:12,640 --> 03:43:15,120
this is a ford mustang the year is 2022
6445
03:43:15,120 --> 03:43:15,920
the color is red
6446
03:43:15,920 --> 03:43:17,520
this car is driving and this car is
6447
03:43:17,520 --> 03:43:19,439
stopped one other thing that we can do
6448
03:43:19,439 --> 03:43:20,000
too
6449
03:43:20,000 --> 03:43:22,399
is that within our methods here let's
6450
03:43:22,399 --> 03:43:23,680
replace car with
6451
03:43:23,680 --> 03:43:25,199
the name of the model that we're working
6452
03:43:25,199 --> 03:43:27,680
with so this
6453
03:43:27,680 --> 03:43:31,279
plus self dot model
6454
03:43:31,279 --> 03:43:34,880
plus is driving now this self keyword
6455
03:43:34,880 --> 03:43:37,920
think of it as you're replacing self
6456
03:43:37,920 --> 03:43:39,760
with the name of the object that we're
6457
03:43:39,760 --> 03:43:41,600
working on if car one
6458
03:43:41,600 --> 03:43:44,479
is using its drive method replace itself
6459
03:43:44,479 --> 03:43:45,600
with car one
6460
03:43:45,600 --> 03:43:47,359
if this is car two that called this
6461
03:43:47,359 --> 03:43:50,160
method then replace itself with car 2.
6462
03:43:50,160 --> 03:43:51,760
think of it that way and let's do the
6463
03:43:51,760 --> 03:43:54,560
same thing for stop
6464
03:43:54,560 --> 03:43:58,479
this plus self dot model
6465
03:43:58,479 --> 03:44:01,520
plus is stopped all right
6466
03:44:01,520 --> 03:44:03,279
probably don't need these anymore so
6467
03:44:03,279 --> 03:44:04,560
let's have car one
6468
03:44:04,560 --> 03:44:07,600
use its drive and stop method this
6469
03:44:07,600 --> 03:44:09,359
corvette is driving this corvette is
6470
03:44:09,359 --> 03:44:09,920
stopped
6471
03:44:09,920 --> 03:44:13,279
let's try this with car two this mustang
6472
03:44:13,279 --> 03:44:13,840
is driving
6473
03:44:13,840 --> 03:44:16,239
this mustang is stopped now you can have
6474
03:44:16,239 --> 03:44:18,080
them do things independently too let's
6475
03:44:18,080 --> 03:44:20,239
have car 1 use its drive method and car
6476
03:44:20,239 --> 03:44:22,239
2 uses stop method
6477
03:44:22,239 --> 03:44:24,080
this corvette is driving and this
6478
03:44:24,080 --> 03:44:26,160
mustang is stopped in conclusion
6479
03:44:26,160 --> 03:44:28,560
a class can function as a blueprint to
6480
03:44:28,560 --> 03:44:29,680
create objects
6481
03:44:29,680 --> 03:44:32,080
we can assign attributes what describe
6482
03:44:32,080 --> 03:44:33,279
an object is or
6483
03:44:33,279 --> 03:44:36,319
has and methods what each object
6484
03:44:36,319 --> 03:44:38,800
can do and then within our class we have
6485
03:44:38,800 --> 03:44:41,439
a special method called the init method
6486
03:44:41,439 --> 03:44:44,160
we can pass in some arguments and assign
6487
03:44:44,160 --> 03:44:45,600
these arguments to each
6488
03:44:45,600 --> 03:44:47,920
object's attributes and then we can
6489
03:44:47,920 --> 03:44:49,920
reuse this class as if it was a
6490
03:44:49,920 --> 03:44:50,640
blueprint
6491
03:44:50,640 --> 03:44:53,520
so we can create more objects out of it
6492
03:44:53,520 --> 03:44:53,840
so
6493
03:44:53,840 --> 03:44:56,000
that is the basics of object oriented
6494
03:44:56,000 --> 03:44:58,000
programming in python if you would like
6495
03:44:58,000 --> 03:45:00,080
a copy of all this code i will post this
6496
03:45:00,080 --> 03:45:01,920
in the comment section down below
6497
03:45:01,920 --> 03:45:04,160
but yeah that's the basics of object
6498
03:45:04,160 --> 03:45:05,279
oriented programming
6499
03:45:05,279 --> 03:45:08,720
in python hey what's going on everybody
6500
03:45:08,720 --> 03:45:10,640
it's bro hope you're doing well and in
6501
03:45:10,640 --> 03:45:12,000
this video i'm going to explain
6502
03:45:12,000 --> 03:45:14,560
the basics of class variables in python
6503
03:45:14,560 --> 03:45:15,840
so sit back
6504
03:45:15,840 --> 03:45:19,760
relax and enjoy the show
6505
03:45:19,760 --> 03:45:21,520
here's a super quick video on the
6506
03:45:21,520 --> 03:45:23,439
differences between class and instance
6507
03:45:23,439 --> 03:45:24,160
variables
6508
03:45:24,160 --> 03:45:26,560
for this example let's say we have two
6509
03:45:26,560 --> 03:45:27,520
car objects
6510
03:45:27,520 --> 03:45:29,840
named car one and car two and i'm just
6511
03:45:29,840 --> 03:45:31,840
passing in a bunch of values to each of
6512
03:45:31,840 --> 03:45:33,439
these car constructors
6513
03:45:33,439 --> 03:45:36,239
now within my class car i have my init
6514
03:45:36,239 --> 03:45:36,800
method
6515
03:45:36,800 --> 03:45:38,720
also known as the constructor and what
6516
03:45:38,720 --> 03:45:40,319
i'm doing is assigning all of these
6517
03:45:40,319 --> 03:45:40,960
values
6518
03:45:40,960 --> 03:45:43,199
to the variables found within my car
6519
03:45:43,199 --> 03:45:44,319
constructor
6520
03:45:44,319 --> 03:45:46,479
these variables declared inside the
6521
03:45:46,479 --> 03:45:47,439
constructor
6522
03:45:47,439 --> 03:45:50,319
are known as instance variables and each
6523
03:45:50,319 --> 03:45:50,960
object
6524
03:45:50,960 --> 03:45:53,120
can have their own unique values
6525
03:45:53,120 --> 03:45:54,000
assigned to each
6526
03:45:54,000 --> 03:45:56,239
of these variables now where a class
6527
03:45:56,239 --> 03:45:57,439
variable is different
6528
03:45:57,439 --> 03:46:00,479
is that a class variable is declared
6529
03:46:00,479 --> 03:46:02,239
inside the class but outside of the
6530
03:46:02,239 --> 03:46:04,000
constructor and what we can do
6531
03:46:04,000 --> 03:46:06,160
is set some default values for some
6532
03:46:06,160 --> 03:46:07,040
variables
6533
03:46:07,040 --> 03:46:08,800
let's say we would like to add an
6534
03:46:08,800 --> 03:46:10,800
additional variable let's say wheels
6535
03:46:10,800 --> 03:46:13,439
wheels equals and we can set a default
6536
03:46:13,439 --> 03:46:14,080
value
6537
03:46:14,080 --> 03:46:16,399
for all instances of this class for each
6538
03:46:16,399 --> 03:46:18,160
car object that we create
6539
03:46:18,160 --> 03:46:20,640
so we set wheels to equal four this is
6540
03:46:20,640 --> 03:46:22,560
an example of a class
6541
03:46:22,560 --> 03:46:25,199
variable whenever we create a car object
6542
03:46:25,199 --> 03:46:26,640
we can pass in a unique
6543
03:46:26,640 --> 03:46:29,600
make model year in color but by default
6544
03:46:29,600 --> 03:46:30,720
they will all have
6545
03:46:30,720 --> 03:46:33,279
four wheels now if i was to print the
6546
03:46:33,279 --> 03:46:34,239
amount of wheels
6547
03:46:34,239 --> 03:46:37,520
that car 1 and car 2 have they will both
6548
03:46:37,520 --> 03:46:38,080
have
6549
03:46:38,080 --> 03:46:40,399
four wheels it's as if we're setting a
6550
03:46:40,399 --> 03:46:41,279
default value
6551
03:46:41,279 --> 03:46:43,840
if i change this to two wheels well then
6552
03:46:43,840 --> 03:46:45,279
these cars have two wheels
6553
03:46:45,279 --> 03:46:47,600
maybe their motorcycles or something now
6554
03:46:47,600 --> 03:46:48,399
you can actually
6555
03:46:48,399 --> 03:46:50,640
change these values each object will
6556
03:46:50,640 --> 03:46:51,520
have their own
6557
03:46:51,520 --> 03:46:53,600
copy of this variable but you can set it
6558
03:46:53,600 --> 03:46:54,960
to a default value
6559
03:46:54,960 --> 03:46:58,399
so let's say car one
6560
03:46:58,399 --> 03:47:01,439
is a motorcycle car one dot wheels
6561
03:47:01,439 --> 03:47:04,319
equals two so if i were to print car
6562
03:47:04,319 --> 03:47:06,319
one's wheels along with card two
6563
03:47:06,319 --> 03:47:09,680
car one will have two wheels but car two
6564
03:47:09,680 --> 03:47:11,359
is still using that default amount of
6565
03:47:11,359 --> 03:47:12,880
wheels which is four
6566
03:47:12,880 --> 03:47:14,640
now there's another way in which you can
6567
03:47:14,640 --> 03:47:16,399
access a class variable
6568
03:47:16,399 --> 03:47:18,080
you don't necessarily need to create an
6569
03:47:18,080 --> 03:47:19,920
object to do so you can just use the
6570
03:47:19,920 --> 03:47:20,880
name of the class
6571
03:47:20,880 --> 03:47:23,120
so what i'm going to do is turn all of
6572
03:47:23,120 --> 03:47:25,040
these lines into comments
6573
03:47:25,040 --> 03:47:28,239
and i will print the amount of wheels
6574
03:47:28,239 --> 03:47:31,040
that our class has so type in the name
6575
03:47:31,040 --> 03:47:33,520
of the class car with a capital c
6576
03:47:33,520 --> 03:47:35,359
it should be the same name as your class
6577
03:47:35,359 --> 03:47:37,359
name followed by the name of the class
6578
03:47:37,359 --> 03:47:38,399
variable
6579
03:47:38,399 --> 03:47:41,359
car dot wheels but make sure you spell
6580
03:47:41,359 --> 03:47:42,080
it right
6581
03:47:42,080 --> 03:47:44,399
all right so this will display four
6582
03:47:44,399 --> 03:47:45,199
wheels
6583
03:47:45,199 --> 03:47:47,600
now what if we decide to change the
6584
03:47:47,600 --> 03:47:48,800
class variable
6585
03:47:48,800 --> 03:47:51,520
through our class well that will affect
6586
03:47:51,520 --> 03:47:51,840
all
6587
03:47:51,840 --> 03:47:55,439
instances of our class so let's say car
6588
03:47:55,439 --> 03:47:59,199
dot wheels equals two it's as if we
6589
03:47:59,199 --> 03:48:01,120
change our mind and we have decided to
6590
03:48:01,120 --> 03:48:03,279
create motorcycles instead of cars
6591
03:48:03,279 --> 03:48:06,319
well if we were to change the wheels
6592
03:48:06,319 --> 03:48:08,479
class variable through the use of our
6593
03:48:08,479 --> 03:48:09,439
car class
6594
03:48:09,439 --> 03:48:11,279
well then it's going to affect it for
6595
03:48:11,279 --> 03:48:13,199
all instances of this class
6596
03:48:13,199 --> 03:48:15,680
you can see that both car 1 and car 2
6597
03:48:15,680 --> 03:48:16,960
now have two wheels
6598
03:48:16,960 --> 03:48:19,199
when we just changed car 1's wheels it
6599
03:48:19,199 --> 03:48:20,080
did not affect
6600
03:48:20,080 --> 03:48:22,239
car 2 at all so that's the main
6601
03:48:22,239 --> 03:48:24,160
difference between a class variable and
6602
03:48:24,160 --> 03:48:25,520
an instance variable
6603
03:48:25,520 --> 03:48:27,840
an instance variable is declared inside
6604
03:48:27,840 --> 03:48:28,880
of constructor
6605
03:48:28,880 --> 03:48:31,520
and they can be given unique values with
6606
03:48:31,520 --> 03:48:32,560
class variables
6607
03:48:32,560 --> 03:48:34,640
they are declared within a class but
6608
03:48:34,640 --> 03:48:35,920
outside of the constructor
6609
03:48:35,920 --> 03:48:38,399
and you can set a default value for all
6610
03:48:38,399 --> 03:48:40,000
instances of this class
6611
03:48:40,000 --> 03:48:42,399
for all unique objects that are created
6612
03:48:42,399 --> 03:48:43,920
and then you can change those values
6613
03:48:43,920 --> 03:48:44,880
later too
6614
03:48:44,880 --> 03:48:46,399
so if you would like a copy of all this
6615
03:48:46,399 --> 03:48:48,000
code i will post all of this in the
6616
03:48:48,000 --> 03:48:49,520
comment section down below
6617
03:48:49,520 --> 03:48:51,840
but yeah that is the basics of class
6618
03:48:51,840 --> 03:48:52,640
variables
6619
03:48:52,640 --> 03:48:56,080
in python hey what's going on everybody
6620
03:48:56,080 --> 03:48:57,920
it's you bro hope you're doing well and
6621
03:48:57,920 --> 03:48:59,279
in this video i'm going to explain
6622
03:48:59,279 --> 03:49:01,040
inheritance in python so
6623
03:49:01,040 --> 03:49:05,920
sit back relax and enjoy the show
6624
03:49:06,080 --> 03:49:08,000
let's talk about irritants the term
6625
03:49:08,000 --> 03:49:10,000
inherit means to receive derive or
6626
03:49:10,000 --> 03:49:11,600
be left with and we can apply this
6627
03:49:11,600 --> 03:49:13,439
concept to programming classes can
6628
03:49:13,439 --> 03:49:14,399
inherit something
6629
03:49:14,399 --> 03:49:16,000
usually attributes and methods from
6630
03:49:16,000 --> 03:49:18,479
another class these classes can form
6631
03:49:18,479 --> 03:49:21,040
parent-child relationships where a child
6632
03:49:21,040 --> 03:49:22,160
will receive everything
6633
03:49:22,160 --> 03:49:24,479
that the parent class has much like you
6634
03:49:24,479 --> 03:49:26,560
inherit jeans from your parents
6635
03:49:26,560 --> 03:49:28,399
and classes can have children and give
6636
03:49:28,399 --> 03:49:30,640
whatever they own to their children
6637
03:49:30,640 --> 03:49:32,239
and in today's video we'll be creating a
6638
03:49:32,239 --> 03:49:33,840
parent class called animal
6639
03:49:33,840 --> 03:49:35,520
and children of the animal class will
6640
03:49:35,520 --> 03:49:37,040
inherit the common attributes and
6641
03:49:37,040 --> 03:49:37,600
methods
6642
03:49:37,600 --> 03:49:39,680
that all animals might have to kick off
6643
03:49:39,680 --> 03:49:41,040
this video i think what we'll do
6644
03:49:41,040 --> 03:49:42,720
is keep all of our classes within the
6645
03:49:42,720 --> 03:49:44,399
same file just so that it's easier for
6646
03:49:44,399 --> 03:49:45,760
us to read and understand
6647
03:49:45,760 --> 03:49:47,680
let's begin by creating a class called
6648
03:49:47,680 --> 03:49:49,199
animal so at the top
6649
03:49:49,199 --> 03:49:51,520
type class animal and what are some
6650
03:49:51,520 --> 03:49:53,359
attributes and methods that all animals
6651
03:49:53,359 --> 03:49:53,920
should have
6652
03:49:53,920 --> 03:49:55,920
let's say we have a class variable
6653
03:49:55,920 --> 03:49:58,239
called alive and we'll set this to true
6654
03:49:58,239 --> 03:50:00,080
if you're an animal you begin by being
6655
03:50:00,080 --> 03:50:02,000
alive and what are some methods
6656
03:50:02,000 --> 03:50:04,399
how about an eat and sleep method so
6657
03:50:04,399 --> 03:50:05,439
let's define those
6658
03:50:05,439 --> 03:50:08,319
def eat what are we gonna do when we
6659
03:50:08,319 --> 03:50:09,120
call this method
6660
03:50:09,120 --> 03:50:12,000
let's print something let's print this
6661
03:50:12,000 --> 03:50:12,720
animal
6662
03:50:12,720 --> 03:50:15,040
is eating let's also create a sleep
6663
03:50:15,040 --> 03:50:16,239
method def
6664
03:50:16,239 --> 03:50:18,080
sleep what are we gonna do when we call
6665
03:50:18,080 --> 03:50:19,680
this method let's print something as
6666
03:50:19,680 --> 03:50:20,880
well
6667
03:50:20,880 --> 03:50:24,319
this animal is sleeping and we now have
6668
03:50:24,319 --> 03:50:25,279
our animal class
6669
03:50:25,279 --> 03:50:27,840
now let's create separate classes for
6670
03:50:27,840 --> 03:50:30,080
specific types of animals let's say
6671
03:50:30,080 --> 03:50:32,880
a class for rabbit a fish and a hawk so
6672
03:50:32,880 --> 03:50:33,520
we can make
6673
03:50:33,520 --> 03:50:36,399
objects of those classes so let's begin
6674
03:50:36,399 --> 03:50:36,960
with a
6675
03:50:36,960 --> 03:50:40,399
rabbit class class rabbit now to use
6676
03:50:40,399 --> 03:50:41,359
inheritance
6677
03:50:41,359 --> 03:50:43,199
with the class that you intend to be the
6678
03:50:43,199 --> 03:50:45,520
child class after the class name add a
6679
03:50:45,520 --> 03:50:46,800
set of parentheses
6680
03:50:46,800 --> 03:50:48,479
and then pass in the name of the parent
6681
03:50:48,479 --> 03:50:49,840
class in this case it's going to be
6682
03:50:49,840 --> 03:50:50,479
animal
6683
03:50:50,479 --> 03:50:53,439
so we will add that to the parentheses
6684
03:50:53,439 --> 03:50:54,160
so now
6685
03:50:54,160 --> 03:50:56,960
rabbit is the child class and animal is
6686
03:50:56,960 --> 03:50:57,920
the parent class
6687
03:50:57,920 --> 03:51:00,000
so the child class is going to inherit
6688
03:51:00,000 --> 03:51:02,080
everything that the animal class has
6689
03:51:02,080 --> 03:51:04,000
so for the time being i'm going to type
6690
03:51:04,000 --> 03:51:05,760
pass so this rabbit class
6691
03:51:05,760 --> 03:51:07,760
will have access to a class variable
6692
03:51:07,760 --> 03:51:08,800
called alive
6693
03:51:08,800 --> 03:51:11,680
and an eat and sleep method so let's
6694
03:51:11,680 --> 03:51:12,239
define
6695
03:51:12,239 --> 03:51:14,800
a fish and hawk class and i'm just going
6696
03:51:14,800 --> 03:51:16,720
to copy what i have here and just change
6697
03:51:16,720 --> 03:51:19,840
rabbit to fish and then do so again and
6698
03:51:19,840 --> 03:51:21,040
change rabbit to
6699
03:51:21,040 --> 03:51:24,080
hawk this is our family tree animal
6700
03:51:24,080 --> 03:51:26,160
is the parent class and it has three
6701
03:51:26,160 --> 03:51:27,840
children a rabbit class
6702
03:51:27,840 --> 03:51:29,920
a fish class and a hot class and for the
6703
03:51:29,920 --> 03:51:31,279
time being we're just going to write
6704
03:51:31,279 --> 03:51:32,960
pass as a placeholder
6705
03:51:32,960 --> 03:51:35,040
so i bet we can create objects from
6706
03:51:35,040 --> 03:51:36,000
these classes
6707
03:51:36,000 --> 03:51:37,760
so let's do so let's say we have an
6708
03:51:37,760 --> 03:51:39,120
object called rabbit
6709
03:51:39,120 --> 03:51:41,760
rabbit equals rabbit let's do the same
6710
03:51:41,760 --> 03:51:42,319
thing for
6711
03:51:42,319 --> 03:51:45,840
fish and hawk
6712
03:51:47,520 --> 03:51:50,080
all right we now have three objects
6713
03:51:50,080 --> 03:51:50,560
rabbit
6714
03:51:50,560 --> 03:51:53,439
fish and hawk and let's take a look at
6715
03:51:53,439 --> 03:51:53,920
their
6716
03:51:53,920 --> 03:51:56,080
class variables i bet they have an alive
6717
03:51:56,080 --> 03:51:58,319
variable let's check that so i would
6718
03:51:58,319 --> 03:51:59,600
like to print
6719
03:51:59,600 --> 03:52:02,399
rabbit dot oh would you look at that
6720
03:52:02,399 --> 03:52:03,439
this rabbit class
6721
03:52:03,439 --> 03:52:05,600
has a class variable called alive even
6722
03:52:05,600 --> 03:52:07,359
though there's nothing within this class
6723
03:52:07,359 --> 03:52:09,439
well that's because we used inheritance
6724
03:52:09,439 --> 03:52:10,479
so each of these
6725
03:52:10,479 --> 03:52:12,560
three children classes inherit
6726
03:52:12,560 --> 03:52:14,319
everything from their parents
6727
03:52:14,319 --> 03:52:16,560
all of these attributes and methods i
6728
03:52:16,560 --> 03:52:18,160
bet they have eaten sleep functions as
6729
03:52:18,160 --> 03:52:19,920
well so let's check that
6730
03:52:19,920 --> 03:52:22,399
with fish let's have fish use its eat
6731
03:52:22,399 --> 03:52:24,080
method
6732
03:52:24,080 --> 03:52:26,640
and hawk will use its sleep method hawk
6733
03:52:26,640 --> 03:52:29,359
dot sleep
6734
03:52:29,760 --> 03:52:31,920
so the rabbit is alive the fish is
6735
03:52:31,920 --> 03:52:32,800
eating and
6736
03:52:32,800 --> 03:52:34,800
the hawk is sleeping now even though we
6737
03:52:34,800 --> 03:52:36,319
have nothing written within each of
6738
03:52:36,319 --> 03:52:36,720
these
6739
03:52:36,720 --> 03:52:39,359
specific types of animal classes all of
6740
03:52:39,359 --> 03:52:41,439
these animals have access to these
6741
03:52:41,439 --> 03:52:43,199
attributes and methods because of
6742
03:52:43,199 --> 03:52:44,239
inheritance
6743
03:52:44,239 --> 03:52:46,479
and another benefit of inheritance is
6744
03:52:46,479 --> 03:52:48,239
that we don't need to keep on
6745
03:52:48,239 --> 03:52:50,080
copying and pasting this code for
6746
03:52:50,080 --> 03:52:51,760
example let's say we were not using
6747
03:52:51,760 --> 03:52:53,520
inheritance so we would not need an
6748
03:52:53,520 --> 03:52:54,880
animal class
6749
03:52:54,880 --> 03:52:56,880
we could write our code this way where
6750
03:52:56,880 --> 03:52:59,199
we would just copy and paste everything
6751
03:52:59,199 --> 03:53:01,600
underneath each of our classes so what
6752
03:53:01,600 --> 03:53:03,520
if we need to make a change to one of
6753
03:53:03,520 --> 03:53:04,560
these methods
6754
03:53:04,560 --> 03:53:06,160
as in your boss says you know what let's
6755
03:53:06,160 --> 03:53:07,600
change sleep to
6756
03:53:07,600 --> 03:53:10,399
slumber okay well we would have to go to
6757
03:53:10,399 --> 03:53:11,760
each of these classes and make the
6758
03:53:11,760 --> 03:53:12,960
change manually
6759
03:53:12,960 --> 03:53:14,720
it's not too bad if you have like three
6760
03:53:14,720 --> 03:53:16,560
classes but what if you have hundreds
6761
03:53:16,560 --> 03:53:18,560
that's going to take a lot of effort so
6762
03:53:18,560 --> 03:53:20,160
wouldn't it be nice if we just
6763
03:53:20,160 --> 03:53:21,840
list everything that each of these
6764
03:53:21,840 --> 03:53:23,439
classes has in common
6765
03:53:23,439 --> 03:53:25,600
and then have all of these classes
6766
03:53:25,600 --> 03:53:26,479
inherit from
6767
03:53:26,479 --> 03:53:28,560
one common class and then if you need to
6768
03:53:28,560 --> 03:53:29,520
make any changes
6769
03:53:29,520 --> 03:53:31,680
you can do so just within this class and
6770
03:53:31,680 --> 03:53:33,199
this change will apply to
6771
03:53:33,199 --> 03:53:35,199
all of the different child classes so
6772
03:53:35,199 --> 03:53:37,279
that's another benefit of inheritance
6773
03:53:37,279 --> 03:53:39,120
and not only does it make any changes
6774
03:53:39,120 --> 03:53:40,720
easier but each class
6775
03:53:40,720 --> 03:53:42,960
can have their own unique attributes and
6776
03:53:42,960 --> 03:53:43,920
methods as well
6777
03:53:43,920 --> 03:53:45,600
along with the attributes and methods
6778
03:53:45,600 --> 03:53:47,359
that they inherit from their parents
6779
03:53:47,359 --> 03:53:49,520
so for rabbit fish and hawk let's have
6780
03:53:49,520 --> 03:53:50,800
each of these classes have
6781
03:53:50,800 --> 03:53:53,040
their own unique method as well so for
6782
03:53:53,040 --> 03:53:54,239
rabbit let's define
6783
03:53:54,239 --> 03:53:57,040
a run method if you're a rabbit then you
6784
03:53:57,040 --> 03:53:57,840
can run
6785
03:53:57,840 --> 03:54:01,120
so let's print this rabbit
6786
03:54:01,120 --> 03:54:04,000
is running and i'm just going to fix
6787
03:54:04,000 --> 03:54:05,680
some of the spacing here
6788
03:54:05,680 --> 03:54:08,239
all right if you're a fish then you can
6789
03:54:08,239 --> 03:54:09,120
swim
6790
03:54:09,120 --> 03:54:13,600
def swim let's print
6791
03:54:13,600 --> 03:54:17,680
this fish is swimming
6792
03:54:17,680 --> 03:54:21,040
and lastly we have hawk if you're a hawk
6793
03:54:21,040 --> 03:54:24,880
then you can fly def fly
6794
03:54:24,880 --> 03:54:28,560
let's print this hawk
6795
03:54:28,560 --> 03:54:32,560
is flying like i said before not only
6796
03:54:32,560 --> 03:54:35,040
do these specific types of animals have
6797
03:54:35,040 --> 03:54:35,840
access to
6798
03:54:35,840 --> 03:54:37,680
all of the attributes and methods that
6799
03:54:37,680 --> 03:54:39,040
they inherit from their parents
6800
03:54:39,040 --> 03:54:40,880
but they can have their own attributes
6801
03:54:40,880 --> 03:54:42,800
and methods as well so let's test some
6802
03:54:42,800 --> 03:54:43,680
of these methods
6803
03:54:43,680 --> 03:54:46,160
so we have our rabbit fish and hawk
6804
03:54:46,160 --> 03:54:46,960
objects
6805
03:54:46,960 --> 03:54:51,120
let's have rabbit use its run method
6806
03:54:51,120 --> 03:54:54,560
fish we'll use its swim method and hawk
6807
03:54:54,560 --> 03:54:58,000
will use its fly method
6808
03:54:59,520 --> 03:55:02,160
and here we go this rabbit is running
6809
03:55:02,160 --> 03:55:03,279
this fish is swimming
6810
03:55:03,279 --> 03:55:06,960
and this hawk is flying so in conclusion
6811
03:55:06,960 --> 03:55:09,279
classes can have children the children
6812
03:55:09,279 --> 03:55:11,279
classes will inherit everything that
6813
03:55:11,279 --> 03:55:12,640
their parent class has
6814
03:55:12,640 --> 03:55:14,319
all of the parents attributes and
6815
03:55:14,319 --> 03:55:16,479
methods but not only that the children
6816
03:55:16,479 --> 03:55:17,760
classes can implement
6817
03:55:17,760 --> 03:55:19,840
their own unique attributes and methods
6818
03:55:19,840 --> 03:55:20,880
as well
6819
03:55:20,880 --> 03:55:22,880
so that is inheritance if you would like
6820
03:55:22,880 --> 03:55:23,920
a copy of this code
6821
03:55:23,920 --> 03:55:25,520
i will post all of this in the comment
6822
03:55:25,520 --> 03:55:27,760
section down below but yeah that's the
6823
03:55:27,760 --> 03:55:31,040
basics of inheritance using python
6824
03:55:31,040 --> 03:55:33,040
yo what's going on everybody it's you
6825
03:55:33,040 --> 03:55:34,399
bro hope you're doing well
6826
03:55:34,399 --> 03:55:36,080
and in this video i'm going to explain
6827
03:55:36,080 --> 03:55:38,319
how multi-level inheritance works in
6828
03:55:38,319 --> 03:55:39,279
python so
6829
03:55:39,279 --> 03:55:43,680
sit back relax and enjoy the show
6830
03:55:43,680 --> 03:55:45,760
all right welcome back everybody let's
6831
03:55:45,760 --> 03:55:47,760
talk about multi-level inheritance
6832
03:55:47,760 --> 03:55:50,479
this is a concept where a derived class
6833
03:55:50,479 --> 03:55:52,399
also known as a child class
6834
03:55:52,399 --> 03:55:54,800
inherits from another derived class
6835
03:55:54,800 --> 03:55:56,080
here's an example let's say
6836
03:55:56,080 --> 03:55:58,560
we have a hierarchy of classes there
6837
03:55:58,560 --> 03:56:00,080
will be three levels to this
6838
03:56:00,080 --> 03:56:02,560
we'll create a family tree of living
6839
03:56:02,560 --> 03:56:03,520
organisms
6840
03:56:03,520 --> 03:56:05,359
so at the very top we're going to have a
6841
03:56:05,359 --> 03:56:06,560
class called
6842
03:56:06,560 --> 03:56:09,040
organism any class that inherits from
6843
03:56:09,040 --> 03:56:10,880
the organism parent class
6844
03:56:10,880 --> 03:56:13,439
will receive one attribute one variable
6845
03:56:13,439 --> 03:56:14,479
named alive
6846
03:56:14,479 --> 03:56:16,960
and this will be set to true so if you
6847
03:56:16,960 --> 03:56:19,439
are an organism you begin by being alive
6848
03:56:19,439 --> 03:56:21,359
now let's say we have a child class that
6849
03:56:21,359 --> 03:56:23,359
will inherit from the organism parent
6850
03:56:23,359 --> 03:56:24,080
class
6851
03:56:24,080 --> 03:56:27,199
let's say animal class animal
6852
03:56:27,199 --> 03:56:30,479
inherits from organism so any animal
6853
03:56:30,479 --> 03:56:31,439
that we create
6854
03:56:31,439 --> 03:56:33,600
will have this alive attribute set the
6855
03:56:33,600 --> 03:56:34,560
true
6856
03:56:34,560 --> 03:56:37,680
and all animals eat so let's define a
6857
03:56:37,680 --> 03:56:39,120
function that does that
6858
03:56:39,120 --> 03:56:40,960
at least i believe all animals eat let's
6859
03:56:40,960 --> 03:56:42,239
pretend that they do
6860
03:56:42,239 --> 03:56:46,239
so this animal is eating
6861
03:56:46,239 --> 03:56:48,160
and now why stop there let's create a
6862
03:56:48,160 --> 03:56:50,640
class for a specific type of animal
6863
03:56:50,640 --> 03:56:53,120
i like dogs so let's create a dog class
6864
03:56:53,120 --> 03:56:54,080
class dog
6865
03:56:54,080 --> 03:56:57,279
inherits from the animal class
6866
03:56:57,279 --> 03:56:59,279
and all dogs should be able to bark so
6867
03:56:59,279 --> 03:57:02,640
let's create a function to do that
6868
03:57:04,000 --> 03:57:07,279
and we'll print this dog
6869
03:57:07,279 --> 03:57:10,960
is barking okay so what we just did here
6870
03:57:10,960 --> 03:57:11,359
is
6871
03:57:11,359 --> 03:57:15,040
multi-level inheritance we had a derived
6872
03:57:15,040 --> 03:57:17,680
child class inherit from another child
6873
03:57:17,680 --> 03:57:18,319
class
6874
03:57:18,319 --> 03:57:20,399
so it's kind of like this dog class is
6875
03:57:20,399 --> 03:57:22,800
the child animal class is the parent
6876
03:57:22,800 --> 03:57:25,040
and then the parent of the parent class
6877
03:57:25,040 --> 03:57:25,840
would be like
6878
03:57:25,840 --> 03:57:27,600
a grandparent right and then if you
6879
03:57:27,600 --> 03:57:29,040
added another layer on top of that to
6880
03:57:29,040 --> 03:57:30,000
this hierarchy
6881
03:57:30,000 --> 03:57:31,840
well then the parent of the grandparent
6882
03:57:31,840 --> 03:57:33,520
would be like a great grandparent
6883
03:57:33,520 --> 03:57:35,040
that's how i think of it at least now
6884
03:57:35,040 --> 03:57:37,040
let's create a dog object just to verify
6885
03:57:37,040 --> 03:57:38,800
that everything's working so let's call
6886
03:57:38,800 --> 03:57:39,760
our dog
6887
03:57:39,760 --> 03:57:42,319
dog and our dog object should have
6888
03:57:42,319 --> 03:57:44,960
access to an attribute named alive
6889
03:57:44,960 --> 03:57:47,279
and our dog object receives that
6890
03:57:47,279 --> 03:57:49,760
attribute from the organism class
6891
03:57:49,760 --> 03:57:51,520
and this will print true if i were to
6892
03:57:51,520 --> 03:57:53,840
print whatever this attribute contains
6893
03:57:53,840 --> 03:57:55,680
and our dog should be able to eat
6894
03:57:55,680 --> 03:57:57,840
because it receives this method from the
6895
03:57:57,840 --> 03:58:00,640
animal parent class
6896
03:58:00,640 --> 03:58:02,319
and lastly our dog should be able to
6897
03:58:02,319 --> 03:58:04,080
bark and that method is defined within
6898
03:58:04,080 --> 03:58:06,800
its own class
6899
03:58:07,680 --> 03:58:09,760
so that's how multi-level inheritance
6900
03:58:09,760 --> 03:58:12,560
works it's a concept where a child class
6901
03:58:12,560 --> 03:58:14,960
will inherit from another child class
6902
03:58:14,960 --> 03:58:17,520
i like to think of it like a family tree
6903
03:58:17,520 --> 03:58:20,080
where a child class will have a parent
6904
03:58:20,080 --> 03:58:22,160
and the parent of that parent would be
6905
03:58:22,160 --> 03:58:24,479
like a grandparent basically
6906
03:58:24,479 --> 03:58:26,399
so if you would like a copy of this code
6907
03:58:26,399 --> 03:58:28,160
i will post all of this to the comment
6908
03:58:28,160 --> 03:58:29,359
section down below
6909
03:58:29,359 --> 03:58:31,120
but yeah that is how multi-level
6910
03:58:31,120 --> 03:58:33,279
inheritance works in python
6911
03:58:33,279 --> 03:58:35,199
hey what's going on everybody it's you
6912
03:58:35,199 --> 03:58:36,720
bro hope you're doing well and in this
6913
03:58:36,720 --> 03:58:38,160
video i'm going to show you all how
6914
03:58:38,160 --> 03:58:40,720
multiple inheritance works in python so
6915
03:58:40,720 --> 03:58:41,439
sit back
6916
03:58:41,439 --> 03:58:45,840
relax and enjoy the show
6917
03:58:45,840 --> 03:58:48,319
all right people multiple inheritance
6918
03:58:48,319 --> 03:58:50,479
multiple inheritance is the concept
6919
03:58:50,479 --> 03:58:52,160
where a child class
6920
03:58:52,160 --> 03:58:54,640
is derived from more than one parent
6921
03:58:54,640 --> 03:58:55,279
class
6922
03:58:55,279 --> 03:58:57,199
here's an example let's say that all of
6923
03:58:57,199 --> 03:58:58,960
our children classes are going to be
6924
03:58:58,960 --> 03:59:01,199
certain types of animals and we would
6925
03:59:01,199 --> 03:59:03,120
like our animal classes
6926
03:59:03,120 --> 03:59:05,600
to inherit from either the prepared
6927
03:59:05,600 --> 03:59:06,399
class
6928
03:59:06,399 --> 03:59:08,640
the predator parrot class or both
6929
03:59:08,640 --> 03:59:10,640
depending on what type of animal it is
6930
03:59:10,640 --> 03:59:12,479
because some animals in the animal
6931
03:59:12,479 --> 03:59:14,319
kingdom can be both prey
6932
03:59:14,319 --> 03:59:16,560
and predators and one example that comes
6933
03:59:16,560 --> 03:59:17,920
to mind would be fish
6934
03:59:17,920 --> 03:59:20,239
fish will eat smaller fish but also can
6935
03:59:20,239 --> 03:59:21,120
get eaten by
6936
03:59:21,120 --> 03:59:23,760
larger fish so here we have two classes
6937
03:59:23,760 --> 03:59:25,040
prey and predator
6938
03:59:25,040 --> 03:59:27,680
each has a dedicated method if a class
6939
03:59:27,680 --> 03:59:28,560
inherits from
6940
03:59:28,560 --> 03:59:30,560
the prey parent class they will have
6941
03:59:30,560 --> 03:59:32,239
access to a flea method
6942
03:59:32,239 --> 03:59:35,120
which will print this animal please if a
6943
03:59:35,120 --> 03:59:37,040
class inherits from the predator parent
6944
03:59:37,040 --> 03:59:37,760
class
6945
03:59:37,760 --> 03:59:39,920
that class will have access to a hunt
6946
03:59:39,920 --> 03:59:41,680
method that will print
6947
03:59:41,680 --> 03:59:43,920
this animal is hunting and it's possible
6948
03:59:43,920 --> 03:59:45,600
that some animals can do both
6949
03:59:45,600 --> 03:59:47,920
they can both flee and hunt but they'll
6950
03:59:47,920 --> 03:59:48,800
need to inherit
6951
03:59:48,800 --> 03:59:50,720
both of these classes both prey and
6952
03:59:50,720 --> 03:59:52,479
predator and that's where multiple
6953
03:59:52,479 --> 03:59:53,680
inheritance comes in
6954
03:59:53,680 --> 03:59:55,520
now let's create some classes based on
6955
03:59:55,520 --> 03:59:56,720
certain types of animals
6956
03:59:56,720 --> 03:59:59,279
let's say we have class rabbit and
6957
03:59:59,279 --> 04:00:01,520
rabbit we'll inherit from the prey class
6958
04:00:01,520 --> 04:00:02,080
because
6959
04:00:02,080 --> 04:00:03,920
rabbits are typically prey and not
6960
04:00:03,920 --> 04:00:05,600
predators but i don't know there could
6961
04:00:05,600 --> 04:00:07,199
be some killer rabbits out there
6962
04:00:07,199 --> 04:00:09,439
haven't run into any yet let's have
6963
04:00:09,439 --> 04:00:12,319
class hawk inherit from the
6964
04:00:12,319 --> 04:00:16,080
predator class and lastly fish
6965
04:00:16,080 --> 04:00:18,720
and fish can be both prey and predators
6966
04:00:18,720 --> 04:00:20,800
so they will inherit from both classes
6967
04:00:20,800 --> 04:00:22,640
and we just separate each class with a
6968
04:00:22,640 --> 04:00:24,080
comma so prey
6969
04:00:24,080 --> 04:00:26,880
comma predator and it's as simple as
6970
04:00:26,880 --> 04:00:27,840
that
6971
04:00:27,840 --> 04:00:29,600
now let's create an object from each of
6972
04:00:29,600 --> 04:00:31,680
these classes we have rabbit
6973
04:00:31,680 --> 04:00:36,000
equals rabbit hawk
6974
04:00:36,000 --> 04:00:39,279
equals hawk and fish
6975
04:00:39,279 --> 04:00:42,960
equals fish now let's just verify
6976
04:00:42,960 --> 04:00:44,640
all of the different methods that each
6977
04:00:44,640 --> 04:00:46,239
of these objects inherited
6978
04:00:46,239 --> 04:00:48,640
so our rabbit should have access to a
6979
04:00:48,640 --> 04:00:49,439
flea method
6980
04:00:49,439 --> 04:00:52,800
but no hunt method and let's just test
6981
04:00:52,800 --> 04:00:53,279
that
6982
04:00:53,279 --> 04:00:57,279
yep this animal flees and my hawk object
6983
04:00:57,279 --> 04:00:59,520
should have access to a hunt method but
6984
04:00:59,520 --> 04:01:01,840
no flea method
6985
04:01:01,840 --> 04:01:04,960
this animal is hunting and lastly
6986
04:01:04,960 --> 04:01:08,239
my fish object has access to both a flea
6987
04:01:08,239 --> 04:01:10,399
and a hunt method and if you take a look
6988
04:01:10,399 --> 04:01:11,920
on the right hand side of this pop-up
6989
04:01:11,920 --> 04:01:13,680
window it shows the class that it
6990
04:01:13,680 --> 04:01:15,520
inherited this method from
6991
04:01:15,520 --> 04:01:17,760
so fleet comes from the parent class of
6992
04:01:17,760 --> 04:01:19,920
prey and hunt comes from the apparent
6993
04:01:19,920 --> 04:01:21,279
class of predator
6994
04:01:21,279 --> 04:01:24,160
so my fish can both flee and hunt
6995
04:01:24,160 --> 04:01:26,319
because it's typically considered both
6996
04:01:26,319 --> 04:01:29,359
prey and predators
6997
04:01:29,359 --> 04:01:31,920
this animal flees this animal is hunting
6998
04:01:31,920 --> 04:01:33,760
so that's basically what multiple
6999
04:01:33,760 --> 04:01:34,720
inheritance is
7000
04:01:34,720 --> 04:01:37,520
it's a concept where a child class is
7001
04:01:37,520 --> 04:01:40,000
derived from more than one parent class
7002
04:01:40,000 --> 04:01:42,160
so that's basically multiple inheritance
7003
04:01:42,160 --> 04:01:43,840
i'll post all of this code to the
7004
04:01:43,840 --> 04:01:45,359
comment section down below
7005
04:01:45,359 --> 04:01:47,279
and well yeah that's how multiple
7006
04:01:47,279 --> 04:01:48,479
inheritance works
7007
04:01:48,479 --> 04:01:51,439
in python hey what's going on everybody
7008
04:01:51,439 --> 04:01:53,120
it's bro hope you're doing well
7009
04:01:53,120 --> 04:01:54,479
and in this video i'm going to teach you
7010
04:01:54,479 --> 04:01:56,640
guys about method overriding in python
7011
04:01:56,640 --> 04:01:57,040
so
7012
04:01:57,040 --> 04:02:01,680
sit back relax and enjoy the show
7013
04:02:01,680 --> 04:02:03,760
here's a super quick video on method
7014
04:02:03,760 --> 04:02:06,000
overriding i have two classes a class
7015
04:02:06,000 --> 04:02:08,000
called animal and a class called rabbit
7016
04:02:08,000 --> 04:02:09,760
rabbit inherits from the animal class
7017
04:02:09,760 --> 04:02:11,600
therefore rabbit is the child class
7018
04:02:11,600 --> 04:02:13,600
an animal is the parent class within the
7019
04:02:13,600 --> 04:02:15,760
animal class there is one method a
7020
04:02:15,760 --> 04:02:16,960
method called eat
7021
04:02:16,960 --> 04:02:18,720
and when you call this method all this
7022
04:02:18,720 --> 04:02:21,040
will do is print this animal is eating
7023
04:02:21,040 --> 04:02:22,560
the rabbit class inherits from the
7024
04:02:22,560 --> 04:02:24,080
animal class therefore
7025
04:02:24,080 --> 04:02:26,720
rabbit has access to this eat method and
7026
04:02:26,720 --> 04:02:28,720
if i were to create a rabbit object
7027
04:02:28,720 --> 04:02:31,040
and this rabbit used its eat method well
7028
04:02:31,040 --> 04:02:32,720
then this is going to print
7029
04:02:32,720 --> 04:02:35,199
this animal is eating now method of
7030
04:02:35,199 --> 04:02:37,199
writing is the ability of an object
7031
04:02:37,199 --> 04:02:38,800
oriented programming language
7032
04:02:38,800 --> 04:02:41,760
to allow a subclass also known as a
7033
04:02:41,760 --> 04:02:42,800
child class
7034
04:02:42,800 --> 04:02:45,439
to provide a specific implementation of
7035
04:02:45,439 --> 04:02:46,080
a method
7036
04:02:46,080 --> 04:02:48,319
that is already provided by one of its
7037
04:02:48,319 --> 04:02:50,080
parents in this case we're going to
7038
04:02:50,080 --> 04:02:50,640
override
7039
04:02:50,640 --> 04:02:52,640
the eat method and what we can do is
7040
04:02:52,640 --> 04:02:54,239
provide a more specific
7041
04:02:54,239 --> 04:02:57,040
implementation for the rabbit class and
7042
04:02:57,040 --> 04:02:59,040
now to override a method what we'll do
7043
04:02:59,040 --> 04:02:59,920
is that within
7044
04:02:59,920 --> 04:03:02,000
the child class we need to define a
7045
04:03:02,000 --> 04:03:03,279
method with the same
7046
04:03:03,279 --> 04:03:05,439
matching method signature that is the
7047
04:03:05,439 --> 04:03:07,359
combination of a method's name
7048
04:03:07,359 --> 04:03:09,120
plus its parameters both of these
7049
04:03:09,120 --> 04:03:10,640
together are known as a
7050
04:03:10,640 --> 04:03:13,040
method signature within the child class
7051
04:03:13,040 --> 04:03:14,000
we will define
7052
04:03:14,000 --> 04:03:16,000
and eat method with the same parameters
7053
04:03:16,000 --> 04:03:17,359
def eat
7054
04:03:17,359 --> 04:03:19,359
and in this case the only parameter is
7055
04:03:19,359 --> 04:03:21,199
self just to keep this simple
7056
04:03:21,199 --> 04:03:23,279
and what we'll do is that will provide a
7057
04:03:23,279 --> 04:03:24,319
more specific
7058
04:03:24,319 --> 04:03:26,319
implementation of the eat method
7059
04:03:26,319 --> 04:03:28,000
specifically for rabbits
7060
04:03:28,000 --> 04:03:29,760
so let's print something more closely
7061
04:03:29,760 --> 04:03:32,080
associated with rabbits such as
7062
04:03:32,080 --> 04:03:35,279
this rabbit is eating a
7063
04:03:35,279 --> 04:03:37,680
carrot now if i were to run this program
7064
04:03:37,680 --> 04:03:38,319
again
7065
04:03:38,319 --> 04:03:40,800
we will instead use this implementation
7066
04:03:40,800 --> 04:03:41,439
of the
7067
04:03:41,439 --> 04:03:42,960
method instead of the one that it
7068
04:03:42,960 --> 04:03:44,880
inherits from its parent class
7069
04:03:44,880 --> 04:03:47,359
of animal basically speaking an object
7070
04:03:47,359 --> 04:03:49,680
will use a method that is more closely
7071
04:03:49,680 --> 04:03:51,920
associated with itself first before
7072
04:03:51,920 --> 04:03:54,080
relying on a method that it may inherit
7073
04:03:54,080 --> 04:03:55,600
from a parent class
7074
04:03:55,600 --> 04:03:57,760
in this example we're using this version
7075
04:03:57,760 --> 04:03:58,800
of the eat method
7076
04:03:58,800 --> 04:04:00,880
specifically for rabbits instead of the
7077
04:04:00,880 --> 04:04:02,800
version that we inherit from the parent
7078
04:04:02,800 --> 04:04:03,359
class
7079
04:04:03,359 --> 04:04:05,760
of animal that is the basics of method
7080
04:04:05,760 --> 04:04:07,760
overriding i will post this code in the
7081
04:04:07,760 --> 04:04:09,279
comment section down below
7082
04:04:09,279 --> 04:04:11,680
and well yeah that's how to override a
7083
04:04:11,680 --> 04:04:14,080
method using python
7084
04:04:14,080 --> 04:04:16,080
hey what's going on everybody it's you
7085
04:04:16,080 --> 04:04:17,359
bro hope you're doing well
7086
04:04:17,359 --> 04:04:18,800
and in this video i'm going to teach you
7087
04:04:18,800 --> 04:04:21,199
guys all about method chaining in python
7088
04:04:21,199 --> 04:04:21,600
so
7089
04:04:21,600 --> 04:04:26,239
sit back relax and enjoy the show
7090
04:04:26,479 --> 04:04:28,160
all right everybody let's talk about
7091
04:04:28,160 --> 04:04:30,239
method training method chaining is used
7092
04:04:30,239 --> 04:04:30,960
to call
7093
04:04:30,960 --> 04:04:33,680
multiple methods sequentially and each
7094
04:04:33,680 --> 04:04:35,520
call performs an action on the same
7095
04:04:35,520 --> 04:04:36,160
object
7096
04:04:36,160 --> 04:04:38,560
and return self here's an example of how
7097
04:04:38,560 --> 04:04:40,399
method chaining would be useful
7098
04:04:40,399 --> 04:04:43,120
let's say we have a class named car car
7099
04:04:43,120 --> 04:04:43,520
has
7100
04:04:43,520 --> 04:04:46,560
four methods turn on drive break and
7101
04:04:46,560 --> 04:04:47,199
turn off
7102
04:04:47,199 --> 04:04:48,800
they will all just print a simple
7103
04:04:48,800 --> 04:04:50,720
message beginning with turn on
7104
04:04:50,720 --> 04:04:53,040
turn on what print you start the engine
7105
04:04:53,040 --> 04:04:55,040
drive will print you drive the car
7106
04:04:55,040 --> 04:04:56,960
brake will print you step on the brakes
7107
04:04:56,960 --> 04:04:58,239
and turn off will print
7108
04:04:58,239 --> 04:05:00,160
you turn off the engine let's say we
7109
04:05:00,160 --> 04:05:01,279
have a car object
7110
04:05:01,279 --> 04:05:04,239
car equals car and i would like my car
7111
04:05:04,239 --> 04:05:04,880
object
7112
04:05:04,880 --> 04:05:07,680
to use its turn on method immediately
7113
04:05:07,680 --> 04:05:08,479
followed by
7114
04:05:08,479 --> 04:05:10,319
the drive method with how we've been
7115
04:05:10,319 --> 04:05:12,319
coding things previously we may write
7116
04:05:12,319 --> 04:05:13,359
something such as
7117
04:05:13,359 --> 04:05:15,439
the name of the object in this case car
7118
04:05:15,439 --> 04:05:17,199
dot and then the method to use
7119
04:05:17,199 --> 04:05:19,359
i would like my card to use the turn on
7120
04:05:19,359 --> 04:05:21,040
method followed by
7121
04:05:21,040 --> 04:05:23,920
car dot drive so currently this will
7122
04:05:23,920 --> 04:05:25,520
take two lines of code
7123
04:05:25,520 --> 04:05:27,760
but this will print you start the engine
7124
04:05:27,760 --> 04:05:29,359
and you drive the car
7125
04:05:29,359 --> 04:05:31,760
now with method training we can call
7126
04:05:31,760 --> 04:05:32,880
multiple methods
7127
04:05:32,880 --> 04:05:34,960
sequentially now the format on this is
7128
04:05:34,960 --> 04:05:36,319
going to be a little bit different
7129
04:05:36,319 --> 04:05:38,000
and if we were to write the same code
7130
04:05:38,000 --> 04:05:39,840
using method chaining it might look a
7131
04:05:39,840 --> 04:05:40,960
little something like this
7132
04:05:40,960 --> 04:05:43,760
after the first method call we would add
7133
04:05:43,760 --> 04:05:44,319
dot
7134
04:05:44,319 --> 04:05:46,080
and then an additional method that we
7135
04:05:46,080 --> 04:05:48,080
would like to call so for example
7136
04:05:48,080 --> 04:05:50,399
after the turn on method we'll
7137
04:05:50,399 --> 04:05:51,359
immediately call
7138
04:05:51,359 --> 04:05:53,279
the drive method but there's one more
7139
04:05:53,279 --> 04:05:54,399
thing that we need to add when we're
7140
04:05:54,399 --> 04:05:56,399
using python to do method training
7141
04:05:56,399 --> 04:05:59,120
so normally this by itself will not work
7142
04:05:59,120 --> 04:06:01,279
when we call a method in python if there
7143
04:06:01,279 --> 04:06:03,279
is nothing that is returned python will
7144
04:06:03,279 --> 04:06:03,920
return
7145
04:06:03,920 --> 04:06:06,479
none so as you can see here attribute
7146
04:06:06,479 --> 04:06:07,600
error none type
7147
04:06:07,600 --> 04:06:10,319
object has no attribute of drive so we
7148
04:06:10,319 --> 04:06:12,720
need to return itself under each method
7149
04:06:12,720 --> 04:06:14,560
that we're using method training with
7150
04:06:14,560 --> 04:06:16,479
so let's add return self to each of
7151
04:06:16,479 --> 04:06:17,680
these methods
7152
04:06:17,680 --> 04:06:20,720
so let's do that for drive
7153
04:06:20,720 --> 04:06:24,160
break and the turn off method and this
7154
04:06:24,160 --> 04:06:26,800
should work now you start the engine
7155
04:06:26,800 --> 04:06:28,640
you drive the car so think of it like
7156
04:06:28,640 --> 04:06:30,319
this after we finish
7157
04:06:30,319 --> 04:06:32,720
calling this method python is going to
7158
04:06:32,720 --> 04:06:33,680
return self
7159
04:06:33,680 --> 04:06:35,680
so it's going to return car so then
7160
04:06:35,680 --> 04:06:37,040
we'll immediately use
7161
04:06:37,040 --> 04:06:39,680
car.drive now here's another example
7162
04:06:39,680 --> 04:06:41,120
let's say i would like to
7163
04:06:41,120 --> 04:06:43,600
call the break method followed by the
7164
04:06:43,600 --> 04:06:44,640
turn off method
7165
04:06:44,640 --> 04:06:47,600
so that would be car dot break
7166
04:06:47,600 --> 04:06:48,399
parentheses
7167
04:06:48,399 --> 04:06:51,760
followed by dot turn
7168
04:06:51,760 --> 04:06:55,040
off so this will print you step on the
7169
04:06:55,040 --> 04:06:55,520
brakes
7170
04:06:55,520 --> 04:06:57,439
you turn off the engine now here's an
7171
04:06:57,439 --> 04:06:59,120
extreme example let's say we would like
7172
04:06:59,120 --> 04:07:01,439
to call all four methods in order
7173
04:07:01,439 --> 04:07:04,479
so that would be car dot
7174
04:07:04,479 --> 04:07:08,720
turn on followed by dot drive
7175
04:07:08,720 --> 04:07:11,840
dot break dot
7176
04:07:11,840 --> 04:07:15,760
turn off so this will call
7177
04:07:15,760 --> 04:07:17,760
all four methods in order starting with
7178
04:07:17,760 --> 04:07:19,439
the leftmost method
7179
04:07:19,439 --> 04:07:21,680
you start the engine you drive the car
7180
04:07:21,680 --> 04:07:22,880
you step on the brakes
7181
04:07:22,880 --> 04:07:25,199
you turn off the engine now if you have
7182
04:07:25,199 --> 04:07:27,120
one long method chain like this
7183
04:07:27,120 --> 04:07:28,880
it might become difficult to read after
7184
04:07:28,880 --> 04:07:30,640
a while so what i would recommend
7185
04:07:30,640 --> 04:07:32,720
if you're doing a lot of method chaining
7186
04:07:32,720 --> 04:07:34,640
is that after each method call
7187
04:07:34,640 --> 04:07:36,640
just hit enter to move each method call
7188
04:07:36,640 --> 04:07:38,159
down to a new line
7189
04:07:38,159 --> 04:07:40,239
so it'll look something like that and
7190
04:07:40,239 --> 04:07:41,199
this backslash
7191
04:07:41,199 --> 04:07:43,199
might be inserted this is a line
7192
04:07:43,199 --> 04:07:44,880
continuation character
7193
04:07:44,880 --> 04:07:47,279
so this is a little more readable but
7194
04:07:47,279 --> 04:07:48,640
you know it still does the same thing
7195
04:07:48,640 --> 04:07:49,600
basically
7196
04:07:49,600 --> 04:07:51,439
all right everybody so that is method
7197
04:07:51,439 --> 04:07:53,040
training it's used to call
7198
04:07:53,040 --> 04:07:55,600
multiple methods sequentially and each
7199
04:07:55,600 --> 04:07:56,479
call performs an
7200
04:07:56,479 --> 04:07:58,560
action on the same object and returns
7201
04:07:58,560 --> 04:08:00,479
self if you would like a copy of this
7202
04:08:00,479 --> 04:08:02,159
code i will post all of this in the
7203
04:08:02,159 --> 04:08:04,479
comments section down below but yeah
7204
04:08:04,479 --> 04:08:06,720
that is how to do a method chaining in
7205
04:08:06,720 --> 04:08:08,319
python
7206
04:08:08,319 --> 04:08:10,720
hey what's going on everybody it's bro
7207
04:08:10,720 --> 04:08:11,840
hope you're doing well
7208
04:08:11,840 --> 04:08:13,359
and in this video i'm going to explain
7209
04:08:13,359 --> 04:08:15,359
the super function in python so
7210
04:08:15,359 --> 04:08:19,840
sit back relax and enjoy the show
7211
04:08:20,159 --> 04:08:21,760
well then since you clicked on this
7212
04:08:21,760 --> 04:08:23,359
video i should probably discuss
7213
04:08:23,359 --> 04:08:25,279
the super function now the super
7214
04:08:25,279 --> 04:08:26,960
function is used to give access
7215
04:08:26,960 --> 04:08:29,840
to the methods of a parent class it
7216
04:08:29,840 --> 04:08:32,000
returns a temporary object
7217
04:08:32,000 --> 04:08:34,560
of a parent class when used here's an
7218
04:08:34,560 --> 04:08:36,560
example of why the super function would
7219
04:08:36,560 --> 04:08:37,439
be useful
7220
04:08:37,439 --> 04:08:38,960
let's take this program that i've
7221
04:08:38,960 --> 04:08:40,800
written there's three classes
7222
04:08:40,800 --> 04:08:43,040
a class called rectangle which is the
7223
04:08:43,040 --> 04:08:43,920
parent class
7224
04:08:43,920 --> 04:08:47,279
to two children classes square and cube
7225
04:08:47,279 --> 04:08:49,199
and all we're doing is creating a square
7226
04:08:49,199 --> 04:08:50,960
object and a cube object
7227
04:08:50,960 --> 04:08:53,040
but we need to pass in a length and a
7228
04:08:53,040 --> 04:08:54,479
width for squares
7229
04:08:54,479 --> 04:08:57,600
and a length width and height for cubes
7230
04:08:57,600 --> 04:08:59,520
now with programming we don't like to
7231
04:08:59,520 --> 04:09:01,840
repeat code we like to reuse code and if
7232
04:09:01,840 --> 04:09:04,080
you take a look within my knit methods
7233
04:09:04,080 --> 04:09:06,880
for both square and cube we're repeating
7234
04:09:06,880 --> 04:09:08,640
self.length equals length
7235
04:09:08,640 --> 04:09:11,760
and self.width equals width twice for
7236
04:09:11,760 --> 04:09:12,960
our init methods
7237
04:09:12,960 --> 04:09:14,720
so one thing that we can do any
7238
04:09:14,720 --> 04:09:16,000
similarities between
7239
04:09:16,000 --> 04:09:18,319
the square and cube class we can place
7240
04:09:18,319 --> 04:09:19,120
within
7241
04:09:19,120 --> 04:09:21,520
the rectangle class and then reuse this
7242
04:09:21,520 --> 04:09:23,439
code so what i'm going to do
7243
04:09:23,439 --> 04:09:26,000
is copy this indent method within our
7244
04:09:26,000 --> 04:09:27,040
square class
7245
04:09:27,040 --> 04:09:29,920
and paste it within our rectangle parent
7246
04:09:29,920 --> 04:09:30,800
class
7247
04:09:30,800 --> 04:09:33,439
and then in order to access this init
7248
04:09:33,439 --> 04:09:33,920
method
7249
04:09:33,920 --> 04:09:36,720
we can use the super function so we no
7250
04:09:36,720 --> 04:09:37,520
longer need
7251
04:09:37,520 --> 04:09:39,279
these two lines of code what i'm going
7252
04:09:39,279 --> 04:09:40,640
to do is ask
7253
04:09:40,640 --> 04:09:42,720
the rectangle class to use its init
7254
04:09:42,720 --> 04:09:44,720
method so we're going to type
7255
04:09:44,720 --> 04:09:48,000
super parentheses dot and then the name
7256
04:09:48,000 --> 04:09:49,760
or the function of the parent class
7257
04:09:49,760 --> 04:09:51,439
that we would like to use and i would
7258
04:09:51,439 --> 04:09:53,199
like to use the init method
7259
04:09:53,199 --> 04:09:55,359
but i need to send a length and a width
7260
04:09:55,359 --> 04:09:56,880
because it requires that
7261
04:09:56,880 --> 04:09:58,560
so let's send these so we're going to
7262
04:09:58,560 --> 04:10:00,159
pass in our length and
7263
04:10:00,159 --> 04:10:01,920
our width and now we can do the same
7264
04:10:01,920 --> 04:10:03,920
thing for our cube init method
7265
04:10:03,920 --> 04:10:06,159
so i will copy this line of code we no
7266
04:10:06,159 --> 04:10:08,080
longer need these two lines of code
7267
04:10:08,080 --> 04:10:10,479
because we will be reusing our init
7268
04:10:10,479 --> 04:10:12,319
method within the parent class
7269
04:10:12,319 --> 04:10:14,800
of rectangle but we'll still need to
7270
04:10:14,800 --> 04:10:16,960
keep this line in to assign the height
7271
04:10:16,960 --> 04:10:19,279
because that's a difference between our
7272
04:10:19,279 --> 04:10:20,880
square and cube class
7273
04:10:20,880 --> 04:10:23,120
and now we can just reuse this init
7274
04:10:23,120 --> 04:10:24,000
method within
7275
04:10:24,000 --> 04:10:26,080
the rectangle parent class now i'm
7276
04:10:26,080 --> 04:10:27,760
thinking that we should probably test to
7277
04:10:27,760 --> 04:10:28,399
see if
7278
04:10:28,399 --> 04:10:30,239
length and width are assigned to these
7279
04:10:30,239 --> 04:10:31,840
attributes and one way
7280
04:10:31,840 --> 04:10:33,439
that i think would be fun would be to
7281
04:10:33,439 --> 04:10:35,120
create an area method
7282
04:10:35,120 --> 04:10:37,199
for squares and a volume method for
7283
04:10:37,199 --> 04:10:38,960
cubes so let's define
7284
04:10:38,960 --> 04:10:41,359
an area method within the square class
7285
04:10:41,359 --> 04:10:42,560
def
7286
04:10:42,560 --> 04:10:46,319
area and i would like to return self
7287
04:10:46,319 --> 04:10:49,520
dot length times self
7288
04:10:49,520 --> 04:10:52,640
dot width and we can create a volume
7289
04:10:52,640 --> 04:10:54,239
method within cubes
7290
04:10:54,239 --> 04:10:57,359
so let's do that def let's change this
7291
04:10:57,359 --> 04:10:58,399
to volume
7292
04:10:58,399 --> 04:11:00,960
we'll multiply it length times width
7293
04:11:00,960 --> 04:11:02,319
times height
7294
04:11:02,319 --> 04:11:05,359
but we need to type self dot pipe
7295
04:11:05,359 --> 04:11:07,920
and we can call these methods to get the
7296
04:11:07,920 --> 04:11:08,880
area
7297
04:11:08,880 --> 04:11:11,760
of a square or the volume of a cube
7298
04:11:11,760 --> 04:11:13,680
let's print
7299
04:11:13,680 --> 04:11:17,600
square dot area
7300
04:11:17,600 --> 04:11:21,439
and then cube dot
7301
04:11:21,439 --> 04:11:25,120
volume so if our init method
7302
04:11:25,120 --> 04:11:27,680
is actually being used we should have a
7303
04:11:27,680 --> 04:11:28,720
length and the width
7304
04:11:28,720 --> 04:11:31,920
assigned to these attributes and it
7305
04:11:31,920 --> 04:11:33,040
looks like we do
7306
04:11:33,040 --> 04:11:35,439
in conclusion the super function is used
7307
04:11:35,439 --> 04:11:37,520
to give access to the methods of a
7308
04:11:37,520 --> 04:11:38,560
parent class
7309
04:11:38,560 --> 04:11:40,960
it returns a temporary object of a
7310
04:11:40,960 --> 04:11:43,040
parent class when used in order to
7311
04:11:43,040 --> 04:11:43,600
access
7312
04:11:43,600 --> 04:11:46,080
the methods of the parent class also
7313
04:11:46,080 --> 04:11:46,880
known as
7314
04:11:46,880 --> 04:11:49,920
a super class and in this example within
7315
04:11:49,920 --> 04:11:52,560
the init methods of both square and cube
7316
04:11:52,560 --> 04:11:55,040
we immediately called the init method of
7317
04:11:55,040 --> 04:11:56,159
the parent class
7318
04:11:56,159 --> 04:11:58,560
to pass in some arguments that both of
7319
04:11:58,560 --> 04:11:59,520
these classes
7320
04:11:59,520 --> 04:12:01,600
have in common so if you would like a
7321
04:12:01,600 --> 04:12:03,520
copy of this code i will post all of
7322
04:12:03,520 --> 04:12:05,520
this in the comment section down below
7323
04:12:05,520 --> 04:12:07,760
but yeah that is how the super function
7324
04:12:07,760 --> 04:12:09,840
works in python
7325
04:12:09,840 --> 04:12:11,920
hey what's going on everybody it's bro
7326
04:12:11,920 --> 04:12:12,880
hope you're doing well
7327
04:12:12,880 --> 04:12:14,479
and in this video i'm going to explain
7328
04:12:14,479 --> 04:12:17,279
how abstract classes work in python so
7329
04:12:17,279 --> 04:12:21,920
sit back relax and enjoy the show
7330
04:12:21,920 --> 04:12:23,600
hey what's good everybody let's talk
7331
04:12:23,600 --> 04:12:25,040
about abstract classes
7332
04:12:25,040 --> 04:12:27,760
now abstract classes prevent a user from
7333
04:12:27,760 --> 04:12:30,080
creating an object of that class
7334
04:12:30,080 --> 04:12:32,479
think of an abstract class more as a
7335
04:12:32,479 --> 04:12:33,359
template
7336
04:12:33,359 --> 04:12:35,840
it's an idea it's not real it's like a
7337
04:12:35,840 --> 04:12:37,439
ghost class basically
7338
04:12:37,439 --> 04:12:40,560
plus an abstract class compels a user to
7339
04:12:40,560 --> 04:12:42,960
override any abstract methods within a
7340
04:12:42,960 --> 04:12:44,000
child class
7341
04:12:44,000 --> 04:12:45,680
so it's also a form of checks and
7342
04:12:45,680 --> 04:12:47,279
balances as well which we'll discuss
7343
04:12:47,279 --> 04:12:48,000
later
7344
04:12:48,000 --> 04:12:49,680
now here's a small program that i've
7345
04:12:49,680 --> 04:12:51,359
written let's say we have
7346
04:12:51,359 --> 04:12:53,680
three classes class vehicle car and
7347
04:12:53,680 --> 04:12:54,800
motorcycle
7348
04:12:54,800 --> 04:12:57,040
car and motorcycle are the children
7349
04:12:57,040 --> 04:12:59,199
classes of the vehicle class
7350
04:12:59,199 --> 04:13:01,520
now within this vehicle class we have a
7351
04:13:01,520 --> 04:13:02,479
go method
7352
04:13:02,479 --> 04:13:04,319
but i'm currently only defining it not
7353
04:13:04,319 --> 04:13:05,760
actually implementing it so i'm just
7354
04:13:05,760 --> 04:13:06,880
writing pass
7355
04:13:06,880 --> 04:13:09,199
now within car and motorcycle we're
7356
04:13:09,199 --> 04:13:10,880
overriding this method and creating our
7357
04:13:10,880 --> 04:13:12,880
own implementation of the skull method
7358
04:13:12,880 --> 04:13:13,760
that we inherit
7359
04:13:13,760 --> 04:13:16,080
the go method for the car will print you
7360
04:13:16,080 --> 04:13:16,960
drive the car
7361
04:13:16,960 --> 04:13:18,479
and the go method for the motorcycle
7362
04:13:18,479 --> 04:13:20,960
class will print you ride the motorcycle
7363
04:13:20,960 --> 04:13:22,880
now currently i'm creating an object of
7364
04:13:22,880 --> 04:13:24,000
each of these classes
7365
04:13:24,000 --> 04:13:26,640
i have a generic vehicle object car
7366
04:13:26,640 --> 04:13:27,120
object
7367
04:13:27,120 --> 04:13:29,439
and motorcycle object now after running
7368
04:13:29,439 --> 04:13:30,319
this program
7369
04:13:30,319 --> 04:13:32,000
currently the go method within our
7370
04:13:32,000 --> 04:13:34,239
vehicle class is not being implemented
7371
04:13:34,239 --> 04:13:36,560
so this won't print anything but the go
7372
04:13:36,560 --> 04:13:38,800
method within our car class will print
7373
04:13:38,800 --> 04:13:41,040
you drive the car and the go method
7374
04:13:41,040 --> 04:13:43,199
within our motorcycle class will print
7375
04:13:43,199 --> 04:13:45,279
you ride the motorcycle with that being
7376
04:13:45,279 --> 04:13:47,279
said let's pretend that we're coding the
7377
04:13:47,279 --> 04:13:47,680
next
7378
04:13:47,680 --> 04:13:49,600
need for speed game and we would like a
7379
04:13:49,600 --> 04:13:52,239
user to create an object from a specific
7380
04:13:52,239 --> 04:13:54,640
kind of vehicle whether it's a car from
7381
04:13:54,640 --> 04:13:56,560
the car class or a motorcycle from the
7382
04:13:56,560 --> 04:13:57,840
motorcycle class
7383
04:13:57,840 --> 04:13:59,520
we would like to prevent a user from
7384
04:13:59,520 --> 04:14:02,159
creating an object of the vehicle class
7385
04:14:02,159 --> 04:14:04,640
because the vehicle class is too generic
7386
04:14:04,640 --> 04:14:05,840
we do not have all of the
7387
04:14:05,840 --> 04:14:06,800
implementations
7388
04:14:06,800 --> 04:14:09,359
set up for a vehicle it would be like
7389
04:14:09,359 --> 04:14:11,359
somebody unlocked the ghost car or the
7390
04:14:11,359 --> 04:14:12,399
invisible car
7391
04:14:12,399 --> 04:14:13,920
which doesn't actually exist and is
7392
04:14:13,920 --> 04:14:16,319
missing a lot of features we need a user
7393
04:14:16,319 --> 04:14:17,520
to create an object
7394
04:14:17,520 --> 04:14:19,760
from a child class because these are
7395
04:14:19,760 --> 04:14:21,760
fully fleshed out and one way in which
7396
04:14:21,760 --> 04:14:23,760
we can prevent a user from creating an
7397
04:14:23,760 --> 04:14:25,040
object of this class
7398
04:14:25,040 --> 04:14:27,040
as well to turn this class into an
7399
04:14:27,040 --> 04:14:28,159
abstract class
7400
04:14:28,159 --> 04:14:30,159
which is what this video is all about go
7401
04:14:30,159 --> 04:14:32,239
figure now the first step to creating an
7402
04:14:32,239 --> 04:14:34,159
abstract class is that we'll need some
7403
04:14:34,159 --> 04:14:34,800
imports
7404
04:14:34,800 --> 04:14:37,120
so at the top of your program write this
7405
04:14:37,120 --> 04:14:37,840
from
7406
04:14:37,840 --> 04:14:41,279
abc abc is an acronym for abstract
7407
04:14:41,279 --> 04:14:44,560
based class abc from abc
7408
04:14:44,560 --> 04:14:48,399
import abc all capital comma
7409
04:14:48,399 --> 04:14:51,520
abstract method all right
7410
04:14:51,520 --> 04:14:53,760
now with our vehicle class the class
7411
04:14:53,760 --> 04:14:56,000
that you intend to be the abstract class
7412
04:14:56,000 --> 04:14:58,239
add us out of parentheses and our
7413
04:14:58,239 --> 04:14:59,040
vehicle class
7414
04:14:59,040 --> 04:15:02,080
will inherit from the abc class
7415
04:15:02,080 --> 04:15:04,000
and now with any methods within your
7416
04:15:04,000 --> 04:15:05,920
vehicle class you're going to add
7417
04:15:05,920 --> 04:15:09,920
this decorator at the top at abstract
7418
04:15:09,920 --> 04:15:12,159
method all right we should now be
7419
04:15:12,159 --> 04:15:13,680
prevented from creating a
7420
04:15:13,680 --> 04:15:16,239
vehicle object our vehicle class is now
7421
04:15:16,239 --> 04:15:17,600
an abstract class
7422
04:15:17,600 --> 04:15:19,600
and we cannot give it a physical form a
7423
04:15:19,600 --> 04:15:21,040
physical manifestation
7424
04:15:21,040 --> 04:15:22,640
and if we attempt to you can see here
7425
04:15:22,640 --> 04:15:24,319
that we have a type error
7426
04:15:24,319 --> 04:15:26,800
can't instantiate abstract class from
7427
04:15:26,800 --> 04:15:28,800
vehicle with abstract methods
7428
04:15:28,800 --> 04:15:31,680
go so an abstract class is a class which
7429
04:15:31,680 --> 04:15:32,640
contains
7430
04:15:32,640 --> 04:15:35,359
one or more abstract methods and an
7431
04:15:35,359 --> 04:15:36,319
abstract method
7432
04:15:36,319 --> 04:15:38,960
is a method that has a declaration but
7433
04:15:38,960 --> 04:15:40,960
does not have an implementation
7434
04:15:40,960 --> 04:15:43,359
now by definition an abstract class
7435
04:15:43,359 --> 04:15:44,000
contains
7436
04:15:44,000 --> 04:15:47,040
one or more abstract methods if i remove
7437
04:15:47,040 --> 04:15:49,199
this one abstract method within our
7438
04:15:49,199 --> 04:15:50,319
vehicle class
7439
04:15:50,319 --> 04:15:52,319
well we could still create a vehicle
7440
04:15:52,319 --> 04:15:54,560
object and i'm just going to turn this
7441
04:15:54,560 --> 04:15:56,560
vehicle.go line into a comment because
7442
04:15:56,560 --> 04:15:58,479
we don't have a go method anymore
7443
04:15:58,479 --> 04:16:00,159
as you can see we can still create a
7444
04:16:00,159 --> 04:16:02,239
vehicle object which we would like to
7445
04:16:02,239 --> 04:16:04,080
prevent a user from doing
7446
04:16:04,080 --> 04:16:06,960
so we need at least one abstract method
7447
04:16:06,960 --> 04:16:08,479
within our vehicle class
7448
04:16:08,479 --> 04:16:10,239
so i'm going to go ahead and add this go
7449
04:16:10,239 --> 04:16:11,520
method back in
7450
04:16:11,520 --> 04:16:13,279
an additional feature of abstract
7451
04:16:13,279 --> 04:16:15,040
classes is that they compel
7452
04:16:15,040 --> 04:16:17,680
a user to override any abstract methods
7453
04:16:17,680 --> 04:16:19,359
within a child class
7454
04:16:19,359 --> 04:16:21,040
a method that is abstract has a
7455
04:16:21,040 --> 04:16:22,640
declaration but does not have
7456
04:16:22,640 --> 04:16:25,279
an implementation so let's say that we
7457
04:16:25,279 --> 04:16:26,720
create our vehicle class
7458
04:16:26,720 --> 04:16:29,120
our abstract vehicle class and now we're
7459
04:16:29,120 --> 04:16:29,840
creating
7460
04:16:29,840 --> 04:16:31,439
the children classes of car and
7461
04:16:31,439 --> 04:16:33,600
motorcycle and let's say i'm not paying
7462
04:16:33,600 --> 04:16:35,920
attention and i forget to add a go
7463
04:16:35,920 --> 04:16:36,880
method
7464
04:16:36,880 --> 04:16:38,640
so if i just write pass within my
7465
04:16:38,640 --> 04:16:40,640
motorcycle class i'm currently missing a
7466
04:16:40,640 --> 04:16:43,279
go method within my motorcycle class
7467
04:16:43,279 --> 04:16:45,439
well python is going to prevent me from
7468
04:16:45,439 --> 04:16:47,920
running this because we have a type air
7469
04:16:47,920 --> 04:16:50,319
can't instantiate abstract class vehicle
7470
04:16:50,319 --> 04:16:52,399
with abstract methods of go
7471
04:16:52,399 --> 04:16:55,120
so by inheriting from an abstract class
7472
04:16:55,120 --> 04:16:57,439
it's a good form of checks and balances
7473
04:16:57,439 --> 04:16:59,680
to be sure that your children classes
7474
04:16:59,680 --> 04:17:00,560
are not missing
7475
04:17:00,560 --> 04:17:02,880
any implementations of any methods that
7476
04:17:02,880 --> 04:17:04,000
they inherit
7477
04:17:04,000 --> 04:17:05,520
here's an additional way of thinking of
7478
04:17:05,520 --> 04:17:07,120
this our vehicle class
7479
04:17:07,120 --> 04:17:08,880
is telling its children if you're going
7480
04:17:08,880 --> 04:17:10,640
to inherit from me then you need to
7481
04:17:10,640 --> 04:17:11,279
override
7482
04:17:11,279 --> 04:17:13,359
this abstract method of mine and if you
7483
04:17:13,359 --> 04:17:14,880
don't well i'm not going to let you be
7484
04:17:14,880 --> 04:17:16,080
instantiated
7485
04:17:16,080 --> 04:17:18,479
so in order for us to create a cart and
7486
04:17:18,479 --> 04:17:19,600
motorcycle class
7487
04:17:19,600 --> 04:17:22,000
we need to override the go method that
7488
04:17:22,000 --> 04:17:23,760
they inherit from its parent class of
7489
04:17:23,760 --> 04:17:24,479
vehicle
7490
04:17:24,479 --> 04:17:26,880
and provide its own implementation now
7491
04:17:26,880 --> 04:17:29,040
let's say we create an additional method
7492
04:17:29,040 --> 04:17:30,000
let's say we have
7493
04:17:30,000 --> 04:17:32,880
a stop method so let's define that def
7494
04:17:32,880 --> 04:17:33,520
stop
7495
04:17:33,520 --> 04:17:35,279
for the time being i'll write pass and
7496
04:17:35,279 --> 04:17:37,040
to make this an abstract method
7497
04:17:37,040 --> 04:17:40,560
add this decorator at abstract
7498
04:17:40,560 --> 04:17:44,479
method now car and motorcycle both need
7499
04:17:44,479 --> 04:17:44,800
to
7500
04:17:44,800 --> 04:17:46,880
implement that method of stop it's
7501
04:17:46,880 --> 04:17:47,920
currently missing it
7502
04:17:47,920 --> 04:17:49,920
as you can see here class car must
7503
04:17:49,920 --> 04:17:51,680
implement all abstract methods
7504
04:17:51,680 --> 04:17:52,800
and the same thing applies for a
7505
04:17:52,800 --> 04:17:54,800
motorcycle as well so we need to
7506
04:17:54,800 --> 04:17:56,880
override this method and provide its own
7507
04:17:56,880 --> 04:17:58,080
implementation
7508
04:17:58,080 --> 04:18:01,040
so let's remove that decorator and let's
7509
04:18:01,040 --> 04:18:02,239
print something
7510
04:18:02,239 --> 04:18:06,880
let's print this car is stopped
7511
04:18:06,880 --> 04:18:08,239
and i'll do the same thing for
7512
04:18:08,239 --> 04:18:10,640
motorcycle
7513
04:18:10,640 --> 04:18:14,399
so def stop let's say
7514
04:18:14,399 --> 04:18:18,239
this motorcycle
7515
04:18:18,239 --> 04:18:21,600
is stopped and we'll call the stop
7516
04:18:21,600 --> 04:18:22,880
method for each of these
7517
04:18:22,880 --> 04:18:26,239
types of vehicles car dot stop
7518
04:18:26,239 --> 04:18:29,760
and motorcycle dot stop
7519
04:18:29,760 --> 04:18:32,159
all right and we can now create a car
7520
04:18:32,159 --> 04:18:33,520
and motorcycle object
7521
04:18:33,520 --> 04:18:36,080
because we are overriding both of these
7522
04:18:36,080 --> 04:18:38,000
abstract methods found within the parent
7523
04:18:38,000 --> 04:18:39,520
class of vehicle
7524
04:18:39,520 --> 04:18:41,279
all right everybody so that's the basics
7525
04:18:41,279 --> 04:18:42,800
of abstract classes
7526
04:18:42,800 --> 04:18:45,359
it's a class which contains one or more
7527
04:18:45,359 --> 04:18:46,720
abstract methods
7528
04:18:46,720 --> 04:18:48,720
and an abstract method is a method that
7529
04:18:48,720 --> 04:18:50,000
has a declaration
7530
04:18:50,000 --> 04:18:52,640
but does not have an implementation and
7531
04:18:52,640 --> 04:18:54,960
the benefits of using an abstract class
7532
04:18:54,960 --> 04:18:56,560
is that they prevent a user from
7533
04:18:56,560 --> 04:18:58,800
creating an object of that class
7534
04:18:58,800 --> 04:19:01,040
plus it compels a user to override any
7535
04:19:01,040 --> 04:19:02,960
abstract methods found within a child
7536
04:19:02,960 --> 04:19:03,920
class
7537
04:19:03,920 --> 04:19:05,680
so if you would like a copy of this code
7538
04:19:05,680 --> 04:19:07,279
i will post all of this in the comments
7539
04:19:07,279 --> 04:19:08,479
section down below
7540
04:19:08,479 --> 04:19:10,560
but yeah that is how abstract classes
7541
04:19:10,560 --> 04:19:12,399
work in python
7542
04:19:12,399 --> 04:19:14,319
hey what's going on everybody it's you
7543
04:19:14,319 --> 04:19:15,680
bro hope you're doing well
7544
04:19:15,680 --> 04:19:17,199
and in this video i'm going to explain
7545
04:19:17,199 --> 04:19:18,960
how we can pass objects as
7546
04:19:18,960 --> 04:19:22,239
arguments so sit back relax and enjoy
7547
04:19:22,239 --> 04:19:24,640
the show
7548
04:19:24,800 --> 04:19:26,880
ladies and gentlemen in today's video
7549
04:19:26,880 --> 04:19:28,399
i'm going to be demonstrating how we can
7550
04:19:28,399 --> 04:19:28,880
pass
7551
04:19:28,880 --> 04:19:31,199
objects as arguments for this example
7552
04:19:31,199 --> 04:19:32,560
let's say we have a class
7553
04:19:32,560 --> 04:19:35,279
car and there is one class variable
7554
04:19:35,279 --> 04:19:36,159
called color
7555
04:19:36,159 --> 04:19:38,640
color equals none what we'll be doing is
7556
04:19:38,640 --> 04:19:40,720
calling a function that accepts an
7557
04:19:40,720 --> 04:19:43,040
object as an argument as well as a color
7558
04:19:43,040 --> 04:19:46,399
so let's create a function named change
7559
04:19:46,399 --> 04:19:48,399
color now make sure when you define this
7560
04:19:48,399 --> 04:19:50,560
function it's not within the cart class
7561
04:19:50,560 --> 04:19:52,640
then technically this would be a method
7562
04:19:52,640 --> 04:19:53,920
of the car class
7563
04:19:53,920 --> 04:19:55,520
what we would like is a separate
7564
04:19:55,520 --> 04:19:57,600
function outside of the car class
7565
04:19:57,600 --> 04:19:59,600
now we need to set up some parameters we
7566
04:19:59,600 --> 04:20:01,680
would like to accept a car object as
7567
04:20:01,680 --> 04:20:02,880
well as a color
7568
04:20:02,880 --> 04:20:05,040
so for the first parameter let's say a
7569
04:20:05,040 --> 04:20:06,720
car and make sure this is in
7570
04:20:06,720 --> 04:20:08,640
uppercase or capital because python
7571
04:20:08,640 --> 04:20:10,640
doesn't like that argument names should
7572
04:20:10,640 --> 04:20:11,600
be lowercase
7573
04:20:11,600 --> 04:20:13,520
so when we pass in a car we will give it
7574
04:20:13,520 --> 04:20:15,120
a nickname of car
7575
04:20:15,120 --> 04:20:17,120
and we would also like to pass in a
7576
04:20:17,120 --> 04:20:19,120
color so let's set up a parameter called
7577
04:20:19,120 --> 04:20:19,760
color
7578
04:20:19,760 --> 04:20:22,720
now when we pass in a car we will assign
7579
04:20:22,720 --> 04:20:24,800
that car's color with whatever color
7580
04:20:24,800 --> 04:20:26,159
that we receive so
7581
04:20:26,159 --> 04:20:29,520
car dot color equals color
7582
04:20:29,520 --> 04:20:31,439
and now let's create some car objects so
7583
04:20:31,439 --> 04:20:33,040
let's say car one
7584
04:20:33,040 --> 04:20:36,720
equals car car two equals car
7585
04:20:36,720 --> 04:20:40,000
and car three equals cart currently if i
7586
04:20:40,000 --> 04:20:40,880
were to print
7587
04:20:40,880 --> 04:20:43,199
their colors this is what we'll see
7588
04:20:43,199 --> 04:20:44,560
print car one
7589
04:20:44,560 --> 04:20:47,279
dot color and repeat the steps for two
7590
04:20:47,279 --> 04:20:48,640
and three
7591
04:20:48,640 --> 04:20:50,239
and we're not calling this function yet
7592
04:20:50,239 --> 04:20:52,399
so all of these colors should be set to
7593
04:20:52,399 --> 04:20:53,040
none
7594
04:20:53,040 --> 04:20:56,080
they are basically cars with no color
7595
04:20:56,080 --> 04:20:58,479
they're i don't know iron or something
7596
04:20:58,479 --> 04:21:00,319
whatever cars are made out of aluminum
7597
04:21:00,319 --> 04:21:02,560
plastic stuff like that okay so now
7598
04:21:02,560 --> 04:21:03,680
let's call this function
7599
04:21:03,680 --> 04:21:06,399
change color perhaps right here so in
7600
04:21:06,399 --> 04:21:08,000
order to call this function we need to
7601
04:21:08,000 --> 04:21:09,040
pass in
7602
04:21:09,040 --> 04:21:11,600
a car object as well as a color so let's
7603
04:21:11,600 --> 04:21:12,319
pass in
7604
04:21:12,319 --> 04:21:15,120
car one and a color perhaps red and
7605
04:21:15,120 --> 04:21:17,840
we'll repeat the steps for two and three
7606
04:21:17,840 --> 04:21:21,199
so let's say car two will be white
7607
04:21:21,199 --> 04:21:24,880
and car 3 will be blue
7608
04:21:24,880 --> 04:21:27,359
now after calling this function we have
7609
04:21:27,359 --> 04:21:28,080
a car
7610
04:21:28,080 --> 04:21:30,080
object and a color and we're assigning
7611
04:21:30,080 --> 04:21:32,080
the color of our car to whatever color
7612
04:21:32,080 --> 04:21:33,439
that we receive
7613
04:21:33,439 --> 04:21:36,640
now these cars in order car 1 2 and 3
7614
04:21:36,640 --> 04:21:39,199
are red white and blue now one thing
7615
04:21:39,199 --> 04:21:39,920
that you should know
7616
04:21:39,920 --> 04:21:41,760
is that the name of this parameter that
7617
04:21:41,760 --> 04:21:42,960
accepts our object
7618
04:21:42,960 --> 04:21:45,040
doesn't necessarily need the same name
7619
04:21:45,040 --> 04:21:46,239
as the class name
7620
04:21:46,239 --> 04:21:47,600
that created the object that we're
7621
04:21:47,600 --> 04:21:49,359
passing in we could name this something
7622
04:21:49,359 --> 04:21:51,040
else entirely perhaps
7623
04:21:51,040 --> 04:21:52,880
vehicle you'll just want to be sure that
7624
04:21:52,880 --> 04:21:54,239
this is at least descriptive
7625
04:21:54,239 --> 04:21:55,760
of the kind of object that you would
7626
04:21:55,760 --> 04:21:58,239
like to pass in and we're not limited to
7627
04:21:58,239 --> 04:22:00,880
just passing in car objects we can pass
7628
04:22:00,880 --> 04:22:02,319
in all sorts of objects
7629
04:22:02,319 --> 04:22:04,800
but we need to pass in an object as well
7630
04:22:04,800 --> 04:22:05,520
as a color
7631
04:22:05,520 --> 04:22:07,600
so this time let's create a class called
7632
04:22:07,600 --> 04:22:08,560
motorcycle
7633
04:22:08,560 --> 04:22:12,239
class motorcycle and this class will
7634
04:22:12,239 --> 04:22:14,319
also have a color set to none
7635
04:22:14,319 --> 04:22:16,560
so what we're doing is actually reusing
7636
04:22:16,560 --> 04:22:18,319
this function for all sorts of different
7637
04:22:18,319 --> 04:22:19,439
kinds of vehicles
7638
04:22:19,439 --> 04:22:21,359
now let's create a vehicle i'll just
7639
04:22:21,359 --> 04:22:22,880
name this bike one for short
7640
04:22:22,880 --> 04:22:26,399
bike one equals motorcycle now let's
7641
04:22:26,399 --> 04:22:27,040
call the
7642
04:22:27,040 --> 04:22:30,479
change color function and we can pass in
7643
04:22:30,479 --> 04:22:33,600
an object as well as color this time we
7644
04:22:33,600 --> 04:22:34,479
will pass in
7645
04:22:34,479 --> 04:22:37,520
our bike object as well as a color i
7646
04:22:37,520 --> 04:22:37,840
think
7647
04:22:37,840 --> 04:22:42,000
black counts as a color and let's print
7648
04:22:42,000 --> 04:22:46,080
bike once color
7649
04:22:46,080 --> 04:22:48,880
and this should work we have red white
7650
04:22:48,880 --> 04:22:50,319
blue and black
7651
04:22:50,319 --> 04:22:52,800
in conclusion we can pass objects as
7652
04:22:52,800 --> 04:22:54,319
arguments to a function
7653
04:22:54,319 --> 04:22:55,920
much like what we've been doing with
7654
04:22:55,920 --> 04:22:58,640
variables however the type of objects
7655
04:22:58,640 --> 04:23:01,279
that we pass in may be limited based on
7656
04:23:01,279 --> 04:23:03,680
the required attributes and methods
7657
04:23:03,680 --> 04:23:05,760
that that given class or object might
7658
04:23:05,760 --> 04:23:08,000
have and we'll get more into this on the
7659
04:23:08,000 --> 04:23:09,040
next video on
7660
04:23:09,040 --> 04:23:11,359
duct typing so if you would like a copy
7661
04:23:11,359 --> 04:23:13,359
of all this code i will post all of this
7662
04:23:13,359 --> 04:23:15,040
in the comment section down below
7663
04:23:15,040 --> 04:23:17,760
but yeah that is how to pass objects as
7664
04:23:17,760 --> 04:23:18,560
arguments
7665
04:23:18,560 --> 04:23:21,040
using python hey what's going on
7666
04:23:21,040 --> 04:23:21,600
everybody
7667
04:23:21,600 --> 04:23:23,600
it's bro hope you're doing well and in
7668
04:23:23,600 --> 04:23:25,199
this video i'm going to explain duck
7669
04:23:25,199 --> 04:23:26,640
typing in python so
7670
04:23:26,640 --> 04:23:31,359
sit back relax and enjoy the show
7671
04:23:31,359 --> 04:23:33,520
i hope you all like ducks because that's
7672
04:23:33,520 --> 04:23:34,880
the topic of this video
7673
04:23:34,880 --> 04:23:37,199
duct typing is the concept where the
7674
04:23:37,199 --> 04:23:39,680
class of an object is less important
7675
04:23:39,680 --> 04:23:41,680
than the methods and or attributes that
7676
04:23:41,680 --> 04:23:43,199
that class might have
7677
04:23:43,199 --> 04:23:45,439
the class type is not checked if the
7678
04:23:45,439 --> 04:23:47,760
minimum methods and or attributes
7679
04:23:47,760 --> 04:23:50,080
are present it's based off of a popular
7680
04:23:50,080 --> 04:23:50,880
phrase
7681
04:23:50,880 --> 04:23:52,720
if it walks like a duck and it quacks
7682
04:23:52,720 --> 04:23:54,800
like a duck then it must be a duck
7683
04:23:54,800 --> 04:23:57,199
here's an example i have two classes
7684
04:23:57,199 --> 04:23:59,120
class duck and class chicken
7685
04:23:59,120 --> 04:24:01,760
both duck and chicken have walk and talk
7686
04:24:01,760 --> 04:24:02,399
methods
7687
04:24:02,399 --> 04:24:04,159
but they print something different with
7688
04:24:04,159 --> 04:24:06,239
the duck class the walk method will
7689
04:24:06,239 --> 04:24:06,880
print
7690
04:24:06,880 --> 04:24:09,359
this duck is walking and the duck talk
7691
04:24:09,359 --> 04:24:10,000
method will
7692
04:24:10,000 --> 04:24:12,640
print this duck is quacking the chicken
7693
04:24:12,640 --> 04:24:13,120
class
7694
04:24:13,120 --> 04:24:15,520
has the same named methods walk and talk
7695
04:24:15,520 --> 04:24:16,720
but they print something slightly
7696
04:24:16,720 --> 04:24:17,199
different
7697
04:24:17,199 --> 04:24:19,439
a slightly different variation the print
7698
04:24:19,439 --> 04:24:20,640
method will print
7699
04:24:20,640 --> 04:24:22,560
this chicken is walking and the talk
7700
04:24:22,560 --> 04:24:23,760
method will print
7701
04:24:23,760 --> 04:24:25,920
this chicken is clucking now let's say
7702
04:24:25,920 --> 04:24:28,080
we have a third class a class
7703
04:24:28,080 --> 04:24:30,960
called person
7704
04:24:32,159 --> 04:24:34,080
now there is one method within our
7705
04:24:34,080 --> 04:24:35,600
person class
7706
04:24:35,600 --> 04:24:37,520
let's say we're attempting to catch a
7707
04:24:37,520 --> 04:24:39,520
duck
7708
04:24:39,520 --> 04:24:41,680
we'll pass in self and we need to pass
7709
04:24:41,680 --> 04:24:42,560
in a
7710
04:24:42,560 --> 04:24:45,920
duck object as an argument so let's type
7711
04:24:45,920 --> 04:24:48,960
duck now within this method
7712
04:24:48,960 --> 04:24:53,199
we'll have our duck use its walk method
7713
04:24:53,199 --> 04:24:56,720
as well as its talk method duck.walk and
7714
04:24:56,720 --> 04:24:58,560
duck.talk
7715
04:24:58,560 --> 04:25:02,000
and then maybe we'll print something
7716
04:25:02,159 --> 04:25:07,279
such as you caught the critter
7717
04:25:07,279 --> 04:25:09,040
now let's create an object from each of
7718
04:25:09,040 --> 04:25:10,960
these classes let's create a
7719
04:25:10,960 --> 04:25:14,399
duck object duck equals duck
7720
04:25:14,399 --> 04:25:17,760
a chicken object chicken equals chicken
7721
04:25:17,760 --> 04:25:21,279
and lastly person person equals person
7722
04:25:21,279 --> 04:25:23,520
now if we would like our person to use
7723
04:25:23,520 --> 04:25:24,720
the catch method
7724
04:25:24,720 --> 04:25:27,520
we need to pass in a duck as an argument
7725
04:25:27,520 --> 04:25:29,680
so with our person let's type person
7726
04:25:29,680 --> 04:25:32,159
dot catch and we need to send a duck
7727
04:25:32,159 --> 04:25:34,159
object because that's a required
7728
04:25:34,159 --> 04:25:35,120
parameter here
7729
04:25:35,120 --> 04:25:37,120
so we're passing in our duck object that
7730
04:25:37,120 --> 04:25:39,199
we created and as soon as we catch the
7731
04:25:39,199 --> 04:25:39,680
duck
7732
04:25:39,680 --> 04:25:41,680
it's going to use its walk and talk
7733
04:25:41,680 --> 04:25:43,199
method and then we'll print
7734
04:25:43,199 --> 04:25:45,279
you caught the critter so as you would
7735
04:25:45,279 --> 04:25:46,560
expect this will print
7736
04:25:46,560 --> 04:25:48,319
this duck is walking the stuck is
7737
04:25:48,319 --> 04:25:50,399
quacking you caught the critter
7738
04:25:50,399 --> 04:25:53,439
now with duct typing we can pass in
7739
04:25:53,439 --> 04:25:56,399
a different type of object as long as it
7740
04:25:56,399 --> 04:25:56,720
has
7741
04:25:56,720 --> 04:25:59,359
the same methods and or attributes as
7742
04:25:59,359 --> 04:26:00,239
our duck
7743
04:26:00,239 --> 04:26:02,000
we could pass in a chicken because a
7744
04:26:02,000 --> 04:26:03,520
chicken can also walk
7745
04:26:03,520 --> 04:26:06,239
and talk so this time let's pass in our
7746
04:26:06,239 --> 04:26:06,800
chicken
7747
04:26:06,800 --> 04:26:09,279
as an argument and this will still work
7748
04:26:09,279 --> 04:26:10,960
even though our parameter is set up to
7749
04:26:10,960 --> 04:26:13,680
take a duck object
7750
04:26:13,680 --> 04:26:15,439
this chicken is walking this chicken is
7751
04:26:15,439 --> 04:26:17,120
clucking you caught the critter
7752
04:26:17,120 --> 04:26:19,359
in layman's terms think of it like this
7753
04:26:19,359 --> 04:26:21,840
python is examining this chicken object
7754
04:26:21,840 --> 04:26:23,600
it's using its walk method it's using
7755
04:26:23,600 --> 04:26:25,439
its talk method which are required
7756
04:26:25,439 --> 04:26:27,439
and python is saying well that's close
7757
04:26:27,439 --> 04:26:29,439
enough remember if it walks like a duck
7758
04:26:29,439 --> 04:26:30,640
and it quacks like a duck
7759
04:26:30,640 --> 04:26:33,279
then it must be a duck so duct typing is
7760
04:26:33,279 --> 04:26:35,920
the concept where the class of an object
7761
04:26:35,920 --> 04:26:38,000
is less important than the methods and
7762
04:26:38,000 --> 04:26:39,840
or attributes that it might have
7763
04:26:39,840 --> 04:26:41,920
the class type is not checked if the
7764
04:26:41,920 --> 04:26:44,000
minimum methods and or attributes
7765
04:26:44,000 --> 04:26:46,000
are present now let's say with our
7766
04:26:46,000 --> 04:26:48,239
chicken object our chicken can no longer
7767
04:26:48,239 --> 04:26:50,239
walk
7768
04:26:50,239 --> 04:26:51,920
well we cannot pass in our chicken
7769
04:26:51,920 --> 04:26:53,279
object anymore
7770
04:26:53,279 --> 04:26:55,120
we have an attribute error chicken
7771
04:26:55,120 --> 04:26:57,199
object has no attribute walk
7772
04:26:57,199 --> 04:26:59,120
so python took a look at this chicken
7773
04:26:59,120 --> 04:27:01,120
and noticed that it does not have a walk
7774
04:27:01,120 --> 04:27:01,760
method
7775
04:27:01,760 --> 04:27:03,760
well it's not walking like a duck but
7776
04:27:03,760 --> 04:27:05,199
it's talking like a duck
7777
04:27:05,199 --> 04:27:07,040
so it doesn't count as a duck because it
7778
04:27:07,040 --> 04:27:08,880
doesn't have its walk method
7779
04:27:08,880 --> 04:27:10,800
in conclusion the class type of an
7780
04:27:10,800 --> 04:27:12,880
object is not as important as the
7781
04:27:12,880 --> 04:27:14,720
methods and or attributes that that
7782
04:27:14,720 --> 04:27:16,159
class might have when using
7783
04:27:16,159 --> 04:27:18,399
duct typing the class type will not be
7784
04:27:18,399 --> 04:27:19,680
checked if the minimum
7785
04:27:19,680 --> 04:27:22,239
methods and or attributes are present
7786
04:27:22,239 --> 04:27:22,720
since
7787
04:27:22,720 --> 04:27:24,720
chickens can both walk and talk like
7788
04:27:24,720 --> 04:27:25,760
ducks then
7789
04:27:25,760 --> 04:27:27,760
they can be a substitute for ducks when
7790
04:27:27,760 --> 04:27:29,760
we pass in our duck object to this
7791
04:27:29,760 --> 04:27:30,640
method
7792
04:27:30,640 --> 04:27:32,319
so if you would like a copy of this code
7793
04:27:32,319 --> 04:27:33,920
i will post all of this in the comment
7794
04:27:33,920 --> 04:27:35,040
section down below
7795
04:27:35,040 --> 04:27:37,040
but yeah that is the basics of duct
7796
04:27:37,040 --> 04:27:38,960
typing in python
7797
04:27:38,960 --> 04:27:40,960
hey what's going on everybody it's you
7798
04:27:40,960 --> 04:27:42,319
bro hope you're doing well
7799
04:27:42,319 --> 04:27:44,000
and in this video i'm going to discuss
7800
04:27:44,000 --> 04:27:46,159
the walrus operator in python
7801
04:27:46,159 --> 04:27:51,279
so sit back relax and enjoy the show
7802
04:27:51,279 --> 04:27:52,880
all right people i hope you like
7803
04:27:52,880 --> 04:27:54,800
walruses because that's the topic of
7804
04:27:54,800 --> 04:27:56,319
this video we'll be discussing
7805
04:27:56,319 --> 04:27:58,960
the walrus operator also known as an
7806
04:27:58,960 --> 04:28:00,319
assignment expression
7807
04:28:00,319 --> 04:28:02,800
it's a colon followed by an equal sign
7808
04:28:02,800 --> 04:28:04,479
and it kind of looks like a walrus on
7809
04:28:04,479 --> 04:28:05,120
its side
7810
04:28:05,120 --> 04:28:08,319
now this is a new feature for python 3.8
7811
04:28:08,319 --> 04:28:10,800
what it does is that it assigns values
7812
04:28:10,800 --> 04:28:11,760
to variables
7813
04:28:11,760 --> 04:28:14,159
as part of a larger expression here's an
7814
04:28:14,159 --> 04:28:16,080
example of why the walrus operator would
7815
04:28:16,080 --> 04:28:16,880
be useful
7816
04:28:16,880 --> 04:28:19,359
let's say we have a variable named happy
7817
04:28:19,359 --> 04:28:19,920
happy
7818
04:28:19,920 --> 04:28:21,920
equals true if you're not happy or
7819
04:28:21,920 --> 04:28:23,760
you're feeling so so you can set this to
7820
04:28:23,760 --> 04:28:24,960
false if you want
7821
04:28:24,960 --> 04:28:27,520
so let's print the value contained
7822
04:28:27,520 --> 04:28:28,800
within happy
7823
04:28:28,800 --> 04:28:30,960
so this will print true to the console
7824
04:28:30,960 --> 04:28:31,920
window
7825
04:28:31,920 --> 04:28:33,520
now wouldn't it be cool if we could
7826
04:28:33,520 --> 04:28:35,120
combine both of these
7827
04:28:35,120 --> 04:28:37,840
statements together so we would want to
7828
04:28:37,840 --> 04:28:39,279
say something such as
7829
04:28:39,279 --> 04:28:42,159
happy equals true and then print it to
7830
04:28:42,159 --> 04:28:44,080
the console window using one line of
7831
04:28:44,080 --> 04:28:44,720
code
7832
04:28:44,720 --> 04:28:47,040
well normally we can't do this using the
7833
04:28:47,040 --> 04:28:49,120
standard assignment operator now one
7834
04:28:49,120 --> 04:28:50,399
thing that we can do is use
7835
04:28:50,399 --> 04:28:53,199
the walrus operator this assigns a value
7836
04:28:53,199 --> 04:28:54,319
to a variable
7837
04:28:54,319 --> 04:28:56,880
as part of a larger expression so this
7838
04:28:56,880 --> 04:28:57,840
would technically
7839
04:28:57,840 --> 04:29:00,159
be allowed if this assignment operator
7840
04:29:00,159 --> 04:29:01,040
was instead
7841
04:29:01,040 --> 04:29:03,199
an assignment expression the walrus
7842
04:29:03,199 --> 04:29:04,880
operator happy
7843
04:29:04,880 --> 04:29:07,520
walrus true and when they're all within
7844
04:29:07,520 --> 04:29:08,479
a print statement
7845
04:29:08,479 --> 04:29:11,760
we can assign a value to a variable and
7846
04:29:11,760 --> 04:29:14,080
use it as a part of a larger expression
7847
04:29:14,080 --> 04:29:15,279
so as you can see
7848
04:29:15,279 --> 04:29:17,840
this new variable of happy was assigned
7849
04:29:17,840 --> 04:29:19,920
and used in one expression
7850
04:29:19,920 --> 04:29:22,080
now here's a more practical example of
7851
04:29:22,080 --> 04:29:24,399
why the walrus operator would be useful
7852
04:29:24,399 --> 04:29:26,560
let's say we have the smell program what
7853
04:29:26,560 --> 04:29:28,880
this will do is consistently ask you
7854
04:29:28,880 --> 04:29:30,880
what food you like and put it into a
7855
04:29:30,880 --> 04:29:32,239
list called foods
7856
04:29:32,239 --> 04:29:35,680
foods equals list while true food equals
7857
04:29:35,680 --> 04:29:38,399
input what food do you like if food
7858
04:29:38,399 --> 04:29:39,359
equals quit
7859
04:29:39,359 --> 04:29:42,560
break then append our list of foods with
7860
04:29:42,560 --> 04:29:44,319
this new food item that we have
7861
04:29:44,319 --> 04:29:47,120
okay let's run this then so this while
7862
04:29:47,120 --> 04:29:47,439
loop
7863
04:29:47,439 --> 04:29:49,439
will continue as long as i don't type in
7864
04:29:49,439 --> 04:29:51,920
quit and it will consistently ask me
7865
04:29:51,920 --> 04:29:54,159
what food do i like and place that food
7866
04:29:54,159 --> 04:29:55,600
item into a list
7867
04:29:55,600 --> 04:29:58,319
named foods so let's begin what food do
7868
04:29:58,319 --> 04:30:00,560
i like well i do like pizza
7869
04:30:00,560 --> 04:30:04,319
i like sushi and i like ice cream
7870
04:30:04,319 --> 04:30:06,000
and that should be enough for me i'm
7871
04:30:06,000 --> 04:30:07,760
going to type in quit to exit this
7872
04:30:07,760 --> 04:30:08,560
program
7873
04:30:08,560 --> 04:30:10,720
now let's write the same program but
7874
04:30:10,720 --> 04:30:11,680
instead use
7875
04:30:11,680 --> 04:30:13,680
a walrus operator so that we can write
7876
04:30:13,680 --> 04:30:15,040
this program using
7877
04:30:15,040 --> 04:30:17,359
less lines of code now if i were to use
7878
04:30:17,359 --> 04:30:19,439
the walrus operator this is how i would
7879
04:30:19,439 --> 04:30:20,560
write the same program
7880
04:30:20,560 --> 04:30:22,800
we would still need our list foods
7881
04:30:22,800 --> 04:30:23,600
equals
7882
04:30:23,600 --> 04:30:26,960
list now for my while loop it's going to
7883
04:30:26,960 --> 04:30:28,399
look something like this
7884
04:30:28,399 --> 04:30:32,720
while food walrus operator
7885
04:30:32,720 --> 04:30:36,159
input what
7886
04:30:36,159 --> 04:30:40,239
food do you like
7887
04:30:40,239 --> 04:30:42,800
all right this is the assignment portion
7888
04:30:42,800 --> 04:30:43,920
now we can use
7889
04:30:43,920 --> 04:30:46,479
this assignment portion within a larger
7890
04:30:46,479 --> 04:30:47,439
expression
7891
04:30:47,439 --> 04:30:50,720
so let's also check to see if our input
7892
04:30:50,720 --> 04:30:54,000
does not equal quit alright
7893
04:30:54,000 --> 04:30:56,560
so then after each iteration of this
7894
04:30:56,560 --> 04:30:57,520
while loop
7895
04:30:57,520 --> 04:31:00,720
we will take our list foods dot
7896
04:31:00,720 --> 04:31:04,159
append food and
7897
04:31:04,159 --> 04:31:06,319
that is it so we have written the same
7898
04:31:06,319 --> 04:31:08,399
program in about half the lines of code
7899
04:31:08,399 --> 04:31:10,239
that we did previously so
7900
04:31:10,239 --> 04:31:12,560
let's test it what food do you like
7901
04:31:12,560 --> 04:31:13,920
pizza
7902
04:31:13,920 --> 04:31:17,199
sushi ice cream
7903
04:31:17,199 --> 04:31:20,000
and quit well everybody that is the
7904
04:31:20,000 --> 04:31:22,479
basics of the walrus operator also known
7905
04:31:22,479 --> 04:31:23,520
as an assignment
7906
04:31:23,520 --> 04:31:25,920
expression it's a new feature for python
7907
04:31:25,920 --> 04:31:26,720
3.8
7908
04:31:26,720 --> 04:31:28,640
and beyond so if this doesn't work for
7909
04:31:28,640 --> 04:31:30,399
you i would check to see what version of
7910
04:31:30,399 --> 04:31:31,760
python you're using first
7911
04:31:31,760 --> 04:31:34,319
it assigns values to variables as part
7912
04:31:34,319 --> 04:31:36,000
of a larger expression
7913
04:31:36,000 --> 04:31:37,680
if you would like a copy of this code i
7914
04:31:37,680 --> 04:31:39,120
will post all of this in the comment
7915
04:31:39,120 --> 04:31:40,479
section down below
7916
04:31:40,479 --> 04:31:42,880
but yeah that is how the walrus operator
7917
04:31:42,880 --> 04:31:45,120
works in python
7918
04:31:45,120 --> 04:31:47,279
hello what's going on everybody it's bro
7919
04:31:47,279 --> 04:31:48,880
hope you're doing well and in this video
7920
04:31:48,880 --> 04:31:50,000
i'm going to show you guys how we can
7921
04:31:50,000 --> 04:31:52,080
assign a function to a variable in
7922
04:31:52,080 --> 04:31:52,720
python
7923
04:31:52,720 --> 04:31:57,680
so sit back relax and enjoy the show
7924
04:31:57,680 --> 04:31:59,840
hey all in today's video i will show you
7925
04:31:59,840 --> 04:32:01,760
all how we can assign a function to a
7926
04:32:01,760 --> 04:32:02,720
variable
7927
04:32:02,720 --> 04:32:04,640
this video will be a prerequisite for a
7928
04:32:04,640 --> 04:32:06,239
few videos i have coming up regarding
7929
04:32:06,239 --> 04:32:06,960
functions
7930
04:32:06,960 --> 04:32:08,960
now let's say we have a function named
7931
04:32:08,960 --> 04:32:11,040
hello and all we'll do is print the word
7932
04:32:11,040 --> 04:32:11,520
hello
7933
04:32:11,520 --> 04:32:13,279
and as you know to call this function
7934
04:32:13,279 --> 04:32:14,640
you would type the name of the function
7935
04:32:14,640 --> 04:32:16,399
followed by a set of parentheses and
7936
04:32:16,399 --> 04:32:17,760
this will print the word
7937
04:32:17,760 --> 04:32:19,840
hello the set of parenthesis that comes
7938
04:32:19,840 --> 04:32:21,439
after a function's name
7939
04:32:21,439 --> 04:32:23,040
is the portion that will call the
7940
04:32:23,040 --> 04:32:24,800
function if you were to remove
7941
04:32:24,800 --> 04:32:26,800
that set of parenthesis afterwards we
7942
04:32:26,800 --> 04:32:29,040
would not in fact call that function
7943
04:32:29,040 --> 04:32:31,199
now with python python will pretty much
7944
04:32:31,199 --> 04:32:32,640
treat everything as objects
7945
04:32:32,640 --> 04:32:34,560
including functions so there's something
7946
04:32:34,560 --> 04:32:35,760
i want to show you guys
7947
04:32:35,760 --> 04:32:37,840
if i was to print the name of my
7948
04:32:37,840 --> 04:32:39,279
function hello
7949
04:32:39,279 --> 04:32:41,600
what will be displayed is the memory
7950
04:32:41,600 --> 04:32:43,600
address of this function
7951
04:32:43,600 --> 04:32:45,680
this is the memory address of where this
7952
04:32:45,680 --> 04:32:47,520
function is located within my computer's
7953
04:32:47,520 --> 04:32:48,159
memory
7954
04:32:48,159 --> 04:32:50,159
and it's in hexadecimal think of it like
7955
04:32:50,159 --> 04:32:51,760
a street address such as
7956
04:32:51,760 --> 04:32:53,600
one two three fake street this is the
7957
04:32:53,600 --> 04:32:55,359
address of where this function is within
7958
04:32:55,359 --> 04:32:56,800
my computer's memory
7959
04:32:56,800 --> 04:32:58,560
and each time that i run this program
7960
04:32:58,560 --> 04:33:00,399
this number can change as you can see
7961
04:33:00,399 --> 04:33:01,040
here
7962
04:33:01,040 --> 04:33:02,560
now one thing that we could do we could
7963
04:33:02,560 --> 04:33:04,799
assign this address to a variable
7964
04:33:04,799 --> 04:33:06,958
let's say hi equals hello and be sure
7965
04:33:06,958 --> 04:33:08,000
that you're not adding that set of
7966
04:33:08,000 --> 04:33:09,438
parentheses afterwards because
7967
04:33:09,438 --> 04:33:11,118
then you would be calling the hello
7968
04:33:11,118 --> 04:33:12,958
function and returning the result to
7969
04:33:12,958 --> 04:33:13,520
high
7970
04:33:13,520 --> 04:33:16,879
so high equals hello no parentheses
7971
04:33:16,879 --> 04:33:20,000
and if i was to print hi well
7972
04:33:20,000 --> 04:33:22,080
the address of hello and hi will be at
7973
04:33:22,080 --> 04:33:23,759
the same memory address
7974
04:33:23,759 --> 04:33:26,400
both of these numbers are the same now
7975
04:33:26,400 --> 04:33:27,759
what do you imagine would happen
7976
04:33:27,759 --> 04:33:30,080
if i was to call the high function after
7977
04:33:30,080 --> 04:33:32,240
we assigned the memory address of hello
7978
04:33:32,240 --> 04:33:33,199
to hi
7979
04:33:33,199 --> 04:33:34,879
well then what we'll end up doing is
7980
04:33:34,879 --> 04:33:36,480
calling the hello function
7981
04:33:36,480 --> 04:33:38,240
even though we're listing that we would
7982
04:33:38,240 --> 04:33:39,680
like to call the
7983
04:33:39,680 --> 04:33:41,118
high function even though it doesn't
7984
04:33:41,118 --> 04:33:43,840
exist so it's as if this hello function
7985
04:33:43,840 --> 04:33:44,799
has two names
7986
04:33:44,799 --> 04:33:46,958
you can either use hello or you can use
7987
04:33:46,958 --> 04:33:49,039
hi and let me demonstrate that
7988
04:33:49,039 --> 04:33:52,400
so you can call either hello hi or both
7989
04:33:52,400 --> 04:33:53,438
for this example
7990
04:33:53,438 --> 04:33:55,039
that's because we're assigning the
7991
04:33:55,039 --> 04:33:57,278
memory address of hello to this variable
7992
04:33:57,278 --> 04:33:58,000
of high
7993
04:33:58,000 --> 04:33:59,919
so we could treat high as a function
7994
04:33:59,919 --> 04:34:01,118
it's kind of like
7995
04:34:01,118 --> 04:34:03,199
an alias where this function has two
7996
04:34:03,199 --> 04:34:05,759
names now here's another demonstration
7997
04:34:05,759 --> 04:34:07,919
let's say that we would like to assign
7998
04:34:07,919 --> 04:34:10,080
our built-in print function to
7999
04:34:10,080 --> 04:34:13,359
a variable so let's say maybe say
8000
04:34:13,359 --> 04:34:16,879
say equals print and when i say print
8001
04:34:16,879 --> 04:34:18,561
i'm referring to the print function and
8002
04:34:18,561 --> 04:34:19,759
be sure you're not adding that set of
8003
04:34:19,759 --> 04:34:20,958
parentheses afterwards
8004
04:34:20,958 --> 04:34:22,480
now if i need to print something to the
8005
04:34:22,480 --> 04:34:24,879
console window i can either use
8006
04:34:24,879 --> 04:34:26,719
print like what we've previously been
8007
04:34:26,719 --> 04:34:28,561
doing or i could use
8008
04:34:28,561 --> 04:34:30,240
say because say has the same memory
8009
04:34:30,240 --> 04:34:32,160
address let's say
8010
04:34:32,160 --> 04:34:35,438
perhaps whoa i can't
8011
04:34:35,438 --> 04:34:39,278
believe this works
8012
04:34:39,278 --> 04:34:42,320
surprise face all right so
8013
04:34:42,320 --> 04:34:44,639
we can call the print function by saying
8014
04:34:44,639 --> 04:34:45,680
the word say
8015
04:34:45,680 --> 04:34:48,240
say all of this text and this will print
8016
04:34:48,240 --> 04:34:49,199
all of this text
8017
04:34:49,199 --> 04:34:51,438
well say all of this text to the console
8018
04:34:51,438 --> 04:34:52,400
window
8019
04:34:52,400 --> 04:34:54,480
under normal circumstances i'm not sure
8020
04:34:54,480 --> 04:34:55,520
why or when
8021
04:34:55,520 --> 04:34:57,039
you would need to assign the print
8022
04:34:57,039 --> 04:34:58,879
function to a variable but
8023
04:34:58,879 --> 04:35:00,958
you can and well that's kind of cool so
8024
04:35:00,958 --> 04:35:02,561
i thought i'd show you guys that
8025
04:35:02,561 --> 04:35:04,639
so everybody that's how to assign a
8026
04:35:04,639 --> 04:35:06,000
function to a variable
8027
04:35:06,000 --> 04:35:07,840
like i said we'll be building upon this
8028
04:35:07,840 --> 04:35:09,359
topic so you want to be sure that you
8029
04:35:09,359 --> 04:35:10,480
understand this
8030
04:35:10,480 --> 04:35:12,080
all right then well if you would like a
8031
04:35:12,080 --> 04:35:14,000
copy of this code i will post all of
8032
04:35:14,000 --> 04:35:15,759
this to the comments section down below
8033
04:35:15,759 --> 04:35:18,400
but yeah that's how to assign a function
8034
04:35:18,400 --> 04:35:19,520
to a variable
8035
04:35:19,520 --> 04:35:22,080
in python hey uh what's going on
8036
04:35:22,080 --> 04:35:22,639
everybody
8037
04:35:22,639 --> 04:35:24,561
it's bro hope you're doing well and in
8038
04:35:24,561 --> 04:35:26,240
today's video i'm going to explain
8039
04:35:26,240 --> 04:35:28,400
higher order functions in python so
8040
04:35:28,400 --> 04:35:33,199
sit back relax and enjoy the show
8041
04:35:33,199 --> 04:35:34,958
all right ladies and gentlemen let's
8042
04:35:34,958 --> 04:35:36,799
talk about higher order functions
8043
04:35:36,799 --> 04:35:38,561
these are functions that do one of two
8044
04:35:38,561 --> 04:35:40,879
things one they either accept a function
8045
04:35:40,879 --> 04:35:42,000
as an argument
8046
04:35:42,000 --> 04:35:44,561
or two they return a function as output
8047
04:35:44,561 --> 04:35:46,400
and in python this is totally allowed
8048
04:35:46,400 --> 04:35:48,240
because functions are also treated as
8049
04:35:48,240 --> 04:35:48,958
objects
8050
04:35:48,958 --> 04:35:50,400
i'll give you guys an example of both of
8051
04:35:50,400 --> 04:35:52,240
these but let's begin with number one
8052
04:35:52,240 --> 04:35:54,240
here's an example of number one let's
8053
04:35:54,240 --> 04:35:55,840
say that i have two functions
8054
04:35:55,840 --> 04:35:57,680
named loud and quiet both of these
8055
04:35:57,680 --> 04:35:59,759
functions will accept a string as an
8056
04:35:59,759 --> 04:36:00,480
argument
8057
04:36:00,480 --> 04:36:02,561
loud will return that string all
8058
04:36:02,561 --> 04:36:05,039
uppercase as if we're shouting something
8059
04:36:05,039 --> 04:36:06,879
quiet we'll return that text i'll
8060
04:36:06,879 --> 04:36:08,879
lowercase as if we're whispering
8061
04:36:08,879 --> 04:36:09,520
something
8062
04:36:09,520 --> 04:36:11,840
now i'm going to create a third function
8063
04:36:11,840 --> 04:36:13,359
called hello
8064
04:36:13,359 --> 04:36:14,958
and this will be the higher order
8065
04:36:14,958 --> 04:36:17,039
function it accepts a function as an
8066
04:36:17,039 --> 04:36:17,759
argument
8067
04:36:17,759 --> 04:36:19,118
i'm not sure if we're going to be
8068
04:36:19,118 --> 04:36:21,118
passing in loud or quiet
8069
04:36:21,118 --> 04:36:22,958
whatever it is i'm going to give it a
8070
04:36:22,958 --> 04:36:24,160
name of funk
8071
04:36:24,160 --> 04:36:26,320
short for function each of these
8072
04:36:26,320 --> 04:36:28,000
functions will return some text
8073
04:36:28,000 --> 04:36:30,719
either all uppercase or lowercase i'm
8074
04:36:30,719 --> 04:36:32,160
going to assign that to
8075
04:36:32,160 --> 04:36:34,958
text text equals the name of our
8076
04:36:34,958 --> 04:36:36,400
function we're not sure what it's going
8077
04:36:36,400 --> 04:36:37,039
to be
8078
04:36:37,039 --> 04:36:39,599
and i will pass in some text in order to
8079
04:36:39,599 --> 04:36:41,039
call one of these functions
8080
04:36:41,039 --> 04:36:42,879
i will pass in just the word hello
8081
04:36:42,879 --> 04:36:44,400
that's capitalized
8082
04:36:44,400 --> 04:36:46,160
and at the end i will print whatever
8083
04:36:46,160 --> 04:36:47,438
text that we have
8084
04:36:47,438 --> 04:36:49,199
in order to call this function of hello
8085
04:36:49,199 --> 04:36:51,039
i need to pass in a function as an
8086
04:36:51,039 --> 04:36:51,599
argument
8087
04:36:51,599 --> 04:36:54,400
do i want to pass in loud or quiet do i
8088
04:36:54,400 --> 04:36:55,039
want the
8089
04:36:55,039 --> 04:36:57,278
loud variety of hello where all of the
8090
04:36:57,278 --> 04:36:58,639
text will be uppercase
8091
04:36:58,639 --> 04:37:00,719
or the quiet variety of flow where all
8092
04:37:00,719 --> 04:37:02,639
the text will be lowercase
8093
04:37:02,639 --> 04:37:05,919
i would like to pass in loud because i
8094
04:37:05,919 --> 04:37:08,320
want to make my text all uppercase
8095
04:37:08,320 --> 04:37:10,480
so i'm typing the name of the higher
8096
04:37:10,480 --> 04:37:12,000
order function and passing in
8097
04:37:12,000 --> 04:37:14,240
a function as an argument and the result
8098
04:37:14,240 --> 04:37:16,000
is that the word hello will be printed
8099
04:37:16,000 --> 04:37:17,199
to the console window
8100
04:37:17,199 --> 04:37:19,359
all uppercase now here's a rundown of
8101
04:37:19,359 --> 04:37:20,400
what just happened
8102
04:37:20,400 --> 04:37:21,919
we're calling the hello function and
8103
04:37:21,919 --> 04:37:23,680
we're passing in loud
8104
04:37:23,680 --> 04:37:26,719
as an argument we're naming loud as
8105
04:37:26,719 --> 04:37:28,639
funk while we're within this hello
8106
04:37:28,639 --> 04:37:29,919
function
8107
04:37:29,919 --> 04:37:33,359
text equals loud and we're sending a
8108
04:37:33,359 --> 04:37:34,719
string of text
8109
04:37:34,719 --> 04:37:37,199
that says hello we're returning that
8110
04:37:37,199 --> 04:37:38,799
text all uppercase
8111
04:37:38,799 --> 04:37:41,118
assigning it to a variable called text
8112
04:37:41,118 --> 04:37:42,320
and printing that text
8113
04:37:42,320 --> 04:37:44,561
to the console window now if i would
8114
04:37:44,561 --> 04:37:45,520
like to
8115
04:37:45,520 --> 04:37:47,919
use the quiet variety of hello then i'm
8116
04:37:47,919 --> 04:37:48,799
going to pass in
8117
04:37:48,799 --> 04:37:52,080
quiet as an argument so quiet will make
8118
04:37:52,080 --> 04:37:53,118
all of this text
8119
04:37:53,118 --> 04:37:55,520
lowercase alright people so that's
8120
04:37:55,520 --> 04:37:56,160
example
8121
04:37:56,160 --> 04:37:58,400
one of a higher order function a higher
8122
04:37:58,400 --> 04:37:59,278
order function
8123
04:37:59,278 --> 04:38:02,080
is a function that accepts a function as
8124
04:38:02,080 --> 04:38:03,039
an argument
8125
04:38:03,039 --> 04:38:05,278
this hello function is an example of a
8126
04:38:05,278 --> 04:38:06,561
higher order function
8127
04:38:06,561 --> 04:38:09,199
because we're accepting either loud or
8128
04:38:09,199 --> 04:38:10,879
quiet as arguments
8129
04:38:10,879 --> 04:38:12,400
all right people let's move on to the
8130
04:38:12,400 --> 04:38:14,240
second part of this definition for
8131
04:38:14,240 --> 04:38:15,438
higher order functions
8132
04:38:15,438 --> 04:38:17,520
a higher order function is a function
8133
04:38:17,520 --> 04:38:18,879
that returns a function
8134
04:38:18,879 --> 04:38:20,561
i'm going to give a different example
8135
04:38:20,561 --> 04:38:22,639
this time let's say we have a pair of
8136
04:38:22,639 --> 04:38:24,958
nested functions the outer function will
8137
04:38:24,958 --> 04:38:26,320
be named divisor
8138
04:38:26,320 --> 04:38:28,320
and we will accept a number as an
8139
04:38:28,320 --> 04:38:30,160
argument that we will call x
8140
04:38:30,160 --> 04:38:32,561
a divisor is a number that is used to
8141
04:38:32,561 --> 04:38:33,199
divide
8142
04:38:33,199 --> 04:38:35,599
another number when using division and
8143
04:38:35,599 --> 04:38:37,520
inside this function we have an inner
8144
04:38:37,520 --> 04:38:38,080
function
8145
04:38:38,080 --> 04:38:41,039
named dividend dividend is the number
8146
04:38:41,039 --> 04:38:42,639
that's going to be divided
8147
04:38:42,639 --> 04:38:44,240
and we will call the argument that is
8148
04:38:44,240 --> 04:38:45,680
passed in y
8149
04:38:45,680 --> 04:38:49,199
all we'll do is return y divided by x
8150
04:38:49,199 --> 04:38:51,359
we're dividing the dividend by the
8151
04:38:51,359 --> 04:38:52,480
divisor y
8152
04:38:52,480 --> 04:38:55,759
divided by x now within the
8153
04:38:55,759 --> 04:38:57,520
outer function but not within the inner
8154
04:38:57,520 --> 04:39:00,080
function we're going to return
8155
04:39:00,080 --> 04:39:02,879
our dividend function so a higher order
8156
04:39:02,879 --> 04:39:04,879
function is a function that returns a
8157
04:39:04,879 --> 04:39:05,680
function
8158
04:39:05,680 --> 04:39:07,759
divisor is a higher order function
8159
04:39:07,759 --> 04:39:09,199
because we're returning
8160
04:39:09,199 --> 04:39:11,759
dividend now if i would like to access
8161
04:39:11,759 --> 04:39:12,639
this nested
8162
04:39:12,639 --> 04:39:14,879
dividend function i first need to call
8163
04:39:14,879 --> 04:39:15,759
the outside
8164
04:39:15,759 --> 04:39:18,320
divisor function and pass in a number as
8165
04:39:18,320 --> 04:39:20,480
an argument to serve as the divisor
8166
04:39:20,480 --> 04:39:22,799
the divisor function is going to return
8167
04:39:22,799 --> 04:39:24,639
my dividend function which we can then
8168
04:39:24,639 --> 04:39:26,480
assign to a variable
8169
04:39:26,480 --> 04:39:29,759
so the variable i'm going to name divide
8170
04:39:29,759 --> 04:39:32,958
divide equals and i need to call divisor
8171
04:39:32,958 --> 04:39:35,759
and set a divisor let's say that i would
8172
04:39:35,759 --> 04:39:37,360
like to divide all numbers
8173
04:39:37,360 --> 04:39:40,878
by two so i will set x to be two
8174
04:39:40,878 --> 04:39:44,400
and now for the dividend i will
8175
04:39:44,400 --> 04:39:47,440
print call the
8176
04:39:47,440 --> 04:39:50,480
divide variable and pass in a number as
8177
04:39:50,480 --> 04:39:52,080
the dividend the number that's going to
8178
04:39:52,080 --> 04:39:52,878
be divided
8179
04:39:52,878 --> 04:39:55,120
and let's say i would like to divide 10
8180
04:39:55,120 --> 04:39:56,400
by two
8181
04:39:56,400 --> 04:39:58,320
and this will work as you can see we
8182
04:39:58,320 --> 04:40:00,240
have five in the console window
8183
04:40:00,240 --> 04:40:01,760
so let me explain what just happened
8184
04:40:01,760 --> 04:40:03,520
here so our program
8185
04:40:03,520 --> 04:40:06,480
begins here divisor and we're passing in
8186
04:40:06,480 --> 04:40:07,360
two
8187
04:40:07,360 --> 04:40:09,840
x will be two and it will stay that way
8188
04:40:09,840 --> 04:40:12,000
until we finish this program or until we
8189
04:40:12,000 --> 04:40:13,520
reassign x
8190
04:40:13,520 --> 04:40:16,240
now x equals two we're skipping this
8191
04:40:16,240 --> 04:40:18,560
function because we did not call it yet
8192
04:40:18,560 --> 04:40:21,040
we're returning dividend and assigning
8193
04:40:21,040 --> 04:40:22,480
it to a variable
8194
04:40:22,480 --> 04:40:24,560
and we can call a variable if it has the
8195
04:40:24,560 --> 04:40:26,480
memory address of a function
8196
04:40:26,480 --> 04:40:28,400
which is what we're doing in this line
8197
04:40:28,400 --> 04:40:29,760
now we're calling
8198
04:40:29,760 --> 04:40:32,958
dividend and passing in 10 so y equals
8199
04:40:32,958 --> 04:40:33,440
10
8200
04:40:33,440 --> 04:40:36,718
and x still equals to we're returning 10
8201
04:40:36,718 --> 04:40:38,798
divided by 2 and printing it to the
8202
04:40:38,798 --> 04:40:40,000
console window
8203
04:40:40,000 --> 04:40:42,240
well everyone in conclusion a higher
8204
04:40:42,240 --> 04:40:43,040
order function
8205
04:40:43,040 --> 04:40:46,320
is a function that either one accepts a
8206
04:40:46,320 --> 04:40:47,920
function as an argument
8207
04:40:47,920 --> 04:40:51,040
or two they return a function the format
8208
04:40:51,040 --> 04:40:52,638
is a little bit strange and we're not
8209
04:40:52,638 --> 04:40:53,680
quite used to it yet
8210
04:40:53,680 --> 04:40:55,440
but in future videos we'll have more
8211
04:40:55,440 --> 04:40:57,440
practice with high order functions
8212
04:40:57,440 --> 04:40:58,878
if you would like a copy of all this
8213
04:40:58,878 --> 04:41:00,480
code i will post all of this to the
8214
04:41:00,480 --> 04:41:02,160
comment section down below
8215
04:41:02,160 --> 04:41:03,920
but yeah that is how higher order
8216
04:41:03,920 --> 04:41:06,400
functions work in python
8217
04:41:06,400 --> 04:41:08,878
yo what's going on everybody it's bro
8218
04:41:08,878 --> 04:41:10,400
hope you're doing well in today's video
8219
04:41:10,400 --> 04:41:11,440
i'm going to explain
8220
04:41:11,440 --> 04:41:14,240
lambda functions in python so sit back
8221
04:41:14,240 --> 04:41:15,120
relax
8222
04:41:15,120 --> 04:41:19,040
and enjoy the show all right
8223
04:41:19,040 --> 04:41:20,878
ladies and gentlemen let's talk about
8224
04:41:20,878 --> 04:41:22,878
lambda functions these are functions
8225
04:41:22,878 --> 04:41:24,718
that are written in one line and use
8226
04:41:24,718 --> 04:41:26,240
this lambda keyword
8227
04:41:26,240 --> 04:41:28,240
they accept any number of arguments but
8228
04:41:28,240 --> 04:41:30,160
they only have one expression
8229
04:41:30,160 --> 04:41:32,240
think of it as a shortcut they're useful
8230
04:41:32,240 --> 04:41:34,160
if you need a function for only one use
8231
04:41:34,160 --> 04:41:35,840
or a short period of time
8232
04:41:35,840 --> 04:41:36,878
and then you're just planning on
8233
04:41:36,878 --> 04:41:38,878
throwing away that function afterwards
8234
04:41:38,878 --> 04:41:40,958
here's the syntax for a lambda function
8235
04:41:40,958 --> 04:41:42,240
you type lambda
8236
04:41:42,240 --> 04:41:44,000
you add the parameters followed by a
8237
04:41:44,000 --> 04:41:45,920
colon and then your expression
8238
04:41:45,920 --> 04:41:47,600
let's say we have this function named
8239
04:41:47,600 --> 04:41:49,760
double double accepts one argument that
8240
04:41:49,760 --> 04:41:50,400
we will name
8241
04:41:50,400 --> 04:41:52,718
x we're returning x times two
8242
04:41:52,718 --> 04:41:54,240
effectively doubling it
8243
04:41:54,240 --> 04:41:56,080
so if i need to call this function i
8244
04:41:56,080 --> 04:41:57,280
would type double
8245
04:41:57,280 --> 04:41:59,280
and then pass in a number as an argument
8246
04:41:59,280 --> 04:42:01,360
so the result if i pass in 5 would be
8247
04:42:01,360 --> 04:42:03,440
10. now let's write the same function
8248
04:42:03,440 --> 04:42:04,958
but instead write it as a
8249
04:42:04,958 --> 04:42:07,120
lambda function so let me turn these
8250
04:42:07,120 --> 04:42:09,040
lines into comments
8251
04:42:09,040 --> 04:42:12,560
now the first step is to type lambda
8252
04:42:12,560 --> 04:42:14,320
then our parameters we only have one
8253
04:42:14,320 --> 04:42:16,000
parameter and that is x
8254
04:42:16,000 --> 04:42:18,000
colon and then our expression we would
8255
04:42:18,000 --> 04:42:19,120
like to return x
8256
04:42:19,120 --> 04:42:21,760
times two now this lambda function is
8257
04:42:21,760 --> 04:42:23,760
going to return a function object and we
8258
04:42:23,760 --> 04:42:25,200
can assign that much like what we do
8259
04:42:25,200 --> 04:42:26,240
with variables
8260
04:42:26,240 --> 04:42:29,600
let's say double equals lambda x colon
8261
04:42:29,600 --> 04:42:32,320
x times two and in order to call this
8262
04:42:32,320 --> 04:42:33,280
lambda function
8263
04:42:33,280 --> 04:42:35,840
we're going to type double then a set of
8264
04:42:35,840 --> 04:42:36,560
parentheses
8265
04:42:36,560 --> 04:42:38,560
to call this function and then pass in
8266
04:42:38,560 --> 04:42:39,600
our arguments
8267
04:42:39,600 --> 04:42:42,000
so let's say that x equals 5 and then
8268
04:42:42,000 --> 04:42:43,200
we're going to double it
8269
04:42:43,200 --> 04:42:45,360
and the result is 10. so that's the
8270
04:42:45,360 --> 04:42:47,600
syntax for a lambda function you type
8271
04:42:47,600 --> 04:42:48,400
lambda
8272
04:42:48,400 --> 04:42:50,480
your parameters colon and then your
8273
04:42:50,480 --> 04:42:52,480
expression this time let's say that we
8274
04:42:52,480 --> 04:42:53,600
have two parameters
8275
04:42:53,600 --> 04:42:55,360
we have two numbers that we want to
8276
04:42:55,360 --> 04:42:56,798
multiply by each other
8277
04:42:56,798 --> 04:42:59,120
so let's say that this function will be
8278
04:42:59,120 --> 04:43:00,480
called multiply
8279
04:43:00,480 --> 04:43:03,840
multiply equals lambda
8280
04:43:03,840 --> 04:43:07,840
x comma y because we have two parameters
8281
04:43:07,840 --> 04:43:10,080
colon followed by our expression we
8282
04:43:10,080 --> 04:43:11,280
would like to multiply
8283
04:43:11,280 --> 04:43:14,480
x times y and that is it
8284
04:43:14,480 --> 04:43:16,638
now in order to call multiply we need to
8285
04:43:16,638 --> 04:43:18,240
pass in two arguments
8286
04:43:18,240 --> 04:43:20,240
if i were to pass in just one well we'll
8287
04:43:20,240 --> 04:43:21,440
have a type error
8288
04:43:21,440 --> 04:43:24,320
so let's pass in five and maybe six and
8289
04:43:24,320 --> 04:43:24,958
the result
8290
04:43:24,958 --> 04:43:28,638
is thirty now let's try three parameters
8291
04:43:28,638 --> 04:43:30,160
let's say we would like to add three
8292
04:43:30,160 --> 04:43:31,680
numbers together
8293
04:43:31,680 --> 04:43:35,040
let's say add lambda x
8294
04:43:35,040 --> 04:43:38,080
comma y comma z so we have all of our
8295
04:43:38,080 --> 04:43:38,958
parameters
8296
04:43:38,958 --> 04:43:41,200
we have our colon here and then we need
8297
04:43:41,200 --> 04:43:42,798
our expression
8298
04:43:42,798 --> 04:43:46,240
x plus y plus z and
8299
04:43:46,240 --> 04:43:49,680
let's add five six and seven together
8300
04:43:49,680 --> 04:43:52,718
the result is 18. now let's try
8301
04:43:52,718 --> 04:43:54,160
something a little more complex let's
8302
04:43:54,160 --> 04:43:56,400
pass in some strings this time
8303
04:43:56,400 --> 04:43:58,878
i'll create a function named full name
8304
04:43:58,878 --> 04:43:59,840
full name
8305
04:43:59,840 --> 04:44:02,958
equals lambda
8306
04:44:02,958 --> 04:44:06,240
first name comma
8307
04:44:06,240 --> 04:44:09,440
last name the expression is going to be
8308
04:44:09,440 --> 04:44:12,878
first name plus perhaps a space
8309
04:44:12,878 --> 04:44:16,878
plus last name now i need to pass in a
8310
04:44:16,878 --> 04:44:19,760
first name and a last name
8311
04:44:19,760 --> 04:44:22,240
full name and you can use your own name
8312
04:44:22,240 --> 04:44:23,600
for this example if you want
8313
04:44:23,600 --> 04:44:29,280
let's say bro comma code
8314
04:44:29,280 --> 04:44:31,280
yep this will print your first name and
8315
04:44:31,280 --> 04:44:33,200
your last name to the console window
8316
04:44:33,200 --> 04:44:34,878
now let's take it a step further let's
8317
04:44:34,878 --> 04:44:36,480
say we would like to check somebody's
8318
04:44:36,480 --> 04:44:38,560
age once using a lambda function we
8319
04:44:38,560 --> 04:44:40,160
would like to check to see if they're 18
8320
04:44:40,160 --> 04:44:41,760
or older because they're signing up for
8321
04:44:41,760 --> 04:44:43,200
something perhaps let's say a credit
8322
04:44:43,200 --> 04:44:43,840
card
8323
04:44:43,840 --> 04:44:47,040
so let's create a function object age
8324
04:44:47,040 --> 04:44:50,560
check equals lambda we have one
8325
04:44:50,560 --> 04:44:51,440
parameter
8326
04:44:51,440 --> 04:44:53,440
somebody's edge colon and then our
8327
04:44:53,440 --> 04:44:55,280
expression
8328
04:44:55,280 --> 04:44:59,120
let's type true if age is greater than
8329
04:44:59,120 --> 04:45:00,080
or equal to
8330
04:45:00,080 --> 04:45:03,680
18 else
8331
04:45:03,680 --> 04:45:07,040
false to call this age track function
8332
04:45:07,040 --> 04:45:10,240
we need to pass in an age let's say the
8333
04:45:10,240 --> 04:45:11,920
user is 12
8334
04:45:11,920 --> 04:45:13,120
and they're trying to sign up for a
8335
04:45:13,120 --> 04:45:15,600
credit card while this will return false
8336
04:45:15,600 --> 04:45:18,480
let's say they're 18 now and this will
8337
04:45:18,480 --> 04:45:19,040
return
8338
04:45:19,040 --> 04:45:22,160
true so that's a lambda function it's a
8339
04:45:22,160 --> 04:45:22,638
function
8340
04:45:22,638 --> 04:45:24,718
that is written in one line using this
8341
04:45:24,718 --> 04:45:25,840
lambda keyword
8342
04:45:25,840 --> 04:45:27,840
they accept any number of arguments but
8343
04:45:27,840 --> 04:45:29,440
they only have one expression
8344
04:45:29,440 --> 04:45:31,200
think of it like a shortcut and they're
8345
04:45:31,200 --> 04:45:32,798
useful if you need to use a function
8346
04:45:32,798 --> 04:45:34,160
only once and then you're just planning
8347
04:45:34,160 --> 04:45:36,160
on throwing it away afterwards
8348
04:45:36,160 --> 04:45:37,600
so if you would like a copy of all this
8349
04:45:37,600 --> 04:45:39,280
code i will post all of this to the
8350
04:45:39,280 --> 04:45:40,798
comment section down below
8351
04:45:40,798 --> 04:45:43,120
but yeah those are lambda functions in
8352
04:45:43,120 --> 04:45:44,480
python
8353
04:45:44,480 --> 04:45:46,798
hey what's going on everybody it's bro
8354
04:45:46,798 --> 04:45:47,760
hope you're doing well
8355
04:45:47,760 --> 04:45:49,200
and in this video i'm going to show you
8356
04:45:49,200 --> 04:45:51,520
all how we can sort iterables in python
8357
04:45:51,520 --> 04:45:52,798
so sit back
8358
04:45:52,798 --> 04:45:56,638
relax and enjoy the show
8359
04:45:57,040 --> 04:45:59,120
well well well welcome back everybody in
8360
04:45:59,120 --> 04:46:00,480
this video i'm going to show you all how
8361
04:46:00,480 --> 04:46:02,240
we can sort data using python we'll
8362
04:46:02,240 --> 04:46:04,160
begin with the sort method which is used
8363
04:46:04,160 --> 04:46:06,000
with lists and the sort function which
8364
04:46:06,000 --> 04:46:07,760
is used with iterables and that would
8365
04:46:07,760 --> 04:46:09,680
include lists let's start with something
8366
04:46:09,680 --> 04:46:11,360
very basic and then we'll crank up the
8367
04:46:11,360 --> 04:46:13,280
difficulty one step at a time
8368
04:46:13,280 --> 04:46:15,680
i have a list named students and i have
8369
04:46:15,680 --> 04:46:17,200
a bunch of student names within this
8370
04:46:17,200 --> 04:46:17,680
list
8371
04:46:17,680 --> 04:46:19,920
we have squidward sandy patrick
8372
04:46:19,920 --> 04:46:21,680
spongebob mr krabs
8373
04:46:21,680 --> 04:46:23,520
let's say that all of these students are
8374
04:46:23,520 --> 04:46:25,120
taking a college course
8375
04:46:25,120 --> 04:46:27,200
perhaps on marine biology what i would
8376
04:46:27,200 --> 04:46:28,798
like to do is to sort this list in
8377
04:46:28,798 --> 04:46:29,840
alphabetical order
8378
04:46:29,840 --> 04:46:31,920
and to do that we have a method that is
8379
04:46:31,920 --> 04:46:33,040
built in with lists
8380
04:46:33,040 --> 04:46:35,280
so type the name of your list students
8381
04:46:35,280 --> 04:46:36,320
dot
8382
04:46:36,320 --> 04:46:38,718
sort and it's easy as that and to
8383
04:46:38,718 --> 04:46:40,240
display this list i think i'll use a for
8384
04:46:40,240 --> 04:46:40,718
loop
8385
04:46:40,718 --> 04:46:44,400
for i in students
8386
04:46:44,400 --> 04:46:48,320
print i
8387
04:46:48,320 --> 04:46:50,000
and there you go that's how to sort a
8388
04:46:50,000 --> 04:46:52,080
list starting with the top we have mr
8389
04:46:52,080 --> 04:46:52,638
krabs
8390
04:46:52,638 --> 04:46:54,560
then patrick sandy spongebob and
8391
04:46:54,560 --> 04:46:57,120
squidward the sort method of lists can
8392
04:46:57,120 --> 04:46:58,718
accept keyword arguments
8393
04:46:58,718 --> 04:47:00,560
there are two optional keyword arguments
8394
04:47:00,560 --> 04:47:02,080
that we can pass in we can pass
8395
04:47:02,080 --> 04:47:05,840
in key and or reverse we'll cover key a
8396
04:47:05,840 --> 04:47:06,878
little bit later
8397
04:47:06,878 --> 04:47:09,920
if we set reverse to true then our list
8398
04:47:09,920 --> 04:47:12,240
will be sorted by reverse alphabetical
8399
04:47:12,240 --> 04:47:13,520
order beginning with
8400
04:47:13,520 --> 04:47:16,160
squidward spongebob sandy patrick then
8401
04:47:16,160 --> 04:47:17,520
mr krabs
8402
04:47:17,520 --> 04:47:19,600
now the sort method does not work with
8403
04:47:19,600 --> 04:47:21,840
other iterables it's a built-in method
8404
04:47:21,840 --> 04:47:22,958
for lists
8405
04:47:22,958 --> 04:47:25,280
if our list of students was instead
8406
04:47:25,280 --> 04:47:26,718
let's say a tuple
8407
04:47:26,718 --> 04:47:28,560
well then the sort method is not going
8408
04:47:28,560 --> 04:47:30,000
to work you can see here that
8409
04:47:30,000 --> 04:47:32,240
we have an attribute error tuple object
8410
04:47:32,240 --> 04:47:34,400
has no attribute of sort
8411
04:47:34,400 --> 04:47:36,000
that's where the sort function would
8412
04:47:36,000 --> 04:47:38,080
come in because that's useful for other
8413
04:47:38,080 --> 04:47:38,798
iterables
8414
04:47:38,798 --> 04:47:40,320
you can also use this for a list 2 i
8415
04:47:40,320 --> 04:47:42,958
suppose the sort function will return a
8416
04:47:42,958 --> 04:47:45,760
sorted list so let's use the sort
8417
04:47:45,760 --> 04:47:47,120
function this time
8418
04:47:47,120 --> 04:47:50,240
and we'll assign the result to a list
8419
04:47:50,240 --> 04:47:53,680
called sorted students
8420
04:47:53,680 --> 04:47:57,360
equals sorted and then we need to pass
8421
04:47:57,360 --> 04:47:57,600
in
8422
04:47:57,600 --> 04:47:59,360
an iterable and we have the option of
8423
04:47:59,360 --> 04:48:01,200
passing in a key
8424
04:48:01,200 --> 04:48:03,840
and or reverse the iterable that we'll
8425
04:48:03,840 --> 04:48:06,000
pass in as an argument is our tuple
8426
04:48:06,000 --> 04:48:06,480
named
8427
04:48:06,480 --> 04:48:09,200
students so this line will be sorted
8428
04:48:09,200 --> 04:48:10,000
students
8429
04:48:10,000 --> 04:48:13,040
equals sorted students sorted students
8430
04:48:13,040 --> 04:48:14,718
is a list the sorted function will
8431
04:48:14,718 --> 04:48:17,280
return a list but it accepts an iterable
8432
04:48:17,280 --> 04:48:18,400
as an argument
8433
04:48:18,400 --> 04:48:20,638
and to display this we should change 4i
8434
04:48:20,638 --> 04:48:21,440
and students
8435
04:48:21,440 --> 04:48:25,280
to 4i in sorted students and now
8436
04:48:25,280 --> 04:48:27,520
this function will accept our iterable
8437
04:48:27,520 --> 04:48:28,480
our tuple
8438
04:48:28,480 --> 04:48:30,958
and create a sorted list and all of
8439
04:48:30,958 --> 04:48:32,798
these are in alphabetical order
8440
04:48:32,798 --> 04:48:35,120
and to reverse this we can pass in the
8441
04:48:35,120 --> 04:48:36,560
keyword argument of
8442
04:48:36,560 --> 04:48:39,920
reverse equals true and now our list is
8443
04:48:39,920 --> 04:48:40,718
sorted in
8444
04:48:40,718 --> 04:48:42,958
reverse alphabetical order all right
8445
04:48:42,958 --> 04:48:44,240
we're gonna take it up a level
8446
04:48:44,240 --> 04:48:46,638
welcome to level two sometimes data
8447
04:48:46,638 --> 04:48:48,878
isn't always as simple here we have a
8448
04:48:48,878 --> 04:48:51,440
list of tuples each tuple has a
8449
04:48:51,440 --> 04:48:53,200
corresponding student record
8450
04:48:53,200 --> 04:48:55,600
we have a name a letter grade for their
8451
04:48:55,600 --> 04:48:56,480
college course
8452
04:48:56,480 --> 04:48:58,718
and the student's age now how can we
8453
04:48:58,718 --> 04:48:59,840
sort this
8454
04:48:59,840 --> 04:49:02,638
list of tuples by either the student's
8455
04:49:02,638 --> 04:49:03,360
name
8456
04:49:03,360 --> 04:49:05,520
their grade or their age well that's
8457
04:49:05,520 --> 04:49:06,638
where the key
8458
04:49:06,638 --> 04:49:08,400
keyword argument's going to come in with
8459
04:49:08,400 --> 04:49:10,320
sorting if you take a look at this list
8460
04:49:10,320 --> 04:49:12,320
of tuples it somewhat resembles a
8461
04:49:12,320 --> 04:49:14,320
spreadsheet there's rows and then
8462
04:49:14,320 --> 04:49:15,360
there's columns
8463
04:49:15,360 --> 04:49:17,520
the first column corresponds to student
8464
04:49:17,520 --> 04:49:19,200
names the second column
8465
04:49:19,200 --> 04:49:21,440
are grades and the third column are all
8466
04:49:21,440 --> 04:49:23,360
the ages of the students
8467
04:49:23,360 --> 04:49:25,920
so by default sorting by the first
8468
04:49:25,920 --> 04:49:27,520
column is actually really easy that's
8469
04:49:27,520 --> 04:49:28,320
the default
8470
04:49:28,320 --> 04:49:30,400
so if we need to sort alphabetically
8471
04:49:30,400 --> 04:49:31,760
that would be the same process
8472
04:49:31,760 --> 04:49:33,760
as before we would type the name of the
8473
04:49:33,760 --> 04:49:35,200
list students
8474
04:49:35,200 --> 04:49:38,798
dot sort but if we were to print
8475
04:49:38,798 --> 04:49:42,480
this iterable for i in students
8476
04:49:42,480 --> 04:49:44,400
instead of just the individual student
8477
04:49:44,400 --> 04:49:45,840
names we're going to get
8478
04:49:45,840 --> 04:49:48,878
each tuple that we have so now all of
8479
04:49:48,878 --> 04:49:50,718
these tuples are arranged in
8480
04:49:50,718 --> 04:49:52,560
alphabetical order starting with
8481
04:49:52,560 --> 04:49:54,718
the first column that we have which are
8482
04:49:54,718 --> 04:49:56,000
all the student names
8483
04:49:56,000 --> 04:49:58,560
now how can we sort these iterables by
8484
04:49:58,560 --> 04:49:59,760
their second column
8485
04:49:59,760 --> 04:50:01,840
for this case it would be student grades
8486
04:50:01,840 --> 04:50:03,840
or even the third column which would be
8487
04:50:03,840 --> 04:50:04,958
the student ages
8488
04:50:04,958 --> 04:50:07,040
well that's where the key keyword
8489
04:50:07,040 --> 04:50:08,400
argument's going to come in
8490
04:50:08,400 --> 04:50:10,798
this is a keyword argument and we set
8491
04:50:10,798 --> 04:50:13,040
key equal to a function that's going to
8492
04:50:13,040 --> 04:50:13,920
return
8493
04:50:13,920 --> 04:50:16,638
the index of that specific column that
8494
04:50:16,638 --> 04:50:17,440
we have
8495
04:50:17,440 --> 04:50:20,638
so let's say key equals grid grade is
8496
04:50:20,638 --> 04:50:22,878
going to be a function object
8497
04:50:22,878 --> 04:50:26,000
grade equals and we can easily use a
8498
04:50:26,000 --> 04:50:29,120
lambda expression for this lambda
8499
04:50:29,120 --> 04:50:32,480
let's say grades colon
8500
04:50:32,480 --> 04:50:36,320
grades and we will set an index of one
8501
04:50:36,320 --> 04:50:39,040
now the first index is zero that's the
8502
04:50:39,040 --> 04:50:39,520
first
8503
04:50:39,520 --> 04:50:41,200
column because computers always start
8504
04:50:41,200 --> 04:50:44,000
with zero column two would have an index
8505
04:50:44,000 --> 04:50:44,798
of one
8506
04:50:44,798 --> 04:50:47,200
and then column three would have an
8507
04:50:47,200 --> 04:50:48,560
index of two
8508
04:50:48,560 --> 04:50:51,760
so grade equals lambda grids
8509
04:50:51,760 --> 04:50:54,240
colon grids index one you can also
8510
04:50:54,240 --> 04:50:55,760
rename these if you want
8511
04:50:55,760 --> 04:50:58,638
so students dot sort we're setting the
8512
04:50:58,638 --> 04:50:59,120
key
8513
04:50:59,120 --> 04:51:01,520
equal to grade and grade is a function
8514
04:51:01,520 --> 04:51:02,240
object
8515
04:51:02,240 --> 04:51:05,040
via a lambda function and now all of
8516
04:51:05,040 --> 04:51:05,840
these students
8517
04:51:05,840 --> 04:51:08,000
will be sorted by their grades starting
8518
04:51:08,000 --> 04:51:09,040
with sandy
8519
04:51:09,040 --> 04:51:11,840
then spongebob mr krabs patrick then
8520
04:51:11,840 --> 04:51:12,798
squidward
8521
04:51:12,798 --> 04:51:15,040
if this needs to be in reverse order we
8522
04:51:15,040 --> 04:51:16,958
can pass in that other keyword argument
8523
04:51:16,958 --> 04:51:17,920
of reverse
8524
04:51:17,920 --> 04:51:21,440
equals true and for practice if you
8525
04:51:21,440 --> 04:51:23,440
wanted to sort all of this data by each
8526
04:51:23,440 --> 04:51:24,400
student's age
8527
04:51:24,400 --> 04:51:26,480
then we would change grade to let's say
8528
04:51:26,480 --> 04:51:28,480
age age equals lambda
8529
04:51:28,480 --> 04:51:31,760
we'll change grades to maybe ages colon
8530
04:51:31,760 --> 04:51:34,878
ages and the index would then be two
8531
04:51:34,878 --> 04:51:37,600
because the first column here is zero
8532
04:51:37,600 --> 04:51:38,480
then one
8533
04:51:38,480 --> 04:51:41,200
then two and change key to our function
8534
04:51:41,200 --> 04:51:42,000
object
8535
04:51:42,000 --> 04:51:45,440
of edge now each student is
8536
04:51:45,440 --> 04:51:47,920
sorted numerically beginning with the
8537
04:51:47,920 --> 04:51:48,958
smallest edge
8538
04:51:48,958 --> 04:51:50,480
well the youngest person starting with
8539
04:51:50,480 --> 04:51:52,878
spongebob sandy patrick squidward then
8540
04:51:52,878 --> 04:51:53,840
mr krabs
8541
04:51:53,840 --> 04:51:56,080
and like i said before if you want your
8542
04:51:56,080 --> 04:51:58,400
data arranged in reverse order you can
8543
04:51:58,400 --> 04:51:58,718
set
8544
04:51:58,718 --> 04:52:01,360
that keyword argument of reverse to true
8545
04:52:01,360 --> 04:52:02,958
within the sort method
8546
04:52:02,958 --> 04:52:05,040
and all of our student data is sorted
8547
04:52:05,040 --> 04:52:07,120
beginning with the eldest student which
8548
04:52:07,120 --> 04:52:08,320
is mr krabs
8549
04:52:08,320 --> 04:52:10,560
then squidward patrick sandy and then
8550
04:52:10,560 --> 04:52:11,520
spongebob
8551
04:52:11,520 --> 04:52:13,040
and for the last part of this video
8552
04:52:13,040 --> 04:52:14,958
let's say that we're working with
8553
04:52:14,958 --> 04:52:17,360
some other iterable let's say we have a
8554
04:52:17,360 --> 04:52:19,200
tuple of tuples instead
8555
04:52:19,200 --> 04:52:21,520
well we can no longer use this sort
8556
04:52:21,520 --> 04:52:23,280
method because that only belongs to
8557
04:52:23,280 --> 04:52:24,160
lists
8558
04:52:24,160 --> 04:52:25,840
you can also use the sort function with
8559
04:52:25,840 --> 04:52:27,840
lists and it will generate a new sorted
8560
04:52:27,840 --> 04:52:29,680
list without changing the placement of
8561
04:52:29,680 --> 04:52:30,638
the original
8562
04:52:30,638 --> 04:52:33,120
so let's say we have a tuple of tuples
8563
04:52:33,120 --> 04:52:34,240
and this will create a
8564
04:52:34,240 --> 04:52:37,040
new sorted list let's call this list
8565
04:52:37,040 --> 04:52:38,480
sorted
8566
04:52:38,480 --> 04:52:41,680
students equals then we'll use the
8567
04:52:41,680 --> 04:52:43,920
sorted function we need to pass in an
8568
04:52:43,920 --> 04:52:46,320
iterable and we can pass in
8569
04:52:46,320 --> 04:52:48,958
both a key and or the reverse keyword
8570
04:52:48,958 --> 04:52:49,920
argument
8571
04:52:49,920 --> 04:52:53,360
so the iterable would be students
8572
04:52:53,360 --> 04:52:56,878
and let's set the key equal to edge
8573
04:52:56,878 --> 04:52:58,718
and i will not reverse it we can keep it
8574
04:52:58,718 --> 04:53:00,480
as it is but let's change
8575
04:53:00,480 --> 04:53:04,320
for i in students to sorted students
8576
04:53:04,320 --> 04:53:06,080
and that's how to use the sorted
8577
04:53:06,080 --> 04:53:07,680
function to sort
8578
04:53:07,680 --> 04:53:10,400
an iterable including a list well all
8579
04:53:10,400 --> 04:53:11,280
right everyone
8580
04:53:11,280 --> 04:53:13,680
that's how to sort iterables in python
8581
04:53:13,680 --> 04:53:15,040
if you would like a copy of all this
8582
04:53:15,040 --> 04:53:16,718
code i will post all of this to the
8583
04:53:16,718 --> 04:53:18,320
comments section down below
8584
04:53:18,320 --> 04:53:20,638
but yeah that's a basic way to sort
8585
04:53:20,638 --> 04:53:21,360
iterables
8586
04:53:21,360 --> 04:53:24,560
in python hey what's going on everybody
8587
04:53:24,560 --> 04:53:26,240
it's your bro hope you're doing well in
8588
04:53:26,240 --> 04:53:27,680
today's video i'm going to show you all
8589
04:53:27,680 --> 04:53:30,160
how the map function works in python so
8590
04:53:30,160 --> 04:53:34,798
sit back relax and enjoy the show
8591
04:53:34,798 --> 04:53:36,878
what up everybody let's discuss the map
8592
04:53:36,878 --> 04:53:39,120
function the map function will apply a
8593
04:53:39,120 --> 04:53:41,520
function to each item in an interval
8594
04:53:41,520 --> 04:53:43,840
that would include lists tuples things
8595
04:53:43,840 --> 04:53:44,560
like that
8596
04:53:44,560 --> 04:53:46,400
and the map function accepts two
8597
04:53:46,400 --> 04:53:48,160
arguments are iterable
8598
04:53:48,160 --> 04:53:50,000
as well as the function we would like to
8599
04:53:50,000 --> 04:53:52,638
apply to each item within our iterable
8600
04:53:52,638 --> 04:53:54,240
for example let's say that we have an
8601
04:53:54,240 --> 04:53:56,798
online store to represent this i have
8602
04:53:56,798 --> 04:53:59,920
a list of tuples named store each tuple
8603
04:53:59,920 --> 04:54:01,040
within this list
8604
04:54:01,040 --> 04:54:03,200
corresponds to an item for sale within
8605
04:54:03,200 --> 04:54:04,080
our store
8606
04:54:04,080 --> 04:54:06,638
there's shirts pants jackets and socks
8607
04:54:06,638 --> 04:54:07,840
and each item has
8608
04:54:07,840 --> 04:54:10,958
a price in us dollars let's say now what
8609
04:54:10,958 --> 04:54:12,560
i would like to do is to convert all of
8610
04:54:12,560 --> 04:54:14,400
the prices within my store
8611
04:54:14,400 --> 04:54:16,560
to euros and currently the conversion
8612
04:54:16,560 --> 04:54:18,878
rate as of the filming of this video is
8613
04:54:18,878 --> 04:54:22,000
one us dollar to 0.82 euros
8614
04:54:22,000 --> 04:54:24,000
so let's begin we can easily convert
8615
04:54:24,000 --> 04:54:25,520
dollars to euros with a
8616
04:54:25,520 --> 04:54:29,520
lambda function so let's say two euros
8617
04:54:29,520 --> 04:54:34,240
equals lambda the parameter will be data
8618
04:54:34,240 --> 04:54:36,160
colon and the next part's a little bit
8619
04:54:36,160 --> 04:54:38,080
tricky within a set of parenthesis to
8620
04:54:38,080 --> 04:54:39,920
represent a tuple
8621
04:54:39,920 --> 04:54:42,878
we'll take data at index 0 that
8622
04:54:42,878 --> 04:54:45,600
corresponds to the first column
8623
04:54:45,600 --> 04:54:49,120
comma data at index 1.
8624
04:54:49,120 --> 04:54:52,000
this represents the index of our prices
8625
04:54:52,000 --> 04:54:52,798
we're going to take
8626
04:54:52,798 --> 04:54:57,200
data at index 1 times 0.82
8627
04:54:57,200 --> 04:54:59,440
and that's it the first column will be
8628
04:54:59,440 --> 04:55:00,480
left untouched
8629
04:55:00,480 --> 04:55:03,840
but the data in column 1 will be
8630
04:55:03,840 --> 04:55:05,840
multiplied by 0.82
8631
04:55:05,840 --> 04:55:07,840
now our map function is going to create
8632
04:55:07,840 --> 04:55:10,480
a map object but we can easily cast that
8633
04:55:10,480 --> 04:55:12,560
to a different type of iterable
8634
04:55:12,560 --> 04:55:16,080
so let's say that store underscore
8635
04:55:16,080 --> 04:55:19,760
euros equals map
8636
04:55:19,760 --> 04:55:21,840
parenthesis and then we can pass in our
8637
04:55:21,840 --> 04:55:23,760
function as well as our iterable
8638
04:55:23,760 --> 04:55:27,120
our function is our two euros function
8639
04:55:27,120 --> 04:55:31,520
comma and our iterable is our store
8640
04:55:31,520 --> 04:55:33,680
and that is it now if you want to
8641
04:55:33,680 --> 04:55:36,160
convert this map object to an iterable
8642
04:55:36,160 --> 04:55:38,160
you can easily surround this with a cast
8643
04:55:38,160 --> 04:55:40,878
i would like to convert this map object
8644
04:55:40,878 --> 04:55:42,000
to a list
8645
04:55:42,000 --> 04:55:44,878
and we will now have a new store named
8646
04:55:44,878 --> 04:55:45,520
store
8647
04:55:45,520 --> 04:55:47,360
euros which will have all of the
8648
04:55:47,360 --> 04:55:49,680
different prices of our store in euros
8649
04:55:49,680 --> 04:55:52,000
and to display this i'll use a for loop
8650
04:55:52,000 --> 04:55:52,958
for i
8651
04:55:52,958 --> 04:55:56,560
in store euros
8652
04:55:56,560 --> 04:56:00,000
print i and
8653
04:56:00,000 --> 04:56:03,520
let's try it yep that seems about right
8654
04:56:03,520 --> 04:56:05,600
all of the prices in our store are now
8655
04:56:05,600 --> 04:56:06,638
in euros
8656
04:56:06,638 --> 04:56:08,560
let's pretend that these prices are
8657
04:56:08,560 --> 04:56:10,240
already in euros and we would like to
8658
04:56:10,240 --> 04:56:10,958
convert these
8659
04:56:10,958 --> 04:56:13,280
to american dollars so we will use the
8660
04:56:13,280 --> 04:56:15,120
same conversion rate but divide
8661
04:56:15,120 --> 04:56:18,000
by 0.82 so let's create a separate
8662
04:56:18,000 --> 04:56:19,280
lambda function
8663
04:56:19,280 --> 04:56:22,400
two dollars equals
8664
04:56:22,400 --> 04:56:25,520
i think i'll just copy this but we are
8665
04:56:25,520 --> 04:56:26,638
going to divide
8666
04:56:26,638 --> 04:56:30,958
the first index of data by 0.82
8667
04:56:30,958 --> 04:56:34,718
and let's say store dollars to represent
8668
04:56:34,718 --> 04:56:35,360
our store
8669
04:56:35,360 --> 04:56:37,200
in dollars because we're pretending now
8670
04:56:37,200 --> 04:56:40,480
that it's currently in euros
8671
04:56:41,680 --> 04:56:45,840
and we will display 4i in store dollars
8672
04:56:45,840 --> 04:56:48,560
so if all of these prices are in euros
8673
04:56:48,560 --> 04:56:50,400
we are now converting them to american
8674
04:56:50,400 --> 04:56:51,280
dollars
8675
04:56:51,280 --> 04:56:52,718
but you may want to do some rounding as
8676
04:56:52,718 --> 04:56:54,480
opposed to the nearest descent
8677
04:56:54,480 --> 04:56:56,638
well everyone that is the map function
8678
04:56:56,638 --> 04:56:58,320
it applies a function to
8679
04:56:58,320 --> 04:57:00,638
each item in an iterable those include
8680
04:57:00,638 --> 04:57:01,760
lists tuples
8681
04:57:01,760 --> 04:57:04,718
the like you pass in your iterable and
8682
04:57:04,718 --> 04:57:07,040
your function as an argument to the map
8683
04:57:07,040 --> 04:57:09,200
function so if you would like a copy of
8684
04:57:09,200 --> 04:57:11,040
all this code i will post all of this to
8685
04:57:11,040 --> 04:57:12,638
the comment section down below
8686
04:57:12,638 --> 04:57:14,798
but yeah that is how the map function
8687
04:57:14,798 --> 04:57:17,840
works in python
8688
04:57:17,840 --> 04:57:19,760
what's going on everybody it's you bro
8689
04:57:19,760 --> 04:57:21,360
hope you're doing well and in today's
8690
04:57:21,360 --> 04:57:22,958
video i'm going to show you all how the
8691
04:57:22,958 --> 04:57:24,878
filter function works in python
8692
04:57:24,878 --> 04:57:29,920
so sit back relax and enjoy the show
8693
04:57:30,000 --> 04:57:32,320
all right all right let's discuss the
8694
04:57:32,320 --> 04:57:33,440
filter function
8695
04:57:33,440 --> 04:57:35,600
the filter function creates a collection
8696
04:57:35,600 --> 04:57:36,878
of elements from an
8697
04:57:36,878 --> 04:57:39,680
iterable for which a function returns
8698
04:57:39,680 --> 04:57:40,400
true
8699
04:57:40,400 --> 04:57:42,320
now what the heck does that mean let me
8700
04:57:42,320 --> 04:57:44,958
explain let's say that we have a list of
8701
04:57:44,958 --> 04:57:45,840
tuples named
8702
04:57:45,840 --> 04:57:48,080
friends each tuple has the name of a
8703
04:57:48,080 --> 04:57:49,920
friend as well as their age
8704
04:57:49,920 --> 04:57:51,360
i just made up some ages for all of
8705
04:57:51,360 --> 04:57:53,120
these friends what i would like to do is
8706
04:57:53,120 --> 04:57:53,840
to create a
8707
04:57:53,840 --> 04:57:55,920
separate list for all the friends that
8708
04:57:55,920 --> 04:57:57,360
are 18 or older
8709
04:57:57,360 --> 04:57:59,200
so let's say that we're all going out
8710
04:57:59,200 --> 04:58:01,120
drinking and i would like to create a
8711
04:58:01,120 --> 04:58:02,638
list of my drinking buddies
8712
04:58:02,638 --> 04:58:04,718
because they're of drinking age i'm in
8713
04:58:04,718 --> 04:58:06,240
the united states and the drinking age
8714
04:58:06,240 --> 04:58:07,520
is currently 21
8715
04:58:07,520 --> 04:58:09,760
but i think worldwide is 18 so let's go
8716
04:58:09,760 --> 04:58:11,360
with 18 for this example since
8717
04:58:11,360 --> 04:58:13,440
i have a worldwide audience all right
8718
04:58:13,440 --> 04:58:15,120
now with this filter function
8719
04:58:15,120 --> 04:58:17,520
we have to pass in our iterable as well
8720
04:58:17,520 --> 04:58:18,560
as a function
8721
04:58:18,560 --> 04:58:21,200
to get anybody's edge that is over 18.
8722
04:58:21,200 --> 04:58:22,798
so let's begin by writing a
8723
04:58:22,798 --> 04:58:24,958
lambda function for this expression here
8724
04:58:24,958 --> 04:58:26,160
our filter function
8725
04:58:26,160 --> 04:58:30,560
so let's say age equals lambda
8726
04:58:30,560 --> 04:58:34,400
data will be the parameter colon data
8727
04:58:34,400 --> 04:58:38,080
at index one so the first index
8728
04:58:38,080 --> 04:58:40,240
the first column in this chart here
8729
04:58:40,240 --> 04:58:42,480
would be zero for other names the next
8730
04:58:42,480 --> 04:58:45,280
index of one would be all the ages
8731
04:58:45,280 --> 04:58:48,320
data at index one is greater than
8732
04:58:48,320 --> 04:58:51,600
or equal to eighteen and that is it
8733
04:58:51,600 --> 04:58:53,680
that is our lambda function and now
8734
04:58:53,680 --> 04:58:55,040
we're going to filter
8735
04:58:55,040 --> 04:58:57,840
our iterable by this function of age so
8736
04:58:57,840 --> 04:58:59,120
let's write
8737
04:58:59,120 --> 04:59:02,240
filter pass in our function
8738
04:59:02,240 --> 04:59:06,000
which is edge comma and our iterable
8739
04:59:06,000 --> 04:59:08,878
friends now this filter function will
8740
04:59:08,878 --> 04:59:10,000
return a filter
8741
04:59:10,000 --> 04:59:11,840
object you can easily cast this i would
8742
04:59:11,840 --> 04:59:13,280
like to cast
8743
04:59:13,280 --> 04:59:17,360
my friends list back into a list
8744
04:59:17,360 --> 04:59:19,520
and i will assign this to a new list
8745
04:59:19,520 --> 04:59:20,400
let's say
8746
04:59:20,400 --> 04:59:24,240
drinking buddies
8747
04:59:24,240 --> 04:59:27,600
equals list filter age
8748
04:59:27,600 --> 04:59:30,240
and friends and i should be able to
8749
04:59:30,240 --> 04:59:30,718
print
8750
04:59:30,718 --> 04:59:33,280
all of the friends that are 18 or older
8751
04:59:33,280 --> 04:59:34,400
for i
8752
04:59:34,400 --> 04:59:38,160
in drinking bodies
8753
04:59:38,160 --> 04:59:42,080
print i
8754
04:59:42,080 --> 04:59:44,878
and we have rachel monica chandler and
8755
04:59:44,878 --> 04:59:45,840
ross
8756
04:59:45,840 --> 04:59:47,600
well everyone that is the filter
8757
04:59:47,600 --> 04:59:49,600
function it creates a collection of
8758
04:59:49,600 --> 04:59:50,240
elements
8759
04:59:50,240 --> 04:59:52,400
from an iterable for which a function
8760
04:59:52,400 --> 04:59:53,520
returns true
8761
04:59:53,520 --> 04:59:55,520
i like to think of it as the search
8762
04:59:55,520 --> 04:59:57,760
results we're searching for any results
8763
04:59:57,760 --> 05:00:00,160
that meet this criteria where age is
8764
05:00:00,160 --> 05:00:02,160
greater than or equal to 18
8765
05:00:02,160 --> 05:00:03,600
and then you can create a separate
8766
05:00:03,600 --> 05:00:05,280
collection or other list if you want
8767
05:00:05,280 --> 05:00:06,718
from the results
8768
05:00:06,718 --> 05:00:08,718
but yeah that is how the filter function
8769
05:00:08,718 --> 05:00:10,480
works in python
8770
05:00:10,480 --> 05:00:12,958
yo what's going on everybody it's bro
8771
05:00:12,958 --> 05:00:13,920
hope you're doing well
8772
05:00:13,920 --> 05:00:15,360
and in today's video i'm going to show
8773
05:00:15,360 --> 05:00:17,200
you how the reduce function works in
8774
05:00:17,200 --> 05:00:18,160
python so
8775
05:00:18,160 --> 05:00:22,560
sit back relax and enjoy the show
8776
05:00:22,560 --> 05:00:24,400
well i suppose we're on the reduce
8777
05:00:24,400 --> 05:00:26,320
function today the reduce function
8778
05:00:26,320 --> 05:00:28,400
applies a function of our choosing
8779
05:00:28,400 --> 05:00:31,520
to an iterable and reduces that iterable
8780
05:00:31,520 --> 05:00:34,240
to a single cumulative value the way
8781
05:00:34,240 --> 05:00:36,000
this works is that the reduce function
8782
05:00:36,000 --> 05:00:37,440
performs our function
8783
05:00:37,440 --> 05:00:39,280
on the first two elements of our
8784
05:00:39,280 --> 05:00:41,360
iterable and repeats the process
8785
05:00:41,360 --> 05:00:44,320
until only one value remains i kind of
8786
05:00:44,320 --> 05:00:45,440
like to think of it like
8787
05:00:45,440 --> 05:00:47,200
we're recycling elements within an
8788
05:00:47,200 --> 05:00:49,120
iterable until a single
8789
05:00:49,120 --> 05:00:51,440
value remains our finished product so
8790
05:00:51,440 --> 05:00:53,600
how is this useful let's say that
8791
05:00:53,600 --> 05:00:55,360
we're playing a game scramble and we
8792
05:00:55,360 --> 05:00:57,440
have a bunch of different letters within
8793
05:00:57,440 --> 05:00:58,400
an iterable
8794
05:00:58,400 --> 05:01:01,120
a list is a type of iterable so i have a
8795
05:01:01,120 --> 05:01:03,200
list named letters and we have all of
8796
05:01:03,200 --> 05:01:05,200
the letters we need to spell the word
8797
05:01:05,200 --> 05:01:07,520
what i would like to do is to reduce all
8798
05:01:07,520 --> 05:01:09,600
of these individual elements into a
8799
05:01:09,600 --> 05:01:12,638
single cumulative value until only one
8800
05:01:12,638 --> 05:01:14,958
value remains so we can do that using
8801
05:01:14,958 --> 05:01:16,718
the reduce function you can also do this
8802
05:01:16,718 --> 05:01:18,160
with the for loop too there's multiple
8803
05:01:18,160 --> 05:01:19,360
ways of doing this
8804
05:01:19,360 --> 05:01:21,840
so using the reduce function we're going
8805
05:01:21,840 --> 05:01:23,440
to import
8806
05:01:23,440 --> 05:01:28,320
func tools and now let's say we have
8807
05:01:28,320 --> 05:01:30,718
our cumulative value stored within a
8808
05:01:30,718 --> 05:01:32,320
variable named word
8809
05:01:32,320 --> 05:01:36,718
word equals func tools dot reduce
8810
05:01:36,718 --> 05:01:38,400
now the reduce function has two
8811
05:01:38,400 --> 05:01:40,798
arguments the function we would like to
8812
05:01:40,798 --> 05:01:41,280
apply
8813
05:01:41,280 --> 05:01:43,520
to the first two elements and then our
8814
05:01:43,520 --> 05:01:44,400
iterable
8815
05:01:44,400 --> 05:01:45,920
the interval is easy that is just
8816
05:01:45,920 --> 05:01:48,000
letters so let's put that in
8817
05:01:48,000 --> 05:01:50,000
letters and for the function we can
8818
05:01:50,000 --> 05:01:52,480
easily write a lambda function for this
8819
05:01:52,480 --> 05:01:55,520
lambda and now this function needs two
8820
05:01:55,520 --> 05:01:56,718
parameters
8821
05:01:56,718 --> 05:02:00,320
let's say x and y now what kind of
8822
05:02:00,320 --> 05:02:02,240
function do we want to perform on the
8823
05:02:02,240 --> 05:02:04,718
first two elements of our iterable
8824
05:02:04,718 --> 05:02:07,120
let's combine them together so let's say
8825
05:02:07,120 --> 05:02:07,760
x
8826
05:02:07,760 --> 05:02:10,878
plus y and now
8827
05:02:10,878 --> 05:02:14,638
let's print our single cumulative value
8828
05:02:14,638 --> 05:02:18,320
of word so this should print
8829
05:02:18,320 --> 05:02:21,040
the word hello i'm fairly confident that
8830
05:02:21,040 --> 05:02:22,718
the word hello is worth like
8831
05:02:22,718 --> 05:02:24,798
20 points in scrabble now let me explain
8832
05:02:24,798 --> 05:02:26,000
what happened step by
8833
05:02:26,000 --> 05:02:28,560
step our reduce function applies our
8834
05:02:28,560 --> 05:02:29,120
function
8835
05:02:29,120 --> 05:02:31,760
to the first two elements within our
8836
05:02:31,760 --> 05:02:32,638
iterable
8837
05:02:32,638 --> 05:02:35,360
it performs this expression that we set
8838
05:02:35,360 --> 05:02:37,040
x plus y but really we can write
8839
05:02:37,040 --> 05:02:38,160
anything here
8840
05:02:38,160 --> 05:02:41,040
so we perform this expression on the
8841
05:02:41,040 --> 05:02:43,360
first two elements
8842
05:02:43,360 --> 05:02:46,160
then we repeat the process again using
8843
05:02:46,160 --> 05:02:46,958
the result
8844
05:02:46,958 --> 05:02:48,560
from the first time we use this
8845
05:02:48,560 --> 05:02:50,718
expression and the next element
8846
05:02:50,718 --> 05:02:54,798
so we're combining x plus y again
8847
05:02:54,798 --> 05:02:56,480
and then we're just going to repeat this
8848
05:02:56,480 --> 05:02:58,240
process until only
8849
05:02:58,240 --> 05:03:00,400
one value remains it's kind of like
8850
05:03:00,400 --> 05:03:01,360
we're recycling
8851
05:03:01,360 --> 05:03:03,280
all of these letters and creating a
8852
05:03:03,280 --> 05:03:05,120
finished product out of all of these
8853
05:03:05,120 --> 05:03:07,360
that's how i think of it at least here's
8854
05:03:07,360 --> 05:03:08,958
a different example let's say that i
8855
05:03:08,958 --> 05:03:10,080
would like to find
8856
05:03:10,080 --> 05:03:13,680
the factorial of 5 so i have the numbers
8857
05:03:13,680 --> 05:03:16,560
5 through 1 within a list named
8858
05:03:16,560 --> 05:03:17,600
factorial
8859
05:03:17,600 --> 05:03:20,320
so if i was to find the factorial of 5
8860
05:03:20,320 --> 05:03:21,760
via the reduce function
8861
05:03:21,760 --> 05:03:23,280
i would probably write something like
8862
05:03:23,280 --> 05:03:25,280
this so let me change
8863
05:03:25,280 --> 05:03:28,480
word to let's say result result
8864
05:03:28,480 --> 05:03:31,840
equals functools reduce now our lambda
8865
05:03:31,840 --> 05:03:32,878
function will be
8866
05:03:32,878 --> 05:03:36,160
x times y because with factorials we
8867
05:03:36,160 --> 05:03:37,440
multiply
8868
05:03:37,440 --> 05:03:40,160
the next two numbers together then we
8869
05:03:40,160 --> 05:03:41,120
take that result
8870
05:03:41,120 --> 05:03:42,958
and then multiply the next one in this
8871
05:03:42,958 --> 05:03:44,480
list so the
8872
05:03:44,480 --> 05:03:47,840
iterable will be factorial and
8873
05:03:47,840 --> 05:03:51,200
let's print what our result is
8874
05:03:51,200 --> 05:03:55,040
so the factorial of 5 should be 120. now
8875
05:03:55,040 --> 05:03:56,798
let's walk through this we're taking
8876
05:03:56,798 --> 05:03:59,360
the first two elements of this iterable
8877
05:03:59,360 --> 05:03:59,840
five
8878
05:03:59,840 --> 05:04:02,080
and four multiplying together in this
8879
05:04:02,080 --> 05:04:05,040
case in the last example we concatenated
8880
05:04:05,040 --> 05:04:07,040
the strings together of x and y
8881
05:04:07,040 --> 05:04:08,560
in this example we're multiplying them
8882
05:04:08,560 --> 05:04:10,240
together so we take
8883
05:04:10,240 --> 05:04:13,360
the first two elements 5 times 4
8884
05:04:13,360 --> 05:04:15,760
we take the result and apply it to the
8885
05:04:15,760 --> 05:04:17,120
next iteration
8886
05:04:17,120 --> 05:04:20,638
of x times y again so 20 times 3
8887
05:04:20,638 --> 05:04:24,240
is 60. we repeat this process again
8888
05:04:24,240 --> 05:04:28,240
60 times 2 is 120 and 120 times 1
8889
05:04:28,240 --> 05:04:31,600
is 120 so that's kind of how the reduce
8890
05:04:31,600 --> 05:04:32,000
function
8891
05:04:32,000 --> 05:04:34,000
works it applies a function of our
8892
05:04:34,000 --> 05:04:36,080
choosing to the first
8893
05:04:36,080 --> 05:04:38,560
two elements of an iterable and repeats
8894
05:04:38,560 --> 05:04:39,760
that process until
8895
05:04:39,760 --> 05:04:43,040
only a single cumulative value remains
8896
05:04:43,040 --> 05:04:44,958
so that is the reduce function if you
8897
05:04:44,958 --> 05:04:46,560
would like a copy of all this code i
8898
05:04:46,560 --> 05:04:48,160
will post all of this to the comment
8899
05:04:48,160 --> 05:04:49,440
section down below
8900
05:04:49,440 --> 05:04:51,520
but yeah that is how the reduce function
8901
05:04:51,520 --> 05:04:54,320
works in python
8902
05:04:54,320 --> 05:04:56,400
hey what's going on everybody it's your
8903
05:04:56,400 --> 05:04:58,160
bro hope you're doing well and in this
8904
05:04:58,160 --> 05:04:59,760
video i'm going to show you all how list
8905
05:04:59,760 --> 05:05:02,000
comprehensions work in python so
8906
05:05:02,000 --> 05:05:06,560
sit back relax and enjoy the show
8907
05:05:06,560 --> 05:05:08,080
all right everybody so list
8908
05:05:08,080 --> 05:05:10,638
comprehensions a list comprehension is a
8909
05:05:10,638 --> 05:05:12,798
way to create a new list with
8910
05:05:12,798 --> 05:05:15,040
less syntax you can also use a list
8911
05:05:15,040 --> 05:05:16,560
comprehension to mimic
8912
05:05:16,560 --> 05:05:18,958
certain lambda functions such as in the
8913
05:05:18,958 --> 05:05:20,480
previous videos where we use
8914
05:05:20,480 --> 05:05:23,040
the map and the filter functions and not
8915
05:05:23,040 --> 05:05:24,798
only that but a list comprehension
8916
05:05:24,798 --> 05:05:27,440
is easier to read than a lambda function
8917
05:05:27,440 --> 05:05:29,120
but there is a formula that we're going
8918
05:05:29,120 --> 05:05:29,920
to follow
8919
05:05:29,920 --> 05:05:32,320
when we create our list within our list
8920
05:05:32,320 --> 05:05:34,160
we're going to write an expression
8921
05:05:34,160 --> 05:05:37,120
for item in iterable now let me give you
8922
05:05:37,120 --> 05:05:39,440
an example of where a list comprehension
8923
05:05:39,440 --> 05:05:40,560
would be useful
8924
05:05:40,560 --> 05:05:42,638
let's take this program that will create
8925
05:05:42,638 --> 05:05:44,320
a list of all the numbers
8926
05:05:44,320 --> 05:05:46,878
1 through 10 squared so if i were to
8927
05:05:46,878 --> 05:05:48,320
print this we'll get the numbers 1
8928
05:05:48,320 --> 05:05:48,958
through 10
8929
05:05:48,958 --> 05:05:50,798
whatever their square is right so we
8930
05:05:50,798 --> 05:05:52,638
have 1 through 10
8931
05:05:52,638 --> 05:05:55,360
and the square of 10 is 100 so we wrote
8932
05:05:55,360 --> 05:05:56,160
this program
8933
05:05:56,160 --> 05:05:58,080
in three lines of code we're creating an
8934
05:05:58,080 --> 05:06:00,320
empty list we're creating a for loop
8935
05:06:00,320 --> 05:06:01,840
and then we're writing an expression for
8936
05:06:01,840 --> 05:06:04,160
what we want each loop iteration to do
8937
05:06:04,160 --> 05:06:05,840
now let's write the same program but
8938
05:06:05,840 --> 05:06:07,680
used a list comprehension
8939
05:06:07,680 --> 05:06:09,840
and we can write the same program with
8940
05:06:09,840 --> 05:06:10,958
less syntax
8941
05:06:10,958 --> 05:06:12,718
so i would write something like this and
8942
05:06:12,718 --> 05:06:14,878
we're going to follow this formula
8943
05:06:14,878 --> 05:06:17,920
so our list is going to be named
8944
05:06:17,920 --> 05:06:21,360
squares equals then within a set of
8945
05:06:21,360 --> 05:06:22,400
straight brackets
8946
05:06:22,400 --> 05:06:24,160
we're going to write our expression
8947
05:06:24,160 --> 05:06:27,840
which is this portion i times i
8948
05:06:27,840 --> 05:06:31,520
for item so that would be 4i
8949
05:06:31,520 --> 05:06:34,638
in our iterable that would be range
8950
05:06:34,638 --> 05:06:38,080
1 through 11 because remember 11's
8951
05:06:38,080 --> 05:06:39,280
exclusive
8952
05:06:39,280 --> 05:06:43,280
and then let's print our squares print
8953
05:06:43,280 --> 05:06:47,040
squares and there you go
8954
05:06:47,040 --> 05:06:49,440
we made the exact same program using a
8955
05:06:49,440 --> 05:06:50,718
list comprehension
8956
05:06:50,718 --> 05:06:52,718
so basically speaking you just follow
8957
05:06:52,718 --> 05:06:55,680
this formula you set your list equal to
8958
05:06:55,680 --> 05:06:58,000
your expression and our expression for
8959
05:06:58,000 --> 05:07:00,480
this example is i times i
8960
05:07:00,480 --> 05:07:03,680
for item for i in
8961
05:07:03,680 --> 05:07:05,920
your interval in range one through
8962
05:07:05,920 --> 05:07:06,878
eleven
8963
05:07:06,878 --> 05:07:09,520
so this took three lines of code well if
8964
05:07:09,520 --> 05:07:11,120
you exclude us printing
8965
05:07:11,120 --> 05:07:14,160
our list and this program took one line
8966
05:07:14,160 --> 05:07:14,958
of code
8967
05:07:14,958 --> 05:07:17,200
now we can also use a list comprehension
8968
05:07:17,200 --> 05:07:19,200
to mimic certain lambda functions i
8969
05:07:19,200 --> 05:07:20,080
wrote a program
8970
05:07:20,080 --> 05:07:22,638
where we will be filtering a list of
8971
05:07:22,638 --> 05:07:23,600
student grades
8972
05:07:23,600 --> 05:07:25,360
let's pretend that all of these students
8973
05:07:25,360 --> 05:07:26,638
took some sort of exam
8974
05:07:26,638 --> 05:07:28,638
so we have one student that received a
8975
05:07:28,638 --> 05:07:32,480
100 90 80 70 60 50 40 30
8976
05:07:32,480 --> 05:07:34,000
and then one student got a zero because
8977
05:07:34,000 --> 05:07:35,680
they didn't show up so what i would like
8978
05:07:35,680 --> 05:07:36,958
to do is to filter
8979
05:07:36,958 --> 05:07:39,760
all of these student grades into a list
8980
05:07:39,760 --> 05:07:40,718
that only contains
8981
05:07:40,718 --> 05:07:42,878
students that passed and the criteria
8982
05:07:42,878 --> 05:07:44,798
for a passing grade will be
8983
05:07:44,798 --> 05:07:48,000
a 60 or above so i would like to filter
8984
05:07:48,000 --> 05:07:50,560
all passing students and if i was to
8985
05:07:50,560 --> 05:07:52,080
write this program using the filter
8986
05:07:52,080 --> 05:07:53,280
function i would probably write
8987
05:07:53,280 --> 05:07:54,400
something like this
8988
05:07:54,400 --> 05:07:56,718
so i need a function and then my list
8989
05:07:56,718 --> 05:07:57,760
that i'm working with
8990
05:07:57,760 --> 05:08:01,040
lambda x colon x is greater than or
8991
05:08:01,040 --> 05:08:02,320
equal to 60
8992
05:08:02,320 --> 05:08:04,240
will filter all of the results and then
8993
05:08:04,240 --> 05:08:06,958
convert it to a list named past students
8994
05:08:06,958 --> 05:08:08,480
so the result will look something like
8995
05:08:08,480 --> 05:08:11,280
this i have five student grades in here
8996
05:08:11,280 --> 05:08:14,400
with 100 90 80 70 and 60.
8997
05:08:14,400 --> 05:08:16,400
now let's write the same program using a
8998
05:08:16,400 --> 05:08:17,520
list comprehension
8999
05:08:17,520 --> 05:08:19,280
now there's just one portion to this
9000
05:08:19,280 --> 05:08:20,958
formula for list comprehension that
9001
05:08:20,958 --> 05:08:21,840
we're going to add
9002
05:08:21,840 --> 05:08:23,920
at the end we're going to add if
9003
05:08:23,920 --> 05:08:25,440
conditional we can check
9004
05:08:25,440 --> 05:08:27,120
some sort of condition after each
9005
05:08:27,120 --> 05:08:28,560
iteration so
9006
05:08:28,560 --> 05:08:30,320
let's write the same program using a
9007
05:08:30,320 --> 05:08:31,920
list comprehension following
9008
05:08:31,920 --> 05:08:34,560
this new updated formula that we have so
9009
05:08:34,560 --> 05:08:34,958
we need
9010
05:08:34,958 --> 05:08:38,718
our list which will be named past
9011
05:08:38,718 --> 05:08:41,200
students equals first comes our
9012
05:08:41,200 --> 05:08:41,840
expression
9013
05:08:41,840 --> 05:08:44,400
and it's just i for this example for
9014
05:08:44,400 --> 05:08:45,040
item
9015
05:08:45,040 --> 05:08:48,080
i in our iterable
9016
05:08:48,080 --> 05:08:51,680
of students and then if we can check our
9017
05:08:51,680 --> 05:08:52,638
conditional
9018
05:08:52,638 --> 05:08:56,878
if i is greater than or equal to 60
9019
05:08:56,878 --> 05:08:59,120
and this will have the same effect as
9020
05:08:59,120 --> 05:09:01,360
our previous program but instead it uses
9021
05:09:01,360 --> 05:09:03,120
a list comprehension
9022
05:09:03,120 --> 05:09:06,080
now if your program has multiple output
9023
05:09:06,080 --> 05:09:07,440
if you need an else statement within
9024
05:09:07,440 --> 05:09:08,400
your conditional
9025
05:09:08,400 --> 05:09:09,760
you're actually going to move this
9026
05:09:09,760 --> 05:09:12,000
portion of the if conditional within
9027
05:09:12,000 --> 05:09:13,520
our formula to right after the
9028
05:09:13,520 --> 05:09:15,360
expression so let's write
9029
05:09:15,360 --> 05:09:18,480
an if else statement here
9030
05:09:18,480 --> 05:09:20,320
so let's say that instead of just
9031
05:09:20,320 --> 05:09:22,638
excluding any student that didn't pass
9032
05:09:22,638 --> 05:09:24,718
we'll replace their grade with the word
9033
05:09:24,718 --> 05:09:26,000
failed and we can do that
9034
05:09:26,000 --> 05:09:28,320
using an if else statement so let's do
9035
05:09:28,320 --> 05:09:30,000
this again i'm going to copy what we
9036
05:09:30,000 --> 05:09:31,440
have here
9037
05:09:31,440 --> 05:09:33,600
paste it and we'll move our conditional
9038
05:09:33,600 --> 05:09:35,600
from the end
9039
05:09:35,600 --> 05:09:39,200
to just after the expression portion i
9040
05:09:39,200 --> 05:09:41,760
if i is greater than or equal to 60 and
9041
05:09:41,760 --> 05:09:43,680
you can see here that we need to add an
9042
05:09:43,680 --> 05:09:46,878
else statement else will return
9043
05:09:46,878 --> 05:09:49,840
the word failed
9044
05:09:51,280 --> 05:09:54,480
so we're replacing each instance of
9045
05:09:54,480 --> 05:09:56,400
a failing grade with the word failed
9046
05:09:56,400 --> 05:09:58,240
instead so if your condition
9047
05:09:58,240 --> 05:10:00,160
returns whatever i is you can just add
9048
05:10:00,160 --> 05:10:01,280
that to the end
9049
05:10:01,280 --> 05:10:02,878
if you need an if else statement you're
9050
05:10:02,878 --> 05:10:04,320
going to add it right after the
9051
05:10:04,320 --> 05:10:05,520
expression
9052
05:10:05,520 --> 05:10:07,680
so everybody in conclusion a list
9053
05:10:07,680 --> 05:10:10,000
comprehension is a way to create a new
9054
05:10:10,000 --> 05:10:10,400
list
9055
05:10:10,400 --> 05:10:13,040
with less syntax you can even mimic
9056
05:10:13,040 --> 05:10:14,878
certain lambda functions which we did
9057
05:10:14,878 --> 05:10:15,520
with the
9058
05:10:15,520 --> 05:10:17,360
filter function and it has the added
9059
05:10:17,360 --> 05:10:19,840
benefit of being easier to read than a
9060
05:10:19,840 --> 05:10:21,040
lambda function
9061
05:10:21,040 --> 05:10:22,798
so you just follow one of these formulas
9062
05:10:22,798 --> 05:10:24,480
depending on what you're trying to do
9063
05:10:24,480 --> 05:10:27,120
so you need at least list equals your
9064
05:10:27,120 --> 05:10:28,080
expression
9065
05:10:28,080 --> 05:10:30,560
for item and your interval if you have a
9066
05:10:30,560 --> 05:10:31,760
condition that you want to check
9067
05:10:31,760 --> 05:10:33,120
you just add that to the end of your
9068
05:10:33,120 --> 05:10:35,280
list comprehension if you have an if
9069
05:10:35,280 --> 05:10:36,240
else statement
9070
05:10:36,240 --> 05:10:38,080
then you'll add that to right after the
9071
05:10:38,080 --> 05:10:40,160
expression so you just follow one of
9072
05:10:40,160 --> 05:10:42,080
these formulas depending on how you want
9073
05:10:42,080 --> 05:10:43,520
to write your program
9074
05:10:43,520 --> 05:10:45,520
so that is a list comprehension if you
9075
05:10:45,520 --> 05:10:47,280
would like a copy of this code i will
9076
05:10:47,280 --> 05:10:48,958
post all of this to the comments section
9077
05:10:48,958 --> 05:10:50,480
down below and well
9078
05:10:50,480 --> 05:10:52,480
yeah that's how list comprehensions work
9079
05:10:52,480 --> 05:10:54,240
in python
9080
05:10:54,240 --> 05:10:56,718
yo what's going on everybody it's bro
9081
05:10:56,718 --> 05:10:57,760
hope you're doing well
9082
05:10:57,760 --> 05:10:59,520
and in this video i'm going to explain
9083
05:10:59,520 --> 05:11:01,840
dictionary comprehensions in python so
9084
05:11:01,840 --> 05:11:06,400
sit back relax and enjoy the show
9085
05:11:06,718 --> 05:11:09,280
okay people dictionary comprehensions
9086
05:11:09,280 --> 05:11:10,560
they're very similar to list
9087
05:11:10,560 --> 05:11:11,840
comprehensions except
9088
05:11:11,840 --> 05:11:13,360
they're with dictionaries that's it
9089
05:11:13,360 --> 05:11:15,280
video over okay but seriously a
9090
05:11:15,280 --> 05:11:17,200
dictionary comprehension is a way to
9091
05:11:17,200 --> 05:11:19,360
create dictionaries using an expression
9092
05:11:19,360 --> 05:11:21,120
and they can replace for loops and
9093
05:11:21,120 --> 05:11:22,560
certain lambda functions
9094
05:11:22,560 --> 05:11:24,240
and all we have to do is follow this
9095
05:11:24,240 --> 05:11:26,080
formula dictionary equals
9096
05:11:26,080 --> 05:11:29,600
key colon our expression for key value
9097
05:11:29,600 --> 05:11:31,600
in iterable so let's go over a few
9098
05:11:31,600 --> 05:11:33,600
examples our first example we're going
9099
05:11:33,600 --> 05:11:34,080
to take
9100
05:11:34,080 --> 05:11:37,040
this dictionary named cities in f f is
9101
05:11:37,040 --> 05:11:38,320
short for fahrenheit
9102
05:11:38,320 --> 05:11:41,200
i have different city names as keys and
9103
05:11:41,200 --> 05:11:42,878
relative temperatures as
9104
05:11:42,878 --> 05:11:46,400
values in fahrenheit so new york is 32
9105
05:11:46,400 --> 05:11:48,958
degrees fahrenheit boston is 75.
9106
05:11:48,958 --> 05:11:51,920
los angeles is 100 and chicago is 50.
9107
05:11:51,920 --> 05:11:53,680
and what we'll be doing is creating a
9108
05:11:53,680 --> 05:11:55,680
separate dictionary where all of these
9109
05:11:55,680 --> 05:11:57,680
temperatures will be in celsius
9110
05:11:57,680 --> 05:12:00,160
using a dictionary comprehension so
9111
05:12:00,160 --> 05:12:02,160
let's follow this formula let's create a
9112
05:12:02,160 --> 05:12:03,600
new name for this dictionary
9113
05:12:03,600 --> 05:12:07,680
let's say cities in c short for celsius
9114
05:12:07,680 --> 05:12:10,160
equals and we will follow this pattern
9115
05:12:10,160 --> 05:12:11,520
first our key
9116
05:12:11,520 --> 05:12:13,360
golden then our expression we'll go back
9117
05:12:13,360 --> 05:12:15,040
to this in just a moment
9118
05:12:15,040 --> 05:12:18,160
for key comma
9119
05:12:18,160 --> 05:12:21,520
value in our iterable
9120
05:12:21,520 --> 05:12:25,280
of cities in f and since we're working
9121
05:12:25,280 --> 05:12:27,120
with a dictionary we're going to use
9122
05:12:27,120 --> 05:12:29,520
the items method now we just need to
9123
05:12:29,520 --> 05:12:31,120
fill in this expression
9124
05:12:31,120 --> 05:12:33,360
there is a formula to convert fahrenheit
9125
05:12:33,360 --> 05:12:34,878
to celsius and it should be on the
9126
05:12:34,878 --> 05:12:36,240
screen right about now
9127
05:12:36,240 --> 05:12:38,480
so let's follow this formula so we'll
9128
05:12:38,480 --> 05:12:40,160
take our value
9129
05:12:40,160 --> 05:12:43,200
minus 32 and just for good measure i'm
9130
05:12:43,200 --> 05:12:45,680
going to put these within parentheses
9131
05:12:45,680 --> 05:12:49,440
and we will multiply all of this by
9132
05:12:49,440 --> 05:12:52,958
5 divided by 9 and that is it so we have
9133
05:12:52,958 --> 05:12:54,320
our key
9134
05:12:54,320 --> 05:12:56,878
our expression to convert fahrenheit to
9135
05:12:56,878 --> 05:12:58,160
celsius
9136
05:12:58,160 --> 05:13:01,600
for key value in our iterable of
9137
05:13:01,600 --> 05:13:04,798
cities in f our previous dictionary and
9138
05:13:04,798 --> 05:13:07,600
let's test this by printing our new
9139
05:13:07,600 --> 05:13:08,480
dictionary
9140
05:13:08,480 --> 05:13:12,638
cities in c
9141
05:13:12,878 --> 05:13:14,080
actually i think i'm going to round
9142
05:13:14,080 --> 05:13:16,160
these numbers just so it's more readable
9143
05:13:16,160 --> 05:13:19,920
so let's round all this there we go so
9144
05:13:19,920 --> 05:13:21,840
new york and celsius is zero
9145
05:13:21,840 --> 05:13:24,878
boston is 24. los angeles is 38 and
9146
05:13:24,878 --> 05:13:26,320
chicago is 10.
9147
05:13:26,320 --> 05:13:29,120
so we created a new dictionary using a
9148
05:13:29,120 --> 05:13:30,798
dictionary comprehension
9149
05:13:30,798 --> 05:13:32,718
now with these dictionary comprehensions
9150
05:13:32,718 --> 05:13:34,798
you can add an if conditional to the end
9151
05:13:34,798 --> 05:13:35,280
of this
9152
05:13:35,280 --> 05:13:37,040
so let's say that we have a separate
9153
05:13:37,040 --> 05:13:38,638
dictionary of weather
9154
05:13:38,638 --> 05:13:40,160
like a description of the weather in
9155
05:13:40,160 --> 05:13:42,240
each city new york is snowing
9156
05:13:42,240 --> 05:13:44,718
boston is sunny los angeles is sunny and
9157
05:13:44,718 --> 05:13:46,160
chicago is cloudy
9158
05:13:46,160 --> 05:13:47,920
let's say that we would like to create a
9159
05:13:47,920 --> 05:13:50,160
separate dictionary with a dictionary
9160
05:13:50,160 --> 05:13:51,440
comprehension
9161
05:13:51,440 --> 05:13:53,920
that only has cities where the weather
9162
05:13:53,920 --> 05:13:54,798
is sunny
9163
05:13:54,798 --> 05:13:57,120
so i would write something like this
9164
05:13:57,120 --> 05:13:58,160
let's say sunny
9165
05:13:58,160 --> 05:13:59,360
weather will be the name of our
9166
05:13:59,360 --> 05:14:02,000
dictionary equals and we'll follow
9167
05:14:02,000 --> 05:14:04,160
this formula and then we just tack on
9168
05:14:04,160 --> 05:14:06,240
our conditional to the end so it's kind
9169
05:14:06,240 --> 05:14:08,000
of the same as before really
9170
05:14:08,000 --> 05:14:11,760
so key colon and we don't really have an
9171
05:14:11,760 --> 05:14:14,240
expression here so let's just say
9172
05:14:14,240 --> 05:14:18,958
value then for
9173
05:14:19,280 --> 05:14:22,400
key comma value
9174
05:14:22,400 --> 05:14:25,440
in our iterable of
9175
05:14:25,440 --> 05:14:28,878
weather dot items
9176
05:14:28,878 --> 05:14:33,840
then our conditional if value
9177
05:14:33,840 --> 05:14:35,840
sunny because we are only looking for
9178
05:14:35,840 --> 05:14:37,600
sunny weather
9179
05:14:37,600 --> 05:14:40,638
and let's print this print
9180
05:14:40,638 --> 05:14:44,080
our dictionary of sunny weather
9181
05:14:44,080 --> 05:14:45,840
and let me just make this more readable
9182
05:14:45,840 --> 05:14:47,840
for you guys and our new dictionary
9183
05:14:47,840 --> 05:14:48,718
comprehension
9184
05:14:48,718 --> 05:14:51,520
will create a dictionary of key values
9185
05:14:51,520 --> 05:14:52,480
where the value
9186
05:14:52,480 --> 05:14:55,360
is sunny using an if conditional at the
9187
05:14:55,360 --> 05:14:57,280
end of our dictionary comprehension
9188
05:14:57,280 --> 05:14:59,120
here's a third example for you well if
9189
05:14:59,120 --> 05:15:00,958
you have an if else condition you can
9190
05:15:00,958 --> 05:15:02,638
add that to where the expression is
9191
05:15:02,638 --> 05:15:04,878
within your dictionary comprehension
9192
05:15:04,878 --> 05:15:06,878
so i'm going to reuse the previous
9193
05:15:06,878 --> 05:15:08,798
dictionary for the first example where
9194
05:15:08,798 --> 05:15:09,280
we have
9195
05:15:09,280 --> 05:15:11,920
cities and their temperatures as values
9196
05:15:11,920 --> 05:15:13,760
so we're going to replace each
9197
05:15:13,760 --> 05:15:15,440
temperature with a description of the
9198
05:15:15,440 --> 05:15:16,160
weather
9199
05:15:16,160 --> 05:15:18,638
is it warm or is it cold so let's do
9200
05:15:18,638 --> 05:15:19,280
that using
9201
05:15:19,280 --> 05:15:21,280
a dictionary comprehension that contains
9202
05:15:21,280 --> 05:15:22,958
an if else statement
9203
05:15:22,958 --> 05:15:24,718
so let's say we have a new dictionary
9204
05:15:24,718 --> 05:15:27,280
named desk short for description
9205
05:15:27,280 --> 05:15:29,920
cities it's a brief description of each
9206
05:15:29,920 --> 05:15:31,120
city's temperature
9207
05:15:31,120 --> 05:15:34,878
so we follow this formula key
9208
05:15:34,878 --> 05:15:37,520
then our if else conditional for key
9209
05:15:37,520 --> 05:15:38,160
value
9210
05:15:38,160 --> 05:15:40,718
and iterable actually i probably can
9211
05:15:40,718 --> 05:15:41,920
copy all this
9212
05:15:41,920 --> 05:15:44,798
just to save some time okay so our
9213
05:15:44,798 --> 05:15:45,840
iterable
9214
05:15:45,840 --> 05:15:49,520
will be cities dot items
9215
05:15:49,520 --> 05:15:53,120
and our conditional is going to be
9216
05:15:53,120 --> 05:15:56,560
we will return warm if
9217
05:15:56,560 --> 05:16:00,000
our value is greater than or equal to
9218
05:16:00,000 --> 05:16:04,080
let's say 40. else
9219
05:16:04,080 --> 05:16:07,600
cold and we will print
9220
05:16:07,600 --> 05:16:11,040
our new dictionary of desk cities
9221
05:16:11,040 --> 05:16:14,240
description of cities so new york is
9222
05:16:14,240 --> 05:16:17,360
cold because it's 32 that's below 40.
9223
05:16:17,360 --> 05:16:19,760
boston is warm los angeles is warm and
9224
05:16:19,760 --> 05:16:21,200
chicago is warm
9225
05:16:21,200 --> 05:16:22,878
now if your condition gets somewhat
9226
05:16:22,878 --> 05:16:25,040
complex you can even call a separate
9227
05:16:25,040 --> 05:16:26,320
function to keep your code more
9228
05:16:26,320 --> 05:16:27,120
organized
9229
05:16:27,120 --> 05:16:29,120
so key then we can call a function and
9230
05:16:29,120 --> 05:16:30,320
pass in a value
9231
05:16:30,320 --> 05:16:32,958
for key value in iterable so i'm going
9232
05:16:32,958 --> 05:16:33,760
to again
9233
05:16:33,760 --> 05:16:35,840
reuse our city names and our
9234
05:16:35,840 --> 05:16:36,798
temperatures
9235
05:16:36,798 --> 05:16:39,040
and this time we are going to call a
9236
05:16:39,040 --> 05:16:40,320
function instead
9237
05:16:40,320 --> 05:16:41,920
i think i'll reuse this code from the
9238
05:16:41,920 --> 05:16:43,520
previous example where we have a
9239
05:16:43,520 --> 05:16:44,878
separate dictionary named
9240
05:16:44,878 --> 05:16:47,360
desk cities and i'm going to replace
9241
05:16:47,360 --> 05:16:48,878
this if else statement
9242
05:16:48,878 --> 05:16:51,120
with a function so let's say we have a
9243
05:16:51,120 --> 05:16:52,718
function that is named
9244
05:16:52,718 --> 05:16:56,320
check temp and we will pass in our value
9245
05:16:56,320 --> 05:16:58,400
and we just need to define this function
9246
05:16:58,400 --> 05:17:00,160
so let's define that here
9247
05:17:00,160 --> 05:17:03,520
so let's define check
9248
05:17:03,520 --> 05:17:06,798
temp and our parameter is our value that
9249
05:17:06,798 --> 05:17:08,480
we're currently working on within our
9250
05:17:08,480 --> 05:17:09,440
dictionary
9251
05:17:09,440 --> 05:17:12,878
value then we'll write if
9252
05:17:12,878 --> 05:17:16,718
value is greater than or equal to
9253
05:17:16,718 --> 05:17:19,840
let's say anything above how about 70
9254
05:17:19,840 --> 05:17:24,480
will return the word hot
9255
05:17:24,480 --> 05:17:27,520
then else if
9256
05:17:27,520 --> 05:17:32,080
let's say 69 greater than or equal to
9257
05:17:32,080 --> 05:17:35,200
value and then
9258
05:17:35,200 --> 05:17:39,600
value is greater than or equal to
9259
05:17:39,600 --> 05:17:43,040
how about 40 then we will
9260
05:17:43,040 --> 05:17:46,878
return the word warm
9261
05:17:46,878 --> 05:17:51,680
and lastly else return
9262
05:17:51,680 --> 05:17:53,840
cold and let me just fix some of the
9263
05:17:53,840 --> 05:17:56,400
spacing
9264
05:17:56,958 --> 05:17:58,958
now depending on the temperature that is
9265
05:17:58,958 --> 05:18:00,878
stored within each value of our
9266
05:18:00,878 --> 05:18:01,680
dictionary
9267
05:18:01,680 --> 05:18:04,400
we will call a function that will return
9268
05:18:04,400 --> 05:18:06,320
one of a few different outputs
9269
05:18:06,320 --> 05:18:08,798
so it's kind of like we're using the map
9270
05:18:08,798 --> 05:18:11,040
function the map lambda function
9271
05:18:11,040 --> 05:18:14,080
so let's print our description
9272
05:18:14,080 --> 05:18:16,878
of cities and we should get a short
9273
05:18:16,878 --> 05:18:18,798
description of the temperature in each
9274
05:18:18,798 --> 05:18:19,360
city
9275
05:18:19,360 --> 05:18:21,760
after we call the check temperature
9276
05:18:21,760 --> 05:18:23,600
function and then pass in each value
9277
05:18:23,600 --> 05:18:24,878
from our dictionary
9278
05:18:24,878 --> 05:18:27,040
so new york is cold because it's 32
9279
05:18:27,040 --> 05:18:28,160
degrees fahrenheit
9280
05:18:28,160 --> 05:18:31,200
boston is hot because it's above 70. los
9281
05:18:31,200 --> 05:18:32,000
angeles is hot
9282
05:18:32,000 --> 05:18:34,480
because it's 100 and then chicago is
9283
05:18:34,480 --> 05:18:36,958
warm because that is 50 which is between
9284
05:18:36,958 --> 05:18:40,000
69 and 40 within our function
9285
05:18:40,000 --> 05:18:42,480
so that is a dictionary comprehension
9286
05:18:42,480 --> 05:18:44,240
it's a shortcut where you can create
9287
05:18:44,240 --> 05:18:46,320
dictionaries using an expression
9288
05:18:46,320 --> 05:18:48,160
and they can replace for loops and
9289
05:18:48,160 --> 05:18:49,840
certain lambda functions
9290
05:18:49,840 --> 05:18:51,360
so if you would like a copy of all this
9291
05:18:51,360 --> 05:18:53,040
code i will post all of this to the
9292
05:18:53,040 --> 05:18:54,560
comment section down below
9293
05:18:54,560 --> 05:18:56,480
and well yeah that's how dictionary
9294
05:18:56,480 --> 05:18:57,840
comprehensions work
9295
05:18:57,840 --> 05:19:00,958
in python hey what's going on everybody
9296
05:19:00,958 --> 05:19:02,798
it's bro hope you're doing well and in
9297
05:19:02,798 --> 05:19:04,320
today's video i'm going to show you how
9298
05:19:04,320 --> 05:19:06,718
the zip function works in python so
9299
05:19:06,718 --> 05:19:11,520
sit back relax and enjoy the show
9300
05:19:11,840 --> 05:19:13,600
all right ladies and gentlemen welcome
9301
05:19:13,600 --> 05:19:14,958
back we're talking about the zip
9302
05:19:14,958 --> 05:19:16,160
function today
9303
05:19:16,160 --> 05:19:18,400
the zip function will aggregate elements
9304
05:19:18,400 --> 05:19:20,480
from two or more iterables
9305
05:19:20,480 --> 05:19:23,120
iterables are those things like lists
9306
05:19:23,120 --> 05:19:23,760
tuples
9307
05:19:23,760 --> 05:19:26,240
sets etc and the zip function will
9308
05:19:26,240 --> 05:19:26,878
create a
9309
05:19:26,878 --> 05:19:29,760
zip object with paired elements from
9310
05:19:29,760 --> 05:19:30,798
each iterable
9311
05:19:30,798 --> 05:19:33,120
stored in a tuple for each element
9312
05:19:33,120 --> 05:19:34,638
within our zip object
9313
05:19:34,638 --> 05:19:37,040
here's an example let's say that we have
9314
05:19:37,040 --> 05:19:38,878
two different types of iterables
9315
05:19:38,878 --> 05:19:41,600
i have a list of usernames and a few
9316
05:19:41,600 --> 05:19:43,120
usernames within here are
9317
05:19:43,120 --> 05:19:46,638
dude bro and mr and i have a
9318
05:19:46,638 --> 05:19:48,878
tuple of passwords and i have some very
9319
05:19:48,878 --> 05:19:49,920
secure passwords
9320
05:19:49,920 --> 05:19:53,280
such as password abc123
9321
05:19:53,280 --> 05:19:55,840
and guest what i would like to do is to
9322
05:19:55,840 --> 05:19:58,400
zip elements from each iterable together
9323
05:19:58,400 --> 05:20:00,638
so that they're in pairs and each pair
9324
05:20:00,638 --> 05:20:03,040
is going to be stored as a tuple within
9325
05:20:03,040 --> 05:20:05,280
a zip object and here's how we can do
9326
05:20:05,280 --> 05:20:07,760
that let's say that we will create a zip
9327
05:20:07,760 --> 05:20:09,200
object called users
9328
05:20:09,200 --> 05:20:11,920
and we will use the zip function the zip
9329
05:20:11,920 --> 05:20:13,120
function will take a
9330
05:20:13,120 --> 05:20:14,878
varying amount of iterables we're going
9331
05:20:14,878 --> 05:20:16,638
to pass in our usernames
9332
05:20:16,638 --> 05:20:18,878
and passwords and zip them together so
9333
05:20:18,878 --> 05:20:20,560
let's pass in usernames
9334
05:20:20,560 --> 05:20:23,200
as well as passwords and now our zip
9335
05:20:23,200 --> 05:20:24,558
object of users
9336
05:20:24,558 --> 05:20:26,558
is actually iterable zip objects are
9337
05:20:26,558 --> 05:20:28,320
iterable so we can use them within a for
9338
05:20:28,320 --> 05:20:28,798
loop
9339
05:20:28,798 --> 05:20:32,480
so let's type for i in users
9340
05:20:32,480 --> 05:20:36,480
and print i
9341
05:20:36,638 --> 05:20:39,120
and what we get is that we have a zip
9342
05:20:39,120 --> 05:20:39,840
object
9343
05:20:39,840 --> 05:20:42,718
of tuples and each tuple is storing each
9344
05:20:42,718 --> 05:20:44,400
pair of elements from our
9345
05:20:44,400 --> 05:20:47,600
two iterables now users is a
9346
05:20:47,600 --> 05:20:49,360
zip object and if you don't believe me
9347
05:20:49,360 --> 05:20:51,520
let me prove it i'm going to print
9348
05:20:51,520 --> 05:20:54,958
the type of users and this will print
9349
05:20:54,958 --> 05:20:58,000
that users is indeed a zip object
9350
05:20:58,000 --> 05:21:00,320
but you can easily convert this to a
9351
05:21:00,320 --> 05:21:02,400
different type of iterable by using a
9352
05:21:02,400 --> 05:21:03,120
cast
9353
05:21:03,120 --> 05:21:04,878
let's say that we would like to convert
9354
05:21:04,878 --> 05:21:07,120
our zip object into a list
9355
05:21:07,120 --> 05:21:09,120
so we'll surround the zip function with
9356
05:21:09,120 --> 05:21:10,958
a cast to a list
9357
05:21:10,958 --> 05:21:13,520
and now the data type of users is now a
9358
05:21:13,520 --> 05:21:14,080
list
9359
05:21:14,080 --> 05:21:16,798
what we have is a list of tuples and
9360
05:21:16,798 --> 05:21:18,400
each tuple is storing a
9361
05:21:18,400 --> 05:21:21,120
pair of elements from our two iterables
9362
05:21:21,120 --> 05:21:22,958
of usernames and passwords
9363
05:21:22,958 --> 05:21:24,718
now currently since we're passing in
9364
05:21:24,718 --> 05:21:26,878
only two different iterables we can
9365
05:21:26,878 --> 05:21:27,840
easily make this
9366
05:21:27,840 --> 05:21:30,878
a dictionary so that these are key value
9367
05:21:30,878 --> 05:21:31,520
pairs
9368
05:21:31,520 --> 05:21:33,840
so let's cast our zip object as a
9369
05:21:33,840 --> 05:21:34,718
dictionary
9370
05:21:34,718 --> 05:21:36,400
and to display all of the elements
9371
05:21:36,400 --> 05:21:38,240
within our dictionary all the key value
9372
05:21:38,240 --> 05:21:38,798
pairs
9373
05:21:38,798 --> 05:21:40,558
we're going to change our for loop to
9374
05:21:40,558 --> 05:21:43,040
this for key value
9375
05:21:43,040 --> 05:21:46,878
in users dot and we will use the
9376
05:21:46,878 --> 05:21:50,558
items method print key
9377
05:21:50,558 --> 05:21:53,280
comma value actually i think i'm going
9378
05:21:53,280 --> 05:21:54,798
to separate each of these with
9379
05:21:54,798 --> 05:21:58,320
a colon just to make it more readable
9380
05:21:58,320 --> 05:22:00,638
and now when we zip these two intervals
9381
05:22:00,638 --> 05:22:01,600
together
9382
05:22:01,600 --> 05:22:04,878
we end up with a dictionary of usernames
9383
05:22:04,878 --> 05:22:06,798
and passwords and the name of this
9384
05:22:06,798 --> 05:22:08,958
dictionary is users
9385
05:22:08,958 --> 05:22:11,040
now you're not limited to just two
9386
05:22:11,040 --> 05:22:12,240
iterables you can add
9387
05:22:12,240 --> 05:22:14,718
a third iterable or more so this time
9388
05:22:14,718 --> 05:22:16,320
let's create a
9389
05:22:16,320 --> 05:22:19,280
maybe a list a list of last login dates
9390
05:22:19,280 --> 05:22:20,240
and i'll just call this
9391
05:22:20,240 --> 05:22:23,760
login date equals and why not make a
9392
05:22:23,760 --> 05:22:24,798
list
9393
05:22:24,798 --> 05:22:27,040
and let's make up some dates let's say 1
9394
05:22:27,040 --> 05:22:27,840
1
9395
05:22:27,840 --> 05:22:31,840
dash 20 21
9396
05:22:32,600 --> 05:22:34,320
12-20-21
9397
05:22:34,320 --> 05:22:38,958
and 1-3-20
9398
05:22:38,958 --> 05:22:42,958
21 okay so let's create
9399
05:22:42,958 --> 05:22:46,718
a zip object of users and we're going to
9400
05:22:46,718 --> 05:22:48,000
zip
9401
05:22:48,000 --> 05:22:52,160
user names passwords
9402
05:22:52,160 --> 05:22:56,638
and login date
9403
05:22:56,958 --> 05:23:00,320
and let's iterate over this for i
9404
05:23:00,320 --> 05:23:03,920
in users print
9405
05:23:03,920 --> 05:23:08,638
i now we have a tuple for each element
9406
05:23:08,638 --> 05:23:10,160
and instead of a pair we now
9407
05:23:10,160 --> 05:23:13,280
have a trio i guess of all of the
9408
05:23:13,280 --> 05:23:14,080
different elements
9409
05:23:14,080 --> 05:23:17,200
from each iterable so in conclusion
9410
05:23:17,200 --> 05:23:19,440
the zip function will aggregate elements
9411
05:23:19,440 --> 05:23:20,558
from two or more
9412
05:23:20,558 --> 05:23:23,760
iterables and create a zip object with
9413
05:23:23,760 --> 05:23:25,920
paired or grouped together elements
9414
05:23:25,920 --> 05:23:26,798
stored in a
9415
05:23:26,798 --> 05:23:29,440
tuple for each element within our zip
9416
05:23:29,440 --> 05:23:30,480
object
9417
05:23:30,480 --> 05:23:32,400
so that is the zip function if you would
9418
05:23:32,400 --> 05:23:34,400
like a copy of this code i will post
9419
05:23:34,400 --> 05:23:36,480
this to the comment section down below
9420
05:23:36,480 --> 05:23:38,240
and well yeah that's how the zip
9421
05:23:38,240 --> 05:23:41,280
function works in python
9422
05:23:41,280 --> 05:23:43,120
hey what's going on everybody it's you
9423
05:23:43,120 --> 05:23:44,480
bro hope you're doing well
9424
05:23:44,480 --> 05:23:46,480
and i'm going to explain the purpose of
9425
05:23:46,480 --> 05:23:48,000
if name equals main
9426
05:23:48,000 --> 05:23:51,040
in python so sit back relax
9427
05:23:51,040 --> 05:23:54,400
and enjoy the show
9428
05:23:54,400 --> 05:23:56,160
during your programming journey you may
9429
05:23:56,160 --> 05:23:58,320
have encountered this strange statement
9430
05:23:58,320 --> 05:23:58,878
of
9431
05:23:58,878 --> 05:24:01,200
if name equals main now what the heck
9432
05:24:01,200 --> 05:24:02,320
does that mean
9433
05:24:02,320 --> 05:24:05,520
so with python files also referred to as
9434
05:24:05,520 --> 05:24:08,000
modules by including this statement it
9435
05:24:08,000 --> 05:24:10,320
gives our modules some flexibility
9436
05:24:10,320 --> 05:24:12,638
one a module that has the statement can
9437
05:24:12,638 --> 05:24:15,040
be run as a standalone program
9438
05:24:15,040 --> 05:24:18,160
or two this module can be imported
9439
05:24:18,160 --> 05:24:20,320
and used by other modules if there's
9440
05:24:20,320 --> 05:24:22,160
some sort of useful functions
9441
05:24:22,160 --> 05:24:24,638
or other resources within this module to
9442
05:24:24,638 --> 05:24:25,360
be imported
9443
05:24:25,360 --> 05:24:27,920
by including the statement of if name
9444
05:24:27,920 --> 05:24:30,080
equals main we're checking to see if a
9445
05:24:30,080 --> 05:24:30,558
user
9446
05:24:30,558 --> 05:24:32,718
is running this module as either a
9447
05:24:32,718 --> 05:24:34,160
standalone program
9448
05:24:34,160 --> 05:24:36,320
or they're importing it from another
9449
05:24:36,320 --> 05:24:38,000
module behind the scenes
9450
05:24:38,000 --> 05:24:40,558
the python interpreter will set special
9451
05:24:40,558 --> 05:24:41,360
variables
9452
05:24:41,360 --> 05:24:44,160
one of which is double underscore name
9453
05:24:44,160 --> 05:24:45,440
it's a variable
9454
05:24:45,440 --> 05:24:48,400
and python will assign the name variable
9455
05:24:48,400 --> 05:24:49,280
a value
9456
05:24:49,280 --> 05:24:52,320
of main if it's the initial module being
9457
05:24:52,320 --> 05:24:52,798
run
9458
05:24:52,798 --> 05:24:54,480
and we can actually test that by
9459
05:24:54,480 --> 05:24:56,320
printing double
9460
05:24:56,320 --> 05:24:59,440
underscore name so since this is the
9461
05:24:59,440 --> 05:25:01,200
initial module being run
9462
05:25:01,200 --> 05:25:04,240
module one for my example name is going
9463
05:25:04,240 --> 05:25:05,760
to be assigned a value
9464
05:25:05,760 --> 05:25:09,760
a string of main now check this out i
9465
05:25:09,760 --> 05:25:12,080
have two modules what if i were to
9466
05:25:12,080 --> 05:25:14,798
import module 2 and check the name
9467
05:25:14,798 --> 05:25:16,718
variable of module 2.
9468
05:25:16,718 --> 05:25:20,878
let's try it import module
9469
05:25:20,878 --> 05:25:23,120
two
9470
05:25:23,920 --> 05:25:26,320
in order to access a variable from
9471
05:25:26,320 --> 05:25:28,160
another module i need to type the name
9472
05:25:28,160 --> 05:25:29,200
of the module
9473
05:25:29,200 --> 05:25:32,958
module two dot and i would like to check
9474
05:25:32,958 --> 05:25:35,760
the special name variable of module 2
9475
05:25:35,760 --> 05:25:37,600
and print it
9476
05:25:37,600 --> 05:25:40,240
so when you import a module this name
9477
05:25:40,240 --> 05:25:41,840
variable is going to be assigned
9478
05:25:41,840 --> 05:25:43,840
the name of the module in this case
9479
05:25:43,840 --> 05:25:44,958
module 2.
9480
05:25:44,958 --> 05:25:46,718
this time what if we change the roles
9481
05:25:46,718 --> 05:25:48,798
around i'm going to copy all of this
9482
05:25:48,798 --> 05:25:49,440
text
9483
05:25:49,440 --> 05:25:52,000
and paste it within module two we're
9484
05:25:52,000 --> 05:25:52,718
going to
9485
05:25:52,718 --> 05:25:56,080
import from module one we will
9486
05:25:56,080 --> 05:25:59,280
print that special name variable of
9487
05:25:59,280 --> 05:26:02,638
this module module 2 and then print the
9488
05:26:02,638 --> 05:26:03,760
name variable
9489
05:26:03,760 --> 05:26:06,798
of module 1 which is being imported now
9490
05:26:06,798 --> 05:26:08,638
when i run this program i'm going to
9491
05:26:08,638 --> 05:26:09,360
instead
9492
05:26:09,360 --> 05:26:12,878
run from module 2.
9493
05:26:12,878 --> 05:26:15,920
now this time that special name variable
9494
05:26:15,920 --> 05:26:19,360
of module 2 is main and the special name
9495
05:26:19,360 --> 05:26:21,360
variable of module 1
9496
05:26:21,360 --> 05:26:24,638
is module 1 the name of the module
9497
05:26:24,638 --> 05:26:27,680
so by including a statement such as
9498
05:26:27,680 --> 05:26:30,480
if name equals main we're checking to
9499
05:26:30,480 --> 05:26:32,638
see if this module is being run
9500
05:26:32,638 --> 05:26:36,240
directly or indirectly so let's test it
9501
05:26:36,240 --> 05:26:39,040
within this statement of if name equals
9502
05:26:39,040 --> 05:26:39,600
main
9503
05:26:39,600 --> 05:26:43,200
let's print running this
9504
05:26:43,200 --> 05:26:47,120
module directly
9505
05:26:47,120 --> 05:26:49,440
else that must mean that we're running
9506
05:26:49,440 --> 05:26:50,320
this module
9507
05:26:50,320 --> 05:26:54,240
indirectly so let's print
9508
05:26:54,840 --> 05:26:57,120
running other
9509
05:26:57,120 --> 05:27:00,480
module indirectly
9510
05:27:00,480 --> 05:27:03,600
okay so i'm going to go to module 1
9511
05:27:03,600 --> 05:27:07,040
and run from here running this module
9512
05:27:07,040 --> 05:27:08,400
directly
9513
05:27:08,400 --> 05:27:10,480
now let's do the same thing with module
9514
05:27:10,480 --> 05:27:12,320
two but we need to
9515
05:27:12,320 --> 05:27:16,320
import module
9516
05:27:16,320 --> 05:27:24,320
one and let's run for module two
9517
05:27:24,320 --> 05:27:27,280
running other module indirectly for the
9518
05:27:27,280 --> 05:27:28,958
time being i'm just going to write pass
9519
05:27:28,958 --> 05:27:29,600
within
9520
05:27:29,600 --> 05:27:32,000
our if name equals main as a placeholder
9521
05:27:32,000 --> 05:27:33,680
and get rid of our if statement
9522
05:27:33,680 --> 05:27:36,080
so let's say within module 1 there's a
9523
05:27:36,080 --> 05:27:38,160
useful function or resource we would
9524
05:27:38,160 --> 05:27:39,120
like to access
9525
05:27:39,120 --> 05:27:41,600
from module 2. so let's say we have a
9526
05:27:41,600 --> 05:27:43,440
function named hello
9527
05:27:43,440 --> 05:27:46,000
and all this will do is print the word
9528
05:27:46,000 --> 05:27:47,840
hello
9529
05:27:47,840 --> 05:27:49,440
let's just pretend that it's a useful
9530
05:27:49,440 --> 05:27:50,958
function not really but
9531
05:27:50,958 --> 05:27:54,320
let's pretend so in order to use this
9532
05:27:54,320 --> 05:27:56,638
function from module two i need to
9533
05:27:56,638 --> 05:27:58,798
import module one
9534
05:27:58,798 --> 05:28:02,080
then type module one dot and
9535
05:28:02,080 --> 05:28:04,160
then the name of the function so i'm
9536
05:28:04,160 --> 05:28:05,120
going to run
9537
05:28:05,120 --> 05:28:08,798
from module 2 and this will print
9538
05:28:08,798 --> 05:28:11,600
hello but what if i run this program
9539
05:28:11,600 --> 05:28:13,200
from module 1.
9540
05:28:13,200 --> 05:28:16,080
so right now module 1 cannot be run
9541
05:28:16,080 --> 05:28:18,320
directly as a standalone program
9542
05:28:18,320 --> 05:28:21,120
so i cannot print the word hello so what
9543
05:28:21,120 --> 05:28:22,160
i could do
9544
05:28:22,160 --> 05:28:25,600
is within if name equals main i can
9545
05:28:25,600 --> 05:28:28,638
call that hello function directly
9546
05:28:28,638 --> 05:28:31,920
so let's run from module one
9547
05:28:31,920 --> 05:28:34,320
hello one other thing that you might see
9548
05:28:34,320 --> 05:28:35,360
too within
9549
05:28:35,360 --> 05:28:37,840
the if statement if name equals main
9550
05:28:37,840 --> 05:28:39,440
there might be a call for
9551
05:28:39,440 --> 05:28:41,440
a main function and you can write the
9552
05:28:41,440 --> 05:28:43,600
main body of your program within
9553
05:28:43,600 --> 05:28:46,240
a main function so everybody in
9554
05:28:46,240 --> 05:28:47,120
conclusion
9555
05:28:47,120 --> 05:28:49,200
the reason that people may include this
9556
05:28:49,200 --> 05:28:50,638
statement if name
9557
05:28:50,638 --> 05:28:52,558
equals main is that it allows our
9558
05:28:52,558 --> 05:28:54,558
modules to have some flexibility
9559
05:28:54,558 --> 05:28:57,200
they can be run as a standalone program
9560
05:28:57,200 --> 05:28:59,280
or they can be imported and used by
9561
05:28:59,280 --> 05:29:00,480
other modules
9562
05:29:00,480 --> 05:29:02,240
and this is because the python
9563
05:29:02,240 --> 05:29:03,520
interpreter sets
9564
05:29:03,520 --> 05:29:06,480
that special variable of name with a
9565
05:29:06,480 --> 05:29:07,280
value
9566
05:29:07,280 --> 05:29:10,160
of main if it's the initial module being
9567
05:29:10,160 --> 05:29:10,798
run
9568
05:29:10,798 --> 05:29:12,958
if you would like a copy of my code and
9569
05:29:12,958 --> 05:29:14,878
my notes i will post all of this to the
9570
05:29:14,878 --> 05:29:16,320
comment section down below
9571
05:29:16,320 --> 05:29:19,040
but yeah that's the purpose of if name
9572
05:29:19,040 --> 05:29:19,920
equals main
9573
05:29:19,920 --> 05:29:23,200
in python hey what's going on everybody
9574
05:29:23,200 --> 05:29:25,280
it's bro hope you're doing well and in
9575
05:29:25,280 --> 05:29:26,798
this video i'm going to show you all a
9576
05:29:26,798 --> 05:29:28,638
few useful functions related to
9577
05:29:28,638 --> 05:29:31,120
times and dates using the time module in
9578
05:29:31,120 --> 05:29:32,080
python so
9579
05:29:32,080 --> 05:29:36,798
sit back relax and enjoy the show
9580
05:29:37,120 --> 05:29:39,440
alright everybody so the time module
9581
05:29:39,440 --> 05:29:41,440
let's begin by importing time
9582
05:29:41,440 --> 05:29:42,798
and the first thing that i'm going to
9583
05:29:42,798 --> 05:29:44,558
explain to you while is how we can find
9584
05:29:44,558 --> 05:29:46,160
our computer's epic
9585
05:29:46,160 --> 05:29:49,200
also pronounced as epoch so this is a
9586
05:29:49,200 --> 05:29:50,160
date and time
9587
05:29:50,160 --> 05:29:52,958
in which your computer thinks time began
9588
05:29:52,958 --> 05:29:54,480
think of it that way at least
9589
05:29:54,480 --> 05:29:58,080
so we use our epic as a reference point
9590
05:29:58,080 --> 05:29:59,920
so to find your computer's epic it's
9591
05:29:59,920 --> 05:30:01,600
going to vary based on your computer and
9592
05:30:01,600 --> 05:30:02,958
your operating system
9593
05:30:02,958 --> 05:30:04,958
so to find your computer's epic type
9594
05:30:04,958 --> 05:30:06,320
time dot
9595
05:30:06,320 --> 05:30:08,878
c time and as an argument we will pass
9596
05:30:08,878 --> 05:30:10,000
in zero
9597
05:30:10,000 --> 05:30:12,080
now what this method will do is that
9598
05:30:12,080 --> 05:30:14,320
this method will convert a time
9599
05:30:14,320 --> 05:30:16,958
expressed in seconds and convert it to a
9600
05:30:16,958 --> 05:30:18,160
readable string
9601
05:30:18,160 --> 05:30:20,878
so if i was to pass in 0 and print it
9602
05:30:20,878 --> 05:30:21,440
well then
9603
05:30:21,440 --> 05:30:23,920
this will display the date and time
9604
05:30:23,920 --> 05:30:25,200
which is my epic
9605
05:30:25,200 --> 05:30:28,718
our reference point so for me my epic is
9606
05:30:28,718 --> 05:30:31,878
wednesday december 31st 6 p.m
9607
05:30:31,878 --> 05:30:34,958
1969 so i've just added a note
9608
05:30:34,958 --> 05:30:37,280
that the c time method will convert a
9609
05:30:37,280 --> 05:30:39,680
time expressed in seconds
9610
05:30:39,680 --> 05:30:42,638
since epic and convert it to a readable
9611
05:30:42,638 --> 05:30:44,240
string a readable
9612
05:30:44,240 --> 05:30:47,120
date and time so for practice let's pass
9613
05:30:47,120 --> 05:30:47,920
in perhaps
9614
05:30:47,920 --> 05:30:50,000
1 million seconds and see what date and
9615
05:30:50,000 --> 05:30:51,520
time that we receive
9616
05:30:51,520 --> 05:30:54,798
so our c time method will return a date
9617
05:30:54,798 --> 05:30:55,760
and time
9618
05:30:55,760 --> 05:30:59,200
one million seconds past this epic here
9619
05:30:59,200 --> 05:31:02,320
so that would be for me monday january
9620
05:31:02,320 --> 05:31:02,798
12th
9621
05:31:02,798 --> 05:31:06,798
about 7 a.m 1970. our next method is
9622
05:31:06,798 --> 05:31:09,520
the time method of the time module let's
9623
05:31:09,520 --> 05:31:10,000
print
9624
05:31:10,000 --> 05:31:12,958
time dot time method and what this
9625
05:31:12,958 --> 05:31:13,760
method will do
9626
05:31:13,760 --> 05:31:16,240
is return the current seconds that have
9627
05:31:16,240 --> 05:31:16,958
passed
9628
05:31:16,958 --> 05:31:19,360
since our epic using our computer's
9629
05:31:19,360 --> 05:31:20,160
clock
9630
05:31:20,160 --> 05:31:23,200
so the number that i end up with is just
9631
05:31:23,200 --> 05:31:23,760
over
9632
05:31:23,760 --> 05:31:27,200
1.6 billion and this is in seconds
9633
05:31:27,200 --> 05:31:30,320
so for me 1.6 billion seconds
9634
05:31:30,320 --> 05:31:33,120
has passed since that date which is my
9635
05:31:33,120 --> 05:31:33,600
epic
9636
05:31:33,600 --> 05:31:37,120
i believe it was december 31st 1969
9637
05:31:37,120 --> 05:31:39,760
and every time that i rerun this program
9638
05:31:39,760 --> 05:31:41,840
you can see that the amount of seconds
9639
05:31:41,840 --> 05:31:44,638
that has passed is actually increasing
9640
05:31:44,638 --> 05:31:45,040
so
9641
05:31:45,040 --> 05:31:47,840
that is the time method it will return
9642
05:31:47,840 --> 05:31:49,120
the current seconds
9643
05:31:49,120 --> 05:31:51,280
since you're epic using your computer's
9644
05:31:51,280 --> 05:31:52,798
clock and you know what
9645
05:31:52,798 --> 05:31:55,120
just for fun let's change the date and
9646
05:31:55,120 --> 05:31:57,520
time under clock and see what happens
9647
05:31:57,520 --> 05:32:00,718
so let's change the year to
9648
05:32:00,718 --> 05:32:04,320
how about the year 2000 change
9649
05:32:04,320 --> 05:32:07,040
and close so let's see what number we
9650
05:32:07,040 --> 05:32:09,840
get with the time method now
9651
05:32:09,840 --> 05:32:14,400
so we get just under 1 billion 948
9652
05:32:14,400 --> 05:32:16,160
million seconds have passed
9653
05:32:16,160 --> 05:32:18,400
and now if you need to retrieve the
9654
05:32:18,400 --> 05:32:19,520
current date and time
9655
05:32:19,520 --> 05:32:21,040
well there's one of a few ways in which
9656
05:32:21,040 --> 05:32:23,520
you could retrieve that but one way is
9657
05:32:23,520 --> 05:32:24,798
that we can combine
9658
05:32:24,798 --> 05:32:27,120
both of these methods of the see time
9659
05:32:27,120 --> 05:32:29,200
method and the time method of the time
9660
05:32:29,200 --> 05:32:30,080
module
9661
05:32:30,080 --> 05:32:33,200
so let's print time dot c
9662
05:32:33,200 --> 05:32:35,520
time and we're going to pass in an
9663
05:32:35,520 --> 05:32:36,558
amount of seconds
9664
05:32:36,558 --> 05:32:39,760
into the c time method as an argument so
9665
05:32:39,760 --> 05:32:40,240
within
9666
05:32:40,240 --> 05:32:42,400
the c time method we're going to call
9667
05:32:42,400 --> 05:32:44,000
the time method
9668
05:32:44,000 --> 05:32:46,400
so the time method will return an amount
9669
05:32:46,400 --> 05:32:47,120
of seconds
9670
05:32:47,120 --> 05:32:49,680
since our epic and the c time method
9671
05:32:49,680 --> 05:32:50,400
will convert
9672
05:32:50,400 --> 05:32:52,400
that amount of seconds to a readable
9673
05:32:52,400 --> 05:32:53,680
date and time
9674
05:32:53,680 --> 05:32:57,120
so the current date and time is saturday
9675
05:32:57,120 --> 05:33:00,558
january 23rd about 3 p.m 20 21
9676
05:33:00,558 --> 05:33:02,240
now there is more than one way to get
9677
05:33:02,240 --> 05:33:03,680
the current date and time
9678
05:33:03,680 --> 05:33:06,718
another way is to use the local time
9679
05:33:06,718 --> 05:33:08,958
method and the local time method will
9680
05:33:08,958 --> 05:33:09,840
create a
9681
05:33:09,840 --> 05:33:12,878
time object based on the current time so
9682
05:33:12,878 --> 05:33:14,000
what i'm going to do is
9683
05:33:14,000 --> 05:33:17,440
create a variable called time object
9684
05:33:17,440 --> 05:33:19,360
and i just need to explain a few things
9685
05:33:19,360 --> 05:33:20,480
about time objects
9686
05:33:20,480 --> 05:33:22,558
so to best explain this i'm going to
9687
05:33:22,558 --> 05:33:23,600
print our time
9688
05:33:23,600 --> 05:33:26,718
object now a time object is also
9689
05:33:26,718 --> 05:33:27,840
referred to as a
9690
05:33:27,840 --> 05:33:30,798
struct time object it is made up of
9691
05:33:30,798 --> 05:33:32,718
different keyword arguments
9692
05:33:32,718 --> 05:33:35,840
there's a year a month day
9693
05:33:35,840 --> 05:33:39,200
hour minutes seconds day of the week
9694
05:33:39,200 --> 05:33:41,440
day of the year and this keyword
9695
05:33:41,440 --> 05:33:42,958
argument here has something to do with
9696
05:33:42,958 --> 05:33:44,400
daylight savings time
9697
05:33:44,400 --> 05:33:46,958
so there's quite a few uses with time
9698
05:33:46,958 --> 05:33:47,840
objects
9699
05:33:47,840 --> 05:33:50,160
and one way is that we can format them
9700
05:33:50,160 --> 05:33:52,480
however we want because right now
9701
05:33:52,480 --> 05:33:54,798
this time object is not in a readable
9702
05:33:54,798 --> 05:33:55,520
format
9703
05:33:55,520 --> 05:33:58,320
so to convert this time object into a
9704
05:33:58,320 --> 05:34:00,400
readable string we'll need the help of a
9705
05:34:00,400 --> 05:34:01,680
separate function
9706
05:34:01,680 --> 05:34:05,600
and that is the strf time function
9707
05:34:05,600 --> 05:34:08,798
str is short for string f4 format and
9708
05:34:08,798 --> 05:34:09,200
time
9709
05:34:09,200 --> 05:34:12,480
well for time i guess so this function
9710
05:34:12,480 --> 05:34:15,600
needs two arguments a format and a time
9711
05:34:15,600 --> 05:34:18,958
object so our strf time function will
9712
05:34:18,958 --> 05:34:19,520
accept
9713
05:34:19,520 --> 05:34:22,558
a format and a time object as an
9714
05:34:22,558 --> 05:34:23,440
argument
9715
05:34:23,440 --> 05:34:26,798
so our format is really just a string
9716
05:34:26,798 --> 05:34:28,878
of different directives and to best
9717
05:34:28,878 --> 05:34:30,558
explain these i'm going to head to
9718
05:34:30,558 --> 05:34:32,798
python's official documentation on the
9719
05:34:32,798 --> 05:34:33,520
subject
9720
05:34:33,520 --> 05:34:35,680
so here i am on python's website
9721
05:34:35,680 --> 05:34:38,080
regarding the time module and underneath
9722
05:34:38,080 --> 05:34:41,360
this section on the strf time function
9723
05:34:41,360 --> 05:34:43,360
there are different directives that we
9724
05:34:43,360 --> 05:34:45,760
can embed within our format string that
9725
05:34:45,760 --> 05:34:47,440
we pass in as an argument
9726
05:34:47,440 --> 05:34:49,840
depending on the directive that we add
9727
05:34:49,840 --> 05:34:50,798
we can display
9728
05:34:50,798 --> 05:34:54,480
a certain format of our date and time
9729
05:34:54,480 --> 05:34:57,360
so for example if i was to pass in let's
9730
05:34:57,360 --> 05:34:57,840
say
9731
05:34:57,840 --> 05:35:01,200
percent lowercase a then we will display
9732
05:35:01,200 --> 05:35:04,000
the time object's weekday name and
9733
05:35:04,000 --> 05:35:05,920
you're not limited to just one directive
9734
05:35:05,920 --> 05:35:07,520
you can add any combination of
9735
05:35:07,520 --> 05:35:08,558
directives
9736
05:35:08,558 --> 05:35:11,040
so if i was to add percent m well we
9737
05:35:11,040 --> 05:35:12,160
would display
9738
05:35:12,160 --> 05:35:14,718
the month of our date time object as a
9739
05:35:14,718 --> 05:35:15,200
number
9740
05:35:15,200 --> 05:35:17,360
1 through 12. so there's a bunch of
9741
05:35:17,360 --> 05:35:18,958
directives here and i'm going to be
9742
05:35:18,958 --> 05:35:20,160
using some of these
9743
05:35:20,160 --> 05:35:22,638
so within a string for the format
9744
05:35:22,638 --> 05:35:24,400
argument i'm going to pass in
9745
05:35:24,400 --> 05:35:27,200
percent capital b for the name of the
9746
05:35:27,200 --> 05:35:28,400
month
9747
05:35:28,400 --> 05:35:31,760
percent d for the day
9748
05:35:31,760 --> 05:35:35,120
percent y for the year
9749
05:35:35,120 --> 05:35:38,000
percent h for the hour and to format
9750
05:35:38,000 --> 05:35:40,000
this i'm going to add a colon
9751
05:35:40,000 --> 05:35:43,600
to separate hours and minutes percent
9752
05:35:43,600 --> 05:35:48,320
m for minutes colon percent s
9753
05:35:48,320 --> 05:35:51,040
for seconds and then we are going to
9754
05:35:51,040 --> 05:35:51,520
assign
9755
05:35:51,520 --> 05:35:54,320
all of this to a variable let's say
9756
05:35:54,320 --> 05:35:55,360
local
9757
05:35:55,360 --> 05:35:58,878
time and local time will be a string
9758
05:35:58,878 --> 05:36:03,120
so let's print our local time
9759
05:36:03,120 --> 05:36:06,558
and the current time is january
9760
05:36:06,558 --> 05:36:10,160
23rd 2021 about 3 p.m
9761
05:36:10,160 --> 05:36:12,320
oh and i almost forgot you can also get
9762
05:36:12,320 --> 05:36:13,760
the utc
9763
05:36:13,760 --> 05:36:16,480
time that is the coordinated universal
9764
05:36:16,480 --> 05:36:18,240
time if you know how that works
9765
05:36:18,240 --> 05:36:20,320
so if you need that you would just use
9766
05:36:20,320 --> 05:36:21,600
the gm
9767
05:36:21,600 --> 05:36:25,120
time method for the utc time coordinated
9768
05:36:25,120 --> 05:36:26,558
universal time
9769
05:36:26,558 --> 05:36:29,680
okay next up we have the strp
9770
05:36:29,680 --> 05:36:32,240
time function and this function will
9771
05:36:32,240 --> 05:36:32,718
parse
9772
05:36:32,718 --> 05:36:36,080
a string representation of a time and or
9773
05:36:36,080 --> 05:36:36,638
date
9774
05:36:36,638 --> 05:36:39,360
and return a time object so we need to
9775
05:36:39,360 --> 05:36:39,920
pass in
9776
05:36:39,920 --> 05:36:42,000
a string representing the date and or
9777
05:36:42,000 --> 05:36:44,000
time as well as a format
9778
05:36:44,000 --> 05:36:47,120
string so let's create a time string and
9779
05:36:47,120 --> 05:36:49,120
this variable is going to be a string
9780
05:36:49,120 --> 05:36:50,240
representation of
9781
05:36:50,240 --> 05:36:54,080
a date let's say 20th of april 2020
9782
05:36:54,080 --> 05:36:56,000
and what we can do is take this string
9783
05:36:56,000 --> 05:36:58,718
representation of a time and or date
9784
05:36:58,718 --> 05:37:01,440
and parse it to a time object so we're
9785
05:37:01,440 --> 05:37:02,958
going to pass in our time
9786
05:37:02,958 --> 05:37:05,760
string variable as well as a format
9787
05:37:05,760 --> 05:37:06,480
string
9788
05:37:06,480 --> 05:37:10,080
so let's say i would like to parse
9789
05:37:10,080 --> 05:37:12,558
the day so that would be percent d for
9790
05:37:12,558 --> 05:37:13,520
day
9791
05:37:13,520 --> 05:37:17,680
then percent b for name of the month
9792
05:37:17,680 --> 05:37:21,840
and then comma percent y for year
9793
05:37:21,840 --> 05:37:23,840
this function will create a time object
9794
05:37:23,840 --> 05:37:26,080
so let's assign that to a variable
9795
05:37:26,080 --> 05:37:29,680
time object equals time dot strp time
9796
05:37:29,680 --> 05:37:31,200
we're passing in our string
9797
05:37:31,200 --> 05:37:32,878
representation of a time
9798
05:37:32,878 --> 05:37:35,920
or date as well as a format string
9799
05:37:35,920 --> 05:37:38,558
and we can print our time object using a
9800
05:37:38,558 --> 05:37:40,080
print statement
9801
05:37:40,080 --> 05:37:41,920
however this is going to be in a form
9802
05:37:41,920 --> 05:37:43,840
that is somewhat difficult to read but
9803
05:37:43,840 --> 05:37:45,440
you can see at least we have a time
9804
05:37:45,440 --> 05:37:46,160
object
9805
05:37:46,160 --> 05:37:48,000
with all of these keywords filled in
9806
05:37:48,000 --> 05:37:49,520
with anything that we passed in
9807
05:37:49,520 --> 05:37:52,160
via these format directives that we have
9808
05:37:52,160 --> 05:37:53,360
now the next function
9809
05:37:53,360 --> 05:37:56,718
is the asc time function
9810
05:37:56,718 --> 05:37:59,520
and this function accepts a time object
9811
05:37:59,520 --> 05:38:01,680
or a tuple representation
9812
05:38:01,680 --> 05:38:04,160
of a relative time so this time let's
9813
05:38:04,160 --> 05:38:04,798
create a
9814
05:38:04,798 --> 05:38:08,160
time tuple and we're going to follow
9815
05:38:08,160 --> 05:38:09,680
this order we can pass in
9816
05:38:09,680 --> 05:38:12,798
up to nine values the first value is a
9817
05:38:12,798 --> 05:38:14,480
year so let's pass in perhaps
9818
05:38:14,480 --> 05:38:17,520
2020 a month let's say four
9819
05:38:17,520 --> 05:38:20,718
a day how about 20 four hours
9820
05:38:20,718 --> 05:38:24,000
let's say four minutes 20 and seconds
9821
05:38:24,000 --> 05:38:26,320
maybe zero let me just fix some of the
9822
05:38:26,320 --> 05:38:27,760
spacing here
9823
05:38:27,760 --> 05:38:30,160
okay you can also pass in a numbered day
9824
05:38:30,160 --> 05:38:31,120
of the week
9825
05:38:31,120 --> 05:38:33,600
um i'm just going to say zero i don't
9826
05:38:33,600 --> 05:38:35,040
think it's really that important
9827
05:38:35,040 --> 05:38:38,160
a day of the year zero and
9828
05:38:38,160 --> 05:38:39,920
negative one or zero for daylight
9829
05:38:39,920 --> 05:38:41,360
savings time so
9830
05:38:41,360 --> 05:38:44,000
we created a time tuple and we can pass
9831
05:38:44,000 --> 05:38:44,718
in a time
9832
05:38:44,718 --> 05:38:47,760
object or a tuple representation of a
9833
05:38:47,760 --> 05:38:48,240
time
9834
05:38:48,240 --> 05:38:51,120
following this formula so let's pass in
9835
05:38:51,120 --> 05:38:52,320
our time tuple
9836
05:38:52,320 --> 05:38:55,360
and this will create a time string a
9837
05:38:55,360 --> 05:38:57,440
string representation of the time that
9838
05:38:57,440 --> 05:38:58,400
we create
9839
05:38:58,400 --> 05:39:02,480
and let's print our time string
9840
05:39:02,798 --> 05:39:05,840
and we should have april 20th
9841
05:39:05,840 --> 05:39:08,638
about four in the morning the year 2020.
9842
05:39:08,638 --> 05:39:11,760
so that is asc time it will convert a
9843
05:39:11,760 --> 05:39:15,040
tuple representation of a time and date
9844
05:39:15,040 --> 05:39:17,440
or a time object and convert it to a
9845
05:39:17,440 --> 05:39:18,160
readable
9846
05:39:18,160 --> 05:39:21,520
string now another option is to use m
9847
05:39:21,520 --> 05:39:24,878
k time and mk time will take a
9848
05:39:24,878 --> 05:39:28,160
tuple representation of a time or a time
9849
05:39:28,160 --> 05:39:28,878
object
9850
05:39:28,878 --> 05:39:32,160
and convert it to seconds since epic
9851
05:39:32,160 --> 05:39:35,440
so april 20th of the year 2020
9852
05:39:35,440 --> 05:39:38,558
is about 1.5 billion seconds
9853
05:39:38,558 --> 05:39:40,958
since our epic date and for me that was
9854
05:39:40,958 --> 05:39:44,558
december 31st the year 1969
9855
05:39:44,558 --> 05:39:47,920
so that is a few useful functions of the
9856
05:39:47,920 --> 05:39:49,120
time module
9857
05:39:49,120 --> 05:39:50,798
if you would like a copy of this code i
9858
05:39:50,798 --> 05:39:52,320
will post all this to the comment
9859
05:39:52,320 --> 05:39:53,520
section down below
9860
05:39:53,520 --> 05:39:56,240
and well yeah that's the time module in
9861
05:39:56,240 --> 05:39:58,000
python
9862
05:39:58,000 --> 05:40:00,400
yo what's going on everybody it's bro
9863
05:40:00,400 --> 05:40:01,360
hope you're doing well
9864
05:40:01,360 --> 05:40:03,040
and in this video i'm going to explain
9865
05:40:03,040 --> 05:40:04,878
multi-threading in python so
9866
05:40:04,878 --> 05:40:09,360
sit back relax and enjoy the show
9867
05:40:09,440 --> 05:40:12,080
think of a thread as a flow of execution
9868
05:40:12,080 --> 05:40:13,120
like a river
9869
05:40:13,120 --> 05:40:15,600
and each thread can carry out its own
9870
05:40:15,600 --> 05:40:17,280
separate order of instructions
9871
05:40:17,280 --> 05:40:18,878
if we use this process of
9872
05:40:18,878 --> 05:40:21,120
multi-threading we can have our program
9873
05:40:21,120 --> 05:40:21,600
run
9874
05:40:21,600 --> 05:40:23,680
different parts of its program at
9875
05:40:23,680 --> 05:40:24,878
different times
9876
05:40:24,878 --> 05:40:27,040
they all run concurrently but not truly
9877
05:40:27,040 --> 05:40:29,520
in parallel that is a concept for later
9878
05:40:29,520 --> 05:40:31,840
referred to as multi-processing
9879
05:40:31,840 --> 05:40:34,480
so with reds they each take a turn
9880
05:40:34,480 --> 05:40:36,558
running to achieve concurrency
9881
05:40:36,558 --> 05:40:38,958
this is due to a notorious feature known
9882
05:40:38,958 --> 05:40:40,638
as the gil
9883
05:40:40,638 --> 05:40:43,440
the global interpreter lock only one
9884
05:40:43,440 --> 05:40:45,440
thread can be running at one time
9885
05:40:45,440 --> 05:40:47,440
but they can all take turns when one
9886
05:40:47,440 --> 05:40:48,798
thread is idle
9887
05:40:48,798 --> 05:40:50,958
so this allows one thread to hold
9888
05:40:50,958 --> 05:40:53,040
control of the python interpreter
9889
05:40:53,040 --> 05:40:55,920
at any one time so they run concurrently
9890
05:40:55,920 --> 05:40:57,760
but not truly in parallel
9891
05:40:57,760 --> 05:40:59,000
which is what we do with
9892
05:40:59,000 --> 05:41:01,600
multi-processing now programs and tasks
9893
05:41:01,600 --> 05:41:02,958
can be divided into
9894
05:41:02,958 --> 05:41:05,280
two different categories they can be cpu
9895
05:41:05,280 --> 05:41:08,080
bound that is a program or a task
9896
05:41:08,080 --> 05:41:10,480
that spends most of its time waiting for
9897
05:41:10,480 --> 05:41:11,680
internal events
9898
05:41:11,680 --> 05:41:14,878
such as a task that is cpu intensive
9899
05:41:14,878 --> 05:41:17,360
it is better to use multi-processing for
9900
05:41:17,360 --> 05:41:19,600
tasks that are cpu bound
9901
05:41:19,600 --> 05:41:22,798
now tasks that are io bound means that
9902
05:41:22,798 --> 05:41:24,798
the program will spend most of its time
9903
05:41:24,798 --> 05:41:26,878
waiting for external events
9904
05:41:26,878 --> 05:41:29,280
such as waiting for user input or if
9905
05:41:29,280 --> 05:41:30,878
you're doing activities like web
9906
05:41:30,878 --> 05:41:31,520
scraping
9907
05:41:31,520 --> 05:41:33,840
you do a lot of sitting around so with i
9908
05:41:33,840 --> 05:41:35,680
o bound tasks it's better to use
9909
05:41:35,680 --> 05:41:36,718
multi-threading
9910
05:41:36,718 --> 05:41:38,798
because we can have multiple threads
9911
05:41:38,798 --> 05:41:40,080
running concurrently
9912
05:41:40,080 --> 05:41:42,080
but not truly in parallel like what we
9913
05:41:42,080 --> 05:41:43,920
do with multi-processing
9914
05:41:43,920 --> 05:41:46,160
after importing the threading module we
9915
05:41:46,160 --> 05:41:48,160
can count the number of threads that are
9916
05:41:48,160 --> 05:41:48,958
currently running
9917
05:41:48,958 --> 05:41:50,798
in the background whenever we run a
9918
05:41:50,798 --> 05:41:52,798
program we have one thread that is
9919
05:41:52,798 --> 05:41:54,400
running that is in charge of
9920
05:41:54,400 --> 05:41:56,958
executing our program and we can print
9921
05:41:56,958 --> 05:41:58,878
the active count of threads running in
9922
05:41:58,878 --> 05:41:59,520
our program
9923
05:41:59,520 --> 05:42:01,920
using the active count function of the
9924
05:42:01,920 --> 05:42:03,120
threading module
9925
05:42:03,120 --> 05:42:05,680
so this will print one we have one
9926
05:42:05,680 --> 05:42:06,958
thread that is running
9927
05:42:06,958 --> 05:42:09,760
and we can print a list of all the
9928
05:42:09,760 --> 05:42:11,040
threads that are running
9929
05:42:11,040 --> 05:42:14,080
by using the enumerate function so
9930
05:42:14,080 --> 05:42:16,080
the one thread that is in charge of
9931
05:42:16,080 --> 05:42:17,360
running our program
9932
05:42:17,360 --> 05:42:20,080
is referred to as the main thread by
9933
05:42:20,080 --> 05:42:22,400
using this concept of multi-threading we
9934
05:42:22,400 --> 05:42:22,958
can have
9935
05:42:22,958 --> 05:42:24,718
more than one thread running
9936
05:42:24,718 --> 05:42:27,120
concurrently not truly in parallel
9937
05:42:27,120 --> 05:42:28,958
all the threads will take turns while
9938
05:42:28,958 --> 05:42:30,400
one of them is idle
9939
05:42:30,400 --> 05:42:32,638
so we can have more than one thread
9940
05:42:32,638 --> 05:42:34,000
running more than just the main
9941
05:42:34,000 --> 05:42:35,840
thread which is in charge of running the
9942
05:42:35,840 --> 05:42:37,360
main body of our program
9943
05:42:37,360 --> 05:42:39,520
so while our main thread is in charge of
9944
05:42:39,520 --> 05:42:41,600
running the main body of our program
9945
05:42:41,600 --> 05:42:43,280
we can have another thread that's in
9946
05:42:43,280 --> 05:42:45,280
charge of a separate part of it
9947
05:42:45,280 --> 05:42:47,120
maybe like a countdown timer or
9948
05:42:47,120 --> 05:42:48,400
something so
9949
05:42:48,400 --> 05:42:50,718
one good example take that quiz game
9950
05:42:50,718 --> 05:42:52,878
that we made some number of videos ago
9951
05:42:52,878 --> 05:42:55,040
while we were waiting for user input
9952
05:42:55,040 --> 05:42:56,000
which is a
9953
05:42:56,000 --> 05:42:58,080
i o bound task we could have had a
9954
05:42:58,080 --> 05:42:59,520
countdown timer going
9955
05:42:59,520 --> 05:43:01,200
like you only have so many seconds to
9956
05:43:01,200 --> 05:43:03,200
answer this question we could have had
9957
05:43:03,200 --> 05:43:05,200
one thread in charge of waiting for user
9958
05:43:05,200 --> 05:43:06,878
input and another thread
9959
05:43:06,878 --> 05:43:09,120
in charge of the countdown timer so
9960
05:43:09,120 --> 05:43:11,200
that's an example of multi-threading
9961
05:43:11,200 --> 05:43:14,000
we had two threads running concurrently
9962
05:43:14,000 --> 05:43:15,680
and what we'll be doing in this video is
9963
05:43:15,680 --> 05:43:17,520
creating a program that involves
9964
05:43:17,520 --> 05:43:19,120
multi-threading we can have
9965
05:43:19,120 --> 05:43:21,040
different threads in charge of different
9966
05:43:21,040 --> 05:43:22,878
parts of our program and they can all
9967
05:43:22,878 --> 05:43:23,200
run
9968
05:43:23,200 --> 05:43:25,200
concurrently they'll all take turns
9969
05:43:25,200 --> 05:43:26,958
while one of them is idle
9970
05:43:26,958 --> 05:43:29,600
so let's say that we're running late for
9971
05:43:29,600 --> 05:43:30,080
school
9972
05:43:30,080 --> 05:43:32,480
or work in the morning and we have three
9973
05:43:32,480 --> 05:43:34,320
different tasks that we need to complete
9974
05:43:34,320 --> 05:43:35,360
before we can leave
9975
05:43:35,360 --> 05:43:38,000
for school or work so think of three
9976
05:43:38,000 --> 05:43:38,558
different
9977
05:43:38,558 --> 05:43:40,320
things you do in the morning such as
9978
05:43:40,320 --> 05:43:41,600
maybe eat
9979
05:43:41,600 --> 05:43:44,958
breakfast some people drink coffee or
9980
05:43:44,958 --> 05:43:48,240
maybe a beverage of your choice so drink
9981
05:43:48,240 --> 05:43:51,280
coffee and some people like me they like
9982
05:43:51,280 --> 05:43:51,680
to
9983
05:43:51,680 --> 05:43:53,520
do their homework last minute so i'll
9984
05:43:53,520 --> 05:43:54,878
say study
9985
05:43:54,878 --> 05:43:57,520
before i leave for work or school in the
9986
05:43:57,520 --> 05:43:58,320
morning
9987
05:43:58,320 --> 05:44:00,000
so what we'll do in each of these
9988
05:44:00,000 --> 05:44:01,760
functions each of these functions
9989
05:44:01,760 --> 05:44:03,600
should take some amount of time to
9990
05:44:03,600 --> 05:44:06,000
complete so we can have our main thread
9991
05:44:06,000 --> 05:44:07,840
sleep for a given number of seconds
9992
05:44:07,840 --> 05:44:10,240
using the sleep function
9993
05:44:10,240 --> 05:44:12,558
time dot sleep and let's say that in
9994
05:44:12,558 --> 05:44:14,080
order for me to eat breakfast
9995
05:44:14,080 --> 05:44:16,638
this task will take me three seconds
9996
05:44:16,638 --> 05:44:18,240
let's just pretend that instead of
9997
05:44:18,240 --> 05:44:19,200
minutes this will be
9998
05:44:19,200 --> 05:44:23,120
in seconds so drink coffee will take me
9999
05:44:23,120 --> 05:44:26,320
four seconds and study will take me
10000
05:44:26,320 --> 05:44:30,240
five seconds then when we finish
10001
05:44:30,240 --> 05:44:31,360
sleeping
10002
05:44:31,360 --> 05:44:34,080
let's print a confirmation message let's
10003
05:44:34,080 --> 05:44:35,440
say
10004
05:44:35,440 --> 05:44:38,718
you eat breakfast
10005
05:44:38,718 --> 05:44:41,760
as in you finish eating breakfast with
10006
05:44:41,760 --> 05:44:43,440
drink coffee
10007
05:44:43,440 --> 05:44:48,000
you drink coffee
10008
05:44:48,000 --> 05:44:50,878
and with study
10009
05:44:51,760 --> 05:44:56,080
you finish studying
10010
05:44:56,080 --> 05:44:58,798
now each of these tasks are i o bound
10011
05:44:58,798 --> 05:45:00,160
they're going to be spending a lot of
10012
05:45:00,160 --> 05:45:01,520
time just waiting around
10013
05:45:01,520 --> 05:45:03,920
for external events they're waiting for
10014
05:45:03,920 --> 05:45:06,160
the sleep function to expire before they
10015
05:45:06,160 --> 05:45:07,680
can finish their task
10016
05:45:07,680 --> 05:45:09,440
so we're going to have all of these
10017
05:45:09,440 --> 05:45:12,240
three functions run on our main thread
10018
05:45:12,240 --> 05:45:13,840
and we'll see how long it takes for us
10019
05:45:13,840 --> 05:45:15,840
to complete our morning ritual
10020
05:45:15,840 --> 05:45:18,160
these three tasks so let's call these
10021
05:45:18,160 --> 05:45:19,440
three functions within
10022
05:45:19,440 --> 05:45:22,558
our main thread so let's call the
10023
05:45:22,558 --> 05:45:26,798
eat breakfast function first followed by
10024
05:45:26,798 --> 05:45:30,558
drink coffee and then study
10025
05:45:30,558 --> 05:45:33,680
in that order so this program is going
10026
05:45:33,680 --> 05:45:35,200
to take approximately
10027
05:45:35,200 --> 05:45:38,240
let's see 12 seconds to complete so
10028
05:45:38,240 --> 05:45:40,958
there's going to be a pause for a second
10029
05:45:40,958 --> 05:45:44,958
you eat breakfast then followed by
10030
05:45:44,958 --> 05:45:50,718
you drank coffee and then study
10031
05:45:50,718 --> 05:45:52,878
you finished studying so this program
10032
05:45:52,878 --> 05:45:53,760
took about
10033
05:45:53,760 --> 05:45:56,480
12 seconds overall so if this were
10034
05:45:56,480 --> 05:45:57,280
realistic
10035
05:45:57,280 --> 05:45:59,280
what we would have done is we would sit
10036
05:45:59,280 --> 05:46:01,040
down and eat breakfast for
10037
05:46:01,040 --> 05:46:03,040
three minutes well three seconds in this
10038
05:46:03,040 --> 05:46:04,400
case and then
10039
05:46:04,400 --> 05:46:06,878
once we finish eating breakfast only
10040
05:46:06,878 --> 05:46:09,360
then are we allowed to drink our coffee
10041
05:46:09,360 --> 05:46:12,240
and once we finish our coffee only then
10042
05:46:12,240 --> 05:46:13,520
can we study
10043
05:46:13,520 --> 05:46:16,480
so we completed these tasks sequentially
10044
05:46:16,480 --> 05:46:19,040
and not concurrently for us to move down
10045
05:46:19,040 --> 05:46:19,760
to the next
10046
05:46:19,760 --> 05:46:21,680
function we need to complete the
10047
05:46:21,680 --> 05:46:23,440
previous functions because we're doing
10048
05:46:23,440 --> 05:46:24,558
this in order
10049
05:46:24,558 --> 05:46:27,440
but realistically us human beings we
10050
05:46:27,440 --> 05:46:28,480
would probably
10051
05:46:28,480 --> 05:46:30,878
eat breakfast drink coffee and study
10052
05:46:30,878 --> 05:46:32,000
altogether because
10053
05:46:32,000 --> 05:46:34,400
we can multitask and we can complete
10054
05:46:34,400 --> 05:46:36,400
these three functions in less time
10055
05:46:36,400 --> 05:46:38,240
and that's kind of the same process as
10056
05:46:38,240 --> 05:46:40,000
multi-threading we can have
10057
05:46:40,000 --> 05:46:42,320
these three separate functions running
10058
05:46:42,320 --> 05:46:43,040
concurrently
10059
05:46:43,040 --> 05:46:45,200
as if we're multitasking we're eating
10060
05:46:45,200 --> 05:46:46,798
breakfast drinking coffee
10061
05:46:46,798 --> 05:46:49,440
and studying all at once now currently
10062
05:46:49,440 --> 05:46:50,000
we have
10063
05:46:50,000 --> 05:46:52,558
one thread that is in charge of these
10064
05:46:52,558 --> 05:46:54,160
three separate functions
10065
05:46:54,160 --> 05:46:56,240
what we could do is that we can create
10066
05:46:56,240 --> 05:46:57,680
three additional threads
10067
05:46:57,680 --> 05:47:00,000
each thread will be in charge of each
10068
05:47:00,000 --> 05:47:02,240
task and then we'll have our main thread
10069
05:47:02,240 --> 05:47:03,520
running in the background that will
10070
05:47:03,520 --> 05:47:05,120
complete the rest of the program
10071
05:47:05,120 --> 05:47:06,878
so this is how to create an additional
10072
05:47:06,878 --> 05:47:08,958
thread let's say that
10073
05:47:08,958 --> 05:47:12,320
x equals threading
10074
05:47:12,320 --> 05:47:15,920
dot thread we need to pass in a target
10075
05:47:15,920 --> 05:47:20,080
target equals and then the name of the
10076
05:47:20,080 --> 05:47:21,520
function so let's say
10077
05:47:21,520 --> 05:47:23,680
thread x will be in charge of eating
10078
05:47:23,680 --> 05:47:24,798
breakfast
10079
05:47:24,798 --> 05:47:26,638
and then you can pass in arguments too
10080
05:47:26,638 --> 05:47:28,638
if your function has parameters by
10081
05:47:28,638 --> 05:47:29,680
typing args
10082
05:47:29,680 --> 05:47:31,600
and then you pass in a tuple so let's
10083
05:47:31,600 --> 05:47:33,600
say you have one argument to pass in
10084
05:47:33,600 --> 05:47:35,600
you'll type your argument followed by
10085
05:47:35,600 --> 05:47:37,680
a comma but we don't have any arguments
10086
05:47:37,680 --> 05:47:38,958
in this example but
10087
05:47:38,958 --> 05:47:40,638
you'll need to be sure to enter them in
10088
05:47:40,638 --> 05:47:42,080
if you do have any
10089
05:47:42,080 --> 05:47:44,638
okay then to begin this thread you type
10090
05:47:44,638 --> 05:47:45,600
the name of the thread
10091
05:47:45,600 --> 05:47:48,878
in this case x dot start so
10092
05:47:48,878 --> 05:47:50,798
we now have an additional thread and
10093
05:47:50,798 --> 05:47:52,320
this thread is in charge of eating
10094
05:47:52,320 --> 05:47:53,200
breakfast
10095
05:47:53,200 --> 05:47:55,840
now let's create another thread to drink
10096
05:47:55,840 --> 05:47:57,280
coffee
10097
05:47:57,280 --> 05:48:00,558
and we'll call this y
10098
05:48:01,760 --> 05:48:05,360
and the target will be drink coffee
10099
05:48:05,360 --> 05:48:08,840
and lastly we have a thread in charge of
10100
05:48:08,840 --> 05:48:10,638
studying
10101
05:48:10,638 --> 05:48:13,840
and this will be z
10102
05:48:14,798 --> 05:48:17,920
and the target is our study function
10103
05:48:17,920 --> 05:48:19,920
okay now let's see how long it takes for
10104
05:48:19,920 --> 05:48:21,520
us to complete our program
10105
05:48:21,520 --> 05:48:23,600
oh and be sure to comment out these
10106
05:48:23,600 --> 05:48:25,200
function calls within the main thread
10107
05:48:25,200 --> 05:48:26,558
because we don't want the main thread in
10108
05:48:26,558 --> 05:48:27,760
charge of those anymore
10109
05:48:27,760 --> 05:48:30,240
okay now we can run the program so we
10110
05:48:30,240 --> 05:48:32,798
have four threads running this time
10111
05:48:32,798 --> 05:48:34,878
you eat breakfast you drink coffee you
10112
05:48:34,878 --> 05:48:35,840
finish studying
10113
05:48:35,840 --> 05:48:38,638
so this program took approximately five
10114
05:48:38,638 --> 05:48:39,840
seconds to finish
10115
05:48:39,840 --> 05:48:41,600
and the reason that this program took
10116
05:48:41,600 --> 05:48:43,200
five seconds instead of 12
10117
05:48:43,200 --> 05:48:45,440
is because before our main thread was in
10118
05:48:45,440 --> 05:48:46,558
charge of running
10119
05:48:46,558 --> 05:48:49,360
these tasks sequentially in order but
10120
05:48:49,360 --> 05:48:50,958
now since we have a thread
10121
05:48:50,958 --> 05:48:53,600
dedicated to each task we can run them
10122
05:48:53,600 --> 05:48:54,080
all
10123
05:48:54,080 --> 05:48:56,400
concurrently instead of sequentially so
10124
05:48:56,400 --> 05:48:57,520
that this program
10125
05:48:57,520 --> 05:48:59,920
now took about five seconds to complete
10126
05:48:59,920 --> 05:49:01,120
and you may have noticed too
10127
05:49:01,120 --> 05:49:03,200
that the activecount function as well as
10128
05:49:03,200 --> 05:49:04,480
the enumerate function
10129
05:49:04,480 --> 05:49:07,440
were called before threads 1 2 and 3
10130
05:49:07,440 --> 05:49:09,520
finished their respective tasks
10131
05:49:09,520 --> 05:49:11,360
that's because the main thread is not
10132
05:49:11,360 --> 05:49:12,638
going to wait around for
10133
05:49:12,638 --> 05:49:14,798
these three threads to complete it has
10134
05:49:14,798 --> 05:49:16,878
its own set of instructions to do
10135
05:49:16,878 --> 05:49:19,920
so it is no longer in charge of these
10136
05:49:19,920 --> 05:49:21,280
three functions
10137
05:49:21,280 --> 05:49:23,120
the program is going to handle those
10138
05:49:23,120 --> 05:49:25,280
three functions to our three threads
10139
05:49:25,280 --> 05:49:27,280
and our main thread is going to continue
10140
05:49:27,280 --> 05:49:28,878
its own set of instructions
10141
05:49:28,878 --> 05:49:31,760
its job is to print the active count as
10142
05:49:31,760 --> 05:49:32,638
well as
10143
05:49:32,638 --> 05:49:35,040
call the enumerate function which it did
10144
05:49:35,040 --> 05:49:36,638
and it finished its tasks
10145
05:49:36,638 --> 05:49:39,680
before threads one two and three now one
10146
05:49:39,680 --> 05:49:40,878
trick that you can do too
10147
05:49:40,878 --> 05:49:43,120
is that you can use the time modules
10148
05:49:43,120 --> 05:49:43,920
performance
10149
05:49:43,920 --> 05:49:46,400
counter function and this function will
10150
05:49:46,400 --> 05:49:47,120
return
10151
05:49:47,120 --> 05:49:49,120
how long it takes our calling thread as
10152
05:49:49,120 --> 05:49:50,480
in our main thread
10153
05:49:50,480 --> 05:49:53,600
to finish its set of instructions so our
10154
05:49:53,600 --> 05:49:55,920
main thread is not in charge of
10155
05:49:55,920 --> 05:49:56,638
executing
10156
05:49:56,638 --> 05:49:59,120
these three functions our main thread is
10157
05:49:59,120 --> 05:50:00,558
in charge of creating
10158
05:50:00,558 --> 05:50:02,718
three additional threads and then
10159
05:50:02,718 --> 05:50:04,798
calling the activecount function
10160
05:50:04,798 --> 05:50:06,958
and the enumerate function as well as
10161
05:50:06,958 --> 05:50:09,360
the performance counter function
10162
05:50:09,360 --> 05:50:11,280
so our main thread will take
10163
05:50:11,280 --> 05:50:13,240
approximately
10164
05:50:13,240 --> 05:50:15,600
0.075 seconds to complete
10165
05:50:15,600 --> 05:50:17,280
and our three threads are still running
10166
05:50:17,280 --> 05:50:19,680
in the background our main threads job
10167
05:50:19,680 --> 05:50:22,798
its order of instructions is to create
10168
05:50:22,798 --> 05:50:24,958
three additional threads and then
10169
05:50:24,958 --> 05:50:25,920
immediately
10170
05:50:25,920 --> 05:50:28,400
print whatever is returned via the
10171
05:50:28,400 --> 05:50:29,840
activecount function
10172
05:50:29,840 --> 05:50:31,840
enumerate and then your performance
10173
05:50:31,840 --> 05:50:33,120
counter so
10174
05:50:33,120 --> 05:50:35,520
our main thread says that it finished
10175
05:50:35,520 --> 05:50:36,920
its tasks in about
10176
05:50:36,920 --> 05:50:40,000
.07 seconds but threads one
10177
05:50:40,000 --> 05:50:42,160
through three were still trying to catch
10178
05:50:42,160 --> 05:50:44,080
up that's why the main thread
10179
05:50:44,080 --> 05:50:45,680
finished before are three additional
10180
05:50:45,680 --> 05:50:47,200
threads and once
10181
05:50:47,200 --> 05:50:49,440
all active threads have completed their
10182
05:50:49,440 --> 05:50:50,558
tasks
10183
05:50:50,558 --> 05:50:52,958
then your program will finish and exit
10184
05:50:52,958 --> 05:50:54,718
there's also this concept called
10185
05:50:54,718 --> 05:50:56,798
thread synchronization we can have a
10186
05:50:56,798 --> 05:50:58,638
calling thread in this case our main
10187
05:50:58,638 --> 05:50:59,280
thread
10188
05:50:59,280 --> 05:51:01,680
wait around for another thread to finish
10189
05:51:01,680 --> 05:51:02,878
before it can move on
10190
05:51:02,878 --> 05:51:05,280
with its own instruction set so let's
10191
05:51:05,280 --> 05:51:06,400
say we would like
10192
05:51:06,400 --> 05:51:08,798
our main thread to wait around for
10193
05:51:08,798 --> 05:51:10,798
thread 1 also known as
10194
05:51:10,798 --> 05:51:13,440
x so we're going to use the join
10195
05:51:13,440 --> 05:51:14,080
function
10196
05:51:14,080 --> 05:51:17,120
of thread x and now our main thread
10197
05:51:17,120 --> 05:51:19,600
has to wait around for thread x also
10198
05:51:19,600 --> 05:51:20,240
known as
10199
05:51:20,240 --> 05:51:22,480
thread 1 to finish before it can move on
10200
05:51:22,480 --> 05:51:24,160
with its instruction set
10201
05:51:24,160 --> 05:51:27,200
so let's do the same with y and
10202
05:51:27,200 --> 05:51:30,160
z so now our main thread before it can
10203
05:51:30,160 --> 05:51:32,080
move on with the rest of the program has
10204
05:51:32,080 --> 05:51:33,760
to wait for all of these threads
10205
05:51:33,760 --> 05:51:36,638
to synchronize and join and then and
10206
05:51:36,638 --> 05:51:37,200
only then
10207
05:51:37,200 --> 05:51:39,040
can it move on with the rest of its own
10208
05:51:39,040 --> 05:51:40,240
instruction set
10209
05:51:40,240 --> 05:51:42,080
so this time our program is going to
10210
05:51:42,080 --> 05:51:44,080
look a little something like this
10211
05:51:44,080 --> 05:51:46,480
you eat breakfast you drink coffee you
10212
05:51:46,480 --> 05:51:47,600
finish studying
10213
05:51:47,600 --> 05:51:50,000
so by the time we reach our active count
10214
05:51:50,000 --> 05:51:50,958
these threads
10215
05:51:50,958 --> 05:51:52,878
are already joined and synchronized
10216
05:51:52,878 --> 05:51:54,320
they're no longer active
10217
05:51:54,320 --> 05:51:56,320
when we enumerate over our active
10218
05:51:56,320 --> 05:51:57,680
threads we only have
10219
05:51:57,680 --> 05:51:59,840
our main thread and our main thread
10220
05:51:59,840 --> 05:52:00,718
completed in
10221
05:52:00,718 --> 05:52:03,600
about 5.1 seconds this time because it
10222
05:52:03,600 --> 05:52:04,958
did a lot of waiting around
10223
05:52:04,958 --> 05:52:08,000
it was waiting for threads x y and z to
10224
05:52:08,000 --> 05:52:10,878
join and finish in conclusion a thread
10225
05:52:10,878 --> 05:52:11,200
is
10226
05:52:11,200 --> 05:52:14,080
a flow of execution like a separate
10227
05:52:14,080 --> 05:52:16,320
order of instructions that a program can
10228
05:52:16,320 --> 05:52:16,958
follow
10229
05:52:16,958 --> 05:52:19,360
and when we run a program we always have
10230
05:52:19,360 --> 05:52:20,320
at least one
10231
05:52:20,320 --> 05:52:22,080
thread running initially and that is
10232
05:52:22,080 --> 05:52:24,240
referred to as the main thread
10233
05:52:24,240 --> 05:52:26,400
however if you have multiple threads
10234
05:52:26,400 --> 05:52:28,798
using the concept of multi-threading
10235
05:52:28,798 --> 05:52:30,718
we can have multiple threads running
10236
05:52:30,718 --> 05:52:33,200
concurrently but not truly in parallel
10237
05:52:33,200 --> 05:52:34,480
which is what we'll cover
10238
05:52:34,480 --> 05:52:36,958
during the video on multi-processing
10239
05:52:36,958 --> 05:52:37,680
this is due
10240
05:52:37,680 --> 05:52:40,718
to a feature known as the gil
10241
05:52:40,718 --> 05:52:43,520
the global interpreter lock which allows
10242
05:52:43,520 --> 05:52:43,920
only
10243
05:52:43,920 --> 05:52:46,240
one thread to hold control of the python
10244
05:52:46,240 --> 05:52:47,120
interpreter
10245
05:52:47,120 --> 05:52:50,240
at any one time and tasks can be either
10246
05:52:50,240 --> 05:52:53,680
cpu bound or io bound a cpu bound task
10247
05:52:53,680 --> 05:52:55,600
is a program or task
10248
05:52:55,600 --> 05:52:58,160
that spends most of its time waiting for
10249
05:52:58,160 --> 05:52:59,200
internal events
10250
05:52:59,200 --> 05:53:02,638
such as a task that is cpu intensive
10251
05:53:02,638 --> 05:53:05,200
and o bound tasks spend most of their
10252
05:53:05,200 --> 05:53:06,638
time waiting for
10253
05:53:06,638 --> 05:53:09,120
external events such as waiting around
10254
05:53:09,120 --> 05:53:10,400
for user input
10255
05:53:10,400 --> 05:53:13,360
or web scraping with i o bound tasks
10256
05:53:13,360 --> 05:53:15,440
it's better to use multi-threading
10257
05:53:15,440 --> 05:53:18,120
with cpu bound tasks it's better to use
10258
05:53:18,120 --> 05:53:19,360
multi-processing
10259
05:53:19,360 --> 05:53:21,680
well everybody that is a quick overview
10260
05:53:21,680 --> 05:53:23,920
of multi-threading i will post all of
10261
05:53:23,920 --> 05:53:25,520
this code to the comment section down
10262
05:53:25,520 --> 05:53:26,080
below
10263
05:53:26,080 --> 05:53:28,160
and well yeah that's how to achieve
10264
05:53:28,160 --> 05:53:29,120
multi-threading
10265
05:53:29,120 --> 05:53:32,638
using python hey what's going on
10266
05:53:32,638 --> 05:53:33,280
everybody
10267
05:53:33,280 --> 05:53:35,200
it's you bro hope you're doing well and
10268
05:53:35,200 --> 05:53:36,958
in this video i'm going to explain demon
10269
05:53:36,958 --> 05:53:38,480
threads in python so
10270
05:53:38,480 --> 05:53:43,040
sit back relax and enjoy the show
10271
05:53:43,040 --> 05:53:46,638
okay so demon threads also pronounced as
10272
05:53:46,638 --> 05:53:49,200
daemon threads a demon thread is a
10273
05:53:49,200 --> 05:53:51,200
thread that runs in the background and
10274
05:53:51,200 --> 05:53:52,958
they are normally not important for your
10275
05:53:52,958 --> 05:53:54,558
program in order to run
10276
05:53:54,558 --> 05:53:56,480
your program will not wait for demon
10277
05:53:56,480 --> 05:53:57,920
threads to complete before
10278
05:53:57,920 --> 05:54:00,878
exiting whereas non-demon threads they
10279
05:54:00,878 --> 05:54:02,558
cannot normally be killed
10280
05:54:02,558 --> 05:54:04,718
they will stay alive until their task is
10281
05:54:04,718 --> 05:54:05,760
complete so
10282
05:54:05,760 --> 05:54:08,320
a few common uses of demon threads would
10283
05:54:08,320 --> 05:54:08,958
be
10284
05:54:08,958 --> 05:54:11,520
background tasks garbage collection
10285
05:54:11,520 --> 05:54:12,718
waiting for input
10286
05:54:12,718 --> 05:54:15,200
or other long-running processes so
10287
05:54:15,200 --> 05:54:17,200
here's an example of why demon threads
10288
05:54:17,200 --> 05:54:18,160
would be useful
10289
05:54:18,160 --> 05:54:20,240
let's say that we'll have two threads
10290
05:54:20,240 --> 05:54:22,080
our main thread will be in charge of
10291
05:54:22,080 --> 05:54:24,160
waiting around for some user input we'll
10292
05:54:24,160 --> 05:54:24,638
ask
10293
05:54:24,638 --> 05:54:26,558
do you wish to exit and in the
10294
05:54:26,558 --> 05:54:28,638
background we'll have a timer going that
10295
05:54:28,638 --> 05:54:29,440
will display
10296
05:54:29,440 --> 05:54:32,160
how long somebody is logged in so let's
10297
05:54:32,160 --> 05:54:32,958
create
10298
05:54:32,958 --> 05:54:36,240
a function named timer
10299
05:54:36,240 --> 05:54:38,638
and what we'll do is just display how
10300
05:54:38,638 --> 05:54:40,000
long somebody's logged in
10301
05:54:40,000 --> 05:54:43,040
so let's say we have a count variable
10302
05:54:43,040 --> 05:54:46,400
while true we'll use
10303
05:54:46,400 --> 05:54:49,280
the sleep function of the time module to
10304
05:54:49,280 --> 05:54:51,520
sleep for approximately one second
10305
05:54:51,520 --> 05:54:53,520
then we'll increment our count variable
10306
05:54:53,520 --> 05:54:56,320
by one and we'll print a message
10307
05:54:56,320 --> 05:54:59,360
so let's say logged in
10308
05:54:59,360 --> 05:55:01,600
for
10309
05:55:03,440 --> 05:55:05,760
count
10310
05:55:06,638 --> 05:55:09,600
seconds and let's create a thread that
10311
05:55:09,600 --> 05:55:11,680
will be in charge of this timer and run
10312
05:55:11,680 --> 05:55:13,280
it in the background so let's say
10313
05:55:13,280 --> 05:55:16,320
x equals threading
10314
05:55:16,320 --> 05:55:19,360
dot thread the target will be our
10315
05:55:19,360 --> 05:55:19,920
function
10316
05:55:19,920 --> 05:55:22,240
of timer and we need to start this
10317
05:55:22,240 --> 05:55:23,120
thread
10318
05:55:23,120 --> 05:55:26,160
x dot start so our thread
10319
05:55:26,160 --> 05:55:29,040
x will be in charge of our timer and run
10320
05:55:29,040 --> 05:55:30,160
it concurrently
10321
05:55:30,160 --> 05:55:32,080
while we're waiting for some user input
10322
05:55:32,080 --> 05:55:34,000
our main thread will be in charge of
10323
05:55:34,000 --> 05:55:36,160
waiting for this user input do you wish
10324
05:55:36,160 --> 05:55:38,000
to exit now here's the issue
10325
05:55:38,000 --> 05:55:40,718
of us using a non-demon thread for a
10326
05:55:40,718 --> 05:55:42,878
background task such as a countdown
10327
05:55:42,878 --> 05:55:43,600
timer
10328
05:55:43,600 --> 05:55:45,120
so our main thread is in charge of
10329
05:55:45,120 --> 05:55:46,958
waiting around for some user input
10330
05:55:46,958 --> 05:55:49,680
and thread x our additional thread is in
10331
05:55:49,680 --> 05:55:50,320
charge of
10332
05:55:50,320 --> 05:55:52,400
running this countdown timer so if i
10333
05:55:52,400 --> 05:55:54,160
wish to exit this program i'm just going
10334
05:55:54,160 --> 05:55:55,040
to type
10335
05:55:55,040 --> 05:55:57,840
okay and hit enter well my background
10336
05:55:57,840 --> 05:56:00,080
timer is still going but my main thread
10337
05:56:00,080 --> 05:56:02,400
is complete so i would like to exit this
10338
05:56:02,400 --> 05:56:03,840
program but
10339
05:56:03,840 --> 05:56:06,480
my program will not exit as long as
10340
05:56:06,480 --> 05:56:08,638
there are non-demon threads that are
10341
05:56:08,638 --> 05:56:09,520
still alive
10342
05:56:09,520 --> 05:56:11,440
we cannot normally close this program
10343
05:56:11,440 --> 05:56:13,360
unless we do like a brute force
10344
05:56:13,360 --> 05:56:15,520
you know close and exit of the program
10345
05:56:15,520 --> 05:56:17,040
so we can turn
10346
05:56:17,040 --> 05:56:20,400
this thread of x into a demon thread
10347
05:56:20,400 --> 05:56:22,480
so that when we wish to exit the program
10348
05:56:22,480 --> 05:56:24,000
when there are no more
10349
05:56:24,000 --> 05:56:26,878
non-demon threads alive and to change a
10350
05:56:26,878 --> 05:56:28,638
thread to a demon thread it's actually
10351
05:56:28,638 --> 05:56:30,400
fairly easy when you create your thread
10352
05:56:30,400 --> 05:56:31,040
you can set
10353
05:56:31,040 --> 05:56:33,840
this flag of demon equal to true and
10354
05:56:33,840 --> 05:56:34,638
that's it
10355
05:56:34,638 --> 05:56:37,040
so when all non-demon threads have
10356
05:56:37,040 --> 05:56:38,480
finished their tasks
10357
05:56:38,480 --> 05:56:41,360
your program will complete and exit but
10358
05:56:41,360 --> 05:56:43,280
if there are demon tasks running in the
10359
05:56:43,280 --> 05:56:43,920
background
10360
05:56:43,920 --> 05:56:45,840
they will be killed automatically so
10361
05:56:45,840 --> 05:56:47,440
let's try this program again
10362
05:56:47,440 --> 05:56:49,600
so do you wish to exit my program is
10363
05:56:49,600 --> 05:56:50,638
going to continue running
10364
05:56:50,638 --> 05:56:52,080
my demon thread is in charge of the
10365
05:56:52,080 --> 05:56:53,920
timer and it's going to continue running
10366
05:56:53,920 --> 05:56:54,638
until
10367
05:56:54,638 --> 05:56:57,200
all non-daemon threads have completed
10368
05:56:57,200 --> 05:56:58,320
their tasks
10369
05:56:58,320 --> 05:57:02,000
so once i type ok my program will
10370
05:57:02,000 --> 05:57:04,080
finish running and my countdown timer
10371
05:57:04,080 --> 05:57:05,280
will stop because
10372
05:57:05,280 --> 05:57:07,840
all demons are killed when your program
10373
05:57:07,840 --> 05:57:09,520
is finished running there's also two
10374
05:57:09,520 --> 05:57:11,040
additional methods that you might be
10375
05:57:11,040 --> 05:57:11,920
interested in
10376
05:57:11,920 --> 05:57:14,958
you can use the set daemon method
10377
05:57:14,958 --> 05:57:16,958
of a thread and you can set it to false
10378
05:57:16,958 --> 05:57:18,798
or true if you would like to change
10379
05:57:18,798 --> 05:57:21,920
a thread to a non-demon or a demon
10380
05:57:21,920 --> 05:57:23,600
however if your thread is currently
10381
05:57:23,600 --> 05:57:25,760
running well you cannot actually change
10382
05:57:25,760 --> 05:57:26,638
it from
10383
05:57:26,638 --> 05:57:28,798
a non-demon thread to a demon thread
10384
05:57:28,798 --> 05:57:30,240
while it's currently running
10385
05:57:30,240 --> 05:57:31,680
so you would have to do that before you
10386
05:57:31,680 --> 05:57:34,240
actually use the start function
10387
05:57:34,240 --> 05:57:36,080
and you can also check to see if a
10388
05:57:36,080 --> 05:57:37,360
thread is a demon or not
10389
05:57:37,360 --> 05:57:40,480
by using the is demon method this will
10390
05:57:40,480 --> 05:57:41,120
return
10391
05:57:41,120 --> 05:57:44,400
true or false so everybody in conclusion
10392
05:57:44,400 --> 05:57:46,878
a demon thread is a thread that runs in
10393
05:57:46,878 --> 05:57:47,600
the background
10394
05:57:47,600 --> 05:57:49,040
and they're not important for your
10395
05:57:49,040 --> 05:57:51,200
program in order to continue running
10396
05:57:51,200 --> 05:57:53,280
your program will not wait for demon
10397
05:57:53,280 --> 05:57:55,200
threads to complete before exiting the
10398
05:57:55,200 --> 05:57:56,000
program
10399
05:57:56,000 --> 05:57:58,480
whereas non-demon threads cannot
10400
05:57:58,480 --> 05:57:59,600
normally be killed
10401
05:57:59,600 --> 05:58:01,760
they will stay alive until their task is
10402
05:58:01,760 --> 05:58:04,160
complete and demon threads are commonly
10403
05:58:04,160 --> 05:58:04,878
used for
10404
05:58:04,878 --> 05:58:07,120
background tasks garbage collection
10405
05:58:07,120 --> 05:58:08,558
waiting around for input
10406
05:58:08,558 --> 05:58:11,520
or other long running processes so if
10407
05:58:11,520 --> 05:58:13,120
you would like a copy of this code i
10408
05:58:13,120 --> 05:58:14,638
will post all of this to the comment
10409
05:58:14,638 --> 05:58:15,840
section down below
10410
05:58:15,840 --> 05:58:18,080
but yeah those are demon threads in
10411
05:58:18,080 --> 05:58:19,760
python
10412
05:58:19,760 --> 05:58:22,240
yo what's going on everybody it's bro
10413
05:58:22,240 --> 05:58:23,200
hope you're doing well
10414
05:58:23,200 --> 05:58:24,798
and in this video i'm going to explain
10415
05:58:24,798 --> 05:58:26,798
multi-processing in python so
10416
05:58:26,798 --> 05:58:31,600
sit back relax and enjoy the show
10417
05:58:31,760 --> 05:58:33,280
what's up let's talk about
10418
05:58:33,280 --> 05:58:35,600
multi-processing multi-processing
10419
05:58:35,600 --> 05:58:38,958
is the act of running tasks in parallel
10420
05:58:38,958 --> 05:58:41,520
on a different cpu cores it's different
10421
05:58:41,520 --> 05:58:42,638
from multi-threading
10422
05:58:42,638 --> 05:58:44,638
although it sounds similar because with
10423
05:58:44,638 --> 05:58:45,680
multi-threading
10424
05:58:45,680 --> 05:58:48,320
we're limited to running one thread at a
10425
05:58:48,320 --> 05:58:48,798
time
10426
05:58:48,798 --> 05:58:51,920
because of the gil that lock which is
10427
05:58:51,920 --> 05:58:53,840
used for threading we can run threads
10428
05:58:53,840 --> 05:58:54,798
concurrently
10429
05:58:54,798 --> 05:58:57,000
but not in parallel however with
10430
05:58:57,000 --> 05:58:58,240
multi-processing
10431
05:58:58,240 --> 05:59:01,120
we can create processes and we can run
10432
05:59:01,120 --> 05:59:02,080
each process
10433
05:59:02,080 --> 05:59:05,200
in parallel on a different cpu course so
10434
05:59:05,200 --> 05:59:06,718
with multi-processing
10435
05:59:06,718 --> 05:59:09,920
it's better for tasks that are cpu bound
10436
05:59:09,920 --> 05:59:13,200
where you need heavy cpu usage whereas
10437
05:59:13,200 --> 05:59:15,760
multi-threading that is better for i o
10438
05:59:15,760 --> 05:59:17,200
bound tasks where you do a lot of
10439
05:59:17,200 --> 05:59:18,160
waiting around
10440
05:59:18,160 --> 05:59:20,638
so before we begin i recommend these two
10441
05:59:20,638 --> 05:59:21,520
imports
10442
05:59:21,520 --> 05:59:24,320
multi-processing and time let's begin
10443
05:59:24,320 --> 05:59:26,160
quick note if you're running the windows
10444
05:59:26,160 --> 05:59:28,160
operating system you'll probably need to
10445
05:59:28,160 --> 05:59:28,480
add
10446
05:59:28,480 --> 05:59:31,600
this line if name is equal to
10447
05:59:31,600 --> 05:59:34,798
main so when we run a program we have a
10448
05:59:34,798 --> 05:59:37,040
main process that is running and if we
10449
05:59:37,040 --> 05:59:38,718
create a child process
10450
05:59:38,718 --> 05:59:41,200
from that process it's going to copy the
10451
05:59:41,200 --> 05:59:43,280
module that we're currently working with
10452
05:59:43,280 --> 05:59:46,160
and that child process will create its
10453
05:59:46,160 --> 05:59:48,160
own children processes and it's going to
10454
05:59:48,160 --> 05:59:49,360
be a problem
10455
05:59:49,360 --> 05:59:51,760
so we're going to add this line if name
10456
05:59:51,760 --> 05:59:52,878
is equal to main
10457
05:59:52,878 --> 05:59:54,798
so when we create a child process it
10458
05:59:54,798 --> 05:59:56,878
will copy our module but it's not going
10459
05:59:56,878 --> 05:59:58,080
to execute it
10460
05:59:58,080 --> 06:00:00,718
so let's create a main function and a
10461
06:00:00,718 --> 06:00:02,480
majority of our code is going to be
10462
06:00:02,480 --> 06:00:04,160
within our main function
10463
06:00:04,160 --> 06:00:05,440
if you're running a different operating
10464
06:00:05,440 --> 06:00:07,040
system you probably don't have to do
10465
06:00:07,040 --> 06:00:08,638
this but if you're running windows
10466
06:00:08,638 --> 06:00:10,480
you probably will now with
10467
06:00:10,480 --> 06:00:12,718
multi-processing multi-processing is
10468
06:00:12,718 --> 06:00:14,718
better for tasks or functions
10469
06:00:14,718 --> 06:00:17,200
that are cpu bound where they require
10470
06:00:17,200 --> 06:00:19,120
heavy cpu usage
10471
06:00:19,120 --> 06:00:21,120
let's say that we have a function named
10472
06:00:21,120 --> 06:00:22,240
counter
10473
06:00:22,240 --> 06:00:24,480
and we'll pass in a number to count up
10474
06:00:24,480 --> 06:00:26,638
to but the number we're going to pass in
10475
06:00:26,638 --> 06:00:28,878
is a ridiculously large number like a
10476
06:00:28,878 --> 06:00:30,958
billion so let's create a function that
10477
06:00:30,958 --> 06:00:31,520
will count
10478
06:00:31,520 --> 06:00:35,040
from zero so count equals zero and
10479
06:00:35,040 --> 06:00:38,080
while count is less than
10480
06:00:38,080 --> 06:00:40,240
our number that we pass in we will
10481
06:00:40,240 --> 06:00:42,240
increment our counter
10482
06:00:42,240 --> 06:00:45,200
by one so that is the function that
10483
06:00:45,200 --> 06:00:46,000
we'll call with
10484
06:00:46,000 --> 06:00:48,798
our processes that we create now within
10485
06:00:48,798 --> 06:00:49,760
our main function
10486
06:00:49,760 --> 06:00:52,000
we'll create a process and to do that
10487
06:00:52,000 --> 06:00:54,638
you'll need the multi-processing module
10488
06:00:54,638 --> 06:00:57,600
so import process and cpu account and
10489
06:00:57,600 --> 06:00:59,280
we'll save this for a little bit later
10490
06:00:59,280 --> 06:01:01,200
to create a process let's say we have
10491
06:01:01,200 --> 06:01:02,558
process a
10492
06:01:02,558 --> 06:01:06,320
a equals process this step is very
10493
06:01:06,320 --> 06:01:08,400
similar to creating a thread
10494
06:01:08,400 --> 06:01:10,798
we have a target our target will be our
10495
06:01:10,798 --> 06:01:13,760
function of counter
10496
06:01:13,760 --> 06:01:16,160
and if we have arguments we will pass
10497
06:01:16,160 --> 06:01:16,878
those in
10498
06:01:16,878 --> 06:01:18,958
so remember with our arguments we have
10499
06:01:18,958 --> 06:01:20,718
to pass in a tuple
10500
06:01:20,718 --> 06:01:23,120
since we only have one argument to pass
10501
06:01:23,120 --> 06:01:23,840
in
10502
06:01:23,840 --> 06:01:25,920
to differentiate this from an expression
10503
06:01:25,920 --> 06:01:27,760
we have to add a comma at the end
10504
06:01:27,760 --> 06:01:31,520
so our number let's say is 1 billion
10505
06:01:31,520 --> 06:01:35,200
so that's a million 10 million 100
10506
06:01:35,200 --> 06:01:35,680
million
10507
06:01:35,680 --> 06:01:38,558
1 billion let me just verify that okay
10508
06:01:38,558 --> 06:01:39,840
so that's 1 billion
10509
06:01:39,840 --> 06:01:42,080
we're going to count from zero to a
10510
06:01:42,080 --> 06:01:42,878
billion
10511
06:01:42,878 --> 06:01:46,558
and in order to start this process a
10512
06:01:46,558 --> 06:01:50,320
my process use the start function
10513
06:01:50,320 --> 06:01:52,878
and then i will use some process
10514
06:01:52,878 --> 06:01:54,480
synchronization by using the
10515
06:01:54,480 --> 06:01:57,920
join function my main process is going
10516
06:01:57,920 --> 06:01:58,638
to wait around
10517
06:01:58,638 --> 06:02:01,360
for my child process of a to finish
10518
06:02:01,360 --> 06:02:03,040
before continuing
10519
06:02:03,040 --> 06:02:06,400
and let's print our performance counter
10520
06:02:06,400 --> 06:02:12,000
so we'll print finished in
10521
06:02:12,000 --> 06:02:17,520
let's say time dot performance counter
10522
06:02:19,760 --> 06:02:23,120
seconds okay so let's see how long it'll
10523
06:02:23,120 --> 06:02:25,520
take to count from zero to a billion
10524
06:02:25,520 --> 06:02:26,080
using
10525
06:02:26,080 --> 06:02:28,558
one process i fast forwarded this video
10526
06:02:28,558 --> 06:02:30,120
but it took my program
10527
06:02:30,120 --> 06:02:33,440
56.7 seconds to count from zero to a
10528
06:02:33,440 --> 06:02:34,080
billion
10529
06:02:34,080 --> 06:02:35,878
but we can speed this up by using
10530
06:02:35,878 --> 06:02:38,240
multi-processing let's create a second
10531
06:02:38,240 --> 06:02:40,878
process named b
10532
06:02:40,878 --> 06:02:42,798
and i'm going to divide the amount of
10533
06:02:42,798 --> 06:02:44,160
work in half so
10534
06:02:44,160 --> 06:02:47,200
each process will count to 500 million
10535
06:02:47,200 --> 06:02:52,000
half of a billion so let's change a to b
10536
06:02:52,000 --> 06:02:53,600
and i'm gonna group these start
10537
06:02:53,600 --> 06:02:56,480
functions together
10538
06:02:57,600 --> 06:02:59,600
just so it's easier to read and then b
10539
06:02:59,600 --> 06:03:02,718
dot join just to synchronize everything
10540
06:03:02,718 --> 06:03:06,080
okay and then let's change 1 billion to
10541
06:03:06,080 --> 06:03:09,360
500 million for each it's the same
10542
06:03:09,360 --> 06:03:12,000
amount of work but divided among workers
10543
06:03:12,000 --> 06:03:14,718
so that is 500 million let's see how
10544
06:03:14,718 --> 06:03:16,400
well it runs this time
10545
06:03:16,400 --> 06:03:19,520
so it took my program about 40.3 seconds
10546
06:03:19,520 --> 06:03:20,080
to finish
10547
06:03:20,080 --> 06:03:22,558
counting from zero to a billion but i
10548
06:03:22,558 --> 06:03:24,080
divided that task among
10549
06:03:24,080 --> 06:03:26,240
different processes each of my two
10550
06:03:26,240 --> 06:03:27,520
processes counted from
10551
06:03:27,520 --> 06:03:30,558
zero to 500 million this time so this
10552
06:03:30,558 --> 06:03:31,600
time let's create
10553
06:03:31,600 --> 06:03:35,840
four processes so we have a
10554
06:03:35,840 --> 06:03:39,520
b c and d
10555
06:03:39,520 --> 06:03:42,958
and we'll need to start them
10556
06:03:44,400 --> 06:03:48,480
and i'm going to join them now this
10557
06:03:48,480 --> 06:03:50,320
might not actually speed it up for you
10558
06:03:50,320 --> 06:03:52,080
and i'll explain why later
10559
06:03:52,080 --> 06:03:54,240
it depends on the amount of cpu cores
10560
06:03:54,240 --> 06:03:55,520
that you have
10561
06:03:55,520 --> 06:03:59,520
okay so oh let's change this to so 250
10562
06:03:59,520 --> 06:04:02,638
million for each
10563
06:04:02,638 --> 06:04:06,000
okay see you in a couple seconds
10564
06:04:06,000 --> 06:04:08,878
okay welcome back so on my computer when
10565
06:04:08,878 --> 06:04:09,440
i ran
10566
06:04:09,440 --> 06:04:11,760
four processes and each was counting up
10567
06:04:11,760 --> 06:04:14,000
to 250 million
10568
06:04:14,000 --> 06:04:17,280
i could finish my program in 27.3
10569
06:04:17,280 --> 06:04:20,480
seconds now for me if i created
10570
06:04:20,480 --> 06:04:22,558
more processes than this it would
10571
06:04:22,558 --> 06:04:24,638
probably take longer and here's why
10572
06:04:24,638 --> 06:04:27,280
so you can print the cpu count of your
10573
06:04:27,280 --> 06:04:27,920
computer
10574
06:04:27,920 --> 06:04:31,200
using this function of cpu count so if i
10575
06:04:31,200 --> 06:04:31,760
were to
10576
06:04:31,760 --> 06:04:34,080
print whatever this function returns i
10577
06:04:34,080 --> 06:04:35,280
can get the count
10578
06:04:35,280 --> 06:04:37,440
of the number of additional processes
10579
06:04:37,440 --> 06:04:38,638
that i can run
10580
06:04:38,638 --> 06:04:40,080
for the time being i'm just going to
10581
06:04:40,080 --> 06:04:42,718
comment out this start function because
10582
06:04:42,718 --> 06:04:44,638
i just want to print whatever this value
10583
06:04:44,638 --> 06:04:47,840
is so my cpu count on my computer
10584
06:04:47,840 --> 06:04:51,280
is four so i can run four additional
10585
06:04:51,280 --> 06:04:52,160
processes
10586
06:04:52,160 --> 06:04:55,280
but let's attempt to run eight processes
10587
06:04:55,280 --> 06:04:57,840
so i'm going to copy all these and let's
10588
06:04:57,840 --> 06:04:59,360
say that we have
10589
06:04:59,360 --> 06:05:02,958
e f g
10590
06:05:02,958 --> 06:05:06,400
and h and we will start all of those two
10591
06:05:06,400 --> 06:05:10,558
so give me a second just to start them
10592
06:05:10,718 --> 06:05:14,558
so a b c d e
10593
06:05:14,558 --> 06:05:18,080
f g
10594
06:05:18,080 --> 06:05:22,878
h and then i'm going to join all of them
10595
06:05:23,920 --> 06:05:28,798
e f g
10596
06:05:28,798 --> 06:05:31,840
and h okay so
10597
06:05:31,840 --> 06:05:34,240
let's run this now oh and change the
10598
06:05:34,240 --> 06:05:35,920
arguments to i keep forgetting to do
10599
06:05:35,920 --> 06:05:36,400
that
10600
06:05:36,400 --> 06:05:40,480
so let's say 125 for each
10601
06:05:40,480 --> 06:05:43,680
so 125 million times eight is
10602
06:05:43,680 --> 06:05:47,360
one billion okay see you in a bit
10603
06:05:47,360 --> 06:05:49,520
all right welcome back this time when i
10604
06:05:49,520 --> 06:05:51,680
had eight processes working it actually
10605
06:05:51,680 --> 06:05:52,160
took me
10606
06:05:52,160 --> 06:05:54,240
longer than when i had four processes
10607
06:05:54,240 --> 06:05:56,240
working when i had four processes
10608
06:05:56,240 --> 06:05:58,798
working it took me about 27 seconds
10609
06:05:58,798 --> 06:06:01,120
this time it took 30 seconds when i had
10610
06:06:01,120 --> 06:06:02,638
eight processes working
10611
06:06:02,638 --> 06:06:05,040
that's because i had more processes than
10612
06:06:05,040 --> 06:06:06,400
my cpu count
10613
06:06:06,400 --> 06:06:08,320
and that's partially because whenever
10614
06:06:08,320 --> 06:06:09,680
you create a process
10615
06:06:09,680 --> 06:06:11,920
there's significant overhead with
10616
06:06:11,920 --> 06:06:12,798
beginning and
10617
06:06:12,798 --> 06:06:15,200
destroying a process and if i can only
10618
06:06:15,200 --> 06:06:16,000
run four
10619
06:06:16,000 --> 06:06:18,638
processes on my computer at one time
10620
06:06:18,638 --> 06:06:20,400
well then i'm creating additional
10621
06:06:20,400 --> 06:06:21,120
processes
10622
06:06:21,120 --> 06:06:23,520
to no extra benefit it's actually
10623
06:06:23,520 --> 06:06:24,798
hindering the performance of the
10624
06:06:24,798 --> 06:06:25,520
computer
10625
06:06:25,520 --> 06:06:27,120
because i'm creating all of these
10626
06:06:27,120 --> 06:06:28,958
additional processes when it really
10627
06:06:28,958 --> 06:06:30,160
doesn't help me
10628
06:06:30,160 --> 06:06:32,638
in conclusion multi-processing is the
10629
06:06:32,638 --> 06:06:33,280
concept
10630
06:06:33,280 --> 06:06:35,600
of running tasks in parallel on
10631
06:06:35,600 --> 06:06:37,200
different cpu cores
10632
06:06:37,200 --> 06:06:38,798
it's similar but different from
10633
06:06:38,798 --> 06:06:40,240
multi-threading because with
10634
06:06:40,240 --> 06:06:41,280
multi-threading
10635
06:06:41,280 --> 06:06:43,680
we can run tasks concurrently but
10636
06:06:43,680 --> 06:06:45,760
they're all taking turns because of the
10637
06:06:45,760 --> 06:06:46,718
gil
10638
06:06:46,718 --> 06:06:49,120
with multi-processing we can run all of
10639
06:06:49,120 --> 06:06:50,240
these different tasks
10640
06:06:50,240 --> 06:06:53,200
together in parallel multi-processing is
10641
06:06:53,200 --> 06:06:53,920
better for
10642
06:06:53,920 --> 06:06:56,958
cpu bound tasks where a task has
10643
06:06:56,958 --> 06:07:00,080
heavy cpu usage and multi-threading
10644
06:07:00,080 --> 06:07:02,798
is better for i o bound tasks tasks that
10645
06:07:02,798 --> 06:07:04,480
involve a lot of waiting around
10646
06:07:04,480 --> 06:07:06,558
so that's multi-processing if you would
10647
06:07:06,558 --> 06:07:08,638
like a copy of all this code i will post
10648
06:07:08,638 --> 06:07:10,240
all of this to the comment section down
10649
06:07:10,240 --> 06:07:10,798
below
10650
06:07:10,798 --> 06:07:12,958
but yeah that is how multi-processing
10651
06:07:12,958 --> 06:07:15,040
works in python
10652
06:07:15,040 --> 06:07:17,040
hey what's going on everybody two bro
10653
06:07:17,040 --> 06:07:18,558
here hope you're doing well and in this
10654
06:07:18,558 --> 06:07:19,920
video we're going to be creating our
10655
06:07:19,920 --> 06:07:20,638
very first
10656
06:07:20,638 --> 06:07:22,958
graphical user interface in python so
10657
06:07:22,958 --> 06:07:26,718
sit back relax and enjoy the show
10658
06:07:27,040 --> 06:07:28,638
welcome one and all today we'll be
10659
06:07:28,638 --> 06:07:30,160
creating our very first
10660
06:07:30,160 --> 06:07:32,638
graphical user interface in python also
10661
06:07:32,638 --> 06:07:33,520
known as a
10662
06:07:33,520 --> 06:07:35,600
gui for short we're going to be using
10663
06:07:35,600 --> 06:07:38,400
the tk inter gui which is a module that
10664
06:07:38,400 --> 06:07:40,080
is included with python
10665
06:07:40,080 --> 06:07:42,400
our first step in order to use tk enter
10666
06:07:42,400 --> 06:07:44,400
is to import this module so that we can
10667
06:07:44,400 --> 06:07:45,520
begin using it
10668
06:07:45,520 --> 06:07:48,878
our first line of code will be from tk
10669
06:07:48,878 --> 06:07:49,760
enter
10670
06:07:49,760 --> 06:07:52,320
import asterisk this will import
10671
06:07:52,320 --> 06:07:54,480
everything related to the tk inter
10672
06:07:54,480 --> 06:07:55,440
module
10673
06:07:55,440 --> 06:07:57,360
and with this import we can now use all
10674
06:07:57,360 --> 06:07:59,520
of the gui features that this module has
10675
06:07:59,520 --> 06:08:00,400
to offer
10676
06:08:00,400 --> 06:08:02,160
now there is one important distinction
10677
06:08:02,160 --> 06:08:04,080
that we need to make we need to discuss
10678
06:08:04,080 --> 06:08:05,440
the differences between
10679
06:08:05,440 --> 06:08:09,200
windows and widgets here's a few quick
10680
06:08:09,200 --> 06:08:11,040
definitions that i definitely did not
10681
06:08:11,040 --> 06:08:12,480
copy from the internet
10682
06:08:12,480 --> 06:08:15,680
widgets are gui elements such as buttons
10683
06:08:15,680 --> 06:08:18,878
text boxes labels images you name it
10684
06:08:18,878 --> 06:08:21,920
and windows they serve as a container
10685
06:08:21,920 --> 06:08:24,878
to hold or contain these widgets for
10686
06:08:24,878 --> 06:08:26,558
this topic we'll be creating and
10687
06:08:26,558 --> 06:08:28,480
customizing our own window
10688
06:08:28,480 --> 06:08:30,958
and in future topics we'll be exploring
10689
06:08:30,958 --> 06:08:32,638
different widgets that are available to
10690
06:08:32,638 --> 06:08:33,120
us
10691
06:08:33,120 --> 06:08:35,280
so let's begin by creating a simple
10692
06:08:35,280 --> 06:08:37,520
window and we should give our window a
10693
06:08:37,520 --> 06:08:38,480
unique name
10694
06:08:38,480 --> 06:08:40,958
such as window that's pretty creative
10695
06:08:40,958 --> 06:08:42,958
and in order to instantiate this window
10696
06:08:42,958 --> 06:08:43,920
we're going to follow
10697
06:08:43,920 --> 06:08:46,958
this window name with equals tk make
10698
06:08:46,958 --> 06:08:48,480
sure you get the capitalization right
10699
06:08:48,480 --> 06:08:49,120
this is a
10700
06:08:49,120 --> 06:08:51,680
uppercase t and a set of parentheses to
10701
06:08:51,680 --> 06:08:53,360
serve as the constructor
10702
06:08:53,360 --> 06:08:54,718
what this will do and i'm just going to
10703
06:08:54,718 --> 06:08:57,920
add a comment is instantiate
10704
06:08:57,920 --> 06:09:02,080
an instance of a window for us
10705
06:09:02,080 --> 06:09:04,320
and when we compile and run this nothing
10706
06:09:04,320 --> 06:09:05,360
appears to happen
10707
06:09:05,360 --> 06:09:07,600
that's because we only instantiated our
10708
06:09:07,600 --> 06:09:09,840
window and we're not actually displaying
10709
06:09:09,840 --> 06:09:10,160
it
10710
06:09:10,160 --> 06:09:11,840
in order to display our window we're
10711
06:09:11,840 --> 06:09:13,200
going to follow this with
10712
06:09:13,200 --> 06:09:16,638
the name of the window dot main loop
10713
06:09:16,638 --> 06:09:19,280
and this will display our window and
10714
06:09:19,280 --> 06:09:20,320
this is our first
10715
06:09:20,320 --> 06:09:22,400
graphical user interface and we'll be
10716
06:09:22,400 --> 06:09:23,680
adding widgets
10717
06:09:23,680 --> 06:09:25,760
to this window so i'm going to add a
10718
06:09:25,760 --> 06:09:27,040
comment that
10719
06:09:27,040 --> 06:09:30,718
this will place window on
10720
06:09:30,718 --> 06:09:34,320
computer screen and it will also listen
10721
06:09:34,320 --> 06:09:36,638
for events but we'll get to that in
10722
06:09:36,638 --> 06:09:37,600
future videos
10723
06:09:37,600 --> 06:09:39,600
now what we'll be doing is customizing
10724
06:09:39,600 --> 06:09:41,120
the appearance of this window
10725
06:09:41,120 --> 06:09:43,520
let's begin by changing the size and we
10726
06:09:43,520 --> 06:09:46,000
can do that with the geometry function
10727
06:09:46,000 --> 06:09:48,958
so we're going to first begin by typing
10728
06:09:48,958 --> 06:09:51,280
the name of our window which is window
10729
06:09:51,280 --> 06:09:53,440
follow this by using the geometry
10730
06:09:53,440 --> 06:09:55,840
function and we pass in a width and a
10731
06:09:55,840 --> 06:09:56,320
height
10732
06:09:56,320 --> 06:09:58,400
but make sure this is within quotes and
10733
06:09:58,400 --> 06:09:59,840
set this to whatever width and height
10734
06:09:59,840 --> 06:10:00,558
that you want
10735
06:10:00,558 --> 06:10:03,440
so if i want 420 by 420 that would be
10736
06:10:03,440 --> 06:10:04,558
420
10737
06:10:04,558 --> 06:10:07,680
x 420 and the size of our window
10738
06:10:07,680 --> 06:10:09,680
is now the size that we set with the
10739
06:10:09,680 --> 06:10:11,280
geometry function
10740
06:10:11,280 --> 06:10:13,200
now take a look at the top left of this
10741
06:10:13,200 --> 06:10:14,718
window we have
10742
06:10:14,718 --> 06:10:17,840
this feather icon as well as a title of
10743
06:10:17,840 --> 06:10:18,638
tk
10744
06:10:18,638 --> 06:10:21,040
that's kind of lame so let's change that
10745
06:10:21,040 --> 06:10:21,840
let's first
10746
06:10:21,840 --> 06:10:24,320
change the window title and there is a
10747
06:10:24,320 --> 06:10:25,760
function to do that
10748
06:10:25,760 --> 06:10:28,480
so we type in the name of our window
10749
06:10:28,480 --> 06:10:29,520
window
10750
06:10:29,520 --> 06:10:33,200
dot title function and we can pass in a
10751
06:10:33,200 --> 06:10:35,200
new title that we want to set
10752
06:10:35,200 --> 06:10:38,878
and i will set the title to be maybe bro
10753
06:10:38,878 --> 06:10:42,558
code first gui
10754
06:10:42,558 --> 06:10:46,558
program that'll work and we now changed
10755
06:10:46,558 --> 06:10:48,718
the title of this window and it says
10756
06:10:48,718 --> 06:10:51,840
bro code first gui program
10757
06:10:51,840 --> 06:10:53,840
the next thing that we can do although
10758
06:10:53,840 --> 06:10:54,958
not necessary
10759
06:10:54,958 --> 06:10:57,520
is that we can change the icon of our
10760
06:10:57,520 --> 06:10:58,878
window bar at the top
10761
06:10:58,878 --> 06:11:00,798
i would like to replace this feather
10762
06:11:00,798 --> 06:11:03,040
icon with an image of my choosing
10763
06:11:03,040 --> 06:11:05,200
here's my desktop don't mind all of the
10764
06:11:05,200 --> 06:11:07,280
clutter and i would like to use this
10765
06:11:07,280 --> 06:11:09,600
image of the logo for my channel and set
10766
06:11:09,600 --> 06:11:10,878
this as the icon
10767
06:11:10,878 --> 06:11:13,680
for my window so what i'm going to do is
10768
06:11:13,680 --> 06:11:15,840
copy
10769
06:11:15,840 --> 06:11:17,680
and then go to my project folder and
10770
06:11:17,680 --> 06:11:19,600
then paste it
10771
06:11:19,600 --> 06:11:22,878
click ok and i now have this image
10772
06:11:22,878 --> 06:11:24,798
within my project folder
10773
06:11:24,798 --> 06:11:27,040
but it's in a format that we cannot use
10774
06:11:27,040 --> 06:11:28,878
currently we need to convert this to
10775
06:11:28,878 --> 06:11:30,718
what's called a photo image
10776
06:11:30,718 --> 06:11:34,000
that is a format that tk enter can use
10777
06:11:34,000 --> 06:11:36,480
so we'll need to convert our image to a
10778
06:11:36,480 --> 06:11:37,600
photo image
10779
06:11:37,600 --> 06:11:39,760
so do that anywhere within the window
10780
06:11:39,760 --> 06:11:42,400
between window equals tk and window.main
10781
06:11:42,400 --> 06:11:42,878
loop
10782
06:11:42,878 --> 06:11:44,718
right here is good i suppose we're going
10783
06:11:44,718 --> 06:11:46,320
to create a photo image
10784
06:11:46,320 --> 06:11:48,718
from this image and this is a png file
10785
06:11:48,718 --> 06:11:50,638
but yours might be something different
10786
06:11:50,638 --> 06:11:53,200
so let's give our photo image a unique
10787
06:11:53,200 --> 06:11:54,878
name like maybe icon
10788
06:11:54,878 --> 06:11:56,400
and in order to create a photo image
10789
06:11:56,400 --> 06:11:58,798
we'll follow this with equals
10790
06:11:58,798 --> 06:12:01,760
photo image and within the constructor
10791
06:12:01,760 --> 06:12:02,798
of our photo image
10792
06:12:02,798 --> 06:12:05,200
there is an option for file we can
10793
06:12:05,200 --> 06:12:05,840
either list
10794
06:12:05,840 --> 06:12:08,000
the file name or the file path if it's
10795
06:12:08,000 --> 06:12:09,920
somewhere else on your computer
10796
06:12:09,920 --> 06:12:12,160
since this image is within my same
10797
06:12:12,160 --> 06:12:14,160
project folder i only have to list the
10798
06:12:14,160 --> 06:12:15,120
file name
10799
06:12:15,120 --> 06:12:18,320
and this is called logo.png
10800
06:12:18,320 --> 06:12:19,920
and this is probably going to be named
10801
06:12:19,920 --> 06:12:21,440
something else for you though
10802
06:12:21,440 --> 06:12:24,000
so we now have this photo image called
10803
06:12:24,000 --> 06:12:24,638
icon
10804
06:12:24,638 --> 06:12:27,520
that we can use and i need to set the
10805
06:12:27,520 --> 06:12:28,160
icon
10806
06:12:28,160 --> 06:12:31,360
of my window to this icon and luckily
10807
06:12:31,360 --> 06:12:33,040
there is a function to take care of that
10808
06:12:33,040 --> 06:12:33,600
for us
10809
06:12:33,600 --> 06:12:35,600
type in the name of the window which is
10810
06:12:35,600 --> 06:12:36,718
window
10811
06:12:36,718 --> 06:12:39,680
dot and we're going to use the icon
10812
06:12:39,680 --> 06:12:40,400
photo
10813
06:12:40,400 --> 06:12:43,600
function there are two arguments the
10814
06:12:43,600 --> 06:12:46,160
first we're going to set to true
10815
06:12:46,160 --> 06:12:48,240
and the second is the photo image that
10816
06:12:48,240 --> 06:12:49,200
we want to use
10817
06:12:49,200 --> 06:12:51,920
and this photo image is called icon so
10818
06:12:51,920 --> 06:12:53,120
we're going to pass
10819
06:12:53,120 --> 06:12:55,520
this photo image as an argument to this
10820
06:12:55,520 --> 06:12:56,240
function
10821
06:12:56,240 --> 06:12:58,958
of icon photo and now when we compile
10822
06:12:58,958 --> 06:12:59,920
and run this
10823
06:12:59,920 --> 06:13:02,480
the image that we wanted to use is now
10824
06:13:02,480 --> 06:13:05,040
set for the top left icon of this window
10825
06:13:05,040 --> 06:13:05,680
bar
10826
06:13:05,680 --> 06:13:07,680
however it loses a lot of the quality
10827
06:13:07,680 --> 06:13:09,600
since the dimensions are so small
10828
06:13:09,600 --> 06:13:11,280
but you can still tell that it's my logo
10829
06:13:11,280 --> 06:13:13,600
though so that's how to replace
10830
06:13:13,600 --> 06:13:15,760
the icon of your window bar in the top
10831
06:13:15,760 --> 06:13:17,680
left corner now let's change the
10832
06:13:17,680 --> 06:13:18,718
background color
10833
06:13:18,718 --> 06:13:20,718
of this window and one way in which we
10834
06:13:20,718 --> 06:13:22,798
can do that is to use the config
10835
06:13:22,798 --> 06:13:24,798
function you can use the config function
10836
06:13:24,798 --> 06:13:26,240
anytime that you want to make any
10837
06:13:26,240 --> 06:13:27,680
changes to this window
10838
06:13:27,680 --> 06:13:29,360
so let's change the background color
10839
06:13:29,360 --> 06:13:31,760
here and there is an option called
10840
06:13:31,760 --> 06:13:34,160
background and we can set this equal to
10841
06:13:34,160 --> 06:13:34,798
either
10842
06:13:34,798 --> 06:13:37,760
a color name or a hexadecimal value
10843
06:13:37,760 --> 06:13:39,760
let's begin with just some color names
10844
06:13:39,760 --> 06:13:41,360
let's say that i want the background
10845
06:13:41,360 --> 06:13:42,878
color to be black
10846
06:13:42,878 --> 06:13:44,638
i'll just type in the word black here
10847
06:13:44,638 --> 06:13:46,160
and this will change the background
10848
06:13:46,160 --> 06:13:48,638
color of my window to black
10849
06:13:48,638 --> 06:13:51,840
alternatively you can set a hex value as
10850
06:13:51,840 --> 06:13:52,798
the color
10851
06:13:52,798 --> 06:13:55,520
so if you were to look up or google hex
10852
06:13:55,520 --> 06:13:56,480
color picker
10853
06:13:56,480 --> 06:13:58,558
you can select whatever color you want
10854
06:13:58,558 --> 06:13:59,840
let's say that i want
10855
06:13:59,840 --> 06:14:03,280
a sky blue color like this well this is
10856
06:14:03,280 --> 06:14:05,520
the hex value i'm going to copy this
10857
06:14:05,520 --> 06:14:07,120
value
10858
06:14:07,120 --> 06:14:09,520
and then paste it within the option for
10859
06:14:09,520 --> 06:14:10,480
my background
10860
06:14:10,480 --> 06:14:12,480
make sure you have this hashtag as well
10861
06:14:12,480 --> 06:14:14,798
because that represents that this is a
10862
06:14:14,798 --> 06:14:17,760
hex value so now the background color is
10863
06:14:17,760 --> 06:14:20,080
that sky blue color that i picked
10864
06:14:20,080 --> 06:14:21,920
well everybody that's the basics of
10865
06:14:21,920 --> 06:14:24,400
creating a window in python and in
10866
06:14:24,400 --> 06:14:25,840
future videos we're going to be
10867
06:14:25,840 --> 06:14:26,558
exploring
10868
06:14:26,558 --> 06:14:28,160
different widgets that we can add to
10869
06:14:28,160 --> 06:14:30,480
this window that have some functionality
10870
06:14:30,480 --> 06:14:32,000
so if you would like a copy of all this
10871
06:14:32,000 --> 06:14:33,600
code i'll post all of this in the
10872
06:14:33,600 --> 06:14:34,718
comments down below
10873
06:14:34,718 --> 06:14:36,480
but yeah that's how to create your very
10874
06:14:36,480 --> 06:14:38,958
first gui in python
10875
06:14:38,958 --> 06:14:40,558
hey what's going on everybody it's your
10876
06:14:40,558 --> 06:14:42,320
bro here hope you're doing well and in
10877
06:14:42,320 --> 06:14:43,520
this video i'm going to teach you guys
10878
06:14:43,520 --> 06:14:44,400
how we can create
10879
06:14:44,400 --> 06:14:47,040
labels in python so sit back relax and
10880
06:14:47,040 --> 06:14:49,760
enjoy the show
10881
06:14:49,840 --> 06:14:52,160
what is a label well i'm glad you asked
10882
06:14:52,160 --> 06:14:52,958
that question
10883
06:14:52,958 --> 06:14:55,920
it's an area widget that holds text and
10884
06:14:55,920 --> 06:14:56,400
or
10885
06:14:56,400 --> 06:14:58,958
an image within a window so to begin
10886
06:14:58,958 --> 06:15:00,958
we'll need to create a window to act as
10887
06:15:00,958 --> 06:15:02,000
a container
10888
06:15:02,000 --> 06:15:04,160
to hold this label we can create a
10889
06:15:04,160 --> 06:15:05,280
window with
10890
06:15:05,280 --> 06:15:07,480
window equals tk followed by
10891
06:15:07,480 --> 06:15:08,718
window.mainloop
10892
06:15:08,718 --> 06:15:10,878
and this will give us a window in which
10893
06:15:10,878 --> 06:15:12,638
we can add a label to
10894
06:15:12,638 --> 06:15:15,120
now let's give our label a unique name
10895
06:15:15,120 --> 06:15:16,480
such as label
10896
06:15:16,480 --> 06:15:18,558
and in order to instantiate a label
10897
06:15:18,558 --> 06:15:20,000
we'll follow this label name
10898
06:15:20,000 --> 06:15:23,200
with equals label with a capital l and a
10899
06:15:23,200 --> 06:15:24,638
set of parentheses
10900
06:15:24,638 --> 06:15:26,480
the parentheses are acting as the
10901
06:15:26,480 --> 06:15:28,240
constructor for this widget
10902
06:15:28,240 --> 06:15:30,798
our label and we can pass in arguments
10903
06:15:30,798 --> 06:15:31,440
into
10904
06:15:31,440 --> 06:15:33,520
these parentheses the first argument
10905
06:15:33,520 --> 06:15:34,480
that we're going to pass
10906
06:15:34,480 --> 06:15:37,760
in is a master the container for this
10907
06:15:37,760 --> 06:15:38,240
label
10908
06:15:38,240 --> 06:15:40,000
we're going to pass in the name of our
10909
06:15:40,000 --> 06:15:42,080
window because our window is acting as
10910
06:15:42,080 --> 06:15:43,440
the container
10911
06:15:43,440 --> 06:15:45,440
with widgets in python we can
10912
06:15:45,440 --> 06:15:47,360
de-eliminate the arguments that we're
10913
06:15:47,360 --> 06:15:48,080
passing in
10914
06:15:48,080 --> 06:15:50,958
with a comma and there are options
10915
06:15:50,958 --> 06:15:52,160
available to us
10916
06:15:52,160 --> 06:15:54,558
options are keyword arguments that we
10917
06:15:54,558 --> 06:15:56,798
can pass in to the constructor for this
10918
06:15:56,798 --> 06:15:57,600
widget
10919
06:15:57,600 --> 06:16:00,400
one option available to us is text and
10920
06:16:00,400 --> 06:16:02,638
we can set the text of this label equal
10921
06:16:02,638 --> 06:16:02,958
to
10922
06:16:02,958 --> 06:16:05,440
a string that we set so let's say that
10923
06:16:05,440 --> 06:16:07,120
we want this label to say
10924
06:16:07,120 --> 06:16:09,600
hello world well we're going to pass in
10925
06:16:09,600 --> 06:16:10,878
this option
10926
06:16:10,878 --> 06:16:14,160
text and it's a keyword argument
10927
06:16:14,160 --> 06:16:16,400
so if we were to do this we're going to
10928
06:16:16,400 --> 06:16:17,280
set the text
10929
06:16:17,280 --> 06:16:20,320
of the label but this label currently
10930
06:16:20,320 --> 06:16:21,920
doesn't appear within our window
10931
06:16:21,920 --> 06:16:23,520
what gives bro i thought you knew how to
10932
06:16:23,520 --> 06:16:25,840
code hold up wait a minute there's one
10933
06:16:25,840 --> 06:16:27,200
more thing that we need to do
10934
06:16:27,200 --> 06:16:29,520
so we're going to actually add this
10935
06:16:29,520 --> 06:16:30,320
label
10936
06:16:30,320 --> 06:16:33,600
to the window now we set the master
10937
06:16:33,600 --> 06:16:35,760
of the label to be our window but now we
10938
06:16:35,760 --> 06:16:38,160
actually have to add it as step two
10939
06:16:38,160 --> 06:16:40,878
one way which we can do that is to use a
10940
06:16:40,878 --> 06:16:41,360
pack
10941
06:16:41,360 --> 06:16:44,320
function of our label so follow label
10942
06:16:44,320 --> 06:16:46,080
equals label and pass in all of your
10943
06:16:46,080 --> 06:16:46,798
arguments
10944
06:16:46,798 --> 06:16:48,558
and then follow this with the name of
10945
06:16:48,558 --> 06:16:51,280
the label dot pack
10946
06:16:51,280 --> 06:16:53,920
so with the pack option by default this
10947
06:16:53,920 --> 06:16:55,760
will place our widget in the top
10948
06:16:55,760 --> 06:16:58,718
center of our window another way in
10949
06:16:58,718 --> 06:17:00,240
which we can add a label
10950
06:17:00,240 --> 06:17:02,958
to a container or window is to use the
10951
06:17:02,958 --> 06:17:03,920
place function
10952
06:17:03,920 --> 06:17:05,600
and we can set some coordinates of where
10953
06:17:05,600 --> 06:17:08,240
we want this label or other widget to
10954
06:17:08,240 --> 06:17:08,798
appear
10955
06:17:08,798 --> 06:17:10,638
within the window if we want this to
10956
06:17:10,638 --> 06:17:12,480
appear in the top left corner
10957
06:17:12,480 --> 06:17:15,520
we can set x to equal zero and y t
10958
06:17:15,520 --> 06:17:17,200
equals zero and these are options
10959
06:17:17,200 --> 06:17:18,958
available to us too that we can pass
10960
06:17:18,958 --> 06:17:22,240
in so this place function will place
10961
06:17:22,240 --> 06:17:25,840
our label at some position or
10962
06:17:25,840 --> 06:17:27,600
some set of coordinates within the
10963
06:17:27,600 --> 06:17:28,958
window and with
10964
06:17:28,958 --> 06:17:31,440
x equals 0 and y equals 0 that will
10965
06:17:31,440 --> 06:17:33,680
place our widget in the top left corner
10966
06:17:33,680 --> 06:17:34,558
of the window
10967
06:17:34,558 --> 06:17:36,400
and if i were to change these to let's
10968
06:17:36,400 --> 06:17:38,240
say x equals 100
10969
06:17:38,240 --> 06:17:40,878
and y equals 100 well this is going to
10970
06:17:40,878 --> 06:17:42,240
be placed
10971
06:17:42,240 --> 06:17:45,280
down by 100 pixels and to the right by
10972
06:17:45,280 --> 06:17:46,480
100 pixels
10973
06:17:46,480 --> 06:17:48,878
so you can use pack or place there's a
10974
06:17:48,878 --> 06:17:49,600
few others
10975
06:17:49,600 --> 06:17:51,200
like grid but they're a little more
10976
06:17:51,200 --> 06:17:53,120
complex so we'll get to that in a future
10977
06:17:53,120 --> 06:17:53,600
video
10978
06:17:53,600 --> 06:17:55,600
for the time being i'm going to use the
10979
06:17:55,600 --> 06:17:57,200
pack function of the label
10980
06:17:57,200 --> 06:17:59,360
and turn this place function into a
10981
06:17:59,360 --> 06:18:01,280
comment
10982
06:18:01,280 --> 06:18:04,878
dot pack all right now
10983
06:18:04,878 --> 06:18:07,040
how can we customize this label we can
10984
06:18:07,040 --> 06:18:08,718
pass in different options
10985
06:18:08,718 --> 06:18:10,958
when we instantiate this label let's
10986
06:18:10,958 --> 06:18:12,638
change the font and that's another
10987
06:18:12,638 --> 06:18:13,360
option
10988
06:18:13,360 --> 06:18:16,638
we can change the font with font equals
10989
06:18:16,638 --> 06:18:18,958
and then we can pass in a font let's
10990
06:18:18,958 --> 06:18:19,760
begin with a
10991
06:18:19,760 --> 06:18:22,958
font family i will say maybe ariel
10992
06:18:22,958 --> 06:18:26,958
a size perhaps 40 and then a style if
10993
06:18:26,958 --> 06:18:28,080
you so choose
10994
06:18:28,080 --> 06:18:30,558
like bold there's also italic and
10995
06:18:30,558 --> 06:18:31,440
underline
10996
06:18:31,440 --> 06:18:33,520
uh but this will work just fine then so
10997
06:18:33,520 --> 06:18:35,520
this will change the font
10998
06:18:35,520 --> 06:18:38,000
of the text of our label and it says
10999
06:18:38,000 --> 06:18:39,760
hello world still
11000
06:18:39,760 --> 06:18:43,280
now let's change the foreground color
11001
06:18:43,280 --> 06:18:46,320
so that is another option the foreground
11002
06:18:46,320 --> 06:18:47,680
is the font color
11003
06:18:47,680 --> 06:18:50,798
and the option is fg for foreground it's
11004
06:18:50,798 --> 06:18:51,920
short for foreground
11005
06:18:51,920 --> 06:18:54,718
foreground equals we can state a color
11006
06:18:54,718 --> 06:18:55,440
name
11007
06:18:55,440 --> 06:18:58,478
such as green and our text
11008
06:18:58,478 --> 06:19:00,478
should now be green you can select
11009
06:19:00,478 --> 06:19:02,160
something more specific by
11010
06:19:02,160 --> 06:19:05,840
passing in a hex value so a custom color
11011
06:19:05,840 --> 06:19:06,558
that i like
11012
06:19:06,558 --> 06:19:10,478
is hashtag zero zero ff00
11013
06:19:10,478 --> 06:19:12,478
this is bright green you can always look
11014
06:19:12,478 --> 06:19:14,400
up what different hex values are
11015
06:19:14,400 --> 06:19:16,000
and we can also change the background
11016
06:19:16,000 --> 06:19:17,520
color too
11017
06:19:17,520 --> 06:19:20,400
that is bg short for background color
11018
06:19:20,400 --> 06:19:22,240
and we can pass in a color name
11019
06:19:22,240 --> 06:19:24,718
or a hex value i'm just going to set
11020
06:19:24,718 --> 06:19:26,558
this to black just to demonstrate that
11021
06:19:26,558 --> 06:19:27,920
we can pass in both
11022
06:19:27,920 --> 06:19:30,798
either hex values or color names and
11023
06:19:30,798 --> 06:19:31,520
what we have
11024
06:19:31,520 --> 06:19:34,558
is green text on a black background
11025
06:19:34,558 --> 06:19:37,200
and this is the limits of our label
11026
06:19:37,200 --> 06:19:38,240
within the window
11027
06:19:38,240 --> 06:19:41,040
because when we expand this window the
11028
06:19:41,040 --> 06:19:42,718
label is still going to stay the same
11029
06:19:42,718 --> 06:19:43,360
size
11030
06:19:43,360 --> 06:19:46,000
if we were to change the text within the
11031
06:19:46,000 --> 06:19:46,878
label
11032
06:19:46,878 --> 06:19:49,040
our label is only going to take up the
11033
06:19:49,040 --> 06:19:50,240
room that it needs
11034
06:19:50,240 --> 06:19:52,160
and you can see that our label is now
11035
06:19:52,160 --> 06:19:54,080
smaller now we have all of these
11036
06:19:54,080 --> 06:19:55,840
arguments within our constructor and
11037
06:19:55,840 --> 06:19:57,360
it's getting a little bit difficult to
11038
06:19:57,360 --> 06:19:58,240
actually read
11039
06:19:58,240 --> 06:20:00,400
so i'm going to separate each of these
11040
06:20:00,400 --> 06:20:02,478
arguments line by line just so that this
11041
06:20:02,478 --> 06:20:03,600
is more organized
11042
06:20:03,600 --> 06:20:05,280
but it'll work just the same as it did
11043
06:20:05,280 --> 06:20:07,680
before now how do we add a
11044
06:20:07,680 --> 06:20:10,160
border around our label one we can set
11045
06:20:10,160 --> 06:20:11,280
the border style
11046
06:20:11,280 --> 06:20:14,160
with the relief option and one border
11047
06:20:14,160 --> 06:20:14,798
that i like
11048
06:20:14,798 --> 06:20:18,878
is raised but right now the border width
11049
06:20:18,878 --> 06:20:21,120
is one so it's a little difficult for us
11050
06:20:21,120 --> 06:20:22,638
to actually see the border
11051
06:20:22,638 --> 06:20:24,718
let's increase the border width with the
11052
06:20:24,718 --> 06:20:26,160
option bd
11053
06:20:26,160 --> 06:20:29,680
and i'll set this equal to maybe 10.
11054
06:20:29,680 --> 06:20:32,638
so this border is now more visible
11055
06:20:32,638 --> 06:20:33,920
another relief
11056
06:20:33,920 --> 06:20:36,798
which is the border style is sunken and
11057
06:20:36,798 --> 06:20:38,558
this will give our label a
11058
06:20:38,558 --> 06:20:40,400
sunken appearance but i think i'll
11059
06:20:40,400 --> 06:20:42,320
change this back to raised
11060
06:20:42,320 --> 06:20:45,040
we can also add some padding around the
11061
06:20:45,040 --> 06:20:45,520
text
11062
06:20:45,520 --> 06:20:47,680
between the text and the border we can
11063
06:20:47,680 --> 06:20:49,440
pad x and pad y
11064
06:20:49,440 --> 06:20:52,320
pad x will add some padding some space
11065
06:20:52,320 --> 06:20:53,200
between
11066
06:20:53,200 --> 06:20:55,360
the x-axis of our text between the
11067
06:20:55,360 --> 06:20:57,840
border so let's set this to maybe 20
11068
06:20:57,840 --> 06:21:00,878
and we now have 20 pixels worth of space
11069
06:21:00,878 --> 06:21:01,360
between
11070
06:21:01,360 --> 06:21:06,320
the text and the border let's also pad y
11071
06:21:06,958 --> 06:21:10,320
this will add some space above and below
11072
06:21:10,320 --> 06:21:13,680
our text between the text and the border
11073
06:21:13,680 --> 06:21:16,400
now how do we add an image to a label we
11074
06:21:16,400 --> 06:21:18,000
first need to create what's called
11075
06:21:18,000 --> 06:21:21,120
a photo image and we will create this
11076
06:21:21,120 --> 06:21:21,760
within
11077
06:21:21,760 --> 06:21:24,400
the lines of window equals tk and
11078
06:21:24,400 --> 06:21:25,680
window.main loop
11079
06:21:25,680 --> 06:21:27,840
and let's give this photo image a unique
11080
06:21:27,840 --> 06:21:28,878
name like
11081
06:21:28,878 --> 06:21:31,280
photo and in order to create a photo
11082
06:21:31,280 --> 06:21:33,040
image we're going to follow this with
11083
06:21:33,040 --> 06:21:36,558
photo equals photo image
11084
06:21:36,558 --> 06:21:38,478
and within the constructor of our photo
11085
06:21:38,478 --> 06:21:40,400
image there's an option called
11086
06:21:40,400 --> 06:21:43,120
file and we can list a file path or a
11087
06:21:43,120 --> 06:21:44,080
file name
11088
06:21:44,080 --> 06:21:46,558
right now on my desktop there is a image
11089
06:21:46,558 --> 06:21:48,320
that i want to use and i need to convert
11090
06:21:48,320 --> 06:21:50,080
this to a photo image
11091
06:21:50,080 --> 06:21:52,320
here's my desktop and this is an image
11092
06:21:52,320 --> 06:21:54,558
that i want to use to add to my label
11093
06:21:54,558 --> 06:21:56,638
i need to get the file path and i can
11094
06:21:56,638 --> 06:21:58,798
get that by going to properties
11095
06:21:58,798 --> 06:22:01,840
then underneath location i can copy this
11096
06:22:01,840 --> 06:22:03,840
location address
11097
06:22:03,840 --> 06:22:07,520
and paste it within the quotes for file
11098
06:22:07,520 --> 06:22:09,840
and then i need to add the image name
11099
06:22:09,840 --> 06:22:12,320
and this is person.png
11100
06:22:12,320 --> 06:22:14,400
and with these backslashes these need to
11101
06:22:14,400 --> 06:22:16,878
be double backslashes because
11102
06:22:16,878 --> 06:22:18,718
that is the escape character for a
11103
06:22:18,718 --> 06:22:20,558
backslash
11104
06:22:20,558 --> 06:22:23,360
but what would be a better option is to
11105
06:22:23,360 --> 06:22:23,840
add
11106
06:22:23,840 --> 06:22:25,840
this image to my project folder that
11107
06:22:25,840 --> 06:22:27,440
contains the python file that i'm
11108
06:22:27,440 --> 06:22:28,400
working with
11109
06:22:28,400 --> 06:22:31,280
so in place of listing the entire file
11110
06:22:31,280 --> 06:22:31,840
path
11111
06:22:31,840 --> 06:22:33,760
i'm going to copy the image that i want
11112
06:22:33,760 --> 06:22:36,240
to use go to my project folder
11113
06:22:36,240 --> 06:22:40,400
and then paste it and i no longer need
11114
06:22:40,400 --> 06:22:42,878
this entire file path i can just list
11115
06:22:42,878 --> 06:22:44,798
the image name that i have
11116
06:22:44,798 --> 06:22:46,958
and now what we need to do is add this
11117
06:22:46,958 --> 06:22:48,958
photo to the label and there's an option
11118
06:22:48,958 --> 06:22:49,680
to do so
11119
06:22:49,680 --> 06:22:52,000
that is the image option and we'll place
11120
06:22:52,000 --> 06:22:53,440
this within the constructor for the
11121
06:22:53,440 --> 06:22:54,000
label
11122
06:22:54,000 --> 06:22:56,558
we're going to set image equal to the
11123
06:22:56,558 --> 06:22:58,080
photo image that we have
11124
06:22:58,080 --> 06:23:00,798
and our photo image is called photo this
11125
06:23:00,798 --> 06:23:02,478
will add our photo image
11126
06:23:02,478 --> 06:23:04,638
to our label or other widget depending
11127
06:23:04,638 --> 06:23:06,718
on what we're working with
11128
06:23:06,718 --> 06:23:09,280
so we have our image but it replaced all
11129
06:23:09,280 --> 06:23:10,080
of the text
11130
06:23:10,080 --> 06:23:12,478
that we had for this label how do we add
11131
06:23:12,478 --> 06:23:13,280
both
11132
06:23:13,280 --> 06:23:16,160
text and an image to a window well we
11133
06:23:16,160 --> 06:23:16,958
can follow this
11134
06:23:16,958 --> 06:23:19,840
with the compound option we can set a
11135
06:23:19,840 --> 06:23:21,040
direction of where
11136
06:23:21,040 --> 06:23:23,680
we want this image to be placed relative
11137
06:23:23,680 --> 06:23:25,520
to the text that we have
11138
06:23:25,520 --> 06:23:28,000
so i'm going to set compound equal to
11139
06:23:28,000 --> 06:23:28,558
bottom
11140
06:23:28,558 --> 06:23:31,040
so that the image appears on the bottom
11141
06:23:31,040 --> 06:23:31,920
or underneath
11142
06:23:31,920 --> 06:23:35,200
our text so we can both have text and an
11143
06:23:35,200 --> 06:23:35,920
image
11144
06:23:35,920 --> 06:23:37,600
or if i want this to be in a different
11145
06:23:37,600 --> 06:23:39,760
location i could set this to maybe top
11146
06:23:39,760 --> 06:23:41,920
left or right if i were to set this to
11147
06:23:41,920 --> 06:23:42,958
top we have
11148
06:23:42,958 --> 06:23:46,400
our image on top of our text and one
11149
06:23:46,400 --> 06:23:48,400
thing to know about labels and many
11150
06:23:48,400 --> 06:23:50,240
other widgets in python is that
11151
06:23:50,240 --> 06:23:52,638
the size of the widget will actually
11152
06:23:52,638 --> 06:23:54,320
increase to accommodate
11153
06:23:54,320 --> 06:23:56,160
the size of all the components that's
11154
06:23:56,160 --> 06:23:58,558
contained within if i were to give the
11155
06:23:58,558 --> 06:24:01,440
text a longer string of characters such
11156
06:24:01,440 --> 06:24:02,160
as
11157
06:24:02,160 --> 06:24:05,440
bro do you even code then
11158
06:24:05,440 --> 06:24:07,680
the width of our label is actually going
11159
06:24:07,680 --> 06:24:08,798
to expand
11160
06:24:08,798 --> 06:24:11,280
to accommodate the size of the longer
11161
06:24:11,280 --> 06:24:13,280
string of text that we have
11162
06:24:13,280 --> 06:24:15,680
so that's the basics of labels everyone
11163
06:24:15,680 --> 06:24:17,040
if you would like a copy of all this
11164
06:24:17,040 --> 06:24:18,798
code i will post all of this in the
11165
06:24:18,798 --> 06:24:20,080
comments down below
11166
06:24:20,080 --> 06:24:22,558
but yeah that's the basics of labels in
11167
06:24:22,558 --> 06:24:24,400
python
11168
06:24:24,400 --> 06:24:26,000
hey what's going on everybody it's your
11169
06:24:26,000 --> 06:24:27,600
bro here hope you're doing well
11170
06:24:27,600 --> 06:24:28,958
and in this video i'm going to teach you
11171
06:24:28,958 --> 06:24:31,200
guys how we can create buttons in python
11172
06:24:31,200 --> 06:24:35,840
so sit back relax and enjoy the show
11173
06:24:35,920 --> 06:24:38,160
buttons you click them and then they do
11174
06:24:38,160 --> 06:24:39,920
stuff that's pretty much it
11175
06:24:39,920 --> 06:24:41,680
so this is how to create a button in
11176
06:24:41,680 --> 06:24:43,280
python we'll need a window
11177
06:24:43,280 --> 06:24:46,000
window equals tk followed by window.main
11178
06:24:46,000 --> 06:24:46,638
loop
11179
06:24:46,638 --> 06:24:49,360
let's call our button just button and in
11180
06:24:49,360 --> 06:24:51,120
order to create this we'll follow this
11181
06:24:51,120 --> 06:24:51,680
with
11182
06:24:51,680 --> 06:24:55,280
equals button and within the constructor
11183
06:24:55,280 --> 06:24:56,798
for this button we're going to list
11184
06:24:56,798 --> 06:24:59,200
what we want to add this button to what
11185
06:24:59,200 --> 06:25:00,400
is the master
11186
06:25:00,400 --> 06:25:03,440
the master is the window and in order to
11187
06:25:03,440 --> 06:25:04,798
display this button
11188
06:25:04,798 --> 06:25:07,280
we're going to follow this with button
11189
06:25:07,280 --> 06:25:07,840
dot
11190
06:25:07,840 --> 06:25:11,440
pack and we should have a small
11191
06:25:11,440 --> 06:25:13,840
rectangular button within our window
11192
06:25:13,840 --> 06:25:16,320
but it doesn't display any text nor does
11193
06:25:16,320 --> 06:25:17,280
it do anything
11194
06:25:17,280 --> 06:25:19,280
so let's add some text and there is an
11195
06:25:19,280 --> 06:25:20,400
option to do that
11196
06:25:20,400 --> 06:25:22,240
within the constructor we're going to
11197
06:25:22,240 --> 06:25:23,840
list the option for text
11198
06:25:23,840 --> 06:25:25,600
and set this to whatever text you want
11199
06:25:25,600 --> 06:25:26,958
to display let's
11200
06:25:26,958 --> 06:25:30,000
say click me and then when we run
11201
06:25:30,000 --> 06:25:32,400
this our button now says the text that
11202
06:25:32,400 --> 06:25:33,040
we set
11203
06:25:33,040 --> 06:25:35,840
and it says click me however it
11204
06:25:35,840 --> 06:25:37,200
currently doesn't do anything though
11205
06:25:37,200 --> 06:25:39,600
we need to set a command that is another
11206
06:25:39,600 --> 06:25:40,400
option
11207
06:25:40,400 --> 06:25:43,520
command equals and we list a function
11208
06:25:43,520 --> 06:25:46,240
name this is what's known as a callback
11209
06:25:46,240 --> 06:25:47,840
so let's create a function
11210
06:25:47,840 --> 06:25:50,000
called click and then outside of the
11211
06:25:50,000 --> 06:25:51,920
window let's define what our click
11212
06:25:51,920 --> 06:25:55,040
function does def click
11213
06:25:55,040 --> 06:25:58,638
and let's print a message you
11214
06:25:58,638 --> 06:26:02,478
clicked the button
11215
06:26:02,478 --> 06:26:05,200
now with the command we're going to list
11216
06:26:05,200 --> 06:26:05,920
the
11217
06:26:05,920 --> 06:26:08,638
function name without the parentheses so
11218
06:26:08,638 --> 06:26:10,160
make sure you're writing it as
11219
06:26:10,160 --> 06:26:12,718
this and not that this is what's known
11220
06:26:12,718 --> 06:26:14,160
as a callback
11221
06:26:14,160 --> 06:26:16,718
so when we click on this button it's
11222
06:26:16,718 --> 06:26:19,280
going to perform our callback
11223
06:26:19,280 --> 06:26:21,680
and perform whatever is within our
11224
06:26:21,680 --> 06:26:22,320
function
11225
06:26:22,320 --> 06:26:25,680
click just like that let's customize the
11226
06:26:25,680 --> 06:26:27,360
appearance of this button i'm going to
11227
06:26:27,360 --> 06:26:28,240
change the font
11228
06:26:28,240 --> 06:26:31,600
that is another option font equals
11229
06:26:31,600 --> 06:26:34,478
and let's pick a professional font such
11230
06:26:34,478 --> 06:26:35,360
as comic
11231
06:26:35,360 --> 06:26:38,798
sans it's very professional and i'll set
11232
06:26:38,798 --> 06:26:41,120
a size of 30.
11233
06:26:41,120 --> 06:26:43,440
and now you can actually read what's on
11234
06:26:43,440 --> 06:26:46,160
this button it's large enough
11235
06:26:46,160 --> 06:26:48,000
let's set the foreground color that is
11236
06:26:48,000 --> 06:26:49,920
the font color fg
11237
06:26:49,920 --> 06:26:52,400
you can place a color name or a hex
11238
06:26:52,400 --> 06:26:53,360
value here
11239
06:26:53,360 --> 06:26:57,520
i'll pick green that is 0 0 ff00
11240
06:26:57,520 --> 06:27:00,320
and our text should now be green which
11241
06:27:00,320 --> 06:27:01,600
it is
11242
06:27:01,600 --> 06:27:04,240
let's also change the background color
11243
06:27:04,240 --> 06:27:05,600
that is bg
11244
06:27:05,600 --> 06:27:08,638
i will pick the color name of black and
11245
06:27:08,638 --> 06:27:09,600
now we have
11246
06:27:09,600 --> 06:27:12,320
green text on a black background notice
11247
06:27:12,320 --> 06:27:14,000
that when i click on this button
11248
06:27:14,000 --> 06:27:16,958
or hold my mouse button down the color
11249
06:27:16,958 --> 06:27:18,798
scheme has changed that's because
11250
06:27:18,798 --> 06:27:21,200
there is a different active foreground
11251
06:27:21,200 --> 06:27:22,718
and active background
11252
06:27:22,718 --> 06:27:24,958
and we can change those too so that this
11253
06:27:24,958 --> 06:27:26,638
won't flash every time you click on the
11254
06:27:26,638 --> 06:27:27,360
button
11255
06:27:27,360 --> 06:27:29,360
so let's change the active foreground
11256
06:27:29,360 --> 06:27:30,958
and active background
11257
06:27:30,958 --> 06:27:33,200
those are other options active
11258
06:27:33,200 --> 06:27:34,558
foreground
11259
06:27:34,558 --> 06:27:36,558
and i'll set this to the same color as
11260
06:27:36,558 --> 06:27:38,320
my foreground
11261
06:27:38,320 --> 06:27:41,280
which is green so the text color is
11262
06:27:41,280 --> 06:27:42,320
going to stay the same
11263
06:27:42,320 --> 06:27:44,160
and i'll also change the active
11264
06:27:44,160 --> 06:27:46,718
background
11265
06:27:47,120 --> 06:27:49,600
and i will also set this to black to
11266
06:27:49,600 --> 06:27:50,400
match my
11267
06:27:50,400 --> 06:27:53,600
background and then this should no
11268
06:27:53,600 --> 06:27:54,400
longer flash
11269
06:27:54,400 --> 06:27:57,520
when we click on our button
11270
06:27:57,520 --> 06:27:59,520
if you need to disable somebody from
11271
06:27:59,520 --> 06:28:00,638
clicking on this button
11272
06:28:00,638 --> 06:28:02,718
there is an option to do so and that
11273
06:28:02,718 --> 06:28:04,638
option is the state
11274
06:28:04,638 --> 06:28:07,280
option and normally this is active but
11275
06:28:07,280 --> 06:28:08,558
you can set this to
11276
06:28:08,558 --> 06:28:11,120
disabled and we can no longer click on
11277
06:28:11,120 --> 06:28:12,558
this button
11278
06:28:12,558 --> 06:28:14,080
and you can see that the color scheme
11279
06:28:14,080 --> 06:28:16,240
has changed too it's all grayed out and
11280
06:28:16,240 --> 06:28:17,200
whatnot
11281
06:28:17,200 --> 06:28:19,360
so i'm going to replace this back with
11282
06:28:19,360 --> 06:28:21,200
active because we still have a few more
11283
06:28:21,200 --> 06:28:22,160
things to do
11284
06:28:22,160 --> 06:28:24,718
now how do we add an image to a button
11285
06:28:24,718 --> 06:28:25,360
there is an
11286
06:28:25,360 --> 06:28:28,000
image option and we set this equal to a
11287
06:28:28,000 --> 06:28:28,958
photo image
11288
06:28:28,958 --> 06:28:30,798
but first we need to create a photo
11289
06:28:30,798 --> 06:28:32,958
image i have this photo of
11290
06:28:32,958 --> 06:28:34,878
a like button a thumbs up that i want to
11291
06:28:34,878 --> 06:28:37,440
use i need to create a photo image from
11292
06:28:37,440 --> 06:28:38,638
this image file
11293
06:28:38,638 --> 06:28:40,718
so within the window let's call this
11294
06:28:40,718 --> 06:28:42,160
photo image just
11295
06:28:42,160 --> 06:28:46,080
photo photo equals
11296
06:28:46,080 --> 06:28:49,280
photo image then i'm going to list the
11297
06:28:49,280 --> 06:28:51,360
file path or the file name
11298
06:28:51,360 --> 06:28:53,600
since this image is within my same
11299
06:28:53,600 --> 06:28:54,638
project folder
11300
06:28:54,638 --> 06:28:57,280
i only have to list the file name and
11301
06:28:57,280 --> 06:28:58,120
this is called
11302
06:28:58,120 --> 06:29:00,798
like.png but if you use your own image
11303
06:29:00,798 --> 06:29:01,840
it's probably going to be named
11304
06:29:01,840 --> 06:29:02,878
something else
11305
06:29:02,878 --> 06:29:05,360
then i need to set the image equal to my
11306
06:29:05,360 --> 06:29:07,040
photo image which i named
11307
06:29:07,040 --> 06:29:11,760
photo but the photo replaces the text on
11308
06:29:11,760 --> 06:29:14,080
this button but it still works the same
11309
06:29:14,080 --> 06:29:16,400
so we would like to display some text as
11310
06:29:16,400 --> 06:29:17,600
well as our image
11311
06:29:17,600 --> 06:29:21,360
we need to use the compound option
11312
06:29:21,360 --> 06:29:24,718
and we list a direction we can list
11313
06:29:24,718 --> 06:29:27,840
top bottom left right we want this image
11314
06:29:27,840 --> 06:29:28,320
to be
11315
06:29:28,320 --> 06:29:31,520
on the perhaps bottom so let's list the
11316
06:29:31,520 --> 06:29:32,080
bottom
11317
06:29:32,080 --> 06:29:34,160
and now when we run this we have our
11318
06:29:34,160 --> 06:29:36,000
text as well as our image
11319
06:29:36,000 --> 06:29:37,840
and if we changed compound to a
11320
06:29:37,840 --> 06:29:39,040
different direction like
11321
06:29:39,040 --> 06:29:41,600
top then the image is going to be
11322
06:29:41,600 --> 06:29:42,240
displayed
11323
06:29:42,240 --> 06:29:45,920
on the top relative to the text
11324
06:29:45,920 --> 06:29:47,920
what if we wanted to count the amount of
11325
06:29:47,920 --> 06:29:49,840
times that we clicked this button
11326
06:29:49,840 --> 06:29:52,080
we could create a variable called count
11327
06:29:52,080 --> 06:29:53,520
and we'll set this equal to
11328
06:29:53,520 --> 06:29:56,718
zero but in order for us to access this
11329
06:29:56,718 --> 06:29:58,080
count variable
11330
06:29:58,080 --> 06:30:01,520
within the function of click we're going
11331
06:30:01,520 --> 06:30:02,478
to list
11332
06:30:02,478 --> 06:30:06,000
count as global so that this is a global
11333
06:30:06,000 --> 06:30:06,878
variable
11334
06:30:06,878 --> 06:30:09,200
every time we click this button let's
11335
06:30:09,200 --> 06:30:10,958
increment our variable count
11336
06:30:10,958 --> 06:30:14,400
by one by saying count plus equals one
11337
06:30:14,400 --> 06:30:16,400
and then we will print whatever the
11338
06:30:16,400 --> 06:30:18,160
value of count is
11339
06:30:18,160 --> 06:30:20,400
so within a print statement we'll just
11340
06:30:20,400 --> 06:30:21,760
say count
11341
06:30:21,760 --> 06:30:23,760
now when we click on this button we have
11342
06:30:23,760 --> 06:30:24,798
access to our count
11343
06:30:24,798 --> 06:30:27,040
variable that's outside of this function
11344
06:30:27,040 --> 06:30:28,798
and every time we click on this button
11345
06:30:28,798 --> 06:30:31,200
it's going to increase our count
11346
06:30:31,200 --> 06:30:32,080
variable by
11347
06:30:32,080 --> 06:30:34,878
one well everybody that's the basics of
11348
06:30:34,878 --> 06:30:35,600
creating
11349
06:30:35,600 --> 06:30:37,680
buttons in python if you want a copy of
11350
06:30:37,680 --> 06:30:39,680
all this code i'll post all of this in
11351
06:30:39,680 --> 06:30:40,878
the comments down below
11352
06:30:40,878 --> 06:30:43,200
but yeah that's the basics of buttons in
11353
06:30:43,200 --> 06:30:44,718
python
11354
06:30:44,718 --> 06:30:46,320
hey what's going on everybody it's your
11355
06:30:46,320 --> 06:30:48,000
bro here hope you're doing well and in
11356
06:30:48,000 --> 06:30:49,280
this video i'm going to teach you guys
11357
06:30:49,280 --> 06:30:51,520
how we can create a simple entry box in
11358
06:30:51,520 --> 06:30:52,000
python
11359
06:30:52,000 --> 06:30:56,160
so sit back relax and enjoy the show
11360
06:30:56,638 --> 06:30:58,240
if you find this video helpful please
11361
06:30:58,240 --> 06:30:59,760
remember to like
11362
06:30:59,760 --> 06:31:02,160
comment and subscribe your support will
11363
06:31:02,160 --> 06:31:04,160
help keep this channel running
11364
06:31:04,160 --> 06:31:06,240
alright my fellow bros in this video
11365
06:31:06,240 --> 06:31:07,840
we're going to be discussing the entry
11366
06:31:07,840 --> 06:31:08,320
widget
11367
06:31:08,320 --> 06:31:10,718
this is a text box that accepts a single
11368
06:31:10,718 --> 06:31:12,080
line of user input
11369
06:31:12,080 --> 06:31:14,240
as always we'll need a window window
11370
06:31:14,240 --> 06:31:15,120
equals tk
11371
06:31:15,120 --> 06:31:17,440
followed by window.main loop and let's
11372
06:31:17,440 --> 06:31:19,680
give our entry box a unique name let's
11373
06:31:19,680 --> 06:31:20,000
call
11374
06:31:20,000 --> 06:31:22,320
this entry for short and then to create
11375
06:31:22,320 --> 06:31:23,920
this entry widget we're going to follow
11376
06:31:23,920 --> 06:31:25,440
this with equals
11377
06:31:25,440 --> 06:31:28,000
entry and within the constructor we have
11378
06:31:28,000 --> 06:31:29,920
to state what we're adding this to
11379
06:31:29,920 --> 06:31:32,558
we're going to add this to our window
11380
06:31:32,558 --> 06:31:34,718
and we should finish this by packing it
11381
06:31:34,718 --> 06:31:36,240
or placing it
11382
06:31:36,240 --> 06:31:39,280
entry dot pack and we should have an
11383
06:31:39,280 --> 06:31:40,638
entry box within
11384
06:31:40,638 --> 06:31:43,040
our window however the text is somewhat
11385
06:31:43,040 --> 06:31:45,680
small so let's change that
11386
06:31:45,680 --> 06:31:47,600
we can set this as an option within the
11387
06:31:47,600 --> 06:31:50,400
constructor of our entry box
11388
06:31:50,400 --> 06:31:53,120
so let's set the font font equals and
11389
06:31:53,120 --> 06:31:54,638
pick whatever font you want
11390
06:31:54,638 --> 06:31:57,840
i'm going to pick maybe ariel and then a
11391
06:31:57,840 --> 06:31:58,400
size
11392
06:31:58,400 --> 06:32:00,878
it'd be 50. and this should be a little
11393
06:32:00,878 --> 06:32:02,798
bit larger
11394
06:32:02,798 --> 06:32:06,320
and we can type in some
11395
06:32:06,320 --> 06:32:10,958
text like this cool right
11396
06:32:10,958 --> 06:32:13,120
now we should add a submit button
11397
06:32:13,120 --> 06:32:14,718
because right now this currently doesn't
11398
06:32:14,718 --> 06:32:16,718
do anything if we type some text in
11399
06:32:16,718 --> 06:32:18,798
so let's create a button i'm going to
11400
06:32:18,798 --> 06:32:20,558
create this button maybe after
11401
06:32:20,558 --> 06:32:23,120
the entry box and let's say that this is
11402
06:32:23,120 --> 06:32:25,040
a submit button so let's call this
11403
06:32:25,040 --> 06:32:28,638
submit button equals button
11404
06:32:28,638 --> 06:32:30,000
what are we adding this to writing the
11405
06:32:30,000 --> 06:32:32,718
square window
11406
06:32:32,878 --> 06:32:35,440
and we'll set some text text equals
11407
06:32:35,440 --> 06:32:36,240
submit
11408
06:32:36,240 --> 06:32:37,840
we'll also need a command for this
11409
06:32:37,840 --> 06:32:39,280
button
11410
06:32:39,280 --> 06:32:42,000
command equals and we'll create a
11411
06:32:42,000 --> 06:32:43,520
function called submit
11412
06:32:43,520 --> 06:32:46,000
and we want to pack the submit button to
11413
06:32:46,000 --> 06:32:47,520
add it to the window
11414
06:32:47,520 --> 06:32:50,798
submit button.pack
11415
06:32:50,958 --> 06:32:53,520
all right now let's put our entry box on
11416
06:32:53,520 --> 06:32:55,280
the left hand side and our submit button
11417
06:32:55,280 --> 06:32:56,718
on the right hand side
11418
06:32:56,718 --> 06:32:58,878
when you pack these widgets you can set
11419
06:32:58,878 --> 06:33:00,638
these on a certain side
11420
06:33:00,638 --> 06:33:04,000
side equals left and our submit button
11421
06:33:04,000 --> 06:33:06,080
we want this on the right not necessary
11422
06:33:06,080 --> 06:33:08,240
but i think it looks better
11423
06:33:08,240 --> 06:33:10,478
and we also need to define our submit
11424
06:33:10,478 --> 06:33:12,320
function outside of the window
11425
06:33:12,320 --> 06:33:16,160
let's define what this does def submit
11426
06:33:16,160 --> 06:33:19,200
what's this going to do we need to get
11427
06:33:19,200 --> 06:33:21,360
the current value that's within our
11428
06:33:21,360 --> 06:33:22,400
entry box
11429
06:33:22,400 --> 06:33:24,478
one way in which we can do that is use
11430
06:33:24,478 --> 06:33:26,478
the entry boxes get
11431
06:33:26,478 --> 06:33:29,200
function this will return a string and
11432
06:33:29,200 --> 06:33:30,718
we can do something with that string
11433
06:33:30,718 --> 06:33:33,440
such as store this within a variable so
11434
06:33:33,440 --> 06:33:35,040
let's create a variable called maybe
11435
06:33:35,040 --> 06:33:35,760
username
11436
06:33:35,760 --> 06:33:38,240
let's pretend we're having a user submit
11437
06:33:38,240 --> 06:33:40,320
their username to login to something i
11438
06:33:40,320 --> 06:33:41,360
suppose
11439
06:33:41,360 --> 06:33:43,840
so we'll get the current value of the
11440
06:33:43,840 --> 06:33:44,798
entry box
11441
06:33:44,798 --> 06:33:46,478
store that within a variable and we'll
11442
06:33:46,478 --> 06:33:47,840
call this username
11443
06:33:47,840 --> 06:33:49,920
and then we will maybe print this for
11444
06:33:49,920 --> 06:33:52,798
fun print
11445
06:33:52,840 --> 06:33:55,600
hello plus user
11446
06:33:55,600 --> 06:33:58,878
name and let's try this
11447
06:33:58,878 --> 06:34:00,798
so here's our entry box here and our
11448
06:34:00,798 --> 06:34:02,478
submit button i'm just going to make up
11449
06:34:02,478 --> 06:34:03,920
something
11450
06:34:03,920 --> 06:34:06,840
click submit and it displays our message
11451
06:34:06,840 --> 06:34:09,440
hello ergo gerger gerger
11452
06:34:09,440 --> 06:34:10,798
i didn't say that this video was going
11453
06:34:10,798 --> 06:34:12,878
to be professional let's add a few other
11454
06:34:12,878 --> 06:34:13,600
buttons
11455
06:34:13,600 --> 06:34:15,840
let's say we want a delete button as
11456
06:34:15,840 --> 06:34:18,400
well as a backspace button
11457
06:34:18,400 --> 06:34:20,160
so let's copy what we have for our
11458
06:34:20,160 --> 06:34:22,320
submit button and create a delete button
11459
06:34:22,320 --> 06:34:24,478
and then a backspace button the delete
11460
06:34:24,478 --> 06:34:26,400
button is a little bit easier to code
11461
06:34:26,400 --> 06:34:28,958
so let's rename our submit button as
11462
06:34:28,958 --> 06:34:31,440
delete button
11463
06:34:31,440 --> 06:34:33,120
make sure to change the name to when you
11464
06:34:33,120 --> 06:34:34,878
pack this and the text
11465
06:34:34,878 --> 06:34:38,160
we will say delete and we'll create a
11466
06:34:38,160 --> 06:34:38,958
function called
11467
06:34:38,958 --> 06:34:42,638
delete and let's define what delete does
11468
06:34:42,638 --> 06:34:44,718
so after our submit function let's
11469
06:34:44,718 --> 06:34:46,320
define
11470
06:34:46,320 --> 06:34:50,000
our delete function and we need to
11471
06:34:50,000 --> 06:34:52,558
delete all of the text within our entry
11472
06:34:52,558 --> 06:34:53,520
box
11473
06:34:53,520 --> 06:34:55,200
one way in which we can do that is to
11474
06:34:55,200 --> 06:34:57,360
type in the name of our entry box
11475
06:34:57,360 --> 06:35:00,638
dot delete function
11476
06:35:00,638 --> 06:35:03,840
and this takes two positional arguments
11477
06:35:03,840 --> 06:35:06,080
which portion of our entry box do we
11478
06:35:06,080 --> 06:35:07,280
want to delete
11479
06:35:07,280 --> 06:35:09,440
so we want the very first character that
11480
06:35:09,440 --> 06:35:10,320
is index
11481
06:35:10,320 --> 06:35:13,200
0 followed by the last character and we
11482
06:35:13,200 --> 06:35:13,760
can say
11483
06:35:13,760 --> 06:35:16,000
end so this will delete all of the
11484
06:35:16,000 --> 06:35:16,798
characters
11485
06:35:16,798 --> 06:35:19,840
within our entry box and let's try it so
11486
06:35:19,840 --> 06:35:21,760
i'm just going to type in my name
11487
06:35:21,760 --> 06:35:24,718
bro i'm going to delete this and all of
11488
06:35:24,718 --> 06:35:27,040
the text is now gone
11489
06:35:27,040 --> 06:35:29,280
just like that let's create a backspace
11490
06:35:29,280 --> 06:35:31,200
button we'll copy what we have for our
11491
06:35:31,200 --> 06:35:32,798
delete button and change
11492
06:35:32,798 --> 06:35:36,558
the word delete to backspace
11493
06:35:37,040 --> 06:35:38,638
make sure you change that when you pack
11494
06:35:38,638 --> 06:35:40,718
this as well and for the text we'll set
11495
06:35:40,718 --> 06:35:41,280
this to
11496
06:35:41,280 --> 06:35:44,000
backspace and we'll create a function
11497
06:35:44,000 --> 06:35:46,718
called backspace
11498
06:35:46,718 --> 06:35:49,280
and let's define what backspace does so
11499
06:35:49,280 --> 06:35:49,760
after
11500
06:35:49,760 --> 06:35:52,798
our delete function let's define back
11501
06:35:52,798 --> 06:35:55,920
space and this is going to do something
11502
06:35:55,920 --> 06:35:58,958
very similar to our delete function
11503
06:35:58,958 --> 06:36:02,320
except we're going to change the first
11504
06:36:02,320 --> 06:36:03,840
positional argument
11505
06:36:03,840 --> 06:36:06,400
we need to place the second to last
11506
06:36:06,400 --> 06:36:06,958
character
11507
06:36:06,958 --> 06:36:09,600
within this positional argument one way
11508
06:36:09,600 --> 06:36:10,798
in which we can do that
11509
06:36:10,798 --> 06:36:13,840
is to first get the entire length of all
11510
06:36:13,840 --> 06:36:16,160
of the characters within our entry box
11511
06:36:16,160 --> 06:36:19,200
so let's begin by typing entry dot
11512
06:36:19,200 --> 06:36:21,920
get and we need to determine the length
11513
06:36:21,920 --> 06:36:22,558
of this
11514
06:36:22,558 --> 06:36:25,600
and we can surround this by placing this
11515
06:36:25,600 --> 06:36:28,478
within the length function so this will
11516
06:36:28,478 --> 06:36:29,040
return
11517
06:36:29,040 --> 06:36:31,360
the current length of all of the
11518
06:36:31,360 --> 06:36:33,280
characters within our entry box
11519
06:36:33,280 --> 06:36:36,160
and then we're going to add -1 so that
11520
06:36:36,160 --> 06:36:36,638
this
11521
06:36:36,638 --> 06:36:38,958
first positional argument is the second
11522
06:36:38,958 --> 06:36:40,240
to last character
11523
06:36:40,240 --> 06:36:42,240
and the second positional argument is
11524
06:36:42,240 --> 06:36:44,080
the end the last character
11525
06:36:44,080 --> 06:36:47,440
and then when we run this let's type in
11526
06:36:47,440 --> 06:36:49,120
our name i'm just going to type in draw
11527
06:36:49,120 --> 06:36:50,798
code this time i'm going to click
11528
06:36:50,798 --> 06:36:52,878
backspace and then it's only going to
11529
06:36:52,878 --> 06:36:53,920
delete the last
11530
06:36:53,920 --> 06:36:55,760
character and then if you were to click
11531
06:36:55,760 --> 06:36:58,958
delete it deletes everything
11532
06:36:58,958 --> 06:37:01,520
broy and then if you click submit it's
11533
06:37:01,520 --> 06:37:03,440
going to submit your name
11534
06:37:03,440 --> 06:37:05,360
all right let's customize the appearance
11535
06:37:05,360 --> 06:37:06,798
of this entry box
11536
06:37:06,798 --> 06:37:09,600
so within the constructor for this entry
11537
06:37:09,600 --> 06:37:10,080
box
11538
06:37:10,080 --> 06:37:13,040
let's change maybe the color maybe the
11539
06:37:13,040 --> 06:37:14,718
background color and foreground color
11540
06:37:14,718 --> 06:37:16,080
let's begin with the foreground color
11541
06:37:16,080 --> 06:37:18,240
you can pass in a color name or a hex
11542
06:37:18,240 --> 06:37:18,798
value
11543
06:37:18,798 --> 06:37:21,040
i want this to be green so the hex value
11544
06:37:21,040 --> 06:37:21,840
for that is
11545
06:37:21,840 --> 06:37:25,440
zero zero f zero ff00 and the foreground
11546
06:37:25,440 --> 06:37:25,920
color
11547
06:37:25,920 --> 06:37:28,240
of the text when we type it in is now
11548
06:37:28,240 --> 06:37:29,680
green
11549
06:37:29,680 --> 06:37:31,200
and we can also change the background
11550
06:37:31,200 --> 06:37:33,440
color that is bg
11551
06:37:33,440 --> 06:37:35,600
we could pass in a color name or a hex
11552
06:37:35,600 --> 06:37:37,200
value i'm just going to say black to
11553
06:37:37,200 --> 06:37:38,638
keep it simple
11554
06:37:38,638 --> 06:37:40,878
and we now have a black background and
11555
06:37:40,878 --> 06:37:42,400
the text color is now
11556
06:37:42,400 --> 06:37:45,680
green here's a few useful features that
11557
06:37:45,680 --> 06:37:47,200
you might be interested in
11558
06:37:47,200 --> 06:37:49,680
let's insert some default text for our
11559
06:37:49,680 --> 06:37:50,638
entry box
11560
06:37:50,638 --> 06:37:53,280
so we can use the insert function entry
11561
06:37:53,280 --> 06:37:54,798
dot insert
11562
06:37:54,798 --> 06:37:56,878
we place a positional argument as well
11563
06:37:56,878 --> 06:37:57,920
as some text
11564
06:37:57,920 --> 06:37:59,840
so for the positional argument let's say
11565
06:37:59,840 --> 06:38:01,760
zero so that's the very beginning
11566
06:38:01,760 --> 06:38:04,400
index zero and let's set the text to
11567
06:38:04,400 --> 06:38:05,280
maybe
11568
06:38:05,280 --> 06:38:08,320
spongebob and then the default text
11569
06:38:08,320 --> 06:38:10,240
within our entry box is now
11570
06:38:10,240 --> 06:38:12,878
spongebob another option available to us
11571
06:38:12,878 --> 06:38:15,200
is that we can set the state
11572
06:38:15,200 --> 06:38:17,440
of this entry box we can set this to
11573
06:38:17,440 --> 06:38:19,040
active or disabled
11574
06:38:19,040 --> 06:38:20,958
let's say that after somebody submits
11575
06:38:20,958 --> 06:38:22,878
our name we want to disable
11576
06:38:22,878 --> 06:38:25,840
this entry box so we can either pass in
11577
06:38:25,840 --> 06:38:27,120
this option
11578
06:38:27,120 --> 06:38:30,080
within our constructor or we can use the
11579
06:38:30,080 --> 06:38:32,320
config function to make any changes
11580
06:38:32,320 --> 06:38:35,360
to the state of our entry box so let's
11581
06:38:35,360 --> 06:38:37,600
say after we submit our username we want
11582
06:38:37,600 --> 06:38:39,760
to disable this entry box
11583
06:38:39,760 --> 06:38:41,520
so let's type in the name of our entry
11584
06:38:41,520 --> 06:38:44,558
box dot config
11585
06:38:44,558 --> 06:38:48,080
and for the option for state let's set
11586
06:38:48,080 --> 06:38:49,200
this to
11587
06:38:49,200 --> 06:38:54,878
disabled and then once we type in a name
11588
06:38:54,878 --> 06:38:57,920
and then click submit our entry box is
11589
06:38:57,920 --> 06:38:58,958
now disabled
11590
06:38:58,958 --> 06:39:02,080
there's also an option called show where
11591
06:39:02,080 --> 06:39:04,160
you can show a certain character in
11592
06:39:04,160 --> 06:39:06,638
place of the normal text on the screen
11593
06:39:06,638 --> 06:39:09,040
so this would be useful for a password
11594
06:39:09,040 --> 06:39:10,798
let's say that we're typing in
11595
06:39:10,798 --> 06:39:13,520
a password but we don't want to display
11596
06:39:13,520 --> 06:39:14,878
the password on the screen
11597
06:39:14,878 --> 06:39:17,440
we can use the show option to show a
11598
06:39:17,440 --> 06:39:19,200
certain character in place
11599
06:39:19,200 --> 06:39:22,000
of our text so you might see bullets or
11600
06:39:22,000 --> 06:39:23,040
an asterisk
11601
06:39:23,040 --> 06:39:24,798
so let's pretend that we're now typing
11602
06:39:24,798 --> 06:39:27,040
in a password so we're going to set
11603
06:39:27,040 --> 06:39:30,240
the option for show and place a
11604
06:39:30,240 --> 06:39:32,080
character that we want to replace each
11605
06:39:32,080 --> 06:39:33,200
character with
11606
06:39:33,200 --> 06:39:35,280
let's say we want to only display
11607
06:39:35,280 --> 06:39:37,040
asterisks when we type in
11608
06:39:37,040 --> 06:39:39,920
some characters and i'm going to type in
11609
06:39:39,920 --> 06:39:41,280
the word password
11610
06:39:41,280 --> 06:39:43,520
but it's only displaying asterisks now
11611
06:39:43,520 --> 06:39:45,200
but if i were to click submit
11612
06:39:45,200 --> 06:39:48,240
it displays our text and i would say
11613
06:39:48,240 --> 06:39:50,718
plain text instead of our hidden text
11614
06:39:50,718 --> 06:39:52,000
because we're only showing
11615
06:39:52,000 --> 06:39:54,000
whatever character that we set so that's
11616
06:39:54,000 --> 06:39:55,760
an option available to you as well
11617
06:39:55,760 --> 06:39:57,760
well everybody that's how to create an
11618
06:39:57,760 --> 06:39:59,760
entry box in python you can submit
11619
06:39:59,760 --> 06:40:00,478
information
11620
06:40:00,478 --> 06:40:03,360
such as a username for example or even a
11621
06:40:03,360 --> 06:40:04,878
password if you set the
11622
06:40:04,878 --> 06:40:07,200
show option available to you if you want
11623
06:40:07,200 --> 06:40:09,200
a copy of all this code i'll post all of
11624
06:40:09,200 --> 06:40:10,798
this in the comments down below
11625
06:40:10,798 --> 06:40:12,798
but yeah that's some of the uses of the
11626
06:40:12,798 --> 06:40:14,958
entry widget in python
11627
06:40:14,958 --> 06:40:16,878
hey how's it going everybody two bro
11628
06:40:16,878 --> 06:40:18,400
here hope you're doing well and in this
11629
06:40:18,400 --> 06:40:19,920
video i'm going to teach you guys how we
11630
06:40:19,920 --> 06:40:21,760
can create and customize our own
11631
06:40:21,760 --> 06:40:23,920
check buttons in python so sit back
11632
06:40:23,920 --> 06:40:27,600
relax and enjoy the show
11633
06:40:27,600 --> 06:40:30,000
all right my fellow bros in this video
11634
06:40:30,000 --> 06:40:31,120
we're going to be creating
11635
06:40:31,120 --> 06:40:33,360
and customizing our own check buttons in
11636
06:40:33,360 --> 06:40:35,520
python i sometimes refer to these as
11637
06:40:35,520 --> 06:40:36,558
check boxes
11638
06:40:36,558 --> 06:40:38,160
so in case i do that's what i'm
11639
06:40:38,160 --> 06:40:40,240
referring to as always we'll need a
11640
06:40:40,240 --> 06:40:41,040
window
11641
06:40:41,040 --> 06:40:44,000
window equals tk followed by window.main
11642
06:40:44,000 --> 06:40:44,718
loop
11643
06:40:44,718 --> 06:40:47,840
and let's call this button just check
11644
06:40:47,840 --> 06:40:50,400
button and in order to create this check
11645
06:40:50,400 --> 06:40:51,040
button
11646
06:40:51,040 --> 06:40:53,040
we type in the name of the check button
11647
06:40:53,040 --> 06:40:54,240
equals
11648
06:40:54,240 --> 06:40:58,240
check button and within the constructor
11649
06:40:58,240 --> 06:41:00,160
what are we adding this check button to
11650
06:41:00,160 --> 06:41:02,000
we're adding this to our window
11651
06:41:02,000 --> 06:41:04,000
and also within the constructor we can
11652
06:41:04,000 --> 06:41:05,760
set the different options
11653
06:41:05,760 --> 06:41:07,840
for how we want to customize this check
11654
06:41:07,840 --> 06:41:10,160
button let's begin with the text
11655
06:41:10,160 --> 06:41:13,840
text equals i agree
11656
06:41:13,840 --> 06:41:17,520
to something and last but not least we
11657
06:41:17,520 --> 06:41:18,320
need to add
11658
06:41:18,320 --> 06:41:20,718
this check button to the window one way
11659
06:41:20,718 --> 06:41:22,000
in which we can do that is
11660
06:41:22,000 --> 06:41:24,798
to use the pack function of this widget
11661
06:41:24,798 --> 06:41:26,080
so follow this with
11662
06:41:26,080 --> 06:41:28,558
check button dot pack when we run this
11663
06:41:28,558 --> 06:41:30,558
we have our check button within our
11664
06:41:30,558 --> 06:41:31,120
window
11665
06:41:31,120 --> 06:41:34,000
and we can toggle this on or off right
11666
06:41:34,000 --> 06:41:35,280
now i'm agreeing to something
11667
06:41:35,280 --> 06:41:37,040
i'm not sure what i'm agreeing to but
11668
06:41:37,040 --> 06:41:38,558
hopefully it's not anything bad
11669
06:41:38,558 --> 06:41:40,400
now currently this doesn't do anything
11670
06:41:40,400 --> 06:41:42,718
so we'll need to associate a variable
11671
06:41:42,718 --> 06:41:43,920
with this check button
11672
06:41:43,920 --> 06:41:46,958
so we'll add that as an option variable
11673
06:41:46,958 --> 06:41:49,200
equals and let's create a variable
11674
06:41:49,200 --> 06:41:50,240
called x
11675
06:41:50,240 --> 06:41:52,080
and we'll need to define this within the
11676
06:41:52,080 --> 06:41:53,760
window so
11677
06:41:53,760 --> 06:41:55,360
after we create our instance of our
11678
06:41:55,360 --> 06:41:57,040
window let's say
11679
06:41:57,040 --> 06:42:00,080
x equals now with check buttons
11680
06:42:00,080 --> 06:42:03,920
they store a 1 or a 0 by default within
11681
06:42:03,920 --> 06:42:04,958
our variable
11682
06:42:04,958 --> 06:42:08,000
our variable that we call x so with
11683
06:42:08,000 --> 06:42:12,160
x we're going to set this equal to a int
11684
06:42:12,160 --> 06:42:15,280
var if this were to return a string we
11685
06:42:15,280 --> 06:42:16,558
would say
11686
06:42:16,558 --> 06:42:19,600
string bar but normally by default this
11687
06:42:19,600 --> 06:42:20,718
returns a one
11688
06:42:20,718 --> 06:42:23,360
or a zero and we can actually change
11689
06:42:23,360 --> 06:42:25,280
that using the on value
11690
06:42:25,280 --> 06:42:28,878
and off value options so the on value
11691
06:42:28,878 --> 06:42:31,440
is what's going to be stored within our
11692
06:42:31,440 --> 06:42:32,320
variable
11693
06:42:32,320 --> 06:42:35,360
if it's toggled on so by default this is
11694
06:42:35,360 --> 06:42:36,000
one
11695
06:42:36,000 --> 06:42:38,958
and the off value is the opposite this
11696
06:42:38,958 --> 06:42:40,160
is what is stored within
11697
06:42:40,160 --> 06:42:43,360
our variable if this is toggled off
11698
06:42:43,360 --> 06:42:44,958
alright now let's have some fun with
11699
06:42:44,958 --> 06:42:47,840
this so let's associate a command with
11700
06:42:47,840 --> 06:42:49,200
this check button
11701
06:42:49,200 --> 06:42:51,440
command equals and let's create a
11702
06:42:51,440 --> 06:42:52,240
function called
11703
06:42:52,240 --> 06:42:55,440
maybe display and then let's define this
11704
06:42:55,440 --> 06:43:00,240
before the window def display and we're
11705
06:43:00,240 --> 06:43:00,958
going to check
11706
06:43:00,958 --> 06:43:04,000
the value of x to see if it's one
11707
06:43:04,000 --> 06:43:07,040
or zero so within our display function
11708
06:43:07,040 --> 06:43:08,478
we're going to check to see
11709
06:43:08,478 --> 06:43:11,600
if x and to get
11710
06:43:11,600 --> 06:43:14,080
the value of x we're going to use the
11711
06:43:14,080 --> 06:43:15,360
get function
11712
06:43:15,360 --> 06:43:19,040
so if x dot get is equal to one
11713
06:43:19,040 --> 06:43:20,878
that means that somebody clicked on the
11714
06:43:20,878 --> 06:43:23,040
check box or toggled it on
11715
06:43:23,040 --> 06:43:26,320
so if they check the check box
11716
06:43:26,320 --> 06:43:29,280
or check the check button then they
11717
06:43:29,280 --> 06:43:30,000
agree
11718
06:43:30,000 --> 06:43:34,718
so print you agree
11719
06:43:34,718 --> 06:43:36,798
if it's zero and we can do this with an
11720
06:43:36,798 --> 06:43:37,920
else statement
11721
06:43:37,920 --> 06:43:41,040
else print
11722
06:43:41,040 --> 06:43:45,360
you don't agree frowny face
11723
06:43:45,360 --> 06:43:48,638
and let's test this theory so we have
11724
06:43:48,638 --> 06:43:49,040
our
11725
06:43:49,040 --> 06:43:51,280
check button when we click on this it's
11726
06:43:51,280 --> 06:43:52,638
actually going to call
11727
06:43:52,638 --> 06:43:54,718
our display function i agree to
11728
06:43:54,718 --> 06:43:56,798
something and it says within our console
11729
06:43:56,798 --> 06:43:57,360
window
11730
06:43:57,360 --> 06:43:59,760
you agree and if i were to toggle this
11731
06:43:59,760 --> 06:44:02,000
off it says you don't agree
11732
06:44:02,000 --> 06:44:03,520
let's change the font of our check
11733
06:44:03,520 --> 06:44:05,680
button so we'll add that as an option
11734
06:44:05,680 --> 06:44:08,478
font equals and we can pass in a font
11735
06:44:08,478 --> 06:44:09,520
that we want
11736
06:44:09,520 --> 06:44:12,638
i'll say maybe ariel and then a size
11737
06:44:12,638 --> 06:44:16,798
of 20 and now this should be readable
11738
06:44:16,798 --> 06:44:19,680
i agree to something let's also color
11739
06:44:19,680 --> 06:44:20,000
this
11740
06:44:20,000 --> 06:44:22,558
because well i like coloring let's begin
11741
06:44:22,558 --> 06:44:24,240
with the foreground color
11742
06:44:24,240 --> 06:44:27,920
so that is fg for short fg equals
11743
06:44:27,920 --> 06:44:30,478
and then we can pass in a color name or
11744
06:44:30,478 --> 06:44:31,760
a hex value
11745
06:44:31,760 --> 06:44:33,520
i like hex values so i'm going to stick
11746
06:44:33,520 --> 06:44:35,680
with those i want a green
11747
06:44:35,680 --> 06:44:39,520
foreground color so that is zero zero f
11748
06:44:39,520 --> 06:44:42,558
zero zero but feel free to adjust these
11749
06:44:42,558 --> 06:44:44,080
values however you want
11750
06:44:44,080 --> 06:44:47,200
so our text should now be green that is
11751
06:44:47,200 --> 06:44:47,440
a
11752
06:44:47,440 --> 06:44:49,680
very bright green and it hurts my eyes
11753
06:44:49,680 --> 06:44:51,200
so i'm also going to change the
11754
06:44:51,200 --> 06:44:51,760
background
11755
06:44:51,760 --> 06:44:54,160
color which is bg and i'm going to set
11756
06:44:54,160 --> 06:44:54,798
this to
11757
06:44:54,798 --> 06:44:57,680
black so we'll have green text on a
11758
06:44:57,680 --> 06:44:59,760
black background
11759
06:44:59,760 --> 06:45:01,760
okay now when you click on this it
11760
06:45:01,760 --> 06:45:03,920
flashes that's kind of annoying
11761
06:45:03,920 --> 06:45:06,320
that's the active foreground and active
11762
06:45:06,320 --> 06:45:07,040
background
11763
06:45:07,040 --> 06:45:09,520
if we click on this or hold this down
11764
06:45:09,520 --> 06:45:11,360
the color scheme switches to
11765
06:45:11,360 --> 06:45:13,760
our active foreground slash background
11766
06:45:13,760 --> 06:45:15,600
and we can change those too
11767
06:45:15,600 --> 06:45:19,200
so i'm going to set those options active
11768
06:45:19,200 --> 06:45:21,520
foreground i'm going to set this the
11769
06:45:21,520 --> 06:45:25,280
same as my foreground color
11770
06:45:25,280 --> 06:45:27,840
and the active background will be the
11771
06:45:27,840 --> 06:45:28,558
same as
11772
06:45:28,558 --> 06:45:31,680
our normal background so that's active
11773
06:45:31,680 --> 06:45:35,040
background equals
11774
06:45:35,040 --> 06:45:38,400
black and this should no longer flash
11775
06:45:38,400 --> 06:45:41,280
when we click on it
11776
06:45:41,280 --> 06:45:43,520
not too bad not too bad let's add some
11777
06:45:43,520 --> 06:45:45,680
padding
11778
06:45:45,680 --> 06:45:49,520
pad x i'll set this to 25
11779
06:45:49,520 --> 06:45:52,558
and pad y i'll set this to
11780
06:45:52,558 --> 06:45:57,200
10. we have a little more padding around
11781
06:45:57,200 --> 06:45:59,600
our check button one feature that's
11782
06:45:59,600 --> 06:46:00,558
available to us
11783
06:46:00,558 --> 06:46:03,360
is that we can add an image next to our
11784
06:46:03,360 --> 06:46:04,240
check button
11785
06:46:04,240 --> 06:46:07,360
i downloaded an image of the python logo
11786
06:46:07,360 --> 06:46:08,718
that i want to add
11787
06:46:08,718 --> 06:46:12,320
so we'll use the image option to add a
11788
06:46:12,320 --> 06:46:13,200
photo image
11789
06:46:13,200 --> 06:46:15,600
to this check button but first we need
11790
06:46:15,600 --> 06:46:16,638
to create a
11791
06:46:16,638 --> 06:46:19,680
photo image from our image file so
11792
06:46:19,680 --> 06:46:21,280
within the window
11793
06:46:21,280 --> 06:46:24,478
let's call this python photo
11794
06:46:24,478 --> 06:46:25,920
but you'll probably use something
11795
06:46:25,920 --> 06:46:27,680
different for your project
11796
06:46:27,680 --> 06:46:31,040
python photo equals photo
11797
06:46:31,040 --> 06:46:33,920
image and within the constructor for our
11798
06:46:33,920 --> 06:46:36,000
photo image
11799
06:46:36,000 --> 06:46:38,558
we can set the file path or the file
11800
06:46:38,558 --> 06:46:39,280
name
11801
06:46:39,280 --> 06:46:41,760
since this image is within my project
11802
06:46:41,760 --> 06:46:44,478
folder i only have to list the file name
11803
06:46:44,478 --> 06:46:49,200
and this photo of mine is python.png
11804
06:46:49,200 --> 06:46:52,878
and now we set the image equal to
11805
06:46:52,878 --> 06:46:55,520
our photo image that we have image
11806
06:46:55,520 --> 06:46:56,478
equals
11807
06:46:56,478 --> 06:46:59,600
python photo but when we run this
11808
06:46:59,600 --> 06:47:02,160
it's going to overlap the text we still
11809
06:47:02,160 --> 06:47:04,000
have our image though
11810
06:47:04,000 --> 06:47:06,878
and this still works as intended so in
11811
06:47:06,878 --> 06:47:08,718
order to display
11812
06:47:08,718 --> 06:47:11,200
the image as well as the text we're
11813
06:47:11,200 --> 06:47:13,120
going to need to use the compound
11814
06:47:13,120 --> 06:47:15,920
option and let's add that at the end
11815
06:47:15,920 --> 06:47:17,200
compound
11816
06:47:17,200 --> 06:47:19,600
is where we're adding this photo
11817
06:47:19,600 --> 06:47:21,040
relative to the text
11818
06:47:21,040 --> 06:47:23,200
i want this photo on the left hand side
11819
06:47:23,200 --> 06:47:24,240
of the text
11820
06:47:24,240 --> 06:47:27,040
so i'm going to set this to left and now
11821
06:47:27,040 --> 06:47:28,160
when we run this
11822
06:47:28,160 --> 06:47:30,400
surprise surprise our image is on the
11823
06:47:30,400 --> 06:47:31,600
left hand side
11824
06:47:31,600 --> 06:47:33,120
one thing that you should know is that
11825
06:47:33,120 --> 06:47:35,520
with the on value and the off value data
11826
06:47:35,520 --> 06:47:37,440
type if you were to change this
11827
06:47:37,440 --> 06:47:40,160
to a non-integer value let's say we're
11828
06:47:40,160 --> 06:47:42,400
going to replace the on value with a
11829
06:47:42,400 --> 06:47:45,600
boolean value of true and the off value
11830
06:47:45,600 --> 06:47:49,360
is false with this variable make sure to
11831
06:47:49,360 --> 06:47:52,000
change the data type as well to reflect
11832
06:47:52,000 --> 06:47:53,200
what you're storing within
11833
06:47:53,200 --> 06:47:56,478
it so with our int var variable we want
11834
06:47:56,478 --> 06:47:57,920
to change this to a
11835
06:47:57,920 --> 06:48:01,360
boolean var value so replace int
11836
06:48:01,360 --> 06:48:04,080
with boolean and this will work much the
11837
06:48:04,080 --> 06:48:04,798
same
11838
06:48:04,798 --> 06:48:06,798
now with this display function we'll
11839
06:48:06,798 --> 06:48:08,400
need to account for that
11840
06:48:08,400 --> 06:48:10,718
so within the if statement here we'll
11841
06:48:10,718 --> 06:48:11,840
say if x
11842
06:48:11,840 --> 06:48:14,718
dot get we don't need to say is equal to
11843
06:48:14,718 --> 06:48:15,280
one
11844
06:48:15,280 --> 06:48:18,400
this will return true or false and this
11845
06:48:18,400 --> 06:48:19,040
will work
11846
06:48:19,040 --> 06:48:21,360
just the same as it did before and if
11847
06:48:21,360 --> 06:48:23,200
you replace this with a string
11848
06:48:23,200 --> 06:48:26,240
let's say the on value is
11849
06:48:26,240 --> 06:48:30,240
yes all caps and the off value is
11850
06:48:30,240 --> 06:48:34,638
no this is now a string bar
11851
06:48:34,638 --> 06:48:36,798
and we'll change our display function to
11852
06:48:36,798 --> 06:48:38,080
say if x
11853
06:48:38,080 --> 06:48:43,040
dot get is equal to yes
11854
06:48:43,760 --> 06:48:46,080
and this should work you agree you don't
11855
06:48:46,080 --> 06:48:47,280
agree you agree
11856
06:48:47,280 --> 06:48:50,558
you don't agree and i'll change that
11857
06:48:50,558 --> 06:48:51,680
back to one so
11858
06:48:51,680 --> 06:48:53,920
one and zero is the default for this but
11859
06:48:53,920 --> 06:48:55,520
you can edit these based on what you
11860
06:48:55,520 --> 06:48:56,478
need
11861
06:48:56,478 --> 06:48:58,718
all right so that's the basics of check
11862
06:48:58,718 --> 06:49:00,878
buttons in python if you want a copy of
11863
06:49:00,878 --> 06:49:01,840
all this code
11864
06:49:01,840 --> 06:49:03,360
i'll post all of this in the comments
11865
06:49:03,360 --> 06:49:05,920
down below but yeah that's the basics of
11866
06:49:05,920 --> 06:49:08,638
check buttons in python hey what's going
11867
06:49:08,638 --> 06:49:10,080
on everybody it's your bro here hope
11868
06:49:10,080 --> 06:49:11,520
you're doing well and in this video i'm
11869
06:49:11,520 --> 06:49:12,958
going to be teaching you guys how we can
11870
06:49:12,958 --> 06:49:15,360
create radio buttons in python so sit
11871
06:49:15,360 --> 06:49:15,680
back
11872
06:49:15,680 --> 06:49:19,200
relax and enjoy the show
11873
06:49:19,200 --> 06:49:21,200
okay everybody let's create some radio
11874
06:49:21,200 --> 06:49:23,600
buttons now these are similar to check
11875
06:49:23,600 --> 06:49:25,840
boxes but you can only select one from
11876
06:49:25,840 --> 06:49:27,360
any single grouping
11877
06:49:27,360 --> 06:49:29,040
so let's pretend that i'm buying you
11878
06:49:29,040 --> 06:49:30,798
lunch and you can only select
11879
06:49:30,798 --> 06:49:33,120
one item from a menu you can select
11880
06:49:33,120 --> 06:49:33,840
either a
11881
06:49:33,840 --> 06:49:37,200
slice of pizza a hamburger or a hot dog
11882
06:49:37,200 --> 06:49:39,600
so let's begin by creating a list i will
11883
06:49:39,600 --> 06:49:40,558
call this list
11884
06:49:40,558 --> 06:49:44,478
food food equals
11885
06:49:44,840 --> 06:49:48,878
pizza hamburger
11886
06:49:48,958 --> 06:49:52,558
hot dog and let's create a window
11887
06:49:52,558 --> 06:49:55,920
window equals tk followed by
11888
06:49:55,920 --> 06:49:59,680
window dot main loop
11889
06:49:59,680 --> 06:50:01,600
so to create a radio button we'll need a
11890
06:50:01,600 --> 06:50:04,240
name so i'll call this radio button
11891
06:50:04,240 --> 06:50:08,320
radio button equals radio button
11892
06:50:08,320 --> 06:50:10,320
and we're going to be adding this to our
11893
06:50:10,320 --> 06:50:12,320
window and we're going to put this
11894
06:50:12,320 --> 06:50:12,878
within
11895
06:50:12,878 --> 06:50:15,280
a for loop so we're going to iterate
11896
06:50:15,280 --> 06:50:17,920
through all of the items within our list
11897
06:50:17,920 --> 06:50:20,558
so let's create a for loop so we'll say
11898
06:50:20,558 --> 06:50:22,000
for
11899
06:50:22,000 --> 06:50:26,878
index in range
11900
06:50:26,878 --> 06:50:30,400
length of food
11901
06:50:30,400 --> 06:50:32,958
so this is going to iterate once through
11902
06:50:32,958 --> 06:50:35,520
all of the elements within our list
11903
06:50:35,520 --> 06:50:38,240
so when we run this it's going to create
11904
06:50:38,240 --> 06:50:38,638
three
11905
06:50:38,638 --> 06:50:40,958
radio buttons for us because we're
11906
06:50:40,958 --> 06:50:42,080
instantiating
11907
06:50:42,080 --> 06:50:44,080
one radio button for each item in the
11908
06:50:44,080 --> 06:50:45,200
list
11909
06:50:45,200 --> 06:50:48,718
now what i'm going to do is set the text
11910
06:50:48,718 --> 06:50:51,760
equal to
11911
06:50:51,760 --> 06:50:55,520
our list of food at a certain index
11912
06:50:55,520 --> 06:50:57,920
so when we first run this for loop it's
11913
06:50:57,920 --> 06:51:00,000
going to be zero for the index
11914
06:51:00,000 --> 06:51:03,120
then one and then two and then so on and
11915
06:51:03,120 --> 06:51:06,400
so forth for each item within our list
11916
06:51:06,400 --> 06:51:09,360
and then we just need to pack this so
11917
06:51:09,360 --> 06:51:10,478
radio
11918
06:51:10,478 --> 06:51:14,160
button dot pack
11919
06:51:14,160 --> 06:51:17,520
and then let's take a look at this
11920
06:51:17,520 --> 06:51:19,520
alright so here are all of the radio
11921
06:51:19,520 --> 06:51:20,878
buttons that we have
11922
06:51:20,878 --> 06:51:22,718
currently they're all selected though so
11923
06:51:22,718 --> 06:51:25,040
we'll need to link these within a single
11924
06:51:25,040 --> 06:51:25,760
grouping
11925
06:51:25,760 --> 06:51:28,160
the next thing we'll add is a variable
11926
06:51:28,160 --> 06:51:29,600
and we'll set this equal to
11927
06:51:29,600 --> 06:51:31,760
x and we'll need to declare this after
11928
06:51:31,760 --> 06:51:33,760
we create an instance of our window
11929
06:51:33,760 --> 06:51:37,040
so x equals int var
11930
06:51:37,040 --> 06:51:40,558
so this will hold an integer object
11931
06:51:40,558 --> 06:51:42,958
so these are all within the same
11932
06:51:42,958 --> 06:51:43,680
grouping
11933
06:51:43,680 --> 06:51:45,920
however if we try to run this again and
11934
06:51:45,920 --> 06:51:47,920
select one of these options
11935
06:51:47,920 --> 06:51:50,000
they all become selected so we're going
11936
06:51:50,000 --> 06:51:51,120
to need to give
11937
06:51:51,120 --> 06:51:53,600
each of these individual radio buttons
11938
06:51:53,600 --> 06:51:54,400
their own
11939
06:51:54,400 --> 06:51:56,320
value because right now they're all
11940
06:51:56,320 --> 06:51:57,920
sharing the same value
11941
06:51:57,920 --> 06:52:01,280
so we can add value equals
11942
06:52:01,280 --> 06:52:04,240
whatever the index is currently so the
11943
06:52:04,240 --> 06:52:04,718
first
11944
06:52:04,718 --> 06:52:07,280
item within our list of food is going to
11945
06:52:07,280 --> 06:52:07,920
have an
11946
06:52:07,920 --> 06:52:11,120
index value of zero then one
11947
06:52:11,120 --> 06:52:14,320
then two so let's try that again and you
11948
06:52:14,320 --> 06:52:15,760
can see by default it's
11949
06:52:15,760 --> 06:52:18,320
zero so pizza is already selected then
11950
06:52:18,320 --> 06:52:20,400
we can select either hamburger or hot
11951
06:52:20,400 --> 06:52:20,878
dog
11952
06:52:20,878 --> 06:52:23,360
but we can only select one so that's
11953
06:52:23,360 --> 06:52:25,120
kind of what we want then
11954
06:52:25,120 --> 06:52:27,360
so do you guys usually know how i call
11955
06:52:27,360 --> 06:52:29,280
the config function after each change
11956
06:52:29,280 --> 06:52:30,638
that i make for an object
11957
06:52:30,638 --> 06:52:32,160
well that's because i like to add
11958
06:52:32,160 --> 06:52:34,400
comments just for teaching purposes
11959
06:52:34,400 --> 06:52:37,120
for what each change does however it
11960
06:52:37,120 --> 06:52:38,718
might be easier for you instead of
11961
06:52:38,718 --> 06:52:40,558
calling the config function a bunch of
11962
06:52:40,558 --> 06:52:41,200
times
11963
06:52:41,200 --> 06:52:43,840
just to list all of the changes within
11964
06:52:43,840 --> 06:52:45,280
the parentheses when you create an
11965
06:52:45,280 --> 06:52:46,400
instance of the
11966
06:52:46,400 --> 06:52:48,240
object that you're working with however
11967
06:52:48,240 --> 06:52:50,320
just for teaching purposes i'm going to
11968
06:52:50,320 --> 06:52:52,080
put this all within
11969
06:52:52,080 --> 06:52:54,080
separate lines that allows me to add
11970
06:52:54,080 --> 06:52:55,120
some comments
11971
06:52:55,120 --> 06:52:56,798
so i think i'm going to start doing that
11972
06:52:56,798 --> 06:52:59,280
from now on uh so what this does
11973
06:52:59,280 --> 06:53:02,400
is that this adds text to
11974
06:53:02,400 --> 06:53:06,400
radio buttons and this
11975
06:53:06,400 --> 06:53:10,798
groups radio buttons together
11976
06:53:10,798 --> 06:53:16,478
if they share the same variable
11977
06:53:16,478 --> 06:53:18,478
so if you wanted a completely different
11978
06:53:18,478 --> 06:53:20,400
grouping of radio buttons you can give
11979
06:53:20,400 --> 06:53:21,760
them a different variable
11980
06:53:21,760 --> 06:53:25,440
like y and what this line does
11981
06:53:25,440 --> 06:53:28,478
is that this assigns
11982
06:53:28,478 --> 06:53:32,718
each radio button a
11983
06:53:32,718 --> 06:53:37,920
different value
11984
06:53:37,920 --> 06:53:39,840
all right so let's make some cosmetic
11985
06:53:39,840 --> 06:53:42,558
changes to our radio buttons
11986
06:53:42,558 --> 06:53:45,680
so right now they are centered let's
11987
06:53:45,680 --> 06:53:47,520
anchor these to the west
11988
06:53:47,520 --> 06:53:49,440
so we'll place this within the pack
11989
06:53:49,440 --> 06:53:50,798
function so
11990
06:53:50,798 --> 06:53:54,718
anchor equals w alternatively you can
11991
06:53:54,718 --> 06:53:56,160
just say capital w
11992
06:53:56,160 --> 06:53:58,478
without the quotes this would also work
11993
06:53:58,478 --> 06:54:00,000
too
11994
06:54:00,000 --> 06:54:01,840
so now they're all lined up let's add
11995
06:54:01,840 --> 06:54:03,680
some padding
11996
06:54:03,680 --> 06:54:06,958
so i'm going to add pad x
11997
06:54:06,958 --> 06:54:10,160
and i'll set this to 25
11998
06:54:10,160 --> 06:54:13,600
and this adds
11999
06:54:13,600 --> 06:54:17,040
padding on x
12000
06:54:17,040 --> 06:54:19,520
axis
12001
06:54:20,240 --> 06:54:23,440
let's also change the font
12002
06:54:23,840 --> 06:54:27,840
so we'll save font
12003
06:54:27,840 --> 06:54:31,440
equals maybe i'll pick impact for a font
12004
06:54:31,440 --> 06:54:32,558
style
12005
06:54:32,558 --> 06:54:36,718
and set the font size to 50
12006
06:54:36,718 --> 06:54:38,558
and now we can actually read these
12007
06:54:38,558 --> 06:54:40,638
because the font is large enough
12008
06:54:40,638 --> 06:54:43,440
now let's add some images so let's
12009
06:54:43,440 --> 06:54:44,958
create some photo images
12010
06:54:44,958 --> 06:54:47,200
you'll want to be sure to create these
12011
06:54:47,200 --> 06:54:49,120
after you create your instance of your
12012
06:54:49,120 --> 06:54:50,240
window
12013
06:54:50,240 --> 06:54:53,040
so let's create a pizza image first
12014
06:54:53,040 --> 06:54:54,080
pizza image
12015
06:54:54,080 --> 06:54:58,160
equals photo image
12016
06:54:58,160 --> 06:55:01,120
file equals the file path or the file
12017
06:55:01,120 --> 06:55:01,840
name
12018
06:55:01,840 --> 06:55:04,878
and my file is pizza.png and it kind of
12019
06:55:04,878 --> 06:55:05,920
looks like this
12020
06:55:05,920 --> 06:55:10,120
it's just the pizza emoji so
12021
06:55:10,120 --> 06:55:12,558
pizza.png
12022
06:55:12,558 --> 06:55:14,320
and then i'll do the same thing for
12023
06:55:14,320 --> 06:55:16,798
hamburger and hotdog
12024
06:55:16,798 --> 06:55:20,878
so this will be hamburger image
12025
06:55:20,878 --> 06:55:25,360
file equals hamburger.png
12026
06:55:25,360 --> 06:55:28,638
and then hotdog image
12027
06:55:35,280 --> 06:55:37,840
and this is what the other emojis look
12028
06:55:37,840 --> 06:55:40,080
like
12029
06:55:40,558 --> 06:55:42,638
and then let's create a list of the
12030
06:55:42,638 --> 06:55:44,240
photo images that we have
12031
06:55:44,240 --> 06:55:47,600
i'll call this food images
12032
06:55:47,600 --> 06:55:52,240
equals than the names of these images
12033
06:55:52,240 --> 06:55:56,638
pizza image hamburger image
12034
06:55:56,638 --> 06:55:59,840
and hot dog image
12035
06:56:01,040 --> 06:56:03,600
so then we can set the image for each
12036
06:56:03,600 --> 06:56:05,200
radio button
12037
06:56:05,200 --> 06:56:08,478
image equals
12038
06:56:08,478 --> 06:56:11,440
the name of the list food images
12039
06:56:11,440 --> 06:56:12,320
followed by
12040
06:56:12,320 --> 06:56:15,040
the index that we're currently on within
12041
06:56:15,040 --> 06:56:16,878
our for loop
12042
06:56:16,878 --> 06:56:20,320
so this adds image
12043
06:56:20,320 --> 06:56:23,520
to radio button and
12044
06:56:23,520 --> 06:56:27,920
let's test this cool here's our images
12045
06:56:27,920 --> 06:56:30,160
now if you want some images as well as
12046
06:56:30,160 --> 06:56:32,958
text you'll have to use compound
12047
06:56:32,958 --> 06:56:36,400
so let's add that next so i'm just going
12048
06:56:36,400 --> 06:56:37,360
to add a comma
12049
06:56:37,360 --> 06:56:41,840
followed by compound
12050
06:56:43,040 --> 06:56:46,878
equals let's say left so this will add
12051
06:56:46,878 --> 06:56:47,760
the image
12052
06:56:47,760 --> 06:56:51,200
to the left of the text
12053
06:56:52,638 --> 06:56:56,400
adds image and text i'll just say left
12054
06:56:56,400 --> 06:56:58,638
side
12055
06:56:59,120 --> 06:57:02,160
so this will display both an image and
12056
06:57:02,160 --> 06:57:04,320
text
12057
06:57:04,320 --> 06:57:06,400
so with these radio buttons you can
12058
06:57:06,400 --> 06:57:07,600
eliminate these
12059
06:57:07,600 --> 06:57:10,000
circle indicators and there's actually a
12060
06:57:10,000 --> 06:57:11,360
way to do that
12061
06:57:11,360 --> 06:57:13,680
so what we'll add if you want to get rid
12062
06:57:13,680 --> 06:57:14,718
of those
12063
06:57:14,718 --> 06:57:18,320
is indicator
12064
06:57:18,320 --> 06:57:21,600
on equals zero
12065
06:57:21,600 --> 06:57:27,600
so this will i should say eliminate
12066
06:57:27,600 --> 06:57:31,520
circle indicators
12067
06:57:32,000 --> 06:57:34,160
so it's going to change these to these
12068
06:57:34,160 --> 06:57:35,600
push buttons
12069
06:57:35,600 --> 06:57:38,160
however they are going to be of uneven
12070
06:57:38,160 --> 06:57:40,080
size you can set the width
12071
06:57:40,080 --> 06:57:43,520
if you want to so we'll do that
12072
06:57:43,520 --> 06:57:47,040
so we'll say width equals
12073
06:57:47,040 --> 06:57:50,558
what about 75 i'm not sure if that's a
12074
06:57:50,558 --> 06:57:52,638
good size or not
12075
06:57:52,638 --> 06:57:57,840
okay that's way too small let's try 375
12076
06:57:59,360 --> 06:58:00,878
so yeah you can have some push buttons
12077
06:58:00,878 --> 06:58:02,478
if you want to if you don't like the
12078
06:58:02,478 --> 06:58:04,558
circle indicators
12079
06:58:04,558 --> 06:58:07,840
so this sets width
12080
06:58:07,840 --> 06:58:11,200
of radio buttons
12081
06:58:11,200 --> 06:58:14,240
now let's actually set a function to
12082
06:58:14,240 --> 06:58:16,878
uh be called when we click one of these
12083
06:58:16,878 --> 06:58:18,240
buttons
12084
06:58:18,240 --> 06:58:20,558
so let's call this function maybe order
12085
06:58:20,558 --> 06:58:24,000
like we're ordering one of these items
12086
06:58:24,000 --> 06:58:27,200
so def we'll call this order like we're
12087
06:58:27,200 --> 06:58:29,760
ordering something
12088
06:58:29,760 --> 06:58:31,440
and what we'll do is just a few if
12089
06:58:31,440 --> 06:58:33,760
statements because i'm feeling lazy
12090
06:58:33,760 --> 06:58:37,280
so we'll just say if x that's
12091
06:58:37,280 --> 06:58:40,320
the value that's going to be stored
12092
06:58:40,320 --> 06:58:42,958
because each of these radio buttons is
12093
06:58:42,958 --> 06:58:45,680
grouped together by the variable x
12094
06:58:45,680 --> 06:58:48,798
if x dot get
12095
06:58:48,798 --> 06:58:52,320
is equal to zero that's our first index
12096
06:58:52,320 --> 06:58:57,040
for pizza what we'll display is
12097
06:58:57,040 --> 06:59:01,280
print you
12098
06:59:01,280 --> 06:59:04,840
oops you ordered
12099
06:59:04,840 --> 06:59:08,558
pizza then we'll just add
12100
06:59:08,558 --> 06:59:12,878
else if x dot get
12101
06:59:12,878 --> 06:59:17,920
is equal to one we will print
12102
06:59:17,920 --> 06:59:23,120
you ordered a hamburger
12103
06:59:25,760 --> 06:59:30,240
and then else if two
12104
06:59:30,798 --> 06:59:35,840
you ordered a hot dog
12105
06:59:37,280 --> 06:59:39,680
else
12106
06:59:40,718 --> 06:59:44,718
print huh
12107
06:59:44,718 --> 06:59:46,878
so there's probably a more efficient way
12108
06:59:46,878 --> 06:59:48,000
to write this however
12109
06:59:48,000 --> 06:59:49,760
i was just feeling lazy and this is
12110
06:59:49,760 --> 06:59:51,760
probably the best way to keep this
12111
06:59:51,760 --> 06:59:53,680
simple and easy to understand
12112
06:59:53,680 --> 06:59:55,920
so let's try this now oh however we need
12113
06:59:55,920 --> 06:59:57,920
to set the command
12114
06:59:57,920 --> 07:00:00,958
for our radio buttons so
12115
07:00:00,958 --> 07:00:04,320
command equals then the
12116
07:00:04,320 --> 07:00:07,520
function name which is order and make
12117
07:00:07,520 --> 07:00:10,080
sure you do not add the parentheses
12118
07:00:10,080 --> 07:00:13,440
so don't do this do that
12119
07:00:13,440 --> 07:00:16,478
and this will set
12120
07:00:16,478 --> 07:00:19,520
command of
12121
07:00:19,520 --> 07:00:24,958
radio button to function
12122
07:00:25,520 --> 07:00:28,320
all right let's test this so if we click
12123
07:00:28,320 --> 07:00:30,878
on pizza it says you ordered pizza
12124
07:00:30,878 --> 07:00:33,040
you ordered a hamburger and you ordered
12125
07:00:33,040 --> 07:00:35,200
a hot dog
12126
07:00:35,200 --> 07:00:37,440
okay everybody so that's the basics of
12127
07:00:37,440 --> 07:00:39,040
radio buttons if you would like a copy
12128
07:00:39,040 --> 07:00:40,718
of all this code i'll post all of this
12129
07:00:40,718 --> 07:00:42,160
in the comments down below
12130
07:00:42,160 --> 07:00:44,400
but yeah that's the basics of radio
12131
07:00:44,400 --> 07:00:46,958
buttons in python
12132
07:00:46,958 --> 07:00:48,478
hey what's going on everybody it's you
12133
07:00:48,478 --> 07:00:50,320
bro here hope you're doing well and in
12134
07:00:50,320 --> 07:00:51,680
this video i'm going to teach you guys
12135
07:00:51,680 --> 07:00:52,558
how we can create
12136
07:00:52,558 --> 07:00:55,520
a sliding scale in python so sit back
12137
07:00:55,520 --> 07:00:59,200
relax and enjoy the show
12138
07:00:59,200 --> 07:01:00,878
let's create a scale so we'll need a
12139
07:01:00,878 --> 07:01:03,360
window window equals tk
12140
07:01:03,360 --> 07:01:06,558
followed by window.mainloop we'll create
12141
07:01:06,558 --> 07:01:07,440
a scale
12142
07:01:07,440 --> 07:01:10,558
called scale scale equals scale
12143
07:01:10,558 --> 07:01:12,798
we need to add the scale to our window
12144
07:01:12,798 --> 07:01:14,400
so we'll put that here
12145
07:01:14,400 --> 07:01:17,360
we can list a from position and a 2
12146
07:01:17,360 --> 07:01:18,240
position
12147
07:01:18,240 --> 07:01:20,718
both of these values will be our range
12148
07:01:20,718 --> 07:01:22,240
of values that we can use
12149
07:01:22,240 --> 07:01:25,840
for our scale so from
12150
07:01:25,840 --> 07:01:28,080
sure to add an underscore after from it
12151
07:01:28,080 --> 07:01:30,638
won't work without this underscore
12152
07:01:30,638 --> 07:01:34,878
from equals let's say 0 and 2 equals 100
12153
07:01:34,878 --> 07:01:36,558
so we're going to create a scale for
12154
07:01:36,558 --> 07:01:37,920
temperature
12155
07:01:37,920 --> 07:01:41,120
and this will be in celsius so 0 will be
12156
07:01:41,120 --> 07:01:43,360
freezing and 100 will be the boiling
12157
07:01:43,360 --> 07:01:45,520
point uh you know for water
12158
07:01:45,520 --> 07:01:48,160
so then to display the scale we'll need
12159
07:01:48,160 --> 07:01:48,798
to add
12160
07:01:48,798 --> 07:01:52,000
scale.pack and let's run this
12161
07:01:52,000 --> 07:01:54,638
so the front position is zero so that's
12162
07:01:54,638 --> 07:01:56,718
at the top here and then if you go down
12163
07:01:56,718 --> 07:01:58,958
it increases so you can actually flip
12164
07:01:58,958 --> 07:02:01,160
this so we'll save from
12165
07:02:01,160 --> 07:02:04,878
102 will be zero
12166
07:02:04,878 --> 07:02:06,798
so now you can scroll up and the number
12167
07:02:06,798 --> 07:02:08,878
will increase
12168
07:02:08,878 --> 07:02:12,000
let's also get the current value so
12169
07:02:12,000 --> 07:02:14,718
we'll need a button
12170
07:02:14,878 --> 07:02:18,400
so button equals button
12171
07:02:18,400 --> 07:02:20,638
we'll add this to the window we'll add
12172
07:02:20,638 --> 07:02:22,400
some text
12173
07:02:22,400 --> 07:02:26,240
text equals submit and then a command
12174
07:02:26,240 --> 07:02:29,360
command equals submit but we'll have to
12175
07:02:29,360 --> 07:02:31,440
make this function
12176
07:02:31,440 --> 07:02:34,958
so let's do that outside the window def
12177
07:02:34,958 --> 07:02:38,320
submit and we will simply print the
12178
07:02:38,320 --> 07:02:39,920
current temperature
12179
07:02:39,920 --> 07:02:43,840
so let's display message the temperature
12180
07:02:43,840 --> 07:02:47,280
is plus
12181
07:02:47,280 --> 07:02:51,600
scale dot get
12182
07:02:51,600 --> 07:02:55,840
plus degrees
12183
07:02:55,840 --> 07:02:59,120
celsius we can just say degree c
12184
07:02:59,120 --> 07:03:01,520
so let's try this uh but we also have to
12185
07:03:01,520 --> 07:03:05,840
pack the button so button.pack
12186
07:03:07,120 --> 07:03:10,320
so if we were to try and submit this
12187
07:03:10,320 --> 07:03:12,558
right now scale.get is going to return
12188
07:03:12,558 --> 07:03:13,360
an integer
12189
07:03:13,360 --> 07:03:16,240
so we need to convert this to a string
12190
07:03:16,240 --> 07:03:16,840
so
12191
07:03:16,840 --> 07:03:20,400
str scale.get
12192
07:03:20,400 --> 07:03:22,000
and you have to do that with python if
12193
07:03:22,000 --> 07:03:23,760
you're displaying a
12194
07:03:23,760 --> 07:03:26,638
integer value along with another string
12195
07:03:26,638 --> 07:03:29,760
uh you need to convert it to a string
12196
07:03:29,760 --> 07:03:33,120
so now this should work so it is 82
12197
07:03:33,120 --> 07:03:35,520
degrees celsius
12198
07:03:35,520 --> 07:03:37,760
so let's do some cosmetic changes for
12199
07:03:37,760 --> 07:03:39,680
our temperature gauge
12200
07:03:39,680 --> 07:03:43,040
because right now it's kind of ugly
12201
07:03:43,120 --> 07:03:46,320
so the first thing we'll change is
12202
07:03:46,320 --> 07:03:49,680
let's say the length so length equals
12203
07:03:49,680 --> 07:03:54,478
600 so that should be fairly large
12204
07:03:56,000 --> 07:03:59,200
you can also set the orientation so that
12205
07:03:59,200 --> 07:03:59,760
is
12206
07:03:59,760 --> 07:04:04,320
orient spelt like that
12207
07:04:04,320 --> 07:04:06,558
so this can either be vertical which it
12208
07:04:06,558 --> 07:04:09,120
currently is
12209
07:04:09,200 --> 07:04:15,440
or horizontal
12210
07:04:15,440 --> 07:04:19,040
but i'll keep this as vertical
12211
07:04:19,840 --> 07:04:22,000
so i'll let a comment that this is
12212
07:04:22,000 --> 07:04:23,360
orientation
12213
07:04:23,360 --> 07:04:27,520
of scale let's also change the
12214
07:04:27,520 --> 07:04:30,798
font so font equals
12215
07:04:30,798 --> 07:04:34,080
pick whatever font you want
12216
07:04:34,320 --> 07:04:39,040
and a size so maybe 20.
12217
07:04:41,840 --> 07:04:45,680
now you can add some tick intervals
12218
07:04:46,798 --> 07:04:50,160
maybe i'll set this to 10. so these will
12219
07:04:50,160 --> 07:04:50,798
be
12220
07:04:50,798 --> 07:04:57,840
numeric indicators on the scale
12221
07:05:00,400 --> 07:05:04,240
and show value this hides the current
12222
07:05:04,240 --> 07:05:04,718
value
12223
07:05:04,718 --> 07:05:06,798
so if you set this to zero this will
12224
07:05:06,798 --> 07:05:07,840
hide
12225
07:05:07,840 --> 07:05:11,840
current value
12226
07:05:12,558 --> 07:05:14,080
so it no longer displays what the
12227
07:05:14,080 --> 07:05:17,200
current value is next to the slider
12228
07:05:17,200 --> 07:05:19,920
to actually retrieve it you'd have to
12229
07:05:19,920 --> 07:05:20,958
get the value
12230
07:05:20,958 --> 07:05:22,798
so right now it's 40 but it helps that
12231
07:05:22,798 --> 07:05:24,320
you have the tick intervals on the
12232
07:05:24,320 --> 07:05:26,878
left-hand side
12233
07:05:26,878 --> 07:05:31,040
i should probably add that this adds
12234
07:05:31,440 --> 07:05:34,478
uh numeric
12235
07:05:34,478 --> 07:05:38,000
indicators for
12236
07:05:38,000 --> 07:05:41,920
value good enough let's continue on
12237
07:05:41,920 --> 07:05:44,000
now let's use the set function we can
12238
07:05:44,000 --> 07:05:46,320
set the current value of the knob
12239
07:05:46,320 --> 07:05:49,120
on our scale and by default it is zero
12240
07:05:49,120 --> 07:05:50,878
so we can use the scale
12241
07:05:50,878 --> 07:05:53,680
dot set function and we can place a
12242
07:05:53,680 --> 07:05:54,558
number in here
12243
07:05:54,558 --> 07:05:57,200
a value so let's say we want this to be
12244
07:05:57,200 --> 07:05:58,080
100
12245
07:05:58,080 --> 07:06:00,718
so this is now at the 100 position
12246
07:06:00,718 --> 07:06:01,840
instead of zero
12247
07:06:01,840 --> 07:06:03,360
if we wanted this in the middle we could
12248
07:06:03,360 --> 07:06:05,520
set this to 50
12249
07:06:05,520 --> 07:06:07,600
and now it's in the middle however if
12250
07:06:07,600 --> 07:06:09,280
you change the
12251
07:06:09,280 --> 07:06:12,878
range of your skill of from minus 2
12252
07:06:12,878 --> 07:06:15,280
and set this to 50 now 50 is all the way
12253
07:06:15,280 --> 07:06:17,280
down here so it no longer appears in the
12254
07:06:17,280 --> 07:06:18,080
middle
12255
07:06:18,080 --> 07:06:20,240
so this isn't really necessary but if
12256
07:06:20,240 --> 07:06:21,440
you want this to be a little more
12257
07:06:21,440 --> 07:06:22,400
sophisticated
12258
07:06:22,400 --> 07:06:23,840
there's actually a formula that you
12259
07:06:23,840 --> 07:06:26,080
could use we can use scale
12260
07:06:26,080 --> 07:06:28,638
straight braces and then place the word
12261
07:06:28,638 --> 07:06:29,120
from
12262
07:06:29,120 --> 07:06:31,680
within here this will give us our max
12263
07:06:31,680 --> 07:06:32,558
basically
12264
07:06:32,558 --> 07:06:35,600
uh so this is from without an underscore
12265
07:06:35,600 --> 07:06:40,400
so this not this this
12266
07:06:40,400 --> 07:06:43,520
scale from minus scale
12267
07:06:43,520 --> 07:06:47,200
two and then we're going to divide this
12268
07:06:47,200 --> 07:06:48,798
by two
12269
07:06:48,798 --> 07:06:50,000
and then i'm just going to put this
12270
07:06:50,000 --> 07:06:53,920
within a extra set of parentheses
12271
07:06:53,920 --> 07:06:57,600
plus scale two just in case
12272
07:06:57,600 --> 07:07:01,360
our two value is greater than zero
12273
07:07:01,360 --> 07:07:03,600
we just want to account for that and
12274
07:07:03,600 --> 07:07:05,600
then i'm just going to
12275
07:07:05,600 --> 07:07:06,878
put these within another set of
12276
07:07:06,878 --> 07:07:08,798
parentheses so
12277
07:07:08,798 --> 07:07:10,400
this should always appear in the middle
12278
07:07:10,400 --> 07:07:14,718
then even if we were to change this
12279
07:07:14,840 --> 07:07:16,478
range
12280
07:07:16,478 --> 07:07:18,478
yeah it seems like it works uh like i
12281
07:07:18,478 --> 07:07:20,558
said it's not really necessary but
12282
07:07:20,558 --> 07:07:21,600
if you want this to be a little more
12283
07:07:21,600 --> 07:07:23,840
sophisticated that would work
12284
07:07:23,840 --> 07:07:25,680
now let's make some cosmetic changes to
12285
07:07:25,680 --> 07:07:28,080
this scale and make it look pretty
12286
07:07:28,080 --> 07:07:30,958
so first let's change the trough color
12287
07:07:30,958 --> 07:07:34,958
this is this gray portion right here
12288
07:07:35,280 --> 07:07:39,920
so let's set the trough color
12289
07:07:39,920 --> 07:07:42,000
equals whatever color you want you could
12290
07:07:42,000 --> 07:07:43,440
say like blue
12291
07:07:43,440 --> 07:07:45,840
or you can place a hex value here and i
12292
07:07:45,840 --> 07:07:47,440
actually picked one out already
12293
07:07:47,440 --> 07:07:50,718
so i'm going to use 6 9 e
12294
07:07:50,718 --> 07:07:54,638
a f f and that is this icy blue color
12295
07:07:54,638 --> 07:07:58,240
kind of like this is sliding on ice
12296
07:07:58,240 --> 07:08:00,878
and i'm also going to change the font
12297
07:08:00,878 --> 07:08:03,200
color
12298
07:08:03,440 --> 07:08:06,160
and i'm going to pick this fiery red
12299
07:08:06,160 --> 07:08:07,200
color
12300
07:08:07,200 --> 07:08:12,718
to symbolize heat
12301
07:08:12,718 --> 07:08:14,160
and then you can change the background
12302
07:08:14,160 --> 07:08:16,718
color too
12303
07:08:16,718 --> 07:08:20,240
so that is bg and i'll just set this to
12304
07:08:20,240 --> 07:08:22,878
like black
12305
07:08:24,718 --> 07:08:27,760
it's looking kind of cool now right and
12306
07:08:27,760 --> 07:08:29,360
for no good reason i'm going to add some
12307
07:08:29,360 --> 07:08:31,200
pictures
12308
07:08:31,200 --> 07:08:32,798
so feel free to pause the video if you'd
12309
07:08:32,798 --> 07:08:34,558
like to download some images i'm going
12310
07:08:34,558 --> 07:08:36,638
to use an image of a flame
12311
07:08:36,638 --> 07:08:39,280
for the hot side of our scale and a
12312
07:08:39,280 --> 07:08:40,000
snowflake
12313
07:08:40,000 --> 07:08:43,440
for the cold side so let's begin by
12314
07:08:43,440 --> 07:08:46,958
adding the hot image uh to the scale
12315
07:08:46,958 --> 07:08:48,080
before we actually
12316
07:08:48,080 --> 07:08:50,638
add the scale to the window so we'll do
12317
07:08:50,638 --> 07:08:52,558
this after we instantiate the window
12318
07:08:52,558 --> 07:08:53,920
because that's the only place that we
12319
07:08:53,920 --> 07:08:55,040
can do that
12320
07:08:55,040 --> 07:08:56,798
so we're going to create a image i'll
12321
07:08:56,798 --> 07:08:58,080
just call this hot image
12322
07:08:58,080 --> 07:09:01,760
for the fire icon so hot image equals
12323
07:09:01,760 --> 07:09:04,798
photo image file
12324
07:09:04,798 --> 07:09:08,760
equals this is hot.png so
12325
07:09:08,760 --> 07:09:12,080
hot.png we're going to add the image to
12326
07:09:12,080 --> 07:09:13,760
a label
12327
07:09:13,760 --> 07:09:17,440
i'll call this hot label hot label
12328
07:09:17,440 --> 07:09:20,638
label image equals
12329
07:09:20,638 --> 07:09:24,160
hot image and then we need to pack this
12330
07:09:24,160 --> 07:09:29,120
so hot label dot pack
12331
07:09:29,120 --> 07:09:31,200
and this should appear at the top let's
12332
07:09:31,200 --> 07:09:32,320
test it
12333
07:09:32,320 --> 07:09:35,040
yep there it is let's do the same thing
12334
07:09:35,040 --> 07:09:37,760
for the cold side
12335
07:09:37,760 --> 07:09:41,520
so i'll add this here before the button
12336
07:09:41,520 --> 07:09:44,240
and i'm going to replace hot with cold
12337
07:09:44,240 --> 07:09:45,958
same thing with the file name
12338
07:09:45,958 --> 07:09:48,958
cold.png
12339
07:09:49,360 --> 07:09:52,798
this will be cold label cold
12340
07:09:52,798 --> 07:09:57,120
image and cold label dot pack
12341
07:09:57,120 --> 07:09:59,920
and that should be it let's take a look
12342
07:09:59,920 --> 07:10:02,160
not too shabby it's looking pretty sweet
12343
07:10:02,160 --> 07:10:03,840
it has our submit button at the bottom
12344
07:10:03,840 --> 07:10:05,440
too uh so
12345
07:10:05,440 --> 07:10:07,440
right now the temperature is 100 degrees
12346
07:10:07,440 --> 07:10:09,040
celsius
12347
07:10:09,040 --> 07:10:11,600
the temperature is 0 degrees celsius the
12348
07:10:11,600 --> 07:10:14,160
temperature is 60 degrees celsius
12349
07:10:14,160 --> 07:10:16,240
well that's how to create a scale in
12350
07:10:16,240 --> 07:10:17,840
python if you would like a copy of all
12351
07:10:17,840 --> 07:10:19,360
this code i'll post all of this in the
12352
07:10:19,360 --> 07:10:20,478
comments down below
12353
07:10:20,478 --> 07:10:22,478
but yeah that's how to create a scale in
12354
07:10:22,478 --> 07:10:24,478
python
12355
07:10:24,478 --> 07:10:26,160
hey what's going on everybody it's your
12356
07:10:26,160 --> 07:10:27,920
bro here hope you're doing well and in
12357
07:10:27,920 --> 07:10:29,280
this video i'm going to teach you guys
12358
07:10:29,280 --> 07:10:31,760
how we can create a list box in python
12359
07:10:31,760 --> 07:10:32,080
so
12360
07:10:32,080 --> 07:10:35,920
sit back relax and enjoy the show
12361
07:10:36,638 --> 07:10:38,320
all right you wonderful people let's
12362
07:10:38,320 --> 07:10:40,558
create a list box a list box
12363
07:10:40,558 --> 07:10:43,200
is a listing of selectable text items
12364
07:10:43,200 --> 07:10:44,958
within its own container
12365
07:10:44,958 --> 07:10:47,440
so as always we'll need a window window
12366
07:10:47,440 --> 07:10:48,478
equals tk
12367
07:10:48,478 --> 07:10:51,280
followed by window dot main loop let's
12368
07:10:51,280 --> 07:10:52,558
create a list box
12369
07:10:52,558 --> 07:10:55,120
simply called list box big surprise
12370
07:10:55,120 --> 07:10:55,920
there right
12371
07:10:55,920 --> 07:10:59,040
so list box equals list box what are we
12372
07:10:59,040 --> 07:11:00,478
adding this list box to
12373
07:11:00,478 --> 07:11:01,840
we're going to place that here within
12374
07:11:01,840 --> 07:11:03,600
the parentheses of the constructor
12375
07:11:03,600 --> 07:11:05,280
we're adding this to our window and then
12376
07:11:05,280 --> 07:11:07,718
we're going to follow this with
12377
07:11:07,718 --> 07:11:09,878
listbox.pack you can also do
12378
07:11:09,878 --> 07:11:11,840
listbox.place as well and place some
12379
07:11:11,840 --> 07:11:12,718
coordinates but
12380
07:11:12,718 --> 07:11:14,400
we've always done pack let's just stick
12381
07:11:14,400 --> 07:11:16,240
with that for now all right so this is
12382
07:11:16,240 --> 07:11:18,400
what our list box looks like
12383
07:11:18,400 --> 07:11:20,718
it's a small rectangular box and we're
12384
07:11:20,718 --> 07:11:22,718
going to add some text items that are
12385
07:11:22,718 --> 07:11:23,520
selectable
12386
07:11:23,520 --> 07:11:25,600
and eventually we'll be able to submit a
12387
07:11:25,600 --> 07:11:27,600
choice so i have an idea
12388
07:11:27,600 --> 07:11:30,000
let's create an online menu like we're
12389
07:11:30,000 --> 07:11:32,000
ordering food from a restaurant
12390
07:11:32,000 --> 07:11:34,320
let's say it's bros italian restaurant
12391
07:11:34,320 --> 07:11:36,798
so let's add some food items to our menu
12392
07:11:36,798 --> 07:11:39,440
and somebody can submit an order so
12393
07:11:39,440 --> 07:11:40,558
we'll need to use the
12394
07:11:40,558 --> 07:11:42,558
insert function of the list box to add
12395
07:11:42,558 --> 07:11:43,920
some items so
12396
07:11:43,920 --> 07:11:47,200
list box dot insert we place
12397
07:11:47,200 --> 07:11:50,478
an index and a text item so let's say
12398
07:11:50,478 --> 07:11:51,040
for
12399
07:11:51,040 --> 07:11:54,080
index one we'll have pizza
12400
07:11:54,080 --> 07:11:56,878
and this is what we end up with we have
12401
07:11:56,878 --> 07:11:58,000
the item pizza
12402
07:11:58,000 --> 07:12:00,558
it's selectable and well that's really
12403
07:12:00,558 --> 07:12:02,240
it we'll need to create a submit button
12404
07:12:02,240 --> 07:12:04,558
and populate the rest of this list box
12405
07:12:04,558 --> 07:12:06,558
so let's add a few other items for our
12406
07:12:06,558 --> 07:12:10,000
menu so let's say we'll have five items
12407
07:12:10,000 --> 07:12:13,120
and we need an index for each so one two
12408
07:12:13,120 --> 07:12:16,718
three four and five so we have
12409
07:12:16,718 --> 07:12:18,798
pizza for the first item for the second
12410
07:12:18,798 --> 07:12:20,878
item let's have pasta
12411
07:12:20,878 --> 07:12:23,440
and third let's say garlic bread because
12412
07:12:23,440 --> 07:12:24,080
garlic bread
12413
07:12:24,080 --> 07:12:26,958
is awesome and restaurants usually have
12414
07:12:26,958 --> 07:12:30,400
appetizers so why not soup and salad
12415
07:12:30,400 --> 07:12:31,600
and that should be enough for this
12416
07:12:31,600 --> 07:12:33,680
example
12417
07:12:33,680 --> 07:12:36,080
and this is what we have we have our
12418
07:12:36,080 --> 07:12:37,520
menu and we can select
12419
07:12:37,520 --> 07:12:40,638
an item from this menu so let's
12420
07:12:40,638 --> 07:12:42,798
customize the appearance of this menu
12421
07:12:42,798 --> 07:12:45,120
well because we can so let's do that
12422
07:12:45,120 --> 07:12:46,958
let's change the background color so we
12423
07:12:46,958 --> 07:12:48,320
do that with the option
12424
07:12:48,320 --> 07:12:50,478
bg for background and you can set this
12425
07:12:50,478 --> 07:12:52,878
equal to a color of your choice
12426
07:12:52,878 --> 07:12:55,840
or a hex value so i have a hex color
12427
07:12:55,840 --> 07:12:57,360
value already picked out
12428
07:12:57,360 --> 07:13:01,360
it is f7fde and this has the appearance
12429
07:13:01,360 --> 07:13:02,320
of i would say
12430
07:13:02,320 --> 07:13:04,798
paper like it's kind of faded i think
12431
07:13:04,798 --> 07:13:06,080
that would fit some sort
12432
07:13:06,080 --> 07:13:08,400
of menu for a restaurant but pick
12433
07:13:08,400 --> 07:13:10,400
whatever background color you want
12434
07:13:10,400 --> 07:13:13,600
so let's change the font next so we can
12435
07:13:13,600 --> 07:13:16,558
do that with the option font
12436
07:13:16,558 --> 07:13:21,840
and i will pick the font
12437
07:13:22,638 --> 07:13:24,638
it's somewhat fancy i think it would fit
12438
07:13:24,638 --> 07:13:26,000
like a restaurant menu
12439
07:13:26,000 --> 07:13:29,600
and then a size of 35.
12440
07:13:29,600 --> 07:13:32,798
yeah we're getting somewhere we can also
12441
07:13:32,798 --> 07:13:35,040
change the width and the height
12442
07:13:35,040 --> 07:13:39,680
so width equals let's say 12.
12443
07:13:39,680 --> 07:13:41,760
that's not too bad for a width let's
12444
07:13:41,760 --> 07:13:43,360
also change the height
12445
07:13:43,360 --> 07:13:46,400
so we can do that with a function
12446
07:13:46,400 --> 07:13:49,280
of our list box so let's get the current
12447
07:13:49,280 --> 07:13:50,958
size of the list box
12448
07:13:50,958 --> 07:13:53,120
and let's actually do that after we
12449
07:13:53,120 --> 07:13:55,120
insert all of the items within our list
12450
07:13:55,120 --> 07:13:55,760
box
12451
07:13:55,760 --> 07:13:58,478
so to change the size of our list box
12452
07:13:58,478 --> 07:13:59,360
what we do
12453
07:13:59,360 --> 07:14:02,400
is type in list box
12454
07:14:02,400 --> 07:14:05,040
dot config you usually use config if you
12455
07:14:05,040 --> 07:14:06,478
need to change any uh
12456
07:14:06,478 --> 07:14:09,600
options and we want to change the
12457
07:14:09,600 --> 07:14:13,520
length of or the height of this list box
12458
07:14:13,520 --> 07:14:17,520
so we're going to say height equals
12459
07:14:17,520 --> 07:14:20,958
list box dot
12460
07:14:20,958 --> 07:14:24,320
size function so then this will adjust
12461
07:14:24,320 --> 07:14:25,680
the size
12462
07:14:25,680 --> 07:14:28,478
of our list box dynamically even if we
12463
07:14:28,478 --> 07:14:30,320
were to remove an item
12464
07:14:30,320 --> 07:14:32,718
then the size of our list box is going
12465
07:14:32,718 --> 07:14:34,478
to shrink
12466
07:14:34,478 --> 07:14:37,280
just like that but let's keep salad in
12467
07:14:37,280 --> 07:14:40,000
there for now because we need salad
12468
07:14:40,000 --> 07:14:43,200
all right so how do we actually submit
12469
07:14:43,200 --> 07:14:44,718
one of these options we'll need to
12470
07:14:44,718 --> 07:14:46,320
create a submit button
12471
07:14:46,320 --> 07:14:48,240
so let's do that after we create our
12472
07:14:48,240 --> 07:14:50,638
list box so near the bottom
12473
07:14:50,638 --> 07:14:53,760
we'll call this submit button
12474
07:14:53,760 --> 07:14:56,638
submit button equals button we're adding
12475
07:14:56,638 --> 07:14:58,080
this to our window
12476
07:14:58,080 --> 07:15:01,360
let's set the text to submit
12477
07:15:01,360 --> 07:15:03,920
and we'll need a command so we'll create
12478
07:15:03,920 --> 07:15:05,520
a function called
12479
07:15:05,520 --> 07:15:08,558
submit and then we need to pack
12480
07:15:08,558 --> 07:15:11,680
this submit button
12481
07:15:11,680 --> 07:15:14,638
submit button.pack now at the top of
12482
07:15:14,638 --> 07:15:15,360
this program
12483
07:15:15,360 --> 07:15:19,120
let's create a submit function
12484
07:15:19,120 --> 07:15:24,000
so at the top def submit
12485
07:15:24,000 --> 07:15:27,120
so to retrieve or get the current
12486
07:15:27,120 --> 07:15:28,160
selected item
12487
07:15:28,160 --> 07:15:30,240
of a list box there is a certain
12488
07:15:30,240 --> 07:15:31,920
function that you can use
12489
07:15:31,920 --> 07:15:35,040
so we'll begin by saying
12490
07:15:35,040 --> 07:15:38,240
list box dot get
12491
07:15:38,240 --> 07:15:39,840
what are we getting we want to get
12492
07:15:39,840 --> 07:15:41,920
anything that is currently selected and
12493
07:15:41,920 --> 07:15:43,120
there is a function
12494
07:15:43,120 --> 07:15:45,920
for that so what we're going to do
12495
07:15:45,920 --> 07:15:47,600
within the parentheses of the get
12496
07:15:47,600 --> 07:15:49,600
function is type in
12497
07:15:49,600 --> 07:15:54,878
listbox dot current selection
12498
07:15:54,878 --> 07:15:57,920
it's shortened to cur selection all
12499
07:15:57,920 --> 07:15:59,520
right and then we can either store this
12500
07:15:59,520 --> 07:16:01,200
within a variable or we can just print
12501
07:16:01,200 --> 07:16:02,718
this but i think we'll just print this
12502
07:16:02,718 --> 07:16:05,520
to keep it simple
12503
07:16:06,080 --> 07:16:09,520
now when we select an item and
12504
07:16:09,520 --> 07:16:11,200
click on the submit button it's going to
12505
07:16:11,200 --> 07:16:12,798
submit our order
12506
07:16:12,798 --> 07:16:16,000
actually better yet let's actually uh
12507
07:16:16,000 --> 07:16:20,840
print you have
12508
07:16:20,840 --> 07:16:25,200
ordered whatever item that
12509
07:16:25,200 --> 07:16:27,840
we select so garlic bread you have
12510
07:16:27,840 --> 07:16:29,680
ordered garlic bread
12511
07:16:29,680 --> 07:16:32,000
cool what we're going to work on now is
12512
07:16:32,000 --> 07:16:33,440
a way to submit
12513
07:16:33,440 --> 07:16:36,478
or insert an item to our list box after
12514
07:16:36,478 --> 07:16:38,798
this program is compiled and already
12515
07:16:38,798 --> 07:16:39,520
running
12516
07:16:39,520 --> 07:16:42,080
so we'll create an entry box within our
12517
07:16:42,080 --> 07:16:42,638
window
12518
07:16:42,638 --> 07:16:44,878
where we can submit a custom item to
12519
07:16:44,878 --> 07:16:45,920
this menu
12520
07:16:45,920 --> 07:16:49,120
so let's create an entry box and let's
12521
07:16:49,120 --> 07:16:50,878
do that before the submit button that
12522
07:16:50,878 --> 07:16:52,320
would be a good spot for this
12523
07:16:52,320 --> 07:16:54,398
so let's call this entry box where
12524
07:16:54,398 --> 07:16:55,600
somebody can
12525
07:16:55,600 --> 07:16:58,878
type in something to add to the menu so
12526
07:16:58,878 --> 07:17:01,280
entry box equals entry and we're adding
12527
07:17:01,280 --> 07:17:02,080
this
12528
07:17:02,080 --> 07:17:05,760
to our window and then entry box
12529
07:17:05,760 --> 07:17:09,760
dot pack then we're going to create a
12530
07:17:09,760 --> 07:17:10,240
button
12531
07:17:10,240 --> 07:17:12,080
called add and i'm just going to copy
12532
07:17:12,080 --> 07:17:13,920
everything from the
12533
07:17:13,920 --> 07:17:16,478
submit button and we'll change submit to
12534
07:17:16,478 --> 07:17:17,120
add
12535
07:17:17,120 --> 07:17:20,000
we're going to add an item to the menu
12536
07:17:20,000 --> 07:17:21,440
so we'll change the text
12537
07:17:21,440 --> 07:17:24,878
to add and command add
12538
07:17:24,878 --> 07:17:28,478
add button dot pack then we're going to
12539
07:17:28,478 --> 07:17:29,040
create
12540
07:17:29,040 --> 07:17:31,120
a function called add and we'll do that
12541
07:17:31,120 --> 07:17:32,718
at the top
12542
07:17:32,718 --> 07:17:35,920
def add parentheses
12543
07:17:35,920 --> 07:17:39,200
colon so to add an item to the list box
12544
07:17:39,200 --> 07:17:40,080
first we need to
12545
07:17:40,080 --> 07:17:43,120
get what is currently in the entry box
12546
07:17:43,120 --> 07:17:47,280
so we can do that by typing in
12547
07:17:47,280 --> 07:17:50,558
list box dot insert
12548
07:17:50,558 --> 07:17:52,558
then within the parentheses we need to
12549
07:17:52,558 --> 07:17:53,600
get
12550
07:17:53,600 --> 07:17:56,638
an index number as well as
12551
07:17:56,638 --> 07:17:59,280
a text item so for the number what we
12552
07:17:59,280 --> 07:18:00,320
can type here
12553
07:18:00,320 --> 07:18:03,840
is the listbox dot size
12554
07:18:03,840 --> 07:18:05,360
so this will give us the current
12555
07:18:05,360 --> 07:18:07,200
position of the
12556
07:18:07,200 --> 07:18:08,798
index that we're currently on for the
12557
07:18:08,798 --> 07:18:11,600
list box and then we need to get the
12558
07:18:11,600 --> 07:18:12,080
text
12559
07:18:12,080 --> 07:18:14,240
in the entry box so we type in the name
12560
07:18:14,240 --> 07:18:15,680
of the entry box
12561
07:18:15,680 --> 07:18:19,040
entry box dot get
12562
07:18:19,040 --> 07:18:22,080
and this will insert a new item within
12563
07:18:22,080 --> 07:18:24,878
our menu our list box so let's try it
12564
07:18:24,878 --> 07:18:27,120
again
12565
07:18:27,440 --> 07:18:29,840
so we can type in an item let's say soda
12566
07:18:29,840 --> 07:18:31,840
we want to add soda to the menu
12567
07:18:31,840 --> 07:18:34,638
and then we click add all right there's
12568
07:18:34,638 --> 07:18:36,398
our soda but we have to scroll down to
12569
07:18:36,398 --> 07:18:37,040
see it
12570
07:18:37,040 --> 07:18:41,040
so let's change the size of our list box
12571
07:18:41,040 --> 07:18:43,120
so there's actually a line of code that
12572
07:18:43,120 --> 07:18:44,440
we did that already
12573
07:18:44,440 --> 07:18:47,440
listbox.config height equals listbox
12574
07:18:47,440 --> 07:18:50,638
size so let's add that within the add
12575
07:18:50,638 --> 07:18:52,240
function at the very end
12576
07:18:52,240 --> 07:18:55,280
so then the height of our list box will
12577
07:18:55,280 --> 07:18:56,080
adjust
12578
07:18:56,080 --> 07:18:57,920
so currently there's no soda within our
12579
07:18:57,920 --> 07:18:59,680
menu let's type in soda
12580
07:18:59,680 --> 07:19:02,398
or some other item click add and then
12581
07:19:02,398 --> 07:19:04,320
that item will appear and the size of
12582
07:19:04,320 --> 07:19:06,638
the list box will change to accommodate
12583
07:19:06,638 --> 07:19:07,840
any new items
12584
07:19:07,840 --> 07:19:10,320
let's say now that we serve sushi we're
12585
07:19:10,320 --> 07:19:11,840
now an italian
12586
07:19:11,840 --> 07:19:14,320
and japanese restaurant now we have
12587
07:19:14,320 --> 07:19:15,760
sushi on the menu
12588
07:19:15,760 --> 07:19:17,440
you know what why stop there let's
12589
07:19:17,440 --> 07:19:19,600
create a delete button to delete an item
12590
07:19:19,600 --> 07:19:21,120
from this menu
12591
07:19:21,120 --> 07:19:24,160
so we'll create a delete button
12592
07:19:24,160 --> 07:19:26,000
i'll just copy everything for add and
12593
07:19:26,000 --> 07:19:27,600
change this to
12594
07:19:27,600 --> 07:19:30,558
delete so delete button text equals
12595
07:19:30,558 --> 07:19:32,000
delete
12596
07:19:32,000 --> 07:19:35,280
command equals delete as well and
12597
07:19:35,280 --> 07:19:38,478
delete button dot pack and then at the
12598
07:19:38,478 --> 07:19:40,718
very top we'll create a delete
12599
07:19:40,718 --> 07:19:44,000
function so we'll do that here
12600
07:19:44,000 --> 07:19:48,398
d e f delete
12601
07:19:48,958 --> 07:19:51,040
and to delete an item from a list box
12602
07:19:51,040 --> 07:19:52,718
there's actually a function to do that
12603
07:19:52,718 --> 07:19:53,360
so
12604
07:19:53,360 --> 07:19:56,558
list box dot delete
12605
07:19:56,558 --> 07:19:58,878
and then within the parenthesis of the
12606
07:19:58,878 --> 07:20:00,478
delete function we can pass
12607
07:20:00,478 --> 07:20:04,320
in list box dot current selection or
12608
07:20:04,320 --> 07:20:04,718
just
12609
07:20:04,718 --> 07:20:07,840
cur selection for short and then we want
12610
07:20:07,840 --> 07:20:08,160
to
12611
07:20:08,160 --> 07:20:10,478
readjust the height of this list box
12612
07:20:10,478 --> 07:20:12,240
after we delete an item
12613
07:20:12,240 --> 07:20:15,600
so we'll just add listbox.config height
12614
07:20:15,600 --> 07:20:18,718
equals listbox.size because the size
12615
07:20:18,718 --> 07:20:20,878
of all the items within the list box is
12616
07:20:20,878 --> 07:20:23,120
going to shrink after we delete an item
12617
07:20:23,120 --> 07:20:26,478
so let's try this so here's our menu
12618
07:20:26,478 --> 07:20:28,558
let's say we no longer want salad
12619
07:20:28,558 --> 07:20:30,398
because salad is healthy well we can
12620
07:20:30,398 --> 07:20:31,360
delete it
12621
07:20:31,360 --> 07:20:33,280
same thing with soup and you know what
12622
07:20:33,280 --> 07:20:34,558
let's also delete the
12623
07:20:34,558 --> 07:20:37,120
pasta and the pizza so we're only left
12624
07:20:37,120 --> 07:20:39,120
with sweet sweet garlic bread
12625
07:20:39,120 --> 07:20:40,638
well ladies and gentlemen we're going to
12626
07:20:40,638 --> 07:20:42,240
get into some more advanced stuff with
12627
07:20:42,240 --> 07:20:43,200
list boxes
12628
07:20:43,200 --> 07:20:45,520
what if you want to select more than one
12629
07:20:45,520 --> 07:20:47,120
item from this list box
12630
07:20:47,120 --> 07:20:48,558
we're going to need to change a few
12631
07:20:48,558 --> 07:20:51,520
things so when we create our list box
12632
07:20:51,520 --> 07:20:54,638
there is a select mode select mode
12633
07:20:54,638 --> 07:20:56,958
and we are going to set this equal to
12634
07:20:56,958 --> 07:20:57,840
multiple
12635
07:20:57,840 --> 07:21:00,478
so now we can actually select multiple
12636
07:21:00,478 --> 07:21:01,280
items
12637
07:21:01,280 --> 07:21:03,680
from this list box like i want to select
12638
07:21:03,680 --> 07:21:04,718
one of everything
12639
07:21:04,718 --> 07:21:07,040
but what happens when we submit this
12640
07:21:07,040 --> 07:21:08,320
well we're going to run into some
12641
07:21:08,320 --> 07:21:09,600
problems so we're going to need to
12642
07:21:09,600 --> 07:21:11,360
change this program around
12643
07:21:11,360 --> 07:21:14,558
so let's begin with the submit option so
12644
07:21:14,558 --> 07:21:16,160
what we're going to do within the submit
12645
07:21:16,160 --> 07:21:16,958
option
12646
07:21:16,958 --> 07:21:20,000
is first get rid of this line let's say
12647
07:21:20,000 --> 07:21:20,398
we
12648
07:21:20,398 --> 07:21:23,920
no longer need this anymore uh and then
12649
07:21:23,920 --> 07:21:26,000
before we print out the items that we
12650
07:21:26,000 --> 07:21:26,958
ordered
12651
07:21:26,958 --> 07:21:30,240
let's create a list called food
12652
07:21:30,240 --> 07:21:31,920
and we'll just leave this empty for now
12653
07:21:31,920 --> 07:21:33,760
we'll fill this later
12654
07:21:33,760 --> 07:21:37,520
so we'll create a for loop for index
12655
07:21:37,520 --> 07:21:40,638
in list box
12656
07:21:40,638 --> 07:21:44,398
dot current selection
12657
07:21:44,398 --> 07:21:46,478
what we're going to do after each
12658
07:21:46,478 --> 07:21:48,000
iteration
12659
07:21:48,000 --> 07:21:52,000
food dot insert
12660
07:21:52,478 --> 07:21:55,840
our index number as well as
12661
07:21:55,840 --> 07:21:59,680
listbox dot get
12662
07:21:59,680 --> 07:22:02,638
the item name at this index what this
12663
07:22:02,638 --> 07:22:04,638
for loop is doing is that it's going to
12664
07:22:04,638 --> 07:22:07,360
iterate once for each item that we
12665
07:22:07,360 --> 07:22:08,080
select
12666
07:22:08,080 --> 07:22:10,398
so since we have three items that are
12667
07:22:10,398 --> 07:22:12,080
currently selected this is going to
12668
07:22:12,080 --> 07:22:13,440
iterate three times
12669
07:22:13,440 --> 07:22:15,840
get the index number as well as the item
12670
07:22:15,840 --> 07:22:17,200
at that index number
12671
07:22:17,200 --> 07:22:18,958
and now we just need to display the
12672
07:22:18,958 --> 07:22:20,478
contents of what we have
12673
07:22:20,478 --> 07:22:23,200
within our list of food so we can do
12674
07:22:23,200 --> 07:22:23,680
that with
12675
07:22:23,680 --> 07:22:26,558
another for loop so let's add that after
12676
07:22:26,558 --> 07:22:27,920
you have ordered
12677
07:22:27,920 --> 07:22:30,798
then for
12678
07:22:30,958 --> 07:22:34,558
index in our list
12679
07:22:34,558 --> 07:22:39,040
food all we'll do is print
12680
07:22:39,040 --> 07:22:42,638
index so this will print out everything
12681
07:22:42,638 --> 07:22:43,520
that we ordered
12682
07:22:43,520 --> 07:22:45,120
so let's say that we want one of
12683
07:22:45,120 --> 07:22:46,878
everything and submit this
12684
07:22:46,878 --> 07:22:49,120
you have ordered pizza pasta garlic
12685
07:22:49,120 --> 07:22:51,120
bread soup and salad
12686
07:22:51,120 --> 07:22:53,280
now what happens if we try to delete
12687
07:22:53,280 --> 07:22:54,398
multiple items
12688
07:22:54,398 --> 07:22:56,718
so let's say we want to delete soup and
12689
07:22:56,718 --> 07:22:58,718
salad and we click delete
12690
07:22:58,718 --> 07:23:00,240
well we're going to run into an error
12691
07:23:00,240 --> 07:23:02,478
because our function is not set up to
12692
07:23:02,478 --> 07:23:03,360
accommodate for
12693
07:23:03,360 --> 07:23:05,440
multiple items so we'll need to change a
12694
07:23:05,440 --> 07:23:07,120
few things around so let's get rid of
12695
07:23:07,120 --> 07:23:08,000
this line for now
12696
07:23:08,000 --> 07:23:10,718
list box dot delete list box dot current
12697
07:23:10,718 --> 07:23:11,440
selection
12698
07:23:11,440 --> 07:23:13,280
and we're going to place a for loop here
12699
07:23:13,280 --> 07:23:14,638
so for
12700
07:23:14,638 --> 07:23:18,398
index in list box
12701
07:23:18,398 --> 07:23:21,680
dot current selection then after each
12702
07:23:21,680 --> 07:23:22,798
iteration
12703
07:23:22,798 --> 07:23:25,680
what we're going to do is use list box
12704
07:23:25,680 --> 07:23:26,240
dot
12705
07:23:26,240 --> 07:23:29,680
delete and we're deleting the item at
12706
07:23:29,680 --> 07:23:32,638
whatever index we're currently on so you
12707
07:23:32,638 --> 07:23:33,840
would think this would work
12708
07:23:33,840 --> 07:23:36,320
at first glance let's say we want to
12709
07:23:36,320 --> 07:23:38,000
delete pizza and pasta
12710
07:23:38,000 --> 07:23:40,478
we're going to click delete so pizza was
12711
07:23:40,478 --> 07:23:41,680
deleted but not
12712
07:23:41,680 --> 07:23:44,718
pasta that's because the indexes are
12713
07:23:44,718 --> 07:23:45,280
changing
12714
07:23:45,280 --> 07:23:48,478
after we delete an item so when we use
12715
07:23:48,478 --> 07:23:49,440
this for loop
12716
07:23:49,440 --> 07:23:53,080
we should actually reverse this so with
12717
07:23:53,080 --> 07:23:54,398
listbox.currentselection
12718
07:23:54,398 --> 07:23:56,558
we are going to enclose this with
12719
07:23:56,558 --> 07:23:59,120
reversed
12720
07:23:59,280 --> 07:24:01,600
so then we're going to start at the last
12721
07:24:01,600 --> 07:24:03,680
index and work our way to zero
12722
07:24:03,680 --> 07:24:06,320
in that order so now if we want to
12723
07:24:06,320 --> 07:24:06,878
delete
12724
07:24:06,878 --> 07:24:09,520
pizza and pasta i don't know why we
12725
07:24:09,520 --> 07:24:10,320
would because
12726
07:24:10,320 --> 07:24:12,878
they're amazing and we click delete it
12727
07:24:12,878 --> 07:24:13,520
deletes
12728
07:24:13,520 --> 07:24:16,080
pizza and pasta but we still have garlic
12729
07:24:16,080 --> 07:24:18,718
bread soup and salad then we can add an
12730
07:24:18,718 --> 07:24:19,360
item
12731
07:24:19,360 --> 07:24:21,280
let's say we want to add sushi against
12732
07:24:21,280 --> 07:24:22,798
the menu we can do that
12733
07:24:22,798 --> 07:24:24,558
and then we can order one of everything
12734
07:24:24,558 --> 07:24:26,160
because we're really hungry
12735
07:24:26,160 --> 07:24:28,558
you have ordered garlic bread soup salad
12736
07:24:28,558 --> 07:24:29,840
and sushi
12737
07:24:29,840 --> 07:24:32,000
so that's the basics of creating a list
12738
07:24:32,000 --> 07:24:34,320
box in python if you want a copy of all
12739
07:24:34,320 --> 07:24:35,040
this code
12740
07:24:35,040 --> 07:24:36,558
i'll post all of this in the comments
12741
07:24:36,558 --> 07:24:38,718
down below but yeah that's the basics
12742
07:24:38,718 --> 07:24:41,760
of list boxes in python
12743
07:24:41,760 --> 07:24:43,360
hey what's going on everybody it's your
12744
07:24:43,360 --> 07:24:45,120
bro here hope you're doing well and in
12745
07:24:45,120 --> 07:24:46,398
this video i'm going to teach you guys
12746
07:24:46,398 --> 07:24:47,280
how we can create
12747
07:24:47,280 --> 07:24:49,840
message boxes in python so sit back
12748
07:24:49,840 --> 07:24:53,280
relax and enjoy the show
12749
07:24:54,398 --> 07:24:56,478
okay people let's create some message
12750
07:24:56,478 --> 07:24:58,718
boxes but first we'll need an import so
12751
07:24:58,718 --> 07:24:59,280
from
12752
07:24:59,280 --> 07:25:02,958
tk enter import
12753
07:25:02,958 --> 07:25:06,558
message box and what this will do
12754
07:25:06,558 --> 07:25:09,840
is import the message box
12755
07:25:09,840 --> 07:25:12,638
library so it's not necessary to add
12756
07:25:12,638 --> 07:25:15,360
this comment uh so we'll need a window
12757
07:25:15,360 --> 07:25:18,878
window equals tk followed by window
12758
07:25:18,878 --> 07:25:22,878
dot main loop let's create a button
12759
07:25:22,878 --> 07:25:26,000
so button equals button we're going to
12760
07:25:26,000 --> 07:25:27,440
add this to the window
12761
07:25:27,440 --> 07:25:29,840
we're going to set the command to
12762
07:25:29,840 --> 07:25:30,878
execute a
12763
07:25:30,878 --> 07:25:33,760
click function that will have a message
12764
07:25:33,760 --> 07:25:35,920
box appear when we click on the button
12765
07:25:35,920 --> 07:25:39,680
and let's set the text so text equals
12766
07:25:39,680 --> 07:25:43,120
click me and then we will need to pack
12767
07:25:43,120 --> 07:25:43,840
this button
12768
07:25:43,840 --> 07:25:46,478
so button.pack now we'll need to create
12769
07:25:46,478 --> 07:25:47,600
a click function
12770
07:25:47,600 --> 07:25:50,958
so let's do that at the top def
12771
07:25:50,958 --> 07:25:54,398
click and what do we want to do
12772
07:25:54,398 --> 07:25:56,638
so there's a few different uh message
12773
07:25:56,638 --> 07:25:58,320
dialog boxes that you can use
12774
07:25:58,320 --> 07:26:01,040
so let's begin with a basic show info
12775
07:26:01,040 --> 07:26:02,080
message box
12776
07:26:02,080 --> 07:26:05,760
so message box dot and there's a few
12777
07:26:05,760 --> 07:26:06,240
options
12778
07:26:06,240 --> 07:26:09,760
let's begin with show info show
12779
07:26:09,760 --> 07:26:12,798
info this just displays a simple message
12780
07:26:12,798 --> 07:26:15,040
so if we were to run this we have a
12781
07:26:15,040 --> 07:26:16,638
button we click on the button and it's
12782
07:26:16,638 --> 07:26:19,280
going to create a message box for us
12783
07:26:19,280 --> 07:26:21,440
and it just shows some info but we don't
12784
07:26:21,440 --> 07:26:23,440
really have any info to show yet
12785
07:26:23,440 --> 07:26:26,000
so we can set a title and we can set a
12786
07:26:26,000 --> 07:26:26,958
message
12787
07:26:26,958 --> 07:26:31,680
so let's set the title title equals
12788
07:26:31,680 --> 07:26:34,878
this is an info
12789
07:26:34,878 --> 07:26:39,520
message box
12790
07:26:39,520 --> 07:26:42,638
although part of the title got cut off
12791
07:26:42,638 --> 07:26:45,360
let's also set the message message
12792
07:26:45,360 --> 07:26:47,360
equals
12793
07:26:47,360 --> 07:26:53,600
you are a person
12794
07:26:53,600 --> 07:26:56,798
click me you are a person thanks i
12795
07:26:56,798 --> 07:26:59,920
didn't know that so there's a few other
12796
07:26:59,920 --> 07:27:01,760
message boxes that we can use too
12797
07:27:01,760 --> 07:27:03,040
so for now i'm just going to turn this
12798
07:27:03,040 --> 07:27:05,600
into a comment and i'll just copy this
12799
07:27:05,600 --> 07:27:08,638
just to save some time
12800
07:27:09,440 --> 07:27:12,558
and the next one that we have is
12801
07:27:12,558 --> 07:27:15,760
show warning
12802
07:27:15,760 --> 07:27:18,398
so this will have a different icon so
12803
07:27:18,398 --> 07:27:19,600
the title
12804
07:27:19,600 --> 07:27:24,000
let's say warning and the message
12805
07:27:24,000 --> 07:27:28,878
you have a virus
12806
07:27:31,040 --> 07:27:33,120
so now we get this annoying warning
12807
07:27:33,120 --> 07:27:35,200
message but you can just click ok to get
12808
07:27:35,200 --> 07:27:36,398
rid of it
12809
07:27:36,398 --> 07:27:38,398
so one thing that you might see with
12810
07:27:38,398 --> 07:27:39,760
tech support scams
12811
07:27:39,760 --> 07:27:42,558
they'll have a message box like this
12812
07:27:42,558 --> 07:27:43,680
that shows a warning
12813
07:27:43,680 --> 07:27:45,760
but they'll put this within a while loop
12814
07:27:45,760 --> 07:27:47,360
that's set to true
12815
07:27:47,360 --> 07:27:51,200
so this will just continue on and on
12816
07:27:52,320 --> 07:27:55,840
kind of like this so if we were to try
12817
07:27:55,840 --> 07:27:56,958
and close out of this
12818
07:27:56,958 --> 07:28:00,478
it's just going to reappear while
12819
07:28:00,478 --> 07:28:03,520
true is true basically so that's just
12820
07:28:03,520 --> 07:28:05,040
something funny that i wanted to show
12821
07:28:05,040 --> 07:28:05,760
you
12822
07:28:05,760 --> 07:28:07,440
so let's move on i'm going to turn this
12823
07:28:07,440 --> 07:28:10,240
into a comment
12824
07:28:10,240 --> 07:28:17,120
then the next one we have is show error
12825
07:28:17,120 --> 07:28:21,120
so show error
12826
07:28:21,120 --> 07:28:24,320
title set it to whatever you want
12827
07:28:24,320 --> 07:28:27,760
error message
12828
07:28:27,760 --> 07:28:31,840
something went wrong
12829
07:28:36,320 --> 07:28:38,080
there's a few more sophisticated ones
12830
07:28:38,080 --> 07:28:42,080
too we can ask for some user input
12831
07:28:42,080 --> 07:28:45,680
so this next one is message box
12832
07:28:45,680 --> 07:28:49,440
dot ask and there's a few to choose from
12833
07:28:49,440 --> 07:28:53,040
let's go with ask okay cancel first
12834
07:28:53,040 --> 07:28:56,638
we'll set the title title equals
12835
07:28:56,638 --> 07:29:00,160
ask okay cancel
12836
07:29:00,160 --> 07:29:04,878
and a message message
12837
07:29:05,600 --> 07:29:08,878
do you want to
12838
07:29:08,878 --> 07:29:13,840
do the thing
12839
07:29:15,600 --> 07:29:18,000
so we can actually put this within an if
12840
07:29:18,000 --> 07:29:19,200
statement
12841
07:29:19,200 --> 07:29:21,840
so this returns either true or false
12842
07:29:21,840 --> 07:29:23,840
depending on what you click
12843
07:29:23,840 --> 07:29:26,398
so let's put this within an if statement
12844
07:29:26,398 --> 07:29:28,080
if this is true
12845
07:29:28,080 --> 07:29:31,280
print you did
12846
07:29:31,280 --> 07:29:33,760
a thing
12847
07:29:34,398 --> 07:29:36,718
if it returns false we can use an else
12848
07:29:36,718 --> 07:29:38,320
statement
12849
07:29:38,320 --> 07:29:40,398
else what else do we want to do we'll
12850
07:29:40,398 --> 07:29:42,000
say else
12851
07:29:42,000 --> 07:29:45,520
print you
12852
07:29:45,520 --> 07:29:48,798
cancelled a thing
12853
07:29:49,120 --> 07:29:55,840
and let's test this
12854
07:29:56,240 --> 07:30:01,120
but i am forgetting a colon there
12855
07:30:01,200 --> 07:30:04,000
do you want to do the thing okay you did
12856
07:30:04,000 --> 07:30:04,638
a thing
12857
07:30:04,638 --> 07:30:07,360
let's try it again do you want to do the
12858
07:30:07,360 --> 07:30:08,080
thing
12859
07:30:08,080 --> 07:30:11,280
cancel you cancelled a thing cool
12860
07:30:11,280 --> 07:30:12,798
there's a few others
12861
07:30:12,798 --> 07:30:17,040
so i'll turn all of these into comments
12862
07:30:17,680 --> 07:30:20,478
and the next one we can go over is ask
12863
07:30:20,478 --> 07:30:22,000
retry cancel
12864
07:30:22,000 --> 07:30:23,760
and this is fairly similar to what we
12865
07:30:23,760 --> 07:30:27,840
had before i'm going to copy this
12866
07:30:28,638 --> 07:30:32,320
let's change this to ask
12867
07:30:32,320 --> 07:30:36,000
retry cancel ask retry
12868
07:30:36,000 --> 07:30:40,000
cancel do you want to
12869
07:30:40,000 --> 07:30:43,440
retry the thing
12870
07:30:44,878 --> 07:30:48,240
you retried a thing
12871
07:30:48,240 --> 07:30:51,200
or you cancelled a thing so it's very
12872
07:30:51,200 --> 07:30:52,958
similar to what we had before
12873
07:30:52,958 --> 07:30:55,760
do you want to retry the thing retry you
12874
07:30:55,760 --> 07:30:58,478
retried a thing
12875
07:30:58,478 --> 07:31:03,200
and cancel you cancelled a thing
12876
07:31:03,360 --> 07:31:06,958
okay the next one we have is ask yes
12877
07:31:06,958 --> 07:31:11,040
no so we can get some user input
12878
07:31:11,040 --> 07:31:14,160
if it's a yes or a no
12879
07:31:14,160 --> 07:31:17,440
so this returns a boolean value a true
12880
07:31:17,440 --> 07:31:18,000
or false
12881
07:31:18,000 --> 07:31:21,680
value so message box
12882
07:31:21,680 --> 07:31:24,798
dot ask
12883
07:31:24,798 --> 07:31:28,080
yes no
12884
07:31:28,080 --> 07:31:31,440
and we'll set the title title equals
12885
07:31:31,440 --> 07:31:35,680
ask yes or no
12886
07:31:35,680 --> 07:31:38,558
and a message
12887
07:31:40,798 --> 07:31:44,638
do you like
12888
07:31:44,840 --> 07:31:47,440
cake
12889
07:31:47,440 --> 07:31:50,000
so let's put this within an if statement
12890
07:31:50,000 --> 07:31:51,520
because this returns
12891
07:31:51,520 --> 07:31:56,000
a true or false answer print
12892
07:31:57,120 --> 07:32:01,440
i like cake too
12893
07:32:01,440 --> 07:32:05,200
else they answered no then if this is
12894
07:32:05,200 --> 07:32:06,000
false
12895
07:32:06,000 --> 07:32:09,040
print why do you
12896
07:32:09,040 --> 07:32:15,760
not like cake
12897
07:32:15,760 --> 07:32:19,600
and i'm missing a colon there we go
12898
07:32:19,600 --> 07:32:22,798
so if they click yes this returns true
12899
07:32:22,798 --> 07:32:25,440
and we execute this line of code i like
12900
07:32:25,440 --> 07:32:25,840
cake
12901
07:32:25,840 --> 07:32:28,638
too if they answer no it's going to
12902
07:32:28,638 --> 07:32:29,520
execute this
12903
07:32:29,520 --> 07:32:32,478
else statement do you like cake no why
12904
07:32:32,478 --> 07:32:33,280
do you not like
12905
07:32:33,280 --> 07:32:36,558
cake now what we have next is ask
12906
07:32:36,558 --> 07:32:38,558
question and this doesn't return a
12907
07:32:38,558 --> 07:32:40,638
boolean value of true or false
12908
07:32:40,638 --> 07:32:43,280
it actually returns a string of yes or
12909
07:32:43,280 --> 07:32:43,920
no
12910
07:32:43,920 --> 07:32:47,360
so this is message box
12911
07:32:47,360 --> 07:32:50,398
dot ask question
12912
07:32:50,398 --> 07:32:54,840
and we'll set the title to ask
12913
07:32:54,840 --> 07:32:57,600
question and a message
12914
07:32:57,600 --> 07:33:00,878
message equals do
12915
07:33:00,878 --> 07:33:05,280
you like pi
12916
07:33:05,600 --> 07:33:08,000
so let's just print the result what this
12917
07:33:08,000 --> 07:33:09,680
returns
12918
07:33:09,680 --> 07:33:13,760
so print everything within here
12919
07:33:13,760 --> 07:33:15,680
so like i said this doesn't return it
12920
07:33:15,680 --> 07:33:16,798
true or false
12921
07:33:16,798 --> 07:33:20,718
but yes or no
12922
07:33:20,718 --> 07:33:23,120
so what we could do is assign the
12923
07:33:23,120 --> 07:33:24,080
response to
12924
07:33:24,080 --> 07:33:27,120
a variable such as answer
12925
07:33:27,120 --> 07:33:30,240
so answer equals whatever the answer is
12926
07:33:30,240 --> 07:33:31,280
whatever they
12927
07:33:31,280 --> 07:33:33,520
click whatever button they click on so
12928
07:33:33,520 --> 07:33:34,398
if
12929
07:33:34,398 --> 07:33:38,718
our answer is equal to yes
12930
07:33:38,718 --> 07:33:42,240
will print a message print
12931
07:33:42,240 --> 07:33:46,240
i like pie 2.
12932
07:33:47,680 --> 07:33:51,280
then else the answer is no
12933
07:33:51,280 --> 07:33:54,958
will print why do you
12934
07:33:54,958 --> 07:33:58,558
not like pie
12935
07:34:00,558 --> 07:34:03,600
so do you like pie yes i like pie too do
12936
07:34:03,600 --> 07:34:04,638
you like pie
12937
07:34:04,638 --> 07:34:07,680
no why do you not like pie
12938
07:34:07,680 --> 07:34:10,080
so this next one is probably the most
12939
07:34:10,080 --> 07:34:10,958
difficult
12940
07:34:10,958 --> 07:34:15,200
it's ask yes no cancel
12941
07:34:15,200 --> 07:34:18,320
so it's message box dot
12942
07:34:18,320 --> 07:34:21,360
ask yes no cancel
12943
07:34:21,360 --> 07:34:23,680
it's this last one so let's set the
12944
07:34:23,680 --> 07:34:24,558
title
12945
07:34:24,558 --> 07:34:28,240
title equals whatever you want yes
12946
07:34:28,240 --> 07:34:35,280
no cancel and a message message
12947
07:34:35,280 --> 07:34:38,478
equals do you
12948
07:34:38,478 --> 07:34:41,680
like to code
12949
07:34:42,000 --> 07:34:45,120
so let's just print the results what
12950
07:34:45,120 --> 07:34:46,398
this returns
12951
07:34:46,398 --> 07:34:50,080
so print everything just so we can take
12952
07:34:50,080 --> 07:34:51,840
a look
12953
07:34:51,840 --> 07:34:55,040
so the possible answers are
12954
07:34:55,040 --> 07:34:58,638
true false
12955
07:34:58,638 --> 07:35:01,600
or none so we can just run some if
12956
07:35:01,600 --> 07:35:04,398
statements for this
12957
07:35:04,478 --> 07:35:07,920
so if
12958
07:35:07,920 --> 07:35:11,840
answer is equal to true
12959
07:35:11,840 --> 07:35:16,160
what we can do is print
12960
07:35:16,160 --> 07:35:20,000
you like to code
12961
07:35:20,718 --> 07:35:22,320
oh but we need to assign this to an
12962
07:35:22,320 --> 07:35:23,920
answer so answer
12963
07:35:23,920 --> 07:35:28,320
equals all of this
12964
07:35:29,760 --> 07:35:32,320
else if
12965
07:35:33,600 --> 07:35:37,520
answer is equal to false
12966
07:35:37,520 --> 07:35:41,120
let's print then
12967
07:35:41,120 --> 07:35:44,718
why are you watching a video
12968
07:35:44,718 --> 07:35:47,680
on coding
12969
07:35:48,478 --> 07:35:51,520
and then else the response is none so we
12970
07:35:51,520 --> 07:35:52,160
can just use an
12971
07:35:52,160 --> 07:35:55,680
else statement so else
12972
07:35:56,240 --> 07:36:01,280
you have dodged the question
12973
07:36:02,080 --> 07:36:05,120
okay let's try this
12974
07:36:05,280 --> 07:36:08,558
do you like to code yes you like to code
12975
07:36:08,558 --> 07:36:10,718
uh but this should really be a smiley
12976
07:36:10,718 --> 07:36:12,798
face right
12977
07:36:12,798 --> 07:36:15,280
do you like to code no then why are you
12978
07:36:15,280 --> 07:36:17,920
watching a video on coding
12979
07:36:17,920 --> 07:36:20,160
do you like to code cancel you have
12980
07:36:20,160 --> 07:36:22,240
dodged the question
12981
07:36:22,240 --> 07:36:24,398
no one other option available to you is
12982
07:36:24,398 --> 07:36:26,240
that you can change the icon that you're
12983
07:36:26,240 --> 07:36:27,120
using
12984
07:36:27,120 --> 07:36:29,040
so let's say that we want to change this
12985
07:36:29,040 --> 07:36:32,000
icon so right now it is currently
12986
07:36:32,000 --> 07:36:34,558
this question mark so we can set this to
12987
07:36:34,558 --> 07:36:35,040
one
12988
07:36:35,040 --> 07:36:38,320
of a few preset icons so icon
12989
07:36:38,320 --> 07:36:42,240
equals and let's set this to warning
12990
07:36:42,240 --> 07:36:44,160
so instead of that question mark we get
12991
07:36:44,160 --> 07:36:46,320
this yellow exclamation point
12992
07:36:46,320 --> 07:36:54,478
and you can also use info
12993
07:36:54,478 --> 07:36:59,680
it's just an eye and error
12994
07:37:02,558 --> 07:37:05,520
so do it fits best for you all right
12995
07:37:05,520 --> 07:37:06,080
well
12996
07:37:06,080 --> 07:37:08,718
that's the basics on message boxes in
12997
07:37:08,718 --> 07:37:10,558
python if you'd like a copy of all this
12998
07:37:10,558 --> 07:37:10,958
code
12999
07:37:10,958 --> 07:37:12,398
i'll post all of this in the comments
13000
07:37:12,398 --> 07:37:14,958
down below but yeah that's the basics of
13001
07:37:14,958 --> 07:37:18,398
message boxes in python hey what's going
13002
07:37:18,398 --> 07:37:19,920
on everybody it's your bro here hope
13003
07:37:19,920 --> 07:37:21,280
you're doing well and in this video i'm
13004
07:37:21,280 --> 07:37:23,040
going to teach you guys about the color
13005
07:37:23,040 --> 07:37:25,200
chooser module in python so
13006
07:37:25,200 --> 07:37:29,040
sit back relax and enjoy the show
13007
07:37:29,360 --> 07:37:31,280
okay everybody so before we begin it
13008
07:37:31,280 --> 07:37:33,280
we'll need a second import so let's add
13009
07:37:33,280 --> 07:37:34,080
this at the top
13010
07:37:34,080 --> 07:37:37,840
so from tk enter import
13011
07:37:37,840 --> 07:37:40,558
color chooser and the reason that we
13012
07:37:40,558 --> 07:37:42,398
need a second import and it's not
13013
07:37:42,398 --> 07:37:44,240
included with this first import
13014
07:37:44,240 --> 07:37:47,360
is because this is a sub module
13015
07:37:47,360 --> 07:37:49,760
it's not necessary to add this comment i
13016
07:37:49,760 --> 07:37:51,520
just like to add comments for teaching
13017
07:37:51,520 --> 07:37:52,398
purposes
13018
07:37:52,398 --> 07:37:54,558
so make sure you have these two imports
13019
07:37:54,558 --> 07:37:55,760
before we begin
13020
07:37:55,760 --> 07:37:58,000
now we'll need a window and a button
13021
07:37:58,000 --> 07:37:59,360
that we can add to the window
13022
07:37:59,360 --> 07:38:00,798
and when we click on the button we're
13023
07:38:00,798 --> 07:38:03,120
going to select a color and change the
13024
07:38:03,120 --> 07:38:04,000
background color
13025
07:38:04,000 --> 07:38:07,200
of the window so let's create a window
13026
07:38:07,200 --> 07:38:11,200
window equals tk followed by window.main
13027
07:38:11,200 --> 07:38:14,320
loop and then let's set the geometry so
13028
07:38:14,320 --> 07:38:15,280
we're going to change
13029
07:38:15,280 --> 07:38:17,520
the background color of the window after
13030
07:38:17,520 --> 07:38:18,798
we select a color
13031
07:38:18,798 --> 07:38:21,840
so let's select a fairly large size for
13032
07:38:21,840 --> 07:38:22,478
a window
13033
07:38:22,478 --> 07:38:25,600
maybe 420 by 420 so this is the width
13034
07:38:25,600 --> 07:38:26,878
and the height
13035
07:38:26,878 --> 07:38:29,280
and then let's create a button so button
13036
07:38:29,280 --> 07:38:31,040
equals button
13037
07:38:31,040 --> 07:38:34,320
let's set some text text equals
13038
07:38:34,320 --> 07:38:37,360
click me and we're going to
13039
07:38:37,360 --> 07:38:39,680
set a command for this button that is
13040
07:38:39,680 --> 07:38:41,360
called so we're going to create a
13041
07:38:41,360 --> 07:38:43,520
function maybe called click
13042
07:38:43,520 --> 07:38:45,360
and then we just need to pack this
13043
07:38:45,360 --> 07:38:47,040
button so we can add this to the window
13044
07:38:47,040 --> 07:38:48,958
so button.pack
13045
07:38:48,958 --> 07:38:50,478
now we're going to need to create a
13046
07:38:50,478 --> 07:38:53,040
function and we'll call it click
13047
07:38:53,040 --> 07:38:55,120
so when we click on the button this
13048
07:38:55,120 --> 07:38:56,798
function is going to be called
13049
07:38:56,798 --> 07:39:00,320
so def click and what do we want to do
13050
07:39:00,320 --> 07:39:01,920
after clicking this button
13051
07:39:01,920 --> 07:39:05,440
what we're going to do is take color
13052
07:39:05,440 --> 07:39:09,120
chooser and we're going to use the ask
13053
07:39:09,120 --> 07:39:12,558
color function so currently this is what
13054
07:39:12,558 --> 07:39:13,760
this does
13055
07:39:13,760 --> 07:39:15,760
we can select a color after clicking
13056
07:39:15,760 --> 07:39:17,520
this button and pick whatever color you
13057
07:39:17,520 --> 07:39:18,478
want
13058
07:39:18,478 --> 07:39:21,040
then you hit ok however we need to store
13059
07:39:21,040 --> 07:39:22,240
this color within
13060
07:39:22,240 --> 07:39:23,600
let's say a variable so we'll create a
13061
07:39:23,600 --> 07:39:26,000
variable called color color equals
13062
07:39:26,000 --> 07:39:28,638
color chooser dot ask color and then we
13063
07:39:28,638 --> 07:39:30,478
can do something with this color
13064
07:39:30,478 --> 07:39:32,558
but for now let's just print this color
13065
07:39:32,558 --> 07:39:33,920
just to take a look to see what it
13066
07:39:33,920 --> 07:39:34,718
contains
13067
07:39:34,718 --> 07:39:38,398
so print color
13068
07:39:39,840 --> 07:39:42,878
so let's say that i want this green
13069
07:39:42,878 --> 07:39:43,760
color
13070
07:39:43,760 --> 07:39:46,478
click ok this is what this prints this
13071
07:39:46,478 --> 07:39:47,680
is our color
13072
07:39:47,680 --> 07:39:51,280
so this is the rgb values that we have
13073
07:39:51,280 --> 07:39:51,920
in this
13074
07:39:51,920 --> 07:39:53,920
first element so this is the amount of
13075
07:39:53,920 --> 07:39:54,958
red
13076
07:39:54,958 --> 07:39:58,320
then green and then blue and the second
13077
07:39:58,320 --> 07:39:58,798
element
13078
07:39:58,798 --> 07:40:01,600
is the hexadecimal representation of
13079
07:40:01,600 --> 07:40:03,280
these values
13080
07:40:03,280 --> 07:40:06,558
so what we want is this hexadecimal
13081
07:40:06,558 --> 07:40:07,360
value
13082
07:40:07,360 --> 07:40:09,600
so what we can do is create a second
13083
07:40:09,600 --> 07:40:11,520
variable to store this
13084
07:40:11,520 --> 07:40:14,798
so let's just call this color hex
13085
07:40:14,798 --> 07:40:18,160
color hex equals color
13086
07:40:18,160 --> 07:40:21,120
and we're going to get the first element
13087
07:40:21,120 --> 07:40:22,638
well technically it's the second element
13088
07:40:22,638 --> 07:40:24,000
but it's the
13089
07:40:24,000 --> 07:40:27,600
element at position one so this one
13090
07:40:27,600 --> 07:40:30,958
this is zero index zero so we're going
13091
07:40:30,958 --> 07:40:31,280
to
13092
07:40:31,280 --> 07:40:33,840
basically extract this hexadecimal value
13093
07:40:33,840 --> 07:40:35,040
and let's just print this
13094
07:40:35,040 --> 07:40:36,878
just to be sure that it's working fine
13095
07:40:36,878 --> 07:40:38,958
so print color
13096
07:40:38,958 --> 07:40:41,520
hex and let's try this again but select
13097
07:40:41,520 --> 07:40:42,798
a different color
13098
07:40:42,798 --> 07:40:45,360
uh maybe i want this purple color then
13099
07:40:45,360 --> 07:40:46,878
click ok
13100
07:40:46,878 --> 07:40:50,558
so this is cb42ff
13101
07:40:50,558 --> 07:40:54,000
and here we have cb42ff
13102
07:40:54,000 --> 07:40:56,398
now let's change the background color so
13103
07:40:56,398 --> 07:40:58,320
since we have this hex value that's
13104
07:40:58,320 --> 07:40:59,680
actually fairly simple
13105
07:40:59,680 --> 07:41:04,320
so we're going to take window dot config
13106
07:41:05,040 --> 07:41:08,718
bg for background color equals
13107
07:41:08,718 --> 07:41:11,600
color hex
13108
07:41:11,760 --> 07:41:15,120
and this will change background
13109
07:41:15,120 --> 07:41:17,440
color
13110
07:41:18,638 --> 07:41:22,398
and let's set this to a sky blue color
13111
07:41:22,398 --> 07:41:25,520
maybe like this click ok and it changes
13112
07:41:25,520 --> 07:41:26,478
the color
13113
07:41:26,478 --> 07:41:28,080
however it's not really necessary to
13114
07:41:28,080 --> 07:41:30,320
continue printing these values you can
13115
07:41:30,320 --> 07:41:33,520
take these out if you want
13116
07:41:33,520 --> 07:41:35,600
and this would work like just the same
13117
07:41:35,600 --> 07:41:37,600
then
13118
07:41:37,600 --> 07:41:40,240
now this part isn't necessary but if you
13119
07:41:40,240 --> 07:41:40,638
want
13120
07:41:40,638 --> 07:41:42,558
you can condense these three lines of
13121
07:41:42,558 --> 07:41:44,798
code that we have for our click function
13122
07:41:44,798 --> 07:41:47,360
into less lines of code so what you
13123
07:41:47,360 --> 07:41:48,478
could do
13124
07:41:48,478 --> 07:41:52,240
instead of assigning the value of color
13125
07:41:52,240 --> 07:41:53,280
at index 1
13126
07:41:53,280 --> 07:41:56,000
to a separate variable we can take color
13127
07:41:56,000 --> 07:41:57,280
at index 1
13128
07:41:57,280 --> 07:42:00,160
get rid of this line and assign bg
13129
07:42:00,160 --> 07:42:01,280
equals color
13130
07:42:01,280 --> 07:42:03,920
at index one so now this is done with
13131
07:42:03,920 --> 07:42:04,398
just
13132
07:42:04,398 --> 07:42:06,958
two lines of code and why stop there we
13133
07:42:06,958 --> 07:42:08,320
can write this with just
13134
07:42:08,320 --> 07:42:10,320
one line of code so now we're going to
13135
07:42:10,320 --> 07:42:12,240
take color chooser dot ask
13136
07:42:12,240 --> 07:42:17,040
color get rid of this line bg equals
13137
07:42:17,040 --> 07:42:20,478
color chooser dot ask color at index
13138
07:42:20,478 --> 07:42:24,240
one and now this is done
13139
07:42:24,240 --> 07:42:27,200
with just one line of code however it's
13140
07:42:27,200 --> 07:42:27,680
a
13141
07:42:27,680 --> 07:42:29,520
i would say a little more difficult to
13142
07:42:29,520 --> 07:42:30,798
teach
13143
07:42:30,798 --> 07:42:32,478
so that's why i kind of like to separate
13144
07:42:32,478 --> 07:42:34,398
this line by line
13145
07:42:34,398 --> 07:42:36,000
so if you want you can condense all of
13146
07:42:36,000 --> 07:42:38,320
this into just one line of code
13147
07:42:38,320 --> 07:42:40,798
so here's some ideas of where the color
13148
07:42:40,798 --> 07:42:43,040
chooser sub module could be helpful
13149
07:42:43,040 --> 07:42:44,320
so of course you can change the
13150
07:42:44,320 --> 07:42:46,718
background color too but let's say that
13151
07:42:46,718 --> 07:42:48,638
you're creating a game and you can
13152
07:42:48,638 --> 07:42:51,280
customize your character or avatar
13153
07:42:51,280 --> 07:42:52,638
well it would be kind of nice if you
13154
07:42:52,638 --> 07:42:54,478
could have some way for the user to
13155
07:42:54,478 --> 07:42:56,478
actually change maybe the character's
13156
07:42:56,478 --> 07:42:58,718
hair color eye color something like this
13157
07:42:58,718 --> 07:43:00,638
would actually work fairly well
13158
07:43:00,638 --> 07:43:02,718
so yeah that's the basics of color
13159
07:43:02,718 --> 07:43:04,398
chooser if you'd like a copy of all this
13160
07:43:04,398 --> 07:43:04,798
code
13161
07:43:04,798 --> 07:43:06,240
i'll post this in the comments down
13162
07:43:06,240 --> 07:43:08,160
below but that's the basics of
13163
07:43:08,160 --> 07:43:11,360
color chooser in python hey what's going
13164
07:43:11,360 --> 07:43:12,958
on everybody it's bro here hope you're
13165
07:43:12,958 --> 07:43:14,320
doing well and in this video i'm going
13166
07:43:14,320 --> 07:43:15,840
to teach you guys how we can create a
13167
07:43:15,840 --> 07:43:18,558
text area in python so sit back relax
13168
07:43:18,558 --> 07:43:21,440
and enjoy the show
13169
07:43:21,760 --> 07:43:24,080
all right my fellow bros let's discuss
13170
07:43:24,080 --> 07:43:24,798
the text
13171
07:43:24,798 --> 07:43:27,840
widget this functions like a text area
13172
07:43:27,840 --> 07:43:30,718
you can enter multiple lines of text i'm
13173
07:43:30,718 --> 07:43:31,520
not sure why
13174
07:43:31,520 --> 07:43:33,680
it's called a text widget instead of a
13175
07:43:33,680 --> 07:43:36,080
text area widget text area would make
13176
07:43:36,080 --> 07:43:37,120
more sense
13177
07:43:37,120 --> 07:43:39,520
but idk though so let's begin by
13178
07:43:39,520 --> 07:43:41,040
creating a window
13179
07:43:41,040 --> 07:43:44,320
window equals tk followed by window.main
13180
07:43:44,320 --> 07:43:46,558
loop
13181
07:43:46,558 --> 07:43:49,760
so to create a text widget also known as
13182
07:43:49,760 --> 07:43:51,280
a text area
13183
07:43:51,280 --> 07:43:54,638
let's call this text text equals text
13184
07:43:54,638 --> 07:43:56,878
and we're going to add this text to our
13185
07:43:56,878 --> 07:43:58,638
window and then we need to
13186
07:43:58,638 --> 07:44:03,120
pack this text text dot pack
13187
07:44:03,120 --> 07:44:06,240
so we should have a text area
13188
07:44:06,240 --> 07:44:09,440
and you can enter in
13189
07:44:09,440 --> 07:44:13,520
stuff like this
13190
07:44:13,520 --> 07:44:16,558
all right but what good is a text area
13191
07:44:16,558 --> 07:44:18,160
if you can't actually submit
13192
07:44:18,160 --> 07:44:20,478
some text so let's create a button to do
13193
07:44:20,478 --> 07:44:21,680
that for us
13194
07:44:21,680 --> 07:44:24,958
so button equals button
13195
07:44:24,958 --> 07:44:26,398
we're going to add this button to the
13196
07:44:26,398 --> 07:44:28,798
window
13197
07:44:28,798 --> 07:44:32,558
and button dot pack
13198
07:44:32,798 --> 07:44:36,000
so we need a command for this button
13199
07:44:36,000 --> 07:44:39,040
and a function as well so let's create a
13200
07:44:39,040 --> 07:44:40,958
function called submit it's going to be
13201
07:44:40,958 --> 07:44:42,240
a submit button
13202
07:44:42,240 --> 07:44:44,398
and let's change the text to because we
13203
07:44:44,398 --> 07:44:45,280
can
13204
07:44:45,280 --> 07:44:50,958
text equals submit
13205
07:44:50,958 --> 07:44:53,600
cool now let's create a function called
13206
07:44:53,600 --> 07:44:54,160
submit
13207
07:44:54,160 --> 07:44:56,958
and we'll do this at the top d e f
13208
07:44:56,958 --> 07:44:58,558
submit
13209
07:44:58,558 --> 07:45:01,360
and what is this going to do well it's
13210
07:45:01,360 --> 07:45:02,080
going to
13211
07:45:02,080 --> 07:45:05,280
get the text from the text area and
13212
07:45:05,280 --> 07:45:07,280
let's assign this to a variable and we
13213
07:45:07,280 --> 07:45:09,120
can call this maybe input or
13214
07:45:09,120 --> 07:45:12,558
whatever you want so to get the text
13215
07:45:12,558 --> 07:45:14,320
from the text area
13216
07:45:14,320 --> 07:45:16,958
let's say text that's the name of this
13217
07:45:16,958 --> 07:45:17,360
text
13218
07:45:17,360 --> 07:45:20,958
area dot get but we need
13219
07:45:20,958 --> 07:45:23,680
a starting index as well as an ending
13220
07:45:23,680 --> 07:45:24,398
index
13221
07:45:24,398 --> 07:45:27,760
so we're going to place 1.0 here
13222
07:45:27,760 --> 07:45:29,840
for the beginning index this is the
13223
07:45:29,840 --> 07:45:30,798
first line
13224
07:45:30,798 --> 07:45:33,200
we want to get everything so for the
13225
07:45:33,200 --> 07:45:34,638
second index we can just put
13226
07:45:34,638 --> 07:45:37,760
end then we can do anything we want with
13227
07:45:37,760 --> 07:45:39,600
this input so let's just print this to
13228
07:45:39,600 --> 07:45:40,878
the console window
13229
07:45:40,878 --> 07:45:44,160
so print input
13230
07:45:44,160 --> 07:45:47,840
and this should work
13231
07:45:49,440 --> 07:45:52,558
hey you drink plenty
13232
07:45:52,558 --> 07:45:55,680
of water love you
13233
07:45:55,680 --> 07:45:58,558
and click submit and then you can do
13234
07:45:58,558 --> 07:45:59,840
whatever you want with this
13235
07:45:59,840 --> 07:46:02,320
input well that's how a text area
13236
07:46:02,320 --> 07:46:04,398
functions but now let's customize the
13237
07:46:04,398 --> 07:46:06,398
appearance of this text area and i have
13238
07:46:06,398 --> 07:46:08,478
a few ideas that you might be interested
13239
07:46:08,478 --> 07:46:09,040
in
13240
07:46:09,040 --> 07:46:11,200
so let's change the background color of
13241
07:46:11,200 --> 07:46:12,398
this text area
13242
07:46:12,398 --> 07:46:15,440
so bg equals and i will pick
13243
07:46:15,440 --> 07:46:17,360
light yellow and you will see why in
13244
07:46:17,360 --> 07:46:19,680
just a second
13245
07:46:19,680 --> 07:46:22,240
so now this text area kind of resembles
13246
07:46:22,240 --> 07:46:23,920
maybe a piece of paper
13247
07:46:23,920 --> 07:46:27,040
a notebook a post-it note however
13248
07:46:27,040 --> 07:46:30,240
this font is really small so let's
13249
07:46:30,240 --> 07:46:31,440
change the font
13250
07:46:31,440 --> 07:46:35,440
styling and the font size so
13251
07:46:35,440 --> 07:46:39,040
font equals and i will pick the font
13252
07:46:39,040 --> 07:46:42,080
ink free this somewhat resembles
13253
07:46:42,080 --> 07:46:43,040
handwriting so
13254
07:46:43,040 --> 07:46:45,200
i kind of like this and it kind of fits
13255
07:46:45,200 --> 07:46:46,878
our notebook theme
13256
07:46:46,878 --> 07:46:49,600
and then a size maybe 25. one thing you
13257
07:46:49,600 --> 07:46:50,160
should know
13258
07:46:50,160 --> 07:46:53,440
the text area size corresponds directly
13259
07:46:53,440 --> 07:46:53,840
with
13260
07:46:53,840 --> 07:46:57,520
the font size so 25 is a fairly
13261
07:46:57,520 --> 07:47:00,638
large font so if we were to compile this
13262
07:47:00,638 --> 07:47:01,840
and run this
13263
07:47:01,840 --> 07:47:05,280
the size of our text area is now massive
13264
07:47:05,280 --> 07:47:07,680
because it corresponds directly with the
13265
07:47:07,680 --> 07:47:08,638
font size
13266
07:47:08,638 --> 07:47:10,718
so we might want to limit the width and
13267
07:47:10,718 --> 07:47:11,680
the height
13268
07:47:11,680 --> 07:47:14,558
so the height is the amount of
13269
07:47:14,558 --> 07:47:16,558
characters that this is tall
13270
07:47:16,558 --> 07:47:19,440
so let's say 8 and the width is the
13271
07:47:19,440 --> 07:47:20,638
amount of characters
13272
07:47:20,638 --> 07:47:24,240
that this is long and let's say 20.
13273
07:47:24,240 --> 07:47:27,120
and now this should be much smaller and
13274
07:47:27,120 --> 07:47:28,718
it kind of resembles a post-it note
13275
07:47:28,718 --> 07:47:31,680
which is kind of cool let's also change
13276
07:47:31,680 --> 07:47:34,240
the amount of padding that we have
13277
07:47:34,240 --> 07:47:36,718
we can do that with pad x and pad y so
13278
07:47:36,718 --> 07:47:37,360
pad x
13279
07:47:37,360 --> 07:47:40,398
let's say 20 and pad
13280
07:47:40,398 --> 07:47:43,920
y also 20. so the text shouldn't
13281
07:47:43,920 --> 07:47:47,040
actually touch any of the borders
13282
07:47:47,040 --> 07:47:50,478
this is some sample
13283
07:47:50,478 --> 07:47:54,478
text cool let's finish this by changing
13284
07:47:54,478 --> 07:47:56,240
the font color so that is
13285
07:47:56,240 --> 07:47:59,520
fg for foreground foreground equals
13286
07:47:59,520 --> 07:48:02,080
and i'll pick purple because purple is a
13287
07:48:02,080 --> 07:48:03,520
pretty color
13288
07:48:03,520 --> 07:48:07,280
and let's run this one last time
13289
07:48:07,280 --> 07:48:10,398
roses are red
13290
07:48:10,398 --> 07:48:14,398
violets are blue
13291
07:48:14,558 --> 07:48:19,680
booty booty booty booty
13292
07:48:19,680 --> 07:48:24,000
rockin everywhere
13293
07:48:25,440 --> 07:48:27,920
well that's the basics of the text
13294
07:48:27,920 --> 07:48:29,600
widget in python if you would like a
13295
07:48:29,600 --> 07:48:30,798
copy of all this code
13296
07:48:30,798 --> 07:48:32,320
i'll post all of this in the comments
13297
07:48:32,320 --> 07:48:34,718
down below but yeah that's the basics of
13298
07:48:34,718 --> 07:48:37,840
the text widget in python
13299
07:48:37,840 --> 07:48:39,360
hey what's going on everybody it's your
13300
07:48:39,360 --> 07:48:41,120
bro here hope you're doing well and in
13301
07:48:41,120 --> 07:48:42,398
this video i'm going to teach you guys
13302
07:48:42,398 --> 07:48:44,080
how we can use a file dialog
13303
07:48:44,080 --> 07:48:46,320
in python to open and read the contents
13304
07:48:46,320 --> 07:48:47,760
of a file so sit back
13305
07:48:47,760 --> 07:48:51,040
relax and enjoy the show
13306
07:48:51,600 --> 07:48:54,160
okay people so this is how you can use
13307
07:48:54,160 --> 07:48:55,120
file dialog
13308
07:48:55,120 --> 07:48:57,600
to open and read the contents of a file
13309
07:48:57,600 --> 07:48:59,680
but first we'll need a second import
13310
07:48:59,680 --> 07:49:01,520
along with this import at the top
13311
07:49:01,520 --> 07:49:03,520
so what we're going to be importing is
13312
07:49:03,520 --> 07:49:05,200
from tk
13313
07:49:05,200 --> 07:49:08,558
enter import file
13314
07:49:08,558 --> 07:49:11,680
dialog and then we'll need a window
13315
07:49:11,680 --> 07:49:15,280
like what we usually do so window equals
13316
07:49:15,280 --> 07:49:18,398
tk followed by window dot
13317
07:49:18,398 --> 07:49:21,040
main loop now we'll need a button that
13318
07:49:21,040 --> 07:49:22,398
we're going to add to the window and
13319
07:49:22,398 --> 07:49:23,920
when we click on this button
13320
07:49:23,920 --> 07:49:26,320
it's going to launch our file dialog so
13321
07:49:26,320 --> 07:49:27,760
we can select a file
13322
07:49:27,760 --> 07:49:30,798
so we'll call this button button equals
13323
07:49:30,798 --> 07:49:31,360
button
13324
07:49:31,360 --> 07:49:34,398
let's set some text maybe to just open
13325
07:49:34,398 --> 07:49:36,878
like we're opening a file and we'll
13326
07:49:36,878 --> 07:49:39,280
associate a function to our command
13327
07:49:39,280 --> 07:49:41,680
for this button so let's create a
13328
07:49:41,680 --> 07:49:43,280
function called
13329
07:49:43,280 --> 07:49:46,958
open file that's fine and then we need
13330
07:49:46,958 --> 07:49:47,280
to
13331
07:49:47,280 --> 07:49:49,040
pack this button so we can add it to the
13332
07:49:49,040 --> 07:49:51,360
screen so button.pack
13333
07:49:51,360 --> 07:49:53,440
now we need to create a function called
13334
07:49:53,440 --> 07:49:55,440
open file and we'll do this before we
13335
07:49:55,440 --> 07:49:57,120
create our window let's type
13336
07:49:57,120 --> 07:50:00,798
def open file
13337
07:50:00,798 --> 07:50:03,040
and when we call this function we're
13338
07:50:03,040 --> 07:50:04,478
going to
13339
07:50:04,478 --> 07:50:07,840
take file dialog and use the
13340
07:50:07,840 --> 07:50:11,200
ask open file name function
13341
07:50:11,200 --> 07:50:14,240
so this is going to return a string
13342
07:50:14,240 --> 07:50:16,638
and that string is the file path of
13343
07:50:16,638 --> 07:50:18,718
where your file is located
13344
07:50:18,718 --> 07:50:21,520
so what we could do is store this within
13345
07:50:21,520 --> 07:50:22,558
a variable
13346
07:50:22,558 --> 07:50:24,478
so let's store this within a variable
13347
07:50:24,478 --> 07:50:26,000
called file path
13348
07:50:26,000 --> 07:50:29,600
and to test this let's just print this
13349
07:50:29,600 --> 07:50:31,680
just to take a look at the contents of
13350
07:50:31,680 --> 07:50:32,718
this variable
13351
07:50:32,718 --> 07:50:35,680
so let's just print the file path now i
13352
07:50:35,680 --> 07:50:37,840
have a file on my desktop that i want to
13353
07:50:37,840 --> 07:50:39,360
use called test file
13354
07:50:39,360 --> 07:50:42,320
so let's click the open button and this
13355
07:50:42,320 --> 07:50:44,000
might not necessarily bring you to your
13356
07:50:44,000 --> 07:50:45,760
desktop it might bring you someplace
13357
07:50:45,760 --> 07:50:46,878
else within your
13358
07:50:46,878 --> 07:50:49,520
file directory so this just happens to
13359
07:50:49,520 --> 07:50:50,000
be
13360
07:50:50,000 --> 07:50:52,000
bring me to my desktop so i'm going to
13361
07:50:52,000 --> 07:50:54,160
click the test file that i have and
13362
07:50:54,160 --> 07:50:54,558
click
13363
07:50:54,558 --> 07:50:57,840
open so this prints the file path
13364
07:50:57,840 --> 07:51:00,478
so it's going to return a string and
13365
07:51:00,478 --> 07:51:02,080
it's the file path basically
13366
07:51:02,080 --> 07:51:04,558
so now we want to open and read the
13367
07:51:04,558 --> 07:51:05,920
contents of this file
13368
07:51:05,920 --> 07:51:07,920
so we don't really need this print line
13369
07:51:07,920 --> 07:51:09,520
anymore so
13370
07:51:09,520 --> 07:51:12,878
what we'll do is create a file
13371
07:51:12,878 --> 07:51:16,160
variable and we're going to use open
13372
07:51:16,160 --> 07:51:18,798
we're going to list the file path and
13373
07:51:18,798 --> 07:51:20,478
then we're going to use
13374
07:51:20,478 --> 07:51:23,520
r for read so the default is normally rt
13375
07:51:23,520 --> 07:51:25,200
for read text otherwise you can read
13376
07:51:25,200 --> 07:51:28,160
binary uh but we can just set this to r
13377
07:51:28,160 --> 07:51:30,958
r is the same as rt basically uh so then
13378
07:51:30,958 --> 07:51:32,320
we're going to open this file
13379
07:51:32,320 --> 07:51:35,600
and then we're going to read it so let's
13380
07:51:35,600 --> 07:51:37,840
just print this to the console window
13381
07:51:37,840 --> 07:51:41,280
so print the name of the file which is
13382
07:51:41,280 --> 07:51:41,840
file
13383
07:51:41,840 --> 07:51:45,600
where this is located file dot
13384
07:51:45,600 --> 07:51:47,920
read function and then it's a good idea
13385
07:51:47,920 --> 07:51:49,040
to close your files
13386
07:51:49,040 --> 07:51:51,520
after you're done with them so file dot
13387
07:51:51,520 --> 07:51:52,798
close
13388
07:51:52,798 --> 07:51:55,840
and let's test this so i'm going to
13389
07:51:55,840 --> 07:51:56,240
click
13390
07:51:56,240 --> 07:52:00,478
open find the file that i want
13391
07:52:00,478 --> 07:52:05,280
click it and then open
13392
07:52:05,280 --> 07:52:07,760
and that text file says my name is bro i
13393
07:52:07,760 --> 07:52:08,798
like pizza
13394
07:52:08,798 --> 07:52:10,320
now let's say you want to make this a
13395
07:52:10,320 --> 07:52:12,160
little more advanced so what i'm going
13396
07:52:12,160 --> 07:52:12,718
to do
13397
07:52:12,718 --> 07:52:14,878
is that i'm going to set the initial
13398
07:52:14,878 --> 07:52:16,558
directory of where we begin
13399
07:52:16,558 --> 07:52:18,558
looking for a file when we launch our
13400
07:52:18,558 --> 07:52:19,680
file dialog
13401
07:52:19,680 --> 07:52:21,680
so with that test file i'm going to move
13402
07:52:21,680 --> 07:52:23,280
this to my project folder so i'm just
13403
07:52:23,280 --> 07:52:26,080
going to paste it
13404
07:52:26,240 --> 07:52:28,878
so now this is within my project folder
13405
07:52:28,878 --> 07:52:30,478
now i'm going to set the initial
13406
07:52:30,478 --> 07:52:31,760
directory within
13407
07:52:31,760 --> 07:52:35,040
the function of ask open file name
13408
07:52:35,040 --> 07:52:38,240
so we're going to set the initial d
13409
07:52:38,240 --> 07:52:40,718
i r for initial directory and then we
13410
07:52:40,718 --> 07:52:42,478
can list a file path
13411
07:52:42,478 --> 07:52:45,200
so one thing you could do i want to get
13412
07:52:45,200 --> 07:52:46,000
this
13413
07:52:46,000 --> 07:52:47,920
path here this file path so if you're
13414
07:52:47,920 --> 07:52:49,920
using pycharm what you could do is right
13415
07:52:49,920 --> 07:52:50,798
click this
13416
07:52:50,798 --> 07:52:53,680
copy path and then get the absolute path
13417
07:52:53,680 --> 07:52:55,840
and then i'm just going to paste this
13418
07:52:55,840 --> 07:52:58,080
and then uh with these backslashes
13419
07:52:58,080 --> 07:52:59,680
you'll have to do double backslashes
13420
07:52:59,680 --> 07:53:00,240
because
13421
07:53:00,240 --> 07:53:02,638
this is the escape character for a
13422
07:53:02,638 --> 07:53:04,000
backslash
13423
07:53:04,000 --> 07:53:06,958
alright so this should open my project
13424
07:53:06,958 --> 07:53:08,240
folder
13425
07:53:08,240 --> 07:53:12,080
which it does and here's that test file
13426
07:53:12,080 --> 07:53:13,920
another thing we could do is that we can
13427
07:53:13,920 --> 07:53:16,478
change the title of our file dialog
13428
07:53:16,478 --> 07:53:18,958
right now the default is just open so we
13429
07:53:18,958 --> 07:53:20,798
can change that to something else
13430
07:53:20,798 --> 07:53:22,878
so i'm going to add a comma maybe put
13431
07:53:22,878 --> 07:53:24,478
this on a different line
13432
07:53:24,478 --> 07:53:27,040
uh so this is still within the function
13433
07:53:27,040 --> 07:53:28,638
of ask open file name
13434
07:53:28,638 --> 07:53:30,240
so i'm going to set the title to
13435
07:53:30,240 --> 07:53:32,798
something else i'll say
13436
07:53:32,798 --> 07:53:35,840
open file
13437
07:53:35,840 --> 07:53:40,000
okay so if we were to run this again
13438
07:53:40,000 --> 07:53:43,040
instead of just saying open it says open
13439
07:53:43,040 --> 07:53:46,638
file okay and we can also limit the
13440
07:53:46,638 --> 07:53:49,680
file types that we accept and look for
13441
07:53:49,680 --> 07:53:54,240
so we can use file types
13442
07:53:55,280 --> 07:53:57,200
so let's say that we want to initially
13443
07:53:57,200 --> 07:53:58,478
look for a
13444
07:53:58,478 --> 07:54:04,160
txt file so what we'll do is type in
13445
07:54:04,638 --> 07:54:06,080
like a name for the file that we're
13446
07:54:06,080 --> 07:54:07,920
looking for so this is what's going to
13447
07:54:07,920 --> 07:54:09,120
appear to the user
13448
07:54:09,120 --> 07:54:12,478
uh text files followed by an extension
13449
07:54:12,478 --> 07:54:16,240
so we want asterisk
13450
07:54:16,240 --> 07:54:19,280
dot txt
13451
07:54:19,280 --> 07:54:21,280
and then we can also have an option for
13452
07:54:21,280 --> 07:54:22,320
all files
13453
07:54:22,320 --> 07:54:24,398
so i'm going to add a comma for a second
13454
07:54:24,398 --> 07:54:26,240
option
13455
07:54:26,240 --> 07:54:29,520
and then let's say all
13456
07:54:29,520 --> 07:54:34,558
files and then an extension so comma
13457
07:54:34,558 --> 07:54:37,440
asterisk dot asterisk this will look for
13458
07:54:37,440 --> 07:54:38,718
all files
13459
07:54:38,718 --> 07:54:40,478
uh but i think i am missing a
13460
07:54:40,478 --> 07:54:42,878
parenthesis
13461
07:54:42,878 --> 07:54:44,798
there that should be good so then when
13462
07:54:44,798 --> 07:54:47,040
we open our file dialog
13463
07:54:47,040 --> 07:54:49,440
it's going to initially be looking for
13464
07:54:49,440 --> 07:54:50,958
plain text files
13465
07:54:50,958 --> 07:54:52,958
and with this drop down menu it says
13466
07:54:52,958 --> 07:54:54,320
text files and
13467
07:54:54,320 --> 07:54:56,558
all files so that's what we have listed
13468
07:54:56,558 --> 07:54:58,878
within our file types of what we want to
13469
07:54:58,878 --> 07:55:00,000
search for
13470
07:55:00,000 --> 07:55:02,320
so you can see that if i switch to all
13471
07:55:02,320 --> 07:55:03,360
files
13472
07:55:03,360 --> 07:55:06,160
our python file actually appears because
13473
07:55:06,160 --> 07:55:08,478
we don't have any limits on the files
13474
07:55:08,478 --> 07:55:10,638
that we're trying to open or search for
13475
07:55:10,638 --> 07:55:13,040
so if i change this back to text files
13476
07:55:13,040 --> 07:55:14,478
only folders and
13477
07:55:14,478 --> 07:55:16,958
plain text files are visible so then we
13478
07:55:16,958 --> 07:55:19,680
can open our test file again
13479
07:55:19,680 --> 07:55:22,080
but yeah that's the basics of file
13480
07:55:22,080 --> 07:55:23,920
dialog in the next video we're going to
13481
07:55:23,920 --> 07:55:24,718
be saving
13482
07:55:24,718 --> 07:55:26,718
a file so if you'd like a copy of all
13483
07:55:26,718 --> 07:55:28,000
this code i'll post
13484
07:55:28,000 --> 07:55:29,600
all of this in the comments down below
13485
07:55:29,600 --> 07:55:31,760
but yeah that's the basics of opening a
13486
07:55:31,760 --> 07:55:32,320
file
13487
07:55:32,320 --> 07:55:34,718
using python hey what's going on
13488
07:55:34,718 --> 07:55:36,240
everybody it's your bro here hope you're
13489
07:55:36,240 --> 07:55:37,760
doing well and in this video i'm going
13490
07:55:37,760 --> 07:55:39,200
to teach you guys how we can use
13491
07:55:39,200 --> 07:55:42,160
python's file dialog to save a file
13492
07:55:42,160 --> 07:55:43,600
someplace on your computer
13493
07:55:43,600 --> 07:55:48,478
so sit back relax and enjoy the show
13494
07:55:48,478 --> 07:55:50,958
okay everybody so this is how we can
13495
07:55:50,958 --> 07:55:52,000
save a file
13496
07:55:52,000 --> 07:55:54,320
we're going to be reusing a file dialog
13497
07:55:54,320 --> 07:55:56,718
again kind of like what we did with the
13498
07:55:56,718 --> 07:55:59,600
video on opening and reading a file so
13499
07:55:59,600 --> 07:56:00,320
from
13500
07:56:00,320 --> 07:56:03,440
tk enter import file dialog we're going
13501
07:56:03,440 --> 07:56:05,200
to be creating a window and adding a
13502
07:56:05,200 --> 07:56:07,600
button as well as a text area to this
13503
07:56:07,600 --> 07:56:08,398
window
13504
07:56:08,398 --> 07:56:10,878
so let's create our window window equals
13505
07:56:10,878 --> 07:56:11,840
tk
13506
07:56:11,840 --> 07:56:15,120
followed by window.main loop
13507
07:56:15,120 --> 07:56:18,398
let's create a button so button equals
13508
07:56:18,398 --> 07:56:19,440
button
13509
07:56:19,440 --> 07:56:22,160
let's set the text of the button so text
13510
07:56:22,160 --> 07:56:22,798
equals
13511
07:56:22,798 --> 07:56:25,440
save it's going to be a save button and
13512
07:56:25,440 --> 07:56:26,718
then a command
13513
07:56:26,718 --> 07:56:28,878
so we'll need to link a function to this
13514
07:56:28,878 --> 07:56:30,878
command so let's create a
13515
07:56:30,878 --> 07:56:34,080
save file function and then we need to
13516
07:56:34,080 --> 07:56:34,558
pack
13517
07:56:34,558 --> 07:56:38,320
this button so button dot pack
13518
07:56:38,320 --> 07:56:40,718
now let's create a text area this is
13519
07:56:40,718 --> 07:56:41,440
just called
13520
07:56:41,440 --> 07:56:44,638
text so text equals text we're going to
13521
07:56:44,638 --> 07:56:46,240
add this to the window
13522
07:56:46,240 --> 07:56:49,440
and we need to pack this text so text
13523
07:56:49,440 --> 07:56:52,878
dot pack and now let's create
13524
07:56:52,878 --> 07:56:56,558
a save file function so at the top of
13525
07:56:56,558 --> 07:56:57,680
our program
13526
07:56:57,680 --> 07:57:02,558
d e f save file
13527
07:57:02,558 --> 07:57:05,440
and then we're going to take file dialog
13528
07:57:05,440 --> 07:57:06,558
and use the
13529
07:57:06,558 --> 07:57:09,840
ask save as file function and we're
13530
07:57:09,840 --> 07:57:11,120
going to store
13531
07:57:11,120 --> 07:57:14,478
the contents within file file equals
13532
07:57:14,478 --> 07:57:18,080
file dialog dot ask save as file
13533
07:57:18,080 --> 07:57:21,120
let's run this just to test it out so we
13534
07:57:21,120 --> 07:57:21,920
have
13535
07:57:21,920 --> 07:57:25,360
our window a save button and this
13536
07:57:25,360 --> 07:57:28,878
is our text area
13537
07:57:28,878 --> 07:57:32,798
we can type in multiple
13538
07:57:32,798 --> 07:57:35,840
lines of text
13539
07:57:35,840 --> 07:57:39,920
yay and let's try and save this just to
13540
07:57:39,920 --> 07:57:41,600
see what we have so far
13541
07:57:41,600 --> 07:57:43,840
so this will bring me to my desktop it
13542
07:57:43,840 --> 07:57:46,320
might bring you someplace else for
13543
07:57:46,320 --> 07:57:48,638
your computer so i'm just going to save
13544
07:57:48,638 --> 07:57:50,320
this as
13545
07:57:50,320 --> 07:57:53,760
test file now with this drop down menu
13546
07:57:53,760 --> 07:57:56,718
save as type there's nothing set up so
13547
07:57:56,718 --> 07:57:58,558
we'll need to do that manually
13548
07:57:58,558 --> 07:58:00,718
uh if we were to save this and here's
13549
07:58:00,718 --> 07:58:02,718
that test file on my desktop
13550
07:58:02,718 --> 07:58:04,878
so let's take a look at the properties
13551
07:58:04,878 --> 07:58:06,160
we did not list a
13552
07:58:06,160 --> 07:58:09,040
file extension so the type of file just
13553
07:58:09,040 --> 07:58:09,520
says
13554
07:58:09,520 --> 07:58:11,760
file so we'll get to that later on in
13555
07:58:11,760 --> 07:58:12,718
this video
13556
07:58:12,718 --> 07:58:14,798
and if we were to open this with a
13557
07:58:14,798 --> 07:58:16,478
program like notepad
13558
07:58:16,478 --> 07:58:18,398
well it's currently empty that's because
13559
07:58:18,398 --> 07:58:19,760
we need to write
13560
07:58:19,760 --> 07:58:22,718
some text to this file so let's do that
13561
07:58:22,718 --> 07:58:23,440
next
13562
07:58:23,440 --> 07:58:26,160
let's store all of the text from our
13563
07:58:26,160 --> 07:58:28,320
text area to a variable
13564
07:58:28,320 --> 07:58:32,160
let's call this variable file text
13565
07:58:32,160 --> 07:58:35,200
file text equals and then we need to get
13566
07:58:35,200 --> 07:58:38,638
all of the text from our text area so
13567
07:58:38,638 --> 07:58:42,000
text dot get and we're going to list the
13568
07:58:42,000 --> 07:58:44,638
starting index as well as the ending
13569
07:58:44,638 --> 07:58:45,440
index
13570
07:58:45,440 --> 07:58:47,760
for this function so we're going to
13571
07:58:47,760 --> 07:58:48,478
begin at
13572
07:58:48,478 --> 07:58:51,280
1.0 that is the beginning of this text
13573
07:58:51,280 --> 07:58:51,920
area
13574
07:58:51,920 --> 07:58:54,160
and the ending we can just put end and
13575
07:58:54,160 --> 07:58:55,520
then we need to convert
13576
07:58:55,520 --> 07:58:57,920
all of this into a string so let's
13577
07:58:57,920 --> 07:58:59,398
surround this with
13578
07:58:59,398 --> 07:59:02,558
str and then we can store all of the
13579
07:59:02,558 --> 07:59:03,040
text
13580
07:59:03,040 --> 07:59:06,320
from our text area within this variable
13581
07:59:06,320 --> 07:59:09,680
file text and now we just need to write
13582
07:59:09,680 --> 07:59:12,878
our text to this file so file dot
13583
07:59:12,878 --> 07:59:15,840
write and we're going to pass in file
13584
07:59:15,840 --> 07:59:16,878
text
13585
07:59:16,878 --> 07:59:18,958
and then it's a good idea to close your
13586
07:59:18,958 --> 07:59:20,558
files when you're done using them so
13587
07:59:20,558 --> 07:59:21,120
file
13588
07:59:21,120 --> 07:59:25,760
dot close and let's try this again
13589
07:59:26,958 --> 07:59:30,160
hello my name
13590
07:59:30,160 --> 07:59:34,478
is bro let's save this
13591
07:59:34,478 --> 07:59:38,558
i'll call this hello and click save
13592
07:59:38,558 --> 07:59:40,638
and we still can't save s-type so we'll
13593
07:59:40,638 --> 07:59:42,398
work on that next
13594
07:59:42,398 --> 07:59:44,798
let's go to my desktop here's the test
13595
07:59:44,798 --> 07:59:46,638
file that we created i'm going to open
13596
07:59:46,638 --> 07:59:48,878
this with notepad and it has all of the
13597
07:59:48,878 --> 07:59:49,360
text
13598
07:59:49,360 --> 07:59:52,320
that we added to that text area now
13599
07:59:52,320 --> 07:59:53,200
let's list
13600
07:59:53,200 --> 07:59:55,440
some available file extensions that we
13601
07:59:55,440 --> 07:59:56,478
can have the user
13602
07:59:56,478 --> 07:59:59,600
save this file as so let's begin by
13603
07:59:59,600 --> 08:00:00,160
setting
13604
08:00:00,160 --> 08:00:04,000
the default extension so default
13605
08:00:04,000 --> 08:00:07,680
extension and let's set this to
13606
08:00:07,680 --> 08:00:11,718
a text file that is dot
13607
08:00:11,718 --> 08:00:14,558
txt and then we can list
13608
08:00:14,558 --> 08:00:17,840
all of the available file types
13609
08:00:17,840 --> 08:00:21,120
so file types
13610
08:00:21,120 --> 08:00:23,040
and we're going to place these within a
13611
08:00:23,040 --> 08:00:25,040
set of straight brackets
13612
08:00:25,040 --> 08:00:26,638
and i'll just separate these line by
13613
08:00:26,638 --> 08:00:28,958
line so let's begin with a plain
13614
08:00:28,958 --> 08:00:32,798
text file so this will take two strings
13615
08:00:32,798 --> 08:00:36,160
the first is the message that will
13616
08:00:36,160 --> 08:00:37,120
appear within
13617
08:00:37,120 --> 08:00:39,840
that save as type drop down menu so
13618
08:00:39,840 --> 08:00:41,600
let's place
13619
08:00:41,600 --> 08:00:44,958
text file and the second string
13620
08:00:44,958 --> 08:00:48,398
the file extension and that is
13621
08:00:48,398 --> 08:00:51,440
dot txt
13622
08:00:51,440 --> 08:00:54,558
then let's add another let's say html
13623
08:00:54,558 --> 08:00:57,680
so i'm going to copy this and paste it
13624
08:00:57,680 --> 08:00:58,240
and
13625
08:00:58,240 --> 08:01:02,000
let's say html next
13626
08:01:02,558 --> 08:01:06,080
dot html and then lastly you can do
13627
08:01:06,080 --> 08:01:09,600
just all files so
13628
08:01:09,680 --> 08:01:13,360
all files and that is dot
13629
08:01:13,360 --> 08:01:17,760
asterisk and let's try this again
13630
08:01:17,920 --> 08:01:21,760
so hello my
13631
08:01:21,760 --> 08:01:25,360
name is bro i
13632
08:01:25,360 --> 08:01:28,638
like pizza and
13633
08:01:28,638 --> 08:01:32,000
ice cream and this time i think i'll
13634
08:01:32,000 --> 08:01:32,638
save this
13635
08:01:32,638 --> 08:01:35,920
as an html file
13636
08:01:35,920 --> 08:01:39,200
and this is we'll call this
13637
08:01:39,200 --> 08:01:43,280
pizza and ice cream i guess
13638
08:01:43,280 --> 08:01:46,080
and click save now let's go back to my
13639
08:01:46,080 --> 08:01:47,120
desktop
13640
08:01:47,120 --> 08:01:49,840
here's that file pizza and ice cream and
13641
08:01:49,840 --> 08:01:51,760
it is an
13642
08:01:51,760 --> 08:01:56,000
file let's just verify that yep.html for
13643
08:01:56,000 --> 08:01:57,600
the type of file
13644
08:01:57,600 --> 08:02:01,760
so we can open this with notepad
13645
08:02:01,760 --> 08:02:04,398
and it has all of the text i could even
13646
08:02:04,398 --> 08:02:06,000
open this with sublime text
13647
08:02:06,000 --> 08:02:11,440
as well kind of like that
13648
08:02:11,760 --> 08:02:14,958
all right let's change the initial
13649
08:02:14,958 --> 08:02:15,840
directory
13650
08:02:15,840 --> 08:02:18,958
for saving a file let's change it to
13651
08:02:18,958 --> 08:02:22,240
my project folder for uh this python
13652
08:02:22,240 --> 08:02:23,760
file that we have
13653
08:02:23,760 --> 08:02:25,840
so that is and i'm just going to
13654
08:02:25,840 --> 08:02:28,160
organize this a little bit
13655
08:02:28,160 --> 08:02:31,680
initial d i r
13656
08:02:31,680 --> 08:02:34,478
equals and then we can list a file path
13657
08:02:34,478 --> 08:02:37,280
so i'm just going to copy this
13658
08:02:37,280 --> 08:02:42,718
so copy path paste it
13659
08:02:42,718 --> 08:02:45,680
then i want to make sure that these are
13660
08:02:45,680 --> 08:02:49,280
double backslashes
13661
08:02:49,280 --> 08:02:52,240
and i need to add a comma at the end
13662
08:02:52,240 --> 08:02:54,320
okay let's try this again
13663
08:02:54,320 --> 08:02:57,760
so testing
13664
08:02:57,760 --> 08:03:01,920
one two three and this should go to my
13665
08:03:01,920 --> 08:03:02,878
project folder
13666
08:03:02,878 --> 08:03:07,360
which it does testing123
13667
08:03:07,360 --> 08:03:10,320
and let's save this as a plain text file
13668
08:03:10,320 --> 08:03:12,638
click save
13669
08:03:12,638 --> 08:03:15,478
and here's that plain text file
13670
08:03:15,478 --> 08:03:16,958
testing123
13671
08:03:16,958 --> 08:03:18,878
another option available to you is that
13672
08:03:18,878 --> 08:03:20,878
you don't necessarily need to use
13673
08:03:20,878 --> 08:03:23,680
a text area to get some text to write to
13674
08:03:23,680 --> 08:03:24,638
a file
13675
08:03:24,638 --> 08:03:26,798
you could use the console window to
13676
08:03:26,798 --> 08:03:28,320
accept some text
13677
08:03:28,320 --> 08:03:30,798
so to demonstrate this i'm going to turn
13678
08:03:30,798 --> 08:03:32,638
this line into a comment
13679
08:03:32,638 --> 08:03:34,320
and we're instead going to use the
13680
08:03:34,320 --> 08:03:36,160
console window this time
13681
08:03:36,160 --> 08:03:40,638
so we'll say file text equals
13682
08:03:40,638 --> 08:03:43,920
input and then let's just have a prompt
13683
08:03:43,920 --> 08:03:44,478
here
13684
08:03:44,478 --> 08:03:47,520
enter some text i
13685
08:03:47,520 --> 08:03:50,558
guess and then let's run this again so
13686
08:03:50,558 --> 08:03:51,680
we still need to
13687
08:03:51,680 --> 08:03:54,958
click this save button and select a file
13688
08:03:54,958 --> 08:03:57,558
location i'll just save over our file
13689
08:03:57,558 --> 08:04:00,718
testing123 i'll click save
13690
08:04:00,718 --> 08:04:02,798
yeah i'll replace it alright so we need
13691
08:04:02,798 --> 08:04:04,718
to enter some text
13692
08:04:04,718 --> 08:04:08,320
i guess so i will write a message
13693
08:04:08,320 --> 08:04:12,000
such as the cake
13694
08:04:12,000 --> 08:04:16,000
is a lie and hit enter
13695
08:04:16,000 --> 08:04:19,760
so then our file testing123 now says
13696
08:04:19,760 --> 08:04:21,520
the cake is a lie so you don't
13697
08:04:21,520 --> 08:04:23,280
necessarily need to use
13698
08:04:23,280 --> 08:04:26,558
the get function for a text area you can
13699
08:04:26,558 --> 08:04:29,040
use the console window as well
13700
08:04:29,040 --> 08:04:30,958
here's one last thing to consider before
13701
08:04:30,958 --> 08:04:33,760
you go so what if we begin the process
13702
08:04:33,760 --> 08:04:34,160
of
13703
08:04:34,160 --> 08:04:37,120
finding a file location to save a file
13704
08:04:37,120 --> 08:04:37,680
to
13705
08:04:37,680 --> 08:04:40,320
and then we exit out of this window well
13706
08:04:40,320 --> 08:04:42,718
we're going to encounter an exception
13707
08:04:42,718 --> 08:04:45,280
so one way that we can prevent this is
13708
08:04:45,280 --> 08:04:46,000
to add
13709
08:04:46,000 --> 08:04:49,600
this line if file is
13710
08:04:49,600 --> 08:04:52,638
none return
13711
08:04:52,638 --> 08:04:56,240
so then if we were to try this again
13712
08:04:56,240 --> 08:04:58,718
begin saving a file and then exit out of
13713
08:04:58,718 --> 08:04:59,760
this
13714
08:04:59,760 --> 08:05:02,320
there is no exception that we encounter
13715
08:05:02,320 --> 08:05:04,320
so that's just one little extra i
13716
08:05:04,320 --> 08:05:05,600
thought i would throw in
13717
08:05:05,600 --> 08:05:07,840
but yeah that's the basics of saving
13718
08:05:07,840 --> 08:05:09,840
files in python if you would like a copy
13719
08:05:09,840 --> 08:05:10,878
of all this code
13720
08:05:10,878 --> 08:05:12,398
i'll post all of this in the comments
13721
08:05:12,398 --> 08:05:15,120
down below but yeah that's how to save a
13722
08:05:15,120 --> 08:05:17,280
file using python
13723
08:05:17,280 --> 08:05:18,798
hey what's going on everybody it's your
13724
08:05:18,798 --> 08:05:20,478
bro here hope you're doing well and in
13725
08:05:20,478 --> 08:05:21,760
this video i'm going to teach you guys
13726
08:05:21,760 --> 08:05:23,520
how we can create a menu bar
13727
08:05:23,520 --> 08:05:26,000
in python so sit back relax and enjoy
13728
08:05:26,000 --> 08:05:28,320
the show
13729
08:05:28,638 --> 08:05:31,040
all right people let's create a menu bar
13730
08:05:31,040 --> 08:05:32,638
not too different from the menu bar that
13731
08:05:32,638 --> 08:05:34,478
you see at the top of many programs
13732
08:05:34,478 --> 08:05:36,000
there's usually a tab
13733
08:05:36,000 --> 08:05:39,840
for file edit view help stuff like that
13734
08:05:39,840 --> 08:05:41,760
except we're going to create one that
13735
08:05:41,760 --> 08:05:43,520
doesn't have as many options
13736
08:05:43,520 --> 08:05:45,760
or features it's going to be the generic
13737
08:05:45,760 --> 08:05:48,240
brand menu bar like the walmart variety
13738
08:05:48,240 --> 08:05:50,478
so let's begin by creating a window so
13739
08:05:50,478 --> 08:05:53,000
window equals tk followed by
13740
08:05:53,000 --> 08:05:54,958
window.mainloop then we're going to
13741
08:05:54,958 --> 08:05:56,320
create a menu bar
13742
08:05:56,320 --> 08:05:59,440
and add menus to the menu bar and each
13743
08:05:59,440 --> 08:06:01,360
menu is going to function like a
13744
08:06:01,360 --> 08:06:03,520
dropdown menu where you hover over one
13745
08:06:03,520 --> 08:06:04,478
of these tabs
13746
08:06:04,478 --> 08:06:07,360
and there's a drop down so we need a
13747
08:06:07,360 --> 08:06:08,080
menu bar
13748
08:06:08,080 --> 08:06:10,878
let's just call this menu bar menu bar
13749
08:06:10,878 --> 08:06:11,520
equals
13750
08:06:11,520 --> 08:06:15,120
menu and we're adding this to our window
13751
08:06:15,120 --> 08:06:17,600
so then we're going to take window dot
13752
08:06:17,600 --> 08:06:18,718
config
13753
08:06:18,718 --> 08:06:21,920
and we can set the menu of this window
13754
08:06:21,920 --> 08:06:24,240
to equal the menu bar that we just
13755
08:06:24,240 --> 08:06:25,040
created
13756
08:06:25,040 --> 08:06:26,878
with each of these tabs for your menu
13757
08:06:26,878 --> 08:06:29,200
bar you need to create a separate menu
13758
08:06:29,200 --> 08:06:31,920
and add each menu to your menu bar and
13759
08:06:31,920 --> 08:06:33,920
the menu bar is added to the window
13760
08:06:33,920 --> 08:06:36,160
there's different levels to the stuff so
13761
08:06:36,160 --> 08:06:37,040
let's create
13762
08:06:37,040 --> 08:06:40,080
a file menu because well this one has a
13763
08:06:40,080 --> 08:06:40,878
file menu
13764
08:06:40,878 --> 08:06:44,080
why not our program so file
13765
08:06:44,080 --> 08:06:47,520
menu equals menu
13766
08:06:47,520 --> 08:06:49,520
kind of like what we did with our menu
13767
08:06:49,520 --> 08:06:52,160
bar except instead of adding this to our
13768
08:06:52,160 --> 08:06:53,680
window we're going to add this
13769
08:06:53,680 --> 08:06:57,840
to our menu bar so menu
13770
08:06:57,840 --> 08:07:02,478
bar like that there's one other step two
13771
08:07:02,478 --> 08:07:05,600
we need to use this function menu
13772
08:07:05,600 --> 08:07:08,638
bar dot add
13773
08:07:08,638 --> 08:07:12,320
underscore cascade this will have a drop
13774
08:07:12,320 --> 08:07:12,718
down
13775
08:07:12,718 --> 08:07:16,160
menu sort of effect so let's create a
13776
08:07:16,160 --> 08:07:16,638
label
13777
08:07:16,638 --> 08:07:19,520
just called file to mimic the file tab
13778
08:07:19,520 --> 08:07:21,680
that you see at the top of many programs
13779
08:07:21,680 --> 08:07:25,360
so label file and we're going to set the
13780
08:07:25,360 --> 08:07:26,558
menu
13781
08:07:26,558 --> 08:07:30,240
to equal the file menu that we created
13782
08:07:30,240 --> 08:07:33,680
so file menu and let's take a look just
13783
08:07:33,680 --> 08:07:35,520
to see what we have
13784
08:07:35,520 --> 08:07:37,600
so here's our menu bar and here's our
13785
08:07:37,600 --> 08:07:39,040
menu there's a drop down
13786
08:07:39,040 --> 08:07:41,120
effect for this but we need to add
13787
08:07:41,120 --> 08:07:42,398
actual options
13788
08:07:42,398 --> 08:07:46,160
to this file menu so let's do that
13789
08:07:46,160 --> 08:07:49,040
so let's begin by creating let's see we
13790
08:07:49,040 --> 08:07:50,240
got
13791
08:07:50,240 --> 08:07:54,718
maybe open save and exit
13792
08:07:54,718 --> 08:07:58,160
that should be good so file
13793
08:07:58,160 --> 08:08:01,200
menu dot add
13794
08:08:01,200 --> 08:08:03,920
underscore command so this is a
13795
08:08:03,920 --> 08:08:05,680
clickable option
13796
08:08:05,680 --> 08:08:08,878
and let's create a label called open
13797
08:08:08,878 --> 08:08:12,478
and we can open a file and maybe two
13798
08:08:12,478 --> 08:08:14,160
more so we got open
13799
08:08:14,160 --> 08:08:17,600
save and exit open save
13800
08:08:17,600 --> 08:08:20,080
exit
13801
08:08:21,760 --> 08:08:25,440
and let's take a look at this
13802
08:08:25,440 --> 08:08:27,840
so here's the drop down menu we can open
13803
08:08:27,840 --> 08:08:29,040
save exit
13804
08:08:29,040 --> 08:08:31,120
now you might notice that there is this
13805
08:08:31,120 --> 08:08:32,320
annoying line
13806
08:08:32,320 --> 08:08:34,080
this is called a tear off you can
13807
08:08:34,080 --> 08:08:36,160
actually get rid of this if you want
13808
08:08:36,160 --> 08:08:39,280
so when we create this file menu we're
13809
08:08:39,280 --> 08:08:40,160
going to
13810
08:08:40,160 --> 08:08:43,760
set tear off to equal
13811
08:08:43,760 --> 08:08:47,760
zero and that should get rid of that
13812
08:08:47,920 --> 08:08:51,360
and it's gone so sometimes you might see
13813
08:08:51,360 --> 08:08:54,878
a line to separate sections well
13814
08:08:54,878 --> 08:08:57,280
you can do that with a separator so
13815
08:08:57,280 --> 08:08:58,240
let's separate
13816
08:08:58,240 --> 08:09:01,280
the exit command that we have
13817
08:09:01,280 --> 08:09:03,360
from open and save and to add that
13818
08:09:03,360 --> 08:09:05,120
separator we just type in the name of
13819
08:09:05,120 --> 08:09:06,080
our menu
13820
08:09:06,080 --> 08:09:10,558
file menu dot add
13821
08:09:10,558 --> 08:09:14,878
separator so this will separate your
13822
08:09:14,878 --> 08:09:16,478
different commands from each other
13823
08:09:16,478 --> 08:09:18,320
within a menu you can see that there is
13824
08:09:18,320 --> 08:09:19,280
a line between
13825
08:09:19,280 --> 08:09:21,760
save and exit because we placed a
13826
08:09:21,760 --> 08:09:22,798
separator here
13827
08:09:22,798 --> 08:09:24,398
now if you want each of these file
13828
08:09:24,398 --> 08:09:26,320
commands to actually do something you
13829
08:09:26,320 --> 08:09:27,520
need to associate
13830
08:09:27,520 --> 08:09:30,320
a command with each of these commands
13831
08:09:30,320 --> 08:09:32,240
it's a command within a command
13832
08:09:32,240 --> 08:09:35,760
so command equals for our open file
13833
08:09:35,760 --> 08:09:36,718
command
13834
08:09:36,718 --> 08:09:40,798
we'll create a function called open file
13835
08:09:40,798 --> 08:09:42,958
and we'll need to declare that function
13836
08:09:42,958 --> 08:09:45,040
and then let's create a command called
13837
08:09:45,040 --> 08:09:48,558
save file and then another called
13838
08:09:48,558 --> 08:09:51,840
exit actually one shortcut if you want
13839
08:09:51,840 --> 08:09:53,520
to exit something you can just say
13840
08:09:53,520 --> 08:09:56,638
quit all right let's create an
13841
08:09:56,638 --> 08:09:59,680
open file and a save file function so
13842
08:09:59,680 --> 08:10:03,520
def open file
13843
08:10:03,520 --> 08:10:05,520
let's just print something just to keep
13844
08:10:05,520 --> 08:10:07,520
this simple
13845
08:10:07,520 --> 08:10:09,520
i already have another video on opening
13846
08:10:09,520 --> 08:10:11,280
files so
13847
08:10:11,280 --> 08:10:14,478
print file has
13848
08:10:14,478 --> 08:10:18,398
been opened and let's do the same thing
13849
08:10:18,398 --> 08:10:18,718
for
13850
08:10:18,718 --> 08:10:21,360
save file
13851
08:10:25,360 --> 08:10:28,558
file has been saved
13852
08:10:28,558 --> 08:10:30,478
and then our last command exit we'll
13853
08:10:30,478 --> 08:10:31,600
just quit
13854
08:10:31,600 --> 08:10:34,958
so let's try this so let's go to file
13855
08:10:34,958 --> 08:10:36,638
here's the drop down menu we can
13856
08:10:36,638 --> 08:10:39,440
open a file file has been opened we can
13857
08:10:39,440 --> 08:10:41,280
save a file
13858
08:10:41,280 --> 08:10:43,600
and then exit and exit will close out of
13859
08:10:43,600 --> 08:10:44,718
our window
13860
08:10:44,718 --> 08:10:46,798
let's create a second menu called edit
13861
08:10:46,798 --> 08:10:48,718
to mimic the one that we have at the top
13862
08:10:48,718 --> 08:10:49,840
of our ide
13863
08:10:49,840 --> 08:10:52,638
so let's create a edit menu so it's the
13864
08:10:52,638 --> 08:10:54,638
same process that we did with our file
13865
08:10:54,638 --> 08:10:55,440
menu
13866
08:10:55,440 --> 08:10:59,680
so let's call this edit menu equals menu
13867
08:10:59,680 --> 08:11:02,478
we're adding this to our menu bar and i
13868
08:11:02,478 --> 08:11:03,760
do not want a tear
13869
08:11:03,760 --> 08:11:07,200
off so tear off equals zero then for the
13870
08:11:07,200 --> 08:11:09,120
next line we need to add
13871
08:11:09,120 --> 08:11:12,478
cascade so i'm going to copy this
13872
08:11:12,478 --> 08:11:15,680
menu bar dot add cascade the label name
13873
08:11:15,680 --> 08:11:18,718
will be edit and the file i mean the
13874
08:11:18,718 --> 08:11:19,360
menu
13875
08:11:19,360 --> 08:11:22,478
will be edit menu and we're setting
13876
08:11:22,478 --> 08:11:26,240
this menu that we're adding to edit menu
13877
08:11:26,240 --> 08:11:28,958
and let's try this so we have file and
13878
08:11:28,958 --> 08:11:29,440
edit
13879
08:11:29,440 --> 08:11:31,760
but we need to add commands to this edit
13880
08:11:31,760 --> 08:11:32,558
menu
13881
08:11:32,558 --> 08:11:34,080
so it's the same process that we did
13882
08:11:34,080 --> 08:11:35,840
with our file menu i'm just going to
13883
08:11:35,840 --> 08:11:36,958
copy this
13884
08:11:36,958 --> 08:11:40,000
change file to edit and let's
13885
08:11:40,000 --> 08:11:44,558
make a command called cut copy and paste
13886
08:11:44,558 --> 08:11:48,160
so the label will be cut we'll create a
13887
08:11:48,160 --> 08:11:49,120
command called
13888
08:11:49,120 --> 08:11:54,000
cut we'll copy this
13889
08:11:54,000 --> 08:11:56,558
and we'll create a another command
13890
08:11:56,558 --> 08:11:58,638
called
13891
08:11:58,638 --> 08:12:08,398
copy and then paste
13892
08:12:08,398 --> 08:12:10,000
all right then we just need to create
13893
08:12:10,000 --> 08:12:11,760
some functions for this so cut
13894
08:12:11,760 --> 08:12:15,920
copy paste let's do that at the top
13895
08:12:18,080 --> 08:12:23,840
so cut
13896
08:12:24,000 --> 08:12:28,000
you cut some text
13897
08:12:29,840 --> 08:12:34,240
so cut copy and then paste
13898
08:12:35,520 --> 08:12:38,798
you copied some text
13899
08:12:38,798 --> 08:12:42,080
and paste you
13900
08:12:42,080 --> 08:12:45,120
pasted some text
13901
08:12:45,120 --> 08:12:47,920
all right let's try this so here's our
13902
08:12:47,920 --> 08:12:49,360
edit menu
13903
08:12:49,360 --> 08:12:51,840
here's the drop down we can cut we can
13904
08:12:51,840 --> 08:12:52,798
copy
13905
08:12:52,798 --> 08:12:55,440
and we can paste not actually but it's
13906
08:12:55,440 --> 08:12:57,760
going to call a function that can
13907
08:12:57,760 --> 08:12:59,520
do something like that if you want to
13908
08:12:59,520 --> 08:13:01,600
set that up maybe that's a video for a
13909
08:13:01,600 --> 08:13:02,320
different day
13910
08:13:02,320 --> 08:13:04,718
let's customize these menus well because
13911
08:13:04,718 --> 08:13:05,600
we can
13912
08:13:05,600 --> 08:13:08,240
so let's begin by changing the font so
13913
08:13:08,240 --> 08:13:09,520
we can just type in
13914
08:13:09,520 --> 08:13:12,638
within each of these menus font equals
13915
08:13:12,638 --> 08:13:14,160
pick a font that you want and then a
13916
08:13:14,160 --> 08:13:16,798
size so i'm going to add this font
13917
08:13:16,798 --> 08:13:19,360
for both of these menus that we have and
13918
08:13:19,360 --> 08:13:20,558
then when we
13919
08:13:20,558 --> 08:13:23,520
hover over and click on these menus you
13920
08:13:23,520 --> 08:13:25,360
can see that the font has changed
13921
08:13:25,360 --> 08:13:27,040
did you guys know that you can actually
13922
08:13:27,040 --> 08:13:28,878
add images next to each of these
13923
08:13:28,878 --> 08:13:29,600
commands
13924
08:13:29,600 --> 08:13:31,360
well you can and this is how we can do
13925
08:13:31,360 --> 08:13:33,520
that first you'll need some images
13926
08:13:33,520 --> 08:13:35,760
here's a few that i'm using so add these
13927
08:13:35,760 --> 08:13:38,000
to your project folder i plan on using a
13928
08:13:38,000 --> 08:13:39,680
floppy disk for save
13929
08:13:39,680 --> 08:13:42,320
and open folder for open and for some
13930
08:13:42,320 --> 08:13:44,558
reason a stop sign for exit because i
13931
08:13:44,558 --> 08:13:46,000
couldn't really think of anything at the
13932
08:13:46,000 --> 08:13:46,558
moment
13933
08:13:46,558 --> 08:13:48,320
i guess an arrow would have worked too i
13934
08:13:48,320 --> 08:13:50,958
guess so let's create some photo images
13935
08:13:50,958 --> 08:13:53,040
out of these images
13936
08:13:53,040 --> 08:13:54,798
so i already did this i'm just going to
13937
08:13:54,798 --> 08:13:57,280
copy and paste the text that i had
13938
08:13:57,280 --> 08:14:00,240
so create a unique name for each image i
13939
08:14:00,240 --> 08:14:00,558
have
13940
08:14:00,558 --> 08:14:03,520
open image save image and exit image
13941
08:14:03,520 --> 08:14:04,240
they're all
13942
08:14:04,240 --> 08:14:07,280
photo images and here is the file path
13943
08:14:07,280 --> 08:14:09,200
or the file name for each of these
13944
08:14:09,200 --> 08:14:12,080
pictures that i'm using so to add an
13945
08:14:12,080 --> 08:14:12,478
image
13946
08:14:12,478 --> 08:14:14,320
to each of these commands when you add
13947
08:14:14,320 --> 08:14:16,240
the command we can just say
13948
08:14:16,240 --> 08:14:18,558
image and then the photo image that you
13949
08:14:18,558 --> 08:14:19,600
want to use
13950
08:14:19,600 --> 08:14:22,398
so with my open command i'm going to use
13951
08:14:22,398 --> 08:14:22,878
this
13952
08:14:22,878 --> 08:14:25,680
open image photo image that i have and
13953
08:14:25,680 --> 08:14:26,478
this is the
13954
08:14:26,478 --> 08:14:28,718
open folder so i'm going to do the same
13955
08:14:28,718 --> 08:14:29,920
thing for save
13956
08:14:29,920 --> 08:14:33,600
and exit so we have save image
13957
08:14:33,600 --> 08:14:37,520
and exit image
13958
08:14:38,638 --> 08:14:40,958
but when we run and compile this let's
13959
08:14:40,958 --> 08:14:42,478
take a look
13960
08:14:42,478 --> 08:14:44,798
yeah so all the text is gone that's
13961
08:14:44,798 --> 08:14:46,080
going to be a problem
13962
08:14:46,080 --> 08:14:48,958
so we'll need to compound views so with
13963
08:14:48,958 --> 08:14:50,398
compound we can add
13964
08:14:50,398 --> 08:14:53,440
an image on a certain side of the text
13965
08:14:53,440 --> 08:14:56,878
and keep the text so compound
13966
08:14:56,878 --> 08:14:59,840
let's say we want the image on the left
13967
08:14:59,840 --> 08:15:00,398
so
13968
08:15:00,398 --> 08:15:02,638
compound left and i'm just going to add
13969
08:15:02,638 --> 08:15:06,080
this to each of these commands
13970
08:15:06,638 --> 08:15:09,360
and now we should have an image as well
13971
08:15:09,360 --> 08:15:11,360
as some text
13972
08:15:11,360 --> 08:15:13,200
well that's the basics of creating a
13973
08:15:13,200 --> 08:15:15,600
menu bar in python if you want a copy of
13974
08:15:15,600 --> 08:15:16,318
all this code
13975
08:15:16,318 --> 08:15:18,000
i'll post all of this in the comments
13976
08:15:18,000 --> 08:15:19,840
down below but yeah that's how you can
13977
08:15:19,840 --> 08:15:20,878
create a simple
13978
08:15:20,878 --> 08:15:24,318
menu bar in python what's going on
13979
08:15:24,318 --> 08:15:25,920
everybody it's bro here hope you're
13980
08:15:25,920 --> 08:15:27,360
doing well and in this video i'm going
13981
08:15:27,360 --> 08:15:28,638
to teach you guys how
13982
08:15:28,638 --> 08:15:31,680
frames work in python so sit back relax
13983
08:15:31,680 --> 08:15:34,478
and enjoy the show
13984
08:15:34,638 --> 08:15:36,558
what's going on people we're going to be
13985
08:15:36,558 --> 08:15:38,478
discussing frames today
13986
08:15:38,478 --> 08:15:40,798
a frame is really just a rectangular
13987
08:15:40,798 --> 08:15:42,638
container to group and hold widgets
13988
08:15:42,638 --> 08:15:43,600
together
13989
08:15:43,600 --> 08:15:45,600
so we're going to practice using frames
13990
08:15:45,600 --> 08:15:47,760
today but first we'll need a window
13991
08:15:47,760 --> 08:15:49,680
we're going to create a frame and add a
13992
08:15:49,680 --> 08:15:51,440
frame to the window
13993
08:15:51,440 --> 08:15:54,360
so window equals tk followed by
13994
08:15:54,360 --> 08:15:56,080
window.main loop
13995
08:15:56,080 --> 08:15:58,240
well we're going to need some widgets to
13996
08:15:58,240 --> 08:15:59,680
add to our frame
13997
08:15:59,680 --> 08:16:01,840
so let's create a bunch of buttons
13998
08:16:01,840 --> 08:16:03,440
because why not
13999
08:16:03,440 --> 08:16:05,360
so i'm going to show you guys a shortcut
14000
08:16:05,360 --> 08:16:06,878
too so let's say
14001
08:16:06,878 --> 08:16:10,638
button equals button
14002
08:16:10,638 --> 08:16:12,958
and then for now we're going to add this
14003
08:16:12,958 --> 08:16:14,718
to our window but we're going to replace
14004
08:16:14,718 --> 08:16:16,240
this eventually with
14005
08:16:16,240 --> 08:16:18,718
frame when we create this and i'm just
14006
08:16:18,718 --> 08:16:20,000
going to set the text
14007
08:16:20,000 --> 08:16:23,200
to equal let's say w
14008
08:16:23,200 --> 08:16:24,558
we're going to have four different
14009
08:16:24,558 --> 08:16:26,398
buttons wasd
14010
08:16:26,398 --> 08:16:28,000
like the buttons on the keyboard if
14011
08:16:28,000 --> 08:16:29,280
you're playing a game and you want to
14012
08:16:29,280 --> 08:16:30,398
move forward
14013
08:16:30,398 --> 08:16:33,360
left back or right so we're adding this
14014
08:16:33,360 --> 08:16:34,240
to the window
14015
08:16:34,240 --> 08:16:38,000
for now let's set the text to w
14016
08:16:38,000 --> 08:16:40,000
and what else can we do uh let's change
14017
08:16:40,000 --> 08:16:41,520
the font so font
14018
08:16:41,520 --> 08:16:45,360
equals i'll pick the font consoles
14019
08:16:45,360 --> 08:16:46,160
because
14020
08:16:46,160 --> 08:16:50,000
that tends to be even uh with buttons
14021
08:16:50,000 --> 08:16:52,958
the size of the button adjusts to uh the
14022
08:16:52,958 --> 08:16:54,318
size of the
14023
08:16:54,318 --> 08:16:56,160
uh letter that you're using or the size
14024
08:16:56,160 --> 08:16:57,440
of the text
14025
08:16:57,440 --> 08:17:00,478
uh so let's set this to 25
14026
08:17:00,478 --> 08:17:04,318
and then a width so maybe
14027
08:17:04,318 --> 08:17:07,520
width equals three all right and then
14028
08:17:07,520 --> 08:17:10,958
button.pack and i just want to take a
14029
08:17:10,958 --> 08:17:13,520
look at this
14030
08:17:14,240 --> 08:17:16,958
so here's our button w so i'd like to
14031
08:17:16,958 --> 08:17:17,760
show you guys a
14032
08:17:17,760 --> 08:17:20,798
shortcut if you don't plan on using this
14033
08:17:20,798 --> 08:17:22,160
button by name
14034
08:17:22,160 --> 08:17:23,600
you don't really need to give it a name
14035
08:17:23,600 --> 08:17:25,360
you can just say button
14036
08:17:25,360 --> 08:17:28,958
and then have all your uh options here
14037
08:17:28,958 --> 08:17:31,120
and then instead of having button.pack
14038
08:17:31,120 --> 08:17:32,000
just add
14039
08:17:32,000 --> 08:17:34,160
dot pack to the end so this will do the
14040
08:17:34,160 --> 08:17:35,520
same thing
14041
08:17:35,520 --> 08:17:38,240
however you can't adjust this button by
14042
08:17:38,240 --> 08:17:39,520
its name though because it doesn't have
14043
08:17:39,520 --> 08:17:40,638
a name anymore
14044
08:17:40,638 --> 08:17:42,958
so let's create a few other buttons one
14045
08:17:42,958 --> 08:17:44,160
for a s
14046
08:17:44,160 --> 08:17:47,520
and d so we have w
14047
08:17:47,520 --> 08:17:51,840
a s d
14048
08:17:52,878 --> 08:17:55,280
and by default they're all top and
14049
08:17:55,280 --> 08:17:56,398
center
14050
08:17:56,398 --> 08:17:59,360
so with pack we can actually put these
14051
08:17:59,360 --> 08:18:00,638
on a certain side
14052
08:18:00,638 --> 08:18:04,398
so let's say we want uh w on the top
14053
08:18:04,398 --> 08:18:07,440
and then the other three will be all
14054
08:18:07,440 --> 08:18:07,920
left
14055
08:18:07,920 --> 08:18:11,120
so side equals left and then after i
14056
08:18:11,120 --> 08:18:12,000
show this to you
14057
08:18:12,000 --> 08:18:15,840
you'll see the benefits of using a frame
14058
08:18:15,840 --> 08:18:18,000
all right so right now they're all at
14059
08:18:18,000 --> 08:18:19,600
least it appears that they're all within
14060
08:18:19,600 --> 08:18:20,478
a container
14061
08:18:20,478 --> 08:18:23,040
so what if i were to expand this well w
14062
08:18:23,040 --> 08:18:24,478
is just drifting off
14063
08:18:24,478 --> 08:18:27,520
it's sailing away by w so
14064
08:18:27,520 --> 08:18:29,280
what if we could put these within a
14065
08:18:29,280 --> 08:18:30,638
frame or container
14066
08:18:30,638 --> 08:18:33,440
to actually contain all of these widgets
14067
08:18:33,440 --> 08:18:34,398
well we can
14068
08:18:34,398 --> 08:18:38,000
so let's create a frame frame
14069
08:18:38,000 --> 08:18:41,120
equals frame
14070
08:18:41,120 --> 08:18:43,040
and instead of adding these buttons to
14071
08:18:43,040 --> 08:18:44,878
the window we're going to add
14072
08:18:44,878 --> 08:18:48,558
these buttons to our frame
14073
08:18:51,840 --> 08:18:54,318
and now we need to add the frame to the
14074
08:18:54,318 --> 08:18:56,718
window
14075
08:18:56,878 --> 08:18:59,040
and this is what this looks like however
14076
08:18:59,040 --> 08:19:00,240
we need to
14077
08:19:00,240 --> 08:19:03,200
actually add this by using pack or place
14078
08:19:03,200 --> 08:19:04,160
so frame
14079
08:19:04,160 --> 08:19:08,000
dot pack so now what this looks
14080
08:19:08,000 --> 08:19:10,318
like is that all of these buttons all of
14081
08:19:10,318 --> 08:19:12,638
these widgets are now contained within a
14082
08:19:12,638 --> 08:19:13,520
frame
14083
08:19:13,520 --> 08:19:16,000
a container let's change the background
14084
08:19:16,000 --> 08:19:17,760
color of this frame just so that we can
14085
08:19:17,760 --> 08:19:18,240
see
14086
08:19:18,240 --> 08:19:20,958
the width and the height of this frame
14087
08:19:20,958 --> 08:19:21,600
so
14088
08:19:21,600 --> 08:19:24,000
when you create your frame there is an
14089
08:19:24,000 --> 08:19:27,200
option for background color it's just bg
14090
08:19:27,200 --> 08:19:29,600
and let's set this to a color we haven't
14091
08:19:29,600 --> 08:19:30,638
done pink yet
14092
08:19:30,638 --> 08:19:33,760
pink is a very masculine color so now
14093
08:19:33,760 --> 08:19:36,718
the background of our frame is pink and
14094
08:19:36,718 --> 08:19:37,520
that might be
14095
08:19:37,520 --> 08:19:40,558
a better visual cue to the balance of
14096
08:19:40,558 --> 08:19:41,360
the frame
14097
08:19:41,360 --> 08:19:43,920
let's also give this frame a border so
14098
08:19:43,920 --> 08:19:44,398
let's
14099
08:19:44,398 --> 08:19:47,680
set a width of 5 using bd
14100
08:19:47,680 --> 08:19:50,398
and then the type of border that we want
14101
08:19:50,398 --> 08:19:51,200
and we can
14102
08:19:51,200 --> 08:19:53,760
specify that with relief so relief
14103
08:19:53,760 --> 08:19:54,318
equals
14104
08:19:54,318 --> 08:19:57,440
let's say raised so this will have a 3d
14105
08:19:57,440 --> 08:19:58,398
pop effect
14106
08:19:58,398 --> 08:20:00,798
kind of like that let's try sunken
14107
08:20:00,798 --> 08:20:02,478
there's a few other ones as well but i
14108
08:20:02,478 --> 08:20:04,240
don't feel like it's necessary to
14109
08:20:04,240 --> 08:20:07,280
go over all the border types let's stick
14110
08:20:07,280 --> 08:20:09,520
with sunken for now that'll work
14111
08:20:09,520 --> 08:20:12,160
alright so with this frame we are
14112
08:20:12,160 --> 08:20:13,600
currently packing this
14113
08:20:13,600 --> 08:20:16,478
we can set this on a certain side within
14114
08:20:16,478 --> 08:20:17,360
our window
14115
08:20:17,360 --> 08:20:19,680
so currently it's at the top let's set
14116
08:20:19,680 --> 08:20:21,040
this to maybe the bottom so
14117
08:20:21,040 --> 08:20:24,878
side equals bottom
14118
08:20:25,040 --> 08:20:28,000
now this frame is sticking to the bottom
14119
08:20:28,000 --> 08:20:29,040
of the window
14120
08:20:29,040 --> 08:20:31,440
and it still contains all of the buttons
14121
08:20:31,440 --> 08:20:32,478
all of the widgets
14122
08:20:32,478 --> 08:20:34,478
that we added another function available
14123
08:20:34,478 --> 08:20:35,520
to you is
14124
08:20:35,520 --> 08:20:38,160
the place function we can place this
14125
08:20:38,160 --> 08:20:38,718
frame
14126
08:20:38,718 --> 08:20:41,520
at certain coordinates within our window
14127
08:20:41,520 --> 08:20:43,280
so let's set
14128
08:20:43,280 --> 08:20:46,478
x to equal zero and y to equal zero
14129
08:20:46,478 --> 08:20:48,958
so then when we place this frame within
14130
08:20:48,958 --> 08:20:51,360
our window and set some coordinates
14131
08:20:51,360 --> 08:20:54,558
this entire frame is going to stick at
14132
08:20:54,558 --> 08:20:56,878
these coordinates even if we were to
14133
08:20:56,878 --> 08:20:59,520
resize this window and we can change
14134
08:20:59,520 --> 08:21:00,080
these too
14135
08:21:00,080 --> 08:21:05,920
let's say x is now 100 and y is also 100
14136
08:21:05,920 --> 08:21:08,718
so the frame along with all of the
14137
08:21:08,718 --> 08:21:10,558
components all of the widgets that it
14138
08:21:10,558 --> 08:21:11,520
contains
14139
08:21:11,520 --> 08:21:13,200
are sticking within the container and
14140
08:21:13,200 --> 08:21:15,440
they move along with this
14141
08:21:15,440 --> 08:21:17,680
so that's the basics of frames it's
14142
08:21:17,680 --> 08:21:19,920
really just a rectangular container to
14143
08:21:19,920 --> 08:21:22,000
group and hold widgets together
14144
08:21:22,000 --> 08:21:24,000
so if you want a copy of all this code
14145
08:21:24,000 --> 08:21:25,680
i'll post all of this in the comments
14146
08:21:25,680 --> 08:21:26,558
down below
14147
08:21:26,558 --> 08:21:29,040
and well yeah that's how frames work in
14148
08:21:29,040 --> 08:21:30,478
python
14149
08:21:30,478 --> 08:21:32,080
hey what's going on everybody it's your
14150
08:21:32,080 --> 08:21:33,680
bro here hope you're doing well and in
14151
08:21:33,680 --> 08:21:35,040
this video i'm going to teach you guys a
14152
08:21:35,040 --> 08:21:36,318
few different ways in which we can
14153
08:21:36,318 --> 08:21:38,798
create new windows using python so sit
14154
08:21:38,798 --> 08:21:42,638
back relax and enjoy the show
14155
08:21:42,638 --> 08:21:44,240
let's get started everybody so we're
14156
08:21:44,240 --> 08:21:45,920
going to create a window and we'll add a
14157
08:21:45,920 --> 08:21:46,958
button to this window
14158
08:21:46,958 --> 08:21:48,718
when we click on this button it'll
14159
08:21:48,718 --> 08:21:50,718
create a new separate window for us
14160
08:21:50,718 --> 08:21:52,798
so window equals tk followed by
14161
08:21:52,798 --> 08:21:54,160
window.main loop
14162
08:21:54,160 --> 08:21:55,760
we'll need to add a button so let's do
14163
08:21:55,760 --> 08:21:57,760
that button we're adding this to the
14164
08:21:57,760 --> 08:21:58,478
window
14165
08:21:58,478 --> 08:22:00,638
let's add some text to the button let's
14166
08:22:00,638 --> 08:22:02,798
say create
14167
08:22:02,798 --> 08:22:06,240
new window and we'll set a command
14168
08:22:06,240 --> 08:22:08,638
command equals and we'll need to define
14169
08:22:08,638 --> 08:22:09,840
a function
14170
08:22:09,840 --> 08:22:12,000
let's create a function called create
14171
08:22:12,000 --> 08:22:14,558
underscore window
14172
08:22:14,558 --> 08:22:16,958
and let's pack this as well so we will
14173
08:22:16,958 --> 08:22:17,840
define
14174
08:22:17,840 --> 08:22:19,760
this function create window and let's do
14175
08:22:19,760 --> 08:22:21,200
so at the top
14176
08:22:21,200 --> 08:22:23,840
def create window and what do we want
14177
08:22:23,840 --> 08:22:24,558
this to do
14178
08:22:24,558 --> 08:22:26,398
well we're going to create a new window
14179
08:22:26,398 --> 08:22:28,240
and let's call this next window
14180
08:22:28,240 --> 08:22:31,440
new underscore window equals and there's
14181
08:22:31,440 --> 08:22:33,760
two popular ways of creating a window
14182
08:22:33,760 --> 08:22:36,718
one is we can use tk and the other is
14183
08:22:36,718 --> 08:22:37,520
that we can use
14184
08:22:37,520 --> 08:22:40,080
something called top level and let me
14185
08:22:40,080 --> 08:22:41,680
explain the difference between the two
14186
08:22:41,680 --> 08:22:43,760
here's a definition for a top level
14187
08:22:43,760 --> 08:22:45,520
window this is a
14188
08:22:45,520 --> 08:22:48,718
new window on top of other windows
14189
08:22:48,718 --> 08:22:51,200
and it's usually linked to a bottom
14190
08:22:51,200 --> 08:22:52,318
window
14191
08:22:52,318 --> 08:22:54,638
our main window is serving as the bottom
14192
08:22:54,638 --> 08:22:55,360
window
14193
08:22:55,360 --> 08:22:57,680
and our new window is serving as the top
14194
08:22:57,680 --> 08:23:00,080
window the top level window
14195
08:23:00,080 --> 08:23:02,318
so let's try this and just experiment
14196
08:23:02,318 --> 08:23:03,920
here is our main window
14197
08:23:03,920 --> 08:23:05,840
the bottom window and if we were to
14198
08:23:05,840 --> 08:23:07,360
click on this button it's going to
14199
08:23:07,360 --> 08:23:08,080
create a
14200
08:23:08,080 --> 08:23:11,920
new window for us a new top level window
14201
08:23:11,920 --> 08:23:13,920
if we were to close out of the bottom
14202
08:23:13,920 --> 08:23:14,958
level window
14203
08:23:14,958 --> 08:23:17,520
any top level windows associated with
14204
08:23:17,520 --> 08:23:18,958
this would also close
14205
08:23:18,958 --> 08:23:20,878
kind of like what you just saw now
14206
08:23:20,878 --> 08:23:22,638
however it's not the same case
14207
08:23:22,638 --> 08:23:25,040
the other way around if we have a bottom
14208
08:23:25,040 --> 08:23:26,878
window and a top level window
14209
08:23:26,878 --> 08:23:29,360
and we close out of the top level window
14210
08:23:29,360 --> 08:23:31,120
our bottom level window
14211
08:23:31,120 --> 08:23:33,520
is just fine it didn't close out of that
14212
08:23:33,520 --> 08:23:34,958
so think of these like
14213
08:23:34,958 --> 08:23:37,440
jenga pieces if you were to pull one of
14214
08:23:37,440 --> 08:23:38,160
the pieces
14215
08:23:38,160 --> 08:23:40,160
one of the windows out from underneath
14216
08:23:40,160 --> 08:23:41,360
the bottom any
14217
08:23:41,360 --> 08:23:44,240
pieces or windows on the top of it will
14218
08:23:44,240 --> 08:23:45,120
close out as well
14219
08:23:45,120 --> 08:23:46,718
or collapse kind of like in the game
14220
08:23:46,718 --> 08:23:48,160
jenga so
14221
08:23:48,160 --> 08:23:50,080
that's one way to think of top level
14222
08:23:50,080 --> 08:23:52,478
windows they're stacked on top of any
14223
08:23:52,478 --> 08:23:53,680
bottom level windows
14224
08:23:53,680 --> 08:23:55,440
if you remove a bottom piece it removes
14225
08:23:55,440 --> 08:23:56,718
any from the top
14226
08:23:56,718 --> 08:23:59,920
on the flip side we have tk this is a
14227
08:23:59,920 --> 08:24:01,840
new independent window and we've
14228
08:24:01,840 --> 08:24:03,200
actually been doing this since
14229
08:24:03,200 --> 08:24:06,558
day one of our gui programming tk is a
14230
08:24:06,558 --> 08:24:09,040
new independent window and our main
14231
08:24:09,040 --> 08:24:10,798
window and the new window that we create
14232
08:24:10,798 --> 08:24:12,478
are not linked whatsoever they're
14233
08:24:12,478 --> 08:24:13,760
completely independent
14234
08:24:13,760 --> 08:24:16,080
so if we were to create a new window
14235
08:24:16,080 --> 08:24:17,760
this is our new window and close out of
14236
08:24:17,760 --> 08:24:18,798
our old window
14237
08:24:18,798 --> 08:24:21,280
well this new window still persists so
14238
08:24:21,280 --> 08:24:22,558
this could be useful for
14239
08:24:22,558 --> 08:24:25,120
let's say a login form or a login screen
14240
08:24:25,120 --> 08:24:26,638
after you log in you'll be brought to
14241
08:24:26,638 --> 08:24:28,398
your main program which could be within
14242
08:24:28,398 --> 08:24:29,600
the new window
14243
08:24:29,600 --> 08:24:31,360
one other thing that you can do too you
14244
08:24:31,360 --> 08:24:33,600
can also destroy the old window after
14245
08:24:33,600 --> 08:24:35,120
creating a new window
14246
08:24:35,120 --> 08:24:37,440
so we need to type in the name of the
14247
08:24:37,440 --> 08:24:39,280
window that we want to destroy
14248
08:24:39,280 --> 08:24:41,840
actually let's call the main window just
14249
08:24:41,840 --> 08:24:43,920
old underscore window just to clarify
14250
08:24:43,920 --> 08:24:44,878
things
14251
08:24:44,878 --> 08:24:47,840
so let's rename that here here and here
14252
08:24:47,840 --> 08:24:48,638
as well
14253
08:24:48,638 --> 08:24:51,040
so we want to destroy our old window as
14254
08:24:51,040 --> 08:24:52,318
if we're closing out of it
14255
08:24:52,318 --> 08:24:55,280
so we type in the name of the window old
14256
08:24:55,280 --> 08:24:56,478
underscore window
14257
08:24:56,478 --> 08:24:58,878
and we use the destroy function and that
14258
08:24:58,878 --> 08:25:00,000
sounds pretty sweet
14259
08:25:00,000 --> 08:25:02,398
so we're going to destroy our old window
14260
08:25:02,398 --> 08:25:03,200
after creating
14261
08:25:03,200 --> 08:25:05,280
our new window so let me add a comment
14262
08:25:05,280 --> 08:25:06,398
that this will
14263
08:25:06,398 --> 08:25:10,638
close out of old window
14264
08:25:10,638 --> 08:25:13,680
and let's test it so we have our old
14265
08:25:13,680 --> 08:25:14,318
window
14266
08:25:14,318 --> 08:25:16,080
our main window we'll create a new
14267
08:25:16,080 --> 08:25:18,000
window it creates our new window and
14268
08:25:18,000 --> 08:25:20,398
then closes out of the old window
14269
08:25:20,398 --> 08:25:22,318
so that's one of a few ways in which you
14270
08:25:22,318 --> 08:25:24,958
can create a new window for your program
14271
08:25:24,958 --> 08:25:26,638
so if you want to copy of all this code
14272
08:25:26,638 --> 08:25:28,080
i'll post all of this in the comments
14273
08:25:28,080 --> 08:25:28,878
down below
14274
08:25:28,878 --> 08:25:30,638
but yeah that's how to create a new
14275
08:25:30,638 --> 08:25:32,718
window using python
14276
08:25:32,718 --> 08:25:34,478
hey what's going on everybody too bro
14277
08:25:34,478 --> 08:25:36,000
here hope you're doing well and in this
14278
08:25:36,000 --> 08:25:37,440
video i'm going to teach you guys how we
14279
08:25:37,440 --> 08:25:39,440
can create separate tabs for our gui
14280
08:25:39,440 --> 08:25:41,040
applications in python so
14281
08:25:41,040 --> 08:25:44,718
sit back relax and enjoy the show
14282
08:25:45,040 --> 08:25:47,520
okay let's begin so in order to create
14283
08:25:47,520 --> 08:25:50,240
tabs for our python gui application
14284
08:25:50,240 --> 08:25:52,638
we're going to need access to a widget
14285
08:25:52,638 --> 08:25:53,760
called notebook
14286
08:25:53,760 --> 08:25:56,160
and that's found in a different module
14287
08:25:56,160 --> 08:25:57,600
so this time we're going to need
14288
08:25:57,600 --> 08:26:00,478
two imports so at the top from tk enter
14289
08:26:00,478 --> 08:26:01,120
import
14290
08:26:01,120 --> 08:26:03,840
just everything but the notebook widget
14291
08:26:03,840 --> 08:26:05,920
is found within a separate module
14292
08:26:05,920 --> 08:26:09,120
so we'll need a second import from tk
14293
08:26:09,120 --> 08:26:10,080
enter
14294
08:26:10,080 --> 08:26:14,318
import ttk so this ttk import
14295
08:26:14,318 --> 08:26:16,478
gives us access to several different
14296
08:26:16,478 --> 08:26:18,160
widgets that are normally not available
14297
08:26:18,160 --> 08:26:18,958
to us
14298
08:26:18,958 --> 08:26:20,798
so once you have these two imports
14299
08:26:20,798 --> 08:26:22,478
you're ready to begin so we'll need a
14300
08:26:22,478 --> 08:26:23,200
window
14301
08:26:23,200 --> 08:26:26,000
window equals tk followed by window.main
14302
08:26:26,000 --> 08:26:26,638
loop
14303
08:26:26,638 --> 08:26:29,440
and we need to create a notebook widget
14304
08:26:29,440 --> 08:26:30,080
so
14305
08:26:30,080 --> 08:26:31,520
right after you create your window we're
14306
08:26:31,520 --> 08:26:33,120
going to create a notebook
14307
08:26:33,120 --> 08:26:37,200
we'll call this notebook equals ttk
14308
08:26:37,200 --> 08:26:40,718
dot notebook and we want to add
14309
08:26:40,718 --> 08:26:43,760
this notebook to our window so let me
14310
08:26:43,760 --> 08:26:45,680
add a comment here what this widget does
14311
08:26:45,680 --> 08:26:49,120
so this is a widget that manages
14312
08:26:49,120 --> 08:26:52,718
a collection of windows
14313
08:26:52,718 --> 08:26:55,600
and displays and that's really all there
14314
08:26:55,600 --> 08:26:56,558
is to it
14315
08:26:56,558 --> 08:26:58,638
so in order to create different tabs
14316
08:26:58,638 --> 08:27:00,318
we're going to create some frames
14317
08:27:00,318 --> 08:27:02,558
so for the first tab i'll call this tab
14318
08:27:02,558 --> 08:27:04,558
1 equals frame
14319
08:27:04,558 --> 08:27:08,478
and we want to add this to the notebook
14320
08:27:08,478 --> 08:27:10,000
and with our notebook we're adding this
14321
08:27:10,000 --> 08:27:12,718
notebook to the window
14322
08:27:12,718 --> 08:27:16,000
all right so this will be a new frame
14323
08:27:16,000 --> 08:27:19,120
for tab one and let's create a second
14324
08:27:19,120 --> 08:27:19,600
tab
14325
08:27:19,600 --> 08:27:22,398
and we'll call this tab two and you can
14326
08:27:22,398 --> 08:27:24,000
create more tabs if you want but that
14327
08:27:24,000 --> 08:27:25,280
might be overkill
14328
08:27:25,280 --> 08:27:28,478
so this is a new frame for tab two
14329
08:27:28,478 --> 08:27:30,240
after you finish these two lines of code
14330
08:27:30,240 --> 08:27:32,200
we're going to follow this up with
14331
08:27:32,200 --> 08:27:36,798
notebook.ad so notebook.ad
14332
08:27:36,798 --> 08:27:39,120
what widget are we adding we're adding
14333
08:27:39,120 --> 08:27:39,840
our tab
14334
08:27:39,840 --> 08:27:43,360
1 frame and we can set some text for
14335
08:27:43,360 --> 08:27:46,798
this tab so text equals and i'll type in
14336
08:27:46,798 --> 08:27:49,440
tab one and we'll repeat the process for
14337
08:27:49,440 --> 08:27:51,200
tab two
14338
08:27:51,200 --> 08:27:54,240
so notebook dot add tab two
14339
08:27:54,240 --> 08:27:57,280
and we'll change the text to tab two and
14340
08:27:57,280 --> 08:27:58,160
then we need to
14341
08:27:58,160 --> 08:28:02,958
pack this notebook notebook dot pack
14342
08:28:02,958 --> 08:28:04,478
and let's take a look to see what we
14343
08:28:04,478 --> 08:28:06,718
have all right we have
14344
08:28:06,718 --> 08:28:09,440
tab one and tab two but we'll probably
14345
08:28:09,440 --> 08:28:11,520
want to actually add something
14346
08:28:11,520 --> 08:28:13,120
to each of these tabs so that the
14347
08:28:13,120 --> 08:28:15,280
content is visible so let's create some
14348
08:28:15,280 --> 08:28:15,920
labels
14349
08:28:15,920 --> 08:28:18,240
so after we pack our notebook let's
14350
08:28:18,240 --> 08:28:19,760
create some labels
14351
08:28:19,760 --> 08:28:21,200
i don't really feel like giving these
14352
08:28:21,200 --> 08:28:22,798
names because we don't really plan on
14353
08:28:22,798 --> 08:28:24,240
using them for anything else
14354
08:28:24,240 --> 08:28:27,360
so we'll just say label tab
14355
08:28:27,360 --> 08:28:30,718
one that's going to be the parent widget
14356
08:28:30,718 --> 08:28:33,920
and let's set some text text equals
14357
08:28:33,920 --> 08:28:36,958
hello this is tab
14358
08:28:36,958 --> 08:28:40,638
number one then i think i'll give this a
14359
08:28:40,638 --> 08:28:41,680
width and a height too
14360
08:28:41,680 --> 08:28:44,000
just so it's more visible so for the
14361
08:28:44,000 --> 08:28:45,920
width i'll make this 50
14362
08:28:45,920 --> 08:28:48,718
and the height 25 so this will be
14363
08:28:48,718 --> 08:28:51,200
roughly a square shape
14364
08:28:51,200 --> 08:28:52,798
and then we need to pack this so i'm
14365
08:28:52,798 --> 08:28:55,120
just going to add pack
14366
08:28:55,120 --> 08:28:57,760
and let's create a second label for tab
14367
08:28:57,760 --> 08:28:58,638
two
14368
08:28:58,638 --> 08:29:02,318
so label tab two maybe i'll say
14369
08:29:02,318 --> 08:29:05,840
goodbye this is tab number two
14370
08:29:05,840 --> 08:29:08,558
okay we should have some actual content
14371
08:29:08,558 --> 08:29:10,878
for these labels now for these tabs
14372
08:29:10,878 --> 08:29:13,520
so here's the label this is tab one and
14373
08:29:13,520 --> 08:29:15,120
this is tab two and you can switch
14374
08:29:15,120 --> 08:29:16,638
between these and they display
14375
08:29:16,638 --> 08:29:19,200
each of these separate labels however
14376
08:29:19,200 --> 08:29:20,718
with the way that this window is
14377
08:29:20,718 --> 08:29:21,440
arranged
14378
08:29:21,440 --> 08:29:23,600
if we were to expand the size of this
14379
08:29:23,600 --> 08:29:25,200
window well
14380
08:29:25,200 --> 08:29:27,760
our frames and our tabs kind of stay in
14381
08:29:27,760 --> 08:29:28,318
the top
14382
08:29:28,318 --> 08:29:30,798
and in the center so i'm going to add a
14383
08:29:30,798 --> 08:29:32,080
few lines within
14384
08:29:32,080 --> 08:29:34,398
the pack function of our notebook the
14385
08:29:34,398 --> 08:29:35,600
first thing i'm going to do
14386
08:29:35,600 --> 08:29:38,398
is use the expand option and set this to
14387
08:29:38,398 --> 08:29:40,160
true
14388
08:29:40,160 --> 08:29:41,600
so what this does and i'm just going to
14389
08:29:41,600 --> 08:29:44,000
add a comment that expand
14390
08:29:44,000 --> 08:29:47,200
this will expand
14391
08:29:47,200 --> 08:29:50,840
to fill any space
14392
08:29:50,840 --> 08:29:53,760
not otherwise
14393
08:29:53,760 --> 08:29:57,280
used so what happens now
14394
08:29:57,280 --> 08:30:00,318
is that this will actually expand
14395
08:30:00,318 --> 08:30:02,000
and it's staying in the center of the
14396
08:30:02,000 --> 08:30:04,160
window but there's one more thing we're
14397
08:30:04,160 --> 08:30:05,680
going to add
14398
08:30:05,680 --> 08:30:08,798
fill equals both
14399
08:30:08,798 --> 08:30:11,520
normally this is x or y or you can do
14400
08:30:11,520 --> 08:30:12,478
both
14401
08:30:12,478 --> 08:30:14,478
so i'll add a comment as to what this
14402
08:30:14,478 --> 08:30:16,318
option does
14403
08:30:16,318 --> 08:30:19,520
so fill will fill
14404
08:30:19,520 --> 08:30:22,878
space on x and
14405
08:30:22,878 --> 08:30:26,160
y axis so if you combine
14406
08:30:26,160 --> 08:30:28,398
both of these together what you get is
14407
08:30:28,398 --> 08:30:30,080
that these tabs will stay
14408
08:30:30,080 --> 08:30:32,318
in the top left corner and if you were
14409
08:30:32,318 --> 08:30:34,558
to expand the size of this window
14410
08:30:34,558 --> 08:30:36,318
the tabs aren't going to move they're
14411
08:30:36,318 --> 08:30:38,160
going to stick to the top left
14412
08:30:38,160 --> 08:30:41,120
but the size of the window will expand
14413
08:30:41,120 --> 08:30:42,878
all right so that's the basics of
14414
08:30:42,878 --> 08:30:44,878
creating tabs in python if you want a
14415
08:30:44,878 --> 08:30:45,920
copy of all this code
14416
08:30:45,920 --> 08:30:47,360
i'll post all of this in the comments
14417
08:30:47,360 --> 08:30:49,520
down below but yeah that's the basics of
14418
08:30:49,520 --> 08:30:52,878
creating tabs in python
14419
08:30:52,878 --> 08:30:54,478
hey what's going on everybody it's your
14420
08:30:54,478 --> 08:30:56,240
bro here hope you're doing well and in
14421
08:30:56,240 --> 08:30:57,600
this video i'm going to teach you guys
14422
08:30:57,600 --> 08:30:59,760
all about the grid geometry manager in
14423
08:30:59,760 --> 08:31:02,080
python so sit back relax and enjoy the
14424
08:31:02,080 --> 08:31:04,318
show
14425
08:31:04,478 --> 08:31:06,398
all right everybody let's talk about the
14426
08:31:06,398 --> 08:31:07,920
grid geometry manager
14427
08:31:07,920 --> 08:31:10,398
well because we have to this is a useful
14428
08:31:10,398 --> 08:31:12,080
way to organize widgets within a
14429
08:31:12,080 --> 08:31:12,878
container
14430
08:31:12,878 --> 08:31:14,958
imagine our windows being arranged into
14431
08:31:14,958 --> 08:31:16,080
a series of
14432
08:31:16,080 --> 08:31:18,638
rows and columns similar to an excel
14433
08:31:18,638 --> 08:31:19,360
spreadsheet
14434
08:31:19,360 --> 08:31:21,920
i tend to use that example a lot the top
14435
08:31:21,920 --> 08:31:22,958
left most cell
14436
08:31:22,958 --> 08:31:25,760
is row zero column zero computers always
14437
08:31:25,760 --> 08:31:27,440
begin with zero so that's why our grid
14438
08:31:27,440 --> 08:31:28,558
doesn't begin with
14439
08:31:28,558 --> 08:31:31,520
row one column one and we can list which
14440
08:31:31,520 --> 08:31:32,478
row and column
14441
08:31:32,478 --> 08:31:34,240
we would like our widgets to be placed
14442
08:31:34,240 --> 08:31:36,558
within and by default we only have
14443
08:31:36,558 --> 08:31:38,718
one row and one column to work with at
14444
08:31:38,718 --> 08:31:41,280
the start but we can explicitly state
14445
08:31:41,280 --> 08:31:43,120
which row and column this widget should
14446
08:31:43,120 --> 08:31:44,318
be placed within
14447
08:31:44,318 --> 08:31:46,240
relative to the container that it's in
14448
08:31:46,240 --> 08:31:48,318
and python will create new relative rows
14449
08:31:48,318 --> 08:31:50,080
and columns for us if we state where
14450
08:31:50,080 --> 08:31:50,878
these widgets
14451
08:31:50,878 --> 08:31:53,360
should be placed so let's create a small
14452
08:31:53,360 --> 08:31:54,398
submission form
14453
08:31:54,398 --> 08:31:55,920
and then organize all of our widgets
14454
08:31:55,920 --> 08:31:58,160
using our new grid system that we have
14455
08:31:58,160 --> 08:32:00,080
so let's begin by creating a window
14456
08:32:00,080 --> 08:32:01,440
window equals tk
14457
08:32:01,440 --> 08:32:04,000
followed by window.mainloop now that we
14458
08:32:04,000 --> 08:32:05,440
have our window we'll need to create
14459
08:32:05,440 --> 08:32:07,520
some widgets to add to this window
14460
08:32:07,520 --> 08:32:10,080
so let's say that we want an entry box
14461
08:32:10,080 --> 08:32:12,638
and a label for a first name last name
14462
08:32:12,638 --> 08:32:15,120
and email address as well as a submit
14463
08:32:15,120 --> 08:32:15,840
button
14464
08:32:15,840 --> 08:32:18,558
so let's create a label and an entry box
14465
08:32:18,558 --> 08:32:20,558
for a first name just to demonstrate
14466
08:32:20,558 --> 08:32:21,120
this
14467
08:32:21,120 --> 08:32:24,558
so let's call this label first name
14468
08:32:24,558 --> 08:32:27,120
label equals label we're adding this to
14469
08:32:27,120 --> 08:32:27,840
the window
14470
08:32:27,840 --> 08:32:31,520
and let's set the text to first
14471
08:32:31,520 --> 08:32:34,558
name for now i'm just going to use that
14472
08:32:34,558 --> 08:32:36,318
pack to demonstrate the differences
14473
08:32:36,318 --> 08:32:36,958
between
14474
08:32:36,958 --> 08:32:40,160
pack and grid and let's create an entry
14475
08:32:40,160 --> 08:32:41,360
box
14476
08:32:41,360 --> 08:32:45,040
let's call this first name
14477
08:32:45,040 --> 08:32:48,398
entry equals entry
14478
08:32:48,398 --> 08:32:51,360
we are adding this to the window and for
14479
08:32:51,360 --> 08:32:52,638
now we're just going to
14480
08:32:52,638 --> 08:32:57,600
pack this and let's take a look
14481
08:32:57,600 --> 08:32:59,600
so here's our label on the top it says
14482
08:32:59,600 --> 08:33:01,200
first name and then it has
14483
08:33:01,200 --> 08:33:03,840
our entry box right underneath so what
14484
08:33:03,840 --> 08:33:05,200
if we want to have
14485
08:33:05,200 --> 08:33:08,160
this label on the left hand side and our
14486
08:33:08,160 --> 08:33:09,760
entry box on the right hand side of the
14487
08:33:09,760 --> 08:33:10,798
label
14488
08:33:10,798 --> 08:33:13,440
we're better off using the grid geometry
14489
08:33:13,440 --> 08:33:14,160
manager
14490
08:33:14,160 --> 08:33:18,878
so we're going to replace pack with grid
14491
08:33:21,040 --> 08:33:23,280
and we have to set which row and column
14492
08:33:23,280 --> 08:33:24,958
that we want each of these widgets to be
14493
08:33:24,958 --> 08:33:26,000
placed within
14494
08:33:26,000 --> 08:33:29,040
normally if you use the pack geometry
14495
08:33:29,040 --> 08:33:29,920
manager
14496
08:33:29,920 --> 08:33:31,840
as you create new widgets they're just
14497
08:33:31,840 --> 08:33:33,840
going to be placed directly underneath
14498
08:33:33,840 --> 08:33:36,878
in one long column so
14499
08:33:36,878 --> 08:33:39,440
let's say that for our name label we
14500
08:33:39,440 --> 08:33:41,600
want to place this where
14501
08:33:41,600 --> 08:33:43,520
the row is zero and the column is zero
14502
08:33:43,520 --> 08:33:45,440
that's the top left corner
14503
08:33:45,440 --> 08:33:48,398
so within the grid geometry manager when
14504
08:33:48,398 --> 08:33:49,280
we call it
14505
08:33:49,280 --> 08:33:52,398
we're going to set row to equal zero as
14506
08:33:52,398 --> 08:33:53,760
well as column
14507
08:33:53,760 --> 08:33:56,558
to equal zero and then for the first
14508
08:33:56,558 --> 08:33:56,878
name
14509
08:33:56,878 --> 08:33:58,798
entry we want this to be on the right
14510
08:33:58,798 --> 08:34:00,478
hand side of our label
14511
08:34:00,478 --> 08:34:03,120
so we'll place this within the same row
14512
08:34:03,120 --> 08:34:03,680
row
14513
08:34:03,680 --> 08:34:06,638
equals zero but in a different column
14514
08:34:06,638 --> 08:34:07,280
column
14515
08:34:07,280 --> 08:34:10,718
equals one so column one is directly to
14516
08:34:10,718 --> 08:34:12,160
the right of column zero
14517
08:34:12,160 --> 08:34:14,398
and then when we compile and run this we
14518
08:34:14,398 --> 08:34:16,240
now have our entry box
14519
08:34:16,240 --> 08:34:18,398
to the right of our label that we have
14520
08:34:18,398 --> 08:34:19,840
so let's repeat the process
14521
08:34:19,840 --> 08:34:22,240
for a last name an email address and
14522
08:34:22,240 --> 08:34:23,760
then a submit button
14523
08:34:23,760 --> 08:34:25,520
so i'm just going to copy what we have
14524
08:34:25,520 --> 08:34:27,600
here and change first name to
14525
08:34:27,600 --> 08:34:31,680
last name so last name label
14526
08:34:31,680 --> 08:34:35,040
and we'll change the text to last name
14527
08:34:35,040 --> 08:34:38,718
last name entry and we'll keep that the
14528
08:34:38,718 --> 08:34:39,920
same
14529
08:34:39,920 --> 08:34:42,638
now we want this last name label to be
14530
08:34:42,638 --> 08:34:43,600
underneath
14531
08:34:43,600 --> 08:34:45,840
the first name label because right now
14532
08:34:45,840 --> 08:34:47,440
they're going to overlap because we have
14533
08:34:47,440 --> 08:34:48,318
them in the same
14534
08:34:48,318 --> 08:34:50,398
row and columns so you can see that we
14535
08:34:50,398 --> 08:34:51,360
can no longer see
14536
08:34:51,360 --> 08:34:53,680
the first name label or the first name
14537
08:34:53,680 --> 08:34:54,478
entry
14538
08:34:54,478 --> 08:34:56,478
so let's place this directly underneath
14539
08:34:56,478 --> 08:34:58,080
we're going to put these underneath by
14540
08:34:58,080 --> 08:34:59,600
one row
14541
08:34:59,600 --> 08:35:01,680
so we'll keep the columns the same but
14542
08:35:01,680 --> 08:35:02,798
we're going to
14543
08:35:02,798 --> 08:35:05,040
put this underneath by one row so row
14544
08:35:05,040 --> 08:35:05,920
one
14545
08:35:05,920 --> 08:35:08,958
and row one for the last name entry and
14546
08:35:08,958 --> 08:35:10,240
what you get is that
14547
08:35:10,240 --> 08:35:12,398
these two labels and entry boxes are
14548
08:35:12,398 --> 08:35:14,398
arranged neatly into
14549
08:35:14,398 --> 08:35:16,558
rows and columns and let's do the same
14550
08:35:16,558 --> 08:35:18,958
thing for an email
14551
08:35:18,958 --> 08:35:20,958
so i'm going to copy what we have and
14552
08:35:20,958 --> 08:35:22,000
we're going to change
14553
08:35:22,000 --> 08:35:25,040
last name to email so
14554
08:35:25,040 --> 08:35:28,080
email label and email entry and then
14555
08:35:28,080 --> 08:35:28,478
change
14556
08:35:28,478 --> 08:35:31,520
for the text last name to just email
14557
08:35:31,520 --> 08:35:34,478
and we're going to place these where row
14558
08:35:34,478 --> 08:35:35,200
equals 2
14559
08:35:35,200 --> 08:35:37,200
and we'll keep the column the same and
14560
08:35:37,200 --> 08:35:39,760
for email entry
14561
08:35:39,760 --> 08:35:43,200
row 2 column one
14562
08:35:43,840 --> 08:35:45,840
so doesn't that look a lot better now
14563
08:35:45,840 --> 08:35:48,958
let's create a submit button
14564
08:35:48,958 --> 08:35:52,240
so let's call this submit button
14565
08:35:52,240 --> 08:35:54,798
equals button and then we're adding this
14566
08:35:54,798 --> 08:35:56,080
to the window
14567
08:35:56,080 --> 08:35:59,360
and we should set some text text equals
14568
08:35:59,360 --> 08:36:03,920
submit i guess dot grid
14569
08:36:03,920 --> 08:36:06,718
and we want to place this underneath
14570
08:36:06,718 --> 08:36:07,920
currently kind of just shoves it
14571
08:36:07,920 --> 08:36:08,958
underneath for now
14572
08:36:08,958 --> 08:36:11,280
let's say that we want this between
14573
08:36:11,280 --> 08:36:13,840
these two columns
14574
08:36:13,840 --> 08:36:15,840
so we'll want to set the row and the
14575
08:36:15,840 --> 08:36:17,120
column to begin with
14576
08:36:17,120 --> 08:36:20,160
row is going to equal three
14577
08:36:20,160 --> 08:36:22,478
and column we'll set this equal to zero
14578
08:36:22,478 --> 08:36:23,360
for now
14579
08:36:23,360 --> 08:36:24,878
there isn't going to be any apparent
14580
08:36:24,878 --> 08:36:26,080
change it's still going to be in the
14581
08:36:26,080 --> 08:36:27,280
same place
14582
08:36:27,280 --> 08:36:29,200
but one thing that we can do is use an
14583
08:36:29,200 --> 08:36:30,478
option called column
14584
08:36:30,478 --> 08:36:32,878
span we can have a widget take up more
14585
08:36:32,878 --> 08:36:34,160
than one column
14586
08:36:34,160 --> 08:36:36,160
and it's going to place this widget in
14587
08:36:36,160 --> 08:36:38,240
between both of these columns
14588
08:36:38,240 --> 08:36:40,478
in between the combined width of both of
14589
08:36:40,478 --> 08:36:41,280
them
14590
08:36:41,280 --> 08:36:44,398
so we'll add column span
14591
08:36:44,398 --> 08:36:47,120
equals two so this widget is going to
14592
08:36:47,120 --> 08:36:48,000
take up
14593
08:36:48,000 --> 08:36:50,398
the next two available columns including
14594
08:36:50,398 --> 08:36:52,080
the one that it's currently in and you
14595
08:36:52,080 --> 08:36:54,000
can see that our submit button
14596
08:36:54,000 --> 08:36:56,558
is now between these two columns that we
14597
08:36:56,558 --> 08:36:57,520
have
14598
08:36:57,520 --> 08:36:59,360
one thing that you should know is that
14599
08:36:59,360 --> 08:37:01,440
the column width is dependent on the
14600
08:37:01,440 --> 08:37:01,840
width
14601
08:37:01,840 --> 08:37:03,760
of the largest widget that is contained
14602
08:37:03,760 --> 08:37:05,680
within that particular column
14603
08:37:05,680 --> 08:37:07,680
let's say that we increase the width of
14604
08:37:07,680 --> 08:37:08,798
our first name
14605
08:37:08,798 --> 08:37:11,520
label well our column is going to expand
14606
08:37:11,520 --> 08:37:12,718
to actually fit
14607
08:37:12,718 --> 08:37:14,638
that widget that we have so for our
14608
08:37:14,638 --> 08:37:15,840
first name label
14609
08:37:15,840 --> 08:37:19,680
let's set the width to maybe 20
14610
08:37:19,680 --> 08:37:22,160
and this is going to increase the width
14611
08:37:22,160 --> 08:37:23,600
of column 0
14612
08:37:23,600 --> 08:37:25,840
and to even better demonstrate this
14613
08:37:25,840 --> 08:37:26,638
let's color
14614
08:37:26,638 --> 08:37:28,958
each label a certain color just for a
14615
08:37:28,958 --> 08:37:29,840
visual
14616
08:37:29,840 --> 08:37:31,680
for our first name label let's say this
14617
08:37:31,680 --> 08:37:34,000
is red and then for
14618
08:37:34,000 --> 08:37:36,398
our last name label let's set this to
14619
08:37:36,398 --> 08:37:38,478
green
14620
08:37:38,478 --> 08:37:42,160
and then for email maybe blue
14621
08:37:46,558 --> 08:37:48,638
now all of these are color coordinated
14622
08:37:48,638 --> 08:37:49,760
and you can see that
14623
08:37:49,760 --> 08:37:51,760
the background color of our first name
14624
08:37:51,760 --> 08:37:52,958
label
14625
08:37:52,958 --> 08:37:55,760
is taking up the entire column space now
14626
08:37:55,760 --> 08:37:58,080
what if we changed the size of our
14627
08:37:58,080 --> 08:38:00,160
email label to something even larger
14628
08:38:00,160 --> 08:38:01,600
like 30
14629
08:38:01,600 --> 08:38:05,120
so width equals 30.
14630
08:38:06,000 --> 08:38:09,760
and now the column 0 has expanded to
14631
08:38:09,760 --> 08:38:11,600
accommodate the size of our largest
14632
08:38:11,600 --> 08:38:13,280
widget which is our email
14633
08:38:13,280 --> 08:38:15,920
which has a width of 30 and before we
14634
08:38:15,920 --> 08:38:17,760
finish this video let's add a
14635
08:38:17,760 --> 08:38:19,840
title to the submission form right on
14636
08:38:19,840 --> 08:38:22,000
top of the first name label and the
14637
08:38:22,000 --> 08:38:25,520
entry label so let's call this title
14638
08:38:25,520 --> 08:38:28,080
label equals label writing this to the
14639
08:38:28,080 --> 08:38:28,798
window
14640
08:38:28,798 --> 08:38:32,398
let's set some text let's say enter
14641
08:38:32,398 --> 08:38:36,318
your info and let's pick font
14642
08:38:36,318 --> 08:38:39,600
font equals
14643
08:38:40,080 --> 08:38:42,478
what am i feeling like today maybe just
14644
08:38:42,478 --> 08:38:43,680
arial
14645
08:38:43,680 --> 08:38:46,718
and then a size maybe 25 we're going to
14646
08:38:46,718 --> 08:38:47,280
use the
14647
08:38:47,280 --> 08:38:50,718
grid geometry manager
14648
08:38:50,718 --> 08:38:53,040
and we want this at the top so we want
14649
08:38:53,040 --> 08:38:55,040
row zero at least
14650
08:38:55,040 --> 08:38:57,920
i want this to take up two columns so
14651
08:38:57,920 --> 08:38:59,040
we'll say column
14652
08:38:59,040 --> 08:39:02,080
equals zero and then set a column span
14653
08:39:02,080 --> 08:39:05,920
column span equals two
14654
08:39:05,920 --> 08:39:07,920
now a few things are going to overlap
14655
08:39:07,920 --> 08:39:09,680
here
14656
08:39:09,680 --> 08:39:11,520
so we're going to move everything down
14657
08:39:11,520 --> 08:39:13,200
by one row
14658
08:39:13,200 --> 08:39:16,638
besides our title label so we want
14659
08:39:16,638 --> 08:39:18,318
this first name label to be directly
14660
08:39:18,318 --> 08:39:20,000
underneath our title label
14661
08:39:20,000 --> 08:39:27,840
so let's move everything down by one row
14662
08:39:32,398 --> 08:39:34,718
and our title is going to be directly on
14663
08:39:34,718 --> 08:39:37,040
top of our first name label as well as
14664
08:39:37,040 --> 08:39:38,000
the first name
14665
08:39:38,000 --> 08:39:40,798
entry box that we have all right so
14666
08:39:40,798 --> 08:39:42,718
that's the very basics of the grid
14667
08:39:42,718 --> 08:39:43,920
geometry manager
14668
08:39:43,920 --> 08:39:45,760
if you want a copy of all this code i'll
14669
08:39:45,760 --> 08:39:47,280
post all of this in the comments down
14670
08:39:47,280 --> 08:39:47,920
below
14671
08:39:47,920 --> 08:39:49,760
but yeah that's the basics of the grid
14672
08:39:49,760 --> 08:39:50,958
geometry manager
14673
08:39:50,958 --> 08:39:53,760
in python hey what's going on everybody
14674
08:39:53,760 --> 08:39:55,200
it's your bro here hope you're doing
14675
08:39:55,200 --> 08:39:56,398
well and in this video i'm going to
14676
08:39:56,398 --> 08:39:57,840
teach you guys how we can create a
14677
08:39:57,840 --> 08:39:59,360
progress bar in python so
14678
08:39:59,360 --> 08:40:03,120
sit back relax and enjoy the show
14679
08:40:03,120 --> 08:40:05,040
okay let's create a progress bar but
14680
08:40:05,040 --> 08:40:07,520
we'll need a few imports to begin
14681
08:40:07,520 --> 08:40:09,520
the progress bar widget is found within
14682
08:40:09,520 --> 08:40:12,160
that ttk module so we'll need a separate
14683
08:40:12,160 --> 08:40:13,600
import from that
14684
08:40:13,600 --> 08:40:16,638
from tk enter dot ttk
14685
08:40:16,638 --> 08:40:19,680
import everything and we'll import
14686
08:40:19,680 --> 08:40:22,160
everything from time as well let's
14687
08:40:22,160 --> 08:40:23,120
create a window
14688
08:40:23,120 --> 08:40:25,240
window equals tk followed by
14689
08:40:25,240 --> 08:40:26,638
window.mainloop
14690
08:40:26,638 --> 08:40:28,798
we're going to add a progress bar and a
14691
08:40:28,798 --> 08:40:30,000
button to this window
14692
08:40:30,000 --> 08:40:31,600
when we click on the button it's going
14693
08:40:31,600 --> 08:40:33,360
to fill our progress bar
14694
08:40:33,360 --> 08:40:35,120
let's begin by creating a button to
14695
08:40:35,120 --> 08:40:37,680
begin with let's call this button
14696
08:40:37,680 --> 08:40:40,160
equals button we're adding this to the
14697
08:40:40,160 --> 08:40:41,040
window
14698
08:40:41,040 --> 08:40:43,920
let's set some text text equals maybe
14699
08:40:43,920 --> 08:40:45,920
download
14700
08:40:45,920 --> 08:40:48,840
and then a command maybe i'll call this
14701
08:40:48,840 --> 08:40:50,318
start
14702
08:40:50,318 --> 08:40:52,638
finish by adding this button to the
14703
08:40:52,638 --> 08:40:54,478
window and we'll just use pack to do
14704
08:40:54,478 --> 08:40:55,280
that
14705
08:40:55,280 --> 08:40:57,360
let's define what our start function
14706
08:40:57,360 --> 08:40:58,398
does
14707
08:40:58,398 --> 08:41:01,280
def start we'll fill this in later i'm
14708
08:41:01,280 --> 08:41:02,398
just going to
14709
08:41:02,398 --> 08:41:05,440
print nothing just as a placeholder and
14710
08:41:05,440 --> 08:41:07,280
we should just have a button
14711
08:41:07,280 --> 08:41:09,360
now let's add a progress bar before this
14712
08:41:09,360 --> 08:41:11,040
button
14713
08:41:11,040 --> 08:41:12,958
and we'll call this progress bar just
14714
08:41:12,958 --> 08:41:14,718
bar for short bar
14715
08:41:14,718 --> 08:41:18,958
equals progress bar
14716
08:41:18,958 --> 08:41:22,160
we're adding this to the window
14717
08:41:22,160 --> 08:41:25,200
we can set an orientation
14718
08:41:25,200 --> 08:41:28,000
this can be horizontal or vertical later
14719
08:41:28,000 --> 08:41:29,520
on in this video i'll show you the
14720
08:41:29,520 --> 08:41:30,878
differences between the two
14721
08:41:30,878 --> 08:41:34,958
but for now let's set this to horizontal
14722
08:41:34,958 --> 08:41:37,360
and then i'm going to pack this bar bar
14723
08:41:37,360 --> 08:41:38,318
dot pack
14724
08:41:38,318 --> 08:41:41,520
and add some padding pad y
14725
08:41:41,520 --> 08:41:46,398
equals 10 and let's see what we have
14726
08:41:46,398 --> 08:41:48,718
okay this is our progress bar at the top
14727
08:41:48,718 --> 08:41:49,920
and this is the button
14728
08:41:49,920 --> 08:41:51,440
when we click on this button it's going
14729
08:41:51,440 --> 08:41:53,440
to fill our progress bar
14730
08:41:53,440 --> 08:41:55,280
but i think i'm going to increase the
14731
08:41:55,280 --> 08:41:57,360
length of this progress bar
14732
08:41:57,360 --> 08:41:59,680
let's set this to maybe 300 i believe
14733
08:41:59,680 --> 08:42:01,760
100 is the default
14734
08:42:01,760 --> 08:42:04,718
okay that's not too bad of a size now
14735
08:42:04,718 --> 08:42:06,638
let's define what our start function
14736
08:42:06,638 --> 08:42:07,600
does
14737
08:42:07,600 --> 08:42:10,080
we can actually increase the value of
14738
08:42:10,080 --> 08:42:11,920
our progress bar by typing in the name
14739
08:42:11,920 --> 08:42:14,798
of this progress bar here
14740
08:42:14,798 --> 08:42:18,958
value plus equals some amount let's say
14741
08:42:18,958 --> 08:42:22,398
10. so what's going to happen right now
14742
08:42:22,398 --> 08:42:24,080
is that every time we click on this
14743
08:42:24,080 --> 08:42:26,318
button it's going to fill our progress
14744
08:42:26,318 --> 08:42:27,040
bar by
14745
08:42:27,040 --> 08:42:31,520
10 out of the total which is 100
14746
08:42:32,240 --> 08:42:34,240
let's pretend that we're waiting for a
14747
08:42:34,240 --> 08:42:35,520
certain amount of tasks
14748
08:42:35,520 --> 08:42:38,478
to complete so let's define a few
14749
08:42:38,478 --> 08:42:39,120
variables
14750
08:42:39,120 --> 08:42:41,360
let's say we have a variable called
14751
08:42:41,360 --> 08:42:43,440
tasks and let's set this to 10 just to
14752
08:42:43,440 --> 08:42:45,520
keep it simple
14753
08:42:45,520 --> 08:42:47,760
and x will represent the current task
14754
08:42:47,760 --> 08:42:48,798
that we're on
14755
08:42:48,798 --> 08:42:51,760
and we'll say 0 to begin with let's
14756
08:42:51,760 --> 08:42:53,440
create a while loop
14757
08:42:53,440 --> 08:42:57,040
while x is less than the amount of
14758
08:42:57,040 --> 08:43:00,240
tasks that we have we will
14759
08:43:00,240 --> 08:43:03,600
increment the value of our bar by 10.
14760
08:43:03,600 --> 08:43:06,638
10 10 for each test that we have which
14761
08:43:06,638 --> 08:43:09,440
is 10 for a total of 100
14762
08:43:09,440 --> 08:43:11,600
and we'll want to increment the task
14763
08:43:11,600 --> 08:43:12,718
that we're on as well
14764
08:43:12,718 --> 08:43:15,440
which is represented by x so let's
14765
08:43:15,440 --> 08:43:18,000
increment x by 1 after each iteration
14766
08:43:18,000 --> 08:43:21,840
of this while loop now when we run this
14767
08:43:21,840 --> 08:43:25,520
and click download this spills instantly
14768
08:43:25,520 --> 08:43:29,040
let's add a delay just to simulate
14769
08:43:29,040 --> 08:43:32,558
each task waiting to be completed so
14770
08:43:32,558 --> 08:43:35,040
let's add that here let's add a delay by
14771
08:43:35,040 --> 08:43:36,080
using time
14772
08:43:36,080 --> 08:43:38,718
dot sleep and let's set this to one
14773
08:43:38,718 --> 08:43:40,000
second
14774
08:43:40,000 --> 08:43:41,920
we're going to wait one second and then
14775
08:43:41,920 --> 08:43:44,718
complete a task
14776
08:43:45,120 --> 08:43:46,958
what ends up happening now is that
14777
08:43:46,958 --> 08:43:48,638
nothing appears to be updating
14778
08:43:48,638 --> 08:43:50,718
well what gives bro you lie to me well
14779
08:43:50,718 --> 08:43:51,680
that's because
14780
08:43:51,680 --> 08:43:53,840
the window is waiting to have this
14781
08:43:53,840 --> 08:43:55,200
progress bar complete
14782
08:43:55,200 --> 08:43:57,920
before it's actually going to display it
14783
08:43:57,920 --> 08:43:58,958
being filled
14784
08:43:58,958 --> 08:44:01,760
so we would want to refresh this window
14785
08:44:01,760 --> 08:44:04,398
after each iteration of the while loop
14786
08:44:04,398 --> 08:44:06,558
so at the end of this while loop let's
14787
08:44:06,558 --> 08:44:07,600
add one thing
14788
08:44:07,600 --> 08:44:11,600
window dot update underscore idle
14789
08:44:11,600 --> 08:44:14,478
tasks after each iteration of this while
14790
08:44:14,478 --> 08:44:16,000
loop it's going to update
14791
08:44:16,000 --> 08:44:18,558
the window that we have so then when we
14792
08:44:18,558 --> 08:44:20,080
download something
14793
08:44:20,080 --> 08:44:22,398
it's going to update our progress bar
14794
08:44:22,398 --> 08:44:24,558
after each iteration of this while loop
14795
08:44:24,558 --> 08:44:26,398
until it's completed
14796
08:44:26,398 --> 08:44:29,520
right about now let's take this a step
14797
08:44:29,520 --> 08:44:31,920
further by adding a label that displays
14798
08:44:31,920 --> 08:44:33,840
the current percent of the progress bar
14799
08:44:33,840 --> 08:44:34,958
that is complete
14800
08:44:34,958 --> 08:44:36,958
let's add this label after the progress
14801
08:44:36,958 --> 08:44:38,478
bar but before the button
14802
08:44:38,478 --> 08:44:39,920
sandwiched right between these two
14803
08:44:39,920 --> 08:44:42,398
widgets so let's add that here
14804
08:44:42,398 --> 08:44:45,440
we'll call this label percent label
14805
08:44:45,440 --> 08:44:47,760
equals label we'll add this to the
14806
08:44:47,760 --> 08:44:48,638
window
14807
08:44:48,638 --> 08:44:51,760
and we'll need a text variable text
14808
08:44:51,760 --> 08:44:54,958
variable equals let's call this percent
14809
08:44:54,958 --> 08:44:56,478
so the reason that we use a text
14810
08:44:56,478 --> 08:44:58,398
variable is so that we can update this
14811
08:44:58,398 --> 08:45:00,240
label with some text
14812
08:45:00,240 --> 08:45:02,718
after each iteration of our while loop
14813
08:45:02,718 --> 08:45:03,680
and we'll need to
14814
08:45:03,680 --> 08:45:07,280
pack this okay within the window
14815
08:45:07,280 --> 08:45:09,840
near the top let's define percent
14816
08:45:09,840 --> 08:45:10,558
percent
14817
08:45:10,558 --> 08:45:14,318
equals string var this allows us to
14818
08:45:14,318 --> 08:45:16,958
update percent with some new text and
14819
08:45:16,958 --> 08:45:18,958
set the percent label with the new text
14820
08:45:18,958 --> 08:45:20,558
that we have
14821
08:45:20,558 --> 08:45:22,798
within the while loop but before we
14822
08:45:22,798 --> 08:45:25,040
update idle tasks
14823
08:45:25,040 --> 08:45:28,240
let's set what percent is
14824
08:45:28,240 --> 08:45:32,240
percent dot set and let's say
14825
08:45:32,240 --> 08:45:35,600
x divided by tasks and then we'll
14826
08:45:35,600 --> 08:45:36,958
multiply this by
14827
08:45:36,958 --> 08:45:39,040
100 i'm just going to put this within
14828
08:45:39,040 --> 08:45:40,240
its own set of
14829
08:45:40,240 --> 08:45:43,280
parentheses times 100
14830
08:45:43,280 --> 08:45:44,878
and then we'll convert all of this to a
14831
08:45:44,878 --> 08:45:46,958
string
14832
08:45:46,958 --> 08:45:48,638
so we can display this along with some
14833
08:45:48,638 --> 08:45:50,160
text
14834
08:45:50,160 --> 08:45:51,840
there are a lot of parentheses here so
14835
08:45:51,840 --> 08:45:53,360
it might be difficult to keep track of
14836
08:45:53,360 --> 08:45:54,240
all this
14837
08:45:54,240 --> 08:45:56,000
all right and then we're just going to
14838
08:45:56,000 --> 08:45:57,600
tack on
14839
08:45:57,600 --> 08:46:03,040
percent and that's it let's try it
14840
08:46:03,520 --> 08:46:07,280
so we got 10 20 30
14841
08:46:07,280 --> 08:46:10,958
40 50. i'll shut up now one change that
14842
08:46:10,958 --> 08:46:13,120
i'm gonna make is that i'm going to cast
14843
08:46:13,120 --> 08:46:13,840
all of this
14844
08:46:13,840 --> 08:46:15,840
as an integer before we convert it to a
14845
08:46:15,840 --> 08:46:18,160
string
14846
08:46:18,398 --> 08:46:20,558
because i do not like that decimal at
14847
08:46:20,558 --> 08:46:22,638
the end of our percentage
14848
08:46:22,638 --> 08:46:25,040
so yeah it says 10 percent 20 so on and
14849
08:46:25,040 --> 08:46:26,000
so forth
14850
08:46:26,000 --> 08:46:28,000
let's add a label that displays the
14851
08:46:28,000 --> 08:46:29,680
current task that we're on
14852
08:46:29,680 --> 08:46:31,520
so let's copy what we have for our
14853
08:46:31,520 --> 08:46:32,878
percent label
14854
08:46:32,878 --> 08:46:35,680
and let's rename this as maybe task
14855
08:46:35,680 --> 08:46:38,000
label
14856
08:46:38,080 --> 08:46:41,600
task label text variable will equal
14857
08:46:41,600 --> 08:46:43,440
maybe text
14858
08:46:43,440 --> 08:46:46,878
and let's define what text is so text
14859
08:46:46,878 --> 08:46:50,478
is also a string var and within our
14860
08:46:50,478 --> 08:46:51,120
while loop
14861
08:46:51,120 --> 08:46:54,638
let's update the value of our text
14862
08:46:54,638 --> 08:46:57,840
text dot set
14863
08:46:57,840 --> 08:47:01,280
will display x plus maybe a forward
14864
08:47:01,280 --> 08:47:03,680
slash
14865
08:47:05,120 --> 08:47:08,398
plus our tasks
14866
08:47:08,398 --> 08:47:10,878
and since we're displaying integers
14867
08:47:10,878 --> 08:47:12,478
along with some strings
14868
08:47:12,478 --> 08:47:14,638
we need to convert these to strings and
14869
08:47:14,638 --> 08:47:16,718
we can use the str function to do that
14870
08:47:16,718 --> 08:47:19,120
for us
14871
08:47:19,920 --> 08:47:22,798
so surround x and tasks with str to
14872
08:47:22,798 --> 08:47:25,040
convert these to strings
14873
08:47:25,040 --> 08:47:28,638
and then we'll add just plus tasks
14874
08:47:28,638 --> 08:47:33,280
completed so the value of x will update
14875
08:47:33,280 --> 08:47:35,840
after each iteration of this while loop
14876
08:47:35,840 --> 08:47:36,718
let's try it
14877
08:47:36,718 --> 08:47:39,440
and after clicking download this will
14878
08:47:39,440 --> 08:47:41,920
display the percentage as well as the
14879
08:47:41,920 --> 08:47:44,638
amount of tasks that have been completed
14880
08:47:44,638 --> 08:47:48,160
all the way until it reaches the end
14881
08:47:49,040 --> 08:47:51,200
let's change this into a more practical
14882
08:47:51,200 --> 08:47:52,160
example
14883
08:47:52,160 --> 08:47:53,840
we'll pretend that we're downloading a
14884
08:47:53,840 --> 08:47:56,798
video game so let's replace or rename
14885
08:47:56,798 --> 08:48:00,160
tasks as maybe gb4 gigabytes
14886
08:48:00,160 --> 08:48:03,440
and this game will be 100 gigabytes
14887
08:48:03,440 --> 08:48:07,040
we'll rename x as well so let's refactor
14888
08:48:07,040 --> 08:48:09,760
rename and change x to maybe download to
14889
08:48:09,760 --> 08:48:10,798
represent
14890
08:48:10,798 --> 08:48:12,398
the amount of gigabytes that are
14891
08:48:12,398 --> 08:48:14,240
downloaded
14892
08:48:14,240 --> 08:48:17,120
and let's create a variable called speed
14893
08:48:17,120 --> 08:48:19,520
and let's set this to one
14894
08:48:19,520 --> 08:48:22,558
alright so while our download
14895
08:48:22,558 --> 08:48:24,958
is less than the amount of gigabytes for
14896
08:48:24,958 --> 08:48:25,920
this game
14897
08:48:25,920 --> 08:48:28,000
we'll sleep for maybe a portion of a
14898
08:48:28,000 --> 08:48:28,958
second
14899
08:48:28,958 --> 08:48:32,160
.05 should be good we'll increase
14900
08:48:32,160 --> 08:48:35,840
the value of our bar by
14901
08:48:35,840 --> 08:48:38,958
this amount speed divided by
14902
08:48:38,958 --> 08:48:43,760
gigabytes and then multiply this by 100
14903
08:48:44,160 --> 08:48:46,000
for the download we're going to
14904
08:48:46,000 --> 08:48:47,360
increment this by whatever
14905
08:48:47,360 --> 08:48:49,520
our speed is and right now it's one so
14906
08:48:49,520 --> 08:48:50,718
that's the same
14907
08:48:50,718 --> 08:48:53,040
we can keep percent the same for our
14908
08:48:53,040 --> 08:48:53,760
text
14909
08:48:53,760 --> 08:48:57,200
let's change tasks to gigabytes
14910
08:48:57,200 --> 08:48:58,718
and that's all the changes that we have
14911
08:48:58,718 --> 08:49:00,240
to make and
14912
08:49:00,240 --> 08:49:02,718
now when we download something we're
14913
08:49:02,718 --> 08:49:04,000
kind of pretending that we're
14914
08:49:04,000 --> 08:49:05,040
downloading
14915
08:49:05,040 --> 08:49:07,520
a large file or a game you can see that
14916
08:49:07,520 --> 08:49:10,398
once it reaches 100 it says 100
14917
08:49:10,398 --> 08:49:13,360
100 out of 100 gigabytes completed now
14918
08:49:13,360 --> 08:49:14,000
we can change
14919
08:49:14,000 --> 08:49:15,760
this to a different value let's say
14920
08:49:15,760 --> 08:49:17,680
maybe 50
14921
08:49:17,680 --> 08:49:21,280
and the speed will be maybe two
14922
08:49:21,280 --> 08:49:23,280
so this will adjust to accommodate
14923
08:49:23,280 --> 08:49:25,520
whatever the new file size is as well as
14924
08:49:25,520 --> 08:49:26,798
the speed
14925
08:49:26,798 --> 08:49:28,958
and before i forget you can change the
14926
08:49:28,958 --> 08:49:30,318
orientation to a
14927
08:49:30,318 --> 08:49:33,920
vertical bar if you so choose so we have
14928
08:49:33,920 --> 08:49:35,680
this vertical download bar now
14929
08:49:35,680 --> 08:49:37,680
which could be good for something all
14930
08:49:37,680 --> 08:49:39,280
right so that's the basics
14931
08:49:39,280 --> 08:49:41,440
of progress bars in python if you want
14932
08:49:41,440 --> 08:49:42,878
to copy of all this code
14933
08:49:42,878 --> 08:49:44,398
i'll post all of this in the comments
14934
08:49:44,398 --> 08:49:46,638
down below but yeah that's the basics of
14935
08:49:46,638 --> 08:49:47,520
progress bars
14936
08:49:47,520 --> 08:49:50,558
in python what's going on everybody it's
14937
08:49:50,558 --> 08:49:52,160
your bro here hope you're doing well and
14938
08:49:52,160 --> 08:49:53,280
in this video i'm going to teach you
14939
08:49:53,280 --> 08:49:55,280
guys how we can use the canvas widget to
14940
08:49:55,280 --> 08:49:56,878
draw some simple shapes so
14941
08:49:56,878 --> 08:50:00,638
sit back relax and enjoy the show
14942
08:50:01,040 --> 08:50:03,520
welcome back everyone well in this video
14943
08:50:03,520 --> 08:50:05,440
we're going to be creating a canvas
14944
08:50:05,440 --> 08:50:07,600
a canvas is a widget that is used to
14945
08:50:07,600 --> 08:50:09,840
draw simple graphs plots and images
14946
08:50:09,840 --> 08:50:11,120
within a window
14947
08:50:11,120 --> 08:50:12,718
we're going to be creating some simple
14948
08:50:12,718 --> 08:50:14,798
shapes in this video not anything too
14949
08:50:14,798 --> 08:50:16,958
sophisticated i might get more into the
14950
08:50:16,958 --> 08:50:18,958
canvas widget in a separate video
14951
08:50:18,958 --> 08:50:20,478
and at the end of this lesson we're
14952
08:50:20,478 --> 08:50:22,958
going to practice by creating a pokeball
14953
08:50:22,958 --> 08:50:25,360
so to begin we'll need a window window
14954
08:50:25,360 --> 08:50:26,318
equals tk
14955
08:50:26,318 --> 08:50:28,958
followed by window.main loop to create a
14956
08:50:28,958 --> 08:50:30,638
canvas we can just simply call this
14957
08:50:30,638 --> 08:50:31,440
canvas
14958
08:50:31,440 --> 08:50:33,680
canvas equals canvas and we're adding
14959
08:50:33,680 --> 08:50:34,638
this canvas
14960
08:50:34,638 --> 08:50:36,718
to our window and then we need to follow
14961
08:50:36,718 --> 08:50:38,478
this with canvas.pack or
14962
08:50:38,478 --> 08:50:40,478
place but i'm just going to use pack
14963
08:50:40,478 --> 08:50:41,920
because it's simple
14964
08:50:41,920 --> 08:50:43,760
this is what our canvas looks like it's
14965
08:50:43,760 --> 08:50:45,760
a rectangular area which we can draw
14966
08:50:45,760 --> 08:50:46,718
graphics on
14967
08:50:46,718 --> 08:50:48,718
we can also set the width and the height
14968
08:50:48,718 --> 08:50:50,718
of this canvas so when we create this
14969
08:50:50,718 --> 08:50:51,360
canvas
14970
08:50:51,360 --> 08:50:53,280
we're going to set the options for a
14971
08:50:53,280 --> 08:50:54,398
height and a width
14972
08:50:54,398 --> 08:50:56,160
so let's begin with the height height
14973
08:50:56,160 --> 08:50:58,240
equals let's say 500
14974
08:50:58,240 --> 08:51:01,360
and the width will be the same 500 and
14975
08:51:01,360 --> 08:51:02,240
now we have
14976
08:51:02,240 --> 08:51:04,240
a square canvas that we can draw
14977
08:51:04,240 --> 08:51:05,280
graphics on
14978
08:51:05,280 --> 08:51:07,040
let's begin by creating some simple
14979
08:51:07,040 --> 08:51:08,638
shapes maybe a line
14980
08:51:08,638 --> 08:51:10,478
i think a line is a shape maybe a
14981
08:51:10,478 --> 08:51:12,318
one-dimensional one i don't know
14982
08:51:12,318 --> 08:51:14,878
i don't geometry so let's create a
14983
08:51:14,878 --> 08:51:16,160
simple line
14984
08:51:16,160 --> 08:51:19,120
so we type the name of the canvas dot
14985
08:51:19,120 --> 08:51:20,398
create and there's a bunch of different
14986
08:51:20,398 --> 08:51:21,600
shapes that we can create
14987
08:51:21,600 --> 08:51:24,478
let's create a line
14988
08:51:24,718 --> 08:51:26,000
now i'm just going to turn this into a
14989
08:51:26,000 --> 08:51:28,240
comment for a moment we need to set some
14990
08:51:28,240 --> 08:51:28,958
coordinates
14991
08:51:28,958 --> 08:51:31,840
for this line this top left corner of
14992
08:51:31,840 --> 08:51:32,638
our canvas
14993
08:51:32,638 --> 08:51:35,760
is 0 0 and the bottom right corner of
14994
08:51:35,760 --> 08:51:36,798
this canvas is
14995
08:51:36,798 --> 08:51:39,440
500 by 500 depends on what you set the
14996
08:51:39,440 --> 08:51:41,280
dimensions of the canvas to
14997
08:51:41,280 --> 08:51:43,360
so when we create a line we need a
14998
08:51:43,360 --> 08:51:45,440
starting point and an ending point
14999
08:51:45,440 --> 08:51:47,120
so if we want this to start in the top
15000
08:51:47,120 --> 08:51:49,360
left corner we're going to pass in
15001
08:51:49,360 --> 08:51:52,398
0 0 that is the starting position and we
15002
08:51:52,398 --> 08:51:54,160
can place the coordinates for the ending
15003
08:51:54,160 --> 08:51:54,958
position
15004
08:51:54,958 --> 08:51:56,398
if we want this line to end in the
15005
08:51:56,398 --> 08:51:58,478
bottom right corner that would be
15006
08:51:58,478 --> 08:52:02,398
where x is 500 and y is 500 and this is
15007
08:52:02,398 --> 08:52:03,440
what this looks like
15008
08:52:03,440 --> 08:52:05,760
we just have a simple line going across
15009
08:52:05,760 --> 08:52:06,798
the screen
15010
08:52:06,798 --> 08:52:08,878
but that's not all we can also change
15011
08:52:08,878 --> 08:52:10,160
the color of this line
15012
08:52:10,160 --> 08:52:11,840
and the thickness let's begin by
15013
08:52:11,840 --> 08:52:13,440
changing the color so
15014
08:52:13,440 --> 08:52:16,638
we can do that with fill fill equals
15015
08:52:16,638 --> 08:52:20,080
let's pick color blue
15016
08:52:20,080 --> 08:52:22,318
and now we have a blue line going across
15017
08:52:22,318 --> 08:52:23,120
the screen
15018
08:52:23,120 --> 08:52:25,120
but we can also change the width of this
15019
08:52:25,120 --> 08:52:26,638
as well so we can do that
15020
08:52:26,638 --> 08:52:28,558
using the width option and let's set
15021
08:52:28,558 --> 08:52:30,718
this to five
15022
08:52:30,718 --> 08:52:32,958
now we get an extra thick line across
15023
08:52:32,958 --> 08:52:34,240
the screen
15024
08:52:34,240 --> 08:52:37,600
now let's make a red line
15025
08:52:37,600 --> 08:52:40,120
so i'm just going to copy this
15026
08:52:40,120 --> 08:52:42,080
canvas.createline and let's change
15027
08:52:42,080 --> 08:52:45,200
some coordinates for this uh let's say
15028
08:52:45,200 --> 08:52:47,280
we want this to start in the bottom left
15029
08:52:47,280 --> 08:52:50,240
corner and go up to the top right
15030
08:52:50,240 --> 08:52:53,360
so the starting x position would be zero
15031
08:52:53,360 --> 08:52:56,558
y would actually be 500 x
15032
08:52:56,558 --> 08:52:58,798
for the ending position would be 500 and
15033
08:52:58,798 --> 08:53:00,318
y would be zero
15034
08:53:00,318 --> 08:53:03,440
let's set this to red and we'll keep the
15035
08:53:03,440 --> 08:53:06,160
width the same
15036
08:53:06,240 --> 08:53:08,638
okay so here's our red line and notice
15037
08:53:08,638 --> 08:53:10,000
that this red line
15038
08:53:10,000 --> 08:53:12,478
is overlapping the blue line so when you
15039
08:53:12,478 --> 08:53:13,520
create shapes
15040
08:53:13,520 --> 08:53:16,080
on a canvas if there's any that overlap
15041
08:53:16,080 --> 08:53:18,160
the one that you most recently created
15042
08:53:18,160 --> 08:53:20,558
will appear on the top one option
15043
08:53:20,558 --> 08:53:22,000
available to you is that you can
15044
08:53:22,000 --> 08:53:22,558
actually
15045
08:53:22,558 --> 08:53:25,120
give these graphics a unique name with
15046
08:53:25,120 --> 08:53:26,878
our blue line we could call this
15047
08:53:26,878 --> 08:53:30,638
blue line blue line equals all of this
15048
08:53:30,638 --> 08:53:32,398
and then we can call our red line
15049
08:53:32,398 --> 08:53:34,000
something different like
15050
08:53:34,000 --> 08:53:35,840
red line this would be useful if you
15051
08:53:35,840 --> 08:53:37,840
want to call or use a
15052
08:53:37,840 --> 08:53:40,080
graphic by its name if you want to like
15053
08:53:40,080 --> 08:53:41,840
move a graphic on the screen or have
15054
08:53:41,840 --> 08:53:42,718
something appear
15055
08:53:42,718 --> 08:53:44,958
you can use this by its name but we
15056
08:53:44,958 --> 08:53:46,558
don't really need it for this lesson so
15057
08:53:46,558 --> 08:53:48,000
i'll just revert this back to what we
15058
08:53:48,000 --> 08:53:49,040
had previously
15059
08:53:49,040 --> 08:53:50,240
i'm just letting you know that you can
15060
08:53:50,240 --> 08:53:52,398
do that alright what do we have up next
15061
08:53:52,398 --> 08:53:55,280
let's create a rectangle so canvas dot
15062
08:53:55,280 --> 08:53:56,878
create
15063
08:53:56,878 --> 08:53:59,520
rectangle we can set some starting
15064
08:53:59,520 --> 08:54:01,280
coordinates and ending coordinates
15065
08:54:01,280 --> 08:54:03,120
the starting coordinates are for the top
15066
08:54:03,120 --> 08:54:04,318
left of the rectangle
15067
08:54:04,318 --> 08:54:05,600
and the ending coordinates are for the
15068
08:54:05,600 --> 08:54:07,520
bottom right so for the starting
15069
08:54:07,520 --> 08:54:08,718
coordinates let's say
15070
08:54:08,718 --> 08:54:11,520
we're going to place this where x is 50
15071
08:54:11,520 --> 08:54:12,478
y is 50.
15072
08:54:12,478 --> 08:54:14,478
for the ending coordinates we want this
15073
08:54:14,478 --> 08:54:15,760
where
15074
08:54:15,760 --> 08:54:18,958
x is 250 and y is 250 i'm just making up
15075
08:54:18,958 --> 08:54:19,520
numbers
15076
08:54:19,520 --> 08:54:22,240
put it whatever you want really and this
15077
08:54:22,240 --> 08:54:24,798
is what this looks like
15078
08:54:24,798 --> 08:54:26,398
but i think i'm going to comment out
15079
08:54:26,398 --> 08:54:29,440
these lines for now
15080
08:54:29,920 --> 08:54:31,920
and here is our rectangle well i guess
15081
08:54:31,920 --> 08:54:33,120
it's also a square
15082
08:54:33,120 --> 08:54:35,040
we can actually give this a fill color
15083
08:54:35,040 --> 08:54:37,360
as well so we do that with the fill
15084
08:54:37,360 --> 08:54:38,000
option
15085
08:54:38,000 --> 08:54:41,120
fill equals purple
15086
08:54:41,120 --> 08:54:44,160
i guess and now we have a
15087
08:54:44,160 --> 08:54:47,600
purple rectangle next on our list
15088
08:54:47,600 --> 08:54:50,240
is create polygon we can create many
15089
08:54:50,240 --> 08:54:52,398
sorts of shapes using create polygon but
15090
08:54:52,398 --> 08:54:53,760
it really depends on how many
15091
08:54:53,760 --> 08:54:55,200
coordinates that we enter
15092
08:54:55,200 --> 08:54:57,280
let's create a simple triangle so we
15093
08:54:57,280 --> 08:54:59,200
need at least three coordinates for this
15094
08:54:59,200 --> 08:54:59,920
to work
15095
08:54:59,920 --> 08:55:02,958
so canvas dot create polygon
15096
08:55:02,958 --> 08:55:04,718
and i have some coordinates picked out
15097
08:55:04,718 --> 08:55:06,958
already so let's place the first set of
15098
08:55:06,958 --> 08:55:07,840
coordinates where
15099
08:55:07,840 --> 08:55:12,080
x is 250 y is zero for the second set
15100
08:55:12,080 --> 08:55:15,200
where x is 500 and y is 500
15101
08:55:15,200 --> 08:55:17,520
in the last set the third set we'll
15102
08:55:17,520 --> 08:55:18,638
place this where x
15103
08:55:18,638 --> 08:55:22,240
is zero and y is 500 and what this looks
15104
08:55:22,240 --> 08:55:22,638
like
15105
08:55:22,638 --> 08:55:25,360
is that we have this black triangle
15106
08:55:25,360 --> 08:55:27,440
that's taking up most of the canvas
15107
08:55:27,440 --> 08:55:29,360
we can also change the fill color as
15108
08:55:29,360 --> 08:55:31,040
well so we do that with
15109
08:55:31,040 --> 08:55:33,680
the option fill and we can set this to
15110
08:55:33,680 --> 08:55:35,200
whatever color you want
15111
08:55:35,200 --> 08:55:38,798
let's say yellow and we now have a
15112
08:55:38,798 --> 08:55:40,638
yellow triangle on our screen and it
15113
08:55:40,638 --> 08:55:42,638
kind of resembles the triforce from the
15114
08:55:42,638 --> 08:55:44,240
legend of zelda series one of my
15115
08:55:44,240 --> 08:55:45,440
personal favorites
15116
08:55:45,440 --> 08:55:48,240
let's also add a border this is actually
15117
08:55:48,240 --> 08:55:48,798
done with
15118
08:55:48,798 --> 08:55:51,360
outline outline equals and then pick a
15119
08:55:51,360 --> 08:55:52,398
color for the outline
15120
08:55:52,398 --> 08:55:54,958
let's say black and we have a black
15121
08:55:54,958 --> 08:55:56,000
outline around
15122
08:55:56,000 --> 08:55:58,080
this graphic and you can also change the
15123
08:55:58,080 --> 08:55:59,280
thickness of this too
15124
08:55:59,280 --> 08:56:01,920
with width and let's set the width to
15125
08:56:01,920 --> 08:56:02,798
five
15126
08:56:02,798 --> 08:56:04,958
and now we have an extra thick line
15127
08:56:04,958 --> 08:56:06,958
around our triangle
15128
08:56:06,958 --> 08:56:09,360
with these shapes you can pass in a list
15129
08:56:09,360 --> 08:56:10,558
of points too
15130
08:56:10,558 --> 08:56:12,718
so i'm going to get rid of all these and
15131
08:56:12,718 --> 08:56:14,318
we're going to pass in
15132
08:56:14,318 --> 08:56:16,798
a list so let's create a list called
15133
08:56:16,798 --> 08:56:17,520
points
15134
08:56:17,520 --> 08:56:20,000
points equals all the points that we
15135
08:56:20,000 --> 08:56:21,920
just entered in previously
15136
08:56:21,920 --> 08:56:24,318
and then instead of passing in all these
15137
08:56:24,318 --> 08:56:25,920
coordinates individually i'm just going
15138
08:56:25,920 --> 08:56:28,398
to pass in our list of points
15139
08:56:28,398 --> 08:56:31,600
and this will work exactly the same okay
15140
08:56:31,600 --> 08:56:34,080
let's create an arc now so canvas dot
15141
08:56:34,080 --> 08:56:34,878
create
15142
08:56:34,878 --> 08:56:37,920
arc and arc is really just a
15143
08:56:37,920 --> 08:56:40,080
curved line between at two points
15144
08:56:40,080 --> 08:56:42,160
however when we list some coordinates
15145
08:56:42,160 --> 08:56:44,080
we're not listing the coordinates of the
15146
08:56:44,080 --> 08:56:45,920
starting position and ending position
15147
08:56:45,920 --> 08:56:48,638
for this arc it's more or less for the
15148
08:56:48,638 --> 08:56:50,958
amount of space that we want to allocate
15149
08:56:50,958 --> 08:56:52,558
to actually draw this arc
15150
08:56:52,558 --> 08:56:54,398
so let's say that we want this arc to
15151
08:56:54,398 --> 08:56:56,718
take up the entire canvas we can pass in
15152
08:56:56,718 --> 08:56:57,520
the dimensions
15153
08:56:57,520 --> 08:56:59,680
for this canvas so the top left corner
15154
08:56:59,680 --> 08:57:00,798
is zero zero
15155
08:57:00,798 --> 08:57:02,478
and the height and the width are five
15156
08:57:02,478 --> 08:57:04,878
hundred
15157
08:57:07,440 --> 08:57:09,680
so this is our arc it resembles a pie
15158
08:57:09,680 --> 08:57:12,000
slice it's not starting in the top left
15159
08:57:12,000 --> 08:57:13,760
corner and ending down here
15160
08:57:13,760 --> 08:57:15,440
that's the amount of space that we're
15161
08:57:15,440 --> 08:57:17,760
giving this crate arc function to
15162
08:57:17,760 --> 08:57:18,638
actually draw
15163
08:57:18,638 --> 08:57:21,520
an arc it's really an entire circle but
15164
08:57:21,520 --> 08:57:23,360
only a portion of it is visible
15165
08:57:23,360 --> 08:57:25,520
let's also set a fill color for this so
15166
08:57:25,520 --> 08:57:26,798
fill equals
15167
08:57:26,798 --> 08:57:29,120
what color did we not pick yet let's say
15168
08:57:29,120 --> 08:57:31,440
green
15169
08:57:32,080 --> 08:57:35,040
and now we have a green pie slice
15170
08:57:35,040 --> 08:57:38,000
there's different styles you can do too
15171
08:57:38,000 --> 08:57:40,798
so we can change that with style equals
15172
08:57:40,798 --> 08:57:42,798
the default is pi slice
15173
08:57:42,798 --> 08:57:44,558
that's what we had previously let's
15174
08:57:44,558 --> 08:57:46,240
change this to chord
15175
08:57:46,240 --> 08:57:48,878
this will draw a line between these two
15176
08:57:48,878 --> 08:57:49,360
points
15177
08:57:49,360 --> 08:57:51,520
and this kind of resembles like a bow
15178
08:57:51,520 --> 08:57:52,958
like a ball with arrows
15179
08:57:52,958 --> 08:57:56,318
and there's also just arc
15180
08:57:56,318 --> 08:57:59,840
where it's simply just a curved line
15181
08:57:59,840 --> 08:58:02,318
let's change this back to pie slice well
15182
08:58:02,318 --> 08:58:04,160
because i like pie
15183
08:58:04,160 --> 08:58:06,478
we can change the starting position of
15184
08:58:06,478 --> 08:58:08,718
where this arc begins with the option
15185
08:58:08,718 --> 08:58:09,600
start
15186
08:58:09,600 --> 08:58:12,478
so start equals 0 by default so there's
15187
08:58:12,478 --> 08:58:14,638
going to be no apparent change
15188
08:58:14,638 --> 08:58:17,920
let's adjust this by 90 degrees so this
15189
08:58:17,920 --> 08:58:18,840
is in
15190
08:58:18,840 --> 08:58:22,318
degrees so this shifted
15191
08:58:22,318 --> 08:58:24,318
what is this direction counterclockwise
15192
08:58:24,318 --> 08:58:26,318
by 90 degrees
15193
08:58:26,318 --> 08:58:31,360
so 180 would theoretically flip this
15194
08:58:31,680 --> 08:58:33,280
and now this is on the other side of
15195
08:58:33,280 --> 08:58:34,878
what we had previously
15196
08:58:34,878 --> 08:58:38,160
and then 270.
15197
08:58:40,718 --> 08:58:44,000
all right we can also set an extent
15198
08:58:44,000 --> 08:58:46,160
so the default is normally 90 degrees
15199
08:58:46,160 --> 08:58:49,680
but let's change this to 180
15200
08:58:49,680 --> 08:58:51,840
and now we have a what is that
15201
08:58:51,840 --> 08:58:54,318
hemisphere half a circle
15202
08:58:54,318 --> 08:58:55,760
all right people let's practice by
15203
08:58:55,760 --> 08:58:57,520
creating a pokeball there's still one
15204
08:58:57,520 --> 08:58:59,280
more function that we have to cover it's
15205
08:58:59,280 --> 08:59:01,280
called create oval but we'll do that as
15206
08:59:01,280 --> 08:59:02,478
we're making this
15207
08:59:02,478 --> 08:59:04,798
so let's begin by creating the top red
15208
08:59:04,798 --> 08:59:06,798
hemisphere of this pokeball so that's
15209
08:59:06,798 --> 08:59:07,840
canvas
15210
08:59:07,840 --> 08:59:11,680
dot create arc and we're going to set
15211
08:59:11,680 --> 08:59:13,360
the extent of this
15212
08:59:13,360 --> 08:59:16,798
to 0 0 500 500
15213
08:59:16,798 --> 08:59:18,478
so that this is going to take up the
15214
08:59:18,478 --> 08:59:20,000
entire canvas
15215
08:59:20,000 --> 08:59:22,558
and then let's also set a fill color to
15216
08:59:22,558 --> 08:59:23,200
red
15217
08:59:23,200 --> 08:59:26,318
so fill equals red
15218
08:59:26,318 --> 08:59:28,478
we need to change the extent of how far
15219
08:59:28,478 --> 08:59:29,440
this extends
15220
08:59:29,440 --> 08:59:31,040
so we'll do that with the extent
15221
08:59:31,040 --> 08:59:32,638
function so extent
15222
08:59:32,638 --> 08:59:36,398
equals 180 so that this is a half circle
15223
08:59:36,398 --> 08:59:39,040
and let's also change the thickness of
15224
08:59:39,040 --> 08:59:40,240
this outline as well
15225
08:59:40,240 --> 08:59:44,080
using the width option so width equals
15226
08:59:44,080 --> 08:59:47,200
10 would be good and we have
15227
08:59:47,200 --> 08:59:49,120
a red hemisphere for the top of our
15228
08:59:49,120 --> 08:59:51,520
pokeball let's work on the bottom half
15229
08:59:51,520 --> 08:59:54,638
so that is a white hemisphere
15230
08:59:54,638 --> 08:59:57,040
so canvas dot create arc these are all
15231
08:59:57,040 --> 08:59:58,080
the same we'll change
15232
08:59:58,080 --> 09:00:01,520
the fill to white the extent will be the
15233
09:00:01,520 --> 09:00:02,000
same
15234
09:00:02,000 --> 09:00:03,680
but we need a different starting
15235
09:00:03,680 --> 09:00:05,200
position so start
15236
09:00:05,200 --> 09:00:08,718
equals 180. so this is going to begin
15237
09:00:08,718 --> 09:00:11,200
at the 180 degree mark so when you
15238
09:00:11,200 --> 09:00:13,120
combine these both together we get
15239
09:00:13,120 --> 09:00:14,958
two different colored hemispheres that
15240
09:00:14,958 --> 09:00:16,798
form a perfect circle
15241
09:00:16,798 --> 09:00:18,718
now let's create an oval within the
15242
09:00:18,718 --> 09:00:21,520
center of this pokeball
15243
09:00:21,520 --> 09:00:24,000
so we're going to do that with canvas
15244
09:00:24,000 --> 09:00:26,000
dot create
15245
09:00:26,000 --> 09:00:29,120
oval we're going to set the amount of
15246
09:00:29,120 --> 09:00:30,638
space that we want to give
15247
09:00:30,638 --> 09:00:33,040
the circle to be drawn so we'll place
15248
09:00:33,040 --> 09:00:33,680
this where
15249
09:00:33,680 --> 09:00:37,360
x is 190 where y is 190
15250
09:00:37,360 --> 09:00:40,798
where x 2 or the second x position is
15251
09:00:40,798 --> 09:00:41,760
310
15252
09:00:41,760 --> 09:00:45,440
and y2 is 310
15253
09:00:46,478 --> 09:00:48,878
let's set the fill color next so we do
15254
09:00:48,878 --> 09:00:49,760
that with fill
15255
09:00:49,760 --> 09:00:53,360
fill equals white
15256
09:00:53,600 --> 09:00:55,440
and lastly let's just give this a
15257
09:00:55,440 --> 09:00:56,638
thicker border
15258
09:00:56,638 --> 09:01:00,080
so that is width
15259
09:01:00,080 --> 09:01:03,520
equals 10 to match the width for the
15260
09:01:03,520 --> 09:01:06,080
two arcs that we have and there you have
15261
09:01:06,080 --> 09:01:07,440
it we have a simple
15262
09:01:07,440 --> 09:01:09,680
pokeball all right well that's the
15263
09:01:09,680 --> 09:01:11,600
basics of the canvas widget if you'd
15264
09:01:11,600 --> 09:01:13,440
like a copy of all this code i'll post
15265
09:01:13,440 --> 09:01:15,200
all of this in the comments down below
15266
09:01:15,200 --> 09:01:17,120
but yeah that's the basics of the canvas
15267
09:01:17,120 --> 09:01:18,958
widget in python
15268
09:01:18,958 --> 09:01:21,040
what's going on everybody it's your bro
15269
09:01:21,040 --> 09:01:22,638
here hope you're doing well and in this
15270
09:01:22,638 --> 09:01:24,160
video i'm going to teach you guys how we
15271
09:01:24,160 --> 09:01:26,558
can create key events in python so sit
15272
09:01:26,558 --> 09:01:30,240
back relax and enjoy the show
15273
09:01:30,638 --> 09:01:33,360
welcome back everyone in this video
15274
09:01:33,360 --> 09:01:34,878
we're going to be discussing at key
15275
09:01:34,878 --> 09:01:35,520
events
15276
09:01:35,520 --> 09:01:38,878
we can bind a key event and a function
15277
09:01:38,878 --> 09:01:41,520
to a widget or a window so that when we
15278
09:01:41,520 --> 09:01:43,040
press a certain key
15279
09:01:43,040 --> 09:01:45,440
or do something we can trigger a
15280
09:01:45,440 --> 09:01:47,280
function to be called that will perform
15281
09:01:47,280 --> 09:01:48,878
some sort of task for us
15282
09:01:48,878 --> 09:01:51,120
so we can actually do this by using the
15283
09:01:51,120 --> 09:01:52,638
bind function
15284
09:01:52,638 --> 09:01:55,840
so widgets and windows have access to a
15285
09:01:55,840 --> 09:01:57,120
bind function
15286
09:01:57,120 --> 09:01:59,600
so when our window or our widget is
15287
09:01:59,600 --> 09:02:00,240
selected
15288
09:02:00,240 --> 09:02:02,160
and then you press a certain key or do
15289
09:02:02,160 --> 09:02:04,080
something then it's going to call a
15290
09:02:04,080 --> 09:02:06,240
certain function to perform a task for
15291
09:02:06,240 --> 09:02:06,798
us
15292
09:02:06,798 --> 09:02:08,638
so there's two arguments within the bind
15293
09:02:08,638 --> 09:02:10,718
function this takes an event
15294
09:02:10,718 --> 09:02:13,680
as well as a function name let's say
15295
09:02:13,680 --> 09:02:15,760
that when we press the enter key we want
15296
09:02:15,760 --> 09:02:16,080
to
15297
09:02:16,080 --> 09:02:18,878
call this function to do something so
15298
09:02:18,878 --> 09:02:20,160
for this event
15299
09:02:20,160 --> 09:02:23,360
what we type for that is a set of quotes
15300
09:02:23,360 --> 09:02:25,840
angle brackets and the name of the
15301
09:02:25,840 --> 09:02:26,558
button
15302
09:02:26,558 --> 09:02:28,958
so for example if you want to press w to
15303
09:02:28,958 --> 09:02:30,558
move forward like you're playing a game
15304
09:02:30,558 --> 09:02:32,000
this would be w
15305
09:02:32,000 --> 09:02:35,520
a s d there's also up
15306
09:02:35,520 --> 09:02:38,558
down and then let's say we want to press
15307
09:02:38,558 --> 09:02:40,478
enter to do something that is actually a
15308
09:02:40,478 --> 09:02:42,478
return and then we will call
15309
09:02:42,478 --> 09:02:44,318
this function so let's create a function
15310
09:02:44,318 --> 09:02:46,398
called do something
15311
09:02:46,398 --> 09:02:50,398
and let's define this at the top def
15312
09:02:50,398 --> 09:02:53,440
do something now pay attention to this
15313
09:02:53,440 --> 09:02:54,718
part we need to set up
15314
09:02:54,718 --> 09:02:56,878
one parameter for this do something
15315
09:02:56,878 --> 09:02:58,000
function
15316
09:02:58,000 --> 09:03:00,080
this needs an event so be sure to
15317
09:03:00,080 --> 09:03:02,080
include that and it's easy to forget
15318
09:03:02,080 --> 09:03:04,958
so what do we want to do after we press
15319
09:03:04,958 --> 09:03:05,680
enter
15320
09:03:05,680 --> 09:03:09,840
let's just print a message you
15321
09:03:09,840 --> 09:03:12,878
did a thing and
15322
09:03:12,878 --> 09:03:16,798
this should work now so let's try it
15323
09:03:17,040 --> 09:03:20,318
so while this window has focus ability
15324
09:03:20,318 --> 09:03:22,718
we can actually uh have this respond to
15325
09:03:22,718 --> 09:03:23,360
events
15326
09:03:23,360 --> 09:03:24,878
so right now i'm pressing all sorts of
15327
09:03:24,878 --> 09:03:26,478
keys you can probably hear me right now
15328
09:03:26,478 --> 09:03:28,318
going crazy on my keyboard but now i'm
15329
09:03:28,318 --> 09:03:29,040
going to hit
15330
09:03:29,040 --> 09:03:32,000
enter and it says you did a thing and
15331
09:03:32,000 --> 09:03:33,920
you can bind all sorts of keys to
15332
09:03:33,920 --> 09:03:35,680
this window let's say that i want to
15333
09:03:35,680 --> 09:03:38,000
press q to quit
15334
09:03:38,000 --> 09:03:39,920
so we'll just replace this with
15335
09:03:39,920 --> 09:03:42,558
lowercase q
15336
09:03:42,558 --> 09:03:44,878
so i'm typing all sorts of keys but now
15337
09:03:44,878 --> 09:03:45,920
i'm going to press q
15338
09:03:45,920 --> 09:03:48,160
and it says you did a thing kind of like
15339
09:03:48,160 --> 09:03:49,440
we're quitting the game
15340
09:03:49,440 --> 09:03:51,920
you can actually have this respond to
15341
09:03:51,920 --> 09:03:52,638
all keys
15342
09:03:52,638 --> 09:03:55,200
well almost all of them so to do that
15343
09:03:55,200 --> 09:03:58,718
you just type in key with the capital k
15344
09:03:58,718 --> 09:04:01,120
so i can pretty much press on any key
15345
09:04:01,120 --> 09:04:02,000
then it's going to
15346
09:04:02,000 --> 09:04:05,680
trigger or call our function
15347
09:04:05,680 --> 09:04:08,878
kind of like that so one thing that we
15348
09:04:08,878 --> 09:04:09,440
could do
15349
09:04:09,440 --> 09:04:11,600
we can display the key event that was
15350
09:04:11,600 --> 09:04:13,120
pressed so i'm just going to add that
15351
09:04:13,120 --> 09:04:14,478
here
15352
09:04:14,478 --> 09:04:18,000
maybe i'll change this message u
15353
09:04:18,000 --> 09:04:21,760
pressed plus and to display
15354
09:04:21,760 --> 09:04:24,798
what key was pressed that is event
15355
09:04:24,798 --> 09:04:28,398
dot key sim i believe that's for
15356
09:04:28,398 --> 09:04:31,360
key symbol so this will actually display
15357
09:04:31,360 --> 09:04:32,798
what button you pressed
15358
09:04:32,798 --> 09:04:36,638
so w-a-s-d
15359
09:04:36,638 --> 09:04:39,200
here's a unique idea for practice let's
15360
09:04:39,200 --> 09:04:40,398
say that we want to add
15361
09:04:40,398 --> 09:04:42,638
a label to our window and we're going to
15362
09:04:42,638 --> 09:04:44,638
change the text on the label
15363
09:04:44,638 --> 09:04:47,200
to reflect whatever key that we press so
15364
09:04:47,200 --> 09:04:48,398
let's create a label
15365
09:04:48,398 --> 09:04:50,878
label equals label we're adding this to
15366
09:04:50,878 --> 09:04:52,558
our window
15367
09:04:52,558 --> 09:04:54,318
and let's change the font as well to
15368
09:04:54,318 --> 09:04:55,840
something massive
15369
09:04:55,840 --> 09:04:59,280
like helvetica
15370
09:04:59,280 --> 09:05:02,240
and a size of 100 that should be good
15371
09:05:02,240 --> 09:05:04,240
then we need to pack this label so
15372
09:05:04,240 --> 09:05:07,600
label dot pack then within
15373
09:05:07,600 --> 09:05:09,600
this function let's turn this into a
15374
09:05:09,600 --> 09:05:10,718
comment
15375
09:05:10,718 --> 09:05:14,160
and we're going to use label dot config
15376
09:05:14,160 --> 09:05:17,280
and we're going to update our text
15377
09:05:17,280 --> 09:05:19,280
and the text is going to be whatever key
15378
09:05:19,280 --> 09:05:21,680
that we press so we can do that with
15379
09:05:21,680 --> 09:05:25,360
event dot key sim so now
15380
09:05:25,360 --> 09:05:28,160
this label is going to display whatever
15381
09:05:28,160 --> 09:05:29,360
key that we press
15382
09:05:29,360 --> 09:05:32,798
so for example w a s
15383
09:05:32,798 --> 09:05:36,080
d backspace escape
15384
09:05:36,080 --> 09:05:38,878
up up down down left right left right b
15385
09:05:38,878 --> 09:05:39,520
a start
15386
09:05:39,520 --> 09:05:41,760
all right so that's the basics of key
15387
09:05:41,760 --> 09:05:43,680
events if you could do me a favor and in
15388
09:05:43,680 --> 09:05:44,878
the comments down below
15389
09:05:44,878 --> 09:05:47,680
just press f to pay respects so if you
15390
09:05:47,680 --> 09:05:49,360
want a copy of this code i'll post this
15391
09:05:49,360 --> 09:05:50,878
in the comments down below but yeah
15392
09:05:50,878 --> 09:05:53,040
that's the basics of key events in
15393
09:05:53,040 --> 09:05:54,160
python
15394
09:05:54,160 --> 09:05:55,680
hey what's going on everybody it's your
15395
09:05:55,680 --> 09:05:57,360
bro here hope you're doing well and in
15396
09:05:57,360 --> 09:05:58,718
this video i'm going to teach you guys
15397
09:05:58,718 --> 09:05:59,200
all about
15398
09:05:59,200 --> 09:06:01,760
mouse events in python so sit back relax
15399
09:06:01,760 --> 09:06:04,638
and enjoy the show
15400
09:06:05,040 --> 09:06:06,318
hey everyone we're going to be
15401
09:06:06,318 --> 09:06:08,398
discussing mouse events in python so
15402
09:06:08,398 --> 09:06:10,557
before we begin we'll need a window so
15403
09:06:10,557 --> 09:06:13,519
window equals tk followed by window.main
15404
09:06:13,519 --> 09:06:14,237
loop
15405
09:06:14,237 --> 09:06:16,398
and in the last video we learned that we
15406
09:06:16,398 --> 09:06:17,519
can bind
15407
09:06:17,519 --> 09:06:19,917
a widget or a window so that when a
15408
09:06:19,917 --> 09:06:21,839
certain event occurs we can call a
15409
09:06:21,839 --> 09:06:22,480
function
15410
09:06:22,480 --> 09:06:24,160
so we're going to do the same thing for
15411
09:06:24,160 --> 09:06:25,839
this video but we have
15412
09:06:25,839 --> 09:06:28,398
various mouse events that we can cover
15413
09:06:28,398 --> 09:06:31,122
so we're going to use the window.bind
15414
09:06:31,122 --> 09:06:31,917
function
15415
09:06:31,917 --> 09:06:34,877
and this takes two arguments an event as
15416
09:06:34,877 --> 09:06:36,237
well as a function
15417
09:06:36,237 --> 09:06:38,640
name so let's define a function let's
15418
09:06:38,640 --> 09:06:39,278
call it the
15419
09:06:39,278 --> 09:06:42,398
do something function so do something
15420
09:06:42,398 --> 09:06:44,000
and we'll need to define this so at the
15421
09:06:44,000 --> 09:06:46,398
top def
15422
09:06:46,398 --> 09:06:49,199
do something and remember that we need
15423
09:06:49,199 --> 09:06:49,839
to set up
15424
09:06:49,839 --> 09:06:52,557
one parameter for this function the
15425
09:06:52,557 --> 09:06:53,199
parameter
15426
09:06:53,199 --> 09:06:55,680
is an event and then we'll just print
15427
09:06:55,680 --> 09:06:57,680
something print
15428
09:06:57,680 --> 09:07:01,438
you did a thing
15429
09:07:01,438 --> 09:07:04,320
okay so there are various mouse events
15430
09:07:04,320 --> 09:07:04,877
for a
15431
09:07:04,877 --> 09:07:08,000
left button click within quotes and then
15432
09:07:08,000 --> 09:07:09,360
within angle brackets
15433
09:07:09,360 --> 09:07:13,278
a left button click is button dash one
15434
09:07:13,278 --> 09:07:16,160
so then if we were to click within this
15435
09:07:16,160 --> 09:07:16,960
window
15436
09:07:16,960 --> 09:07:19,360
it's going to call our function you did
15437
09:07:19,360 --> 09:07:20,160
a thing
15438
09:07:20,160 --> 09:07:22,000
but if i were to right click it doesn't
15439
09:07:22,000 --> 09:07:24,237
do anything
15440
09:07:24,237 --> 09:07:26,800
so there are other mouse events too so
15441
09:07:26,800 --> 09:07:27,598
button two
15442
09:07:27,598 --> 09:07:29,917
is that scroll wheel not if you scroll
15443
09:07:29,917 --> 09:07:31,519
up or down but if you press
15444
09:07:31,519 --> 09:07:34,237
in on it so right now i'm clicking left
15445
09:07:34,237 --> 09:07:35,438
i'm clicking right
15446
09:07:35,438 --> 09:07:37,199
now i'm going to press in on the mouse
15447
09:07:37,199 --> 09:07:40,398
wheel and it says you did a thing
15448
09:07:40,398 --> 09:07:44,800
button three is a right mouse click
15449
09:07:46,877 --> 09:07:49,278
so i'm clicking left nothing but i'm
15450
09:07:49,278 --> 09:07:51,598
gonna click right
15451
09:07:51,598 --> 09:07:53,598
and that seems to work all right so
15452
09:07:53,598 --> 09:07:56,480
button one i'm just going to add a note
15453
09:07:56,480 --> 09:08:00,960
left mouse click button two
15454
09:08:00,960 --> 09:08:04,877
is the scroll wheel
15455
09:08:04,877 --> 09:08:07,122
that's if you press in on it and then
15456
09:08:07,122 --> 09:08:08,000
button three
15457
09:08:08,000 --> 09:08:11,438
is a right mouse click
15458
09:08:11,438 --> 09:08:13,758
so did you guys know we can actually get
15459
09:08:13,758 --> 09:08:15,839
the x and y coordinates of where this
15460
09:08:15,839 --> 09:08:16,640
event occurred
15461
09:08:16,640 --> 09:08:19,199
so to do that we type in the name of the
15462
09:08:19,199 --> 09:08:20,160
event
15463
09:08:20,160 --> 09:08:23,040
dot x however since we're displaying
15464
09:08:23,040 --> 09:08:24,320
this along with some text
15465
09:08:24,320 --> 09:08:28,160
we'll need to convert this to a string
15466
09:08:28,160 --> 09:08:31,598
so string event dot x then i'll add the
15467
09:08:31,598 --> 09:08:34,718
y coordinates as well
15468
09:08:35,557 --> 09:08:38,800
event.y i think i'll just add a comma
15469
09:08:38,800 --> 09:08:39,519
between these
15470
09:08:39,519 --> 09:08:42,960
to separate them and we'll change our
15471
09:08:42,960 --> 09:08:44,480
text
15472
09:08:44,480 --> 09:08:48,960
so we'll say mouse coordinates
15473
09:08:48,960 --> 09:08:51,199
all right let's try this again so this
15474
09:08:51,199 --> 09:08:52,557
is going to
15475
09:08:52,557 --> 09:08:54,160
give us the coordinates of where we
15476
09:08:54,160 --> 09:08:55,598
click and right now i'm just left
15477
09:08:55,598 --> 09:08:58,078
clicking
15478
09:08:58,960 --> 09:09:02,398
so the top left corner is zero zero
15479
09:09:02,398 --> 09:09:04,640
well at least close to it not there
15480
09:09:04,640 --> 09:09:06,557
exactly
15481
09:09:06,557 --> 09:09:08,398
alright so here's a few other events
15482
09:09:08,398 --> 09:09:11,040
that we can cover
15483
09:09:11,680 --> 09:09:14,480
i'm going to copy this we have button
15484
09:09:14,480 --> 09:09:15,917
release
15485
09:09:15,917 --> 09:09:18,398
if we were to release a button it's
15486
09:09:18,398 --> 09:09:21,040
going to trigger our event
15487
09:09:21,040 --> 09:09:23,360
so right now i'm holding in on the left
15488
09:09:23,360 --> 09:09:25,519
mouse button but i'm going to let go
15489
09:09:25,519 --> 09:09:28,078
then it triggers our event if i were to
15490
09:09:28,078 --> 09:09:29,122
right click
15491
09:09:29,122 --> 09:09:31,199
and hold it it doesn't do anything but
15492
09:09:31,199 --> 09:09:32,557
as soon as i let go
15493
09:09:32,557 --> 09:09:35,519
then our event is triggered we also have
15494
09:09:35,519 --> 09:09:36,320
enter
15495
09:09:36,320 --> 09:09:39,438
not to be confused with the enter button
15496
09:09:39,438 --> 09:09:42,557
just going to get rid of that so that
15497
09:09:42,557 --> 09:09:46,398
is enter if we were to enter our binded
15498
09:09:46,398 --> 09:09:49,438
window or widget
15499
09:09:49,839 --> 09:09:51,758
so if i were to enter it's going to give
15500
09:09:51,758 --> 09:09:55,598
the coordinates of where i entered this
15501
09:09:56,078 --> 09:09:59,200
there is also leave
15502
09:09:59,680 --> 09:10:03,438
so i'm going to say that enter
15503
09:10:03,438 --> 09:10:07,040
the window
15504
09:10:07,758 --> 09:10:14,877
and leave is leave the window
15505
09:10:14,877 --> 09:10:16,800
so if i enter nothing happens but as
15506
09:10:16,800 --> 09:10:20,800
soon as i leave it gives the coordinates
15507
09:10:22,160 --> 09:10:24,960
and the last one is motion as long as
15508
09:10:24,960 --> 09:10:28,640
the cursor is in motion
15509
09:10:28,640 --> 09:10:31,438
so this one might be good for a game so
15510
09:10:31,438 --> 09:10:34,320
that is motion
15511
09:10:34,960 --> 09:10:39,360
where the mouse
15512
09:10:40,839 --> 09:10:42,078
moved
15513
09:10:42,078 --> 09:10:43,360
it's consistently giving me the
15514
09:10:43,360 --> 09:10:45,122
coordinates if i'm moving the mouse but
15515
09:10:45,122 --> 09:10:45,917
if i were
15516
09:10:45,917 --> 09:10:49,999
stationary or stop moving it stops
15517
09:10:50,160 --> 09:10:52,237
all right so that's the basics of mouse
15518
09:10:52,237 --> 09:10:54,078
events in python if you want to copy of
15519
09:10:54,078 --> 09:10:54,877
all this code
15520
09:10:54,877 --> 09:10:56,237
i'll post all of this in the comments
15521
09:10:56,237 --> 09:10:58,800
down below but yeah that's the basics of
15522
09:10:58,800 --> 09:11:01,839
mouse events in python hey what's going
15523
09:11:01,839 --> 09:11:03,598
on everybody it's bro here hope you're
15524
09:11:03,598 --> 09:11:05,122
doing well and in this video i'm going
15525
09:11:05,122 --> 09:11:06,718
to teach you guys how we can drag
15526
09:11:06,718 --> 09:11:09,199
and drop widgets in python so sit back
15527
09:11:09,199 --> 09:11:12,479
relax and enjoy the show
15528
09:11:12,960 --> 09:11:15,040
all right people let's just dive into
15529
09:11:15,040 --> 09:11:16,800
this so we'll need a window and then
15530
09:11:16,800 --> 09:11:18,480
we're going to be adding widgets to the
15531
09:11:18,480 --> 09:11:19,122
window
15532
09:11:19,122 --> 09:11:21,718
so window equals tk followed by
15533
09:11:21,718 --> 09:11:23,438
window.main loop
15534
09:11:23,438 --> 09:11:25,278
now you can drag and drop all sorts of
15535
09:11:25,278 --> 09:11:26,877
widgets but let's just stick with the
15536
09:11:26,877 --> 09:11:29,519
label because creating a label is simple
15537
09:11:29,519 --> 09:11:32,877
so let's define a label label equals
15538
09:11:32,877 --> 09:11:35,122
label we're going to be adding this
15539
09:11:35,122 --> 09:11:36,718
label to the window
15540
09:11:36,718 --> 09:11:38,557
and let's set a background color so we
15541
09:11:38,557 --> 09:11:40,237
can actually see this label
15542
09:11:40,237 --> 09:11:42,000
pick i don't know your favorite color
15543
09:11:42,000 --> 09:11:43,360
i'm going to pick red
15544
09:11:43,360 --> 09:11:45,598
and then i'm going to set a width and a
15545
09:11:45,598 --> 09:11:47,598
height because normally if you have a
15546
09:11:47,598 --> 09:11:50,078
label without any text it's really small
15547
09:11:50,078 --> 09:11:53,917
so let's set a width of maybe 10
15548
09:11:53,917 --> 09:11:57,122
and then a height of 5.
15549
09:11:57,122 --> 09:11:59,360
that should be good and we want to place
15550
09:11:59,360 --> 09:12:00,160
this label
15551
09:12:00,160 --> 09:12:03,199
someplace within the window so label dot
15552
09:12:03,199 --> 09:12:05,278
place and we can pass in some x
15553
09:12:05,278 --> 09:12:07,122
coordinates as well as y coordinates
15554
09:12:07,122 --> 09:12:11,680
so x equals zero and y equals zero
15555
09:12:11,680 --> 09:12:13,519
so that this is going to appear in the
15556
09:12:13,519 --> 09:12:14,877
top left corner
15557
09:12:14,877 --> 09:12:17,519
of our window so this is what we have
15558
09:12:17,519 --> 09:12:18,000
this
15559
09:12:18,000 --> 09:12:20,000
is our label and we want to be able to
15560
09:12:20,000 --> 09:12:21,199
drag and drop
15561
09:12:21,199 --> 09:12:23,519
this label someplace within the window
15562
09:12:23,519 --> 09:12:24,718
what we're going to need to do
15563
09:12:24,718 --> 09:12:27,519
is actually bind this label twice but
15564
09:12:27,519 --> 09:12:28,800
let's begin with just
15565
09:12:28,800 --> 09:12:31,438
the first binding so in order to bind a
15566
09:12:31,438 --> 09:12:32,960
widget you type in the name of the
15567
09:12:32,960 --> 09:12:33,680
widget
15568
09:12:33,680 --> 09:12:36,960
followed by calling the bind function
15569
09:12:36,960 --> 09:12:39,278
so this bind function can take two
15570
09:12:39,278 --> 09:12:40,078
arguments
15571
09:12:40,078 --> 09:12:42,480
it can take an event as well as a
15572
09:12:42,480 --> 09:12:43,199
function
15573
09:12:43,199 --> 09:12:45,758
name so for the function name let's call
15574
09:12:45,758 --> 09:12:46,480
this
15575
09:12:46,480 --> 09:12:48,800
drag underscore start but you don't
15576
09:12:48,800 --> 09:12:49,917
really need to
15577
09:12:49,917 --> 09:12:52,160
name it the same that i do and for the
15578
09:12:52,160 --> 09:12:53,199
event that occurs
15579
09:12:53,199 --> 09:12:55,917
this is going to be a left button click
15580
09:12:55,917 --> 09:12:56,800
with the mouse
15581
09:12:56,800 --> 09:12:59,438
so that event is within quotes and
15582
09:12:59,438 --> 09:13:01,199
within angle brackets
15583
09:13:01,199 --> 09:13:04,640
button dash one so if we were to click
15584
09:13:04,640 --> 09:13:06,640
someplace within the label using the
15585
09:13:06,640 --> 09:13:07,758
left mouse button
15586
09:13:07,758 --> 09:13:10,000
it's going to call our drag start
15587
09:13:10,000 --> 09:13:12,078
function but we need to define this
15588
09:13:12,078 --> 09:13:13,839
so at the top of our program let's
15589
09:13:13,839 --> 09:13:16,398
define this function
15590
09:13:16,398 --> 09:13:19,360
def drag start and this takes one
15591
09:13:19,360 --> 09:13:20,160
parameter
15592
09:13:20,160 --> 09:13:22,800
the parameter we have is our event so
15593
09:13:22,800 --> 09:13:23,917
what we're going to do
15594
09:13:23,917 --> 09:13:26,398
is actually get the coordinates of where
15595
09:13:26,398 --> 09:13:27,040
we click
15596
09:13:27,040 --> 09:13:29,360
within this label and we're going to
15597
09:13:29,360 --> 09:13:30,237
assign these
15598
09:13:30,237 --> 09:13:33,360
to a new variable or attribute of our
15599
09:13:33,360 --> 09:13:34,160
label
15600
09:13:34,160 --> 09:13:37,360
so label dot let's create an attribute
15601
09:13:37,360 --> 09:13:37,758
called
15602
09:13:37,758 --> 09:13:41,438
start x equals our event
15603
09:13:41,438 --> 09:13:44,718
dot x so this is where we click
15604
09:13:44,718 --> 09:13:47,199
within the label and not necessarily the
15605
09:13:47,199 --> 09:13:48,160
window
15606
09:13:48,160 --> 09:13:50,718
and let's repeat the process for y so
15607
09:13:50,718 --> 09:13:51,278
label
15608
09:13:51,278 --> 09:13:54,877
dot start y equals event dot
15609
09:13:54,877 --> 09:13:57,199
y so we're going to have some
15610
09:13:57,199 --> 09:13:58,877
coordinates of where we click
15611
09:13:58,877 --> 09:14:01,278
within this label and we can actually
15612
09:14:01,278 --> 09:14:02,877
use that for our next
15613
09:14:02,877 --> 09:14:04,877
function so we're going to bind this
15614
09:14:04,877 --> 09:14:06,237
label a second time
15615
09:14:06,237 --> 09:14:08,877
so i'm going to copy this line and paste
15616
09:14:08,877 --> 09:14:09,519
it
15617
09:14:09,519 --> 09:14:12,800
the second binding let's say is
15618
09:14:12,800 --> 09:14:15,680
drag underscore motion for the function
15619
09:14:15,680 --> 09:14:16,640
name
15620
09:14:16,640 --> 09:14:19,519
and this event is going to occur if we
15621
09:14:19,519 --> 09:14:21,758
were to hold down the left mouse button
15622
09:14:21,758 --> 09:14:25,040
and then drag so that is b
15623
09:14:25,040 --> 09:14:28,398
one dash motion
15624
09:14:28,398 --> 09:14:30,640
and then we need to define this function
15625
09:14:30,640 --> 09:14:32,320
so at the top
15626
09:14:32,320 --> 09:14:35,598
d e f drag underscore motion and this
15627
09:14:35,598 --> 09:14:37,758
takes our event as a parameter
15628
09:14:37,758 --> 09:14:39,680
so this part is going to be a little
15629
09:14:39,680 --> 09:14:41,122
funky we're going to
15630
09:14:41,122 --> 09:14:44,160
get or create new x and y
15631
09:14:44,160 --> 09:14:47,360
coordinates so let's begin with x x
15632
09:14:47,360 --> 09:14:50,877
equals the name of our label or widget
15633
09:14:50,877 --> 09:14:54,000
label dot w info
15634
09:14:54,000 --> 09:14:56,557
underscore x this is a function so add
15635
09:14:56,557 --> 09:14:58,000
some parentheses
15636
09:14:58,000 --> 09:15:01,758
this will get the top left x coordinate
15637
09:15:01,758 --> 09:15:04,078
of our label relative to the window that
15638
09:15:04,078 --> 09:15:05,598
we're in
15639
09:15:05,598 --> 09:15:11,199
minus label x well labeled.startx
15640
09:15:11,199 --> 09:15:14,960
plus event dot x
15641
09:15:14,960 --> 09:15:17,438
all right so we're going to do the same
15642
09:15:17,438 --> 09:15:19,598
thing for y
15643
09:15:19,598 --> 09:15:21,758
well we're going to replace x with y for
15644
09:15:21,758 --> 09:15:24,398
the second line
15645
09:15:25,917 --> 09:15:29,598
all right so this is the top
15646
09:15:29,598 --> 09:15:32,718
left corner the x coordinate of our
15647
09:15:32,718 --> 09:15:33,519
widget
15648
09:15:33,519 --> 09:15:37,199
relative to the window this
15649
09:15:37,199 --> 09:15:40,237
is the place where we click within
15650
09:15:40,237 --> 09:15:43,519
the label itself and this is where we
15651
09:15:43,519 --> 09:15:44,078
begin
15652
09:15:44,078 --> 09:15:47,438
dragging our widget to it's a little
15653
09:15:47,438 --> 09:15:48,718
difficult to explain but if you can find
15654
09:15:48,718 --> 09:15:49,519
combine
15655
09:15:49,519 --> 09:15:51,438
combine all of these together you'll get
15656
09:15:51,438 --> 09:15:52,877
the new coordinates
15657
09:15:52,877 --> 09:15:56,160
of where you want to drag this component
15658
09:15:56,160 --> 09:15:57,278
this widget to
15659
09:15:57,278 --> 09:16:00,557
so we need to replace this widget so
15660
09:16:00,557 --> 09:16:04,640
label dot place x equals x
15661
09:16:04,640 --> 09:16:07,917
and y equals y so then when you combine
15662
09:16:07,917 --> 09:16:09,199
all of these together
15663
09:16:09,199 --> 09:16:11,438
we have our widget and we can drag it
15664
09:16:11,438 --> 09:16:13,519
and drop it someplace else within the
15665
09:16:13,519 --> 09:16:15,122
window
15666
09:16:15,122 --> 09:16:17,438
now wait a second what if we have more
15667
09:16:17,438 --> 09:16:18,800
than one widget
15668
09:16:18,800 --> 09:16:20,718
well with the way that our functions are
15669
09:16:20,718 --> 09:16:22,960
written now they wouldn't be compatible
15670
09:16:22,960 --> 09:16:24,960
with more than one widget
15671
09:16:24,960 --> 09:16:27,598
just this label itself so what if we
15672
09:16:27,598 --> 09:16:28,640
were to create a
15673
09:16:28,640 --> 09:16:30,877
second label so i'm going to call the
15674
09:16:30,877 --> 09:16:32,078
second label just
15675
09:16:32,078 --> 09:16:34,237
label two so i'm just going to rename
15676
09:16:34,237 --> 09:16:35,122
them and
15677
09:16:35,122 --> 09:16:37,122
let's change the background color to
15678
09:16:37,122 --> 09:16:38,320
maybe
15679
09:16:38,320 --> 09:16:40,000
blue and i'm just going to place this
15680
09:16:40,000 --> 09:16:41,839
somewhere else within our window so
15681
09:16:41,839 --> 09:16:42,718
maybe where x
15682
09:16:42,718 --> 09:16:45,839
is 100 and y is 100 and we'll need to
15683
09:16:45,839 --> 09:16:46,320
bind
15684
09:16:46,320 --> 09:16:49,758
label two as well so
15685
09:16:49,758 --> 09:16:53,199
make sure to bind label two so we have
15686
09:16:53,199 --> 09:16:55,278
label one binded it's just called label
15687
09:16:55,278 --> 09:16:57,199
and the same thing with label two
15688
09:16:57,199 --> 09:16:59,360
all right let's see what happens i'm
15689
09:16:59,360 --> 09:17:01,438
going to attempt to drag
15690
09:17:01,438 --> 09:17:04,398
label 2. the label 1 is moving that's
15691
09:17:04,398 --> 09:17:04,960
because
15692
09:17:04,960 --> 09:17:07,519
with our functions we stated we're
15693
09:17:07,519 --> 09:17:09,278
getting the coordinates
15694
09:17:09,278 --> 09:17:10,557
and we're storing them within the
15695
09:17:10,557 --> 09:17:12,320
attributes of label
15696
09:17:12,320 --> 09:17:14,960
1 and not necessarily label two so in
15697
09:17:14,960 --> 09:17:15,917
order to make
15698
09:17:15,917 --> 09:17:18,480
these functions compatible with all
15699
09:17:18,480 --> 09:17:19,199
widgets
15700
09:17:19,199 --> 09:17:22,718
we can actually add one line of code
15701
09:17:22,718 --> 09:17:26,640
widget equals event dot widget
15702
09:17:26,640 --> 09:17:28,877
so this is going to get the widget of
15703
09:17:28,877 --> 09:17:30,718
the event that we're dealing with
15704
09:17:30,718 --> 09:17:32,800
and we're going to temporarily rename
15705
09:17:32,800 --> 09:17:34,078
this as widget
15706
09:17:34,078 --> 09:17:36,237
so now we're going to replace all
15707
09:17:36,237 --> 09:17:37,839
instances of where we use
15708
09:17:37,839 --> 09:17:42,078
label and replace it with widget
15709
09:17:42,078 --> 09:17:43,839
be sure to do the same thing too with
15710
09:17:43,839 --> 09:17:46,480
drag motion
15711
09:17:46,480 --> 09:17:48,640
and then we will need to add this line
15712
09:17:48,640 --> 09:17:49,758
at the top of
15713
09:17:49,758 --> 09:17:53,040
drag motion so now
15714
09:17:53,040 --> 09:17:55,839
these functions are now compatible with
15715
09:17:55,839 --> 09:17:59,121
any widget that we create
15716
09:17:59,598 --> 09:18:02,557
and we should be able to drag and drop
15717
09:18:02,557 --> 09:18:03,040
both
15718
09:18:03,040 --> 09:18:06,640
widgets all right well that's the basics
15719
09:18:06,640 --> 09:18:08,480
of dragging and dropping widgets in
15720
09:18:08,480 --> 09:18:10,398
python if you want a copy of all this
15721
09:18:10,398 --> 09:18:11,917
code i'll post all of this in the
15722
09:18:11,917 --> 09:18:13,122
comments down below
15723
09:18:13,122 --> 09:18:14,960
but yeah that's the basics of dragging
15724
09:18:14,960 --> 09:18:18,480
and dropping widgets in python
15725
09:18:18,480 --> 09:18:20,078
hey what's going on everybody it's your
15726
09:18:20,078 --> 09:18:21,598
bro here hope you're doing well and in
15727
09:18:21,598 --> 09:18:22,877
this video i'm going to teach you guys
15728
09:18:22,877 --> 09:18:24,160
how we can move an image
15729
09:18:24,160 --> 09:18:27,278
on both a window and a canvas in python
15730
09:18:27,278 --> 09:18:31,200
so sit back relax and enjoy the show
15731
09:18:31,839 --> 09:18:33,758
all right my fellow bros i'm going to
15732
09:18:33,758 --> 09:18:35,360
divide this video into
15733
09:18:35,360 --> 09:18:37,122
two different sections in the first
15734
09:18:37,122 --> 09:18:39,122
section we're going to move a widget
15735
09:18:39,122 --> 09:18:40,237
within a window
15736
09:18:40,237 --> 09:18:42,078
and in the next section we're going to
15737
09:18:42,078 --> 09:18:43,278
move an image
15738
09:18:43,278 --> 09:18:46,320
on a canvas so let's begin by moving a
15739
09:18:46,320 --> 09:18:47,598
widget within a window
15740
09:18:47,598 --> 09:18:50,000
so we'll need a window window equals tk
15741
09:18:50,000 --> 09:18:52,237
followed by window.mainloop and we'll
15742
09:18:52,237 --> 09:18:54,160
also need to set some geometry for this
15743
09:18:54,160 --> 09:18:55,199
window
15744
09:18:55,199 --> 09:18:58,839
window dot geometry let's make this
15745
09:18:58,839 --> 09:19:01,278
500x500
15746
09:19:01,278 --> 09:19:06,237
500 x 500 and let's take a look
15747
09:19:06,237 --> 09:19:08,718
okay that should be a decent size let's
15748
09:19:08,718 --> 09:19:11,680
add a label to this window
15749
09:19:11,680 --> 09:19:13,839
so i actually have a race car image that
15750
09:19:13,839 --> 09:19:15,680
i want to use if you would like an image
15751
09:19:15,680 --> 09:19:16,880
feel free to pause the video and
15752
09:19:16,880 --> 09:19:17,756
download one
15753
09:19:17,756 --> 09:19:19,360
what i would like to do is create a
15754
09:19:19,360 --> 09:19:21,680
photo image from this png file that i
15755
09:19:21,680 --> 09:19:22,240
have
15756
09:19:22,240 --> 09:19:25,436
i'm just going to call this my
15757
09:19:25,436 --> 09:19:28,880
image equals photo
15758
09:19:28,880 --> 09:19:32,880
image file equals since this is within
15759
09:19:32,880 --> 09:19:34,720
my project folder i can just list the
15760
09:19:34,720 --> 09:19:36,320
file name
15761
09:19:36,320 --> 09:19:37,916
otherwise you might have to list the
15762
09:19:37,916 --> 09:19:40,200
file path and for me this is called
15763
09:19:40,200 --> 09:19:42,240
racecar.png
15764
09:19:42,240 --> 09:19:45,040
i want to add this image to my label not
15765
09:19:45,040 --> 09:19:46,000
necessary but
15766
09:19:46,000 --> 09:19:48,000
it'll be helpful for this example i'm
15767
09:19:48,000 --> 09:19:50,240
going to call my label just a label
15768
09:19:50,240 --> 09:19:52,080
label equals label i'm adding this to
15769
09:19:52,080 --> 09:19:54,000
the window and i'm going to set the
15770
09:19:54,000 --> 09:19:55,436
image to equal
15771
09:19:55,436 --> 09:19:57,680
my image that i created it's a photo
15772
09:19:57,680 --> 09:19:58,960
image
15773
09:19:58,960 --> 09:20:00,560
if you do not have an image that you
15774
09:20:00,560 --> 09:20:02,000
want to use you can simply
15775
09:20:02,000 --> 09:20:03,360
just change the background color you
15776
09:20:03,360 --> 09:20:05,040
could say bg
15777
09:20:05,040 --> 09:20:08,000
i don't know red
15778
09:20:08,240 --> 09:20:10,400
oh then we need to place this so be sure
15779
09:20:10,400 --> 09:20:11,276
to do that
15780
09:20:11,276 --> 09:20:13,520
label dot place and i'm going to place
15781
09:20:13,520 --> 09:20:15,040
this in the top left corner that is
15782
09:20:15,040 --> 09:20:15,596
where x
15783
09:20:15,596 --> 09:20:18,640
equals zero and y equals zero and then
15784
09:20:18,640 --> 09:20:20,320
we should have a race car
15785
09:20:20,320 --> 09:20:22,320
or whatever image you used and a red
15786
09:20:22,320 --> 09:20:24,080
background you can get rid of the
15787
09:20:24,080 --> 09:20:25,916
background color if you prefer
15788
09:20:25,916 --> 09:20:27,360
that's just if you don't have an image
15789
09:20:27,360 --> 09:20:29,200
to use you can set the background color
15790
09:20:29,200 --> 09:20:30,000
just for a
15791
09:20:30,000 --> 09:20:32,160
visual to show you where the label
15792
09:20:32,160 --> 09:20:33,520
currently is
15793
09:20:33,520 --> 09:20:35,360
let's set some key bindings for this
15794
09:20:35,360 --> 09:20:37,120
window and let's add them maybe here
15795
09:20:37,120 --> 09:20:39,840
before we create the label and our image
15796
09:20:39,840 --> 09:20:43,120
so let's find the w key for up s for
15797
09:20:43,120 --> 09:20:43,520
down
15798
09:20:43,520 --> 09:20:46,720
a for left and d for right so we do that
15799
09:20:46,720 --> 09:20:47,040
with
15800
09:20:47,040 --> 09:20:51,120
window dot bind and we pass in an event
15801
09:20:51,120 --> 09:20:54,480
as well as a function name so we want to
15802
09:20:54,480 --> 09:20:58,240
tie the w key to a function and we'll
15803
09:20:58,240 --> 09:20:58,880
call this
15804
09:20:58,880 --> 09:21:02,560
move underscore up and then we will need
15805
09:21:02,560 --> 09:21:04,400
to define this
15806
09:21:04,400 --> 09:21:08,080
so outside of the window let's define
15807
09:21:08,080 --> 09:21:12,000
move underscore up and this takes
15808
09:21:12,000 --> 09:21:14,240
an event so we'll place that as a
15809
09:21:14,240 --> 09:21:15,276
parameter
15810
09:21:15,276 --> 09:21:18,000
and to move our label up we're going to
15811
09:21:18,000 --> 09:21:18,720
take
15812
09:21:18,720 --> 09:21:22,000
label dot place
15813
09:21:22,000 --> 09:21:23,756
and we're going to place this at some
15814
09:21:23,756 --> 09:21:25,040
new coordinates
15815
09:21:25,040 --> 09:21:28,960
x equals the label's current position
15816
09:21:28,960 --> 09:21:31,040
relative to the window that it's in so
15817
09:21:31,040 --> 09:21:32,480
that is label
15818
09:21:32,480 --> 09:21:35,596
dot w info
15819
09:21:35,596 --> 09:21:38,640
underscore x
15820
09:21:38,640 --> 09:21:41,680
all right so then for y it's similar
15821
09:21:41,680 --> 09:21:45,680
y equals label dot w
15822
09:21:45,680 --> 09:21:48,720
info underscore y
15823
09:21:48,720 --> 09:21:52,880
but we're going to subtract maybe one
15824
09:21:52,880 --> 09:21:56,640
all right so this is all we need
15825
09:21:56,640 --> 09:21:58,720
although this is only going to move up
15826
09:21:58,720 --> 09:21:59,680
so i'm going to press
15827
09:21:59,680 --> 09:22:03,120
w and our label our race car or whatever
15828
09:22:03,120 --> 09:22:04,640
image you used is going to move
15829
09:22:04,640 --> 09:22:08,080
slowly up i'm going to increase this to
15830
09:22:08,080 --> 09:22:11,120
10 so the change is more drastic
15831
09:22:11,120 --> 09:22:13,680
so now our race car is drifting off into
15832
09:22:13,680 --> 09:22:15,200
space
15833
09:22:15,200 --> 09:22:18,320
let's find a few other keys s a
15834
09:22:18,320 --> 09:22:21,680
and d sad all right so
15835
09:22:21,680 --> 09:22:26,880
s will be down move underscore down
15836
09:22:27,520 --> 09:22:31,916
a for left move left
15837
09:22:31,916 --> 09:22:35,436
and d for right move
15838
09:22:35,436 --> 09:22:38,000
right we'll just copy what we have here
15839
09:22:38,000 --> 09:22:39,436
and make a few changes
15840
09:22:39,436 --> 09:22:44,080
let's begin with move down
15841
09:22:44,480 --> 09:22:45,596
the only thing we're going to change
15842
09:22:45,596 --> 09:22:48,160
within here is in place of subtracting
15843
09:22:48,160 --> 09:22:48,560
10
15844
09:22:48,560 --> 09:22:50,800
from the y coordinate we're going to add
15845
09:22:50,800 --> 09:22:52,880
10
15846
09:22:52,880 --> 09:22:54,720
and with our race car oh i guess we have
15847
09:22:54,720 --> 09:22:56,240
to define these so let's do that real
15848
09:22:56,240 --> 09:22:57,120
quick
15849
09:22:57,120 --> 09:23:00,320
uh so we have left
15850
09:23:00,320 --> 09:23:03,680
move left we're going to subtract
15851
09:23:03,680 --> 09:23:07,360
10 from the x coordinate and keep y the
15852
09:23:07,360 --> 09:23:09,120
same
15853
09:23:09,120 --> 09:23:11,756
and then the same thing for move right
15854
09:23:11,756 --> 09:23:12,160
move
15855
09:23:12,160 --> 09:23:15,360
underscore right and this time we are
15856
09:23:15,360 --> 09:23:16,640
going to add 10
15857
09:23:16,640 --> 09:23:21,200
to x and y will stay the same
15858
09:23:21,200 --> 09:23:25,040
okay now with our image we can move up
15859
09:23:25,040 --> 09:23:29,520
down right and then left
15860
09:23:29,520 --> 09:23:32,080
and y stop there we combine these to our
15861
09:23:32,080 --> 09:23:34,720
arrow keys
15862
09:23:36,320 --> 09:23:39,520
so up is just up with a capital u
15863
09:23:39,520 --> 09:23:42,320
down is down these are all capital by
15864
09:23:42,320 --> 09:23:43,596
the way
15865
09:23:43,596 --> 09:23:48,400
left is left and then right is right
15866
09:23:49,120 --> 09:23:51,200
and now i can use my arrow keys to move
15867
09:23:51,200 --> 09:23:53,756
this image
15868
09:23:58,960 --> 09:24:00,560
now for this part of the video i'm going
15869
09:24:00,560 --> 09:24:02,080
to teach you guys how we can move an
15870
09:24:02,080 --> 09:24:04,560
image on a canvas this time and not just
15871
09:24:04,560 --> 09:24:05,680
within a window
15872
09:24:05,680 --> 09:24:07,360
but we'll need a window to begin with
15873
09:24:07,360 --> 09:24:09,320
window equals tk followed by
15874
09:24:09,320 --> 09:24:10,640
window.mainloop
15875
09:24:10,640 --> 09:24:12,960
and we're going to create a canvas
15876
09:24:12,960 --> 09:24:15,436
canvas equals canvas
15877
09:24:15,436 --> 09:24:17,200
we're adding this canvas to the window
15878
09:24:17,200 --> 09:24:19,040
and let's set a width and height
15879
09:24:19,040 --> 09:24:21,276
for the width let's make this 500 and
15880
09:24:21,276 --> 09:24:22,880
the height 500 as well
15881
09:24:22,880 --> 09:24:25,840
so this is an even square and we'll need
15882
09:24:25,840 --> 09:24:26,160
to
15883
09:24:26,160 --> 09:24:28,800
pack or place this canvas canvas.pack
15884
09:24:28,800 --> 09:24:30,400
should take care of that
15885
09:24:30,400 --> 09:24:32,400
and let's run this and this is our
15886
09:24:32,400 --> 09:24:33,756
canvas
15887
09:24:33,756 --> 09:24:36,080
at this step we need to add an image to
15888
09:24:36,080 --> 09:24:38,240
our canvas and i have an image of a
15889
09:24:38,240 --> 09:24:39,916
racecar that i want to add
15890
09:24:39,916 --> 09:24:42,160
so i need to create a photo image from
15891
09:24:42,160 --> 09:24:43,200
this file
15892
09:24:43,200 --> 09:24:46,080
so we can do that let's give this photo
15893
09:24:46,080 --> 09:24:47,436
image a unique name i'm just going to
15894
09:24:47,436 --> 09:24:50,000
call this photo image to keep it simple
15895
09:24:50,000 --> 09:24:53,200
photo image equals photo image and i can
15896
09:24:53,200 --> 09:24:55,840
list the file path or the file name
15897
09:24:55,840 --> 09:24:56,320
since
15898
09:24:56,320 --> 09:24:59,040
this image is within my project folder i
15899
09:24:59,040 --> 09:25:00,880
only have to list the file name
15900
09:25:00,880 --> 09:25:04,960
and my file is called racecar.png
15901
09:25:04,960 --> 09:25:07,360
this next step we need to give our image
15902
09:25:07,360 --> 09:25:08,320
a unique name
15903
09:25:08,320 --> 09:25:09,840
the image that's being added to the
15904
09:25:09,840 --> 09:25:11,596
canvas i'll call this
15905
09:25:11,596 --> 09:25:15,436
my image equals
15906
09:25:15,436 --> 09:25:18,000
and we need to turn this photo image
15907
09:25:18,000 --> 09:25:20,000
into an image added to the canvas
15908
09:25:20,000 --> 09:25:22,320
so we type in the name of the canvas
15909
09:25:22,320 --> 09:25:23,040
canvas
15910
09:25:23,040 --> 09:25:26,800
dot create underscore image
15911
09:25:26,800 --> 09:25:28,640
we first need to list the coordinates of
15912
09:25:28,640 --> 09:25:31,276
where we want to place this photo image
15913
09:25:31,276 --> 09:25:32,960
so let's place this in the top left
15914
09:25:32,960 --> 09:25:34,320
corner that is where
15915
09:25:34,320 --> 09:25:37,840
x is 0 and y 0 and we need to set the
15916
09:25:37,840 --> 09:25:40,560
image of the image that we're creating
15917
09:25:40,560 --> 09:25:41,840
so we can do that with saying
15918
09:25:41,840 --> 09:25:46,160
image equals the name of our photo image
15919
09:25:46,160 --> 09:25:49,436
and let's see what we have so far so
15920
09:25:49,436 --> 09:25:51,276
this image is somewhat cut off in the
15921
09:25:51,276 --> 09:25:53,276
top left corner we can easily fix that
15922
09:25:53,276 --> 09:25:54,560
by anchoring
15923
09:25:54,560 --> 09:25:56,800
our image when we create it so let's at
15924
09:25:56,800 --> 09:25:57,680
the end add
15925
09:25:57,680 --> 09:26:00,880
anchor equals northwest
15926
09:26:00,880 --> 09:26:03,040
so that should fix that problem and we
15927
09:26:03,040 --> 09:26:06,000
now have our image that we want to use
15928
09:26:06,000 --> 09:26:07,916
and to move our image we can set some
15929
09:26:07,916 --> 09:26:10,320
key bindings
15930
09:26:10,320 --> 09:26:12,960
so we type in the name of the window dot
15931
09:26:12,960 --> 09:26:13,680
bind
15932
09:26:13,680 --> 09:26:16,480
and we pass in two arguments an event as
15933
09:26:16,480 --> 09:26:18,320
well as a function name
15934
09:26:18,320 --> 09:26:21,120
let's tie pressing the w key to moving
15935
09:26:21,120 --> 09:26:21,756
up
15936
09:26:21,756 --> 09:26:24,160
so the first argument is going to be w
15937
09:26:24,160 --> 09:26:25,596
and the second is going to be
15938
09:26:25,596 --> 09:26:28,240
a function name so let's say move
15939
09:26:28,240 --> 09:26:29,916
underscore up
15940
09:26:29,916 --> 09:26:33,200
and let's find some other keys
15941
09:26:33,200 --> 09:26:37,436
w for up s for down
15942
09:26:39,436 --> 09:26:42,880
a for left and
15943
09:26:42,880 --> 09:26:45,520
d for right
15944
09:26:45,916 --> 09:26:48,720
we'll need to define these now so at the
15945
09:26:48,720 --> 09:26:50,400
top outside of the window
15946
09:26:50,400 --> 09:26:53,756
let's begin by defining move up
15947
09:26:53,756 --> 09:26:56,480
def move up this has an event as a
15948
09:26:56,480 --> 09:26:59,040
parameter
15949
09:26:59,200 --> 09:27:02,400
and let's create the other definitions
15950
09:27:02,400 --> 09:27:04,640
here
15951
09:27:05,436 --> 09:27:08,080
so i'm just going to rename these so we
15952
09:27:08,080 --> 09:27:08,560
have move
15953
09:27:08,560 --> 09:27:12,320
up move down move left
15954
09:27:12,320 --> 09:27:18,000
and move right okay now this next step
15955
09:27:18,000 --> 09:27:20,640
we can use the canvases move function to
15956
09:27:20,640 --> 09:27:21,520
move an image
15957
09:27:21,520 --> 09:27:24,240
a certain amount of pixels on the x-axis
15958
09:27:24,240 --> 09:27:24,560
and
15959
09:27:24,560 --> 09:27:27,680
y-axis so let's begin with move up we
15960
09:27:27,680 --> 09:27:29,520
type in the name of the canvas
15961
09:27:29,520 --> 09:27:32,400
dot and use the move function and this
15962
09:27:32,400 --> 09:27:32,800
has
15963
09:27:32,800 --> 09:27:34,880
three arguments the image that we want
15964
09:27:34,880 --> 09:27:35,916
to use
15965
09:27:35,916 --> 09:27:38,000
and this image is called my image the
15966
09:27:38,000 --> 09:27:40,800
one with the race car
15967
09:27:41,436 --> 09:27:43,680
the next argument is the amount of
15968
09:27:43,680 --> 09:27:45,596
pixels we're moving this on the
15969
09:27:45,596 --> 09:27:50,080
x-axis with up we can keep this the same
15970
09:27:50,080 --> 09:27:52,400
but on the y-axis let's say we want to
15971
09:27:52,400 --> 09:27:53,276
move this
15972
09:27:53,276 --> 09:27:57,436
up 10 pixels so that would be -10
15973
09:27:57,436 --> 09:27:59,276
and let's repeat the process for move
15974
09:27:59,276 --> 09:28:01,276
down move left and move right
15975
09:28:01,276 --> 09:28:04,400
for move down we're going to change
15976
09:28:04,400 --> 09:28:08,160
y to 10 for move left
15977
09:28:08,160 --> 09:28:11,120
we'll keep y the same at zero but we're
15978
09:28:11,120 --> 09:28:12,480
going to subtract
15979
09:28:12,480 --> 09:28:16,480
10 and then move right
15980
09:28:16,480 --> 09:28:20,800
we are going to add 10 to our x's
15981
09:28:20,800 --> 09:28:24,480
and that should be it so we can move up
15982
09:28:24,480 --> 09:28:28,720
down right left
15983
09:28:28,720 --> 09:28:30,720
and if you want to bind these to some
15984
09:28:30,720 --> 09:28:31,840
arrow keys
15985
09:28:31,840 --> 09:28:34,720
all we have to do is just change the
15986
09:28:34,720 --> 09:28:35,276
event
15987
09:28:35,276 --> 09:28:38,960
so we can bind these to our wsad keys
15988
09:28:38,960 --> 09:28:42,480
as well as up
15989
09:28:42,480 --> 09:28:46,000
down left
15990
09:28:46,000 --> 09:28:49,276
and right and this time i'm going to use
15991
09:28:49,276 --> 09:28:50,960
my arrow keys
15992
09:28:50,960 --> 09:28:56,320
up down right and left
15993
09:28:56,320 --> 09:28:58,480
all right so that's a few of the basics
15994
09:28:58,480 --> 09:29:00,480
of moving either widgets or
15995
09:29:00,480 --> 09:29:02,960
images on a canvas or window if you
15996
09:29:02,960 --> 09:29:04,640
would like a copy of all this code for
15997
09:29:04,640 --> 09:29:06,880
both examples i'll post both of them in
15998
09:29:06,880 --> 09:29:08,240
the comments down below
15999
09:29:08,240 --> 09:29:10,240
but yeah that's the basics of moving
16000
09:29:10,240 --> 09:29:11,756
widgets and images
16001
09:29:11,756 --> 09:29:14,960
in python hey what's going on everybody
16002
09:29:14,960 --> 09:29:16,480
it's your bro here hope you're doing
16003
09:29:16,480 --> 09:29:17,680
well and in this video i'm going to
16004
09:29:17,680 --> 09:29:19,276
teach you guys how we can create some
16005
09:29:19,276 --> 09:29:22,240
simple 2d animations in python so sit
16006
09:29:22,240 --> 09:29:25,756
back relax and enjoy the show
16007
09:29:26,400 --> 09:29:28,480
alright my fellow bros in this video
16008
09:29:28,480 --> 09:29:30,720
we're going to be animating an image on
16009
09:29:30,720 --> 09:29:33,040
a canvas we're only going to be moving
16010
09:29:33,040 --> 09:29:33,840
this image
16011
09:29:33,840 --> 09:29:36,400
up down left and right feel free to
16012
09:29:36,400 --> 09:29:37,916
download an image that you want to use
16013
09:29:37,916 --> 09:29:39,276
i'm just going to use this
16014
09:29:39,276 --> 09:29:42,560
ufo emoji that i found so we'll need two
16015
09:29:42,560 --> 09:29:43,200
imports
16016
09:29:43,200 --> 09:29:45,596
at the top from tk enter import
16017
09:29:45,596 --> 09:29:47,596
everything that's what we usually have
16018
09:29:47,596 --> 09:29:50,880
we'll also need to import time as well
16019
09:29:50,880 --> 09:29:53,360
we'll need a window window equals tk
16020
09:29:53,360 --> 09:29:55,360
followed by window.main loop
16021
09:29:55,360 --> 09:29:57,040
we'll need a canvas i'm going to call
16022
09:29:57,040 --> 09:29:58,880
the canvas canvas
16023
09:29:58,880 --> 09:30:01,200
canvas equals canvas we're adding the
16024
09:30:01,200 --> 09:30:01,916
canvas
16025
09:30:01,916 --> 09:30:03,916
to the window i'm going to create
16026
09:30:03,916 --> 09:30:05,120
something that's called a
16027
09:30:05,120 --> 09:30:07,520
constant we're going to create a
16028
09:30:07,520 --> 09:30:09,680
constant for the width and the height
16029
09:30:09,680 --> 09:30:12,480
of our canvas so a constant is a
16030
09:30:12,480 --> 09:30:14,720
variable or value that you don't plan on
16031
09:30:14,720 --> 09:30:16,080
changing later at all
16032
09:30:16,080 --> 09:30:18,000
and a common naming convention for a
16033
09:30:18,000 --> 09:30:20,000
constant is that the name of the
16034
09:30:20,000 --> 09:30:20,880
constant is
16035
09:30:20,880 --> 09:30:23,916
all uppercase so i want the width and
16036
09:30:23,916 --> 09:30:24,480
the height
16037
09:30:24,480 --> 09:30:27,120
of this canvas to be constants so for
16038
09:30:27,120 --> 09:30:28,560
the width this is going to be
16039
09:30:28,560 --> 09:30:31,276
all uppercase it's not necessary to make
16040
09:30:31,276 --> 09:30:32,080
it all uppercase
16041
09:30:32,080 --> 09:30:34,400
but that's a common naming convention
16042
09:30:34,400 --> 09:30:35,916
for the width i'm going to set this to
16043
09:30:35,916 --> 09:30:36,800
500
16044
09:30:36,800 --> 09:30:39,840
and the height set to 500 as well
16045
09:30:39,840 --> 09:30:41,756
and for the width of this canvas i'm
16046
09:30:41,756 --> 09:30:43,436
going to set the width
16047
09:30:43,436 --> 09:30:46,720
to equal my constant which is 500
16048
09:30:46,720 --> 09:30:50,640
and the height to my constant height
16049
09:30:50,640 --> 09:30:52,640
so the reason that i'm making constants
16050
09:30:52,640 --> 09:30:54,800
is that we'll be reusing these a lot
16051
09:30:54,800 --> 09:30:56,720
later in this program so we might as
16052
09:30:56,720 --> 09:30:58,480
well just declare them now and get it
16053
09:30:58,480 --> 09:30:59,360
over with
16054
09:30:59,360 --> 09:31:01,680
so for the canvas we're adding this to
16055
09:31:01,680 --> 09:31:03,596
the window we're setting the width to
16056
09:31:03,596 --> 09:31:05,680
our constant of width and the height to
16057
09:31:05,680 --> 09:31:07,040
our constant of height
16058
09:31:07,040 --> 09:31:08,880
and we need to follow this by packing
16059
09:31:08,880 --> 09:31:10,480
this or place
16060
09:31:10,480 --> 09:31:13,520
canvas dot pack pack to make it simple
16061
09:31:13,520 --> 09:31:17,276
so we should have a canvas on our window
16062
09:31:17,276 --> 09:31:18,640
now if you have an image that you want
16063
09:31:18,640 --> 09:31:20,640
to use we're going to animate it
16064
09:31:20,640 --> 09:31:22,880
i need to create a photo image from this
16065
09:31:22,880 --> 09:31:25,276
file it's a png file it's called
16066
09:31:25,276 --> 09:31:27,360
ufo but yours is probably going to be
16067
09:31:27,360 --> 09:31:28,560
named something different so
16068
09:31:28,560 --> 09:31:31,120
don't copy me exactly i need to create a
16069
09:31:31,120 --> 09:31:32,240
photo image from this
16070
09:31:32,240 --> 09:31:35,520
i'm going to call this photo underscore
16071
09:31:35,520 --> 09:31:38,320
image but name it whatever you want this
16072
09:31:38,320 --> 09:31:39,120
equals
16073
09:31:39,120 --> 09:31:42,240
photo image and i need to list the file
16074
09:31:42,240 --> 09:31:43,916
path or the file name
16075
09:31:43,916 --> 09:31:46,160
since this file is within my project
16076
09:31:46,160 --> 09:31:48,880
folder i only have to list the file name
16077
09:31:48,880 --> 09:31:53,040
and for me this is called ufo.png
16078
09:31:53,040 --> 09:31:54,880
our next step is that we're going to
16079
09:31:54,880 --> 09:31:57,276
take our photo image and add this photo
16080
09:31:57,276 --> 09:31:58,720
image to the canvas
16081
09:31:58,720 --> 09:32:00,960
and we'll also give this image a unique
16082
09:32:00,960 --> 09:32:03,680
name i'm going to call this my
16083
09:32:03,680 --> 09:32:06,720
image equals we're going to use the
16084
09:32:06,720 --> 09:32:07,596
canvases
16085
09:32:07,596 --> 09:32:11,040
create image function so canvas dot
16086
09:32:11,040 --> 09:32:13,840
create image there's a few arguments
16087
09:32:13,840 --> 09:32:14,720
that we need to pass
16088
09:32:14,720 --> 09:32:17,360
in where we want this image to appear we
16089
09:32:17,360 --> 09:32:18,800
need a set of coordinates
16090
09:32:18,800 --> 09:32:20,320
if we want this to appear in the top
16091
09:32:20,320 --> 09:32:22,000
left corner that is where x
16092
09:32:22,000 --> 09:32:25,596
is zero and y is zero for the image
16093
09:32:25,596 --> 09:32:27,360
we're going to set this equal to
16094
09:32:27,360 --> 09:32:30,880
our photo image whatever you named this
16095
09:32:30,880 --> 09:32:33,120
and lastly we should anchor this i'm
16096
09:32:33,120 --> 09:32:35,276
going to anchor this in the northwest
16097
09:32:35,276 --> 09:32:37,120
corner to keep it simple
16098
09:32:37,120 --> 09:32:39,680
and we should have our image on our
16099
09:32:39,680 --> 09:32:41,520
canvas in the top left corner
16100
09:32:41,520 --> 09:32:44,080
and here is my ufo that's being piloted
16101
09:32:44,080 --> 09:32:46,560
by a green alien that's waving to me
16102
09:32:46,560 --> 09:32:48,000
aliens are usually not that friendly
16103
09:32:48,000 --> 09:32:49,680
unless they're abducting you our next
16104
09:32:49,680 --> 09:32:50,960
step is to create a
16105
09:32:50,960 --> 09:32:53,360
while loop so we'll say wow true this
16106
09:32:53,360 --> 09:32:55,120
will continue forever until we close out
16107
09:32:55,120 --> 09:32:56,160
of the program
16108
09:32:56,160 --> 09:32:57,756
if you're looking at the code for a game
16109
09:32:57,756 --> 09:32:59,596
this might say instead
16110
09:32:59,596 --> 09:33:03,120
while running running will be a boolean
16111
09:33:03,120 --> 09:33:05,276
variable that contains either true or
16112
09:33:05,276 --> 09:33:07,680
false so if this is true while the game
16113
09:33:07,680 --> 09:33:08,240
is running
16114
09:33:08,240 --> 09:33:10,160
they're going to continue moving all of
16115
09:33:10,160 --> 09:33:11,840
the characters on the screen
16116
09:33:11,840 --> 09:33:13,596
and then if somebody pauses the game or
16117
09:33:13,596 --> 09:33:14,880
closes out of the game
16118
09:33:14,880 --> 09:33:16,880
then they'll flip this running variable
16119
09:33:16,880 --> 09:33:19,040
to false but to keep things simple we'll
16120
09:33:19,040 --> 09:33:20,000
just say wow
16121
09:33:20,000 --> 09:33:21,840
true but that's something that you might
16122
09:33:21,840 --> 09:33:24,240
see what we'll want to do is get the
16123
09:33:24,240 --> 09:33:25,756
coordinates of our image
16124
09:33:25,756 --> 09:33:28,800
within the canvas and this will return a
16125
09:33:28,800 --> 09:33:29,916
list of coordinates
16126
09:33:29,916 --> 09:33:31,680
so let's create a list called
16127
09:33:31,680 --> 09:33:33,120
coordinates so
16128
09:33:33,120 --> 09:33:37,120
coordinates equals canvas
16129
09:33:37,120 --> 09:33:40,640
and we can use the chords function to
16130
09:33:40,640 --> 09:33:41,680
get the coordinates
16131
09:33:41,680 --> 09:33:44,240
i guess that's short for coordinates and
16132
09:33:44,240 --> 09:33:45,756
we're getting the coordinates of
16133
09:33:45,756 --> 09:33:48,400
my image so we'll pass this into this
16134
09:33:48,400 --> 09:33:49,840
function
16135
09:33:49,840 --> 09:33:51,756
and then let's print this just to see
16136
09:33:51,756 --> 09:33:52,880
what this returns
16137
09:33:52,880 --> 09:33:56,720
we'll print coordinates
16138
09:33:57,040 --> 09:33:59,840
okay now to update the window after each
16139
09:33:59,840 --> 09:34:00,400
cycle
16140
09:34:00,400 --> 09:34:02,960
of this while loop we can use the update
16141
09:34:02,960 --> 09:34:04,320
function of our window
16142
09:34:04,320 --> 09:34:07,596
so window.update will also have this
16143
09:34:07,596 --> 09:34:10,960
thread sleep for let's say 0.01 seconds
16144
09:34:10,960 --> 09:34:15,916
so time dot sleep 0.01 one
16145
09:34:15,916 --> 09:34:17,520
so here's a quick rundown of what we
16146
09:34:17,520 --> 09:34:19,360
have going on so far within this while
16147
09:34:19,360 --> 09:34:19,756
loop
16148
09:34:19,756 --> 09:34:21,916
we're going to first get the coordinates
16149
09:34:21,916 --> 09:34:23,200
of where our image
16150
09:34:23,200 --> 09:34:26,000
is located we'll print the coordinates
16151
09:34:26,000 --> 09:34:27,596
to the console window
16152
09:34:27,596 --> 09:34:29,680
this part isn't required but it's going
16153
09:34:29,680 --> 09:34:32,080
to help us for learning purposes
16154
09:34:32,080 --> 09:34:34,480
we'll update this window for any changes
16155
09:34:34,480 --> 09:34:36,080
and then our thread
16156
09:34:36,080 --> 09:34:37,916
which is in charge of running the
16157
09:34:37,916 --> 09:34:39,596
program is going to sleep
16158
09:34:39,596 --> 09:34:41,840
for i believe that's a hundredth of a
16159
09:34:41,840 --> 09:34:43,276
second
16160
09:34:43,276 --> 09:34:46,240
so when we run this our image is staying
16161
09:34:46,240 --> 09:34:47,120
in place
16162
09:34:47,120 --> 09:34:48,640
and we know that our while loop is
16163
09:34:48,640 --> 09:34:50,560
running because it's continuously
16164
09:34:50,560 --> 09:34:51,916
printing the coordinates
16165
09:34:51,916 --> 09:34:55,200
the first number is the x position
16166
09:34:55,200 --> 09:34:58,080
the second number is the y position and
16167
09:34:58,080 --> 09:34:59,916
now we're going to give our image a
16168
09:34:59,916 --> 09:35:00,400
speed
16169
09:35:00,400 --> 09:35:03,200
or velocity i like to call it velocity
16170
09:35:03,200 --> 09:35:05,120
and in fact i'm going to give this image
16171
09:35:05,120 --> 09:35:06,160
two velocities
16172
09:35:06,160 --> 09:35:08,400
one for how fast this image is moving on
16173
09:35:08,400 --> 09:35:09,756
the x-axis
16174
09:35:09,756 --> 09:35:11,756
and another for how fast it's moving on
16175
09:35:11,756 --> 09:35:13,360
the y axis
16176
09:35:13,360 --> 09:35:15,596
so i'm going to create two variables one
16177
09:35:15,596 --> 09:35:17,520
called x velocity
16178
09:35:17,520 --> 09:35:19,916
and i'm going to set this equal to one
16179
09:35:19,916 --> 09:35:22,800
and y velocity
16180
09:35:22,800 --> 09:35:26,160
and i'll set this to one as well okay
16181
09:35:26,160 --> 09:35:29,436
so what we need to do now is update
16182
09:35:29,436 --> 09:35:32,480
the position of our image so let's do
16183
09:35:32,480 --> 09:35:33,680
that before we update
16184
09:35:33,680 --> 09:35:35,916
our window so i'm going to type in the
16185
09:35:35,916 --> 09:35:36,960
name of the canvas
16186
09:35:36,960 --> 09:35:39,276
and use the move function this move
16187
09:35:39,276 --> 09:35:41,680
function takes three arguments
16188
09:35:41,680 --> 09:35:44,080
what you want to move i want to move my
16189
09:35:44,080 --> 09:35:46,240
image my ufo
16190
09:35:46,240 --> 09:35:49,120
and how far i want to move this image on
16191
09:35:49,120 --> 09:35:51,276
each axis each coordinate
16192
09:35:51,276 --> 09:35:54,640
so what i want to do is update the
16193
09:35:54,640 --> 09:35:58,400
x velocity the x position of my image
16194
09:35:58,400 --> 09:36:02,080
so i'm going to pass in my x velocity
16195
09:36:02,080 --> 09:36:04,640
variable and then y we'll just keep the
16196
09:36:04,640 --> 09:36:06,080
same for now so we'll just say
16197
09:36:06,080 --> 09:36:08,640
zero so after each iteration of this
16198
09:36:08,640 --> 09:36:09,276
while loop
16199
09:36:09,276 --> 09:36:11,840
my ufo is going to move one pixel to the
16200
09:36:11,840 --> 09:36:12,320
right
16201
09:36:12,320 --> 09:36:13,916
and it's just going to go off into the
16202
09:36:13,916 --> 09:36:15,756
void forever and now
16203
09:36:15,756 --> 09:36:19,520
if we replaced x velocity with zero
16204
09:36:19,520 --> 09:36:22,960
and for y we set this to our y velocity
16205
09:36:22,960 --> 09:36:26,240
this image is now going to travel down
16206
09:36:26,240 --> 09:36:27,840
and it will continue into the void
16207
09:36:27,840 --> 09:36:29,360
forever
16208
09:36:29,360 --> 09:36:30,800
and then if we combine both of these
16209
09:36:30,800 --> 09:36:33,200
together
16210
09:36:33,840 --> 09:36:36,160
we're going to move diagonally down and
16211
09:36:36,160 --> 09:36:37,680
to the right
16212
09:36:37,680 --> 09:36:40,000
so what if we want this image to bounce
16213
09:36:40,000 --> 09:36:41,756
off the walls we're going to add some
16214
09:36:41,756 --> 09:36:43,596
if statements we're going to begin by
16215
09:36:43,596 --> 09:36:45,680
just having this image bounce off the
16216
09:36:45,680 --> 09:36:47,120
left and right walls
16217
09:36:47,120 --> 09:36:50,000
for now let's set y velocity to zero so
16218
09:36:50,000 --> 09:36:51,840
that this image is only moving
16219
09:36:51,840 --> 09:36:55,436
left and right so before we move this
16220
09:36:55,436 --> 09:36:56,960
image we'll add an if statement we're
16221
09:36:56,960 --> 09:36:58,080
going to check to see
16222
09:36:58,080 --> 09:37:01,520
if the x position of our image
16223
09:37:01,520 --> 09:37:03,680
is either greater than the width of the
16224
09:37:03,680 --> 09:37:04,560
canvas
16225
09:37:04,560 --> 09:37:07,680
or if it's less than zero so we take
16226
09:37:07,680 --> 09:37:09,840
our coordinates and we're going to get
16227
09:37:09,840 --> 09:37:11,680
the first element from this list
16228
09:37:11,680 --> 09:37:15,276
so that is zero that is x the x value
16229
09:37:15,276 --> 09:37:18,000
if coordinates at element zero is
16230
09:37:18,000 --> 09:37:20,160
greater than or equal to
16231
09:37:20,160 --> 09:37:22,160
the width of this window and this is a
16232
09:37:22,160 --> 09:37:24,480
constant
16233
09:37:24,480 --> 09:37:26,320
so we can just place that here if
16234
09:37:26,320 --> 09:37:27,840
coordinates
16235
09:37:27,840 --> 09:37:29,916
well x within our coordinates is greater
16236
09:37:29,916 --> 09:37:32,480
than or equal to width
16237
09:37:32,480 --> 09:37:36,080
or coordinates at index
16238
09:37:36,080 --> 09:37:40,400
0 is less than 0 then we want to reverse
16239
09:37:40,400 --> 09:37:43,436
the direction or flip the velocity
16240
09:37:43,436 --> 09:37:45,680
so we want to change our x velocity and
16241
09:37:45,680 --> 09:37:47,596
make this a negative number
16242
09:37:47,596 --> 09:37:49,680
one easy way to do that is that we can
16243
09:37:49,680 --> 09:37:51,276
take x velocity
16244
09:37:51,276 --> 09:37:55,200
equals x velocity times negative one
16245
09:37:55,200 --> 09:37:57,200
or another way of writing this is just
16246
09:37:57,200 --> 09:37:59,200
to say x velocity
16247
09:37:59,200 --> 09:38:02,640
minus negative velocity now what ends up
16248
09:38:02,640 --> 09:38:03,200
happening
16249
09:38:03,200 --> 09:38:05,840
is that when this image reaches the
16250
09:38:05,840 --> 09:38:08,400
right border it's going to bounce back
16251
09:38:08,400 --> 09:38:10,960
however it only bounces back after it
16252
09:38:10,960 --> 09:38:11,520
reaches
16253
09:38:11,520 --> 09:38:13,840
the top left corner of the image we need
16254
09:38:13,840 --> 09:38:14,960
to factor in
16255
09:38:14,960 --> 09:38:17,360
the width of the image as well so we're
16256
09:38:17,360 --> 09:38:18,160
going to create
16257
09:38:18,160 --> 09:38:19,916
two new variables and let's do this
16258
09:38:19,916 --> 09:38:21,436
right before the while loop
16259
09:38:21,436 --> 09:38:23,276
we'll want to determine what the height
16260
09:38:23,276 --> 09:38:25,360
and the width are for our photo image
16261
09:38:25,360 --> 09:38:26,400
that we have
16262
09:38:26,400 --> 09:38:28,240
so this is what we're going to write
16263
09:38:28,240 --> 09:38:29,756
let's call these two values
16264
09:38:29,756 --> 09:38:33,436
image underscore width and
16265
09:38:33,436 --> 09:38:36,720
image underscore height
16266
09:38:37,040 --> 09:38:39,520
so we need to determine what the width
16267
09:38:39,520 --> 09:38:40,320
and the height is
16268
09:38:40,320 --> 09:38:42,560
for our photo image and there is a
16269
09:38:42,560 --> 09:38:44,320
function to do that
16270
09:38:44,320 --> 09:38:47,120
type in the name of the photo image dot
16271
09:38:47,120 --> 09:38:48,320
and there is a width
16272
09:38:48,320 --> 09:38:53,360
function and the same thing for height
16273
09:38:53,360 --> 09:38:56,800
photo image dot height
16274
09:38:56,800 --> 09:38:59,596
and then we can reuse these so i'm just
16275
09:38:59,596 --> 09:39:00,320
going to add
16276
09:39:00,320 --> 09:39:03,916
one line to the sift statement if
16277
09:39:03,916 --> 09:39:06,480
the x position of our image is greater
16278
09:39:06,480 --> 09:39:07,520
uh greater than
16279
09:39:07,520 --> 09:39:10,800
or equal to the width of our window
16280
09:39:10,800 --> 09:39:13,756
minus the width of our image and i'm
16281
09:39:13,756 --> 09:39:15,040
just going to put these within
16282
09:39:15,040 --> 09:39:16,480
parentheses just to keep these more
16283
09:39:16,480 --> 09:39:17,916
organized
16284
09:39:17,916 --> 09:39:20,640
and what ends up happening now is that
16285
09:39:20,640 --> 09:39:22,000
when
16286
09:39:22,000 --> 09:39:25,200
the right portion of our image hits the
16287
09:39:25,200 --> 09:39:27,756
right wall it's going to bounce off
16288
09:39:27,756 --> 09:39:29,680
and let's do the same thing for our y
16289
09:39:29,680 --> 09:39:32,240
velocity
16290
09:39:32,720 --> 09:39:35,756
we're going to replace coordinates at
16291
09:39:35,756 --> 09:39:38,640
index 0 with 1 and change that here as
16292
09:39:38,640 --> 09:39:39,200
well
16293
09:39:39,200 --> 09:39:42,960
replace with with height
16294
09:39:43,040 --> 09:39:46,720
and this is image underscore height
16295
09:39:46,720 --> 09:39:49,040
and we're going to change the y velocity
16296
09:39:49,040 --> 09:39:53,840
this time
16297
09:39:54,160 --> 09:39:56,560
and when we move this image let's also
16298
09:39:56,560 --> 09:39:57,436
add the
16299
09:39:57,436 --> 09:40:00,240
y velocity
16300
09:40:00,800 --> 09:40:03,120
and now our image is just going to
16301
09:40:03,120 --> 09:40:04,720
bounce off the walls forever
16302
09:40:04,720 --> 09:40:06,480
but it's kind of predictable since we
16303
09:40:06,480 --> 09:40:08,080
set x and y
16304
09:40:08,080 --> 09:40:10,560
to equal one now let's change the speeds
16305
09:40:10,560 --> 09:40:12,000
to kind of spice things up
16306
09:40:12,000 --> 09:40:14,080
let's say for the x velocity this is
16307
09:40:14,080 --> 09:40:16,720
three and y velocity this is now two
16308
09:40:16,720 --> 09:40:18,640
this is going to go in a completely
16309
09:40:18,640 --> 09:40:21,120
different direction
16310
09:40:21,120 --> 09:40:22,560
so you can mess with the speeds however
16311
09:40:22,560 --> 09:40:24,160
you want so
16312
09:40:24,160 --> 09:40:26,240
guys how about we add a background image
16313
09:40:26,240 --> 09:40:28,320
to this well because we can
16314
09:40:28,320 --> 09:40:30,560
i'm going to reuse this image of space
16315
09:40:30,560 --> 09:40:33,436
from my java video on this same topic
16316
09:40:33,436 --> 09:40:35,596
i need to create a photo image from this
16317
09:40:35,596 --> 09:40:37,520
and i'm going to copy what we did for
16318
09:40:37,520 --> 09:40:40,000
our normal image that we were moving and
16319
09:40:40,000 --> 09:40:41,756
animating
16320
09:40:41,756 --> 09:40:44,480
so this is called space.png well that's
16321
09:40:44,480 --> 09:40:46,080
the name of my file
16322
09:40:46,080 --> 09:40:47,840
and i'm going to give this image a
16323
09:40:47,840 --> 09:40:50,080
different name maybe i'll call this
16324
09:40:50,080 --> 09:40:53,436
background underscore photo
16325
09:40:53,436 --> 09:40:55,596
and then i'm going to add this photo to
16326
09:40:55,596 --> 09:40:57,756
the canvas
16327
09:40:57,756 --> 09:40:59,360
and i'm going to call this a different
16328
09:40:59,360 --> 09:41:01,360
name
16329
09:41:01,360 --> 09:41:04,400
i'll just call this background all right
16330
09:41:04,400 --> 09:41:04,960
so
16331
09:41:04,960 --> 09:41:08,160
our window should have a background now
16332
09:41:08,160 --> 09:41:10,160
and then our image moves on top of the
16333
09:41:10,160 --> 09:41:10,880
background
16334
09:41:10,880 --> 09:41:12,560
so make sure you add and create the
16335
09:41:12,560 --> 09:41:14,400
background before you add any other
16336
09:41:14,400 --> 09:41:15,360
images
16337
09:41:15,360 --> 09:41:17,596
otherwise the background might overlap
16338
09:41:17,596 --> 09:41:18,880
your image
16339
09:41:18,880 --> 09:41:21,040
alright so that's the basics of some
16340
09:41:21,040 --> 09:41:22,560
simple animations in
16341
09:41:22,560 --> 09:41:24,560
python if you want to copy all this code
16342
09:41:24,560 --> 09:41:26,000
i'll post all of this in the comments
16343
09:41:26,000 --> 09:41:26,880
down below
16344
09:41:26,880 --> 09:41:28,560
but yeah that's the basics of some
16345
09:41:28,560 --> 09:41:31,596
animations in python
16346
09:41:31,596 --> 09:41:33,436
hey what's going on everybody it's your
16347
09:41:33,436 --> 09:41:35,200
bro here hope you're doing well and in
16348
09:41:35,200 --> 09:41:36,480
this video i'm going to teach you guys
16349
09:41:36,480 --> 09:41:38,480
how we can animate multiple objects in
16350
09:41:38,480 --> 09:41:39,360
python so
16351
09:41:39,360 --> 09:41:43,200
sit back relax and enjoy the show
16352
09:41:43,680 --> 09:41:45,756
welcome back my fellow bros in this
16353
09:41:45,756 --> 09:41:47,200
video we're going to be animating
16354
09:41:47,200 --> 09:41:49,040
multiple objects within a window
16355
09:41:49,040 --> 09:41:50,960
more specifically we'll animate some
16356
09:41:50,960 --> 09:41:52,960
circles and each of these circles will
16357
09:41:52,960 --> 09:41:54,880
have their own characteristics
16358
09:41:54,880 --> 09:41:57,520
speeds and directions so we'll need two
16359
09:41:57,520 --> 09:41:58,560
imports to begin
16360
09:41:58,560 --> 09:42:01,200
from ck enter import everything we'll
16361
09:42:01,200 --> 09:42:02,880
also be importing everything from the
16362
09:42:02,880 --> 09:42:04,240
time module as well
16363
09:42:04,240 --> 09:42:06,480
we'll need a window window equals tk
16364
09:42:06,480 --> 09:42:08,480
followed by window.main loop
16365
09:42:08,480 --> 09:42:10,320
i'm going to create two constants one
16366
09:42:10,320 --> 09:42:12,320
for the width and one for the height
16367
09:42:12,320 --> 09:42:14,480
let's set the width equal to 500 and the
16368
09:42:14,480 --> 09:42:15,436
height equal to
16369
09:42:15,436 --> 09:42:17,840
500 as well we'll create a canvas and
16370
09:42:17,840 --> 09:42:19,040
we'll be animating
16371
09:42:19,040 --> 09:42:20,960
the circles that we're going to create
16372
09:42:20,960 --> 09:42:23,916
on the canvas so let's create a canvas
16373
09:42:23,916 --> 09:42:27,040
canvas equals canvas
16374
09:42:27,040 --> 09:42:28,480
we're going to be adding the canvas to
16375
09:42:28,480 --> 09:42:30,560
the window and we'll set the width of
16376
09:42:30,560 --> 09:42:33,120
our canvas equal to our constant width
16377
09:42:33,120 --> 09:42:36,160
which is 500 and our height equal to our
16378
09:42:36,160 --> 09:42:37,276
constant of height
16379
09:42:37,276 --> 09:42:40,000
which is also 500 and we will add this
16380
09:42:40,000 --> 09:42:42,200
canvas by using the pack function
16381
09:42:42,200 --> 09:42:45,436
canvas.pack here's our canvas and we're
16382
09:42:45,436 --> 09:42:47,276
going to be creating multiple circles
16383
09:42:47,276 --> 09:42:48,960
let's pretend that they're all different
16384
09:42:48,960 --> 09:42:51,520
sports balls like a tennis ball baseball
16385
09:42:51,520 --> 09:42:53,120
volleyball bowling ball
16386
09:42:53,120 --> 09:42:55,120
they'll all be bouncing off of the
16387
09:42:55,120 --> 09:42:56,160
borders and once
16388
09:42:56,160 --> 09:42:57,756
each of them encounters a border they
16389
09:42:57,756 --> 09:42:59,680
will change direction
16390
09:42:59,680 --> 09:43:01,200
since we're going to be creating
16391
09:43:01,200 --> 09:43:03,360
multiple objects i think it's best if we
16392
09:43:03,360 --> 09:43:05,276
do this with another class
16393
09:43:05,276 --> 09:43:07,520
so let's create another class i'm going
16394
09:43:07,520 --> 09:43:08,960
to go to file
16395
09:43:08,960 --> 09:43:12,160
new python file and let's call this
16396
09:43:12,160 --> 09:43:12,800
class
16397
09:43:12,800 --> 09:43:16,480
ball and click python file and we need
16398
09:43:16,480 --> 09:43:17,276
to define
16399
09:43:17,276 --> 09:43:22,240
this class class ball
16400
09:43:22,240 --> 09:43:24,560
and we also need a constructor for this
16401
09:43:24,560 --> 09:43:25,596
ball class
16402
09:43:25,596 --> 09:43:28,160
we can create one with def two
16403
09:43:28,160 --> 09:43:28,960
underscores
16404
09:43:28,960 --> 09:43:32,800
i-n-i-t two underscores again
16405
09:43:32,800 --> 09:43:34,800
and we can set up the parameters of what
16406
09:43:34,800 --> 09:43:36,960
we want to receive if we were to create
16407
09:43:36,960 --> 09:43:37,680
a ball
16408
09:43:37,680 --> 09:43:40,720
object so let's pretend that
16409
09:43:40,720 --> 09:43:42,480
we're going to be creating a volleyball
16410
09:43:42,480 --> 09:43:44,080
to begin with so
16411
09:43:44,080 --> 09:43:47,360
let's call this ball volley
16412
09:43:47,360 --> 09:43:50,720
underscore ball equals
16413
09:43:50,720 --> 09:43:53,596
ball and we'll pass in some arguments so
16414
09:43:53,596 --> 09:43:55,360
let's set up the parameters first
16415
09:43:55,360 --> 09:43:57,680
what do we want to receive one we want
16416
09:43:57,680 --> 09:43:58,400
to receive
16417
09:43:58,400 --> 09:44:00,960
our canvas so that we can actually draw
16418
09:44:00,960 --> 09:44:03,596
this oval we can draw this ball
16419
09:44:03,596 --> 09:44:07,360
we should also have an x and y position
16420
09:44:07,360 --> 09:44:10,160
a diameter
16421
09:44:11,276 --> 09:44:15,756
x velocity y velocity
16422
09:44:15,756 --> 09:44:18,880
and a color all right so
16423
09:44:18,880 --> 09:44:21,276
when we create a ball like a volleyball
16424
09:44:21,276 --> 09:44:22,240
we're going to pass
16425
09:44:22,240 --> 09:44:25,276
in whatever the parameters require so we
16426
09:44:25,276 --> 09:44:26,880
need a canvas
16427
09:44:26,880 --> 09:44:30,480
x y a diameter an x velocity
16428
09:44:30,480 --> 09:44:32,880
y velocity and a color so within the
16429
09:44:32,880 --> 09:44:34,160
constructor for the ball
16430
09:44:34,160 --> 09:44:37,040
we're passing in our canvas it's kind of
16431
09:44:37,040 --> 09:44:38,960
like what we do with widgets
16432
09:44:38,960 --> 09:44:40,640
we're specifying what we're going to be
16433
09:44:40,640 --> 09:44:42,160
adding this widget to
16434
09:44:42,160 --> 09:44:44,000
we're going to be adding this ball to
16435
09:44:44,000 --> 09:44:46,160
our canvas we also need
16436
09:44:46,160 --> 09:44:48,800
x y and a diameter let's make this
16437
09:44:48,800 --> 09:44:51,680
simple by setting x and y to both zero
16438
09:44:51,680 --> 09:44:54,800
for the diameter let's set this to maybe
16439
09:44:54,800 --> 09:44:58,720
100 and we also need an x velocity and y
16440
09:44:58,720 --> 09:44:59,596
velocity
16441
09:44:59,596 --> 09:45:02,560
let's set this both to one and a color
16442
09:45:02,560 --> 09:45:04,160
so let's pass in a string
16443
09:45:04,160 --> 09:45:07,276
like white let's head back to our ball
16444
09:45:07,276 --> 09:45:08,960
class and finish assigning
16445
09:45:08,960 --> 09:45:10,400
all of these arguments that we're going
16446
09:45:10,400 --> 09:45:12,480
to receive the first step is that we'll
16447
09:45:12,480 --> 09:45:13,200
say
16448
09:45:13,200 --> 09:45:16,240
self dot canvas equals
16449
09:45:16,240 --> 09:45:18,480
the canvas that we receive when it's
16450
09:45:18,480 --> 09:45:21,840
passed to us as an argument
16451
09:45:21,840 --> 09:45:24,720
our next step is to draw this oval draw
16452
09:45:24,720 --> 09:45:25,596
this ball
16453
09:45:25,596 --> 09:45:29,436
and we'll say self dot image
16454
09:45:29,436 --> 09:45:33,200
equals canvas dot create
16455
09:45:33,200 --> 09:45:36,480
underscore oval and with creating ovals
16456
09:45:36,480 --> 09:45:39,916
we can pass in x y
16457
09:45:39,916 --> 09:45:42,960
and with circles the diameter is equal
16458
09:45:42,960 --> 09:45:44,560
for both the width and the height
16459
09:45:44,560 --> 09:45:48,480
so we'll pass in our diameter twice
16460
09:45:48,880 --> 09:45:52,480
and lastly a fill color
16461
09:45:52,720 --> 09:45:54,880
and we'll say this is equal to the color
16462
09:45:54,880 --> 09:45:55,840
that we receive
16463
09:45:55,840 --> 09:45:58,640
which is white
16464
09:45:58,880 --> 09:46:00,880
we're going to set the x velocity and
16465
09:46:00,880 --> 09:46:02,640
the y velocity
16466
09:46:02,640 --> 09:46:04,720
this is related to the direction that
16467
09:46:04,720 --> 09:46:06,480
it's initially going to head in
16468
09:46:06,480 --> 09:46:10,640
so self dot x velocity
16469
09:46:10,640 --> 09:46:15,200
equals the x velocity that we receive
16470
09:46:15,200 --> 09:46:16,880
and we'll do the same thing for y
16471
09:46:16,880 --> 09:46:19,436
velocity
16472
09:46:19,520 --> 09:46:23,596
self.y velocity equals y velocity
16473
09:46:23,596 --> 09:46:25,916
now since this is in another class we
16474
09:46:25,916 --> 09:46:29,040
need to import this class
16475
09:46:29,040 --> 09:46:32,240
from ball import
16476
09:46:32,240 --> 09:46:34,560
everything and you can see that this red
16477
09:46:34,560 --> 09:46:36,240
underline went away
16478
09:46:36,240 --> 09:46:39,276
so let's see what happens now
16479
09:46:39,276 --> 09:46:42,080
we have our volleyball but it's not
16480
09:46:42,080 --> 09:46:42,960
currently moving
16481
09:46:42,960 --> 09:46:45,680
so that's the next step so let's create
16482
09:46:45,680 --> 09:46:46,560
a function
16483
09:46:46,560 --> 09:46:48,320
within the ball class called move that
16484
09:46:48,320 --> 09:46:49,680
we can call upon
16485
09:46:49,680 --> 09:46:51,916
so we'll do this after the constructor
16486
09:46:51,916 --> 09:46:53,360
cef
16487
09:46:53,360 --> 09:46:56,400
move and this will take self as an
16488
09:46:56,400 --> 09:46:58,640
argument
16489
09:46:58,640 --> 09:47:00,320
the first thing that we should do is get
16490
09:47:00,320 --> 09:47:02,640
the coordinates
16491
09:47:02,640 --> 09:47:04,400
so we'll say coordinates and this is a
16492
09:47:04,400 --> 09:47:06,560
list coordinates equals
16493
09:47:06,560 --> 09:47:09,756
self dot canvas dot
16494
09:47:09,756 --> 09:47:13,756
c o o rds
16495
09:47:14,240 --> 09:47:18,160
and we'll pass in self dot image
16496
09:47:18,160 --> 09:47:20,000
and we will print these coordinates just
16497
09:47:20,000 --> 09:47:22,800
to see what's here
16498
09:47:26,800 --> 09:47:29,360
all right now we'll create a while loop
16499
09:47:29,360 --> 09:47:30,800
within our main class
16500
09:47:30,800 --> 09:47:33,680
so let's add that near the end and we'll
16501
09:47:33,680 --> 09:47:35,360
just say
16502
09:47:35,360 --> 09:47:39,276
while true so while this is running
16503
09:47:39,276 --> 09:47:43,040
let's move our volleyball so volleyball
16504
09:47:43,040 --> 09:47:45,840
dot move so this will call the move
16505
09:47:45,840 --> 09:47:48,160
function of our volleyball
16506
09:47:48,160 --> 09:47:51,436
and then we will update the window
16507
09:47:51,436 --> 09:47:54,960
window dot update so that this refreshes
16508
09:47:54,960 --> 09:47:57,276
and we will sleep for maybe a hundredth
16509
09:47:57,276 --> 09:47:58,560
of a second
16510
09:47:58,560 --> 09:48:02,720
time dot sleep 0.01 but you can put
16511
09:48:02,720 --> 09:48:04,320
whatever you want here
16512
09:48:04,320 --> 09:48:06,800
so this is going to at first just print
16513
09:48:06,800 --> 09:48:09,520
the coordinates of this ball
16514
09:48:09,520 --> 09:48:11,520
so it's not currently moving and this
16515
09:48:11,520 --> 09:48:13,276
time with our list we have
16516
09:48:13,276 --> 09:48:15,680
four coordinates zero zero that's the
16517
09:48:15,680 --> 09:48:18,480
top left corner and 100 100 that's the
16518
09:48:18,480 --> 09:48:18,960
bottom
16519
09:48:18,960 --> 09:48:21,680
right corner so this time we have four
16520
09:48:21,680 --> 09:48:23,200
coordinates that we have to deal with
16521
09:48:23,200 --> 09:48:25,120
the top left corner as well as the
16522
09:48:25,120 --> 09:48:26,560
bottom right corner
16523
09:48:26,560 --> 09:48:28,960
our next step is that we should move
16524
09:48:28,960 --> 09:48:30,560
this image
16525
09:48:30,560 --> 09:48:34,640
self dot canvas dot move
16526
09:48:34,640 --> 09:48:36,320
what object are we going to move we're
16527
09:48:36,320 --> 09:48:38,000
going to move self
16528
09:48:38,000 --> 09:48:41,916
dot image and in what directions
16529
09:48:41,916 --> 09:48:44,960
self.x velocity as well as
16530
09:48:44,960 --> 09:48:48,240
self dot y velocity now
16531
09:48:48,240 --> 09:48:50,000
our ball is going to move across the
16532
09:48:50,000 --> 09:48:52,000
window and it right now it's displaying
16533
09:48:52,000 --> 09:48:53,200
all of the coordinates
16534
09:48:53,200 --> 09:48:55,360
we don't have any bounds set up so it's
16535
09:48:55,360 --> 09:48:56,960
just going to continue into the void
16536
09:48:56,960 --> 09:48:59,276
forever so we should set up some if
16537
09:48:59,276 --> 09:49:01,200
statements to check to see if one of the
16538
09:49:01,200 --> 09:49:02,000
borders
16539
09:49:02,000 --> 09:49:04,160
touches one of the walls of the window
16540
09:49:04,160 --> 09:49:05,756
if so it's going to change
16541
09:49:05,756 --> 09:49:09,040
direction so let's check to see
16542
09:49:09,040 --> 09:49:11,596
if the left or right side of our ball
16543
09:49:11,596 --> 09:49:13,840
touches one of the left or right borders
16544
09:49:13,840 --> 09:49:17,200
so let's write an if statement if
16545
09:49:17,200 --> 09:49:18,400
and then let's take a look at these
16546
09:49:18,400 --> 09:49:20,880
coordinates if
16547
09:49:20,880 --> 09:49:24,320
coordinates at index 2
16548
09:49:24,320 --> 09:49:27,520
that is this one 0 1
16549
09:49:27,520 --> 09:49:31,040
2. so this is the bottom right corner
16550
09:49:31,040 --> 09:49:33,360
with these two sets of coordinates if
16551
09:49:33,360 --> 09:49:35,276
coordinates at index 2
16552
09:49:35,276 --> 09:49:38,320
is greater than or equal to
16553
09:49:38,320 --> 09:49:39,276
i'm going to put these within
16554
09:49:39,276 --> 09:49:41,840
parentheses self
16555
09:49:41,840 --> 09:49:44,320
dot canvas and we need to get the width
16556
09:49:44,320 --> 09:49:45,520
of the canvas
16557
09:49:45,520 --> 09:49:47,276
one way which we can do that is to use
16558
09:49:47,276 --> 09:49:49,040
the w
16559
09:49:49,040 --> 09:49:53,436
info underscore with function
16560
09:49:53,436 --> 09:49:55,520
so this is going to get the width of the
16561
09:49:55,520 --> 09:49:57,120
canvas
16562
09:49:57,120 --> 09:50:00,320
and then let's add one more thing
16563
09:50:00,320 --> 09:50:03,680
or coordinates at index
16564
09:50:03,680 --> 09:50:07,756
zero is less than zero
16565
09:50:07,756 --> 09:50:10,160
so if this ball touches the left or
16566
09:50:10,160 --> 09:50:11,276
right border
16567
09:50:11,276 --> 09:50:14,160
we want to change direction so we can
16568
09:50:14,160 --> 09:50:14,640
flip
16569
09:50:14,640 --> 09:50:18,800
the x velocity self.x velocity
16570
09:50:18,800 --> 09:50:22,080
equals negative self x
16571
09:50:22,080 --> 09:50:24,560
velocity and let's do the same thing for
16572
09:50:24,560 --> 09:50:26,880
the top and bottom borders
16573
09:50:26,880 --> 09:50:30,240
so let's copy what we have
16574
09:50:31,276 --> 09:50:33,840
and we'll replace coordinates at index
16575
09:50:33,840 --> 09:50:34,840
two with
16576
09:50:34,840 --> 09:50:38,560
three and we'll also replace
16577
09:50:38,560 --> 09:50:43,120
w info underscore width with height
16578
09:50:43,120 --> 09:50:46,320
and coordinates at index one
16579
09:50:46,320 --> 09:50:49,040
change self dot x velocity with y
16580
09:50:49,040 --> 09:50:50,720
velocity
16581
09:50:50,720 --> 09:50:54,160
same thing here as well alright and
16582
09:50:54,160 --> 09:50:57,840
that should be everything let's try it
16583
09:50:58,240 --> 09:50:59,916
so let's see if this ball actually
16584
09:50:59,916 --> 09:51:02,080
bounces off the borders
16585
09:51:02,080 --> 09:51:04,320
which it does pretty cool so that's
16586
09:51:04,320 --> 09:51:06,160
everything we need to do within our ball
16587
09:51:06,160 --> 09:51:06,960
class
16588
09:51:06,960 --> 09:51:09,276
now if we need to create multiple balls
16589
09:51:09,276 --> 09:51:10,960
we can just construct them
16590
09:51:10,960 --> 09:51:12,880
so let's say this time we would like a
16591
09:51:12,880 --> 09:51:14,400
tennis ball
16592
09:51:14,400 --> 09:51:16,400
i'm going to name this volleyball as
16593
09:51:16,400 --> 09:51:18,240
tennis ball and
16594
09:51:18,240 --> 09:51:20,240
let's make the diameter smaller like
16595
09:51:20,240 --> 09:51:22,480
five but we'll make this a lot faster
16596
09:51:22,480 --> 09:51:23,276
like
16597
09:51:23,276 --> 09:51:25,596
for the velocity let's say four and
16598
09:51:25,596 --> 09:51:26,880
three and the color
16599
09:51:26,880 --> 09:51:30,640
let's make this yellow and we also need
16600
09:51:30,640 --> 09:51:31,520
to call the
16601
09:51:31,520 --> 09:51:33,916
move function of this tennis ball so
16602
09:51:33,916 --> 09:51:34,960
within the while loop
16603
09:51:34,960 --> 09:51:38,240
tennis ball dot move and then we should
16604
09:51:38,240 --> 09:51:40,160
also have a tennis ball moving across
16605
09:51:40,160 --> 09:51:40,720
the screen
16606
09:51:40,720 --> 09:51:43,040
in a different direction now technically
16607
09:51:43,040 --> 09:51:44,720
we don't need to print the coordinates
16608
09:51:44,720 --> 09:51:46,480
but i tend to do that just for
16609
09:51:46,480 --> 09:51:48,720
teaching purposes to better demonstrate
16610
09:51:48,720 --> 09:51:49,916
all of this
16611
09:51:49,916 --> 09:51:52,240
now our ball class is acting as a
16612
09:51:52,240 --> 09:51:53,200
blueprint
16613
09:51:53,200 --> 09:51:55,756
for how balls should be created and the
16614
09:51:55,756 --> 09:51:57,680
behavior that they exhibit
16615
09:51:57,680 --> 09:51:59,756
and if we need to create a ball we can
16616
09:51:59,756 --> 09:52:01,840
just come up with a name for the ball
16617
09:52:01,840 --> 09:52:03,916
and call the ball constructor to take
16618
09:52:03,916 --> 09:52:05,040
care of that for us
16619
09:52:05,040 --> 09:52:06,640
kind of like what we've been doing with
16620
09:52:06,640 --> 09:52:09,200
widgets so all we need to do to create
16621
09:52:09,200 --> 09:52:10,080
another ball
16622
09:52:10,080 --> 09:52:11,916
is come up with a unique name for the
16623
09:52:11,916 --> 09:52:13,756
ball and instantiate it using
16624
09:52:13,756 --> 09:52:15,916
our ball class that we created so this
16625
09:52:15,916 --> 09:52:17,120
time let's create
16626
09:52:17,120 --> 09:52:19,840
a basketball i'll rename this as
16627
09:52:19,840 --> 09:52:20,960
basketball
16628
09:52:20,960 --> 09:52:22,880
and we'll pass in different arguments
16629
09:52:22,880 --> 09:52:25,040
based on how we want to customize
16630
09:52:25,040 --> 09:52:28,160
this unique ball so for the diameter
16631
09:52:28,160 --> 09:52:30,480
let's say this is now 125
16632
09:52:30,480 --> 09:52:32,960
and the x and y velocity let's set this
16633
09:52:32,960 --> 09:52:35,436
to something very fast like
16634
09:52:35,436 --> 09:52:38,720
eight and seven and with the color let's
16635
09:52:38,720 --> 09:52:40,160
change this to orange
16636
09:52:40,160 --> 09:52:42,160
and make sure you also call the move
16637
09:52:42,160 --> 09:52:44,080
function
16638
09:52:44,080 --> 09:52:47,276
so basketball dot move and we should
16639
09:52:47,276 --> 09:52:49,120
have a basketball moving across the
16640
09:52:49,120 --> 09:52:50,880
screen
16641
09:52:50,880 --> 09:52:53,040
all right everybody so that's the basics
16642
09:52:53,040 --> 09:52:55,596
of animating multiple objects in python
16643
09:52:55,596 --> 09:52:56,000
if you want
16644
09:52:56,000 --> 09:52:58,000
a copy of all this code i'll post all of
16645
09:52:58,000 --> 09:52:59,680
this in the comments down below
16646
09:52:59,680 --> 09:53:01,756
but yeah that's how to animate multiple
16647
09:53:01,756 --> 09:53:04,960
objects in python
16648
09:53:04,960 --> 09:53:06,880
hey what's going on everybody it's ibro
16649
09:53:06,880 --> 09:53:08,400
here hope you're doing well and in this
16650
09:53:08,400 --> 09:53:09,840
video i'm going to teach you guys how we
16651
09:53:09,840 --> 09:53:12,240
can create a clock program in python so
16652
09:53:12,240 --> 09:53:16,080
sit back relax and enjoy the show
16653
09:53:16,320 --> 09:53:18,560
all right my fellow bros in this video
16654
09:53:18,560 --> 09:53:20,240
we're going to be creating a clock
16655
09:53:20,240 --> 09:53:22,880
gui program so we'll need a few imports
16656
09:53:22,880 --> 09:53:23,436
from
16657
09:53:23,436 --> 09:53:27,276
tk enter import everything
16658
09:53:27,276 --> 09:53:31,120
and from time import everything as well
16659
09:53:31,120 --> 09:53:33,520
we'll need a window window equals tk
16660
09:53:33,520 --> 09:53:35,596
followed by window.main loop
16661
09:53:35,596 --> 09:53:37,200
and in this program we're going to be
16662
09:53:37,200 --> 09:53:39,040
creating three separate labels
16663
09:53:39,040 --> 09:53:41,120
one for the time another for the day of
16664
09:53:41,120 --> 09:53:43,596
the week and another for the date
16665
09:53:43,596 --> 09:53:45,520
so let's begin with just displaying the
16666
09:53:45,520 --> 09:53:47,520
time and work on the day of the week and
16667
09:53:47,520 --> 09:53:49,040
the date later
16668
09:53:49,040 --> 09:53:51,120
so let's create a label for the time
16669
09:53:51,120 --> 09:53:52,480
called time
16670
09:53:52,480 --> 09:53:55,596
label equals label we're adding this to
16671
09:53:55,596 --> 09:53:56,640
the window
16672
09:53:56,640 --> 09:53:59,120
and let's set the font because we can
16673
09:53:59,120 --> 09:54:00,480
i'm going to choose
16674
09:54:00,480 --> 09:54:03,680
maybe ariel and then a size of 50 so
16675
09:54:03,680 --> 09:54:05,276
that this is somewhat large
16676
09:54:05,276 --> 09:54:07,120
i'm going to change the foreground color
16677
09:54:07,120 --> 09:54:09,360
that is the font color
16678
09:54:09,360 --> 09:54:12,400
and i will pick a hex value of green so
16679
09:54:12,400 --> 09:54:12,880
that is
16680
09:54:12,880 --> 09:54:16,800
0 0 f 0 0 but feel free to pick whatever
16681
09:54:16,800 --> 09:54:17,756
color you want
16682
09:54:17,756 --> 09:54:19,360
and i'm also going to set the background
16683
09:54:19,360 --> 09:54:21,916
color and you can pick a hex value or a
16684
09:54:21,916 --> 09:54:24,640
color name i'm just going to say black
16685
09:54:24,640 --> 09:54:27,840
and then i'm going to pack this label
16686
09:54:27,840 --> 09:54:32,480
time label dot pack
16687
09:54:32,480 --> 09:54:35,360
now we're going to call a function and
16688
09:54:35,360 --> 09:54:37,200
we'll call this update
16689
09:54:37,200 --> 09:54:39,120
and we'll define what this does after
16690
09:54:39,120 --> 09:54:40,560
every second has passed
16691
09:54:40,560 --> 09:54:43,520
we will use a recursive function to keep
16692
09:54:43,520 --> 09:54:44,480
on updating the
16693
09:54:44,480 --> 09:54:47,520
time every one second or so so let's
16694
09:54:47,520 --> 09:54:49,120
define what this function does
16695
09:54:49,120 --> 09:54:52,640
def update
16696
09:54:52,720 --> 09:54:54,720
there is a function within the time
16697
09:54:54,720 --> 09:54:57,120
module called strf time
16698
09:54:57,120 --> 09:54:59,200
which we can use to return the current
16699
09:54:59,200 --> 09:55:01,520
time and format it however we want
16700
09:55:01,520 --> 09:55:03,840
so we're going to actually place this
16701
09:55:03,840 --> 09:55:05,916
within a string variable and we'll call
16702
09:55:05,916 --> 09:55:06,880
this time
16703
09:55:06,880 --> 09:55:10,720
string and this function is strf
16704
09:55:10,720 --> 09:55:12,320
time i'm actually going to go to
16705
09:55:12,320 --> 09:55:14,240
python's website just to give you an
16706
09:55:14,240 --> 09:55:15,756
example of how this works
16707
09:55:15,756 --> 09:55:18,400
here we are on python's website and this
16708
09:55:18,400 --> 09:55:18,960
is the
16709
09:55:18,960 --> 09:55:22,560
strf time function and this converts a
16710
09:55:22,560 --> 09:55:26,080
tuple representing a time as returned by
16711
09:55:26,080 --> 09:55:29,680
gm time or local time to a string
16712
09:55:29,680 --> 09:55:32,880
as specified by the format arguments
16713
09:55:32,880 --> 09:55:35,360
so when we call this function we can
16714
09:55:35,360 --> 09:55:36,160
pass in
16715
09:55:36,160 --> 09:55:39,040
directives as arguments based on what we
16716
09:55:39,040 --> 09:55:40,400
want to display
16717
09:55:40,400 --> 09:55:42,880
if we want to display the current time
16718
09:55:42,880 --> 09:55:44,800
there is a few directives that we can
16719
09:55:44,800 --> 09:55:45,680
pass in
16720
09:55:45,680 --> 09:55:48,160
so let's say that we want a 12 hour
16721
09:55:48,160 --> 09:55:50,640
clock as represented by a decimal number
16722
09:55:50,640 --> 09:55:51,276
between
16723
09:55:51,276 --> 09:55:54,640
1 and 12 well we would pass in percent
16724
09:55:54,640 --> 09:55:57,436
i as an argument if we want the minutes
16725
09:55:57,436 --> 09:55:58,400
that would be
16726
09:55:58,400 --> 09:56:01,200
right here percent m and if we want the
16727
09:56:01,200 --> 09:56:02,320
seconds that is
16728
09:56:02,320 --> 09:56:05,200
percent s so we're going to pass in
16729
09:56:05,200 --> 09:56:06,400
these directives
16730
09:56:06,400 --> 09:56:09,840
as if they are arguments so within
16731
09:56:09,840 --> 09:56:13,360
this function we'll pass in those format
16732
09:56:13,360 --> 09:56:16,880
specifiers or directives so i want
16733
09:56:16,880 --> 09:56:20,160
percent i followed by
16734
09:56:20,160 --> 09:56:23,916
percent m and percent
16735
09:56:23,916 --> 09:56:27,200
s alright uh but these also have to be
16736
09:56:27,200 --> 09:56:28,880
within quotes so don't forget that
16737
09:56:28,880 --> 09:56:31,840
because i just did
16738
09:56:32,160 --> 09:56:35,360
okay this will display the current time
16739
09:56:35,360 --> 09:56:38,800
oh then we also have to update our time
16740
09:56:38,800 --> 09:56:39,436
label so
16741
09:56:39,436 --> 09:56:44,560
time label dot config
16742
09:56:44,640 --> 09:56:47,840
text equals our time string because
16743
09:56:47,840 --> 09:56:48,960
remember that this
16744
09:56:48,960 --> 09:56:52,160
returns a string of text
16745
09:56:52,160 --> 09:56:54,880
text equals time string so this will
16746
09:56:54,880 --> 09:56:56,880
display the current time
16747
09:56:56,880 --> 09:56:59,120
and right now it is six in the morning
16748
09:56:59,120 --> 09:56:59,916
or so
16749
09:56:59,916 --> 09:57:02,720
however we just get one long jumbled
16750
09:57:02,720 --> 09:57:03,436
number
16751
09:57:03,436 --> 09:57:05,200
so it would be a good idea if we
16752
09:57:05,200 --> 09:57:07,040
separated some of these numbers
16753
09:57:07,040 --> 09:57:10,080
so within our strf time function i'm
16754
09:57:10,080 --> 09:57:11,596
just going to add some colons to
16755
09:57:11,596 --> 09:57:14,800
separate these values
16756
09:57:15,680 --> 09:57:17,360
and this will display the current time
16757
09:57:17,360 --> 09:57:18,720
it is currently 6
16758
09:57:18,720 --> 09:57:22,240
46 in the morning let's add am or pm to
16759
09:57:22,240 --> 09:57:23,120
the end of this
16760
09:57:23,120 --> 09:57:26,160
so the directive for that is percent
16761
09:57:26,160 --> 09:57:28,800
lowercase p
16762
09:57:29,276 --> 09:57:33,040
6 46 am now how do we update this label
16763
09:57:33,040 --> 09:57:35,200
every one second to reflect the current
16764
09:57:35,200 --> 09:57:35,596
time
16765
09:57:35,596 --> 09:57:37,120
well that's a good question and i'm glad
16766
09:57:37,120 --> 09:57:39,276
you asked it so we're going to use
16767
09:57:39,276 --> 09:57:42,560
the time labels after function
16768
09:57:42,560 --> 09:57:45,276
we pass in a delay and then a function
16769
09:57:45,276 --> 09:57:46,400
that we want to call
16770
09:57:46,400 --> 09:57:49,520
after that delay so let's say we want to
16771
09:57:49,520 --> 09:57:52,400
call this function update every 1000
16772
09:57:52,400 --> 09:57:53,360
milliseconds
16773
09:57:53,360 --> 09:57:56,240
or one second so we'll pass in the delay
16774
09:57:56,240 --> 09:57:57,360
as an argument
16775
09:57:57,360 --> 09:57:59,840
1000 milliseconds and the function that
16776
09:57:59,840 --> 09:58:00,960
we want to call
16777
09:58:00,960 --> 09:58:02,960
and we're going to perform what is known
16778
09:58:02,960 --> 09:58:04,000
as a recursive
16779
09:58:04,000 --> 09:58:07,120
function we're going to call a function
16780
09:58:07,120 --> 09:58:10,240
within itself so after 1000 seconds
16781
09:58:10,240 --> 09:58:11,120
we're going to call
16782
09:58:11,120 --> 09:58:12,720
update again and it's going to repeat
16783
09:58:12,720 --> 09:58:14,880
the process it's going to return the
16784
09:58:14,880 --> 09:58:15,680
time
16785
09:58:15,680 --> 09:58:18,880
and format that time and update
16786
09:58:18,880 --> 09:58:21,756
our label with the new time so what ends
16787
09:58:21,756 --> 09:58:22,880
up happening now
16788
09:58:22,880 --> 09:58:25,840
is that our clock is going to update
16789
09:58:25,840 --> 09:58:28,560
every 1000 milliseconds
16790
09:58:28,560 --> 09:58:30,880
now let's add a few other labels let's
16791
09:58:30,880 --> 09:58:32,240
say the day of the week
16792
09:58:32,240 --> 09:58:34,880
i'm going to copy what we have here and
16793
09:58:34,880 --> 09:58:36,320
let's rename this as
16794
09:58:36,320 --> 09:58:38,960
day label
16795
09:58:40,800 --> 09:58:42,480
but i probably don't want this to be the
16796
09:58:42,480 --> 09:58:44,960
same size for the font let's say
16797
09:58:44,960 --> 09:58:48,080
ink free because i like that font and a
16798
09:58:48,080 --> 09:58:50,480
size of maybe 25 and i'll get rid of the
16799
09:58:50,480 --> 09:58:53,040
coloring
16800
09:58:53,756 --> 09:58:56,880
it's the same process as before really
16801
09:58:56,880 --> 09:59:00,160
so let's replace
16802
09:59:00,160 --> 09:59:04,240
time string with day string
16803
09:59:05,520 --> 09:59:09,916
the directive for strf time to display
16804
09:59:09,916 --> 09:59:11,276
the day of the week
16805
09:59:11,276 --> 09:59:14,960
is percent a
16806
09:59:14,960 --> 09:59:17,840
and we're going to replace time label
16807
09:59:17,840 --> 09:59:18,960
with
16808
09:59:18,960 --> 09:59:23,840
day label text equals day string
16809
09:59:23,840 --> 09:59:25,436
and this will display the day of the
16810
09:59:25,436 --> 09:59:27,276
week and it is currently
16811
09:59:27,276 --> 09:59:31,520
wednesday and if we want to display
16812
09:59:31,520 --> 09:59:34,960
the current date we can create a label
16813
09:59:34,960 --> 09:59:36,000
and we'll call this
16814
09:59:36,000 --> 09:59:38,800
date label
16815
09:59:40,160 --> 09:59:41,680
and i'll make the font slightly bigger
16816
09:59:41,680 --> 09:59:44,560
maybe 30.
16817
09:59:44,800 --> 09:59:46,560
we'll copy what we have for our day
16818
09:59:46,560 --> 09:59:48,000
string
16819
09:59:48,000 --> 09:59:52,320
and rename this as date string
16820
09:59:54,480 --> 09:59:56,560
so we have a few options for the date i
16821
09:59:56,560 --> 09:59:58,160
want to display
16822
09:59:58,160 --> 10:00:00,640
the name of the month the day and then
16823
10:00:00,640 --> 10:00:01,436
the year
16824
10:00:01,436 --> 10:00:06,080
so that would be percent capital b
16825
10:00:06,080 --> 10:00:09,360
percent lowercase d i'm going to add a
16826
10:00:09,360 --> 10:00:12,000
comma to separate the day and the year
16827
10:00:12,000 --> 10:00:15,436
and percent capital y
16828
10:00:15,436 --> 10:00:17,840
and then i want to set the text of my
16829
10:00:17,840 --> 10:00:18,560
date label
16830
10:00:18,560 --> 10:00:22,080
to my date string so that this will now
16831
10:00:22,080 --> 10:00:24,640
display the current date there's just
16832
10:00:24,640 --> 10:00:26,320
one quick change that i think we should
16833
10:00:26,320 --> 10:00:26,960
make
16834
10:00:26,960 --> 10:00:28,800
right now we have three different labels
16835
10:00:28,800 --> 10:00:30,720
a time label a day label
16836
10:00:30,720 --> 10:00:33,276
and a date label and right now we're
16837
10:00:33,276 --> 10:00:35,360
using the after function of the time
16838
10:00:35,360 --> 10:00:36,160
label
16839
10:00:36,160 --> 10:00:37,840
so since we have all of these different
16840
10:00:37,840 --> 10:00:39,756
labels i think we should replace time
16841
10:00:39,756 --> 10:00:40,480
label
16842
10:00:40,480 --> 10:00:42,800
with our window because our window has
16843
10:00:42,800 --> 10:00:44,480
an after function as well
16844
10:00:44,480 --> 10:00:47,120
so that after 1000 milliseconds has
16845
10:00:47,120 --> 10:00:47,680
passed
16846
10:00:47,680 --> 10:00:50,160
we can update the time the day of the
16847
10:00:50,160 --> 10:00:50,720
week
16848
10:00:50,720 --> 10:00:53,200
and the date all right so that's the
16849
10:00:53,200 --> 10:00:55,276
basics of creating a simple clock
16850
10:00:55,276 --> 10:00:57,436
program in python if you want a copy of
16851
10:00:57,436 --> 10:00:58,160
all this code
16852
10:00:58,160 --> 10:00:59,596
i'll post all of this in the comments
16853
10:00:59,596 --> 10:01:01,680
down below but yeah that's the basics of
16854
10:01:01,680 --> 10:01:04,080
creating a clock in python
16855
10:01:04,080 --> 10:01:06,000
hey what's going on everybody it's you
16856
10:01:06,000 --> 10:01:07,200
bro hope you're doing well
16857
10:01:07,200 --> 10:01:08,640
and in this video i'm going to show you
16858
10:01:08,640 --> 10:01:10,400
all how we can send an email using
16859
10:01:10,400 --> 10:01:11,276
python so
16860
10:01:11,276 --> 10:01:15,840
sit back relax and enjoy the show
16861
10:01:16,080 --> 10:01:17,840
oh a quick note for you all you're going
16862
10:01:17,840 --> 10:01:20,000
to need a gmail account to sign into
16863
10:01:20,000 --> 10:01:22,640
as well as your username and password so
16864
10:01:22,640 --> 10:01:24,240
once you have those set you're ready to
16865
10:01:24,240 --> 10:01:24,800
begin
16866
10:01:24,800 --> 10:01:27,120
all right let's begin by importing the
16867
10:01:27,120 --> 10:01:28,160
simple mail
16868
10:01:28,160 --> 10:01:31,276
transfer protocol library we'll need a
16869
10:01:31,276 --> 10:01:32,880
couple variables to hold strings
16870
10:01:32,880 --> 10:01:37,680
let's say we have a sender a receiver
16871
10:01:37,680 --> 10:01:40,480
a password
16872
10:01:40,960 --> 10:01:44,240
a subject for your email as well as
16873
10:01:44,240 --> 10:01:46,880
a body for the email okay sender is
16874
10:01:46,880 --> 10:01:47,520
going to be you
16875
10:01:47,520 --> 10:01:49,840
sending the email so enter your email in
16876
10:01:49,840 --> 10:01:51,596
here i'm just going to make up one
16877
10:01:51,596 --> 10:01:54,640
you at gmail.com
16878
10:01:54,640 --> 10:01:56,080
actually sender would be better i would
16879
10:01:56,080 --> 10:01:58,080
say let me change that sender at
16880
10:01:58,080 --> 10:01:59,360
gmail.com
16881
10:01:59,360 --> 10:02:01,360
let's say we have a receiver this is who
16882
10:02:01,360 --> 10:02:04,080
you're going to send the email to
16883
10:02:04,080 --> 10:02:07,120
receiver gmail.com
16884
10:02:07,120 --> 10:02:09,040
don't actually enter in these emails use
16885
10:02:09,040 --> 10:02:10,720
your owner the person that you intend to
16886
10:02:10,720 --> 10:02:12,480
send the email to for your test
16887
10:02:12,480 --> 10:02:14,720
because these will probably not work i'm
16888
10:02:14,720 --> 10:02:15,520
assuming
16889
10:02:15,520 --> 10:02:17,680
then enter the password for your gmail
16890
10:02:17,680 --> 10:02:20,680
account so let's say
16891
10:02:20,680 --> 10:02:23,916
password123 and a subject
16892
10:02:23,916 --> 10:02:27,436
uh let's say python email
16893
10:02:27,436 --> 10:02:30,800
test and a body what do you want to say
16894
10:02:30,800 --> 10:02:32,960
within your email
16895
10:02:32,960 --> 10:02:36,320
i wrote an email
16896
10:02:36,320 --> 10:02:38,720
okay so that should be good so remember
16897
10:02:38,720 --> 10:02:40,240
that you're going to want to change
16898
10:02:40,240 --> 10:02:41,436
these to your actual
16899
10:02:41,436 --> 10:02:43,520
gmail account and you'll want to send it
16900
10:02:43,520 --> 10:02:45,520
to a valid email address too
16901
10:02:45,520 --> 10:02:47,520
and make sure that you use your password
16902
10:02:47,520 --> 10:02:49,200
for your gmail account
16903
10:02:49,200 --> 10:02:51,040
okay now we're going to create a header
16904
10:02:51,040 --> 10:02:53,436
for our email and the syntax on this is
16905
10:02:53,436 --> 10:02:55,040
a little strange we're going to say
16906
10:02:55,040 --> 10:02:56,160
message
16907
10:02:56,160 --> 10:02:59,120
f and then triple quotes so what this
16908
10:02:59,120 --> 10:03:00,160
triple quotes is
16909
10:03:00,160 --> 10:03:03,520
is that a triple quote string can span
16910
10:03:03,520 --> 10:03:06,400
multiple lines of text so the first
16911
10:03:06,400 --> 10:03:07,680
thing we'll enter is
16912
10:03:07,680 --> 10:03:10,960
from within our header from and then
16913
10:03:10,960 --> 10:03:12,400
since we're using an f-string we can
16914
10:03:12,400 --> 10:03:13,200
insert
16915
10:03:13,200 --> 10:03:16,080
a variable at a given location when we
16916
10:03:16,080 --> 10:03:16,880
insert
16917
10:03:16,880 --> 10:03:20,160
a pair of these curly braces so from
16918
10:03:20,160 --> 10:03:23,596
will be sender the next line
16919
10:03:23,596 --> 10:03:27,680
to colon
16920
10:03:27,680 --> 10:03:30,800
receiver then the next line
16921
10:03:30,800 --> 10:03:34,240
subject colon
16922
10:03:34,720 --> 10:03:36,640
subject this will be the title of the
16923
10:03:36,640 --> 10:03:39,680
email then i'm going to add a new line
16924
10:03:39,680 --> 10:03:43,916
and then insert the body
16925
10:03:43,916 --> 10:03:45,756
and if you want you can give yourself a
16926
10:03:45,756 --> 10:03:47,436
name when you send this email
16927
10:03:47,436 --> 10:03:50,480
let's say maybe snoop dogg and if
16928
10:03:50,480 --> 10:03:51,916
there's somebody you want to send the
16929
10:03:51,916 --> 10:03:52,640
email to
16930
10:03:52,640 --> 10:03:55,360
and give them a name you can say like
16931
10:03:55,360 --> 10:03:56,640
nicholas
16932
10:03:56,640 --> 10:03:59,916
cage but these aren't necessary
16933
10:03:59,916 --> 10:04:02,160
okay so we have our header completed let
16934
10:04:02,160 --> 10:04:03,680
me just add a note that this is our
16935
10:04:03,680 --> 10:04:05,436
header
16936
10:04:05,436 --> 10:04:07,840
okay we're going to create a server
16937
10:04:07,840 --> 10:04:08,640
object
16938
10:04:08,640 --> 10:04:17,276
server equals smtplib.smtp
16939
10:04:17,276 --> 10:04:20,840
there are two arguments the first is
16940
10:04:20,840 --> 10:04:23,840
smtp
16941
10:04:25,680 --> 10:04:28,000
the other argument is the port number
16942
10:04:28,000 --> 10:04:29,480
and that is going to be
16943
10:04:29,480 --> 10:04:32,640
587 that is the default mail submission
16944
10:04:32,640 --> 10:04:33,120
port
16945
10:04:33,120 --> 10:04:36,320
587 now we're going to take our server
16946
10:04:36,320 --> 10:04:37,040
object
16947
10:04:37,040 --> 10:04:42,880
and start tls transport layer security
16948
10:04:42,880 --> 10:04:46,400
okay now we need to log in so server
16949
10:04:46,400 --> 10:04:50,320
dot login we will pass in
16950
10:04:50,320 --> 10:04:53,436
our sender email that's us as well as
16951
10:04:53,436 --> 10:04:56,080
our password
16952
10:04:56,880 --> 10:05:02,000
password and let's print
16953
10:05:02,880 --> 10:05:06,080
logged in
16954
10:05:06,080 --> 10:05:08,320
we'll eventually place all this within a
16955
10:05:08,320 --> 10:05:10,720
try and accept block later
16956
10:05:10,720 --> 10:05:13,756
all right and then to send an email type
16957
10:05:13,756 --> 10:05:17,040
server dot send mail
16958
10:05:17,040 --> 10:05:20,160
and we pass in our self sender
16959
10:05:20,160 --> 10:05:23,840
receiver and our message
16960
10:05:23,840 --> 10:05:27,360
sender comma receiver
16961
10:05:27,360 --> 10:05:31,040
comma message and let's print a
16962
10:05:31,040 --> 10:05:32,160
confirmation
16963
10:05:32,160 --> 10:05:36,000
print email has
16964
10:05:36,000 --> 10:05:39,520
been sent now if you were to run this
16965
10:05:39,520 --> 10:05:40,080
program
16966
10:05:40,080 --> 10:05:43,520
you may encounter this error smtp
16967
10:05:43,520 --> 10:05:45,840
authentication error that either means
16968
10:05:45,840 --> 10:05:46,800
your username
16969
10:05:46,800 --> 10:05:49,680
password combination was not correct or
16970
10:05:49,680 --> 10:05:50,960
you need to turn on
16971
10:05:50,960 --> 10:05:53,840
the less secure app access on your gmail
16972
10:05:53,840 --> 10:05:54,400
account
16973
10:05:54,400 --> 10:05:56,720
which you can find here however i would
16974
10:05:56,720 --> 10:05:58,480
recommend turning that back off when
16975
10:05:58,480 --> 10:05:59,756
you're done with this program just
16976
10:05:59,756 --> 10:06:00,080
because
16977
10:06:00,080 --> 10:06:02,080
your account will be less secure and i'm
16978
10:06:02,080 --> 10:06:03,680
not liable if you get hacked hey you
16979
10:06:03,680 --> 10:06:04,240
know what
16980
10:06:04,240 --> 10:06:06,800
let's place this code within a try block
16981
10:06:06,800 --> 10:06:07,916
we will try
16982
10:06:07,916 --> 10:06:10,320
all of this code and then if we
16983
10:06:10,320 --> 10:06:10,960
encounter
16984
10:06:10,960 --> 10:06:13,200
this exception we can catch and handle
16985
10:06:13,200 --> 10:06:14,400
it properly
16986
10:06:14,400 --> 10:06:17,120
so we will try these four lines of code
16987
10:06:17,120 --> 10:06:18,320
and we will handle
16988
10:06:18,320 --> 10:06:21,880
our exception of smt lib dot
16989
10:06:21,880 --> 10:06:25,276
smtp authentication error
16990
10:06:25,276 --> 10:06:27,360
that means we could not sign in so let's
16991
10:06:27,360 --> 10:06:28,880
print
16992
10:06:28,880 --> 10:06:32,320
unable to sign in
16993
10:06:32,320 --> 10:06:34,400
so i'm going to run this one more time
16994
10:06:34,400 --> 10:06:36,080
and instead of getting that ugly error
16995
10:06:36,080 --> 10:06:38,320
we should get a simple message that says
16996
10:06:38,320 --> 10:06:40,320
unable to sign in once you have the
16997
10:06:40,320 --> 10:06:42,560
proper username password combination for
16998
10:06:42,560 --> 10:06:43,916
your gmail account
16999
10:06:43,916 --> 10:06:46,480
and if you need to set less secure app
17000
10:06:46,480 --> 10:06:47,436
access to on
17001
10:06:47,436 --> 10:06:49,040
but make sure that you turn it back off
17002
10:06:49,040 --> 10:06:50,720
when you're done with this you should be
17003
10:06:50,720 --> 10:06:51,916
able to send an email
17004
10:06:51,916 --> 10:06:54,480
let's try it logged in email has been
17005
10:06:54,480 --> 10:06:55,040
sent
17006
10:06:55,040 --> 10:06:57,520
here is a snapshot of the email that i
17007
10:06:57,520 --> 10:06:58,160
received
17008
10:06:58,160 --> 10:07:00,560
i blurred out my actual email because i
17009
10:07:00,560 --> 10:07:01,916
don't want it public because
17010
10:07:01,916 --> 10:07:04,160
some of you guys are weird so we have
17011
10:07:04,160 --> 10:07:05,276
our sender
17012
10:07:05,276 --> 10:07:07,596
our receiver and i gave myself a
17013
10:07:07,596 --> 10:07:09,360
nickname of snoop dogg that's why it
17014
10:07:09,360 --> 10:07:10,560
says snoop
17015
10:07:10,560 --> 10:07:12,800
to nicholas the receiver but you don't
17016
10:07:12,800 --> 10:07:14,720
actually need a name for these as long
17017
10:07:14,720 --> 10:07:16,560
as you have the sender and receiver
17018
10:07:16,560 --> 10:07:18,480
fields filled in you should be good
17019
10:07:18,480 --> 10:07:21,680
we have our subject python email test
17020
10:07:21,680 --> 10:07:24,560
as well as our body i wrote an email
17021
10:07:24,560 --> 10:07:26,560
well everybody that's how to write and
17022
10:07:26,560 --> 10:07:28,960
send a simple email using python if you
17023
10:07:28,960 --> 10:07:30,560
would like a copy of this code i will
17024
10:07:30,560 --> 10:07:32,080
post this to the comment section down
17025
10:07:32,080 --> 10:07:32,800
below
17026
10:07:32,800 --> 10:07:34,800
but yeah that's how to send a simple
17027
10:07:34,800 --> 10:07:37,276
email using python
17028
10:07:37,276 --> 10:07:39,200
hey what's going on everybody it's you
17029
10:07:39,200 --> 10:07:40,400
bro hope you're doing well
17030
10:07:40,400 --> 10:07:41,916
and in this video i'm going to show you
17031
10:07:41,916 --> 10:07:44,160
all how we can run a python file using
17032
10:07:44,160 --> 10:07:45,360
command prompt so
17033
10:07:45,360 --> 10:07:50,080
sit back relax and enjoy the show
17034
10:07:50,240 --> 10:07:52,080
hey y'all in today's video i'm going to
17035
10:07:52,080 --> 10:07:53,916
show you all how we can run a python
17036
10:07:53,916 --> 10:07:55,520
file with command prompt
17037
10:07:55,520 --> 10:07:57,120
so first we'll need a script to work
17038
10:07:57,120 --> 10:07:59,276
with let's just make a very simple
17039
10:07:59,276 --> 10:08:00,080
program
17040
10:08:00,080 --> 10:08:03,360
let's say print hello world and then
17041
10:08:03,360 --> 10:08:04,480
maybe we will ask
17042
10:08:04,480 --> 10:08:08,560
for some user input name equals input
17043
10:08:08,560 --> 10:08:12,480
what's your name and then at the end
17044
10:08:12,480 --> 10:08:16,880
we will print the word hello
17045
10:08:16,880 --> 10:08:20,160
plus the user's name okay so
17046
10:08:20,160 --> 10:08:22,080
let's save this to a place that will
17047
10:08:22,080 --> 10:08:23,200
remember perhaps
17048
10:08:23,200 --> 10:08:25,756
my desktop now to save this file to a
17049
10:08:25,756 --> 10:08:27,360
separate location go to
17050
10:08:27,360 --> 10:08:30,720
file save as you can pick a name for
17051
10:08:30,720 --> 10:08:32,000
this python file
17052
10:08:32,000 --> 10:08:34,640
then to directory click these three dots
17053
10:08:34,640 --> 10:08:36,480
and pick a location you want to save
17054
10:08:36,480 --> 10:08:37,276
this to
17055
10:08:37,276 --> 10:08:39,040
to make this easy i'm going to save this
17056
10:08:39,040 --> 10:08:40,880
python file to my desktop
17057
10:08:40,880 --> 10:08:43,680
and then click ok so the name of this
17058
10:08:43,680 --> 10:08:44,880
python file of mine
17059
10:08:44,880 --> 10:08:48,160
is hello underscore world dot py then
17060
10:08:48,160 --> 10:08:49,520
click ok
17061
10:08:49,520 --> 10:08:51,756
now our second step is to open command
17062
10:08:51,756 --> 10:08:53,916
prompt to open command prompt just
17063
10:08:53,916 --> 10:08:54,720
search for
17064
10:08:54,720 --> 10:08:57,840
cmd if you're using mac i believe it's
17065
10:08:57,840 --> 10:08:58,880
terminal
17066
10:08:58,880 --> 10:09:00,560
step number three is that we need to
17067
10:09:00,560 --> 10:09:02,480
navigate to the directory that contains
17068
10:09:02,480 --> 10:09:05,200
our python file i saved this python file
17069
10:09:05,200 --> 10:09:06,240
to my desktop
17070
10:09:06,240 --> 10:09:08,320
so if you right click on your file and
17071
10:09:08,320 --> 10:09:10,400
go to location you can just copy this
17072
10:09:10,400 --> 10:09:11,360
address
17073
10:09:11,360 --> 10:09:13,916
now within command prompt type cd to
17074
10:09:13,916 --> 10:09:15,120
change directory
17075
10:09:15,120 --> 10:09:18,240
space and then paste that directory
17076
10:09:18,240 --> 10:09:21,276
that contains your python file hit enter
17077
10:09:21,276 --> 10:09:23,436
and for the last step we need to invoke
17078
10:09:23,436 --> 10:09:25,840
the python interpreter by typing
17079
10:09:25,840 --> 10:09:29,120
python space and then
17080
10:09:29,120 --> 10:09:31,040
the name of the script and the name of
17081
10:09:31,040 --> 10:09:32,800
my script is hello
17082
10:09:32,800 --> 10:09:36,400
underscore world dot p y hit enter to
17083
10:09:36,400 --> 10:09:37,680
run the script
17084
10:09:37,680 --> 10:09:40,880
hello world what is your name bro hello
17085
10:09:40,880 --> 10:09:41,436
bro
17086
10:09:41,436 --> 10:09:43,840
so that is how to run a python file with
17087
10:09:43,840 --> 10:09:45,840
command prompt i will post all of these
17088
10:09:45,840 --> 10:09:47,680
notes to the comments section down below
17089
10:09:47,680 --> 10:09:48,400
and well
17090
10:09:48,400 --> 10:09:50,400
yeah that's how to run a python file
17091
10:09:50,400 --> 10:09:52,400
with command prompt
17092
10:09:52,400 --> 10:09:54,240
it's you bro hope you're all doing well
17093
10:09:54,240 --> 10:09:55,756
and in this video i'm going to show you
17094
10:09:55,756 --> 10:09:58,000
all how we can use pip for python to
17095
10:09:58,000 --> 10:10:00,560
download packages and modules so sit
17096
10:10:00,560 --> 10:10:01,120
back
17097
10:10:01,120 --> 10:10:04,720
relax and enjoy the show
17098
10:10:05,040 --> 10:10:08,080
alright people pip for python pip is a
17099
10:10:08,080 --> 10:10:10,960
package manager for packages and modules
17100
10:10:10,960 --> 10:10:13,916
from the python package index if you're
17101
10:10:13,916 --> 10:10:14,400
using
17102
10:10:14,400 --> 10:10:17,680
python version 3.4 or above pip should
17103
10:10:17,680 --> 10:10:18,960
already be installed
17104
10:10:18,960 --> 10:10:21,436
if you're using a python version below
17105
10:10:21,436 --> 10:10:22,400
3.4
17106
10:10:22,400 --> 10:10:24,480
one option is that you can always update
17107
10:10:24,480 --> 10:10:26,480
your version of python just click
17108
10:10:26,480 --> 10:10:30,160
download open when done
17109
10:10:30,160 --> 10:10:32,720
go to customize installation make sure
17110
10:10:32,720 --> 10:10:33,436
that pip
17111
10:10:33,436 --> 10:10:36,480
is checked go to next and add
17112
10:10:36,480 --> 10:10:38,880
python to environment variables then
17113
10:10:38,880 --> 10:10:40,080
install
17114
10:10:40,080 --> 10:10:42,400
now to use pip open up command prompt
17115
10:10:42,400 --> 10:10:43,436
and type pip
17116
10:10:43,436 --> 10:10:47,040
for a help menu all sorts of different
17117
10:10:47,040 --> 10:10:49,916
general options and commands will appear
17118
10:10:49,916 --> 10:10:50,960
if you need to check
17119
10:10:50,960 --> 10:10:53,360
your version of pip type this command
17120
10:10:53,360 --> 10:10:55,436
and here's a cheat sheet that i wrote
17121
10:10:55,436 --> 10:10:57,436
so to check the current version of pip
17122
10:10:57,436 --> 10:10:58,880
type pip
17123
10:10:58,880 --> 10:11:02,960
dash dash version enter
17124
10:11:02,960 --> 10:11:05,436
and currently the version that i'm using
17125
10:11:05,436 --> 10:11:06,800
is 21
17126
10:11:06,800 --> 10:11:08,800
if you need to upgrade pip use this
17127
10:11:08,800 --> 10:11:10,880
command pip
17128
10:11:10,880 --> 10:11:14,240
install dash dash up
17129
10:11:14,240 --> 10:11:18,080
grade pip mine should be the latest
17130
10:11:18,080 --> 10:11:18,960
version
17131
10:11:18,960 --> 10:11:21,916
yeah requirement already satisfied and
17132
10:11:21,916 --> 10:11:24,240
if you need a list of installed packages
17133
10:11:24,240 --> 10:11:24,720
type
17134
10:11:24,720 --> 10:11:27,596
pip list
17135
10:11:28,960 --> 10:11:31,276
so here's all of the packages that i
17136
10:11:31,276 --> 10:11:32,320
currently have installed
17137
10:11:32,320 --> 10:11:34,320
some of these you might recognize like
17138
10:11:34,320 --> 10:11:35,596
numpy
17139
10:11:35,596 --> 10:11:38,000
pie game pie installer those are just a
17140
10:11:38,000 --> 10:11:40,320
few to check to see if any of your
17141
10:11:40,320 --> 10:11:41,040
packages
17142
10:11:41,040 --> 10:11:45,520
are outdated type pip list
17143
10:11:45,520 --> 10:11:49,520
dash dash outdated
17144
10:11:49,520 --> 10:11:51,436
and this might take a second here are
17145
10:11:51,436 --> 10:11:53,680
four packages that are outdated on my
17146
10:11:53,680 --> 10:11:54,400
computer
17147
10:11:54,400 --> 10:11:58,000
babel pi game pi tz and setup tools
17148
10:11:58,000 --> 10:11:59,520
if you need to upgrade one of your
17149
10:11:59,520 --> 10:12:01,840
packages type pip
17150
10:12:01,840 --> 10:12:04,000
install the name of the package let's
17151
10:12:04,000 --> 10:12:06,080
say that i would like to update pygame
17152
10:12:06,080 --> 10:12:10,800
then dash dash upgrade
17153
10:12:10,960 --> 10:12:12,800
i just realized that in my notes i'm
17154
10:12:12,800 --> 10:12:14,880
missing updated package so i just added
17155
10:12:14,880 --> 10:12:15,596
that here
17156
10:12:15,596 --> 10:12:17,756
now you can see that pie game has
17157
10:12:17,756 --> 10:12:18,640
successfully
17158
10:12:18,640 --> 10:12:20,720
updated and let's just check to see if
17159
10:12:20,720 --> 10:12:22,480
it has by using
17160
10:12:22,480 --> 10:12:25,520
pip list outdated pip
17161
10:12:25,520 --> 10:12:29,596
list dash dash outdated
17162
10:12:29,596 --> 10:12:32,080
and pi game is no longer in here so it
17163
10:12:32,080 --> 10:12:33,040
is up to date
17164
10:12:33,040 --> 10:12:34,480
and if you would like to install a
17165
10:12:34,480 --> 10:12:36,480
package type hip
17166
10:12:36,480 --> 10:12:38,720
install and the name of the package you
17167
10:12:38,720 --> 10:12:39,680
would like to download
17168
10:12:39,680 --> 10:12:41,436
let's say i would like to download
17169
10:12:41,436 --> 10:12:42,880
pandas pip
17170
10:12:42,880 --> 10:12:44,720
install the name of the package in this
17171
10:12:44,720 --> 10:12:47,596
case pandas
17172
10:12:47,596 --> 10:12:50,560
two valley boring minutes later and it's
17173
10:12:50,560 --> 10:12:50,960
done
17174
10:12:50,960 --> 10:12:52,960
so i should now have pandas installed
17175
10:12:52,960 --> 10:12:54,240
and let me check
17176
10:12:54,240 --> 10:12:57,276
pip list
17177
10:12:57,680 --> 10:12:59,916
and here it is i have successfully
17178
10:12:59,916 --> 10:13:02,480
downloaded the package of pandas
17179
10:13:02,480 --> 10:13:05,120
so those are a few commands for pip and
17180
10:13:05,120 --> 10:13:06,720
if you're interested in downloading more
17181
10:13:06,720 --> 10:13:09,840
packages head to python's package index
17182
10:13:09,840 --> 10:13:11,360
on second thought i should probably show
17183
10:13:11,360 --> 10:13:13,520
you all where that is python's package
17184
10:13:13,520 --> 10:13:14,000
index
17185
10:13:14,000 --> 10:13:17,120
is located at pipi.org
17186
10:13:17,120 --> 10:13:19,120
and you can do a search for all sorts of
17187
10:13:19,120 --> 10:13:21,360
different projects and packages
17188
10:13:21,360 --> 10:13:24,080
so everybody that is pip4 python i will
17189
10:13:24,080 --> 10:13:25,840
post my notes in the comments section
17190
10:13:25,840 --> 10:13:26,640
down below
17191
10:13:26,640 --> 10:13:29,200
and well yeah that's how to use pip for
17192
10:13:29,200 --> 10:13:30,480
python
17193
10:13:30,480 --> 10:13:32,560
hey what's going on everybody it's bro
17194
10:13:32,560 --> 10:13:33,520
hope you're doing well
17195
10:13:33,520 --> 10:13:34,960
and in this video i'm going to show you
17196
10:13:34,960 --> 10:13:37,120
all how we can convert a python file to
17197
10:13:37,120 --> 10:13:38,160
an executable
17198
10:13:38,160 --> 10:13:43,360
so sit back relax and enjoy the show
17199
10:13:43,360 --> 10:13:45,276
here's a quick disclaimer before we
17200
10:13:45,276 --> 10:13:47,276
begin windows defender and other
17201
10:13:47,276 --> 10:13:49,520
anti-virus programs may prevent your
17202
10:13:49,520 --> 10:13:51,276
executable from running but if you head
17203
10:13:51,276 --> 10:13:52,720
to the settings section of your
17204
10:13:52,720 --> 10:13:54,640
anti-virus you can disable that so that
17205
10:13:54,640 --> 10:13:56,560
your python file can run
17206
10:13:56,560 --> 10:13:58,720
also please be sure that pip and pi
17207
10:13:58,720 --> 10:14:00,720
installer are both installed and up to
17208
10:14:00,720 --> 10:14:01,200
date
17209
10:14:01,200 --> 10:14:03,040
i have a separate video on that if
17210
10:14:03,040 --> 10:14:04,880
you're interested now the program that
17211
10:14:04,880 --> 10:14:06,080
i'm going to be converting to an
17212
10:14:06,080 --> 10:14:06,880
executable
17213
10:14:06,880 --> 10:14:09,360
has a gui portion to it i created a
17214
10:14:09,360 --> 10:14:11,596
clock program in a previous video
17215
10:14:11,596 --> 10:14:13,680
so i'm going to turn this clock program
17216
10:14:13,680 --> 10:14:15,040
into an executable
17217
10:14:15,040 --> 10:14:17,436
now i recommend creating a new folder on
17218
10:14:17,436 --> 10:14:19,680
your desktop just because this process
17219
10:14:19,680 --> 10:14:21,200
is normally somewhat messy so we'll
17220
10:14:21,200 --> 10:14:23,276
create a folder to contain it all
17221
10:14:23,276 --> 10:14:25,756
now copy any relevant python files and
17222
10:14:25,756 --> 10:14:27,680
or images and paste them to within
17223
10:14:27,680 --> 10:14:29,680
this folder if you have an image that
17224
10:14:29,680 --> 10:14:31,120
you would like to set as an icon for
17225
10:14:31,120 --> 10:14:32,080
your executable
17226
10:14:32,080 --> 10:14:33,596
i recommend moving that to the same
17227
10:14:33,596 --> 10:14:36,240
folder as well our next step is to open
17228
10:14:36,240 --> 10:14:38,400
command prompt
17229
10:14:38,400 --> 10:14:40,400
and we need to change the directory of
17230
10:14:40,400 --> 10:14:42,240
command prompt so that it's pointing to
17231
10:14:42,240 --> 10:14:44,000
that folder that contains our python
17232
10:14:44,000 --> 10:14:44,640
file
17233
10:14:44,640 --> 10:14:46,800
so on your python file right click on it
17234
10:14:46,800 --> 10:14:48,560
go to properties and copy
17235
10:14:48,560 --> 10:14:52,080
this location and in command prompt to
17236
10:14:52,080 --> 10:14:53,596
change directory type
17237
10:14:53,596 --> 10:14:56,400
cd space and then paste that location
17238
10:14:56,400 --> 10:14:58,400
now here's the cheat code to convert a
17239
10:14:58,400 --> 10:15:00,240
python file to an executable
17240
10:15:00,240 --> 10:15:02,640
make sure that pi installer and pip are
17241
10:15:02,640 --> 10:15:03,520
installed and both
17242
10:15:03,520 --> 10:15:07,916
up to date so type pi installer
17243
10:15:07,916 --> 10:15:10,720
dash capital f to make all of this one
17244
10:15:10,720 --> 10:15:11,520
file
17245
10:15:11,520 --> 10:15:14,000
if you need the terminal window in your
17246
10:15:14,000 --> 10:15:14,640
program
17247
10:15:14,640 --> 10:15:16,640
then omit this next part since i'm
17248
10:15:16,640 --> 10:15:18,800
running a gui application i do not need
17249
10:15:18,800 --> 10:15:20,000
that terminal window
17250
10:15:20,000 --> 10:15:23,916
so to remove that window type dash w
17251
10:15:23,916 --> 10:15:26,240
if you have an icon that you want to set
17252
10:15:26,240 --> 10:15:27,436
type dash
17253
10:15:27,436 --> 10:15:30,640
i and the name of your icon however it
17254
10:15:30,640 --> 10:15:32,320
should be an ico file
17255
10:15:32,320 --> 10:15:34,160
so if you have some other image that you
17256
10:15:34,160 --> 10:15:36,640
would like to convert to an ico file
17257
10:15:36,640 --> 10:15:39,276
here's how if you need to convert an
17258
10:15:39,276 --> 10:15:41,040
image to an ico file
17259
10:15:41,040 --> 10:15:43,360
you can just google convert to ico but
17260
10:15:43,360 --> 10:15:45,276
one website that i commonly use
17261
10:15:45,276 --> 10:15:48,640
is icoconvert.com just pick a file to
17262
10:15:48,640 --> 10:15:49,200
upload
17263
10:15:49,200 --> 10:15:52,240
so choose file i'm going to convert this
17264
10:15:52,240 --> 10:15:54,800
png file of an alarm clock
17265
10:15:54,800 --> 10:15:57,200
upload it there is a bunch of different
17266
10:15:57,200 --> 10:15:58,640
settings you can use
17267
10:15:58,640 --> 10:16:02,080
convert ico and then download my icon
17268
10:16:02,080 --> 10:16:04,560
and once your ico file is downloaded i
17269
10:16:04,560 --> 10:16:06,560
will drag and drop it to my folder
17270
10:16:06,560 --> 10:16:09,120
and i will rename this let's rename this
17271
10:16:09,120 --> 10:16:09,756
as
17272
10:16:09,756 --> 10:16:13,040
icon dot ico and here's that cheat code
17273
10:16:13,040 --> 10:16:16,320
one more time pi installer dash f
17274
10:16:16,320 --> 10:16:18,800
dash w if you do not need the terminal
17275
10:16:18,800 --> 10:16:19,756
window
17276
10:16:19,756 --> 10:16:22,080
dash i if you would like to add an icon
17277
10:16:22,080 --> 10:16:23,840
the name of your icon it should be an
17278
10:16:23,840 --> 10:16:24,960
ico file
17279
10:16:24,960 --> 10:16:27,520
followed by the name of the python file
17280
10:16:27,520 --> 10:16:29,400
and mine is named
17281
10:16:29,400 --> 10:16:34,000
clock.p.y then hit enter
17282
10:16:36,960 --> 10:16:39,120
alright looks like building our
17283
10:16:39,120 --> 10:16:41,596
executable has completed successfully so
17284
10:16:41,596 --> 10:16:43,120
open that folder back up
17285
10:16:43,120 --> 10:16:45,756
your executable is located within this
17286
10:16:45,756 --> 10:16:46,640
dist folder
17287
10:16:46,640 --> 10:16:48,080
i would recommend just dragging and
17288
10:16:48,080 --> 10:16:50,000
dropping it to your desktop
17289
10:16:50,000 --> 10:16:53,680
and we can get rid of this old folder
17290
10:16:53,680 --> 10:16:56,640
but close out a command prompt and by
17291
10:16:56,640 --> 10:16:58,400
clicking on this executable
17292
10:16:58,400 --> 10:17:00,800
this executable will now run my python
17293
10:17:00,800 --> 10:17:01,520
program
17294
10:17:01,520 --> 10:17:03,680
well everyone that is how to convert a
17295
10:17:03,680 --> 10:17:04,880
python file to
17296
10:17:04,880 --> 10:17:07,436
an exe file i will post my notes to the
17297
10:17:07,436 --> 10:17:08,800
comment section down below
17298
10:17:08,800 --> 10:17:10,960
and well yeah that's how to convert a
17299
10:17:10,960 --> 10:17:13,120
python file to an executable
17300
10:17:13,120 --> 10:17:15,120
what's going on everybody it's you bro
17301
10:17:15,120 --> 10:17:16,160
hope you're doing well
17302
10:17:16,160 --> 10:17:17,520
and in this video i'm going to show you
17303
10:17:17,520 --> 10:17:19,840
all how we can create a basic calculator
17304
10:17:19,840 --> 10:17:21,120
using python so
17305
10:17:21,120 --> 10:17:25,840
sit back relax and enjoy the show
17306
10:17:26,000 --> 10:17:27,756
let's create a calculator after
17307
10:17:27,756 --> 10:17:30,080
importing tkinter we'll need to define
17308
10:17:30,080 --> 10:17:32,160
three separate functions our first
17309
10:17:32,160 --> 10:17:33,756
function will be named
17310
10:17:33,756 --> 10:17:37,436
button press and there is one parameter
17311
10:17:37,436 --> 10:17:39,520
which we will call num we're not going
17312
10:17:39,520 --> 10:17:41,040
to fill this in quite yet we'll get back
17313
10:17:41,040 --> 10:17:42,240
to it later we'll use
17314
10:17:42,240 --> 10:17:45,520
pass as a placeholder we'll also need an
17315
10:17:45,520 --> 10:17:47,596
equals function to calculate our
17316
10:17:47,596 --> 10:17:50,080
expression
17317
10:17:50,080 --> 10:17:55,040
and a function to clear our calculator
17318
10:17:55,040 --> 10:17:57,040
okay that's it for our three functions
17319
10:17:57,040 --> 10:17:58,960
let's create a window
17320
10:17:58,960 --> 10:18:02,400
window equals tk and at the end of our
17321
10:18:02,400 --> 10:18:02,960
program
17322
10:18:02,960 --> 10:18:06,640
we'll write window.mainloop
17323
10:18:06,640 --> 10:18:09,756
let's set a title window
17324
10:18:09,756 --> 10:18:14,560
dot title calculator
17325
10:18:14,560 --> 10:18:17,840
programs good but make sure you spell
17326
10:18:17,840 --> 10:18:20,480
title right
17327
10:18:21,596 --> 10:18:25,360
and let's set a geometry of
17328
10:18:25,360 --> 10:18:29,436
500 500 should be decent
17329
10:18:29,436 --> 10:18:31,756
now we're going to create a string named
17330
10:18:31,756 --> 10:18:32,960
equation
17331
10:18:32,960 --> 10:18:36,880
text equals a set of quotes
17332
10:18:36,880 --> 10:18:38,880
this will effectively be empty for the
17333
10:18:38,880 --> 10:18:40,000
time being
17334
10:18:40,000 --> 10:18:42,880
and we'll need to create a string var
17335
10:18:42,880 --> 10:18:44,000
and i will call this
17336
10:18:44,000 --> 10:18:48,840
equation label equals string
17337
10:18:48,840 --> 10:18:51,596
var let's create a label
17338
10:18:51,596 --> 10:18:54,400
label equals label we're adding our
17339
10:18:54,400 --> 10:18:54,880
label
17340
10:18:54,880 --> 10:18:58,240
to our window i will set our text
17341
10:18:58,240 --> 10:19:04,160
variable equal to our equation label
17342
10:19:04,160 --> 10:19:07,596
you can pick a font if you wish
17343
10:19:11,040 --> 10:19:13,680
i'll set the background color equal to
17344
10:19:13,680 --> 10:19:15,916
white
17345
10:19:17,436 --> 10:19:20,960
a width of 24
17346
10:19:20,960 --> 10:19:24,080
and a height of two
17347
10:19:24,080 --> 10:19:26,240
and we need to finish this by using
17348
10:19:26,240 --> 10:19:27,840
label dot pack
17349
10:19:27,840 --> 10:19:30,560
and we should have a blank label within
17350
10:19:30,560 --> 10:19:31,840
our calculator
17351
10:19:31,840 --> 10:19:33,680
this will display our numbers that we're
17352
10:19:33,680 --> 10:19:36,480
entering in or our expression
17353
10:19:36,480 --> 10:19:38,080
okay this is the tedious part we have to
17354
10:19:38,080 --> 10:19:40,000
create all of the buttons and i think we
17355
10:19:40,000 --> 10:19:42,640
should place these all within a frame
17356
10:19:42,640 --> 10:19:44,800
frame equals frame we're adding our
17357
10:19:44,800 --> 10:19:46,560
frame to our window
17358
10:19:46,560 --> 10:19:49,840
frame dot pack
17359
10:19:50,080 --> 10:19:52,240
now let's create button one we'll do
17360
10:19:52,240 --> 10:19:53,916
some copying and pasting later
17361
10:19:53,916 --> 10:19:57,360
so it's not as tedious so button one
17362
10:19:57,360 --> 10:20:00,000
equals button we're adding our button to
17363
10:20:00,000 --> 10:20:01,120
our frame
17364
10:20:01,120 --> 10:20:04,480
i'll set the text equal to 1
17365
10:20:04,480 --> 10:20:07,680
the height equal to 4 and the width
17366
10:20:07,680 --> 10:20:11,916
equal to 9 and a font size
17367
10:20:11,916 --> 10:20:15,120
of 35
17368
10:20:15,120 --> 10:20:18,480
okay we'll need a command command
17369
10:20:18,480 --> 10:20:22,320
equals i'll write a lambda function here
17370
10:20:22,320 --> 10:20:28,160
lambda we will call button press
17371
10:20:28,160 --> 10:20:29,200
i think i'm going to put this on the
17372
10:20:29,200 --> 10:20:31,360
next line just so it's easier to read
17373
10:20:31,360 --> 10:20:33,360
button press and we will pass in the
17374
10:20:33,360 --> 10:20:35,596
number one
17375
10:20:35,596 --> 10:20:37,596
and we need to add our button to our
17376
10:20:37,596 --> 10:20:38,800
frame
17377
10:20:38,800 --> 10:20:42,080
button one dot grid
17378
10:20:42,080 --> 10:20:45,040
and i'm going to set a row equal to zero
17379
10:20:45,040 --> 10:20:46,640
and a column
17380
10:20:46,640 --> 10:20:50,960
equal to zero and let's just test it
17381
10:20:50,960 --> 10:20:53,040
okay let's add the other buttons i'm
17382
10:20:53,040 --> 10:20:54,720
going to copy and paste everything that
17383
10:20:54,720 --> 10:20:56,320
we have for button one
17384
10:20:56,320 --> 10:21:00,000
and change button one to button two
17385
10:21:00,000 --> 10:21:03,520
set the text equal to two pass into
17386
10:21:03,520 --> 10:21:06,320
to button press and for the row and
17387
10:21:06,320 --> 10:21:06,960
column
17388
10:21:06,960 --> 10:21:09,120
i'm going to set the column equal to one
17389
10:21:09,120 --> 10:21:11,276
but keep row the same
17390
10:21:11,276 --> 10:21:14,400
okay button three
17391
10:21:15,840 --> 10:21:19,040
it's gonna be the same process really so
17392
10:21:19,040 --> 10:21:20,400
text equals three
17393
10:21:20,400 --> 10:21:25,840
button press three row zero column two
17394
10:21:27,436 --> 10:21:30,240
and button four
17395
10:21:30,960 --> 10:21:34,080
text equals four button press
17396
10:21:34,080 --> 10:21:37,360
four column
17397
10:21:37,360 --> 10:21:40,640
actually let's set this to row one
17398
10:21:40,640 --> 10:21:44,320
column zero and i need to change that
17399
10:21:44,320 --> 10:21:46,640
too
17400
10:21:47,040 --> 10:21:52,480
okay let's just double check this
17401
10:21:52,480 --> 10:21:55,596
okay button five
17402
10:21:57,436 --> 10:22:01,840
so button five text equals five
17403
10:22:01,840 --> 10:22:05,040
button press five and
17404
10:22:05,040 --> 10:22:08,880
let's set column equal to one
17405
10:22:10,800 --> 10:22:14,480
okay number six i told you all this is
17406
10:22:14,480 --> 10:22:21,040
going to be tedious
17407
10:22:21,040 --> 10:22:26,400
okay so row one column two
17408
10:22:29,596 --> 10:22:35,840
button seven
17409
10:22:38,800 --> 10:22:42,800
okay so row two column two
17410
10:22:44,000 --> 10:22:46,840
actually let's change that to column
17411
10:22:46,840 --> 10:22:48,320
zero
17412
10:22:48,320 --> 10:22:51,436
much better okay
17413
10:22:51,436 --> 10:22:54,000
button eight
17414
10:22:55,520 --> 10:22:59,520
eight eight eight eight
17415
10:22:59,520 --> 10:23:03,840
row two column one
17416
10:23:04,320 --> 10:23:06,880
number nine
17417
10:23:14,640 --> 10:23:18,160
row two column two
17418
10:23:20,720 --> 10:23:24,000
and lastly zero
17419
10:23:26,800 --> 10:23:30,160
so button zero
17420
10:23:30,160 --> 10:23:33,040
text equals zero button press pass in
17421
10:23:33,040 --> 10:23:34,560
zero
17422
10:23:34,560 --> 10:23:38,160
and this will be row three column
17423
10:23:38,160 --> 10:23:41,276
zero okay
17424
10:23:41,276 --> 10:23:43,840
not too bad now let's fill in some of
17425
10:23:43,840 --> 10:23:45,200
the symbols
17426
10:23:45,200 --> 10:23:48,160
so we need plus
17427
10:23:49,120 --> 10:23:53,040
i'm just going to rename this as plus
17428
10:23:54,880 --> 10:23:58,000
the text will equal the plus sign put
17429
10:23:58,000 --> 10:24:00,560
that within quotes
17430
10:24:00,560 --> 10:24:04,880
and button press will be plus
17431
10:24:05,436 --> 10:24:07,436
oh and we should probably put this in a
17432
10:24:07,436 --> 10:24:09,596
row and column so let's say
17433
10:24:09,596 --> 10:24:12,720
row 0 column three
17434
10:24:12,720 --> 10:24:14,320
so it should be on the right hand side
17435
10:24:14,320 --> 10:24:15,916
here
17436
10:24:15,916 --> 10:24:18,960
okay we need minus
17437
10:24:25,680 --> 10:24:29,436
let's change plus to minus
17438
10:24:30,800 --> 10:24:35,520
and place this at row one column three
17439
10:24:36,000 --> 10:24:41,840
we need multiply
17440
10:24:45,680 --> 10:24:48,800
that is an asterisk
17441
10:24:51,360 --> 10:24:55,596
row two column three
17442
10:24:55,596 --> 10:24:58,320
and divide
17443
10:25:04,640 --> 10:25:09,840
that is a forward slash
17444
10:25:12,640 --> 10:25:16,560
row three column three
17445
10:25:18,400 --> 10:25:20,080
i promise we're near the end of creating
17446
10:25:20,080 --> 10:25:23,520
these buttons we'll need an equal sign
17447
10:25:23,520 --> 10:25:26,400
let's say equal
17448
10:25:27,680 --> 10:25:31,200
equal text
17449
10:25:31,200 --> 10:25:34,560
is the equal sign and our command
17450
10:25:34,560 --> 10:25:37,596
is going to be
17451
10:25:37,596 --> 10:25:40,960
equals the function
17452
10:25:41,040 --> 10:25:50,320
and this will be row 3 column 2.
17453
10:25:50,320 --> 10:25:53,276
we need a decimal
17454
10:26:02,320 --> 10:26:06,400
let's change the text to a decimal
17455
10:26:08,000 --> 10:26:11,680
row three column one
17456
10:26:13,840 --> 10:26:16,160
okay and lastly we just need a clear
17457
10:26:16,160 --> 10:26:19,840
button let's put this at the bottom
17458
10:26:24,240 --> 10:26:28,960
so clear clear
17459
10:26:28,960 --> 10:26:31,040
let's set the text equal to the word
17460
10:26:31,040 --> 10:26:33,120
clear
17461
10:26:33,120 --> 10:26:36,640
and the command will be our clear
17462
10:26:36,640 --> 10:26:38,880
function
17463
10:26:38,880 --> 10:26:40,480
and i think i'm going to pack this at
17464
10:26:40,480 --> 10:26:42,640
the end
17465
10:26:42,640 --> 10:26:46,960
but let's change frame to window
17466
10:26:46,960 --> 10:26:50,480
okay let's check out all these buttons
17467
10:26:50,480 --> 10:26:51,916
okay not too bad i think i'm going to
17468
10:26:51,916 --> 10:26:53,680
change the width on our clear button so
17469
10:26:53,680 --> 10:26:55,436
it's a little bit wider
17470
10:26:55,436 --> 10:26:58,960
let's change the width to 12.
17471
10:26:58,960 --> 10:27:01,040
that's a little bit better okay let's
17472
10:27:01,040 --> 10:27:03,276
fill in some of these functions
17473
10:27:03,276 --> 10:27:06,080
let's start with button press we're
17474
10:27:06,080 --> 10:27:07,276
going to state
17475
10:27:07,276 --> 10:27:10,720
global equation
17476
10:27:10,720 --> 10:27:16,000
text then our equation text
17477
10:27:16,000 --> 10:27:20,160
is equal to equation text plus
17478
10:27:20,160 --> 10:27:22,400
we're converting whatever number or
17479
10:27:22,400 --> 10:27:24,000
symbol that we receive
17480
10:27:24,000 --> 10:27:29,120
to a string and our equation
17481
10:27:29,120 --> 10:27:32,240
label which is a string var will be set
17482
10:27:32,240 --> 10:27:36,240
to our equation text
17483
10:27:36,240 --> 10:27:39,436
and let's test this
17484
10:27:39,520 --> 10:27:42,560
so we can type in any combination of
17485
10:27:42,560 --> 10:27:46,160
numbers or symbols
17486
10:27:47,276 --> 10:27:50,480
okay let's work on our equals
17487
10:27:50,480 --> 10:27:53,840
function next so let's say
17488
10:27:53,840 --> 10:27:57,436
global equation
17489
10:27:57,436 --> 10:28:01,520
text total
17490
10:28:01,520 --> 10:28:04,080
equals
17491
10:28:04,560 --> 10:28:07,840
eval now eval will parse the expression
17492
10:28:07,840 --> 10:28:10,400
that we pass in
17493
10:28:10,400 --> 10:28:13,200
equation
17494
10:28:14,080 --> 10:28:16,480
text
17495
10:28:17,276 --> 10:28:20,800
and our equation label
17496
10:28:20,800 --> 10:28:25,840
will be set to whatever our total is
17497
10:28:26,000 --> 10:28:28,640
and if we want to reuse our total let's
17498
10:28:28,640 --> 10:28:29,360
set
17499
10:28:29,360 --> 10:28:33,120
our equation text
17500
10:28:33,120 --> 10:28:37,436
equal to total 2 plus 2
17501
10:28:37,436 --> 10:28:41,520
equals four minus three equals one
17502
10:28:41,520 --> 10:28:44,800
times five equals five divided by two
17503
10:28:44,800 --> 10:28:47,680
equals two point five now what if we
17504
10:28:47,680 --> 10:28:49,680
divide by zero
17505
10:28:49,680 --> 10:28:51,916
well we'll run into that ugly zero
17506
10:28:51,916 --> 10:28:53,200
division error
17507
10:28:53,200 --> 10:28:55,680
so let's write our code within a try and
17508
10:28:55,680 --> 10:28:56,960
accept block
17509
10:28:56,960 --> 10:29:01,120
so i'm going to indent some of this
17510
10:29:01,120 --> 10:29:04,240
and let's write try all of this code
17511
10:29:04,240 --> 10:29:06,640
and we will catch a zero division error
17512
10:29:06,640 --> 10:29:08,160
exception
17513
10:29:08,160 --> 10:29:13,916
so accept zero division error
17514
10:29:15,596 --> 10:29:19,120
and i will set our equation
17515
10:29:19,120 --> 10:29:22,960
label set
17516
10:29:22,960 --> 10:29:26,640
let's say arithmetic
17517
10:29:26,640 --> 10:29:30,560
error equation text
17518
10:29:30,560 --> 10:29:33,200
equals a set of quotes okay so we should
17519
10:29:33,200 --> 10:29:37,040
be able to catch this exception now
17520
10:29:37,596 --> 10:29:40,400
five divided by zero equals arithmetic
17521
10:29:40,400 --> 10:29:41,520
error
17522
10:29:41,520 --> 10:29:45,120
now what if we have a syntax here
17523
10:29:45,120 --> 10:29:46,880
like we type in just a bunch of symbols
17524
10:29:46,880 --> 10:29:49,120
and hit equals
17525
10:29:49,120 --> 10:29:51,360
we'll probably want to catch the syntax
17526
10:29:51,360 --> 10:29:52,960
error as well
17527
10:29:52,960 --> 10:29:55,120
so let's write an additional accept
17528
10:29:55,120 --> 10:29:56,320
block before
17529
10:29:56,320 --> 10:29:59,040
zero division error or after i guess it
17530
10:29:59,040 --> 10:30:01,360
doesn't matter
17531
10:30:01,360 --> 10:30:04,560
accept syntax
17532
10:30:04,560 --> 10:30:09,040
error let's change arithmetic to
17533
10:30:09,040 --> 10:30:11,680
syntax and we can keep equation text the
17534
10:30:11,680 --> 10:30:13,120
same
17535
10:30:13,120 --> 10:30:15,596
so now our program won't crash if we
17536
10:30:15,596 --> 10:30:17,680
type in a bunch of symbols
17537
10:30:17,680 --> 10:30:20,640
syntax error now let's work on the clear
17538
10:30:20,640 --> 10:30:22,880
button
17539
10:30:22,880 --> 10:30:27,120
so that is within the clear function
17540
10:30:28,160 --> 10:30:31,840
so we will set global
17541
10:30:31,840 --> 10:30:36,320
equation text
17542
10:30:36,960 --> 10:30:41,040
equation label
17543
10:30:41,040 --> 10:30:43,756
dot set to a set of quotes to
17544
10:30:43,756 --> 10:30:46,000
effectively clear it
17545
10:30:46,000 --> 10:30:49,360
and our equation
17546
10:30:49,680 --> 10:30:54,000
text equal to a set of quotes as well
17547
10:30:54,000 --> 10:30:55,840
and that should be it for this program
17548
10:30:55,840 --> 10:30:57,360
let's run this one last time
17549
10:30:57,360 --> 10:31:01,320
let's say 3.14 times
17550
10:31:01,320 --> 10:31:05,436
2.1 equals 6.594
17551
10:31:05,436 --> 10:31:08,960
divided by 2.1
17552
10:31:08,960 --> 10:31:12,400
equals 3.14 minus one i'm just making up
17553
10:31:12,400 --> 10:31:13,276
numbers here
17554
10:31:13,276 --> 10:31:16,480
plus ten equals twelve point one four
17555
10:31:16,480 --> 10:31:19,040
and let's clear this and start again one
17556
10:31:19,040 --> 10:31:19,596
plus
17557
10:31:19,596 --> 10:31:23,276
two plus three plus four equals ten
17558
10:31:23,276 --> 10:31:24,720
i think that's good enough all right
17559
10:31:24,720 --> 10:31:26,560
everybody so that's how to create a
17560
10:31:26,560 --> 10:31:27,120
basic
17561
10:31:27,120 --> 10:31:29,680
calculator in python i will post all of
17562
10:31:29,680 --> 10:31:31,360
my code in the comments section down
17563
10:31:31,360 --> 10:31:33,276
below if you would like a copy for
17564
10:31:33,276 --> 10:31:34,160
yourself
17565
10:31:34,160 --> 10:31:36,160
but yeah that's how to create a basic
17566
10:31:36,160 --> 10:31:38,880
calculator using python
17567
10:31:38,880 --> 10:31:40,560
hey what's going on everybody it's your
17568
10:31:40,560 --> 10:31:42,320
bro hope you're doing well and in this
17569
10:31:42,320 --> 10:31:43,756
video i'm going to explain how we can
17570
10:31:43,756 --> 10:31:46,160
build a basic text editor program in
17571
10:31:46,160 --> 10:31:47,040
python so
17572
10:31:47,040 --> 10:31:51,680
sit back relax and enjoy the show
17573
10:31:51,840 --> 10:31:53,916
okay people let's create a text editor
17574
10:31:53,916 --> 10:31:55,360
here's all the different imports that
17575
10:31:55,360 --> 10:31:57,436
you'll need and we'll begin by defining
17576
10:31:57,436 --> 10:31:58,800
all of the different functions that
17577
10:31:58,800 --> 10:31:59,680
we'll need
17578
10:31:59,680 --> 10:32:01,756
so let's say we have a change color
17579
10:32:01,756 --> 10:32:03,436
function to change the color
17580
10:32:03,436 --> 10:32:05,520
of our font and for all of these
17581
10:32:05,520 --> 10:32:07,520
functions i'm going to write pass as a
17582
10:32:07,520 --> 10:32:08,800
placeholder
17583
10:32:08,800 --> 10:32:12,240
so we have change font and
17584
10:32:12,240 --> 10:32:14,320
this function will accept a varying
17585
10:32:14,320 --> 10:32:15,596
amount of arguments
17586
10:32:15,596 --> 10:32:19,120
so we'll use that args keyword
17587
10:32:19,756 --> 10:32:23,520
we will need to open a file new
17588
10:32:23,520 --> 10:32:25,840
file
17589
10:32:27,596 --> 10:32:30,320
open file
17590
10:32:34,400 --> 10:32:37,840
save file
17591
10:32:40,480 --> 10:32:42,720
cut
17592
10:32:44,160 --> 10:32:46,560
copy
17593
10:32:49,040 --> 10:32:52,080
paste yeah there's a lot of functions
17594
10:32:52,080 --> 10:32:54,320
here
17595
10:32:54,400 --> 10:32:56,800
about
17596
10:32:57,680 --> 10:33:01,120
this is for an about section
17597
10:33:01,360 --> 10:33:05,840
and lastly quit
17598
10:33:05,840 --> 10:33:09,120
so how many do we have one two three
17599
10:33:09,120 --> 10:33:12,320
four five six seven eight
17600
10:33:12,320 --> 10:33:15,596
nine ten ten functions let's create the
17601
10:33:15,596 --> 10:33:17,840
main window that we're gonna work with
17602
10:33:17,840 --> 10:33:20,560
window equals tk
17603
10:33:20,560 --> 10:33:24,080
and at the very end window.main
17604
10:33:24,080 --> 10:33:26,320
loop
17605
10:33:27,200 --> 10:33:28,880
okay so let's create a title for this
17606
10:33:28,880 --> 10:33:30,840
window window
17607
10:33:30,840 --> 10:33:34,480
title notepad
17608
10:33:34,480 --> 10:33:36,880
program i guess or maybe text editor
17609
10:33:36,880 --> 10:33:39,200
program would be more appropriate
17610
10:33:39,200 --> 10:33:43,680
text editor program
17611
10:33:44,560 --> 10:33:46,640
okay and we will need a file to work
17612
10:33:46,640 --> 10:33:48,400
with and i'll declare that now
17613
10:33:48,400 --> 10:33:51,436
file equals none
17614
10:33:51,436 --> 10:33:55,276
we'll need a window width and height
17615
10:33:55,276 --> 10:34:00,400
window with equals 500 is good and a
17616
10:34:00,400 --> 10:34:02,640
height
17617
10:34:03,596 --> 10:34:05,916
okay so let's attempt to center this
17618
10:34:05,916 --> 10:34:06,880
window
17619
10:34:06,880 --> 10:34:08,400
because right now it just appears to the
17620
10:34:08,400 --> 10:34:10,640
side
17621
10:34:10,880 --> 10:34:12,960
so our next step is to get the screen
17622
10:34:12,960 --> 10:34:15,840
width
17623
10:34:16,720 --> 10:34:20,560
equals window dot w
17624
10:34:20,560 --> 10:34:24,320
info screen width
17625
10:34:24,880 --> 10:34:29,840
and i'll copy this for screen height
17626
10:34:35,436 --> 10:34:37,360
okay so we need to figure out how much
17627
10:34:37,360 --> 10:34:39,680
we're going to move our window on the x
17628
10:34:39,680 --> 10:34:42,800
and y axis so let's say x
17629
10:34:42,800 --> 10:34:46,640
equals screen
17630
10:34:46,640 --> 10:34:49,756
width divided by 2
17631
10:34:49,756 --> 10:34:52,240
minus
17632
10:34:52,800 --> 10:34:56,080
window width divided by
17633
10:34:56,080 --> 10:34:58,880
two and we're going to cast all of this
17634
10:34:58,880 --> 10:34:59,520
to an
17635
10:34:59,520 --> 10:35:04,080
end so add an inch cast around this
17636
10:35:04,080 --> 10:35:08,160
let's copy this do the same thing for y
17637
10:35:08,560 --> 10:35:11,840
except this will be screen height
17638
10:35:11,840 --> 10:35:14,960
and window height
17639
10:35:17,520 --> 10:35:19,200
okay let's set the geometry for the
17640
10:35:19,200 --> 10:35:21,436
window window
17641
10:35:21,436 --> 10:35:24,800
geometry i'm going to use a
17642
10:35:24,800 --> 10:35:29,840
string format method so let's say
17643
10:35:32,160 --> 10:35:34,160
these will be placeholders for our
17644
10:35:34,160 --> 10:35:36,720
values
17645
10:35:37,276 --> 10:35:40,880
so we have window width for the first
17646
10:35:40,880 --> 10:35:42,720
value
17647
10:35:42,720 --> 10:35:46,320
window height
17648
10:35:46,320 --> 10:35:50,240
x and y so this should be near the
17649
10:35:50,240 --> 10:35:51,680
middle
17650
10:35:51,680 --> 10:35:53,840
cool
17651
10:35:55,436 --> 10:35:57,520
we're going to create a string var
17652
10:35:57,520 --> 10:35:58,960
variable to hold the
17653
10:35:58,960 --> 10:36:02,400
font name font name
17654
10:36:02,400 --> 10:36:05,680
equals string var
17655
10:36:05,680 --> 10:36:08,880
and pass in your window and you can set
17656
10:36:08,880 --> 10:36:09,916
this to
17657
10:36:09,916 --> 10:36:12,080
what do you want your default font to be
17658
10:36:12,080 --> 10:36:13,200
so let's say font
17659
10:36:13,200 --> 10:36:16,480
name dot set and then pick a font
17660
10:36:16,480 --> 10:36:19,680
i'll use ariel i think
17661
10:36:19,680 --> 10:36:22,240
let's create a font size string bar as
17662
10:36:22,240 --> 10:36:24,560
well
17663
10:36:28,560 --> 10:36:31,916
we're going to add this to the window
17664
10:36:32,160 --> 10:36:35,840
and we can set a default size
17665
10:36:37,200 --> 10:36:40,400
perhaps 25.
17666
10:36:40,560 --> 10:36:43,916
okay let's create a text area
17667
10:36:43,916 --> 10:36:48,320
text area equals text
17668
10:36:48,320 --> 10:36:51,200
we're adding this to the window i'm
17669
10:36:51,200 --> 10:36:52,080
going to set a
17670
10:36:52,080 --> 10:36:55,520
font equal to font
17671
10:36:55,520 --> 10:36:59,360
name dot get the name of the font that
17672
10:36:59,360 --> 10:37:00,400
we want to use
17673
10:37:00,400 --> 10:37:03,360
will be the font that we set within our
17674
10:37:03,360 --> 10:37:03,840
font
17675
10:37:03,840 --> 10:37:07,520
name string var so now we'll use a size
17676
10:37:07,520 --> 10:37:11,756
of font size dot get
17677
10:37:11,756 --> 10:37:16,240
font size dot get
17678
10:37:16,240 --> 10:37:20,000
okay now we need a scroll bar
17679
10:37:20,000 --> 10:37:23,596
scroll bar equals
17680
10:37:23,596 --> 10:37:26,400
scroll bar
17681
10:37:26,640 --> 10:37:29,200
writing this to our text area so pass
17682
10:37:29,200 --> 10:37:31,276
that in
17683
10:37:31,276 --> 10:37:34,320
and i'm going to configure my window
17684
10:37:34,320 --> 10:37:39,040
using grid row configure
17685
10:37:39,040 --> 10:37:42,880
this will allow our text area to expand
17686
10:37:42,880 --> 10:37:44,800
but we're going to set a weight equal to
17687
10:37:44,800 --> 10:37:47,436
1 so that it doesn't back expand
17688
10:37:47,436 --> 10:37:51,596
and then we need column configure
17689
10:37:55,276 --> 10:37:59,040
okay and text
17690
10:37:59,040 --> 10:38:02,800
area dot grid
17691
10:38:03,840 --> 10:38:07,276
i'm going to use the sticky keyword and
17692
10:38:07,276 --> 10:38:10,480
this will stick north plus east
17693
10:38:10,480 --> 10:38:14,000
plus south plus west
17694
10:38:14,000 --> 10:38:16,160
so my text area should take most of the
17695
10:38:16,160 --> 10:38:18,480
window
17696
10:38:19,276 --> 10:38:24,320
this is some sample text
17697
10:38:24,840 --> 10:38:27,360
hooray so it goes down to the next line
17698
10:38:27,360 --> 10:38:29,596
once it runs out of room on one line
17699
10:38:29,596 --> 10:38:31,840
unless you resize the window which is
17700
10:38:31,840 --> 10:38:33,840
exactly what we wanted
17701
10:38:33,840 --> 10:38:37,120
let's create a scroll bar while we
17702
10:38:37,120 --> 10:38:39,040
already created it we just have to
17703
10:38:39,040 --> 10:38:42,240
add a few details to it
17704
10:38:42,240 --> 10:38:45,520
so that will be near the bottom
17705
10:38:45,520 --> 10:38:49,680
and why don't we say scroll
17706
10:38:49,680 --> 10:38:53,276
bar dot pack
17707
10:38:53,276 --> 10:38:56,320
side equals right
17708
10:38:56,320 --> 10:39:00,400
fill equals y
17709
10:39:00,880 --> 10:39:03,120
and then we just have to configure our
17710
10:39:03,120 --> 10:39:04,720
text area
17711
10:39:04,720 --> 10:39:08,080
with the scroll bar so text
17712
10:39:08,080 --> 10:39:12,720
area dot config
17713
10:39:12,720 --> 10:39:16,640
y scroll command
17714
10:39:16,640 --> 10:39:20,560
equals scroll bar
17715
10:39:20,560 --> 10:39:24,480
dot set so that should work but
17716
10:39:24,480 --> 10:39:27,520
let's test it to be sure okay i'm just
17717
10:39:27,520 --> 10:39:30,720
going to type a bunch of f's
17718
10:39:31,520 --> 10:39:34,960
okay look at that our scroll bar works
17719
10:39:34,960 --> 10:39:39,520
cool okay let's head to this portion
17720
10:39:39,520 --> 10:39:42,480
and we'll create a bunch of buttons well
17721
10:39:42,480 --> 10:39:43,120
a button
17722
10:39:43,120 --> 10:39:46,400
an option menu and a spin box for the
17723
10:39:46,400 --> 10:39:49,680
font color the size and the font style
17724
10:39:49,680 --> 10:39:51,436
so i'm going to place all of these
17725
10:39:51,436 --> 10:39:53,596
different widgets within a frame
17726
10:39:53,596 --> 10:39:58,160
frame equals frame add it to the window
17727
10:39:58,160 --> 10:40:02,640
and then i'm going to use frame dot grid
17728
10:40:02,640 --> 10:40:05,040
okay let's create a color button to
17729
10:40:05,040 --> 10:40:07,040
change the fonts color
17730
10:40:07,040 --> 10:40:10,080
color button equals button
17731
10:40:10,080 --> 10:40:13,276
i'll add my button to my frame
17732
10:40:13,276 --> 10:40:16,840
i'll set the text of the button equal to
17733
10:40:16,840 --> 10:40:18,000
color
17734
10:40:18,000 --> 10:40:21,200
and i will associate a command of
17735
10:40:21,200 --> 10:40:23,436
change color which we have already
17736
10:40:23,436 --> 10:40:25,596
defined at the top
17737
10:40:25,596 --> 10:40:32,480
where is it at the top here change color
17738
10:40:32,480 --> 10:40:35,120
and then actually that's it for this
17739
10:40:35,120 --> 10:40:36,880
portion
17740
10:40:36,880 --> 10:40:40,800
so then we need to add this button color
17741
10:40:40,800 --> 10:40:44,480
button dot grid i'll set the row
17742
10:40:44,480 --> 10:40:47,596
equal to zero and the column equal to
17743
10:40:47,596 --> 10:40:48,480
zero
17744
10:40:48,480 --> 10:40:51,116
so let's be sure that this appears make
17745
10:40:51,116 --> 10:40:53,360
sure you spell grid right
17746
10:40:53,360 --> 10:40:56,800
okay and we have a color button but this
17747
10:40:56,800 --> 10:40:58,080
button doesn't do anything
17748
10:40:58,080 --> 10:41:00,560
so let's head to the change color
17749
10:41:00,560 --> 10:41:02,000
function
17750
10:41:02,000 --> 10:41:04,320
within the change color function we're
17751
10:41:04,320 --> 10:41:05,840
going to say color
17752
10:41:05,840 --> 10:41:10,240
equals color chooser
17753
10:41:10,240 --> 10:41:14,160
dot ask color
17754
10:41:14,160 --> 10:41:17,436
and you can set a title to
17755
10:41:17,436 --> 10:41:21,200
pick a color or
17756
10:41:21,200 --> 10:41:25,200
else you don't technically need a title
17757
10:41:25,200 --> 10:41:28,240
and then text area
17758
10:41:28,240 --> 10:41:32,160
dot config oh i should probably print
17759
10:41:32,160 --> 10:41:32,800
this color
17760
10:41:32,800 --> 10:41:35,520
so let me do that um i'm just gonna
17761
10:41:35,520 --> 10:41:36,720
explain something here
17762
10:41:36,720 --> 10:41:39,276
so this color is going to be i believe a
17763
10:41:39,276 --> 10:41:39,756
tuple
17764
10:41:39,756 --> 10:41:43,436
so let's print it so let's say that i
17765
10:41:43,436 --> 10:41:45,276
would like
17766
10:41:45,276 --> 10:41:49,596
white okay so we have a tuple we just
17767
10:41:49,596 --> 10:41:50,080
need
17768
10:41:50,080 --> 10:41:53,200
one of these values not both of them
17769
10:41:53,200 --> 10:41:56,640
so i'm going to say
17770
10:41:56,640 --> 10:42:00,880
text area dot config
17771
10:42:00,880 --> 10:42:03,680
foreground equals our color but we do
17772
10:42:03,680 --> 10:42:05,596
not want to assign a tuple
17773
10:42:05,596 --> 10:42:07,680
to our color we just need one of these
17774
10:42:07,680 --> 10:42:09,756
values so let's say i would like this
17775
10:42:09,756 --> 10:42:11,040
hex value
17776
10:42:11,040 --> 10:42:15,520
so i'm going to say at index 1
17777
10:42:15,520 --> 10:42:19,520
and let's try it color
17778
10:42:19,520 --> 10:42:22,160
perhaps i would like my text pink for
17779
10:42:22,160 --> 10:42:24,960
some reason so we're going with pink
17780
10:42:24,960 --> 10:42:28,800
this is some text i suppose
17781
10:42:28,800 --> 10:42:30,560
and we can change this back too by using
17782
10:42:30,560 --> 10:42:32,800
this color button now i would like
17783
10:42:32,800 --> 10:42:36,640
blue and we have blue text okay so that
17784
10:42:36,640 --> 10:42:38,720
is the change color function i'm going
17785
10:42:38,720 --> 10:42:40,560
to minimize this because we do not need
17786
10:42:40,560 --> 10:42:41,840
it anymore
17787
10:42:41,840 --> 10:42:44,560
okay we're going to change the font next
17788
10:42:44,560 --> 10:42:44,960
so
17789
10:42:44,960 --> 10:42:48,400
head to the portion of our code right
17790
10:42:48,400 --> 10:42:49,040
after the
17791
10:42:49,040 --> 10:42:51,520
color button and we'll create an option
17792
10:42:51,520 --> 10:42:52,320
menu
17793
10:42:52,320 --> 10:42:53,840
to list all of the different fonts
17794
10:42:53,840 --> 10:42:55,916
available to us so let's say
17795
10:42:55,916 --> 10:42:59,596
font box equals option
17796
10:42:59,596 --> 10:43:04,080
menu we're adding this to our frame
17797
10:43:04,320 --> 10:43:08,240
we need to set a variable a string var
17798
10:43:08,240 --> 10:43:10,480
so that will be our font name that we
17799
10:43:10,480 --> 10:43:12,720
declared up here
17800
10:43:12,720 --> 10:43:14,960
now here's a trick to add all of the
17801
10:43:14,960 --> 10:43:17,916
fonts as options to our option menu
17802
10:43:17,916 --> 10:43:20,160
we can use the unpacking operator which
17803
10:43:20,160 --> 10:43:21,360
is an asterisk
17804
10:43:21,360 --> 10:43:25,596
followed by font dot families
17805
10:43:25,596 --> 10:43:28,320
and then call this function so this will
17806
10:43:28,320 --> 10:43:29,040
return
17807
10:43:29,040 --> 10:43:31,040
all of the different fonts available to
17808
10:43:31,040 --> 10:43:34,400
us and we just need to set a command
17809
10:43:34,400 --> 10:43:37,360
and when we use our option menu we'll
17810
10:43:37,360 --> 10:43:38,000
call
17811
10:43:38,000 --> 10:43:41,840
change font and we need to add
17812
10:43:41,840 --> 10:43:46,160
our font box to our grid
17813
10:43:46,160 --> 10:43:49,360
row equals zero and column
17814
10:43:49,360 --> 10:43:51,756
equals one so this should be on the
17815
10:43:51,756 --> 10:43:55,436
right hand side of our color button
17816
10:43:55,680 --> 10:43:56,720
yeah it's going to look something like
17817
10:43:56,720 --> 10:43:58,320
that and you can see the default is
17818
10:43:58,320 --> 10:43:59,916
arial that we set
17819
10:43:59,916 --> 10:44:02,160
and we can change it to something else
17820
10:44:02,160 --> 10:44:03,360
like
17821
10:44:03,360 --> 10:44:06,480
comic sans or whatever
17822
10:44:06,480 --> 10:44:08,080
uh however it's not going to change it
17823
10:44:08,080 --> 10:44:10,480
quite yet so that'll be the change font
17824
10:44:10,480 --> 10:44:11,436
function
17825
10:44:11,436 --> 10:44:13,436
before we fill in the change font
17826
10:44:13,436 --> 10:44:15,520
function there's another widget that's
17827
10:44:15,520 --> 10:44:17,040
going to share the same command of
17828
10:44:17,040 --> 10:44:18,640
change font so we should create that
17829
10:44:18,640 --> 10:44:19,436
next
17830
10:44:19,436 --> 10:44:22,560
so our next widget is a spin box which i
17831
10:44:22,560 --> 10:44:23,360
will name
17832
10:44:23,360 --> 10:44:26,000
sizebox this will be in charge of
17833
10:44:26,000 --> 10:44:28,560
increasing or decreasing the font
17834
10:44:28,560 --> 10:44:32,320
so size box equals spin box
17835
10:44:32,320 --> 10:44:35,276
we're adding the spin box to our frame
17836
10:44:35,276 --> 10:44:37,520
and we'll set a range
17837
10:44:37,520 --> 10:44:39,680
so from underscore you have to add that
17838
10:44:39,680 --> 10:44:41,040
underscore it's kind of weird
17839
10:44:41,040 --> 10:44:43,596
this is the range of our spin box for
17840
10:44:43,596 --> 10:44:44,640
the font size
17841
10:44:44,640 --> 10:44:49,596
from 1 to let's say 100
17842
10:44:49,596 --> 10:44:53,436
the text variable will be
17843
10:44:53,436 --> 10:44:56,800
font size
17844
10:44:57,276 --> 10:45:01,360
and our command equals change
17845
10:45:01,360 --> 10:45:04,720
font so when
17846
10:45:04,720 --> 10:45:08,000
we use our spin box and call this
17847
10:45:08,000 --> 10:45:10,320
command of change font it's going to
17848
10:45:10,320 --> 10:45:11,040
pass in
17849
10:45:11,040 --> 10:45:13,680
our font size as an argument whereas
17850
10:45:13,680 --> 10:45:14,800
with option menu
17851
10:45:14,800 --> 10:45:17,040
it doesn't pass any arguments that's why
17852
10:45:17,040 --> 10:45:17,840
with our
17853
10:45:17,840 --> 10:45:20,080
change font function we're accepting a
17854
10:45:20,080 --> 10:45:21,756
varying amount of arguments
17855
10:45:21,756 --> 10:45:23,520
because one's going to pass in an
17856
10:45:23,520 --> 10:45:26,480
argument and the other isn't and we just
17857
10:45:26,480 --> 10:45:28,880
need to add our spin box
17858
10:45:28,880 --> 10:45:32,720
so that would be size box
17859
10:45:32,720 --> 10:45:36,080
dot grid row equals zero
17860
10:45:36,080 --> 10:45:39,756
and column equals two
17861
10:45:39,756 --> 10:45:42,560
so we should have a spin box right on
17862
10:45:42,560 --> 10:45:43,276
the bottom
17863
10:45:43,276 --> 10:45:46,560
and it's set to 25 that was our default
17864
10:45:46,560 --> 10:45:47,360
for
17865
10:45:47,360 --> 10:45:50,560
our font size string var which we set as
17866
10:45:50,560 --> 10:45:52,160
the text variable
17867
10:45:52,160 --> 10:45:54,560
okay so we can fill in this function of
17868
10:45:54,560 --> 10:45:55,680
change font now
17869
10:45:55,680 --> 10:45:58,000
which will apply both to our option menu
17870
10:45:58,000 --> 10:46:00,640
and our spin box
17871
10:46:00,640 --> 10:46:03,520
so that is all the way at the top within
17872
10:46:03,520 --> 10:46:04,960
the change font function
17873
10:46:04,960 --> 10:46:08,000
we're going to take our text area
17874
10:46:08,000 --> 10:46:11,116
dot config set
17875
10:46:11,116 --> 10:46:14,800
the font equal to font
17876
10:46:14,800 --> 10:46:18,560
name dot get
17877
10:46:18,560 --> 10:46:22,160
and the size will be size box
17878
10:46:22,160 --> 10:46:25,756
dot get and that's it for this function
17879
10:46:25,756 --> 10:46:28,480
i'll minimize it for now and let's test
17880
10:46:28,480 --> 10:46:30,240
it
17881
10:46:30,240 --> 10:46:34,560
this is some sample text
17882
10:46:34,560 --> 10:46:38,080
hey how you doing good
17883
10:46:38,080 --> 10:46:41,360
looking okay so let's change
17884
10:46:41,360 --> 10:46:45,596
the font uh let's pick comic sans or
17885
10:46:45,596 --> 10:46:46,560
whatever you want
17886
10:46:46,560 --> 10:46:51,040
and we can increase or decrease the size
17887
10:46:51,040 --> 10:46:54,240
and we can change the color how about
17888
10:46:54,240 --> 10:46:56,000
red this time
17889
10:46:56,000 --> 10:46:59,596
all right nice now let's add a menu bar
17890
10:46:59,596 --> 10:47:02,480
at the top so we can save this file or
17891
10:47:02,480 --> 10:47:04,640
open a file or create a new file and
17892
10:47:04,640 --> 10:47:06,320
some other stuff
17893
10:47:06,320 --> 10:47:08,000
on second thought i'm going to move
17894
10:47:08,000 --> 10:47:09,840
these two last lines of code
17895
10:47:09,840 --> 10:47:12,960
to this section here
17896
10:47:12,960 --> 10:47:14,720
where we create our scroll bar just so
17897
10:47:14,720 --> 10:47:16,320
that it's more organized
17898
10:47:16,320 --> 10:47:19,520
okay so at the bottom let's create
17899
10:47:19,520 --> 10:47:21,756
various drop-down menus so we'll need a
17900
10:47:21,756 --> 10:47:23,436
menu bar to begin with
17901
10:47:23,436 --> 10:47:27,360
so let's say menu bar
17902
10:47:27,360 --> 10:47:30,400
equals menu we're adding this to our
17903
10:47:30,400 --> 10:47:31,436
window
17904
10:47:31,436 --> 10:47:35,200
make sure that's capital by the way
17905
10:47:35,436 --> 10:47:39,756
and window dot config
17906
10:47:39,756 --> 10:47:44,320
menu equals menu bar
17907
10:47:44,320 --> 10:47:46,960
so we'll create a file menu edit menu
17908
10:47:46,960 --> 10:47:47,436
and
17909
10:47:47,436 --> 10:47:50,800
a help menu so let's begin with the file
17910
10:47:50,800 --> 10:47:54,480
menu to add to the menu bar file menu
17911
10:47:54,480 --> 10:47:57,916
equals menu we're adding this to our
17912
10:47:57,916 --> 10:47:58,840
menu
17913
10:47:58,840 --> 10:48:04,240
bar and set a tear off equal to zero
17914
10:48:06,960 --> 10:48:09,276
we'll need a cascade for it to actually
17915
10:48:09,276 --> 10:48:10,960
be a drop down menu
17916
10:48:10,960 --> 10:48:14,080
so that will be menu bar
17917
10:48:14,080 --> 10:48:19,116
dot add cascade
17918
10:48:21,436 --> 10:48:25,520
label equals file
17919
10:48:25,520 --> 10:48:28,960
comma menu equals
17920
10:48:28,960 --> 10:48:32,080
file menu
17921
10:48:32,080 --> 10:48:35,360
let's see if anything appears yet yep so
17922
10:48:35,360 --> 10:48:37,116
we have a file menu
17923
10:48:37,116 --> 10:48:40,720
but there's nothing to drop down yet
17924
10:48:40,720 --> 10:48:43,756
so we need some options
17925
10:48:43,756 --> 10:48:47,680
well commands technically file menu
17926
10:48:47,680 --> 10:48:52,560
dot add command
17927
10:48:54,880 --> 10:49:00,080
label equals new
17928
10:49:00,080 --> 10:49:03,276
and the command will equal our
17929
10:49:03,276 --> 10:49:06,480
new file function
17930
10:49:06,480 --> 10:49:09,040
actually i'll put these together okay so
17931
10:49:09,040 --> 10:49:10,320
let's copy this
17932
10:49:10,320 --> 10:49:15,520
and paste it we have open
17933
10:49:16,400 --> 10:49:20,320
the command will be open file
17934
10:49:22,160 --> 10:49:25,200
we have save
17935
10:49:25,200 --> 10:49:28,240
the command will be save
17936
10:49:28,240 --> 10:49:33,680
file and i'll add a separator
17937
10:49:33,680 --> 10:49:37,276
file menu dot add
17938
10:49:37,276 --> 10:49:40,800
separator and lastly
17939
10:49:40,800 --> 10:49:44,240
we will have exit
17940
10:49:44,400 --> 10:49:47,916
command quit
17941
10:49:47,916 --> 10:49:49,360
okay let me show you what this looks
17942
10:49:49,360 --> 10:49:51,040
like
17943
10:49:51,040 --> 10:49:54,320
so here's our drop down menu
17944
10:49:54,320 --> 10:49:56,800
that separator is this bold line going
17945
10:49:56,800 --> 10:49:57,520
across
17946
10:49:57,520 --> 10:50:00,080
so if i was to remove this then we
17947
10:50:00,080 --> 10:50:01,596
wouldn't have that bolt line
17948
10:50:01,596 --> 10:50:03,916
you can keep it in or get rid of it do
17949
10:50:03,916 --> 10:50:05,436
whatever you want
17950
10:50:05,436 --> 10:50:07,756
and this is the tear off so i'm going to
17951
10:50:07,756 --> 10:50:10,800
remove the tear off
17952
10:50:11,596 --> 10:50:13,360
and you get this like tear off line at
17953
10:50:13,360 --> 10:50:15,360
the top i guess it's the default
17954
10:50:15,360 --> 10:50:17,360
i usually don't like it so i set tear
17955
10:50:17,360 --> 10:50:18,880
off equal to zero
17956
10:50:18,880 --> 10:50:21,520
so that is our file menu let's work on a
17957
10:50:21,520 --> 10:50:24,160
edit menu
17958
10:50:24,400 --> 10:50:27,680
edit menu equals
17959
10:50:27,680 --> 10:50:32,800
menu menu
17960
10:50:32,840 --> 10:50:37,276
bar make sure that's capital two
17961
10:50:38,160 --> 10:50:42,720
tare off equals zero
17962
10:50:42,880 --> 10:50:46,320
we'll need a cascade menu bar
17963
10:50:46,320 --> 10:50:50,640
dot add cascade
17964
10:50:55,116 --> 10:50:58,480
label equals
17965
10:51:00,840 --> 10:51:02,720
edit
17966
10:51:02,720 --> 10:51:06,800
menu equals edit
17967
10:51:06,800 --> 10:51:09,200
menu
17968
10:51:10,240 --> 10:51:14,840
edit menu dot add
17969
10:51:14,840 --> 10:51:17,840
command
17970
10:51:19,596 --> 10:51:24,800
label equals cut to cut some text
17971
10:51:25,520 --> 10:51:30,240
command equals copy
17972
10:51:30,240 --> 10:51:33,436
actually that's cut my bad
17973
10:51:33,436 --> 10:51:36,880
okay next is copy so let's copy this
17974
10:51:36,880 --> 10:51:39,596
and paste it
17975
10:51:40,880 --> 10:51:44,000
cut copy
17976
10:51:44,000 --> 10:51:46,800
we have paste
17977
10:51:47,436 --> 10:51:50,560
command paste
17978
10:51:51,276 --> 10:51:54,400
all right let's take a look
17979
10:51:55,040 --> 10:51:58,080
oh make sure you spell label right
17980
10:51:58,080 --> 10:52:01,840
okay there we go okay so we can cut
17981
10:52:01,840 --> 10:52:05,756
copy and paste and lastly we should have
17982
10:52:05,756 --> 10:52:08,640
a help menu
17983
10:52:08,960 --> 10:52:12,800
help menu equals
17984
10:52:12,800 --> 10:52:15,916
menu menu
17985
10:52:15,916 --> 10:52:19,116
bar tear off
17986
10:52:19,116 --> 10:52:22,320
equals zero
17987
10:52:22,320 --> 10:52:26,400
i think i'll copy this for my cascade
17988
10:52:26,400 --> 10:52:30,400
so menu bar dot add cascade
17989
10:52:30,400 --> 10:52:33,680
help menu equals help
17990
10:52:33,680 --> 10:52:36,000
menu
17991
10:52:38,240 --> 10:52:40,400
what we'll be doing for a command is
17992
10:52:40,400 --> 10:52:41,680
that we'll be creating
17993
10:52:41,680 --> 10:52:44,080
an about section that will bring up a
17994
10:52:44,080 --> 10:52:46,000
pop-up window if you need to display
17995
10:52:46,000 --> 10:52:49,116
any like help information or anything
17996
10:52:49,116 --> 10:52:52,640
uh so let's say about and the command
17997
10:52:52,640 --> 10:52:54,080
will be
17998
10:52:54,080 --> 10:52:58,240
about okay let's take a look
17999
10:52:58,240 --> 10:53:00,880
so we have help and we'll have an about
18000
10:53:00,880 --> 10:53:02,480
section that will tell you more about
18001
10:53:02,480 --> 10:53:03,916
this program
18002
10:53:03,916 --> 10:53:05,436
but we need to work on some of these
18003
10:53:05,436 --> 10:53:08,240
functions let's head to the quit
18004
10:53:08,240 --> 10:53:09,200
function
18005
10:53:09,200 --> 10:53:11,200
this one's fairly easy this is found
18006
10:53:11,200 --> 10:53:12,960
within the file menu
18007
10:53:12,960 --> 10:53:16,320
window dot destroy that will close out
18008
10:53:16,320 --> 10:53:18,880
of the window
18009
10:53:18,880 --> 10:53:22,240
so go to file then exit to exit the
18010
10:53:22,240 --> 10:53:23,436
program
18011
10:53:23,436 --> 10:53:25,116
we don't need it anymore so let's
18012
10:53:25,116 --> 10:53:26,960
minimize this
18013
10:53:26,960 --> 10:53:31,116
okay let's go to the about function
18014
10:53:31,276 --> 10:53:36,240
let's bring up a message box show info
18015
10:53:36,240 --> 10:53:38,400
this first argument is the title so
18016
10:53:38,400 --> 10:53:39,276
let's say
18017
10:53:39,276 --> 10:53:43,116
about this program
18018
10:53:43,116 --> 10:53:45,436
and the text will be whatever you want
18019
10:53:45,436 --> 10:53:46,160
to
18020
10:53:46,160 --> 10:53:49,436
tell the user about this program this
18021
10:53:49,436 --> 10:53:53,680
is a program written
18022
10:53:53,680 --> 10:53:57,116
by you
18023
10:53:57,116 --> 10:53:59,596
okay so we should have a pop-up window
18024
10:53:59,596 --> 10:54:00,720
when you go to help
18025
10:54:00,720 --> 10:54:03,840
then about about this program this is a
18026
10:54:03,840 --> 10:54:04,560
program
18027
10:54:04,560 --> 10:54:07,276
written by you the next function is the
18028
10:54:07,276 --> 10:54:10,720
paste function so we can paste some text
18029
10:54:10,720 --> 10:54:14,640
text area dot event
18030
10:54:14,640 --> 10:54:18,160
generate and the event is going to be
18031
10:54:18,160 --> 10:54:19,116
within quotes
18032
10:54:19,116 --> 10:54:22,480
and double angle brackets
18033
10:54:22,480 --> 10:54:26,320
paste let's copy this
18034
10:54:26,320 --> 10:54:30,880
and change paste to copy
18035
10:54:31,040 --> 10:54:33,840
and then cut
18036
10:54:37,360 --> 10:54:39,840
okay so we should be able to cut copy
18037
10:54:39,840 --> 10:54:42,480
and paste
18038
10:54:44,000 --> 10:54:47,276
hey how's it
18039
10:54:47,276 --> 10:54:50,480
hanging okay
18040
10:54:50,480 --> 10:54:54,480
let's highlight this edit
18041
10:54:54,480 --> 10:54:58,560
copy let's move down to the next line
18042
10:54:58,560 --> 10:55:01,916
and paste and we should be able to cut
18043
10:55:01,916 --> 10:55:02,720
as well
18044
10:55:02,720 --> 10:55:06,160
edit cut nice
18045
10:55:06,160 --> 10:55:09,756
head to the new file function
18046
10:55:09,756 --> 10:55:12,640
and we'll set the title of our window to
18047
10:55:12,640 --> 10:55:13,276
maybe
18048
10:55:13,276 --> 10:55:16,640
untitled until we save something so
18049
10:55:16,640 --> 10:55:19,276
untitled
18050
10:55:20,560 --> 10:55:23,360
and i need to delete any text within our
18051
10:55:23,360 --> 10:55:24,640
text area
18052
10:55:24,640 --> 10:55:28,000
text area dot delete and we need to set
18053
10:55:28,000 --> 10:55:30,320
a range so this has to be a floating
18054
10:55:30,320 --> 10:55:31,040
point number
18055
10:55:31,040 --> 10:55:34,560
the first character until the end
18056
10:55:34,560 --> 10:55:40,000
let's try it so i'll write some sample
18057
10:55:40,840 --> 10:55:42,080
text
18058
10:55:42,080 --> 10:55:45,436
and we are going to go to file new
18059
10:55:45,436 --> 10:55:48,160
that changed the title and it deleted
18060
10:55:48,160 --> 10:55:49,360
our text
18061
10:55:49,360 --> 10:55:52,640
so that is the new file function
18062
10:55:52,640 --> 10:55:55,520
let's work on open file we are now
18063
10:55:55,520 --> 10:55:56,000
within
18064
10:55:56,000 --> 10:55:58,800
the open file function and we'll say
18065
10:55:58,800 --> 10:55:59,840
file
18066
10:55:59,840 --> 10:56:03,756
equals ask open
18067
10:56:03,756 --> 10:56:07,360
file name so we have a
18068
10:56:07,360 --> 10:56:11,436
default extension if you would like one
18069
10:56:11,436 --> 10:56:14,360
default i think i spelled that wrong
18070
10:56:14,360 --> 10:56:17,360
extension
18071
10:56:18,080 --> 10:56:23,436
equals let's say a txt file
18072
10:56:23,436 --> 10:56:28,800
comma what are the accepted file types
18073
10:56:28,800 --> 10:56:32,160
i'll set this equal to
18074
10:56:32,560 --> 10:56:35,840
all files
18075
10:56:35,840 --> 10:56:38,880
comma and then we need asterisk
18076
10:56:38,880 --> 10:56:43,436
dot asterisk for all file types
18077
10:56:43,436 --> 10:56:46,640
as well as text documents
18078
10:56:46,640 --> 10:56:49,680
so these are what is accepted when we
18079
10:56:49,680 --> 10:56:52,640
look for a file so we have either all
18080
10:56:52,640 --> 10:56:53,200
files
18081
10:56:53,200 --> 10:56:57,436
or all text documents text
18082
10:56:57,436 --> 10:57:02,800
documents comma
18083
10:57:02,800 --> 10:57:07,840
asterisk dot txt
18084
10:57:08,800 --> 10:57:14,400
okay so then let's try some code
18085
10:57:14,560 --> 10:57:18,000
we'll set our window title
18086
10:57:18,000 --> 10:57:21,360
equal to os dot path
18087
10:57:21,360 --> 10:57:24,480
dot base name of our file
18088
10:57:24,480 --> 10:57:26,960
so that we will change the title of the
18089
10:57:26,960 --> 10:57:27,520
window
18090
10:57:27,520 --> 10:57:29,520
to match whatever the file is that we
18091
10:57:29,520 --> 10:57:31,840
opened
18092
10:57:32,560 --> 10:57:35,596
then let's take our text
18093
10:57:35,596 --> 10:57:38,960
area delete what's currently here
18094
10:57:38,960 --> 10:57:42,960
from one until the end
18095
10:57:42,960 --> 10:57:47,436
take our file use the open function
18096
10:57:47,436 --> 10:57:52,960
open our file and read it so pass in our
18097
10:57:52,960 --> 10:57:57,916
text area dot insert
18098
10:57:58,720 --> 10:58:03,840
beginning at index of 1.0
18099
10:58:04,160 --> 10:58:07,360
file dot read
18100
10:58:07,360 --> 10:58:11,520
that you can catch any exceptions to
18101
10:58:11,520 --> 10:58:13,680
i'll just write accept exception but
18102
10:58:13,680 --> 10:58:15,436
it's better to name specific
18103
10:58:15,436 --> 10:58:20,160
exceptions i'll just print
18104
10:58:20,800 --> 10:58:24,000
couldn't read
18105
10:58:24,000 --> 10:58:28,720
file and we should close our file at the
18106
10:58:28,720 --> 10:58:31,040
end within a finally block
18107
10:58:31,040 --> 10:58:35,040
finally file dot close
18108
10:58:35,040 --> 10:58:37,200
we'll need a sample file to read so i'm
18109
10:58:37,200 --> 10:58:40,880
going to create a new file on my desktop
18110
10:58:40,880 --> 10:58:44,160
i'll call this test.txt
18111
10:58:44,160 --> 10:58:47,756
and let's write something whoa
18112
10:58:47,756 --> 10:58:53,040
you can actually read this
18113
10:58:53,436 --> 10:58:56,480
awesome okay let's save
18114
10:58:56,480 --> 10:59:01,040
close out of it add run our program
18115
10:59:01,040 --> 10:59:05,596
file open test.txt
18116
10:59:05,596 --> 10:59:08,800
open whoa you can actually read this
18117
10:59:08,800 --> 10:59:12,160
awesome nice so we know that the open
18118
10:59:12,160 --> 10:59:13,756
function is working
18119
10:59:13,756 --> 10:59:15,840
if we need to create a new file just go
18120
10:59:15,840 --> 10:59:17,596
to file new
18121
10:59:17,596 --> 10:59:20,800
and that will delete our old text
18122
10:59:20,800 --> 10:59:25,040
so lastly we have save file
18123
10:59:25,040 --> 10:59:28,400
underneath the save file function file
18124
10:59:28,400 --> 10:59:31,436
equals file dialog
18125
10:59:31,436 --> 10:59:34,720
dot ask save as
18126
10:59:34,720 --> 10:59:38,400
file name so this will be the default
18127
10:59:38,400 --> 10:59:40,080
name for the file but you can change
18128
10:59:40,080 --> 10:59:41,596
that
18129
10:59:41,596 --> 10:59:45,200
initial file equals
18130
10:59:45,200 --> 10:59:49,040
let's say untitled
18131
10:59:49,040 --> 10:59:51,276
dot
18132
10:59:53,680 --> 10:59:57,680
you txt set a default extension default
18133
10:59:57,680 --> 11:00:03,520
extension equals let's say txt
18134
11:00:05,840 --> 11:00:11,040
file types equals
18135
11:00:14,080 --> 11:00:17,756
all files
18136
11:00:18,080 --> 11:00:22,160
comma then asterisk dot asterisk
18137
11:00:22,160 --> 11:00:25,360
for all file types
18138
11:00:30,320 --> 11:00:34,560
and any text documents text
18139
11:00:34,840 --> 11:00:37,840
documents
18140
11:00:38,320 --> 11:00:43,116
asterisk dot txt
18141
11:00:46,160 --> 11:00:49,276
okay if file
18142
11:00:49,276 --> 11:00:52,720
is none that's if they close out of our
18143
11:00:52,720 --> 11:00:55,436
file dialog
18144
11:00:56,000 --> 11:00:58,960
we'll just return
18145
11:00:59,436 --> 11:01:01,840
else
18146
11:01:03,680 --> 11:01:07,200
so we'll set the window title first
18147
11:01:07,200 --> 11:01:11,276
window title os dot path
18148
11:01:11,276 --> 11:01:15,276
dot base name
18149
11:01:16,840 --> 11:01:18,800
file
18150
11:01:18,800 --> 11:01:22,080
file equals open open
18151
11:01:22,080 --> 11:01:26,240
our file and write
18152
11:01:27,276 --> 11:01:31,116
file dot right
18153
11:01:31,116 --> 11:01:34,320
take our text area and get
18154
11:01:34,320 --> 11:01:37,360
the text from
18155
11:01:37,360 --> 11:01:41,680
index one through the end
18156
11:01:42,800 --> 11:01:45,040
catch any exceptions it's better to name
18157
11:01:45,040 --> 11:01:46,000
them instead of just
18158
11:01:46,000 --> 11:01:49,436
doing a general accept exception
18159
11:01:49,436 --> 11:01:53,436
but i'm just going to print couldn't
18160
11:01:53,436 --> 11:01:56,880
save file
18161
11:01:57,200 --> 11:02:00,320
and finally close our file
18162
11:02:00,320 --> 11:02:03,916
file dot close all right so
18163
11:02:03,916 --> 11:02:07,360
let's test it so i'm going to change
18164
11:02:07,360 --> 11:02:11,680
the font i wonder if impact is in here
18165
11:02:11,756 --> 11:02:15,360
yes it is i'll change the font size
18166
11:02:15,360 --> 11:02:18,400
maybe 30 and i'll pick a color
18167
11:02:18,400 --> 11:02:22,240
how about this swamp green color okay so
18168
11:02:22,240 --> 11:02:24,480
write something perhaps some lyrics to a
18169
11:02:24,480 --> 11:02:29,840
song you like
18170
11:02:40,000 --> 11:02:42,480
i should probably test copying and
18171
11:02:42,480 --> 11:02:43,596
pasting
18172
11:02:43,596 --> 11:02:46,400
edit copy
18173
11:02:46,880 --> 11:02:49,680
edit paste
18174
11:02:50,720 --> 11:02:53,040
nice
18175
11:02:55,116 --> 11:02:56,880
all right let's actually try and save
18176
11:02:56,880 --> 11:02:58,640
this now i'll go to
18177
11:02:58,640 --> 11:03:02,400
file save the default file name is
18178
11:03:02,400 --> 11:03:05,756
untitled because we set it to that
18179
11:03:05,756 --> 11:03:09,520
i'll say all star
18180
11:03:09,520 --> 11:03:12,080
lyrics
18181
11:03:13,916 --> 11:03:17,116
and save now i should be able to open
18182
11:03:17,116 --> 11:03:18,800
this
18183
11:03:18,800 --> 11:03:21,040
so i'm going to go to my desktop here's
18184
11:03:21,040 --> 11:03:21,916
that text file
18185
11:03:21,916 --> 11:03:23,840
i'm going to open it and here are the
18186
11:03:23,840 --> 11:03:25,596
lyrics that i wrote within my text
18187
11:03:25,596 --> 11:03:26,240
editor
18188
11:03:26,240 --> 11:03:29,040
and let's try and read this so let's go
18189
11:03:29,040 --> 11:03:29,596
to
18190
11:03:29,596 --> 11:03:33,436
file open find your text document
18191
11:03:33,436 --> 11:03:35,680
open and would you look at that my
18192
11:03:35,680 --> 11:03:37,200
lyrics are still here
18193
11:03:37,200 --> 11:03:39,200
however this program doesn't save the
18194
11:03:39,200 --> 11:03:40,640
font
18195
11:03:40,640 --> 11:03:42,400
so i thought of a fun last minute
18196
11:03:42,400 --> 11:03:44,160
project that we can do we can actually
18197
11:03:44,160 --> 11:03:46,640
use our text editor to write a python
18198
11:03:46,640 --> 11:03:49,200
file much like what we do with pycharm
18199
11:03:49,200 --> 11:03:51,916
so let's write a program using our text
18200
11:03:51,916 --> 11:03:53,360
editor program
18201
11:03:53,360 --> 11:03:56,560
it'll be just a simple program let's
18202
11:03:56,560 --> 11:03:58,560
print
18203
11:03:58,560 --> 11:04:02,720
hello world and ask for some user input
18204
11:04:02,720 --> 11:04:07,840
let's say name equals input
18205
11:04:09,116 --> 11:04:13,276
what's your name
18206
11:04:14,480 --> 11:04:17,596
and we will print
18207
11:04:18,840 --> 11:04:21,680
hello plus our name
18208
11:04:21,680 --> 11:04:24,720
variable and you can change the font and
18209
11:04:24,720 --> 11:04:27,756
everything too if you want
18210
11:04:31,040 --> 11:04:34,640
okay so let's save this file save
18211
11:04:34,640 --> 11:04:36,960
i'll save this to my desktop but make
18212
11:04:36,960 --> 11:04:38,560
sure you save it as a
18213
11:04:38,560 --> 11:04:42,560
py file a python file i'll name this as
18214
11:04:42,560 --> 11:04:47,596
hello dot py and save
18215
11:04:47,596 --> 11:04:49,840
now to run a file using command prompt
18216
11:04:49,840 --> 11:04:50,720
we need to find
18217
11:04:50,720 --> 11:04:52,720
the directory that contains our python
18218
11:04:52,720 --> 11:04:56,160
file mine is hello.py and it's located
18219
11:04:56,160 --> 11:04:57,596
on my desktop
18220
11:04:57,596 --> 11:05:00,160
so i need the location right click go to
18221
11:05:00,160 --> 11:05:01,116
properties
18222
11:05:01,116 --> 11:05:03,840
you can copy the location then within
18223
11:05:03,840 --> 11:05:05,840
command prompt or terminal
18224
11:05:05,840 --> 11:05:08,400
you can change directory by typing cd
18225
11:05:08,400 --> 11:05:09,116
space
18226
11:05:09,116 --> 11:05:11,436
and then paste your new directory the
18227
11:05:11,436 --> 11:05:12,800
new location
18228
11:05:12,800 --> 11:05:15,840
enter then to run a python program
18229
11:05:15,840 --> 11:05:19,436
type python space and then the name of
18230
11:05:19,436 --> 11:05:20,080
the program
18231
11:05:20,080 --> 11:05:23,200
the python file mine is hello dot
18232
11:05:23,200 --> 11:05:26,960
py hit enter hello world what is your
18233
11:05:26,960 --> 11:05:27,596
name
18234
11:05:27,596 --> 11:05:31,200
bro hello bro i just thought that would
18235
11:05:31,200 --> 11:05:32,800
be a fun last minute project
18236
11:05:32,800 --> 11:05:33,916
that we can do you don't even
18237
11:05:33,916 --> 11:05:36,080
technically need an ide to write a
18238
11:05:36,080 --> 11:05:37,276
python program
18239
11:05:37,276 --> 11:05:39,840
screw you pycharm well everyone that is
18240
11:05:39,840 --> 11:05:40,800
a basic
18241
11:05:40,800 --> 11:05:43,276
text editor program i will post the code
18242
11:05:43,276 --> 11:05:44,160
for this program
18243
11:05:44,160 --> 11:05:46,400
in the comment section down below and
18244
11:05:46,400 --> 11:05:48,800
well yeah that's a basic text editor
18245
11:05:48,800 --> 11:05:49,436
program
18246
11:05:49,436 --> 11:05:52,480
using python all right
18247
11:05:52,480 --> 11:05:54,640
ladies and gentlemen let's create a game
18248
11:05:54,640 --> 11:05:56,800
of tic-tac-toe to begin we'll need to
18249
11:05:56,800 --> 11:05:59,436
import tkinter as well as random and
18250
11:05:59,436 --> 11:06:00,800
let's begin by defining
18251
11:06:00,800 --> 11:06:02,240
all of the different functions that
18252
11:06:02,240 --> 11:06:04,880
we'll need let's define a function named
18253
11:06:04,880 --> 11:06:08,320
next turn for the time being we'll just
18254
11:06:08,320 --> 11:06:10,800
write pass we'll fill this in later on
18255
11:06:10,800 --> 11:06:15,680
we'll need a function named check winner
18256
11:06:16,720 --> 11:06:21,040
a function named empty spaces to check
18257
11:06:21,040 --> 11:06:24,080
if there are any empty spaces left and
18258
11:06:24,080 --> 11:06:26,000
lastly a function named new
18259
11:06:26,000 --> 11:06:29,040
game that will launch a new game for us
18260
11:06:29,040 --> 11:06:31,200
okay we have our four functions now
18261
11:06:31,200 --> 11:06:32,640
let's create a window
18262
11:06:32,640 --> 11:06:35,840
let's create a window window equals
18263
11:06:35,840 --> 11:06:39,040
tk and at the end of our program we need
18264
11:06:39,040 --> 11:06:40,240
to use window
18265
11:06:40,240 --> 11:06:43,916
dot main loop and when we run this we
18266
11:06:43,916 --> 11:06:45,360
should have just a small
18267
11:06:45,360 --> 11:06:49,360
basic window let's set the title window
18268
11:06:49,360 --> 11:06:53,200
dot title let's set this to
18269
11:06:53,200 --> 11:06:57,200
tick tack toe
18270
11:06:57,200 --> 11:07:00,160
we'll need a list of players players
18271
11:07:00,160 --> 11:07:02,080
equals
18272
11:07:02,080 --> 11:07:05,360
x comma o
18273
11:07:05,360 --> 11:07:07,116
and with the way that we're writing this
18274
11:07:07,116 --> 11:07:08,560
program we can swap
18275
11:07:08,560 --> 11:07:10,880
these symbols with a different character
18276
11:07:10,880 --> 11:07:12,880
for example we could say
18277
11:07:12,880 --> 11:07:15,436
dollar sign is playing against the at
18278
11:07:15,436 --> 11:07:16,480
symbol too
18279
11:07:16,480 --> 11:07:18,160
but i'll demonstrate that later let's
18280
11:07:18,160 --> 11:07:21,680
keep it as x and o for now
18281
11:07:21,680 --> 11:07:24,240
now we need to select a random player to
18282
11:07:24,240 --> 11:07:25,116
begin
18283
11:07:25,116 --> 11:07:28,960
player equals random
18284
11:07:28,960 --> 11:07:32,160
dot choice and pass in our list of
18285
11:07:32,160 --> 11:07:33,040
players
18286
11:07:33,040 --> 11:07:35,040
now we'll need nine buttons i'm going to
18287
11:07:35,040 --> 11:07:36,240
create a 2d
18288
11:07:36,240 --> 11:07:39,520
list of buttons named buttons
18289
11:07:39,520 --> 11:07:42,800
and this will be the first row this will
18290
11:07:42,800 --> 11:07:44,400
be the second row
18291
11:07:44,400 --> 11:07:45,840
for the time being i'm just going to
18292
11:07:45,840 --> 11:07:47,436
initialize these with
18293
11:07:47,436 --> 11:07:50,640
0 for everything and put these all
18294
11:07:50,640 --> 11:07:51,756
within
18295
11:07:51,756 --> 11:07:54,720
a list so we have a 2d list named
18296
11:07:54,720 --> 11:07:56,000
buttons
18297
11:07:56,000 --> 11:07:58,160
and to better visualize this i'm going
18298
11:07:58,160 --> 11:08:00,400
to place each row on a new line
18299
11:08:00,400 --> 11:08:01,680
so that's what our board is going to
18300
11:08:01,680 --> 11:08:04,240
look like we have a 2d list
18301
11:08:04,240 --> 11:08:06,800
named buttons we'll need a label to
18302
11:08:06,800 --> 11:08:08,480
display whose turn it is
18303
11:08:08,480 --> 11:08:11,756
label equals label
18304
11:08:11,756 --> 11:08:15,520
i'll set the text equal to
18305
11:08:15,520 --> 11:08:18,960
player plus the word
18306
11:08:18,960 --> 11:08:22,320
turn and i'll set the font to
18307
11:08:22,320 --> 11:08:25,436
font equals pick whatever font that you
18308
11:08:25,436 --> 11:08:27,756
want
18309
11:08:30,480 --> 11:08:33,520
and i am going to pack this label
18310
11:08:33,520 --> 11:08:37,200
label dot pack and set the side
18311
11:08:37,200 --> 11:08:40,880
equal to top and let's run this just to
18312
11:08:40,880 --> 11:08:42,400
test it
18313
11:08:42,400 --> 11:08:45,040
okay looks like it's x's turn let's try
18314
11:08:45,040 --> 11:08:46,480
it again
18315
11:08:46,480 --> 11:08:49,596
oh stern okay that's fine for now
18316
11:08:49,596 --> 11:08:52,640
let's create a reset button
18317
11:08:52,640 --> 11:08:55,756
reset underscore button
18318
11:08:55,756 --> 11:08:59,520
equals button
18319
11:08:59,520 --> 11:09:02,800
i'll set the text equal to
18320
11:09:02,800 --> 11:09:04,880
restart it's going to be a restart
18321
11:09:04,880 --> 11:09:07,276
button or reset button
18322
11:09:07,276 --> 11:09:14,560
pick a font
18323
11:09:14,560 --> 11:09:17,840
maybe i'll set the size to 20
18324
11:09:17,840 --> 11:09:21,040
and we'll need a command command
18325
11:09:21,040 --> 11:09:24,240
equals new game
18326
11:09:24,240 --> 11:09:26,240
when we click on this button it's going
18327
11:09:26,240 --> 11:09:27,436
to call this
18328
11:09:27,436 --> 11:09:31,360
new game function for us and we need to
18329
11:09:31,360 --> 11:09:34,400
pack this button reset
18330
11:09:34,400 --> 11:09:37,520
button dot pack
18331
11:09:37,520 --> 11:09:40,320
and i will set the side equal to top
18332
11:09:40,320 --> 11:09:41,040
okay
18333
11:09:41,040 --> 11:09:43,756
let's see if that appears alright so we
18334
11:09:43,756 --> 11:09:45,040
have our
18335
11:09:45,040 --> 11:09:47,276
turn order a label that will display
18336
11:09:47,276 --> 11:09:49,040
whose turn it is as well as our reset
18337
11:09:49,040 --> 11:09:50,160
button
18338
11:09:50,160 --> 11:09:51,680
now we'll need to create all of the
18339
11:09:51,680 --> 11:09:53,596
buttons to add to our 2d
18340
11:09:53,596 --> 11:09:55,916
list named buttons but i'm going to
18341
11:09:55,916 --> 11:09:57,040
place these all within
18342
11:09:57,040 --> 11:10:00,320
a frame frame equals frame
18343
11:10:00,320 --> 11:10:03,360
we're adding our frame to our window and
18344
11:10:03,360 --> 11:10:04,400
i'm going to
18345
11:10:04,400 --> 11:10:08,640
pack this frame frame dot pack
18346
11:10:08,640 --> 11:10:11,360
now we'll take our 2d list of buttons
18347
11:10:11,360 --> 11:10:13,200
and add a button to each spot
18348
11:10:13,200 --> 11:10:14,800
and i think the best way to do this
18349
11:10:14,800 --> 11:10:17,276
would be to use nested for loops
18350
11:10:17,276 --> 11:10:19,276
we'll have an outer for loop in charge
18351
11:10:19,276 --> 11:10:20,480
of the rows
18352
11:10:20,480 --> 11:10:24,240
four row in range three
18353
11:10:24,240 --> 11:10:26,400
remember we only have three rows and
18354
11:10:26,400 --> 11:10:27,520
three columns
18355
11:10:27,520 --> 11:10:29,840
the inner for loop will be in charge of
18356
11:10:29,840 --> 11:10:30,720
the columns
18357
11:10:30,720 --> 11:10:34,480
four column and range three
18358
11:10:34,480 --> 11:10:36,320
and inside the inner for loop we're
18359
11:10:36,320 --> 11:10:39,680
going to create a new button
18360
11:10:39,680 --> 11:10:43,200
we'll say buttons at index
18361
11:10:43,200 --> 11:10:46,400
row and column we have two indexes
18362
11:10:46,400 --> 11:10:47,436
because
18363
11:10:47,436 --> 11:10:50,640
we have a 2d list so buttons
18364
11:10:50,640 --> 11:10:53,360
at row whatever column whatever
18365
11:10:53,360 --> 11:10:54,480
depending on
18366
11:10:54,480 --> 11:10:56,640
what iteration we are within our for
18367
11:10:56,640 --> 11:10:57,520
loops
18368
11:10:57,520 --> 11:11:00,880
we'll create a new button
18369
11:11:00,880 --> 11:11:03,520
and we'll set the text oh first add this
18370
11:11:03,520 --> 11:11:04,080
to the frame
18371
11:11:04,080 --> 11:11:06,800
almost forgot about that we're adding
18372
11:11:06,800 --> 11:11:08,480
our buttons to our frame and adding the
18373
11:11:08,480 --> 11:11:10,400
frame to the window
18374
11:11:10,400 --> 11:11:12,880
now we'll set the text equal to a blank
18375
11:11:12,880 --> 11:11:14,640
set of quotes because we don't want any
18376
11:11:14,640 --> 11:11:16,800
text
18377
11:11:16,800 --> 11:11:19,200
we'll set the font i'll just copy what
18378
11:11:19,200 --> 11:11:20,240
we have here
18379
11:11:20,240 --> 11:11:22,880
for label
18380
11:11:26,320 --> 11:11:29,596
for a width let's say five and a height
18381
11:11:29,596 --> 11:11:32,560
let's say two i'm going to hit enter
18382
11:11:32,560 --> 11:11:33,916
just to move down to the next line
18383
11:11:33,916 --> 11:11:35,436
because we still have a few more things
18384
11:11:35,436 --> 11:11:36,400
to fill in
18385
11:11:36,400 --> 11:11:39,436
okay we'll need a command command
18386
11:11:39,436 --> 11:11:41,520
equals and i'm going to set this equal
18387
11:11:41,520 --> 11:11:42,800
to a lambda function
18388
11:11:42,800 --> 11:11:45,200
so we need some arguments actually i
18389
11:11:45,200 --> 11:11:46,400
think i'll pass in some keyword
18390
11:11:46,400 --> 11:11:46,960
arguments
18391
11:11:46,960 --> 11:11:50,080
row equals row and column
18392
11:11:50,080 --> 11:11:54,400
equals column then our expression is
18393
11:11:54,400 --> 11:11:57,756
next turn and we will pass in
18394
11:11:57,756 --> 11:12:00,880
row and column now we also need to add
18395
11:12:00,880 --> 11:12:02,000
our buttons to our
18396
11:12:02,000 --> 11:12:05,436
frame buttons at
18397
11:12:05,436 --> 11:12:09,596
index of row and column
18398
11:12:09,596 --> 11:12:12,400
we'll use the grid function for this
18399
11:12:12,400 --> 11:12:13,680
grid
18400
11:12:13,680 --> 11:12:16,800
row equals and
18401
11:12:16,800 --> 11:12:20,560
column equals column
18402
11:12:20,560 --> 11:12:22,320
and it's always a good idea to test your
18403
11:12:22,320 --> 11:12:24,800
program after making any major changes
18404
11:12:24,800 --> 11:12:27,756
so we should have our label our reset
18405
11:12:27,756 --> 11:12:28,400
button
18406
11:12:28,400 --> 11:12:31,680
and our grid of buttons now that the
18407
11:12:31,680 --> 11:12:33,680
main body of our program is complete
18408
11:12:33,680 --> 11:12:36,720
let's head to the next turn function
18409
11:12:36,720 --> 11:12:38,960
and remember that we're passing in row
18410
11:12:38,960 --> 11:12:40,000
and column as
18411
11:12:40,000 --> 11:12:42,080
arguments so we need to set up those
18412
11:12:42,080 --> 11:12:44,240
parameters within the next turn
18413
11:12:44,240 --> 11:12:47,520
function row and column and the first
18414
11:12:47,520 --> 11:12:48,480
line within here
18415
11:12:48,480 --> 11:12:52,160
we would like access to our player
18416
11:12:52,160 --> 11:12:54,480
so let's say global player so that we
18417
11:12:54,480 --> 11:12:56,000
have access to it
18418
11:12:56,000 --> 11:12:58,800
and we're first going to check to see if
18419
11:12:58,800 --> 11:12:59,436
the button
18420
11:12:59,436 --> 11:13:03,520
that we click on is empty so buttons
18421
11:13:03,520 --> 11:13:07,276
at index of row and index of column
18422
11:13:07,276 --> 11:13:10,000
to access the text of a button just add
18423
11:13:10,000 --> 11:13:10,480
a
18424
11:13:10,480 --> 11:13:14,080
third index operator and type in text
18425
11:13:14,080 --> 11:13:17,276
if the text of our button that we click
18426
11:13:17,276 --> 11:13:20,400
is equal to a set of quotes that means
18427
11:13:20,400 --> 11:13:21,840
it's empty
18428
11:13:21,840 --> 11:13:25,756
and when we call check winner
18429
11:13:25,756 --> 11:13:28,640
winner winner chicken dinner and it's
18430
11:13:28,640 --> 11:13:29,116
false
18431
11:13:29,116 --> 11:13:32,960
there is no winner then we will fill in
18432
11:13:32,960 --> 11:13:35,520
that button with our player our player's
18433
11:13:35,520 --> 11:13:37,116
character whatever it is
18434
11:13:37,116 --> 11:13:40,320
x or o or whatever we decide
18435
11:13:40,320 --> 11:13:44,160
so within here let's check to see if
18436
11:13:44,160 --> 11:13:47,916
player is equal to players
18437
11:13:47,916 --> 11:13:51,040
at index zero that is our first player
18438
11:13:51,040 --> 11:13:52,880
and remember that we created
18439
11:13:52,880 --> 11:13:55,756
a list of players and we're planning on
18440
11:13:55,756 --> 11:13:56,960
swapping these later
18441
11:13:56,960 --> 11:13:59,520
we could say like if player is equal to
18442
11:13:59,520 --> 11:14:00,080
x
18443
11:14:00,080 --> 11:14:01,840
but that would make our program less
18444
11:14:01,840 --> 11:14:03,520
flexible if we want to pick like a
18445
11:14:03,520 --> 11:14:05,360
different character instead of an x
18446
11:14:05,360 --> 11:14:08,240
so if player is equal to player at index
18447
11:14:08,240 --> 11:14:11,520
zero whatever symbol is there
18448
11:14:11,520 --> 11:14:14,640
then we are going to take buttons
18449
11:14:14,640 --> 11:14:19,276
at index of row index of column
18450
11:14:19,276 --> 11:14:23,680
take our text and set it equal to
18451
11:14:23,680 --> 11:14:26,320
our player
18452
11:14:27,276 --> 11:14:29,116
and we should check to see if there's a
18453
11:14:29,116 --> 11:14:32,000
winner after filling in this button
18454
11:14:32,000 --> 11:14:35,116
if check
18455
11:14:35,116 --> 11:14:37,200
winner and we still need to fill in this
18456
11:14:37,200 --> 11:14:38,640
function
18457
11:14:38,640 --> 11:14:42,080
returns false
18458
11:14:42,080 --> 11:14:44,560
then we will switch players player
18459
11:14:44,560 --> 11:14:45,360
equals
18460
11:14:45,360 --> 11:14:50,400
players at index of one and
18461
11:14:50,400 --> 11:14:54,880
label dot config
18462
11:14:55,116 --> 11:14:58,880
text equals
18463
11:14:59,680 --> 11:15:02,720
players at index 1
18464
11:15:02,720 --> 11:15:06,880
plus the word turn
18465
11:15:06,880 --> 11:15:08,720
so what we did here is that we're
18466
11:15:08,720 --> 11:15:09,916
checking to see
18467
11:15:09,916 --> 11:15:13,680
if after placing our text of our player
18468
11:15:13,680 --> 11:15:16,640
on that button that we click if there is
18469
11:15:16,640 --> 11:15:17,436
no winner
18470
11:15:17,436 --> 11:15:20,080
then we're going to swap players player
18471
11:15:20,080 --> 11:15:21,680
equals our next player
18472
11:15:21,680 --> 11:15:23,840
and we're configuring our label so that
18473
11:15:23,840 --> 11:15:26,240
it displays the next player's turn
18474
11:15:26,240 --> 11:15:29,520
players at index 1 turn
18475
11:15:29,520 --> 11:15:32,240
now what if there is a winner let's say
18476
11:15:32,240 --> 11:15:33,680
else if
18477
11:15:33,680 --> 11:15:36,880
check winner
18478
11:15:36,880 --> 11:15:40,560
is true
18479
11:15:40,800 --> 11:15:44,840
then we will take our label dot
18480
11:15:44,840 --> 11:15:48,160
config and set the text
18481
11:15:48,160 --> 11:15:51,360
equal to players
18482
11:15:51,360 --> 11:15:54,560
at index 0
18483
11:15:54,560 --> 11:15:59,276
plus the word wins
18484
11:15:59,276 --> 11:16:02,400
now let's add another else if statement
18485
11:16:02,400 --> 11:16:06,480
else if there's a tie let's say check
18486
11:16:06,480 --> 11:16:09,520
winner is equal to
18487
11:16:09,520 --> 11:16:12,320
the word tie because we are already
18488
11:16:12,320 --> 11:16:12,800
using
18489
11:16:12,800 --> 11:16:16,800
false and true then let's change our
18490
11:16:16,800 --> 11:16:17,360
label
18491
11:16:17,360 --> 11:16:20,400
and i'm just going to copy this
18492
11:16:20,400 --> 11:16:23,596
text equals the word
18493
11:16:23,596 --> 11:16:26,720
tie okay
18494
11:16:26,720 --> 11:16:29,840
so let's create an else block
18495
11:16:29,840 --> 11:16:32,880
so that goes right here
18496
11:16:32,880 --> 11:16:35,680
so if it's not player one's turn player
18497
11:16:35,680 --> 11:16:37,276
at index zero
18498
11:16:37,276 --> 11:16:39,756
then it's our other player's turn player
18499
11:16:39,756 --> 11:16:40,880
at index
18500
11:16:40,880 --> 11:16:43,756
of one so oh then so we just need to
18501
11:16:43,756 --> 11:16:46,080
mostly just copy this text and change a
18502
11:16:46,080 --> 11:16:47,520
few things around
18503
11:16:47,520 --> 11:16:51,276
so i'm going to copy all this
18504
11:16:52,800 --> 11:16:56,000
and within here let's set
18505
11:16:56,000 --> 11:16:59,360
player equals players at index
18506
11:16:59,360 --> 11:17:02,640
zero then players at
18507
11:17:02,640 --> 11:17:06,160
index zero's turn players
18508
11:17:06,160 --> 11:17:09,276
at index one wins
18509
11:17:09,276 --> 11:17:11,840
and that is it now our next turn
18510
11:17:11,840 --> 11:17:13,916
function is now complete let's head down
18511
11:17:13,916 --> 11:17:14,480
to the
18512
11:17:14,480 --> 11:17:16,480
check winner function and we need to
18513
11:17:16,480 --> 11:17:18,000
check all of the different win
18514
11:17:18,000 --> 11:17:18,960
conditions
18515
11:17:18,960 --> 11:17:22,000
and return it true if somebody won false
18516
11:17:22,000 --> 11:17:24,480
if there is yet no winner and the word
18517
11:17:24,480 --> 11:17:26,240
tie if it's a tie
18518
11:17:26,240 --> 11:17:28,720
so let's check all of the horizontal win
18519
11:17:28,720 --> 11:17:29,680
conditions
18520
11:17:29,680 --> 11:17:32,960
so we can do this using a for loop for
18521
11:17:32,960 --> 11:17:37,040
row in range three
18522
11:17:37,040 --> 11:17:39,436
we need to check the text of each button
18523
11:17:39,436 --> 11:17:40,640
in each row
18524
11:17:40,640 --> 11:17:44,720
if buttons at index of row
18525
11:17:44,720 --> 11:17:48,080
and column index of 0 we'll use the
18526
11:17:48,080 --> 11:17:50,720
index operator to check the text
18527
11:17:50,720 --> 11:17:54,480
is equal to the next button in our row
18528
11:17:54,480 --> 11:17:57,276
so i will copy this and change zero to
18529
11:17:57,276 --> 11:17:58,720
one
18530
11:17:58,720 --> 11:18:02,080
check to see if that is equal to buttons
18531
11:18:02,080 --> 11:18:06,400
at index of row index of two
18532
11:18:06,400 --> 11:18:08,640
and i'm going to check to see if this is
18533
11:18:08,640 --> 11:18:10,160
all not
18534
11:18:10,160 --> 11:18:13,276
equal let me move this a little bit
18535
11:18:13,276 --> 11:18:16,560
to a space
18536
11:18:17,596 --> 11:18:19,840
so if that is the case that means
18537
11:18:19,840 --> 11:18:20,880
somebody won
18538
11:18:20,880 --> 11:18:23,520
so if all of these buttons are the same
18539
11:18:23,520 --> 11:18:25,200
and they are not equal to
18540
11:18:25,200 --> 11:18:27,360
an empty space that means they're all
18541
11:18:27,360 --> 11:18:29,596
the same so let's return
18542
11:18:29,596 --> 11:18:32,640
true that means that somebody won
18543
11:18:32,640 --> 11:18:34,960
no we need to check the vertical win
18544
11:18:34,960 --> 11:18:35,680
conditions
18545
11:18:35,680 --> 11:18:38,240
if anybody has the same character all
18546
11:18:38,240 --> 11:18:39,040
the way down
18547
11:18:39,040 --> 11:18:41,756
a single column this next for loop is
18548
11:18:41,756 --> 11:18:44,880
going to be four column in range three
18549
11:18:44,880 --> 11:18:47,520
for the first button this will be button
18550
11:18:47,520 --> 11:18:49,436
at index of zero
18551
11:18:49,436 --> 11:18:52,880
and column then
18552
11:18:52,880 --> 11:18:56,720
one and column let me just paste that
18553
11:18:56,720 --> 11:19:01,596
and two and column
18554
11:19:01,680 --> 11:19:03,840
now we need to check the diagonal win
18555
11:19:03,840 --> 11:19:04,720
conditions
18556
11:19:04,720 --> 11:19:07,916
so if buttons at
18557
11:19:07,916 --> 11:19:11,756
index 0 0 that's the top left corner
18558
11:19:11,756 --> 11:19:14,880
if the text of that button is equal to
18559
11:19:14,880 --> 11:19:19,596
buttons at indexes of one one
18560
11:19:19,596 --> 11:19:23,116
is equal to the indexes of
18561
11:19:23,116 --> 11:19:27,040
button two two and if all of this
18562
11:19:27,040 --> 11:19:30,880
does not equal an empty space
18563
11:19:30,880 --> 11:19:35,680
then return true so there is a winner
18564
11:19:35,680 --> 11:19:37,756
and we have one last win condition to
18565
11:19:37,756 --> 11:19:39,276
check that is the other
18566
11:19:39,276 --> 11:19:41,680
diagonal win condition so we just need
18567
11:19:41,680 --> 11:19:44,080
to change some of these indexes around
18568
11:19:44,080 --> 11:19:47,200
so we have zero two
18569
11:19:47,200 --> 11:19:50,800
one one and two zero
18570
11:19:50,800 --> 11:19:52,640
i'm going to change this statement to an
18571
11:19:52,640 --> 11:19:54,240
else if statement
18572
11:19:54,240 --> 11:19:56,000
no we need to check to see if there's
18573
11:19:56,000 --> 11:19:58,000
any spaces remaining
18574
11:19:58,000 --> 11:20:01,756
else if we will call the empty spaces
18575
11:20:01,756 --> 11:20:05,116
function which we have yet to fill in if
18576
11:20:05,116 --> 11:20:09,200
this returns false then we will
18577
11:20:09,200 --> 11:20:12,560
return the word
18578
11:20:12,560 --> 11:20:14,800
tie
18579
11:20:15,916 --> 11:20:19,756
and lastly else else there is no winner
18580
11:20:19,756 --> 11:20:20,080
and
18581
11:20:20,080 --> 11:20:24,560
no tie so we will return false
18582
11:20:24,560 --> 11:20:27,360
and that is it for the check winner
18583
11:20:27,360 --> 11:20:28,080
function
18584
11:20:28,080 --> 11:20:30,880
let's test this
18585
11:20:31,436 --> 11:20:33,680
so this doesn't account for a tie quite
18586
11:20:33,680 --> 11:20:35,040
yet but we can fill in
18587
11:20:35,040 --> 11:20:37,916
some of these spaces looks like x wins
18588
11:20:37,916 --> 11:20:40,240
and we can no longer fill in buttons
18589
11:20:40,240 --> 11:20:42,160
and we cannot start a new game quite yet
18590
11:20:42,160 --> 11:20:43,360
either
18591
11:20:43,360 --> 11:20:45,840
within the empty spaces function let's
18592
11:20:45,840 --> 11:20:47,040
create a local
18593
11:20:47,040 --> 11:20:49,596
variable named spaces and set this equal
18594
11:20:49,596 --> 11:20:53,200
to nine whenever we call this function
18595
11:20:53,200 --> 11:20:56,800
four row in range
18596
11:20:56,800 --> 11:20:59,840
three and we'll create a nested for loop
18597
11:20:59,840 --> 11:21:04,640
for column in range
18598
11:21:04,640 --> 11:21:07,756
three we will check the text of each
18599
11:21:07,756 --> 11:21:08,400
button
18600
11:21:08,400 --> 11:21:11,520
if buttons at indexes of
18601
11:21:11,520 --> 11:21:15,360
row and column
18602
11:21:15,360 --> 11:21:20,320
and we will check the text to see
18603
11:21:21,596 --> 11:21:26,320
if it's not equal to an empty space
18604
11:21:26,320 --> 11:21:29,596
if that is the case spaces
18605
11:21:29,596 --> 11:21:33,436
minus equals one
18606
11:21:33,436 --> 11:21:36,160
then we'll write an if statement if
18607
11:21:36,160 --> 11:21:36,880
spaces
18608
11:21:36,880 --> 11:21:41,436
as in spaces remaining is equal to zero
18609
11:21:41,436 --> 11:21:44,400
that means we will return false and
18610
11:21:44,400 --> 11:21:45,276
there are no
18611
11:21:45,276 --> 11:21:49,756
spaces left else
18612
11:21:49,756 --> 11:21:52,640
we will return
18613
11:21:52,960 --> 11:21:56,960
true okay let's test it
18614
11:21:56,960 --> 11:22:05,840
so this time i'm trying to get a tie
18615
11:22:06,880 --> 11:22:10,000
yep looks like it's a tie this next
18616
11:22:10,000 --> 11:22:11,596
part's optional but i would like to
18617
11:22:11,596 --> 11:22:12,000
change
18618
11:22:12,000 --> 11:22:14,080
the color of each button for the winning
18619
11:22:14,080 --> 11:22:15,360
combination
18620
11:22:15,360 --> 11:22:17,680
so within our check winner function
18621
11:22:17,680 --> 11:22:20,160
underneath the first win condition
18622
11:22:20,160 --> 11:22:23,840
i'm going to take buttons at index
18623
11:22:23,840 --> 11:22:29,276
of row and index of zero
18624
11:22:29,360 --> 11:22:33,680
use the config method
18625
11:22:33,680 --> 11:22:36,400
and set the background color equal to
18626
11:22:36,400 --> 11:22:39,200
let's say green
18627
11:22:39,436 --> 11:22:41,520
and i'm going to repeat this process for
18628
11:22:41,520 --> 11:22:43,116
the other buttons within this win
18629
11:22:43,116 --> 11:22:44,160
condition
18630
11:22:44,160 --> 11:22:47,360
so that is row
18631
11:22:47,360 --> 11:22:50,880
zero row one and row two
18632
11:22:50,880 --> 11:22:55,040
i'll copy all of this paste it
18633
11:22:55,040 --> 11:22:57,040
and switch some of these around so these
18634
11:22:57,040 --> 11:23:00,720
should all be matching
18635
11:23:00,720 --> 11:23:03,596
so zero and column one and column and
18636
11:23:03,596 --> 11:23:06,400
two in column
18637
11:23:08,240 --> 11:23:11,276
okay so we have zero zero one
18638
11:23:11,276 --> 11:23:14,720
one and two two
18639
11:23:14,720 --> 11:23:17,436
and one more
18640
11:23:17,916 --> 11:23:20,960
zero two one one and
18641
11:23:20,960 --> 11:23:24,880
two zero now when we have a winning
18642
11:23:24,880 --> 11:23:26,000
combination
18643
11:23:26,000 --> 11:23:28,240
the color of the buttons involved in
18644
11:23:28,240 --> 11:23:30,320
that combination are going to change to
18645
11:23:30,320 --> 11:23:30,960
green
18646
11:23:30,960 --> 11:23:33,276
now if there is a tie let's change all
18647
11:23:33,276 --> 11:23:34,400
of the buttons to
18648
11:23:34,400 --> 11:23:37,596
let's say yellow so i'm going to write
18649
11:23:37,596 --> 11:23:39,360
nested for loops for this
18650
11:23:39,360 --> 11:23:43,116
four row in range
18651
11:23:43,116 --> 11:23:47,040
three for column
18652
11:23:47,040 --> 11:23:51,680
in range three
18653
11:23:51,680 --> 11:23:54,960
take our buttons at row
18654
11:23:54,960 --> 11:23:58,320
and column and change
18655
11:23:58,320 --> 11:24:00,960
the background color to yellow or some
18656
11:24:00,960 --> 11:24:03,276
other color of your choosing
18657
11:24:03,276 --> 11:24:06,240
so if there's a tie they should all be
18658
11:24:06,240 --> 11:24:08,640
yellow
18659
11:24:08,880 --> 11:24:10,880
i'm trying not to win here it's actually
18660
11:24:10,880 --> 11:24:14,320
more complex than what i thought
18661
11:24:16,080 --> 11:24:18,720
yep it looks like it's all a tie and the
18662
11:24:18,720 --> 11:24:20,240
last thing that we need to do is to fill
18663
11:24:20,240 --> 11:24:20,480
in
18664
11:24:20,480 --> 11:24:22,160
the new game function so that we can
18665
11:24:22,160 --> 11:24:24,000
begin a new game
18666
11:24:24,000 --> 11:24:26,480
so let's say global player we would like
18667
11:24:26,480 --> 11:24:27,756
access to our player
18668
11:24:27,756 --> 11:24:30,640
from inside this function and set player
18669
11:24:30,640 --> 11:24:31,436
equal
18670
11:24:31,436 --> 11:24:35,116
to a new random choice random
18671
11:24:35,116 --> 11:24:38,320
dot choice and pass in our list
18672
11:24:38,320 --> 11:24:41,756
of players and we will change our label
18673
11:24:41,756 --> 11:24:44,800
label dot config
18674
11:24:44,800 --> 11:24:48,880
and set the text equal to player
18675
11:24:48,880 --> 11:24:52,960
plus the word turn and we also need to
18676
11:24:52,960 --> 11:24:56,240
reset all of our buttons
18677
11:24:56,320 --> 11:25:00,080
for row in range
18678
11:25:00,080 --> 11:25:03,360
three for
18679
11:25:03,360 --> 11:25:07,116
column in range
18680
11:25:07,116 --> 11:25:10,800
three buttons
18681
11:25:10,800 --> 11:25:15,756
at index of row and index of column
18682
11:25:16,240 --> 11:25:19,680
dot config
18683
11:25:19,680 --> 11:25:22,480
text equals a set of quotes so it's
18684
11:25:22,480 --> 11:25:23,360
empty
18685
11:25:23,360 --> 11:25:26,080
and we'll change the background color
18686
11:25:26,080 --> 11:25:27,840
factors previously
18687
11:25:27,840 --> 11:25:30,720
so the default color for buttons is
18688
11:25:30,720 --> 11:25:32,320
actually hexadecimal
18689
11:25:32,320 --> 11:25:35,680
f 0 f 0 f 0
18690
11:25:35,680 --> 11:25:37,756
so this function will begin a new game
18691
11:25:37,756 --> 11:25:38,720
for us
18692
11:25:38,720 --> 11:25:41,520
and let's test it to be sure i'm going
18693
11:25:41,520 --> 11:25:42,480
to start a new game
18694
11:25:42,480 --> 11:25:46,080
reset reset
18695
11:25:46,080 --> 11:25:49,360
i'll actually try and win this time and
18696
11:25:49,360 --> 11:25:50,480
reset
18697
11:25:50,480 --> 11:25:52,320
okay so it looks like the new game
18698
11:25:52,320 --> 11:25:54,160
function is working
18699
11:25:54,160 --> 11:25:56,160
now another thing that you can do too is
18700
11:25:56,160 --> 11:25:57,436
that you can change
18701
11:25:57,436 --> 11:25:59,596
the players around let's say we would
18702
11:25:59,596 --> 11:26:01,276
like to play as
18703
11:26:01,276 --> 11:26:04,960
dollar sign and at sign so this program
18704
11:26:04,960 --> 11:26:06,080
is flexible enough
18705
11:26:06,080 --> 11:26:08,400
so that you can change the icons of the
18706
11:26:08,400 --> 11:26:10,240
players
18707
11:26:10,240 --> 11:26:12,400
all right everybody so that's a basic
18708
11:26:12,400 --> 11:26:14,160
game of tic-tac-toe for
18709
11:26:14,160 --> 11:26:16,160
python if you would like a copy of this
18710
11:26:16,160 --> 11:26:17,840
code i will post all of this to the
18711
11:26:17,840 --> 11:26:19,520
comments section down below
18712
11:26:19,520 --> 11:26:21,916
but yeah that's how to code a basic game
18713
11:26:21,916 --> 11:26:23,040
of tic-tac-toe
18714
11:26:23,040 --> 11:26:26,640
for python hey what's going on everybody
18715
11:26:26,640 --> 11:26:28,640
it's your bro hope you're doing well and
18716
11:26:28,640 --> 11:26:30,080
in this video we're going to create a
18717
11:26:30,080 --> 11:26:32,080
game of snake using python so
18718
11:26:32,080 --> 11:26:36,640
sit back relax and enjoy the show
18719
11:26:36,960 --> 11:26:39,040
first thing we should do is import from
18720
11:26:39,040 --> 11:26:41,840
tkinter as well as the random module
18721
11:26:41,840 --> 11:26:43,680
and let's define all of the different
18722
11:26:43,680 --> 11:26:45,840
classes and functions that we'll need
18723
11:26:45,840 --> 11:26:48,560
so let's create a class for our snake
18724
11:26:48,560 --> 11:26:49,520
object
18725
11:26:49,520 --> 11:26:52,160
as well as our food object so for the
18726
11:26:52,160 --> 11:26:54,080
time being i'm just going to write pass
18727
11:26:54,080 --> 11:26:56,560
for my classes we'll fill these in later
18728
11:26:56,560 --> 11:26:58,000
so we have class snake and
18729
11:26:58,000 --> 11:27:01,200
class food and
18730
11:27:01,200 --> 11:27:02,560
let's define all of the different
18731
11:27:02,560 --> 11:27:04,320
functions that we'll need so let's say
18732
11:27:04,320 --> 11:27:06,160
we have a function named next
18733
11:27:06,160 --> 11:27:08,400
turn
18734
11:27:10,960 --> 11:27:13,840
change direction
18735
11:27:18,800 --> 11:27:21,756
check collisions
18736
11:27:22,880 --> 11:27:27,840
and lastly game over
18737
11:27:29,916 --> 11:27:32,160
okay for change direction i'm going to
18738
11:27:32,160 --> 11:27:34,000
have one parameter
18739
11:27:34,000 --> 11:27:37,360
a new direction
18740
11:27:37,360 --> 11:27:39,360
now when i create a game i like to place
18741
11:27:39,360 --> 11:27:41,436
a bunch of constants at the top
18742
11:27:41,436 --> 11:27:44,400
of my game constants are variables that
18743
11:27:44,400 --> 11:27:45,680
you do not want to change
18744
11:27:45,680 --> 11:27:47,916
later they're kind of like the settings
18745
11:27:47,916 --> 11:27:49,360
however in python
18746
11:27:49,360 --> 11:27:51,520
there are no constants compared to other
18747
11:27:51,520 --> 11:27:52,800
programming languages
18748
11:27:52,800 --> 11:27:54,320
so we're just going to create a bunch of
18749
11:27:54,320 --> 11:27:56,560
variables that will behave like them
18750
11:27:56,560 --> 11:27:58,640
so constants are values we do not want
18751
11:27:58,640 --> 11:27:59,840
to change and they're kind of like the
18752
11:27:59,840 --> 11:28:00,800
game settings
18753
11:28:00,800 --> 11:28:02,400
and i placed them at the top for
18754
11:28:02,400 --> 11:28:04,320
convenience and the naming convention
18755
11:28:04,320 --> 11:28:05,840
for a constant is that
18756
11:28:05,840 --> 11:28:08,000
all the letters are uppercase so these
18757
11:28:08,000 --> 11:28:09,436
will be settings like
18758
11:28:09,436 --> 11:28:12,560
the game width the speed etc
18759
11:28:12,560 --> 11:28:14,800
so let's say the width of our game will
18760
11:28:14,800 --> 11:28:15,916
be 700
18761
11:28:15,916 --> 11:28:17,840
but feel free to take the liberty to
18762
11:28:17,840 --> 11:28:19,520
pick whatever size you want
18763
11:28:19,520 --> 11:28:22,640
so we have game width game height
18764
11:28:22,640 --> 11:28:24,960
i'll set this to 700 as well so it's a
18765
11:28:24,960 --> 11:28:26,240
square
18766
11:28:26,240 --> 11:28:28,240
let's say we have a speed the speed of
18767
11:28:28,240 --> 11:28:30,480
the snake how often will our canvas
18768
11:28:30,480 --> 11:28:31,200
update
18769
11:28:31,200 --> 11:28:33,596
so let's say 50 but the lower the number
18770
11:28:33,596 --> 11:28:35,436
the faster the game
18771
11:28:35,436 --> 11:28:38,480
and a space size how large are the items
18772
11:28:38,480 --> 11:28:39,436
in our game like
18773
11:28:39,436 --> 11:28:42,720
the food and body parts of the snake
18774
11:28:42,720 --> 11:28:44,960
so i'm going to pick 50 but you can
18775
11:28:44,960 --> 11:28:46,640
change this
18776
11:28:46,640 --> 11:28:50,080
and body parts how many body parts does
18777
11:28:50,080 --> 11:28:52,400
our snake have when we begin a game
18778
11:28:52,400 --> 11:28:55,680
let's say three how about a snake
18779
11:28:55,680 --> 11:28:58,960
color you can pick a color name
18780
11:28:58,960 --> 11:29:01,360
you can use rgb values or you can use a
18781
11:29:01,360 --> 11:29:02,720
hexadecimal value
18782
11:29:02,720 --> 11:29:06,880
so i'm going to pick green 0 0 ff00 but
18783
11:29:06,880 --> 11:29:09,756
you can pick any color you want
18784
11:29:09,756 --> 11:29:13,040
how about food color
18785
11:29:13,200 --> 11:29:16,240
equals red
18786
11:29:16,240 --> 11:29:20,720
and that is ff000
18787
11:29:20,720 --> 11:29:22,240
and what about a background color for
18788
11:29:22,240 --> 11:29:25,040
the canvas background
18789
11:29:25,040 --> 11:29:28,240
color i'll pick black so that is
18790
11:29:28,240 --> 11:29:30,960
six zeros
18791
11:29:31,436 --> 11:29:32,960
feel free to mess with some of these
18792
11:29:32,960 --> 11:29:35,916
colors too so we have a green snake
18793
11:29:35,916 --> 11:29:37,596
our food is going to be red and our
18794
11:29:37,596 --> 11:29:39,520
background is going to be black
18795
11:29:39,520 --> 11:29:41,596
so that is all of the constants for our
18796
11:29:41,596 --> 11:29:43,436
game but you can feel free to adjust
18797
11:29:43,436 --> 11:29:44,560
them if you like
18798
11:29:44,560 --> 11:29:46,320
let's head down to the bottom and make
18799
11:29:46,320 --> 11:29:48,000
our window so we have
18800
11:29:48,000 --> 11:29:51,436
window equals tk and at the very end we
18801
11:29:51,436 --> 11:29:52,080
should have
18802
11:29:52,080 --> 11:29:56,480
window dot main loop
18803
11:29:57,840 --> 11:30:01,276
let's set a title for this window window
18804
11:30:01,276 --> 11:30:04,000
dot title
18805
11:30:04,720 --> 11:30:08,880
snack game okay fine i'll spell it right
18806
11:30:08,880 --> 11:30:12,880
snake game and if you do not want your
18807
11:30:12,880 --> 11:30:14,320
window to be resizable
18808
11:30:14,320 --> 11:30:18,000
you can use resizable
18809
11:30:18,000 --> 11:30:20,480
and then you have to pass in false twice
18810
11:30:20,480 --> 11:30:23,116
it's kind of strange but it's how it is
18811
11:30:23,116 --> 11:30:26,240
okay we should have a small window and
18812
11:30:26,240 --> 11:30:30,160
we cannot resize this even if we tried
18813
11:30:30,240 --> 11:30:33,596
okay let's create a score label
18814
11:30:33,596 --> 11:30:35,436
but we probably need a score first so
18815
11:30:35,436 --> 11:30:37,680
let's say score equals zero
18816
11:30:37,680 --> 11:30:41,520
and an initial direction direction
18817
11:30:41,520 --> 11:30:45,200
equals let's say down
18818
11:30:45,200 --> 11:30:47,520
now let's create a score label so let's
18819
11:30:47,520 --> 11:30:49,200
say label
18820
11:30:49,200 --> 11:30:52,240
equals label we're adding our
18821
11:30:52,240 --> 11:30:56,000
label to our window let's set the text
18822
11:30:56,000 --> 11:30:59,200
equal to score
18823
11:30:59,200 --> 11:31:02,800
colon then i'm going to use the format
18824
11:31:02,800 --> 11:31:04,720
method
18825
11:31:04,720 --> 11:31:08,000
and we will pass in our score whatever
18826
11:31:08,000 --> 11:31:10,000
it is
18827
11:31:10,000 --> 11:31:12,720
and i'll set a font pick whatever font
18828
11:31:12,720 --> 11:31:15,200
you prefer
18829
11:31:17,360 --> 11:31:20,960
and 40 is a decent size
18830
11:31:21,200 --> 11:31:25,116
and then i'm going to pack this label
18831
11:31:25,116 --> 11:31:28,080
let's test it okay we're getting
18832
11:31:28,080 --> 11:31:29,756
somewhere
18833
11:31:29,756 --> 11:31:33,436
we'll need to create a canvas canvas
18834
11:31:33,436 --> 11:31:36,840
equals canvas we're adding it to our
18835
11:31:36,840 --> 11:31:39,520
window i'm going to set the background
18836
11:31:39,520 --> 11:31:40,080
color
18837
11:31:40,080 --> 11:31:44,640
equal to our background constant
18838
11:31:45,436 --> 11:31:47,916
i'm going to set our height equal to the
18839
11:31:47,916 --> 11:31:48,880
game
18840
11:31:48,880 --> 11:31:52,320
height and the width equal
18841
11:31:52,320 --> 11:31:56,880
to the game width that we set
18842
11:31:56,880 --> 11:32:01,596
and we need to pack this canvas dot pack
18843
11:32:02,080 --> 11:32:06,320
and we should have a game board
18844
11:32:06,320 --> 11:32:08,080
i'm going to try and center this window
18845
11:32:08,080 --> 11:32:09,436
when it appears
18846
11:32:09,436 --> 11:32:11,520
so we can do that using a few lines of
18847
11:32:11,520 --> 11:32:13,276
code the first thing we'll do is
18848
11:32:13,276 --> 11:32:16,320
update our window so that it renders and
18849
11:32:16,320 --> 11:32:18,480
then we need to find some dimensions
18850
11:32:18,480 --> 11:32:22,320
so let's say the window width
18851
11:32:22,320 --> 11:32:26,560
equals window dot w
18852
11:32:26,560 --> 11:32:30,800
info width
18853
11:32:31,436 --> 11:32:35,680
and window height
18854
11:32:35,680 --> 11:32:38,800
equals window
18855
11:32:38,800 --> 11:32:42,960
dot w info height
18856
11:32:43,116 --> 11:32:48,160
we'll need our screen width
18857
11:32:48,160 --> 11:32:52,560
equals window
18858
11:32:53,360 --> 11:32:56,720
dot screen width
18859
11:32:56,720 --> 11:33:00,880
and screen height
18860
11:33:00,880 --> 11:33:04,720
equals window dot w
18861
11:33:04,720 --> 11:33:07,840
info screen height
18862
11:33:07,840 --> 11:33:12,880
where is it there it is
18863
11:33:13,436 --> 11:33:15,756
okay then we need to see how much we're
18864
11:33:15,756 --> 11:33:17,840
going to adjust the position of our
18865
11:33:17,840 --> 11:33:19,040
window
18866
11:33:19,040 --> 11:33:23,436
so let's say x equals
18867
11:33:24,080 --> 11:33:28,840
screen width divided by 2
18868
11:33:28,840 --> 11:33:32,960
minus window
18869
11:33:32,960 --> 11:33:36,720
width divided by two
18870
11:33:36,800 --> 11:33:40,320
and do the same thing for y
18871
11:33:40,880 --> 11:33:45,116
except this will be screen height
18872
11:33:45,116 --> 11:33:48,480
and window height
18873
11:33:48,480 --> 11:33:51,360
and that will be why
18874
11:33:52,880 --> 11:33:55,360
okay then we need to set the geometry
18875
11:33:55,360 --> 11:33:56,560
window
18876
11:33:56,560 --> 11:33:59,840
dot geometry
18877
11:34:00,960 --> 11:34:04,480
we're going to use an f string
18878
11:34:04,640 --> 11:34:07,680
so let's set the window
18879
11:34:07,680 --> 11:34:12,640
width times
18880
11:34:12,640 --> 11:34:15,840
window height
18881
11:34:17,200 --> 11:34:21,200
and then add plus
18882
11:34:21,756 --> 11:34:24,800
x plus y
18883
11:34:24,800 --> 11:34:27,840
so when we pass in x and y
18884
11:34:27,840 --> 11:34:30,160
these cannot be floats they have to be
18885
11:34:30,160 --> 11:34:31,040
whole integers
18886
11:34:31,040 --> 11:34:35,916
so let's add a cast around x and y
18887
11:34:38,400 --> 11:34:40,480
okay now this should be fairly close to
18888
11:34:40,480 --> 11:34:42,240
the center
18889
11:34:42,240 --> 11:34:44,000
yeah that's not too bad now when we
18890
11:34:44,000 --> 11:34:45,680
begin a new game we should create a
18891
11:34:45,680 --> 11:34:48,480
snake object as well as a food object
18892
11:34:48,480 --> 11:34:51,520
snake equals snake and called the snake
18893
11:34:51,520 --> 11:34:52,640
constructor
18894
11:34:52,640 --> 11:34:56,000
and food equals food
18895
11:34:56,000 --> 11:34:58,640
okay let's fill in our food class first
18896
11:34:58,640 --> 11:35:00,240
because i think that'll be easier than
18897
11:35:00,240 --> 11:35:01,596
our snake class
18898
11:35:01,596 --> 11:35:04,880
so let's head to the food class
18899
11:35:04,880 --> 11:35:08,720
now let's create an init method so def
18900
11:35:08,720 --> 11:35:11,520
init this will construct a food object
18901
11:35:11,520 --> 11:35:12,560
for us
18902
11:35:12,560 --> 11:35:14,720
and we need to place our food object
18903
11:35:14,720 --> 11:35:16,160
randomly
18904
11:35:16,160 --> 11:35:18,960
so for the x coordinate let's say x
18905
11:35:18,960 --> 11:35:19,520
equals
18906
11:35:19,520 --> 11:35:23,520
random dot rand int and we need a range
18907
11:35:23,520 --> 11:35:27,040
the range is going to be zero comma now
18908
11:35:27,040 --> 11:35:29,360
with our game board i view it like a
18909
11:35:29,360 --> 11:35:30,080
chess board
18910
11:35:30,080 --> 11:35:33,116
there's a given amount of spaces so
18911
11:35:33,116 --> 11:35:37,840
700 divided by our space size
18912
11:35:37,840 --> 11:35:41,596
is 14 possible spots on the x-axis
18913
11:35:41,596 --> 11:35:45,040
and then 14 possible spots on the y-axis
18914
11:35:45,040 --> 11:35:47,276
so i need to pick one of these spots
18915
11:35:47,276 --> 11:35:49,360
randomly
18916
11:35:49,360 --> 11:35:53,596
so let's say game
18917
11:35:53,596 --> 11:35:56,640
width divided by our space
18918
11:35:56,640 --> 11:35:58,480
size so we'll get a random number
18919
11:35:58,480 --> 11:36:00,640
between 0 and 14
18920
11:36:00,640 --> 11:36:02,880
however this should be exclusive so
18921
11:36:02,880 --> 11:36:05,436
let's say -1
18922
11:36:05,436 --> 11:36:07,040
and for good measure i'm just going to
18923
11:36:07,040 --> 11:36:09,200
add a set of parentheses around this
18924
11:36:09,200 --> 11:36:11,680
okay and then we'll convert this to
18925
11:36:11,680 --> 11:36:12,960
pixels
18926
11:36:12,960 --> 11:36:14,960
so let's multiply all of this by our
18927
11:36:14,960 --> 11:36:16,480
space
18928
11:36:16,480 --> 11:36:19,360
size the size of each item in the game
18929
11:36:19,360 --> 11:36:20,720
and we'll do the same thing for
18930
11:36:20,720 --> 11:36:25,520
y but change game width to height
18931
11:36:30,240 --> 11:36:33,840
okay we should be good
18932
11:36:35,360 --> 11:36:37,200
all right now let's set the coordinates
18933
11:36:37,200 --> 11:36:41,200
let's say self dot coordinates
18934
11:36:41,756 --> 11:36:45,040
equals a list of x and y
18935
11:36:45,040 --> 11:36:47,756
and that's it but make sure you spell
18936
11:36:47,756 --> 11:36:50,160
coordinates right
18937
11:36:50,160 --> 11:36:52,640
now we need to draw our food object on
18938
11:36:52,640 --> 11:36:54,720
our canvas
18939
11:36:54,720 --> 11:36:58,480
canvas dot create
18940
11:36:58,480 --> 11:37:00,640
oval you can pick a square two if you
18941
11:37:00,640 --> 11:37:01,680
like
18942
11:37:01,680 --> 11:37:03,916
we need a starting corner that will be
18943
11:37:03,916 --> 11:37:05,116
where x
18944
11:37:05,116 --> 11:37:08,640
and y is and an ending coordinator
18945
11:37:08,640 --> 11:37:11,916
so that will be x and y plus
18946
11:37:11,916 --> 11:37:14,080
our space size the size of an object in
18947
11:37:14,080 --> 11:37:15,116
that game
18948
11:37:15,116 --> 11:37:19,116
so x plus space
18949
11:37:19,116 --> 11:37:22,560
size y plus
18950
11:37:22,560 --> 11:37:26,000
space size
18951
11:37:26,000 --> 11:37:29,360
and you can set a fill color too
18952
11:37:29,360 --> 11:37:32,400
fill will be our food color that we
18953
11:37:32,400 --> 11:37:34,800
declared
18954
11:37:34,880 --> 11:37:37,756
and i recommend adding a tag as well
18955
11:37:37,756 --> 11:37:39,520
that'll make it easy to delete this
18956
11:37:39,520 --> 11:37:41,200
object
18957
11:37:41,200 --> 11:37:44,880
so tag will equal the string food
18958
11:37:44,880 --> 11:37:47,276
now let's just test this so we should
18959
11:37:47,276 --> 11:37:48,800
have a random circle
18960
11:37:48,800 --> 11:37:50,160
or a square if you picked a square
18961
11:37:50,160 --> 11:37:52,400
appear someplace at some spot
18962
11:37:52,400 --> 11:37:55,680
on my game board cool seems like it's
18963
11:37:55,680 --> 11:37:56,800
working
18964
11:37:56,800 --> 11:37:59,680
let's work on the snake class next our
18965
11:37:59,680 --> 11:38:03,520
snake class we'll need a constructor
18966
11:38:06,720 --> 11:38:10,400
and we will set a body size
18967
11:38:10,400 --> 11:38:15,276
equal to our body parts
18968
11:38:16,320 --> 11:38:24,000
we'll need a list of coordinates
18969
11:38:24,000 --> 11:38:27,596
fill this in later and a list
18970
11:38:27,596 --> 11:38:30,800
of square graphics
18971
11:38:31,040 --> 11:38:34,160
so that will be a list okay
18972
11:38:34,160 --> 11:38:37,040
so we need to create a list of
18973
11:38:37,040 --> 11:38:38,000
coordinates
18974
11:38:38,000 --> 11:38:40,640
we can use a for loop for that so let's
18975
11:38:40,640 --> 11:38:41,596
say for
18976
11:38:41,596 --> 11:38:45,040
i in range zero
18977
11:38:45,040 --> 11:38:49,276
through body parts
18978
11:38:50,320 --> 11:38:54,960
we will take self dot coordinates
18979
11:38:54,960 --> 11:38:59,116
and append a new list
18980
11:38:59,360 --> 11:39:01,596
and the coordinates for each body part
18981
11:39:01,596 --> 11:39:03,200
at the start of the game will be
18982
11:39:03,200 --> 11:39:06,160
0 0 so that our snake will appear in the
18983
11:39:06,160 --> 11:39:09,040
top left corner
18984
11:39:09,276 --> 11:39:11,520
now we'll need to create some squares
18985
11:39:11,520 --> 11:39:12,320
okay so
18986
11:39:12,320 --> 11:39:15,360
for x y
18987
11:39:15,360 --> 11:39:18,800
in self dot coordinates
18988
11:39:18,800 --> 11:39:21,116
and remember that we have a list of
18989
11:39:21,116 --> 11:39:22,960
lists so that's why we're using
18990
11:39:22,960 --> 11:39:27,040
x y in self coordinates
18991
11:39:27,040 --> 11:39:31,436
we'll create a square equals
18992
11:39:31,436 --> 11:39:35,276
canvas dot create
18993
11:39:35,276 --> 11:39:37,916
rectangle
18994
11:39:38,960 --> 11:39:40,960
so we need a starting corner that will
18995
11:39:40,960 --> 11:39:44,320
be x and y
18996
11:39:45,116 --> 11:39:48,400
then x plus our
18997
11:39:48,400 --> 11:39:51,040
space size the size of each object in
18998
11:39:51,040 --> 11:39:53,436
the game
18999
11:39:53,840 --> 11:39:57,436
let me use the constant though space
19000
11:39:57,436 --> 11:40:00,840
size y plus
19001
11:40:00,840 --> 11:40:06,080
space size
19002
11:40:06,240 --> 11:40:09,680
and let's set a fill color equal to
19003
11:40:09,680 --> 11:40:13,360
our snake color
19004
11:40:14,800 --> 11:40:18,320
all right and let's set a tag
19005
11:40:18,320 --> 11:40:22,080
for convenience tag equals
19006
11:40:22,080 --> 11:40:25,916
a string of snake okay so we have a
19007
11:40:25,916 --> 11:40:28,080
list of squares and we can append each
19008
11:40:28,080 --> 11:40:31,360
square into our list
19009
11:40:31,360 --> 11:40:35,360
self dot squares
19010
11:40:36,240 --> 11:40:39,916
dot append and pass in whatever square
19011
11:40:39,916 --> 11:40:42,480
that you create
19012
11:40:42,640 --> 11:40:46,960
so we have a snake that has a body size
19013
11:40:46,960 --> 11:40:49,360
a list of coordinates and a list of
19014
11:40:49,360 --> 11:40:50,640
square graphics
19015
11:40:50,640 --> 11:40:52,960
now after testing this we should have
19016
11:40:52,960 --> 11:40:54,960
the head of our snake in the top left
19017
11:40:54,960 --> 11:40:55,520
corner
19018
11:40:55,520 --> 11:40:57,596
and now we just need the snake to move
19019
11:40:57,596 --> 11:40:59,916
in a given direction every turn
19020
11:40:59,916 --> 11:41:02,000
so the initial direction is down but
19021
11:41:02,000 --> 11:41:03,756
we'll be able to change that later
19022
11:41:03,756 --> 11:41:06,800
so let's head to the next turn function
19023
11:41:06,800 --> 11:41:08,880
and there's actually two things we'll
19024
11:41:08,880 --> 11:41:10,800
need as parameters that i forgot to fill
19025
11:41:10,800 --> 11:41:11,360
in
19026
11:41:11,360 --> 11:41:14,400
snake as well as food and we will call
19027
11:41:14,400 --> 11:41:17,040
this function when we begin our game
19028
11:41:17,040 --> 11:41:19,756
so let's unpack the head of the snake so
19029
11:41:19,756 --> 11:41:20,240
that's
19030
11:41:20,240 --> 11:41:23,596
x comma y equals
19031
11:41:23,596 --> 11:41:26,880
snake dot coordinates
19032
11:41:26,880 --> 11:41:30,560
at index of zero so that's the head of
19033
11:41:30,560 --> 11:41:31,680
the snake
19034
11:41:31,680 --> 11:41:34,080
the coordinates will be stored in x and
19035
11:41:34,080 --> 11:41:35,436
y
19036
11:41:35,436 --> 11:41:39,116
so let's check to see if our direction
19037
11:41:39,116 --> 11:41:42,560
our initial direction equals
19038
11:41:42,560 --> 11:41:46,080
up and we'll need some else if
19039
11:41:46,080 --> 11:41:46,800
statements
19040
11:41:46,800 --> 11:41:50,240
we'll fill this in just a moment
19041
11:41:52,240 --> 11:41:55,360
else if our direction equals
19042
11:41:55,360 --> 11:41:57,680
down
19043
11:42:00,640 --> 11:42:03,520
then left
19044
11:42:06,560 --> 11:42:09,840
and then right
19045
11:42:11,756 --> 11:42:14,960
okay if our direction is up
19046
11:42:14,960 --> 11:42:17,436
then let's take our y coordinate for the
19047
11:42:17,436 --> 11:42:18,960
head of our snake
19048
11:42:18,960 --> 11:42:22,160
minus equals our space
19049
11:42:22,160 --> 11:42:26,480
size so that we move one space up
19050
11:42:26,560 --> 11:42:29,756
and then down is plus equals
19051
11:42:29,756 --> 11:42:33,040
space size left
19052
11:42:33,040 --> 11:42:36,240
is x minus equals
19053
11:42:36,240 --> 11:42:39,436
space size and lastly
19054
11:42:39,436 --> 11:42:44,640
right is x plus equals space size
19055
11:42:45,276 --> 11:42:47,596
okay then we need to call the next turn
19056
11:42:47,596 --> 11:42:48,640
function again
19057
11:42:48,640 --> 11:42:52,560
for the next turn so we can use window
19058
11:42:52,560 --> 11:42:56,400
dot after we need the time
19059
11:42:56,400 --> 11:42:59,596
so let's say our game speed
19060
11:42:59,596 --> 11:43:01,680
we're going to call the next turn
19061
11:43:01,680 --> 11:43:03,840
function and we need to pass in our
19062
11:43:03,840 --> 11:43:07,436
arguments of snake and food
19063
11:43:08,400 --> 11:43:12,080
snake food
19064
11:43:12,080 --> 11:43:13,756
oh and make sure you're not actually
19065
11:43:13,756 --> 11:43:15,840
calling the next turn function inside of
19066
11:43:15,840 --> 11:43:17,596
the after method you just have to write
19067
11:43:17,596 --> 11:43:19,520
the function name of next turn without
19068
11:43:19,520 --> 11:43:21,040
the parentheses
19069
11:43:21,040 --> 11:43:23,360
okay so let's update the coordinates for
19070
11:43:23,360 --> 11:43:24,480
the height for the snake
19071
11:43:24,480 --> 11:43:26,720
and write that before we move on to the
19072
11:43:26,720 --> 11:43:28,240
next turn
19073
11:43:28,240 --> 11:43:33,840
so snake dot coordinates
19074
11:43:34,640 --> 11:43:36,480
and we will insert a new set of
19075
11:43:36,480 --> 11:43:37,840
coordinates
19076
11:43:37,840 --> 11:43:41,276
after updating one of them
19077
11:43:41,276 --> 11:43:44,480
so zero will be the index the head of
19078
11:43:44,480 --> 11:43:45,520
the snake
19079
11:43:45,520 --> 11:43:48,640
and we will insert x and y coordinates
19080
11:43:48,640 --> 11:43:48,960
at
19081
11:43:48,960 --> 11:43:52,560
this new location now we're going to
19082
11:43:52,560 --> 11:43:54,720
create a new graphic for the head of the
19083
11:43:54,720 --> 11:43:55,436
snake
19084
11:43:55,436 --> 11:43:59,520
square equals canvas dot create
19085
11:43:59,520 --> 11:44:02,880
rectangle pass in x
19086
11:44:02,880 --> 11:44:05,520
and y for the starting corner of our
19087
11:44:05,520 --> 11:44:06,560
rectangle and
19088
11:44:06,560 --> 11:44:09,596
the ending corner will be x plus
19089
11:44:09,596 --> 11:44:14,400
our space size y
19090
11:44:14,400 --> 11:44:17,520
plus our space size
19091
11:44:17,520 --> 11:44:22,000
then i will add a fill color of
19092
11:44:22,000 --> 11:44:26,640
snake color and then we need to update
19093
11:44:26,640 --> 11:44:30,000
our snakes list of squares
19094
11:44:30,000 --> 11:44:35,040
so that will be snake dot squares
19095
11:44:35,840 --> 11:44:39,040
and insert at
19096
11:44:39,040 --> 11:44:44,160
index 0 a new square that we create
19097
11:44:44,160 --> 11:44:46,640
okay let's test this one last thing that
19098
11:44:46,640 --> 11:44:48,480
we'll need to get this program to run
19099
11:44:48,480 --> 11:44:50,560
is that after you create your snake and
19100
11:44:50,560 --> 11:44:52,320
food object we should call it the
19101
11:44:52,320 --> 11:44:55,520
next turn function and pass in our snake
19102
11:44:55,520 --> 11:44:57,916
and food object so we should be able to
19103
11:44:57,916 --> 11:44:59,520
test this
19104
11:44:59,520 --> 11:45:01,520
so our snake is going to move but we
19105
11:45:01,520 --> 11:45:02,720
need to delete the
19106
11:45:02,720 --> 11:45:05,756
last body part in our snake within the
19107
11:45:05,756 --> 11:45:07,116
next turn function
19108
11:45:07,116 --> 11:45:10,400
but before we update to the next turn
19109
11:45:10,400 --> 11:45:12,640
let's delete the last body part of our
19110
11:45:12,640 --> 11:45:14,720
snake so delete
19111
11:45:14,720 --> 11:45:18,880
snake dot coordinates
19112
11:45:18,880 --> 11:45:21,916
at negative index of one that is the
19113
11:45:21,916 --> 11:45:24,720
last set of coordinates
19114
11:45:24,720 --> 11:45:27,680
we will update our canvas canvas dot
19115
11:45:27,680 --> 11:45:30,080
delete
19116
11:45:31,520 --> 11:45:36,160
snake dot squares
19117
11:45:36,240 --> 11:45:39,840
an index of negative one
19118
11:45:40,560 --> 11:45:45,520
and lastly delete snake
19119
11:45:46,080 --> 11:45:49,040
the list of squares
19120
11:45:49,116 --> 11:45:52,800
at index of negative one
19121
11:45:52,800 --> 11:45:54,800
so it should appear that our snake is
19122
11:45:54,800 --> 11:45:56,880
moving cool
19123
11:45:56,880 --> 11:45:59,680
we need some controls for our snake so
19124
11:45:59,680 --> 11:46:01,756
at the bottom of our program
19125
11:46:01,756 --> 11:46:06,880
let's bind some keys let's do that here
19126
11:46:06,880 --> 11:46:10,880
so window dot bind
19127
11:46:10,880 --> 11:46:15,520
let's find the left arrow key
19128
11:46:16,080 --> 11:46:20,080
and we will use a lambda
19129
11:46:20,080 --> 11:46:23,276
the argument is event and we will call
19130
11:46:23,276 --> 11:46:23,680
the
19131
11:46:23,680 --> 11:46:27,916
change direction function and pass in
19132
11:46:27,916 --> 11:46:30,880
the word left
19133
11:46:31,520 --> 11:46:33,200
okay then we have to do the same thing
19134
11:46:33,200 --> 11:46:36,400
for the other directions
19135
11:46:40,560 --> 11:46:43,680
so we have right
19136
11:46:44,000 --> 11:46:48,720
pass in right
19137
11:46:48,720 --> 11:46:52,400
up pass in up
19138
11:46:53,200 --> 11:46:57,756
down pass in down
19139
11:46:59,680 --> 11:47:01,756
let's head to the change direction
19140
11:47:01,756 --> 11:47:03,360
function
19141
11:47:03,360 --> 11:47:07,200
we need to access our direction global
19142
11:47:07,200 --> 11:47:11,276
direction this is the old direction
19143
11:47:12,320 --> 11:47:16,560
if our new direction that is passed in
19144
11:47:16,560 --> 11:47:20,880
is equal to left
19145
11:47:22,320 --> 11:47:26,160
and if our old direction this direction
19146
11:47:26,160 --> 11:47:28,800
does not equal right because we do not
19147
11:47:28,800 --> 11:47:31,596
want to go backwards and do a 180 degree
19148
11:47:31,596 --> 11:47:33,840
turn
19149
11:47:34,240 --> 11:47:37,360
then we will set our direction
19150
11:47:37,360 --> 11:47:40,400
equal to our new
19151
11:47:40,400 --> 11:47:42,880
direction and we just need to repeat
19152
11:47:42,880 --> 11:47:44,320
this for the other
19153
11:47:44,320 --> 11:47:48,000
directions for new direction
19154
11:47:48,640 --> 11:47:52,560
and i'm going to change this to else if
19155
11:47:52,560 --> 11:47:56,640
else if new direction is right
19156
11:47:56,640 --> 11:47:59,756
and our direction is not already
19157
11:47:59,756 --> 11:48:02,080
left
19158
11:48:05,276 --> 11:48:08,080
then we have up
19159
11:48:09,040 --> 11:48:13,840
if our direction is not down
19160
11:48:16,480 --> 11:48:20,160
and down if our
19161
11:48:20,160 --> 11:48:23,916
direction is not up okay so we should be
19162
11:48:23,916 --> 11:48:24,880
able to change
19163
11:48:24,880 --> 11:48:28,240
the direction of our snake
19164
11:48:28,320 --> 11:48:31,596
sweet okay we need to eat that pesky
19165
11:48:31,596 --> 11:48:34,480
apple next so let's work on that
19166
11:48:34,480 --> 11:48:35,840
there's nothing else that we need to
19167
11:48:35,840 --> 11:48:37,680
change within the change direction
19168
11:48:37,680 --> 11:48:39,680
function so i'm going to minimize
19169
11:48:39,680 --> 11:48:42,800
this function and head to the
19170
11:48:42,800 --> 11:48:46,160
next turn function
19171
11:48:47,520 --> 11:48:50,640
we'll place an if statement here if
19172
11:48:50,640 --> 11:48:53,436
x remember that we unpacked the
19173
11:48:53,436 --> 11:48:54,240
coordinates
19174
11:48:54,240 --> 11:48:57,520
for the head of the snake if x
19175
11:48:57,520 --> 11:48:59,276
the x coordinate for the head of the
19176
11:48:59,276 --> 11:49:02,960
snake is equal to
19177
11:49:02,960 --> 11:49:08,400
our food objects coordinates
19178
11:49:08,400 --> 11:49:12,240
at index of zero that's the x-coordinate
19179
11:49:12,240 --> 11:49:16,080
for our food object and
19180
11:49:16,080 --> 11:49:19,200
y is equal to
19181
11:49:19,200 --> 11:49:23,756
food coordinates at index of one
19182
11:49:23,756 --> 11:49:27,200
that means they're overlapping
19183
11:49:27,200 --> 11:49:31,436
let's take our score
19184
11:49:31,680 --> 11:49:36,640
and increment it by one
19185
11:49:36,640 --> 11:49:42,000
and change our label label.config
19186
11:49:44,320 --> 11:49:48,800
the text will equal
19187
11:49:48,840 --> 11:49:51,840
score
19188
11:49:52,160 --> 11:49:54,160
and then i'm going to use the format
19189
11:49:54,160 --> 11:49:55,840
method
19190
11:49:55,840 --> 11:50:00,960
and pass in my new score
19191
11:50:01,116 --> 11:50:04,640
let's delete our food object
19192
11:50:04,640 --> 11:50:07,116
and we gave our food object a tag so we
19193
11:50:07,116 --> 11:50:09,200
can just use the name of the tag
19194
11:50:09,200 --> 11:50:11,916
to delete it and create a new food
19195
11:50:11,916 --> 11:50:14,320
object
19196
11:50:16,720 --> 11:50:18,880
then i'm going to write this part of our
19197
11:50:18,880 --> 11:50:22,480
program within an else statement
19198
11:50:23,756 --> 11:50:26,640
we will only delete the last body part
19199
11:50:26,640 --> 11:50:28,000
of our snake
19200
11:50:28,000 --> 11:50:32,080
if we did not eat a food object
19201
11:50:32,080 --> 11:50:35,276
okay let's test it again
19202
11:50:36,000 --> 11:50:38,800
so i have three body parts now i have
19203
11:50:38,800 --> 11:50:39,680
four
19204
11:50:39,680 --> 11:50:42,000
five
19205
11:50:43,436 --> 11:50:47,360
six seven eight nine
19206
11:50:47,360 --> 11:50:49,040
ten you can see that the score is going
19207
11:50:49,040 --> 11:50:51,276
up too
19208
11:50:51,276 --> 11:50:53,916
okay let's work on collisions next
19209
11:50:53,916 --> 11:50:55,436
because i should not be able to go off
19210
11:50:55,436 --> 11:50:56,960
screen like this
19211
11:50:56,960 --> 11:50:58,240
there's one thing that we're going to
19212
11:50:58,240 --> 11:51:00,640
change so let's add an
19213
11:51:00,640 --> 11:51:04,560
if statement that will check collisions
19214
11:51:04,560 --> 11:51:07,040
this will return true or false if we
19215
11:51:07,040 --> 11:51:08,560
detect a collision
19216
11:51:08,560 --> 11:51:12,400
and pass in our snake object
19217
11:51:13,360 --> 11:51:16,080
if there is a collision we'll call the
19218
11:51:16,080 --> 11:51:19,040
game over function
19219
11:51:20,160 --> 11:51:25,200
else we will update to the next turn
19220
11:51:25,520 --> 11:51:28,720
okay let's fill in the check collisions
19221
11:51:28,720 --> 11:51:29,200
function
19222
11:51:29,200 --> 11:51:31,360
and we no longer need our function of
19223
11:51:31,360 --> 11:51:32,800
next turn
19224
11:51:32,800 --> 11:51:34,160
actually we don't need these classes
19225
11:51:34,160 --> 11:51:36,400
either so i'll minimize them
19226
11:51:36,400 --> 11:51:39,040
okay check collisions so it looks like
19227
11:51:39,040 --> 11:51:41,276
we will need a parameter of snake
19228
11:51:41,276 --> 11:51:44,560
so be sure to set that
19229
11:51:44,560 --> 11:51:47,916
let's unpack the head of the snake x
19230
11:51:47,916 --> 11:51:52,160
y equals snake dot coordinates
19231
11:51:52,160 --> 11:51:57,360
at index of zero
19232
11:51:57,360 --> 11:51:59,756
and let's check to see if we cross the
19233
11:51:59,756 --> 11:52:00,400
left or
19234
11:52:00,400 --> 11:52:03,756
right border of the game if x
19235
11:52:03,756 --> 11:52:07,596
is less than zero or
19236
11:52:07,596 --> 11:52:11,276
x is greater than or equal to
19237
11:52:11,276 --> 11:52:15,276
our game width
19238
11:52:15,840 --> 11:52:19,360
then return true i think for testing
19239
11:52:19,360 --> 11:52:21,520
purposes i'm going to print something to
19240
11:52:21,520 --> 11:52:24,480
the console window
19241
11:52:24,480 --> 11:52:29,276
game over let's test it
19242
11:52:29,276 --> 11:52:30,880
okay i'm going to go over to the right
19243
11:52:30,880 --> 11:52:34,000
border game over and our game stopped
19244
11:52:34,000 --> 11:52:37,040
let's go over the left game over
19245
11:52:37,040 --> 11:52:39,840
cool so we know that it's working okay
19246
11:52:39,840 --> 11:52:43,360
let's do the same thing for y
19247
11:52:43,360 --> 11:52:46,720
we can use else if here
19248
11:52:47,200 --> 11:52:50,400
if y is less than zero or
19249
11:52:50,400 --> 11:52:54,640
y is greater than or equal to our game
19250
11:52:54,640 --> 11:52:59,040
height let's print game over
19251
11:52:59,040 --> 11:53:01,756
and return true and we should probably
19252
11:53:01,756 --> 11:53:03,916
test it
19253
11:53:03,916 --> 11:53:07,596
okay i'm going up game over and let's go
19254
11:53:07,596 --> 11:53:08,720
down
19255
11:53:08,720 --> 11:53:11,276
game over
19256
11:53:12,480 --> 11:53:15,520
so what if our snake touches its tail or
19257
11:53:15,520 --> 11:53:17,680
another body part
19258
11:53:17,680 --> 11:53:20,800
so let's say four i actually i
19259
11:53:20,800 --> 11:53:22,640
isn't too descriptive let's say for
19260
11:53:22,640 --> 11:53:25,756
every body part
19261
11:53:25,840 --> 11:53:34,000
in snake dot coordinates
19262
11:53:34,000 --> 11:53:37,200
we're going to set this to everything
19263
11:53:37,200 --> 11:53:40,240
after the head of the snake
19264
11:53:40,240 --> 11:53:41,756
we're going to check to see if any of
19265
11:53:41,756 --> 11:53:45,596
the coordinates are matching
19266
11:53:45,680 --> 11:53:48,800
if x is equal to
19267
11:53:48,800 --> 11:53:52,240
body part at index
19268
11:53:52,240 --> 11:53:55,756
0 and
19269
11:53:55,756 --> 11:53:59,520
y is equal to body
19270
11:53:59,520 --> 11:54:03,596
part at index 1
19271
11:54:03,596 --> 11:54:07,200
then return true
19272
11:54:07,200 --> 11:54:10,560
and i'll print game over to test it
19273
11:54:10,560 --> 11:54:15,040
print game over
19274
11:54:15,756 --> 11:54:18,480
otherwise we can return false there are
19275
11:54:18,480 --> 11:54:20,880
no collisions
19276
11:54:20,880 --> 11:54:23,040
i think i'm going to change the size of
19277
11:54:23,040 --> 11:54:24,720
the snake
19278
11:54:24,720 --> 11:54:29,116
to 10. okay let's run into each other
19279
11:54:29,116 --> 11:54:32,720
cool game over and the last thing that
19280
11:54:32,720 --> 11:54:33,520
we need to do
19281
11:54:33,520 --> 11:54:36,000
is to fill in the game over function
19282
11:54:36,000 --> 11:54:37,756
because it looks like everything else is
19283
11:54:37,756 --> 11:54:38,560
fine
19284
11:54:38,560 --> 11:54:42,000
so head to the game over function take
19285
11:54:42,000 --> 11:54:47,116
your canvas and delete all
19286
11:54:49,436 --> 11:54:52,080
and we'll create some game over text
19287
11:54:52,080 --> 11:54:53,680
canvas
19288
11:54:53,680 --> 11:54:57,520
create text
19289
11:54:57,520 --> 11:54:59,916
i would like this in the center of my
19290
11:54:59,916 --> 11:55:00,720
canvas
19291
11:55:00,720 --> 11:55:04,320
i'll take canvas w info
19292
11:55:04,320 --> 11:55:08,560
width divided by two comma
19293
11:55:08,560 --> 11:55:12,240
canvas w info height
19294
11:55:12,240 --> 11:55:14,960
divided by two i'm going to put some of
19295
11:55:14,960 --> 11:55:16,240
this on the next line
19296
11:55:16,240 --> 11:55:19,680
for readability i'll set a font
19297
11:55:19,680 --> 11:55:26,400
pick whatever font that you want
19298
11:55:26,400 --> 11:55:29,360
we'll need some text
19299
11:55:30,400 --> 11:55:35,756
text equals game over
19300
11:55:36,240 --> 11:55:40,320
pick a color red's decent
19301
11:55:40,320 --> 11:55:44,720
i'll add a tag too for convenience game
19302
11:55:44,720 --> 11:55:47,840
over and that should be it for the game
19303
11:55:47,840 --> 11:55:49,200
over function
19304
11:55:49,200 --> 11:55:51,596
i'm going to change the body parts of
19305
11:55:51,596 --> 11:55:53,680
the snake back to what it was originally
19306
11:55:53,680 --> 11:55:55,360
and we should probably test that game
19307
11:55:55,360 --> 11:55:57,916
over screen
19308
11:55:58,320 --> 11:56:01,116
all right it appeared okay so let's
19309
11:56:01,116 --> 11:56:03,116
change some of these settings around
19310
11:56:03,116 --> 11:56:05,596
you can create a larger game board let's
19311
11:56:05,596 --> 11:56:06,880
say
19312
11:56:06,880 --> 11:56:11,040
1000 by 700
19313
11:56:11,840 --> 11:56:14,880
that still works you can slow down the
19314
11:56:14,880 --> 11:56:16,720
speed or speed it up
19315
11:56:16,720 --> 11:56:19,916
so 100 will be about half as fast
19316
11:56:19,916 --> 11:56:22,320
it's going pretty slow now but what if
19317
11:56:22,320 --> 11:56:23,276
we set it to
19318
11:56:23,276 --> 11:56:25,680
20.
19319
11:56:28,400 --> 11:56:31,916
you can change the space size
19320
11:56:32,720 --> 11:56:35,680
so everything is a lot smaller now
19321
11:56:35,680 --> 11:56:36,720
including the food
19322
11:56:36,720 --> 11:56:39,040
object
19323
11:56:40,240 --> 11:56:42,720
you can change the body parts what about
19324
11:56:42,720 --> 11:56:45,596
20 to begin with
19325
11:56:46,160 --> 11:56:50,480
that's fairly excessive
19326
11:56:50,480 --> 11:56:53,276
i'll change that back to three you can
19327
11:56:53,276 --> 11:56:54,640
change the snake color
19328
11:56:54,640 --> 11:56:57,680
let's say we would like a blue snake
19329
11:56:57,680 --> 11:57:01,276
so that is for a hex color
19330
11:57:01,276 --> 11:57:05,840
four zeros and then two f's
19331
11:57:05,840 --> 11:57:07,840
you can change the food color so let's
19332
11:57:07,840 --> 11:57:09,756
say we would like a yellow food
19333
11:57:09,756 --> 11:57:11,756
object so that would be four f's and
19334
11:57:11,756 --> 11:57:14,000
then two zeros
19335
11:57:14,000 --> 11:57:16,880
kind of resembles the python logo that
19336
11:57:16,880 --> 11:57:19,840
color scheme
19337
11:57:20,640 --> 11:57:23,040
you can change the background color too
19338
11:57:23,040 --> 11:57:23,916
so let's say
19339
11:57:23,916 --> 11:57:28,160
all white that would be six apps
19340
11:57:28,960 --> 11:57:32,160
i do not like that go back delete delete
19341
11:57:32,160 --> 11:57:33,520
delete alright well
19342
11:57:33,520 --> 11:57:35,276
that should be everything let's run this
19343
11:57:35,276 --> 11:57:46,710
game one last time
19344
11:57:46,710 --> 11:58:12,039
[Music]
19345
11:58:14,520 --> 11:58:43,910
[Music]
19346
11:58:44,000 --> 11:58:46,800
well everybody that is a very basic game
19347
11:58:46,800 --> 11:58:48,960
of snake for beginners i will post all
19348
11:58:48,960 --> 11:58:50,720
of this code to the comment section down
19349
11:58:50,720 --> 11:58:53,276
below but yeah that's a basic game of
19350
11:58:53,276 --> 11:58:53,840
snake
19351
11:58:53,840 --> 11:58:57,040
using python hey you
19352
11:58:57,040 --> 11:58:58,800
yeah i'm talking to you if you learned
19353
11:58:58,800 --> 11:59:00,480
something new then help me
19354
11:59:00,480 --> 11:59:03,360
help you in three easy steps by smashing
19355
11:59:03,360 --> 11:59:04,320
that like button
19356
11:59:04,320 --> 11:59:06,560
drop a comment down below and subscribe
19357
11:59:06,560 --> 11:59:11,840
if you'd like to become a fellow bro
19358
11:59:11,950 --> 11:59:29,859
[Music]
19359
11:59:59,756 --> 12:00:01,840
you
1273378
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.