Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:31,920 --> 00:00:34,160
hi everyone welcome to the simplyland
2
00:00:34,160 --> 00:00:36,559
youtube channel this session will be
3
00:00:36,559 --> 00:00:38,960
about python for beginners before we
4
00:00:38,960 --> 00:00:41,280
begin let me inform you about the daily
5
00:00:41,280 --> 00:00:43,600
updates on multiple technologies on our
6
00:00:43,600 --> 00:00:44,399
channel
7
00:00:44,399 --> 00:00:46,559
if you love hunt for technological stuff
8
00:00:46,559 --> 00:00:48,239
consider getting subscribing to our
9
00:00:48,239 --> 00:00:50,399
youtube channel and don't forget to hit
10
00:00:50,399 --> 00:00:53,039
the bell icon to never miss an update
11
00:00:53,039 --> 00:00:54,960
we from simplyland bring you this
12
00:00:54,960 --> 00:00:57,280
beautifully designed organized and
13
00:00:57,280 --> 00:00:59,280
upgraded python full course for
14
00:00:59,280 --> 00:01:01,440
beginners for 2022.
15
00:01:01,440 --> 00:01:03,840
now let's get started with today's
16
00:01:03,840 --> 00:01:06,720
agenda first we will start with the
17
00:01:06,720 --> 00:01:09,119
briefing on python programming language
18
00:01:09,119 --> 00:01:12,000
then we will learn to install python on
19
00:01:12,000 --> 00:01:14,720
windows and write our first program in
20
00:01:14,720 --> 00:01:17,280
python after that we will start with
21
00:01:17,280 --> 00:01:20,080
basic concepts that is variables and
22
00:01:20,080 --> 00:01:22,880
data types in python numbers in python
23
00:01:22,880 --> 00:01:25,520
python object types and type conversion
24
00:01:25,520 --> 00:01:27,520
strings in python and operators in
25
00:01:27,520 --> 00:01:28,560
python
26
00:01:28,560 --> 00:01:30,960
now understanding the basic concepts we
27
00:01:30,960 --> 00:01:33,200
will educate ourselves with if
28
00:01:33,200 --> 00:01:35,439
statements in python followed by the
29
00:01:35,439 --> 00:01:37,759
python loops python loop coding
30
00:01:37,759 --> 00:01:40,560
techniques control statements in python
31
00:01:40,560 --> 00:01:44,159
and conditional statements in python
32
00:01:44,159 --> 00:01:46,320
with that we will develop a good hold on
33
00:01:46,320 --> 00:01:49,119
the basic concepts now moving on
34
00:01:49,119 --> 00:01:51,920
we will start with python lists tuples
35
00:01:51,920 --> 00:01:53,200
dictionaries
36
00:01:53,200 --> 00:01:55,600
and list comprehension in python
37
00:01:55,600 --> 00:01:58,079
followed by python functions and lambda
38
00:01:58,079 --> 00:01:59,119
function
39
00:01:59,119 --> 00:02:02,479
further we will have hands-on map filter
40
00:02:02,479 --> 00:02:05,360
and reduce function in python moving on
41
00:02:05,360 --> 00:02:07,439
we will get ourself educated with
42
00:02:07,439 --> 00:02:10,479
classes in python inheritance in python
43
00:02:10,479 --> 00:02:13,200
iterators and generators in python
44
00:02:13,200 --> 00:02:15,599
string formatting in python scopes and
45
00:02:15,599 --> 00:02:18,160
arguments in python oops concepts in
46
00:02:18,160 --> 00:02:20,400
python and operator overloading in
47
00:02:20,400 --> 00:02:21,520
python
48
00:02:21,520 --> 00:02:22,400
finally
49
00:02:22,400 --> 00:02:24,400
we will be encountering some language
50
00:02:24,400 --> 00:02:25,840
building concepts
51
00:02:25,840 --> 00:02:28,400
that is modules in python
52
00:02:28,400 --> 00:02:30,720
packages in python file handling in
53
00:02:30,720 --> 00:02:33,680
python exception handling in python
54
00:02:33,680 --> 00:02:36,400
and our concluding concept that would be
55
00:02:36,400 --> 00:02:39,440
sorting in python what is python
56
00:02:39,440 --> 00:02:42,239
python is a high-level object oriented
57
00:02:42,239 --> 00:02:44,800
programming language developed by guido
58
00:02:44,800 --> 00:02:47,519
van rossum in 1989 and was first
59
00:02:47,519 --> 00:02:50,000
released in 1991.
60
00:02:50,000 --> 00:02:52,000
python is often called a batteries
61
00:02:52,000 --> 00:02:53,680
included language
62
00:02:53,680 --> 00:02:55,200
due to its comprehensive standard
63
00:02:55,200 --> 00:02:56,560
library
64
00:02:56,560 --> 00:02:59,280
a fun fact about python is that the name
65
00:02:59,280 --> 00:03:00,879
python was actually taken from the
66
00:03:00,879 --> 00:03:03,599
popular bbc comedy show of that time
67
00:03:03,599 --> 00:03:06,480
monty python's flying circus
68
00:03:06,480 --> 00:03:07,280
now
69
00:03:07,280 --> 00:03:11,920
let's look at the top features of python
70
00:03:12,319 --> 00:03:14,720
first so python has a simple structure
71
00:03:14,720 --> 00:03:17,440
and a clearly defined syntax this allows
72
00:03:17,440 --> 00:03:18,800
the learners to pick up the language
73
00:03:18,800 --> 00:03:22,560
quickly so it is easy to learn and use
74
00:03:22,560 --> 00:03:24,319
python can run on different operating
75
00:03:24,319 --> 00:03:27,200
systems such as windows linux and mac
76
00:03:27,200 --> 00:03:29,840
making it a portable language it enables
77
00:03:29,840 --> 00:03:31,519
programmers to develop the software for
78
00:03:31,519 --> 00:03:33,840
several competing platforms by writing a
79
00:03:33,840 --> 00:03:36,720
program only once
80
00:03:36,720 --> 00:03:38,959
third python is freely available at the
81
00:03:38,959 --> 00:03:41,760
official website since it is open source
82
00:03:41,760 --> 00:03:43,519
this means that source code is also
83
00:03:43,519 --> 00:03:46,000
available to the public
84
00:03:46,000 --> 00:03:47,840
now python uses an object-oriented
85
00:03:47,840 --> 00:03:49,920
approach that encapsulates code within
86
00:03:49,920 --> 00:03:51,840
objects
87
00:03:51,840 --> 00:03:53,360
python provides a collection of
88
00:03:53,360 --> 00:03:55,040
libraries for various tasks such as
89
00:03:55,040 --> 00:03:56,879
machine learning web development and
90
00:03:56,879 --> 00:03:58,560
data analysis
91
00:03:58,560 --> 00:04:01,680
and finally in python you don't need to
92
00:04:01,680 --> 00:04:03,760
assign the data type of the variable
93
00:04:03,760 --> 00:04:05,040
when you assign some value to the
94
00:04:05,040 --> 00:04:07,360
variable it automatically allocates the
95
00:04:07,360 --> 00:04:10,400
memory to the variable at runtime
96
00:04:10,400 --> 00:04:11,760
now with that
97
00:04:11,760 --> 00:04:13,360
let's move on to the users of python
98
00:04:13,360 --> 00:04:15,760
programming
99
00:04:15,760 --> 00:04:17,519
so python programming language is used
100
00:04:17,519 --> 00:04:19,918
to develop desktop applications and
101
00:04:19,918 --> 00:04:22,479
build web applications too
102
00:04:22,479 --> 00:04:24,000
it is popularly used in the field of
103
00:04:24,000 --> 00:04:25,759
data science machine learning and
104
00:04:25,759 --> 00:04:28,479
artificial intelligence to analyze data
105
00:04:28,479 --> 00:04:29,919
build predictive models and make
106
00:04:29,919 --> 00:04:32,400
business decisions python is also widely
107
00:04:32,400 --> 00:04:34,160
used in game development
108
00:04:34,160 --> 00:04:35,199
now
109
00:04:35,199 --> 00:04:36,880
let's see some of the popular python
110
00:04:36,880 --> 00:04:39,040
frameworks and libraries
111
00:04:39,040 --> 00:04:41,040
python can be used for web development
112
00:04:41,040 --> 00:04:43,600
using frameworks like xango flask
113
00:04:43,600 --> 00:04:45,759
pyramid and cherry pie
114
00:04:45,759 --> 00:04:47,520
now you can build graphical user
115
00:04:47,520 --> 00:04:48,960
interfaces using libraries and
116
00:04:48,960 --> 00:04:50,400
frameworks such as
117
00:04:50,400 --> 00:04:52,800
tkinter or just kinter
118
00:04:52,800 --> 00:04:57,680
you can also use pi gtk by qt or pygs or
119
00:04:57,680 --> 00:05:00,240
python javascript
120
00:05:00,240 --> 00:05:01,919
now python is also used to perform
121
00:05:01,919 --> 00:05:03,759
machine learning tasks using libraries
122
00:05:03,759 --> 00:05:07,120
such as tensorflow pytorch scikit-learn
123
00:05:07,120 --> 00:05:10,000
matplotlib and scipy
124
00:05:10,000 --> 00:05:11,840
you can also perform mathematical
125
00:05:11,840 --> 00:05:15,840
computations using numpy and pandas
126
00:05:15,840 --> 00:05:17,919
now let's look at the best ids that you
127
00:05:17,919 --> 00:05:19,840
can use to write programs in python and
128
00:05:19,840 --> 00:05:21,680
perform specific tasks
129
00:05:21,680 --> 00:05:24,000
so we have jupyter notebook which is
130
00:05:24,000 --> 00:05:25,840
part of the anaconda distribution that
131
00:05:25,840 --> 00:05:28,320
is widely used these days even for our
132
00:05:28,320 --> 00:05:30,160
demo in this video we'll be using
133
00:05:30,160 --> 00:05:32,000
jupyter notebook i'll show you in a
134
00:05:32,000 --> 00:05:34,080
while then we have the visual code
135
00:05:34,080 --> 00:05:36,639
editor from microsoft this is also one
136
00:05:36,639 --> 00:05:38,400
of the preferred
137
00:05:38,400 --> 00:05:41,199
ides by learners and companies
138
00:05:41,199 --> 00:05:43,199
then we also have the popular text
139
00:05:43,199 --> 00:05:46,000
editor called sublime text editor
140
00:05:46,000 --> 00:05:48,720
then we also have pycharm followed by
141
00:05:48,720 --> 00:05:52,479
python idli and spyder as our top ides
142
00:05:52,479 --> 00:05:54,479
now let's look at the top companies that
143
00:05:54,479 --> 00:05:58,639
are using python in our day to day work
144
00:05:58,639 --> 00:06:02,319
so we have google quora facebook even
145
00:06:02,319 --> 00:06:05,280
netflix spotify and instagram now there
146
00:06:05,280 --> 00:06:07,360
are other top product based service
147
00:06:07,360 --> 00:06:09,120
based and startups that also use python
148
00:06:09,120 --> 00:06:10,720
programming
149
00:06:10,720 --> 00:06:11,600
okay
150
00:06:11,600 --> 00:06:14,240
now with this theory let's
151
00:06:14,240 --> 00:06:16,319
move to our jupyter notebook where i'll
152
00:06:16,319 --> 00:06:19,440
show you how to write basic python code
153
00:06:19,440 --> 00:06:21,600
so i have my anaconda distribution of
154
00:06:21,600 --> 00:06:23,919
python already installed and i'm working
155
00:06:23,919 --> 00:06:25,120
on a
156
00:06:25,120 --> 00:06:27,600
windows system so
157
00:06:27,600 --> 00:06:29,360
i'll search here for
158
00:06:29,360 --> 00:06:31,120
jupyter notebook
159
00:06:31,120 --> 00:06:33,039
you can see here i have my jupyter
160
00:06:33,039 --> 00:06:36,160
notebook anaconda 3 installed let me
161
00:06:36,160 --> 00:06:37,520
just open it
162
00:06:37,520 --> 00:06:40,400
now this will open on a web browser
163
00:06:40,400 --> 00:06:43,840
you see here this is my chrome browser
164
00:06:43,840 --> 00:06:44,880
okay
165
00:06:44,880 --> 00:06:46,240
so
166
00:06:46,240 --> 00:06:49,280
here i have some python notebooks
167
00:06:49,280 --> 00:06:51,199
already created
168
00:06:51,199 --> 00:06:52,880
now for our demo let me go ahead and
169
00:06:52,880 --> 00:06:54,400
create a new one
170
00:06:54,400 --> 00:06:57,280
so here i'm going to
171
00:06:57,280 --> 00:06:58,960
click on new
172
00:06:58,960 --> 00:07:02,319
and then i'll select python3
173
00:07:02,319 --> 00:07:03,599
this opens
174
00:07:03,599 --> 00:07:05,680
in a
175
00:07:05,680 --> 00:07:06,720
new tab
176
00:07:06,720 --> 00:07:09,360
and here i can write whatever code i
177
00:07:09,360 --> 00:07:10,720
want
178
00:07:10,720 --> 00:07:12,160
all right
179
00:07:12,160 --> 00:07:13,840
so first
180
00:07:13,840 --> 00:07:14,800
let's
181
00:07:14,800 --> 00:07:17,919
see how you can print a simple
182
00:07:17,919 --> 00:07:21,120
sentence or a text in python
183
00:07:21,120 --> 00:07:23,840
now to print something you can just use
184
00:07:23,840 --> 00:07:26,080
the print function
185
00:07:26,080 --> 00:07:29,120
if i write print
186
00:07:29,360 --> 00:07:33,039
within brackets suppose i want to print
187
00:07:33,039 --> 00:07:35,680
my country that is india
188
00:07:35,680 --> 00:07:38,319
now india is actually
189
00:07:38,319 --> 00:07:40,639
a string so i need to pass in within
190
00:07:40,639 --> 00:07:42,720
double quotes
191
00:07:42,720 --> 00:07:44,560
so this is it if you want to print
192
00:07:44,560 --> 00:07:46,319
something you can just use the print
193
00:07:46,319 --> 00:07:48,160
function and pass within the function
194
00:07:48,160 --> 00:07:49,919
whatever you want to print
195
00:07:49,919 --> 00:07:53,039
and now to run this cell
196
00:07:53,039 --> 00:07:55,520
i just need to
197
00:07:55,520 --> 00:07:58,479
click on this run button or you can hit
198
00:07:58,479 --> 00:08:01,199
shift enter to run this cell
199
00:08:01,199 --> 00:08:03,360
i'll just hit shift enter
200
00:08:03,360 --> 00:08:06,160
you can see it here it has printed
201
00:08:06,160 --> 00:08:08,240
india for me
202
00:08:08,240 --> 00:08:09,759
all right
203
00:08:09,759 --> 00:08:11,120
now let's say
204
00:08:11,120 --> 00:08:12,560
you want to
205
00:08:12,560 --> 00:08:14,080
perform some
206
00:08:14,080 --> 00:08:17,120
simple mathematical calculations that
207
00:08:17,120 --> 00:08:19,120
also you can do it suppose i want to
208
00:08:19,120 --> 00:08:20,479
find the sum of
209
00:08:20,479 --> 00:08:21,520
10
210
00:08:21,520 --> 00:08:22,479
plus
211
00:08:22,479 --> 00:08:24,080
50.
212
00:08:24,080 --> 00:08:28,080
i'll just run this cell you see here
213
00:08:28,080 --> 00:08:31,199
it gives me 60. it's very simple
214
00:08:31,199 --> 00:08:32,958
similarly if you want to find the
215
00:08:32,958 --> 00:08:34,240
product of
216
00:08:34,240 --> 00:08:36,000
let's say three numbers i'll write let's
217
00:08:36,000 --> 00:08:40,479
say 40 multiplied by 2 multiplied by 5
218
00:08:40,479 --> 00:08:41,760
if i run this
219
00:08:41,760 --> 00:08:44,399
you get the result that is 400
220
00:08:44,399 --> 00:08:46,160
which is correct
221
00:08:46,160 --> 00:08:48,399
ok
222
00:08:48,880 --> 00:08:50,800
now to store our
223
00:08:50,800 --> 00:08:52,560
values we can
224
00:08:52,560 --> 00:08:54,560
take the help of a variable
225
00:08:54,560 --> 00:08:56,880
suppose
226
00:08:56,880 --> 00:08:58,560
if i write x
227
00:08:58,560 --> 00:08:59,839
equal to
228
00:08:59,839 --> 00:09:00,959
7
229
00:09:00,959 --> 00:09:04,480
this means that my value 7 is stored in
230
00:09:04,480 --> 00:09:07,360
the variable that is x
231
00:09:07,360 --> 00:09:08,959
you might be aware that in other
232
00:09:08,959 --> 00:09:11,680
programming languages also we have
233
00:09:11,680 --> 00:09:13,519
variables and data types which i am
234
00:09:13,519 --> 00:09:14,720
going to
235
00:09:14,720 --> 00:09:16,240
tell you now
236
00:09:16,240 --> 00:09:18,959
so here i have assigned my value 7 to
237
00:09:18,959 --> 00:09:21,200
the variable x
238
00:09:21,200 --> 00:09:24,000
now let me go ahead and print what is
239
00:09:24,000 --> 00:09:25,519
there in x
240
00:09:25,519 --> 00:09:27,120
if i run this cell
241
00:09:27,120 --> 00:09:28,240
there you go
242
00:09:28,240 --> 00:09:31,440
it gives me 7 which means x holds the
243
00:09:31,440 --> 00:09:33,839
value of 7.
244
00:09:33,839 --> 00:09:35,839
now if you want to check the data type
245
00:09:35,839 --> 00:09:38,160
of x you can use the type function in
246
00:09:38,160 --> 00:09:39,120
python
247
00:09:39,120 --> 00:09:40,720
so if i write
248
00:09:40,720 --> 00:09:43,440
type of x
249
00:09:43,440 --> 00:09:47,040
and if i print it
250
00:09:47,040 --> 00:09:49,360
it will also result
251
00:09:49,360 --> 00:09:52,160
in the type of data that x is holding
252
00:09:52,160 --> 00:09:53,600
you can see here the first print
253
00:09:53,600 --> 00:09:55,519
statement prints the value of x that is
254
00:09:55,519 --> 00:09:58,080
7 and the second print statement gives
255
00:09:58,080 --> 00:10:00,080
me the type or the data type of x which
256
00:10:00,080 --> 00:10:02,839
says it is of class integer or
257
00:10:02,839 --> 00:10:05,920
int cool
258
00:10:05,920 --> 00:10:07,519
now
259
00:10:07,519 --> 00:10:11,120
let me show you how you can use
260
00:10:11,120 --> 00:10:14,320
the input function to take user inputs
261
00:10:14,320 --> 00:10:16,720
suppose
262
00:10:16,720 --> 00:10:20,240
i have a variable x
263
00:10:20,240 --> 00:10:23,440
where i want to store some
264
00:10:23,440 --> 00:10:25,519
numbers
265
00:10:25,519 --> 00:10:27,120
i'm going to use the
266
00:10:27,120 --> 00:10:29,440
int function and inside the in function
267
00:10:29,440 --> 00:10:31,360
i'll give my input function
268
00:10:31,360 --> 00:10:32,959
so when i run it
269
00:10:32,959 --> 00:10:36,720
it will ask the user to input a number
270
00:10:37,200 --> 00:10:38,640
i'll write
271
00:10:38,640 --> 00:10:40,640
enter
272
00:10:40,640 --> 00:10:42,079
number
273
00:10:42,079 --> 00:10:44,320
x
274
00:10:45,680 --> 00:10:47,440
okay
275
00:10:47,440 --> 00:10:48,800
similarly
276
00:10:48,800 --> 00:10:51,760
i'll just copy this
277
00:10:52,079 --> 00:10:55,040
come to the next line
278
00:10:56,480 --> 00:10:58,320
i want to create another variable called
279
00:10:58,320 --> 00:10:59,360
y
280
00:10:59,360 --> 00:11:01,600
which will have another number i will
281
00:11:01,600 --> 00:11:04,399
write enter number
282
00:11:04,399 --> 00:11:06,079
for y
283
00:11:06,079 --> 00:11:08,320
and then
284
00:11:08,320 --> 00:11:11,040
i am going to
285
00:11:11,279 --> 00:11:12,880
print
286
00:11:12,880 --> 00:11:14,720
the addition
287
00:11:14,720 --> 00:11:16,160
so i'll write
288
00:11:16,160 --> 00:11:18,399
addition of
289
00:11:18,399 --> 00:11:20,160
x and
290
00:11:20,160 --> 00:11:22,480
y is
291
00:11:22,480 --> 00:11:24,320
give a colon
292
00:11:24,320 --> 00:11:26,160
and then
293
00:11:26,160 --> 00:11:28,160
i'll pass in the operation within
294
00:11:28,160 --> 00:11:31,279
brackets that is x plus y
295
00:11:31,279 --> 00:11:32,399
okay
296
00:11:32,399 --> 00:11:35,760
so essentially what i am doing here is
297
00:11:35,760 --> 00:11:36,800
i am
298
00:11:36,800 --> 00:11:39,440
assigning the value of
299
00:11:39,440 --> 00:11:43,040
my first number to x then i'm assigning
300
00:11:43,040 --> 00:11:45,279
the value of my second number to y and
301
00:11:45,279 --> 00:11:47,200
then i'm finding the
302
00:11:47,200 --> 00:11:49,200
sum of x and y
303
00:11:49,200 --> 00:11:50,800
let me
304
00:11:50,800 --> 00:11:53,600
show you the result if i hit shift enter
305
00:11:53,600 --> 00:11:55,519
it is asking for the user input because
306
00:11:55,519 --> 00:11:58,000
i had defined my input function
307
00:11:58,000 --> 00:11:59,200
so
308
00:11:59,200 --> 00:12:00,560
it is asking
309
00:12:00,560 --> 00:12:02,800
enter number x let me
310
00:12:02,800 --> 00:12:05,519
give my number as let's say 30
311
00:12:05,519 --> 00:12:07,279
i'll hit enter
312
00:12:07,279 --> 00:12:09,360
again it is asking for my
313
00:12:09,360 --> 00:12:12,160
buy input let's say i'll give
314
00:12:12,160 --> 00:12:13,920
60
315
00:12:13,920 --> 00:12:16,000
and then i'll hit enter
316
00:12:16,000 --> 00:12:17,920
there you go it has written me the
317
00:12:17,920 --> 00:12:19,279
addition of
318
00:12:19,279 --> 00:12:22,880
30 and 60 that is 90. you can see the
319
00:12:22,880 --> 00:12:25,440
message as well addition of x and y is
320
00:12:25,440 --> 00:12:26,800
90.
321
00:12:26,800 --> 00:12:29,040
cool
322
00:12:29,440 --> 00:12:30,720
now
323
00:12:30,720 --> 00:12:32,959
let me show you
324
00:12:32,959 --> 00:12:35,360
how to add two strings
325
00:12:35,360 --> 00:12:37,839
and we'll compare the results when you
326
00:12:37,839 --> 00:12:40,160
add two strings and two numbers so
327
00:12:40,160 --> 00:12:41,200
basically
328
00:12:41,200 --> 00:12:43,600
when you add two strings
329
00:12:43,600 --> 00:12:45,680
it is going to merge both the strings or
330
00:12:45,680 --> 00:12:49,120
concatenate both the strings
331
00:12:50,320 --> 00:12:53,760
the way to do is i'll just copy the
332
00:12:53,760 --> 00:12:55,600
let me copy all the three lines of code
333
00:12:55,600 --> 00:12:59,279
from the top i'll paste it here
334
00:12:59,279 --> 00:13:01,519
now i'm going to remove the
335
00:13:01,519 --> 00:13:03,920
integer function that i had used
336
00:13:03,920 --> 00:13:05,920
the integer function was actually used
337
00:13:05,920 --> 00:13:07,360
to
338
00:13:07,360 --> 00:13:09,279
typecast the
339
00:13:09,279 --> 00:13:11,920
input to integer
340
00:13:11,920 --> 00:13:15,200
but now i am going to take the values
341
00:13:15,200 --> 00:13:16,639
in terms of
342
00:13:16,639 --> 00:13:18,720
text or string
343
00:13:18,720 --> 00:13:21,839
now let's see the difference
344
00:13:23,360 --> 00:13:25,360
if i run this
345
00:13:25,360 --> 00:13:28,000
let's say i'll enter my
346
00:13:28,000 --> 00:13:30,399
number as 10 and then
347
00:13:30,399 --> 00:13:33,680
my second number would be let's say 20
348
00:13:33,680 --> 00:13:36,399
if i add 10 and 20
349
00:13:36,399 --> 00:13:39,040
it is going to merge both the numbers
350
00:13:39,040 --> 00:13:41,920
and return me 1 0 to 0.
351
00:13:41,920 --> 00:13:43,120
so
352
00:13:43,120 --> 00:13:45,680
when you add two strings
353
00:13:45,680 --> 00:13:48,880
it is going to return you
354
00:13:48,880 --> 00:13:52,480
the merged value of both the strings
355
00:13:52,480 --> 00:13:53,680
all right
356
00:13:53,680 --> 00:13:55,680
now let's go ahead and explore some of
357
00:13:55,680 --> 00:13:58,079
the popular data types in python
358
00:13:58,079 --> 00:14:01,440
now i am giving a hashtag and typing
359
00:14:01,440 --> 00:14:03,920
data types this means
360
00:14:03,920 --> 00:14:06,560
anything followed by the hash represents
361
00:14:06,560 --> 00:14:08,079
a comment
362
00:14:08,079 --> 00:14:09,600
so
363
00:14:09,600 --> 00:14:11,360
in other programming languages also we
364
00:14:11,360 --> 00:14:13,120
have comments similarly in python if you
365
00:14:13,120 --> 00:14:15,519
want to write a comment you can give
366
00:14:15,519 --> 00:14:17,360
a hash and
367
00:14:17,360 --> 00:14:19,440
pass in the comment
368
00:14:19,440 --> 00:14:23,040
so first let's see i have a variable
369
00:14:23,040 --> 00:14:23,839
a
370
00:14:23,839 --> 00:14:26,880
which has a value of 7
371
00:14:26,880 --> 00:14:29,199
or let's say 10
372
00:14:29,199 --> 00:14:32,560
now if you were to find
373
00:14:32,560 --> 00:14:35,360
the type of
374
00:14:35,920 --> 00:14:37,760
value that a
375
00:14:37,760 --> 00:14:40,560
variable is holding you can use the type
376
00:14:40,560 --> 00:14:43,279
function if i run this it gives me
377
00:14:43,279 --> 00:14:46,320
integer because 10 is an integer
378
00:14:46,320 --> 00:14:49,920
let's say i want to find
379
00:14:50,639 --> 00:14:53,040
the type of value
380
00:14:53,040 --> 00:14:55,040
minus 7 is
381
00:14:55,040 --> 00:14:57,199
now if i run this again minus 7 is an
382
00:14:57,199 --> 00:14:59,199
integer let's say
383
00:14:59,199 --> 00:15:01,760
i want to know
384
00:15:01,760 --> 00:15:04,800
what 3.5 is actually if i run this it
385
00:15:04,800 --> 00:15:08,240
gives me float because 3.5 has a
386
00:15:08,240 --> 00:15:11,360
floating point or a decimal point
387
00:15:11,360 --> 00:15:13,839
similarly let's see
388
00:15:13,839 --> 00:15:18,399
another example i'll write minus 2.4566
389
00:15:18,399 --> 00:15:20,800
if i run this again this is also a float
390
00:15:20,800 --> 00:15:22,079
value
391
00:15:22,079 --> 00:15:23,839
similarly
392
00:15:23,839 --> 00:15:26,399
now let's write a complex number
393
00:15:26,399 --> 00:15:28,720
which has a real part and an imaginary
394
00:15:28,720 --> 00:15:29,680
part
395
00:15:29,680 --> 00:15:31,199
let's say i write
396
00:15:31,199 --> 00:15:32,079
7
397
00:15:32,079 --> 00:15:32,880
plus
398
00:15:32,880 --> 00:15:35,360
10 j
399
00:15:35,360 --> 00:15:38,000
if i run this there you go this is a
400
00:15:38,000 --> 00:15:40,880
complex number hence it has returned
401
00:15:40,880 --> 00:15:43,680
the value is complex
402
00:15:43,680 --> 00:15:45,360
now
403
00:15:45,360 --> 00:15:49,199
if i just write 10 j let's see what it
404
00:15:49,199 --> 00:15:50,399
represents
405
00:15:50,399 --> 00:15:52,880
again it's a complex value
406
00:15:52,880 --> 00:15:55,440
now let's pass in a boolean value
407
00:15:55,440 --> 00:15:58,720
suppose i write true
408
00:15:58,880 --> 00:16:01,680
now true and false are boolean values
409
00:16:01,680 --> 00:16:03,680
so ideally our python should return
410
00:16:03,680 --> 00:16:06,320
boolean only there you go you can see it
411
00:16:06,320 --> 00:16:09,680
has returned us boolean
412
00:16:09,680 --> 00:16:13,440
now let's move ahead and
413
00:16:13,600 --> 00:16:15,519
explore some of the
414
00:16:15,519 --> 00:16:18,240
popular objects in python so we'll start
415
00:16:18,240 --> 00:16:19,440
with
416
00:16:19,440 --> 00:16:20,720
list
417
00:16:20,720 --> 00:16:22,639
list is one of the popular objects in
418
00:16:22,639 --> 00:16:23,759
python
419
00:16:23,759 --> 00:16:24,800
now to
420
00:16:24,800 --> 00:16:27,279
declare a list i'll
421
00:16:27,279 --> 00:16:31,759
create a variable called let's say l1
422
00:16:32,000 --> 00:16:34,000
i'll give an equal to
423
00:16:34,000 --> 00:16:36,079
and within square brackets i'll pass in
424
00:16:36,079 --> 00:16:38,160
my list
425
00:16:38,160 --> 00:16:41,519
list can hold data of multiple data
426
00:16:41,519 --> 00:16:42,720
types
427
00:16:42,720 --> 00:16:45,040
i'll show you now so john here is a
428
00:16:45,040 --> 00:16:47,120
string
429
00:16:47,120 --> 00:16:49,360
i'll give something like 1 0
430
00:16:49,360 --> 00:16:53,920
2 could be the employee id of john
431
00:16:53,920 --> 00:16:55,120
i'll give
432
00:16:55,120 --> 00:16:57,279
usa let's say the
433
00:16:57,279 --> 00:16:59,839
country to which jon belongs to
434
00:16:59,839 --> 00:17:01,839
and i'll give a floating point value
435
00:17:01,839 --> 00:17:04,160
let's say 180.5
436
00:17:04,160 --> 00:17:06,400
assuming it's centimeters and this is
437
00:17:06,400 --> 00:17:09,199
john's height suppose
438
00:17:09,199 --> 00:17:11,439
if i run this
439
00:17:11,439 --> 00:17:13,760
you see i have created my list and if
440
00:17:13,760 --> 00:17:14,880
you
441
00:17:14,880 --> 00:17:18,240
want to print it i'll just write l1
442
00:17:18,240 --> 00:17:19,280
there you go
443
00:17:19,280 --> 00:17:21,439
so here i have my
444
00:17:21,439 --> 00:17:24,400
list l1 printed
445
00:17:24,400 --> 00:17:26,959
now suppose you want to
446
00:17:26,959 --> 00:17:28,319
check the
447
00:17:28,319 --> 00:17:30,720
type of l1
448
00:17:30,720 --> 00:17:32,799
you can use the type function again and
449
00:17:32,799 --> 00:17:34,480
pass in the
450
00:17:34,480 --> 00:17:37,440
variable that is l1
451
00:17:37,440 --> 00:17:38,720
there you go
452
00:17:38,720 --> 00:17:41,440
it tells that l1
453
00:17:41,440 --> 00:17:45,039
has the values in the form of a list
454
00:17:45,039 --> 00:17:47,679
similarly you can create
455
00:17:47,679 --> 00:17:48,640
list
456
00:17:48,640 --> 00:17:51,280
of the same data type let's say
457
00:17:51,280 --> 00:17:52,880
only of numbers
458
00:17:52,880 --> 00:17:55,200
okay
459
00:17:55,360 --> 00:17:59,120
now if i just print l2 you can see
460
00:17:59,120 --> 00:18:01,200
2 has only numbers
461
00:18:01,200 --> 00:18:04,480
similarly you can check the
462
00:18:04,480 --> 00:18:06,640
type of l2
463
00:18:06,640 --> 00:18:10,480
run this it is also a list
464
00:18:10,640 --> 00:18:13,640
okay
465
00:18:18,400 --> 00:18:20,480
now another popular object in python is
466
00:18:20,480 --> 00:18:23,600
called a tuple or tuple
467
00:18:23,600 --> 00:18:25,919
now the only difference between
468
00:18:25,919 --> 00:18:29,520
a list and a tuple is that lists are
469
00:18:29,520 --> 00:18:31,919
mutable which means once you declare a
470
00:18:31,919 --> 00:18:34,080
list you can change the values
471
00:18:34,080 --> 00:18:36,960
while tuples or tuples are immutable
472
00:18:36,960 --> 00:18:38,960
which means that once you declare a
473
00:18:38,960 --> 00:18:43,240
tuple you cannot change the values
474
00:18:44,000 --> 00:18:46,160
now another difference is that
475
00:18:46,160 --> 00:18:50,559
list is written within square brackets
476
00:18:50,559 --> 00:18:53,919
but when you assign a tuple it is within
477
00:18:53,919 --> 00:18:54,840
normal
478
00:18:54,840 --> 00:18:58,640
braces or round brackets as you say
479
00:18:58,640 --> 00:19:02,640
so i'll create a tuple called tup
480
00:19:02,640 --> 00:19:03,840
and within
481
00:19:03,840 --> 00:19:06,240
brackets
482
00:19:06,240 --> 00:19:08,559
i'll
483
00:19:08,559 --> 00:19:10,480
give the values let's say
484
00:19:10,480 --> 00:19:12,960
i want to give the
485
00:19:12,960 --> 00:19:16,320
values of vehicles or cars
486
00:19:16,320 --> 00:19:18,160
so brands of different car we have in
487
00:19:18,160 --> 00:19:20,880
this tuple let's say bmw then we have
488
00:19:20,880 --> 00:19:23,880
audi
489
00:19:24,240 --> 00:19:28,080
next we have mercedes
490
00:19:28,880 --> 00:19:30,080
let's say
491
00:19:30,080 --> 00:19:32,640
jaguar
492
00:19:34,640 --> 00:19:36,799
okay
493
00:19:36,799 --> 00:19:37,679
now
494
00:19:37,679 --> 00:19:40,080
you can use the print statement to print
495
00:19:40,080 --> 00:19:42,640
the tuple
496
00:19:44,240 --> 00:19:48,000
and let's say i also want to print
497
00:19:48,000 --> 00:19:51,440
the type so i'll use the type function
498
00:19:51,440 --> 00:19:54,000
and pass in my variable that is tup if i
499
00:19:54,000 --> 00:19:55,280
run this
500
00:19:55,280 --> 00:19:57,440
you can see the first print statement
501
00:19:57,440 --> 00:19:58,960
prints the
502
00:19:58,960 --> 00:20:02,000
values present in the tuple top and then
503
00:20:02,000 --> 00:20:03,600
we have the type
504
00:20:03,600 --> 00:20:08,320
you can see it is of type or class tuple
505
00:20:08,720 --> 00:20:11,039
now next we have our
506
00:20:11,039 --> 00:20:12,559
third object
507
00:20:12,559 --> 00:20:16,000
which is called a dictionary
508
00:20:16,640 --> 00:20:20,159
i am giving comment here for readability
509
00:20:20,159 --> 00:20:22,480
okay
510
00:20:23,840 --> 00:20:26,400
now dictionary consists of key value
511
00:20:26,400 --> 00:20:28,720
pairs and
512
00:20:28,720 --> 00:20:31,280
again mutable objects just like list
513
00:20:31,280 --> 00:20:33,200
which means you can change the values of
514
00:20:33,200 --> 00:20:36,559
the dictionary once they are assigned
515
00:20:36,559 --> 00:20:39,200
and dictionaries are written within
516
00:20:39,200 --> 00:20:41,760
curly braces
517
00:20:41,760 --> 00:20:43,600
let's say i want to create an employee
518
00:20:43,600 --> 00:20:45,840
dictionary
519
00:20:45,840 --> 00:20:47,840
so i'll give my variable name as
520
00:20:47,840 --> 00:20:48,960
employee
521
00:20:48,960 --> 00:20:49,919
and then
522
00:20:49,919 --> 00:20:51,280
start with my
523
00:20:51,280 --> 00:20:53,039
curly braces
524
00:20:53,039 --> 00:20:54,400
first
525
00:20:54,400 --> 00:20:56,640
i'll give the key
526
00:20:56,640 --> 00:20:57,440
say
527
00:20:57,440 --> 00:21:00,880
the name and then we have the
528
00:21:00,880 --> 00:21:02,559
name of the person in terms of value
529
00:21:02,559 --> 00:21:04,400
that is john
530
00:21:04,400 --> 00:21:07,280
next we have his age
531
00:21:07,280 --> 00:21:10,320
let's say ages 29
532
00:21:10,320 --> 00:21:12,159
now one thing to remember here is the
533
00:21:12,159 --> 00:21:14,080
keys should be passed within double
534
00:21:14,080 --> 00:21:15,120
quotes
535
00:21:15,120 --> 00:21:17,360
and the value is a string you can pass
536
00:21:17,360 --> 00:21:19,440
within quotes
537
00:21:19,440 --> 00:21:21,919
but if the value is an integer or a
538
00:21:21,919 --> 00:21:23,600
floating point
539
00:21:23,600 --> 00:21:26,000
number you don't have to pass within
540
00:21:26,000 --> 00:21:28,400
quotes
541
00:21:28,480 --> 00:21:31,840
let's say i want to have the
542
00:21:31,840 --> 00:21:34,720
salary of john as well
543
00:21:34,720 --> 00:21:37,760
again salary is in terms of numbers so
544
00:21:37,760 --> 00:21:40,559
you don't have to pass it within
545
00:21:40,559 --> 00:21:42,480
quotes
546
00:21:42,480 --> 00:21:45,200
let's say
547
00:21:45,200 --> 00:21:46,799
the third information or the fourth
548
00:21:46,799 --> 00:21:48,799
information i have about
549
00:21:48,799 --> 00:21:52,799
john is the company in which he works
550
00:21:53,760 --> 00:21:54,799
let's say
551
00:21:54,799 --> 00:21:56,400
john
552
00:21:56,400 --> 00:21:59,840
works at oracle
553
00:21:59,919 --> 00:22:02,960
so i have my dictionary defined
554
00:22:02,960 --> 00:22:05,280
now
555
00:22:05,280 --> 00:22:08,080
i'll write print
556
00:22:08,080 --> 00:22:11,520
my dictionary employee
557
00:22:11,520 --> 00:22:13,520
and then
558
00:22:13,520 --> 00:22:16,880
i also want to check the
559
00:22:17,360 --> 00:22:19,600
type
560
00:22:21,679 --> 00:22:23,600
okay
561
00:22:23,600 --> 00:22:25,360
let's print
562
00:22:25,360 --> 00:22:27,200
there you go so the first print
563
00:22:27,200 --> 00:22:29,280
statement prints the dictionary employee
564
00:22:29,280 --> 00:22:31,520
and then we have the
565
00:22:31,520 --> 00:22:33,360
data type of
566
00:22:33,360 --> 00:22:35,919
my dictionary employee that is dict
567
00:22:35,919 --> 00:22:40,039
which means dictionary or dict
568
00:22:41,120 --> 00:22:43,120
the fourth python object we are going to
569
00:22:43,120 --> 00:22:46,320
discuss is called a set
570
00:22:46,320 --> 00:22:47,360
now even
571
00:22:47,360 --> 00:22:50,480
a set is created using curly braces like
572
00:22:50,480 --> 00:22:52,720
how we have used in the dictionary but
573
00:22:52,720 --> 00:22:56,000
the difference is sets do not contain
574
00:22:56,000 --> 00:22:57,840
key value pairs
575
00:22:57,840 --> 00:22:58,799
but
576
00:22:58,799 --> 00:23:01,760
they can only contain unique values now
577
00:23:01,760 --> 00:23:03,600
let me show you what i mean
578
00:23:03,600 --> 00:23:05,679
let's say i want to create a set
579
00:23:05,679 --> 00:23:08,880
that has only unique values of months so
580
00:23:08,880 --> 00:23:10,159
within
581
00:23:10,159 --> 00:23:13,600
curly braces i'll pass in my
582
00:23:13,600 --> 00:23:16,320
month names let's say jan
583
00:23:16,320 --> 00:23:17,919
then i have
584
00:23:17,919 --> 00:23:20,240
feb
585
00:23:20,480 --> 00:23:23,760
then i have mar for march
586
00:23:23,760 --> 00:23:26,080
let's say apr for april
587
00:23:26,080 --> 00:23:28,159
so this is a set
588
00:23:28,159 --> 00:23:30,960
and if i want to print i can just
589
00:23:30,960 --> 00:23:32,480
pass in the
590
00:23:32,480 --> 00:23:35,200
variable name that is month let's run it
591
00:23:35,200 --> 00:23:39,440
okay you can see it here it has printed
592
00:23:39,440 --> 00:23:42,240
my set which is not in an order
593
00:23:42,240 --> 00:23:44,080
okay
594
00:23:44,080 --> 00:23:47,679
now you can also check the
595
00:23:48,840 --> 00:23:54,080
type of the variable that is month
596
00:23:55,760 --> 00:23:57,840
if i run it you can see it here it is of
597
00:23:57,840 --> 00:23:58,960
type
598
00:23:58,960 --> 00:24:01,120
or class set
599
00:24:01,120 --> 00:24:02,880
now the difference i want to show here
600
00:24:02,880 --> 00:24:04,400
is as i mentioned
601
00:24:04,400 --> 00:24:06,960
set can only contain unique values
602
00:24:06,960 --> 00:24:10,320
suppose i have another value of
603
00:24:10,320 --> 00:24:13,440
month let's say february feb
604
00:24:13,440 --> 00:24:15,440
if i run this
605
00:24:15,440 --> 00:24:16,960
i'll remove this
606
00:24:16,960 --> 00:24:18,960
if i run it you can see here
607
00:24:18,960 --> 00:24:20,159
still
608
00:24:20,159 --> 00:24:22,480
my set has only unique values so it does
609
00:24:22,480 --> 00:24:25,600
not consider any duplicate values
610
00:24:25,600 --> 00:24:27,679
all right
611
00:24:27,679 --> 00:24:30,320
now moving ahead we will explore how to
612
00:24:30,320 --> 00:24:33,840
extract certain values from the
613
00:24:33,840 --> 00:24:35,840
python objects we'll be using different
614
00:24:35,840 --> 00:24:39,039
techniques bait indexing or slicing
615
00:24:39,039 --> 00:24:40,400
okay
616
00:24:40,400 --> 00:24:42,720
so if i go to the top i had created a
617
00:24:42,720 --> 00:24:46,039
list called
618
00:24:46,240 --> 00:24:47,679
l1
619
00:24:47,679 --> 00:24:51,279
let me print l1 again
620
00:24:51,600 --> 00:24:53,600
okay so these are the values that i have
621
00:24:53,600 --> 00:24:55,279
in my list
622
00:24:55,279 --> 00:24:57,760
l1
623
00:24:57,840 --> 00:24:58,840
now let's
624
00:24:58,840 --> 00:25:02,720
say i want to print
625
00:25:02,720 --> 00:25:05,039
the first element
626
00:25:05,039 --> 00:25:07,279
in my list l1
627
00:25:07,279 --> 00:25:09,120
the way to do is i'll use my print
628
00:25:09,120 --> 00:25:11,039
statement
629
00:25:11,039 --> 00:25:14,640
i'll give my variable name that is l1
630
00:25:14,640 --> 00:25:16,880
and within square brackets i'll give the
631
00:25:16,880 --> 00:25:18,000
index
632
00:25:18,000 --> 00:25:20,320
let's say one
633
00:25:20,320 --> 00:25:22,080
if i run it
634
00:25:22,080 --> 00:25:25,200
it gives me the value 102 the reason
635
00:25:25,200 --> 00:25:26,159
being
636
00:25:26,159 --> 00:25:28,640
in python the index values start from
637
00:25:28,640 --> 00:25:29,520
zero
638
00:25:29,520 --> 00:25:33,679
so this the first value is at index zero
639
00:25:33,679 --> 00:25:36,240
then the second value is at index one
640
00:25:36,240 --> 00:25:38,880
the third value is at index two and the
641
00:25:38,880 --> 00:25:41,120
fourth value is at index three
642
00:25:41,120 --> 00:25:43,840
so if you have to
643
00:25:44,320 --> 00:25:47,200
find what is there at index
644
00:25:47,200 --> 00:25:48,480
3
645
00:25:48,480 --> 00:25:51,200
i just need to replace
646
00:25:51,200 --> 00:25:53,279
1 with 3.
647
00:25:53,279 --> 00:25:54,799
if i run this
648
00:25:54,799 --> 00:25:57,039
you see it here it gives me
649
00:25:57,039 --> 00:25:59,360
180.5
650
00:25:59,360 --> 00:26:01,520
which we had assumed to be the height of
651
00:26:01,520 --> 00:26:03,360
john in centimeters
652
00:26:03,360 --> 00:26:05,200
now you can also do this without even
653
00:26:05,200 --> 00:26:07,120
using the print statement that is the
654
00:26:07,120 --> 00:26:09,360
beauty of python
655
00:26:09,360 --> 00:26:12,159
if i write l1 and within
656
00:26:12,159 --> 00:26:15,520
square brackets let's say i write
657
00:26:15,520 --> 00:26:17,679
2 if i run this
658
00:26:17,679 --> 00:26:20,320
it gives me usa because
659
00:26:20,320 --> 00:26:22,400
usa is the value that is present at
660
00:26:22,400 --> 00:26:25,120
index 0. so
661
00:26:25,120 --> 00:26:27,600
very important thing to remember while
662
00:26:27,600 --> 00:26:30,000
indexing or slicing
663
00:26:30,000 --> 00:26:33,039
is that the index values start from 0
664
00:26:33,039 --> 00:26:35,520
and not 1. so if you had to find the
665
00:26:35,520 --> 00:26:37,360
value
666
00:26:37,360 --> 00:26:39,679
where john is present
667
00:26:39,679 --> 00:26:41,760
you need to use
668
00:26:41,760 --> 00:26:44,960
0 instead of 1. if i run this it will
669
00:26:44,960 --> 00:26:46,720
return me john
670
00:26:46,720 --> 00:26:48,159
ok
671
00:26:48,159 --> 00:26:51,120
now you can also use negative indexing
672
00:26:51,120 --> 00:26:52,799
let's say i write
673
00:26:52,799 --> 00:26:54,320
l1
674
00:26:54,320 --> 00:26:56,720
-1
675
00:26:56,720 --> 00:26:59,120
you can take a guess
676
00:26:59,120 --> 00:27:00,960
what this will return
677
00:27:00,960 --> 00:27:02,320
if i run this
678
00:27:02,320 --> 00:27:05,120
it returns me 180.5
679
00:27:05,120 --> 00:27:07,520
when you use negative indexing
680
00:27:07,520 --> 00:27:09,440
it will start from the
681
00:27:09,440 --> 00:27:11,919
last position so minus 1
682
00:27:11,919 --> 00:27:13,840
is actually this index
683
00:27:13,840 --> 00:27:16,320
let's say i'll write minus 3
684
00:27:16,320 --> 00:27:19,840
and see the result it gives me 102 which
685
00:27:19,840 --> 00:27:23,760
is fair enough if you start from
686
00:27:23,840 --> 00:27:27,600
the back this is minus 1 usa is at minus
687
00:27:27,600 --> 00:27:31,520
2 and at minus 3 we have 1 0 2
688
00:27:31,520 --> 00:27:33,200
all right
689
00:27:33,200 --> 00:27:35,840
also you can use slicing
690
00:27:35,840 --> 00:27:39,520
let's say i'll write l1
691
00:27:39,520 --> 00:27:41,600
within square brackets i'll give let's
692
00:27:41,600 --> 00:27:42,799
say
693
00:27:42,799 --> 00:27:44,720
from 1
694
00:27:44,720 --> 00:27:46,159
to
695
00:27:46,159 --> 00:27:47,760
3
696
00:27:47,760 --> 00:27:49,200
so i want
697
00:27:49,200 --> 00:27:51,679
all the values present from the first
698
00:27:51,679 --> 00:27:53,440
index which is this
699
00:27:53,440 --> 00:27:54,640
till
700
00:27:54,640 --> 00:27:56,559
the third index but
701
00:27:56,559 --> 00:27:58,559
excluding the third index
702
00:27:58,559 --> 00:28:01,120
if i run this it gives me
703
00:28:01,120 --> 00:28:04,880
102 which is at the first index and then
704
00:28:04,880 --> 00:28:07,279
usa which is at the second index now in
705
00:28:07,279 --> 00:28:08,559
python
706
00:28:08,559 --> 00:28:09,440
the
707
00:28:09,440 --> 00:28:11,600
last index is exclusive it is not
708
00:28:11,600 --> 00:28:14,120
included so in case you wanted to print
709
00:28:14,120 --> 00:28:16,960
180.5 as well we have to change this
710
00:28:16,960 --> 00:28:18,720
from three to four
711
00:28:18,720 --> 00:28:20,240
now if i run this
712
00:28:20,240 --> 00:28:21,480
you get
713
00:28:21,480 --> 00:28:24,320
180.5 as well
714
00:28:24,320 --> 00:28:26,640
okay
715
00:28:29,200 --> 00:28:31,679
moving ahead
716
00:28:31,679 --> 00:28:33,360
there are some basic
717
00:28:33,360 --> 00:28:36,799
functions that you can use
718
00:28:37,520 --> 00:28:40,159
with python objects for example if you
719
00:28:40,159 --> 00:28:41,360
want to
720
00:28:41,360 --> 00:28:43,840
add any
721
00:28:43,840 --> 00:28:46,240
value to your list l1 you can use the
722
00:28:46,240 --> 00:28:47,520
append function
723
00:28:47,520 --> 00:28:49,760
suppose i write l1
724
00:28:49,760 --> 00:28:51,200
dot
725
00:28:51,200 --> 00:28:52,399
append
726
00:28:52,399 --> 00:28:56,080
let's say i want to add the
727
00:28:56,799 --> 00:28:59,039
vehicle or the car
728
00:28:59,039 --> 00:29:01,600
that john has
729
00:29:01,600 --> 00:29:04,240
let's see i'll write append
730
00:29:04,240 --> 00:29:07,039
range rover
731
00:29:08,240 --> 00:29:10,399
if i run this
732
00:29:10,399 --> 00:29:12,640
and let's print l1 again
733
00:29:12,640 --> 00:29:14,880
you can see it here we have appended or
734
00:29:14,880 --> 00:29:17,840
added a new value to our list l1
735
00:29:17,840 --> 00:29:19,120
cool
736
00:29:19,120 --> 00:29:20,799
now you can also use the reverse
737
00:29:20,799 --> 00:29:22,640
function
738
00:29:22,640 --> 00:29:25,279
in python if you want to print the list
739
00:29:25,279 --> 00:29:26,880
in reverse order
740
00:29:26,880 --> 00:29:29,200
so let's see
741
00:29:29,200 --> 00:29:31,520
i want to print the elements present in
742
00:29:31,520 --> 00:29:33,200
l1 in reverse order
743
00:29:33,200 --> 00:29:35,440
i just need to write my variable that is
744
00:29:35,440 --> 00:29:37,200
l1
745
00:29:37,200 --> 00:29:40,320
followed by dot and the reverse function
746
00:29:40,320 --> 00:29:42,240
it's very simple
747
00:29:42,240 --> 00:29:44,320
if i run this
748
00:29:44,320 --> 00:29:48,000
and i will print l1 now
749
00:29:48,000 --> 00:29:51,760
it prints my list in reverse order here
750
00:29:51,760 --> 00:29:52,840
it was from
751
00:29:52,840 --> 00:29:56,399
john all the way till range rover
752
00:29:56,399 --> 00:29:58,799
once i reverse it it starts from range
753
00:29:58,799 --> 00:30:01,360
rover and all the way to john
754
00:30:01,360 --> 00:30:03,279
cool
755
00:30:03,279 --> 00:30:04,399
similarly
756
00:30:04,399 --> 00:30:05,760
you can
757
00:30:05,760 --> 00:30:06,720
also
758
00:30:06,720 --> 00:30:09,919
extract specific values from tuples and
759
00:30:09,919 --> 00:30:12,159
dictionaries let me show you
760
00:30:12,159 --> 00:30:14,799
i'll just go to the top and
761
00:30:14,799 --> 00:30:16,960
okay so we had created a tuple called
762
00:30:16,960 --> 00:30:18,720
tup
763
00:30:18,720 --> 00:30:21,440
let me print that tuple again
764
00:30:21,440 --> 00:30:23,600
so these are the values of the different
765
00:30:23,600 --> 00:30:27,120
brand of cars we have in the tuple
766
00:30:27,120 --> 00:30:29,279
let's say
767
00:30:29,279 --> 00:30:32,399
you want to find
768
00:30:32,399 --> 00:30:35,120
the car that is present in the second
769
00:30:35,120 --> 00:30:38,559
index so you can just write
770
00:30:38,559 --> 00:30:40,799
2 within square brackets
771
00:30:40,799 --> 00:30:43,120
so i have my double name followed by the
772
00:30:43,120 --> 00:30:45,440
square brackets and then i have my index
773
00:30:45,440 --> 00:30:48,159
if i run this you get more cities and
774
00:30:48,159 --> 00:30:49,440
again
775
00:30:49,440 --> 00:30:52,000
in python tuples the index values start
776
00:30:52,000 --> 00:30:54,799
at zero and not one so bmw is at index
777
00:30:54,799 --> 00:30:58,000
zero audi is at index one mercedes is at
778
00:30:58,000 --> 00:31:00,880
index two you see it here and then
779
00:31:00,880 --> 00:31:03,760
jaguar we have it index three
780
00:31:03,760 --> 00:31:06,880
again you can use slicing by using colon
781
00:31:06,880 --> 00:31:08,559
and you can also use
782
00:31:08,559 --> 00:31:10,399
negative indexing let's say i'll change
783
00:31:10,399 --> 00:31:11,600
the value from
784
00:31:11,600 --> 00:31:15,120
2 to let's say -3
785
00:31:15,279 --> 00:31:17,840
see the result you see here we have audi
786
00:31:17,840 --> 00:31:20,320
because this is minus one
787
00:31:20,320 --> 00:31:22,640
mercedes is at minus two and aud is at
788
00:31:22,640 --> 00:31:24,159
minus three
789
00:31:24,159 --> 00:31:25,039
okay
790
00:31:25,039 --> 00:31:27,200
now
791
00:31:28,159 --> 00:31:29,840
you can extract the values in the
792
00:31:29,840 --> 00:31:30,960
dictionary
793
00:31:30,960 --> 00:31:34,240
by using the keys in the dictionary
794
00:31:34,240 --> 00:31:36,240
let me go to the top and
795
00:31:36,240 --> 00:31:38,240
see the dictionary that we had created
796
00:31:38,240 --> 00:31:42,000
it is employee let me print it
797
00:31:45,519 --> 00:31:47,440
now in jupyter notebook you can also use
798
00:31:47,440 --> 00:31:50,720
the tab to auto complete a variable name
799
00:31:50,720 --> 00:31:52,559
or a function name
800
00:31:52,559 --> 00:31:54,080
is one of the features that jupiter
801
00:31:54,080 --> 00:31:56,000
notebook provides
802
00:31:56,000 --> 00:31:58,240
so here we have the employee
803
00:31:58,240 --> 00:32:00,399
dictionary
804
00:32:00,399 --> 00:32:03,039
and let's say
805
00:32:03,679 --> 00:32:06,320
you want to
806
00:32:06,960 --> 00:32:09,440
print
807
00:32:10,720 --> 00:32:13,960
the value
808
00:32:14,720 --> 00:32:16,399
of salary
809
00:32:16,399 --> 00:32:18,320
the way to do is
810
00:32:18,320 --> 00:32:20,640
i'll write print
811
00:32:20,640 --> 00:32:21,919
employee
812
00:32:21,919 --> 00:32:24,480
dot i can use the get function
813
00:32:24,480 --> 00:32:27,679
and within the get function
814
00:32:27,679 --> 00:32:29,600
i'll pass in my
815
00:32:29,600 --> 00:32:31,679
key for
816
00:32:31,679 --> 00:32:34,240
salary
817
00:32:34,559 --> 00:32:38,080
if i run this you see here it gives me
818
00:32:38,080 --> 00:32:40,480
2 lakhs 50 000 which is the salary of
819
00:32:40,480 --> 00:32:42,399
john
820
00:32:42,399 --> 00:32:44,960
similarly if you want to let's say
821
00:32:44,960 --> 00:32:47,360
update any value in the dictionary you
822
00:32:47,360 --> 00:32:49,519
can use the update function
823
00:32:49,519 --> 00:32:50,799
suppose
824
00:32:50,799 --> 00:32:53,120
i want to update
825
00:32:53,120 --> 00:32:55,440
the company of john
826
00:32:55,440 --> 00:32:57,760
from oracle to let's say
827
00:32:57,760 --> 00:32:58,960
google
828
00:32:58,960 --> 00:33:00,880
the way to do is
829
00:33:00,880 --> 00:33:02,240
i'll write
830
00:33:02,240 --> 00:33:04,799
my dictionary name that is employee
831
00:33:04,799 --> 00:33:08,320
followed by the update function
832
00:33:08,320 --> 00:33:12,559
and then within curly braces
833
00:33:12,559 --> 00:33:14,720
i'll pass in
834
00:33:14,720 --> 00:33:18,159
my key that is company
835
00:33:18,159 --> 00:33:20,000
give a colon
836
00:33:20,000 --> 00:33:22,159
and change it to
837
00:33:22,159 --> 00:33:25,480
let's say google
838
00:33:26,000 --> 00:33:27,200
now
839
00:33:27,200 --> 00:33:29,120
i can just
840
00:33:29,120 --> 00:33:31,760
print my
841
00:33:32,240 --> 00:33:33,760
employee dictionary
842
00:33:33,760 --> 00:33:34,880
there you go
843
00:33:34,880 --> 00:33:37,519
so from company oracle we have changed
844
00:33:37,519 --> 00:33:39,679
it to google now
845
00:33:39,679 --> 00:33:41,519
so these are some of the very
846
00:33:41,519 --> 00:33:43,200
crucial features or important features
847
00:33:43,200 --> 00:33:44,960
that are available in python which helps
848
00:33:44,960 --> 00:33:46,000
you do
849
00:33:46,000 --> 00:33:47,519
a lot of calculations and data
850
00:33:47,519 --> 00:33:49,840
manipulations
851
00:33:49,840 --> 00:33:51,200
all right
852
00:33:51,200 --> 00:33:52,720
next
853
00:33:52,720 --> 00:33:55,360
let's see how to use
854
00:33:55,360 --> 00:33:56,799
an if else
855
00:33:56,799 --> 00:33:58,480
condition or any file statement in
856
00:33:58,480 --> 00:34:00,080
python
857
00:34:00,080 --> 00:34:02,159
like in any other programming languages
858
00:34:02,159 --> 00:34:05,039
be it c c plus plus or java you have if
859
00:34:05,039 --> 00:34:06,880
else statements
860
00:34:06,880 --> 00:34:10,159
in python also we have a full statements
861
00:34:10,159 --> 00:34:12,159
so basically you check some condition
862
00:34:12,159 --> 00:34:14,079
and return a value
863
00:34:14,079 --> 00:34:16,399
suppose i have a variable called a which
864
00:34:16,399 --> 00:34:19,599
has a value of let's say 400
865
00:34:19,599 --> 00:34:21,359
and then i have a
866
00:34:21,359 --> 00:34:23,760
variable called b which has a value of
867
00:34:23,760 --> 00:34:26,639
250 okay
868
00:34:26,639 --> 00:34:28,079
now
869
00:34:28,079 --> 00:34:32,320
i'll use if condition i'll write if
870
00:34:32,320 --> 00:34:33,679
b
871
00:34:33,679 --> 00:34:34,879
is
872
00:34:34,879 --> 00:34:37,280
greater than a
873
00:34:37,280 --> 00:34:39,119
then
874
00:34:39,119 --> 00:34:41,520
print
875
00:34:42,159 --> 00:34:43,918
b
876
00:34:43,918 --> 00:34:45,440
is
877
00:34:45,440 --> 00:34:48,440
greater
878
00:34:51,119 --> 00:34:53,599
now you will
879
00:34:53,599 --> 00:34:55,760
also have an else statement i'll write
880
00:34:55,760 --> 00:34:58,240
alif which means else
881
00:34:58,240 --> 00:34:59,440
if
882
00:34:59,440 --> 00:35:03,280
a is equal to equal to b
883
00:35:03,280 --> 00:35:05,040
colon then
884
00:35:05,040 --> 00:35:07,440
print
885
00:35:07,520 --> 00:35:11,839
a and b are equal
886
00:35:13,680 --> 00:35:16,400
finally i'll write my else statement
887
00:35:16,400 --> 00:35:17,520
else
888
00:35:17,520 --> 00:35:19,599
print
889
00:35:19,599 --> 00:35:21,440
a
890
00:35:21,440 --> 00:35:22,480
is
891
00:35:22,480 --> 00:35:24,960
greater
892
00:35:26,160 --> 00:35:28,000
so what i am doing here is i have
893
00:35:28,000 --> 00:35:30,880
assigned two variables a and b to value
894
00:35:30,880 --> 00:35:33,680
400 and 250 respectively then i am
895
00:35:33,680 --> 00:35:35,680
writing an if else condition so i am
896
00:35:35,680 --> 00:35:37,280
checking a condition
897
00:35:37,280 --> 00:35:40,160
if b is greater than a which means i am
898
00:35:40,160 --> 00:35:43,760
comparing if 250 is greater than 400 in
899
00:35:43,760 --> 00:35:47,359
case it is true then print b is greater
900
00:35:47,359 --> 00:35:49,200
else if
901
00:35:49,200 --> 00:35:50,880
both the values if they are same then
902
00:35:50,880 --> 00:35:53,359
print a and b are equal
903
00:35:53,359 --> 00:35:56,000
otherwise print a is greater
904
00:35:56,000 --> 00:35:59,520
let's run it and see the result
905
00:35:59,520 --> 00:36:01,119
this is fair enough
906
00:36:01,119 --> 00:36:04,079
it prints me a is greater because
907
00:36:04,079 --> 00:36:07,280
b is not greater than a so 250 is
908
00:36:07,280 --> 00:36:09,839
definitely not greater than 400
909
00:36:09,839 --> 00:36:13,040
so this
910
00:36:13,040 --> 00:36:15,839
condition fails even the alif condition
911
00:36:15,839 --> 00:36:18,720
fails only the final condition else is
912
00:36:18,720 --> 00:36:22,560
true that is a is greater than b
913
00:36:22,560 --> 00:36:25,040
so this is a simple example
914
00:36:25,040 --> 00:36:27,119
of using the files
915
00:36:27,119 --> 00:36:28,640
condition of the file statement in
916
00:36:28,640 --> 00:36:30,320
python
917
00:36:30,320 --> 00:36:31,760
now moving ahead
918
00:36:31,760 --> 00:36:33,839
let's see how to use a while loop in
919
00:36:33,839 --> 00:36:36,240
python
920
00:36:39,280 --> 00:36:42,000
now using the while loop you can execute
921
00:36:42,000 --> 00:36:43,839
a set of statements as long as a
922
00:36:43,839 --> 00:36:46,720
condition is true
923
00:36:46,800 --> 00:36:48,560
let me show you a very simple example
924
00:36:48,560 --> 00:36:50,720
i'll write
925
00:36:50,720 --> 00:36:55,280
i equal to 0 so i've assigned value of 0
926
00:36:55,280 --> 00:36:56,560
to i
927
00:36:56,560 --> 00:36:58,640
then i'll write while
928
00:36:58,640 --> 00:36:59,920
i
929
00:36:59,920 --> 00:37:02,240
is less than
930
00:37:02,240 --> 00:37:04,320
8
931
00:37:04,320 --> 00:37:05,680
then
932
00:37:05,680 --> 00:37:06,839
i am going
933
00:37:06,839 --> 00:37:08,400
to
934
00:37:08,400 --> 00:37:09,280
add
935
00:37:09,280 --> 00:37:12,240
i so i'll write i equal to
936
00:37:12,240 --> 00:37:14,960
i plus 1
937
00:37:16,000 --> 00:37:19,119
i'll use another condition if
938
00:37:19,119 --> 00:37:20,079
i
939
00:37:20,079 --> 00:37:22,400
equal to equal to
940
00:37:22,400 --> 00:37:24,640
4
941
00:37:24,640 --> 00:37:26,400
give a colon
942
00:37:26,400 --> 00:37:27,920
and then i am going to use a statement
943
00:37:27,920 --> 00:37:30,800
called continue
944
00:37:32,960 --> 00:37:36,320
i'll print i
945
00:37:36,320 --> 00:37:37,440
so
946
00:37:37,440 --> 00:37:39,040
using the continue statement you can
947
00:37:39,040 --> 00:37:41,040
stop the current iteration and continue
948
00:37:41,040 --> 00:37:43,040
with the next iteration
949
00:37:43,040 --> 00:37:45,359
let's print the result and see
950
00:37:45,359 --> 00:37:46,240
okay
951
00:37:46,240 --> 00:37:47,520
so here
952
00:37:47,520 --> 00:37:50,560
we are checking if i is less than 8 so
953
00:37:50,560 --> 00:37:53,520
first value is 0 0 is less than 8 then i
954
00:37:53,520 --> 00:37:56,240
am incrementing the value of i to 1 so i
955
00:37:56,240 --> 00:37:57,359
becomes
956
00:37:57,359 --> 00:38:00,640
1 now so from 0 it becomes 1
957
00:38:00,640 --> 00:38:03,119
then i am printing i
958
00:38:03,119 --> 00:38:05,280
this condition continues
959
00:38:05,280 --> 00:38:10,079
until we have the value of i equal to 4
960
00:38:10,079 --> 00:38:13,119
so if the value of i is equal to 4 i am
961
00:38:13,119 --> 00:38:15,839
skipping the current iteration so if you
962
00:38:15,839 --> 00:38:20,000
notice the result after 3 we have 5 so
963
00:38:20,000 --> 00:38:21,520
we have skipped
964
00:38:21,520 --> 00:38:23,599
printing 4 and
965
00:38:23,599 --> 00:38:27,680
gone to the next value of i which is 5
966
00:38:27,680 --> 00:38:30,400
so the loop runs till this condition
967
00:38:30,400 --> 00:38:31,440
fails
968
00:38:31,440 --> 00:38:32,880
so
969
00:38:32,880 --> 00:38:34,800
when i is
970
00:38:34,800 --> 00:38:36,960
not less than 8
971
00:38:36,960 --> 00:38:39,760
the loop terminates
972
00:38:39,760 --> 00:38:42,160
all right
973
00:38:42,560 --> 00:38:44,720
finally in our demo let's see how a for
974
00:38:44,720 --> 00:38:47,359
loop works so a for loop is used for
975
00:38:47,359 --> 00:38:49,760
iterating over a sequence that is either
976
00:38:49,760 --> 00:38:52,720
a list a tuple a dictionary a set or a
977
00:38:52,720 --> 00:38:54,960
string
978
00:38:54,960 --> 00:38:58,160
so let me go ahead and create my tuple
979
00:38:58,160 --> 00:38:59,599
tup
980
00:38:59,599 --> 00:39:01,760
equal to
981
00:39:01,760 --> 00:39:03,839
let's say i have
982
00:39:03,839 --> 00:39:08,320
the same brand of cars we have bmw
983
00:39:08,320 --> 00:39:10,960
then i have audi
984
00:39:10,960 --> 00:39:13,680
let's see i have
985
00:39:13,680 --> 00:39:16,320
more cities
986
00:39:17,359 --> 00:39:18,880
i have
987
00:39:18,880 --> 00:39:21,440
jaguar
988
00:39:23,040 --> 00:39:24,720
and then
989
00:39:24,720 --> 00:39:28,960
i have bmw again
990
00:39:28,960 --> 00:39:31,359
all right
991
00:39:31,680 --> 00:39:34,400
i'm going to print
992
00:39:34,400 --> 00:39:36,400
the values present in the tuple one by
993
00:39:36,400 --> 00:39:38,960
one so for that i'm going to use a for
994
00:39:38,960 --> 00:39:42,079
loop so i'll write for
995
00:39:42,079 --> 00:39:44,160
a variable x
996
00:39:44,160 --> 00:39:44,880
in
997
00:39:44,880 --> 00:39:47,440
my tuple
998
00:39:47,440 --> 00:39:48,880
i'll say
999
00:39:48,880 --> 00:39:50,880
print x
1000
00:39:50,880 --> 00:39:54,079
so what it will do is
1001
00:39:54,240 --> 00:39:56,800
this randomly assigned variable x will
1002
00:39:56,800 --> 00:39:58,880
iterate through all the elements in the
1003
00:39:58,880 --> 00:40:01,520
tuple and print them one by one
1004
00:40:01,520 --> 00:40:02,800
let's hit
1005
00:40:02,800 --> 00:40:05,280
shift enter to run it there you go you
1006
00:40:05,280 --> 00:40:08,160
can see here i have all the
1007
00:40:08,160 --> 00:40:10,079
different
1008
00:40:10,079 --> 00:40:13,760
car brands printed one by one
1009
00:40:13,760 --> 00:40:14,800
cool
1010
00:40:14,800 --> 00:40:17,359
so this brings us to the end of our demo
1011
00:40:17,359 --> 00:40:19,119
session on introduction to python let me
1012
00:40:19,119 --> 00:40:21,760
go to the top and tell you what we did
1013
00:40:21,760 --> 00:40:25,680
so first i told you how to print any
1014
00:40:25,680 --> 00:40:28,079
set of statements or any string text
1015
00:40:28,079 --> 00:40:30,560
numbers in python you just use the print
1016
00:40:30,560 --> 00:40:32,560
statement or the print function
1017
00:40:32,560 --> 00:40:35,440
you can also perform very
1018
00:40:35,440 --> 00:40:37,920
simple mathematical calculations using
1019
00:40:37,920 --> 00:40:39,200
python
1020
00:40:39,200 --> 00:40:41,440
then you can also check the data type of
1021
00:40:41,440 --> 00:40:44,560
the variable using the type function
1022
00:40:44,560 --> 00:40:46,640
then we saw how to use the input
1023
00:40:46,640 --> 00:40:48,800
function here we inputted an integer and
1024
00:40:48,800 --> 00:40:50,480
then we found the sum of both the
1025
00:40:50,480 --> 00:40:54,560
integers x and y the value was 90.
1026
00:40:54,560 --> 00:40:56,560
in the next example we removed the
1027
00:40:56,560 --> 00:40:58,560
integer function by considering our
1028
00:40:58,560 --> 00:41:01,359
values as strings and then we saw the
1029
00:41:01,359 --> 00:41:03,119
difference in the results so when you
1030
00:41:03,119 --> 00:41:04,720
use strings
1031
00:41:04,720 --> 00:41:06,880
the addition operation will concatenate
1032
00:41:06,880 --> 00:41:10,480
or merge both the strings or the numbers
1033
00:41:10,480 --> 00:41:12,480
then we saw the different data types we
1034
00:41:12,480 --> 00:41:13,520
saw
1035
00:41:13,520 --> 00:41:15,520
about integer data types about floating
1036
00:41:15,520 --> 00:41:19,880
data types boolean data types
1037
00:41:20,480 --> 00:41:23,520
if i scroll down we saw
1038
00:41:23,520 --> 00:41:24,800
the important
1039
00:41:24,800 --> 00:41:27,599
python objects like list
1040
00:41:27,599 --> 00:41:28,960
then we had
1041
00:41:28,960 --> 00:41:31,119
tuple then we saw how to write a
1042
00:41:31,119 --> 00:41:34,640
dictionary how to create a set then we
1043
00:41:34,640 --> 00:41:36,319
saw the difference between
1044
00:41:36,319 --> 00:41:38,880
list and tuples and even
1045
00:41:38,880 --> 00:41:41,040
how set can take only
1046
00:41:41,040 --> 00:41:43,040
unique values
1047
00:41:43,040 --> 00:41:44,720
then we used some
1048
00:41:44,720 --> 00:41:48,000
indexing operations to extract specific
1049
00:41:48,000 --> 00:41:50,640
values from lists tuples dictionaries
1050
00:41:50,640 --> 00:41:52,720
we also
1051
00:41:52,720 --> 00:41:55,280
learned some of the important functions
1052
00:41:55,280 --> 00:41:56,960
these are all inbuilt functions that
1053
00:41:56,960 --> 00:41:58,400
python gives us
1054
00:41:58,400 --> 00:42:00,640
to perform manipulations and operations
1055
00:42:00,640 --> 00:42:01,760
in python
1056
00:42:01,760 --> 00:42:04,319
so you used append then we saw how to
1057
00:42:04,319 --> 00:42:06,000
use reverse
1058
00:42:06,000 --> 00:42:07,760
now let me just go ahead and show you
1059
00:42:07,760 --> 00:42:08,960
another
1060
00:42:08,960 --> 00:42:10,880
function an inbuilt function in python
1061
00:42:10,880 --> 00:42:12,560
i'll just insert a
1062
00:42:12,560 --> 00:42:15,280
cell below this
1063
00:42:15,280 --> 00:42:17,200
suppose you want to find the length of
1064
00:42:17,200 --> 00:42:18,319
your list
1065
00:42:18,319 --> 00:42:20,480
you can use the length function or len
1066
00:42:20,480 --> 00:42:21,680
function that is
1067
00:42:21,680 --> 00:42:24,880
inbuilt in python i'll just pass in
1068
00:42:24,880 --> 00:42:27,200
ln followed by
1069
00:42:27,200 --> 00:42:28,000
my
1070
00:42:28,000 --> 00:42:29,680
list that is l1
1071
00:42:29,680 --> 00:42:31,440
if i run this
1072
00:42:31,440 --> 00:42:33,680
it gives me the result 5 because you can
1073
00:42:33,680 --> 00:42:35,119
see it here
1074
00:42:35,119 --> 00:42:37,359
my list has 5 elements in it you can
1075
00:42:37,359 --> 00:42:40,160
count it so the length becomes 5.
1076
00:42:40,160 --> 00:42:42,720
similarly you can also
1077
00:42:42,720 --> 00:42:44,880
check the
1078
00:42:44,880 --> 00:42:46,960
length of the tuple
1079
00:42:46,960 --> 00:42:50,160
let me insert a cell here as well
1080
00:42:50,160 --> 00:42:53,280
suppose i write lin
1081
00:42:53,280 --> 00:42:55,920
of my tuple that is tup if i run it
1082
00:42:55,920 --> 00:42:56,880
again
1083
00:42:56,880 --> 00:42:58,480
it says 5
1084
00:42:58,480 --> 00:43:00,480
now this was the earlier tuple that we
1085
00:43:00,480 --> 00:43:01,839
had created
1086
00:43:01,839 --> 00:43:05,119
later we actually created
1087
00:43:05,119 --> 00:43:06,560
another tuple
1088
00:43:06,560 --> 00:43:07,920
let me show you
1089
00:43:07,920 --> 00:43:10,079
okay so this was the tuple it has
1090
00:43:10,079 --> 00:43:12,160
taken this new tuple which has five
1091
00:43:12,160 --> 00:43:15,200
values in it so the length becomes five
1092
00:43:15,200 --> 00:43:18,240
let me go to the top
1093
00:43:18,319 --> 00:43:20,560
okay and after that we learned how to
1094
00:43:20,560 --> 00:43:22,240
use if else
1095
00:43:22,240 --> 00:43:24,640
followed by how to write a while loop
1096
00:43:24,640 --> 00:43:26,800
and then finally we saw how to
1097
00:43:26,800 --> 00:43:28,880
use a for loop in python
1098
00:43:28,880 --> 00:43:30,800
in this session we will learn how to
1099
00:43:30,800 --> 00:43:34,560
install python and install numpy scipy
1100
00:43:34,560 --> 00:43:37,760
matplotlib and pandas libraries then we
1101
00:43:37,760 --> 00:43:40,160
will install the anaconda distribution
1102
00:43:40,160 --> 00:43:41,520
in our system
1103
00:43:41,520 --> 00:43:42,960
so let's start
1104
00:43:42,960 --> 00:43:45,760
as you all know python is an interpreted
1105
00:43:45,760 --> 00:43:48,480
language so you need to install a python
1106
00:43:48,480 --> 00:43:51,280
interpreter so for that we will go to
1107
00:43:51,280 --> 00:43:52,640
the browser
1108
00:43:52,640 --> 00:43:58,079
and search for python dot org
1109
00:43:59,359 --> 00:44:02,800
now we will move to the downloads page
1110
00:44:02,800 --> 00:44:04,880
and choose the latest version of python
1111
00:44:04,880 --> 00:44:06,880
to download and the latest version is
1112
00:44:06,880 --> 00:44:09,920
python 3.9.5
1113
00:44:09,920 --> 00:44:13,119
we'll click on the download
1114
00:44:13,440 --> 00:44:16,000
once the installer has downloaded open
1115
00:44:16,000 --> 00:44:18,160
it
1116
00:44:19,440 --> 00:44:21,680
now you can see the installation window
1117
00:44:21,680 --> 00:44:23,440
for python
1118
00:44:23,440 --> 00:44:25,599
you have to double check both the boxes
1119
00:44:25,599 --> 00:44:28,400
add python 3.9 to path
1120
00:44:28,400 --> 00:44:31,839
and click on install now
1121
00:44:32,319 --> 00:44:34,400
as the installation completes you can
1122
00:44:34,400 --> 00:44:36,480
see a screen that says the setup was
1123
00:44:36,480 --> 00:44:40,400
successful now click on close
1124
00:44:41,040 --> 00:44:43,200
now to check whether the python has been
1125
00:44:43,200 --> 00:44:45,280
installed successfully on the system or
1126
00:44:45,280 --> 00:44:47,680
not we will run a command on the command
1127
00:44:47,680 --> 00:44:50,400
prompt let's open the command prompt
1128
00:44:50,400 --> 00:44:51,839
click on start
1129
00:44:51,839 --> 00:44:55,119
and write cmd
1130
00:44:55,520 --> 00:44:57,200
now to check whether the python is
1131
00:44:57,200 --> 00:44:59,760
installed or not we will run the command
1132
00:44:59,760 --> 00:45:03,960
python minus minus version
1133
00:45:06,960 --> 00:45:09,839
python space minus minus
1134
00:45:09,839 --> 00:45:10,880
version
1135
00:45:10,880 --> 00:45:13,839
and press enter so here we can see that
1136
00:45:13,839 --> 00:45:16,560
python 3.9.5 version is installed in the
1137
00:45:16,560 --> 00:45:19,280
system so yeah python is working in the
1138
00:45:19,280 --> 00:45:22,000
system now we'll run some commands for
1139
00:45:22,000 --> 00:45:23,119
python
1140
00:45:23,119 --> 00:45:25,920
so for that first we will type py
1141
00:45:25,920 --> 00:45:27,760
and press enter
1142
00:45:27,760 --> 00:45:31,680
and now we will write the basic command
1143
00:45:31,680 --> 00:45:33,599
that is
1144
00:45:33,599 --> 00:45:35,119
print
1145
00:45:35,119 --> 00:45:37,760
bracket
1146
00:45:40,000 --> 00:45:42,960
hello world
1147
00:45:43,680 --> 00:45:46,560
semicolon and press enter
1148
00:45:46,560 --> 00:45:48,640
as you can see hello world is printed
1149
00:45:48,640 --> 00:45:51,440
here so yeah python has started working
1150
00:45:51,440 --> 00:45:52,960
in our system
1151
00:45:52,960 --> 00:45:55,920
now we will install libraries of python
1152
00:45:55,920 --> 00:45:58,800
we will be using pip to install numpy
1153
00:45:58,800 --> 00:46:02,319
sci-fi matplotlib and pandas library and
1154
00:46:02,319 --> 00:46:04,400
before installing the libraries make
1155
00:46:04,400 --> 00:46:06,480
sure the latest version of pip is
1156
00:46:06,480 --> 00:46:08,960
installed in your system so to check
1157
00:46:08,960 --> 00:46:11,119
that we will run a command in command
1158
00:46:11,119 --> 00:46:12,400
prompt
1159
00:46:12,400 --> 00:46:14,720
we will open the command prompt
1160
00:46:14,720 --> 00:46:16,640
and write a command
1161
00:46:16,640 --> 00:46:20,800
pip space minus minus version
1162
00:46:20,800 --> 00:46:22,720
and press enter
1163
00:46:22,720 --> 00:46:25,319
as you can see the pip version is
1164
00:46:25,319 --> 00:46:27,920
21.1.1 and it's being downloaded with
1165
00:46:27,920 --> 00:46:30,640
the python 3.9 so it's the latest
1166
00:46:30,640 --> 00:46:32,640
version we have downloaded
1167
00:46:32,640 --> 00:46:33,920
so now
1168
00:46:33,920 --> 00:46:36,240
we have to keep one thing in mind before
1169
00:46:36,240 --> 00:46:38,800
installing scipy matte protlib and
1170
00:46:38,800 --> 00:46:41,680
pandas library first we need to install
1171
00:46:41,680 --> 00:46:43,359
numpy library
1172
00:46:43,359 --> 00:46:47,440
so for that we will run a command
1173
00:46:47,440 --> 00:46:48,319
pip
1174
00:46:48,319 --> 00:46:49,680
space
1175
00:46:49,680 --> 00:46:52,160
install
1176
00:46:52,319 --> 00:46:53,440
numpy
1177
00:46:53,440 --> 00:46:56,160
and press enter
1178
00:46:56,160 --> 00:46:58,160
depending on your internet speed it will
1179
00:46:58,160 --> 00:47:00,960
get installed
1180
00:47:02,480 --> 00:47:04,880
as you can see the numpy library has
1181
00:47:04,880 --> 00:47:08,160
been installed and there's a warning
1182
00:47:08,160 --> 00:47:11,280
that our pip version is 21.1.1
1183
00:47:11,280 --> 00:47:14,480
and we should upgrade it to 21.1.2
1184
00:47:14,480 --> 00:47:17,440
that is the latest version available so
1185
00:47:17,440 --> 00:47:19,839
we'll upgrade the pip version for that
1186
00:47:19,839 --> 00:47:21,440
we will write a command
1187
00:47:21,440 --> 00:47:22,880
python
1188
00:47:22,880 --> 00:47:26,000
space minus m space
1189
00:47:26,000 --> 00:47:27,839
pip space
1190
00:47:27,839 --> 00:47:31,680
install space minus
1191
00:47:31,680 --> 00:47:33,839
upgrade space
1192
00:47:33,839 --> 00:47:34,960
pip
1193
00:47:34,960 --> 00:47:36,640
enter
1194
00:47:36,640 --> 00:47:38,880
okay so we made a mistake here in the
1195
00:47:38,880 --> 00:47:41,280
command so the mistake is we have
1196
00:47:41,280 --> 00:47:43,119
written only one minus sign before the
1197
00:47:43,119 --> 00:47:45,680
upgrade we have to write two minus signs
1198
00:47:45,680 --> 00:47:47,119
before the upgrade
1199
00:47:47,119 --> 00:47:48,960
so we'll write the command again
1200
00:47:48,960 --> 00:47:50,559
python
1201
00:47:50,559 --> 00:47:52,480
space minus m
1202
00:47:52,480 --> 00:47:53,440
space
1203
00:47:53,440 --> 00:47:55,280
pip space
1204
00:47:55,280 --> 00:48:00,559
install space minus minus upgrade
1205
00:48:00,559 --> 00:48:01,520
space
1206
00:48:01,520 --> 00:48:04,079
pip and then press enter
1207
00:48:04,079 --> 00:48:06,800
and now depending on your internet speed
1208
00:48:06,800 --> 00:48:09,359
the pip version the latest version will
1209
00:48:09,359 --> 00:48:12,559
be installed on your system
1210
00:48:14,800 --> 00:48:17,200
as you can see they have uninstalled the
1211
00:48:17,200 --> 00:48:21,119
pep version 21.1 and they have installed
1212
00:48:21,119 --> 00:48:24,880
the latest version that is 21.1.2
1213
00:48:24,880 --> 00:48:26,800
we have installed the first library that
1214
00:48:26,800 --> 00:48:28,559
is numpy and that's the basic
1215
00:48:28,559 --> 00:48:30,000
requirement
1216
00:48:30,000 --> 00:48:32,640
for the installation of scipy library so
1217
00:48:32,640 --> 00:48:35,599
now we will install the scifi library
1218
00:48:35,599 --> 00:48:37,440
so for that we will write the command
1219
00:48:37,440 --> 00:48:40,400
pip install scipy
1220
00:48:40,400 --> 00:48:42,240
pip space
1221
00:48:42,240 --> 00:48:44,000
install
1222
00:48:44,000 --> 00:48:47,760
space sci-fi and press enter
1223
00:48:47,760 --> 00:48:49,599
and depending on your internet speed it
1224
00:48:49,599 --> 00:48:52,559
will be downloaded
1225
00:48:52,880 --> 00:48:55,280
now you can see that the scipy library
1226
00:48:55,280 --> 00:48:57,359
has been installed successfully and here
1227
00:48:57,359 --> 00:48:58,480
you can see
1228
00:48:58,480 --> 00:49:00,839
that the requirement already satisfied
1229
00:49:00,839 --> 00:49:03,520
numpy so we have firstly downloaded the
1230
00:49:03,520 --> 00:49:06,240
numpy library so that we can easily run
1231
00:49:06,240 --> 00:49:07,839
the scipy library
1232
00:49:07,839 --> 00:49:10,079
now we will download and install pandas
1233
00:49:10,079 --> 00:49:11,760
library for that we will write the
1234
00:49:11,760 --> 00:49:13,680
command
1235
00:49:13,680 --> 00:49:17,280
pip space install
1236
00:49:17,280 --> 00:49:18,960
pandas
1237
00:49:18,960 --> 00:49:20,640
and press enter
1238
00:49:20,640 --> 00:49:22,240
and this will also
1239
00:49:22,240 --> 00:49:26,240
install depending on your internet speed
1240
00:49:26,240 --> 00:49:28,960
we can see that the pandas library has
1241
00:49:28,960 --> 00:49:31,280
been successfully installed now we are
1242
00:49:31,280 --> 00:49:34,000
left with matplot lib library so now we
1243
00:49:34,000 --> 00:49:36,160
will install that for that we will write
1244
00:49:36,160 --> 00:49:37,680
the command
1245
00:49:37,680 --> 00:49:39,359
pip space
1246
00:49:39,359 --> 00:49:41,760
install
1247
00:49:41,760 --> 00:49:43,119
mat
1248
00:49:43,119 --> 00:49:47,599
load lift and press enter
1249
00:49:54,160 --> 00:49:57,359
as we can see the matplotlib library has
1250
00:49:57,359 --> 00:50:00,079
been successfully installed
1251
00:50:00,079 --> 00:50:01,760
now we have installed all the four
1252
00:50:01,760 --> 00:50:03,119
libraries
1253
00:50:03,119 --> 00:50:04,800
so if you want to check whether the
1254
00:50:04,800 --> 00:50:07,040
library is installed on your system or
1255
00:50:07,040 --> 00:50:08,160
not
1256
00:50:08,160 --> 00:50:10,319
you can write a command to check that
1257
00:50:10,319 --> 00:50:12,480
first you have to write python and press
1258
00:50:12,480 --> 00:50:13,520
enter
1259
00:50:13,520 --> 00:50:15,680
first we will check for numpy library
1260
00:50:15,680 --> 00:50:18,880
and for that we will create a array
1261
00:50:18,880 --> 00:50:21,200
for that we will write the command
1262
00:50:21,200 --> 00:50:23,839
import
1263
00:50:24,839 --> 00:50:26,559
numpy
1264
00:50:26,559 --> 00:50:29,920
s and p and press enter
1265
00:50:29,920 --> 00:50:32,079
if you don't get any error it means that
1266
00:50:32,079 --> 00:50:33,680
the library has been installed
1267
00:50:33,680 --> 00:50:35,920
successfully so now we will create an
1268
00:50:35,920 --> 00:50:38,240
array
1269
00:50:38,800 --> 00:50:40,400
a equal to
1270
00:50:40,400 --> 00:50:42,480
np dot
1271
00:50:42,480 --> 00:50:44,960
array
1272
00:50:50,880 --> 00:50:55,119
1 comma 2 comma 4
1273
00:50:55,119 --> 00:50:57,520
comma
1274
00:50:57,920 --> 00:51:00,800
5 comma 7 comma
1275
00:51:00,800 --> 00:51:03,119
9
1276
00:51:04,960 --> 00:51:06,480
comma
1277
00:51:06,480 --> 00:51:09,800
d type
1278
00:51:11,839 --> 00:51:12,960
equal to
1279
00:51:12,960 --> 00:51:16,960
we'll set it for float
1280
00:51:19,359 --> 00:51:22,800
press enter and we want to print this
1281
00:51:22,800 --> 00:51:24,240
array
1282
00:51:24,240 --> 00:51:27,200
like print
1283
00:51:28,960 --> 00:51:30,480
array
1284
00:51:30,480 --> 00:51:33,480
created
1285
00:51:40,720 --> 00:51:42,000
a
1286
00:51:42,000 --> 00:51:43,680
and press enter
1287
00:51:43,680 --> 00:51:46,160
you can see the output error created one
1288
00:51:46,160 --> 00:51:47,359
two four
1289
00:51:47,359 --> 00:51:49,119
and five seven nine
1290
00:51:49,119 --> 00:51:53,280
so the numpy library is working fine
1291
00:51:53,280 --> 00:51:56,000
so we will check for other libraries for
1292
00:51:56,000 --> 00:51:57,680
that
1293
00:51:57,680 --> 00:51:59,200
we will write
1294
00:51:59,200 --> 00:52:01,280
import
1295
00:52:01,280 --> 00:52:03,440
space
1296
00:52:03,440 --> 00:52:05,520
mat port live
1297
00:52:05,520 --> 00:52:07,760
and press enter
1298
00:52:07,760 --> 00:52:10,160
if you don't get any error it means that
1299
00:52:10,160 --> 00:52:11,680
the library has been installed
1300
00:52:11,680 --> 00:52:13,440
successfully
1301
00:52:13,440 --> 00:52:16,240
so for the rest also we can
1302
00:52:16,240 --> 00:52:19,599
write the same commands import
1303
00:52:19,599 --> 00:52:21,920
pandas and press enter
1304
00:52:21,920 --> 00:52:24,240
we do not get any error so the library
1305
00:52:24,240 --> 00:52:26,480
has been installed successfully and we
1306
00:52:26,480 --> 00:52:28,880
are left with sci-fi so we'll write
1307
00:52:28,880 --> 00:52:30,160
import
1308
00:52:30,160 --> 00:52:33,520
space sci-fi and press enter
1309
00:52:33,520 --> 00:52:35,760
so here also we didn't get any error so
1310
00:52:35,760 --> 00:52:37,599
the library has also been installed
1311
00:52:37,599 --> 00:52:39,440
successfully
1312
00:52:39,440 --> 00:52:43,040
now we will see what anaconda is
1313
00:52:43,040 --> 00:52:44,800
anagona is a free and open source
1314
00:52:44,800 --> 00:52:47,760
distribution of python and our language
1315
00:52:47,760 --> 00:52:50,240
here we have conda which is a package
1316
00:52:50,240 --> 00:52:54,240
manager system similar to papen python
1317
00:52:54,240 --> 00:52:56,160
so now we will see how to download
1318
00:52:56,160 --> 00:52:58,640
anaconda and then we will explore its
1319
00:52:58,640 --> 00:53:01,200
features
1320
00:53:01,200 --> 00:53:03,440
to download anaconda first we will open
1321
00:53:03,440 --> 00:53:06,000
the browser
1322
00:53:06,319 --> 00:53:10,680
and search for the keyword anaconda
1323
00:53:15,680 --> 00:53:17,200
we have to visit the official website
1324
00:53:17,200 --> 00:53:20,599
that is www.anaconda.com
1325
00:53:24,000 --> 00:53:25,760
and now we will click on the products
1326
00:53:25,760 --> 00:53:28,240
section and choose the individual
1327
00:53:28,240 --> 00:53:31,119
edition as it is open source edition and
1328
00:53:31,119 --> 00:53:32,960
it is free of post
1329
00:53:32,960 --> 00:53:36,000
now we will scroll down
1330
00:53:37,359 --> 00:53:39,200
here is the toolkit for the individual
1331
00:53:39,200 --> 00:53:43,720
edition and will click on the download
1332
00:53:44,000 --> 00:53:46,000
on clicking the download we are directed
1333
00:53:46,000 --> 00:53:48,720
to the bottom of the web page and here
1334
00:53:48,720 --> 00:53:50,160
we can see the different versions
1335
00:53:50,160 --> 00:53:52,880
available for windows that is 32 bit and
1336
00:53:52,880 --> 00:53:54,480
64 bit
1337
00:53:54,480 --> 00:53:57,359
we will download the 64 bit and it is of
1338
00:53:57,359 --> 00:54:00,559
size 477 mb so it will take ample amount
1339
00:54:00,559 --> 00:54:02,400
of time to download
1340
00:54:02,400 --> 00:54:05,359
we will download it
1341
00:54:05,839 --> 00:54:08,559
as the download completes click on the
1342
00:54:08,559 --> 00:54:11,839
application file
1343
00:54:13,920 --> 00:54:16,800
now click on next
1344
00:54:16,800 --> 00:54:18,960
the license agreement will appear
1345
00:54:18,960 --> 00:54:22,000
now click on i agree
1346
00:54:22,000 --> 00:54:23,680
then you have to select the installation
1347
00:54:23,680 --> 00:54:24,640
type
1348
00:54:24,640 --> 00:54:26,559
if you want to install for a single user
1349
00:54:26,559 --> 00:54:28,079
click on just me
1350
00:54:28,079 --> 00:54:30,079
i'll select for all users and click on
1351
00:54:30,079 --> 00:54:32,480
next i am installing for me
1352
00:54:32,480 --> 00:54:34,000
so i'll click
1353
00:54:34,000 --> 00:54:35,440
next
1354
00:54:35,440 --> 00:54:37,040
then it will ask for installation
1355
00:54:37,040 --> 00:54:38,640
location
1356
00:54:38,640 --> 00:54:40,400
after selecting location click on the
1357
00:54:40,400 --> 00:54:41,520
next
1358
00:54:41,520 --> 00:54:44,160
my for me the location is fine so i will
1359
00:54:44,160 --> 00:54:46,720
click next
1360
00:54:46,960 --> 00:54:48,799
then advanced installation options will
1361
00:54:48,799 --> 00:54:50,640
appear if you have already installed
1362
00:54:50,640 --> 00:54:52,559
python on your system as we have
1363
00:54:52,559 --> 00:54:55,359
installed already so we do not have to
1364
00:54:55,359 --> 00:54:57,520
click on this section
1365
00:54:57,520 --> 00:54:59,280
we will only click on this section and
1366
00:54:59,280 --> 00:55:02,000
press install
1367
00:55:05,760 --> 00:55:09,040
it will take time to install
1368
00:55:10,799 --> 00:55:12,799
after the installation is complete click
1369
00:55:12,799 --> 00:55:15,280
on next
1370
00:55:16,720 --> 00:55:18,640
then click on next
1371
00:55:18,640 --> 00:55:21,760
and uncheck both these options
1372
00:55:21,760 --> 00:55:24,799
and click on finish
1373
00:55:25,119 --> 00:55:26,880
as you can see the installation has been
1374
00:55:26,880 --> 00:55:28,480
completed
1375
00:55:28,480 --> 00:55:29,520
now
1376
00:55:29,520 --> 00:55:34,680
we'll go to start and type anaconda
1377
00:55:36,160 --> 00:55:38,240
we can see that
1378
00:55:38,240 --> 00:55:40,960
anaconda prompt and anaconda navigator
1379
00:55:40,960 --> 00:55:42,400
are visible
1380
00:55:42,400 --> 00:55:46,920
first we will open anaconda navigator
1381
00:55:49,119 --> 00:55:53,040
it's initializing and adding features
1382
00:55:53,040 --> 00:55:54,960
here you can see some pre-installed
1383
00:55:54,960 --> 00:55:56,319
applications
1384
00:55:56,319 --> 00:55:58,480
and tools that are already installed in
1385
00:55:58,480 --> 00:55:59,839
the anaconda
1386
00:55:59,839 --> 00:56:01,599
and if you want to install any other
1387
00:56:01,599 --> 00:56:04,319
tool you can
1388
00:56:05,440 --> 00:56:07,920
like you can install our studio python
1389
00:56:07,920 --> 00:56:11,280
professional orange 3 blue is
1390
00:56:11,280 --> 00:56:13,040
in the anaconda
1391
00:56:13,040 --> 00:56:15,839
and you already have spider qd console
1392
00:56:15,839 --> 00:56:20,160
powershell prompt jupiter notebook
1393
00:56:20,160 --> 00:56:23,200
jupyter lab ibm watson studio cloud
1394
00:56:23,200 --> 00:56:26,400
datalore cmd.exe prompt
1395
00:56:26,400 --> 00:56:31,799
so now we will open an upunder prompt
1396
00:56:37,680 --> 00:56:39,520
now let's see which version of conda is
1397
00:56:39,520 --> 00:56:41,839
installed in the system for that we'll
1398
00:56:41,839 --> 00:56:45,520
write a command conda space info
1399
00:56:45,520 --> 00:56:48,000
and press enter
1400
00:56:48,000 --> 00:56:50,000
here we can see that the corner version
1401
00:56:50,000 --> 00:56:52,400
is 4.10.1
1402
00:56:52,400 --> 00:56:54,799
now we will launch jupyter notebook for
1403
00:56:54,799 --> 00:56:57,200
that we will write the command
1404
00:56:57,200 --> 00:57:00,160
jupyter notebook
1405
00:57:02,799 --> 00:57:06,359
and press enter
1406
00:57:13,200 --> 00:57:15,040
you can see that the jupiter notebook
1407
00:57:15,040 --> 00:57:16,559
has initiated
1408
00:57:16,559 --> 00:57:18,400
now create a folder where you want to
1409
00:57:18,400 --> 00:57:20,640
store the projects and rename
1410
00:57:20,640 --> 00:57:22,319
accordingly
1411
00:57:22,319 --> 00:57:24,480
so to create folder
1412
00:57:24,480 --> 00:57:27,200
will create the folder in onedrive
1413
00:57:27,200 --> 00:57:29,680
sorry
1414
00:57:30,160 --> 00:57:33,920
i will create the folder on desktop
1415
00:57:35,440 --> 00:57:37,200
new
1416
00:57:37,200 --> 00:57:40,000
folder
1417
00:57:40,000 --> 00:57:42,480
we will click the untitled folder
1418
00:57:42,480 --> 00:57:44,880
and rename it
1419
00:57:44,880 --> 00:57:46,559
and rename it as
1420
00:57:46,559 --> 00:57:48,160
python
1421
00:57:48,160 --> 00:57:50,480
1
1422
00:57:52,000 --> 00:57:55,040
now minimize the window
1423
00:57:55,040 --> 00:57:56,319
and
1424
00:57:56,319 --> 00:57:59,359
open the desktop
1425
00:57:59,520 --> 00:58:01,200
you can see that the python one
1426
00:58:01,200 --> 00:58:02,960
directory is created
1427
00:58:02,960 --> 00:58:05,200
and now we will click on the python one
1428
00:58:05,200 --> 00:58:06,839
folder
1429
00:58:06,839 --> 00:58:11,760
and create a python 3 notebook
1430
00:58:15,200 --> 00:58:17,359
as we have created a python 3 file in
1431
00:58:17,359 --> 00:58:19,760
the jupyter notebook now we can run the
1432
00:58:19,760 --> 00:58:22,799
python commands on it so we will run
1433
00:58:22,799 --> 00:58:24,000
some commands
1434
00:58:24,000 --> 00:58:26,319
and first we will run a basic command
1435
00:58:26,319 --> 00:58:28,559
that would be print
1436
00:58:28,559 --> 00:58:31,040
print
1437
00:58:32,079 --> 00:58:33,760
india
1438
00:58:33,760 --> 00:58:36,079
and we will run this command media is
1439
00:58:36,079 --> 00:58:37,280
printed here
1440
00:58:37,280 --> 00:58:38,960
i'm going to run some
1441
00:58:38,960 --> 00:58:41,520
operators
1442
00:58:41,520 --> 00:58:43,839
like we will
1443
00:58:43,839 --> 00:58:46,160
print
1444
00:58:46,160 --> 00:58:48,559
7
1445
00:58:48,839 --> 00:58:52,240
into eight
1446
00:58:52,240 --> 00:58:53,119
and
1447
00:58:53,119 --> 00:58:55,760
the run so it's 56
1448
00:58:55,760 --> 00:58:58,000
and we'll also run an exponential
1449
00:58:58,000 --> 00:59:00,400
operator
1450
00:59:00,400 --> 00:59:02,720
print
1451
00:59:02,720 --> 00:59:07,079
to 6.4
1452
00:59:08,640 --> 00:59:10,079
so you can see
1453
00:59:10,079 --> 00:59:12,640
16 the result comes out
1454
00:59:12,640 --> 00:59:14,880
so now to save this file first we'll
1455
00:59:14,880 --> 00:59:17,359
rename it
1456
00:59:17,359 --> 00:59:19,680
demo one
1457
00:59:19,680 --> 00:59:21,680
rename
1458
00:59:21,680 --> 00:59:23,760
and we will save it
1459
00:59:23,760 --> 00:59:26,079
save and checkpoint
1460
00:59:26,079 --> 00:59:27,839
now you can check in the directory you
1461
00:59:27,839 --> 00:59:31,119
have created python one
1462
00:59:31,119 --> 00:59:34,240
that it is being saved as demo1 in this
1463
00:59:34,240 --> 00:59:36,640
session we will learn to write a program
1464
00:59:36,640 --> 00:59:39,200
in python ideally then we will see a
1465
00:59:39,200 --> 00:59:41,280
program to add two numbers and
1466
00:59:41,280 --> 00:59:43,520
afterwards we will run a program in
1467
00:59:43,520 --> 00:59:45,200
jupyter notebook
1468
00:59:45,200 --> 00:59:47,440
as you all know python is an interpreted
1469
00:59:47,440 --> 00:59:49,839
language so we require a python
1470
00:59:49,839 --> 00:59:51,760
interpreter to work upon while
1471
00:59:51,760 --> 00:59:54,079
installing python in our system we also
1472
00:59:54,079 --> 00:59:57,040
get python idi automatically installed
1473
00:59:57,040 --> 00:59:59,280
but before writing a program in python
1474
00:59:59,280 --> 01:00:01,280
ideally we will write a program on
1475
01:00:01,280 --> 01:00:03,520
command prompt
1476
01:00:03,520 --> 01:00:05,599
so for that first we will open command
1477
01:00:05,599 --> 01:00:06,720
prompt
1478
01:00:06,720 --> 01:00:09,839
we will click on start and write cmd
1479
01:00:09,839 --> 01:00:12,079
and click enter
1480
01:00:12,079 --> 01:00:15,760
now the command prompt appears
1481
01:00:15,760 --> 01:00:18,559
first we will check the python version
1482
01:00:18,559 --> 01:00:22,559
pip space minus minus version
1483
01:00:22,559 --> 01:00:25,520
and press enter
1484
01:00:26,400 --> 01:00:29,760
you will see the python version 3.9
1485
01:00:29,760 --> 01:00:30,720
now
1486
01:00:30,720 --> 01:00:33,359
we will start writing the python program
1487
01:00:33,359 --> 01:00:35,520
for that first we have to write the
1488
01:00:35,520 --> 01:00:38,559
command p y and press enter now we can
1489
01:00:38,559 --> 01:00:40,319
start writing our program
1490
01:00:40,319 --> 01:00:41,839
we will write a basic program on
1491
01:00:41,839 --> 01:00:43,760
division
1492
01:00:43,760 --> 01:00:46,640
for that we will assign a value
1493
01:00:46,640 --> 01:00:48,240
5
1494
01:00:48,240 --> 01:00:50,559
to a variable a
1495
01:00:50,559 --> 01:00:52,000
and
1496
01:00:52,000 --> 01:00:56,240
value 10 to the variable b
1497
01:00:56,240 --> 01:00:58,480
and we will divide b
1498
01:00:58,480 --> 01:01:00,000
by a
1499
01:01:00,000 --> 01:01:03,440
and we'll print the value
1500
01:01:03,599 --> 01:01:05,359
of c
1501
01:01:05,359 --> 01:01:07,359
press enter
1502
01:01:07,359 --> 01:01:11,280
as you can see we have got an output 2.0
1503
01:01:11,280 --> 01:01:13,280
now we have run a program on command
1504
01:01:13,280 --> 01:01:15,280
prompt successfully now i will write a
1505
01:01:15,280 --> 01:01:17,440
program in python ideally
1506
01:01:17,440 --> 01:01:19,839
so to write a program in python idli we
1507
01:01:19,839 --> 01:01:22,640
will see some steps
1508
01:01:22,640 --> 01:01:25,040
first we have to open the python ideally
1509
01:01:25,040 --> 01:01:27,280
then click on file menu and choose the
1510
01:01:27,280 --> 01:01:28,559
new file
1511
01:01:28,559 --> 01:01:29,359
and
1512
01:01:29,359 --> 01:01:31,040
after writing the program we have to
1513
01:01:31,040 --> 01:01:34,160
save the program with dot py extension
1514
01:01:34,160 --> 01:01:36,720
and then we can click on the run menu
1515
01:01:36,720 --> 01:01:39,440
and then we will see a run module and we
1516
01:01:39,440 --> 01:01:41,200
have to click on that
1517
01:01:41,200 --> 01:01:43,440
now we'll see it practically for that we
1518
01:01:43,440 --> 01:01:46,160
will click on start
1519
01:01:46,160 --> 01:01:49,680
and search for id ali
1520
01:01:50,000 --> 01:01:54,240
and now we'll click on python ideally
1521
01:01:55,599 --> 01:01:57,599
now we will follow the steps as we have
1522
01:01:57,599 --> 01:02:00,079
seen now
1523
01:02:00,079 --> 01:02:02,160
we will click on file menu and choose
1524
01:02:02,160 --> 01:02:04,799
new file a new window will appear
1525
01:02:04,799 --> 01:02:07,599
here we will type the program to get two
1526
01:02:07,599 --> 01:02:10,480
numbers inserted by user and add them
1527
01:02:10,480 --> 01:02:12,559
but before writing this program let's
1528
01:02:12,559 --> 01:02:15,280
understand this program
1529
01:02:15,280 --> 01:02:18,000
so python program on adding two numbers
1530
01:02:18,000 --> 01:02:19,920
first we have to get an int value
1531
01:02:19,920 --> 01:02:22,160
inserted in variable a for that we will
1532
01:02:22,160 --> 01:02:24,480
write the command a equal to
1533
01:02:24,480 --> 01:02:25,599
int
1534
01:02:25,599 --> 01:02:28,160
round brackets input round bracket
1535
01:02:28,160 --> 01:02:30,319
started for the message we have to put
1536
01:02:30,319 --> 01:02:32,640
it in inverted commas and the first
1537
01:02:32,640 --> 01:02:35,359
number colon round bracket close and
1538
01:02:35,359 --> 01:02:37,440
another round bracket close
1539
01:02:37,440 --> 01:02:39,760
now we will insert an end value in
1540
01:02:39,760 --> 01:02:41,039
variable b
1541
01:02:41,039 --> 01:02:43,200
for that we will write the command b
1542
01:02:43,200 --> 01:02:46,480
equal to int round bracket started input
1543
01:02:46,480 --> 01:02:48,400
around bracket started for the message
1544
01:02:48,400 --> 01:02:50,319
we will put the message in inverted
1545
01:02:50,319 --> 01:02:53,680
commas and the second number colon
1546
01:02:53,680 --> 01:02:56,240
and then we will close this message
1547
01:02:56,240 --> 01:02:58,480
by round bracket and another round
1548
01:02:58,480 --> 01:03:00,400
bracket closing
1549
01:03:00,400 --> 01:03:02,960
then we will assign a variable c
1550
01:03:02,960 --> 01:03:06,640
and add the operation a plus b for that
1551
01:03:06,640 --> 01:03:08,480
now we'll print
1552
01:03:08,480 --> 01:03:10,480
a message that would be addition of two
1553
01:03:10,480 --> 01:03:12,559
numbers equal to that would be an
1554
01:03:12,559 --> 01:03:14,160
inverted commas
1555
01:03:14,160 --> 01:03:15,599
comma c
1556
01:03:15,599 --> 01:03:18,480
as c variable will store the result of a
1557
01:03:18,480 --> 01:03:20,720
plus b
1558
01:03:20,720 --> 01:03:22,720
all the statements in the program will
1559
01:03:22,720 --> 01:03:25,359
get executed line mainly now we will
1560
01:03:25,359 --> 01:03:28,960
execute this program on python ideally
1561
01:03:28,960 --> 01:03:31,359
so for that we will write our program
1562
01:03:31,359 --> 01:03:32,480
here
1563
01:03:32,480 --> 01:03:35,359
that would be a equal to
1564
01:03:35,359 --> 01:03:36,559
and
1565
01:03:36,559 --> 01:03:38,480
round bracket start
1566
01:03:38,480 --> 01:03:40,160
input
1567
01:03:40,160 --> 01:03:42,319
round bracket
1568
01:03:42,319 --> 01:03:44,400
inverted commas
1569
01:03:44,400 --> 01:03:47,520
enter first number
1570
01:03:50,319 --> 01:03:52,720
invited commas close
1571
01:03:52,720 --> 01:03:56,440
round bracket close
1572
01:03:58,000 --> 01:04:00,799
be equal to
1573
01:04:00,960 --> 01:04:03,200
end
1574
01:04:04,079 --> 01:04:05,599
input
1575
01:04:05,599 --> 01:04:08,079
round bracket started inverted commas
1576
01:04:08,079 --> 01:04:10,640
for message
1577
01:04:10,640 --> 01:04:13,839
enter second number
1578
01:04:16,079 --> 01:04:17,599
inverted commas
1579
01:04:17,599 --> 01:04:20,559
round bracket close
1580
01:04:21,920 --> 01:04:23,920
now we will assign the operation a plus
1581
01:04:23,920 --> 01:04:26,880
b in variable c for that we will write c
1582
01:04:26,880 --> 01:04:29,839
equal to a plus b
1583
01:04:29,839 --> 01:04:33,119
and now we will print c
1584
01:04:36,720 --> 01:04:38,880
before executing this program we will
1585
01:04:38,880 --> 01:04:41,839
save this program file
1586
01:04:41,839 --> 01:04:43,839
save as
1587
01:04:43,839 --> 01:04:47,839
we will save it as demo1
1588
01:04:47,839 --> 01:04:50,880
and we will write the extension py
1589
01:04:50,880 --> 01:04:53,119
and press save
1590
01:04:53,119 --> 01:04:55,119
as the program is saved now we will run
1591
01:04:55,119 --> 01:04:57,680
this program by click on this run menu
1592
01:04:57,680 --> 01:05:00,720
and then click on run module
1593
01:05:00,720 --> 01:05:02,480
you can see the program has been
1594
01:05:02,480 --> 01:05:04,720
executed now we will write the first
1595
01:05:04,720 --> 01:05:07,520
number that would be 10
1596
01:05:07,520 --> 01:05:09,839
press enter and second number will write
1597
01:05:09,839 --> 01:05:12,720
6 and press enter we will get 16 as an
1598
01:05:12,720 --> 01:05:14,480
output
1599
01:05:14,480 --> 01:05:16,799
so we have executed the program on
1600
01:05:16,799 --> 01:05:19,440
python ideally
1601
01:05:19,440 --> 01:05:22,559
now we will see some best python ids
1602
01:05:22,559 --> 01:05:24,000
in the market
1603
01:05:24,000 --> 01:05:26,559
for that
1604
01:05:28,079 --> 01:05:30,240
the best python ids in the market are
1605
01:05:30,240 --> 01:05:32,799
jupiter notebook visual studio code
1606
01:05:32,799 --> 01:05:35,440
sublime text pycharm
1607
01:05:35,440 --> 01:05:38,559
python ideally and spider notebook among
1608
01:05:38,559 --> 01:05:40,720
all these ids we have worked with python
1609
01:05:40,720 --> 01:05:43,119
ideally now we will write a program in
1610
01:05:43,119 --> 01:05:44,880
jupyter notebook
1611
01:05:44,880 --> 01:05:46,720
for that you have to install jopeta
1612
01:05:46,720 --> 01:05:47,760
notebook
1613
01:05:47,760 --> 01:05:50,079
i have installed anaconda distribution
1614
01:05:50,079 --> 01:05:52,880
and it has python id already installed
1615
01:05:52,880 --> 01:05:54,640
if you want to know how to install
1616
01:05:54,640 --> 01:05:56,720
anaconda distribution go and check the
1617
01:05:56,720 --> 01:05:59,039
video in python playlist
1618
01:05:59,039 --> 01:06:01,520
so let's open jupiter notebook for that
1619
01:06:01,520 --> 01:06:04,480
first we'll click on start
1620
01:06:04,480 --> 01:06:05,520
and write
1621
01:06:05,520 --> 01:06:08,559
jupyter notebook
1622
01:06:09,200 --> 01:06:11,039
and click on the jupyter notebook
1623
01:06:11,039 --> 01:06:13,920
anaconda 3
1624
01:06:15,520 --> 01:06:17,599
and we will wait to get it started in a
1625
01:06:17,599 --> 01:06:19,839
browser
1626
01:06:22,079 --> 01:06:23,920
now we'll select the location where we
1627
01:06:23,920 --> 01:06:26,000
want to store the program
1628
01:06:26,000 --> 01:06:28,960
we will select the location in onedrive
1629
01:06:28,960 --> 01:06:30,400
desktop
1630
01:06:30,400 --> 01:06:33,680
and i've created a python 1 folder there
1631
01:06:33,680 --> 01:06:36,400
so here we'll create a new python 3
1632
01:06:36,400 --> 01:06:38,880
notebook
1633
01:06:42,720 --> 01:06:43,680
and
1634
01:06:43,680 --> 01:06:45,440
name it as
1635
01:06:45,440 --> 01:06:48,319
first python
1636
01:06:50,559 --> 01:06:53,039
program
1637
01:06:55,200 --> 01:06:57,760
and now here we can start writing a
1638
01:06:57,760 --> 01:06:59,680
program
1639
01:06:59,680 --> 01:07:01,839
here we will write a program to multiply
1640
01:07:01,839 --> 01:07:03,599
three numbers
1641
01:07:03,599 --> 01:07:05,839
so for that we will get all the three
1642
01:07:05,839 --> 01:07:08,319
integers inserted by the users in three
1643
01:07:08,319 --> 01:07:10,240
variables and then we will operate the
1644
01:07:10,240 --> 01:07:12,720
operation multiplication
1645
01:07:12,720 --> 01:07:15,359
so for that we will write the command a
1646
01:07:15,359 --> 01:07:18,880
equal to end round bracket started
1647
01:07:18,880 --> 01:07:19,920
input
1648
01:07:19,920 --> 01:07:21,839
round bracket and here we will print the
1649
01:07:21,839 --> 01:07:23,760
message
1650
01:07:23,760 --> 01:07:24,799
enter
1651
01:07:24,799 --> 01:07:28,200
first number
1652
01:07:28,240 --> 01:07:29,760
colon
1653
01:07:29,760 --> 01:07:30,799
and then
1654
01:07:30,799 --> 01:07:32,480
noted commas clause and the round
1655
01:07:32,480 --> 01:07:34,799
bracket clause now we will enter
1656
01:07:34,799 --> 01:07:38,079
another integer in variable b
1657
01:07:38,079 --> 01:07:40,240
for that we will write the command equal
1658
01:07:40,240 --> 01:07:43,760
to end round bracket
1659
01:07:43,760 --> 01:07:45,599
input
1660
01:07:45,599 --> 01:07:49,839
round bracket started inverted commas
1661
01:07:51,440 --> 01:07:54,640
enter second number
1662
01:07:55,280 --> 01:07:56,480
colon
1663
01:07:56,480 --> 01:07:57,839
and then we will close the inverted
1664
01:07:57,839 --> 01:08:00,160
commas and the round brackets
1665
01:08:00,160 --> 01:08:02,480
and now we will get the third integer
1666
01:08:02,480 --> 01:08:05,200
inputted by the user in variable c for
1667
01:08:05,200 --> 01:08:08,000
that we'll write the command c equal to
1668
01:08:08,000 --> 01:08:08,720
in
1669
01:08:08,720 --> 01:08:12,079
round brackets input round brackets
1670
01:08:12,079 --> 01:08:14,799
inverted commas
1671
01:08:14,799 --> 01:08:17,200
enter
1672
01:08:17,600 --> 01:08:19,839
third number
1673
01:08:19,839 --> 01:08:20,960
colon
1674
01:08:20,960 --> 01:08:22,719
inverted commas close and round bracket
1675
01:08:22,719 --> 01:08:25,198
close now we will assign the operation
1676
01:08:25,198 --> 01:08:27,839
of multiplication in variable d
1677
01:08:27,839 --> 01:08:30,799
that would be by the command d equal to
1678
01:08:30,799 --> 01:08:32,479
a
1679
01:08:32,479 --> 01:08:33,600
into
1680
01:08:33,600 --> 01:08:34,319
b
1681
01:08:34,319 --> 01:08:36,238
into c
1682
01:08:36,238 --> 01:08:39,679
now we'll print the variable d
1683
01:08:39,679 --> 01:08:41,600
and for that we will also print the
1684
01:08:41,600 --> 01:08:44,000
message
1685
01:08:45,359 --> 01:08:46,799
multiplication
1686
01:08:46,799 --> 01:08:50,238
of three numbers
1687
01:08:50,238 --> 01:08:52,640
and close the inverted commas and then
1688
01:08:52,640 --> 01:08:53,520
we will
1689
01:08:53,520 --> 01:08:55,600
insert a comma and then write the
1690
01:08:55,600 --> 01:08:56,799
variable d
1691
01:08:56,799 --> 01:08:59,679
as d contains the output of a into b
1692
01:08:59,679 --> 01:09:01,839
into c now we'll click on the run
1693
01:09:01,839 --> 01:09:04,319
command
1694
01:09:04,319 --> 01:09:07,198
and now we will enter our first number
1695
01:09:07,198 --> 01:09:08,399
that would be
1696
01:09:08,399 --> 01:09:09,439
five
1697
01:09:09,439 --> 01:09:11,520
and press enter then the second number
1698
01:09:11,520 --> 01:09:12,399
four
1699
01:09:12,399 --> 01:09:14,399
then the third number six and then press
1700
01:09:14,399 --> 01:09:15,359
enter
1701
01:09:15,359 --> 01:09:16,799
and then we will get the output of
1702
01:09:16,799 --> 01:09:20,880
multiplication of three numbers as 120.
1703
01:09:20,880 --> 01:09:22,960
now we have successfully run the program
1704
01:09:22,960 --> 01:09:25,679
on jupyter notebook so in this video
1705
01:09:25,679 --> 01:09:27,520
we will learn what is a variable in
1706
01:09:27,520 --> 01:09:28,719
python
1707
01:09:28,719 --> 01:09:30,000
then
1708
01:09:30,000 --> 01:09:31,600
we'll understand what are object
1709
01:09:31,600 --> 01:09:32,960
references
1710
01:09:32,960 --> 01:09:34,799
we'll also see the different rules for
1711
01:09:34,799 --> 01:09:37,600
python variables and finally
1712
01:09:37,600 --> 01:09:39,520
we'll understand variables in detail
1713
01:09:39,520 --> 01:09:40,799
with hands-on demonstration on the
1714
01:09:40,799 --> 01:09:43,679
jupyter notebook
1715
01:09:43,679 --> 01:09:46,238
so variables in python hold an object of
1716
01:09:46,238 --> 01:09:48,960
a particular data type python variables
1717
01:09:48,960 --> 01:09:51,439
are containers for storing data values
1718
01:09:51,439 --> 01:09:53,120
we do not need to declare variables
1719
01:09:53,120 --> 01:09:55,120
before using them or declare that data
1720
01:09:55,120 --> 01:09:56,960
type like we do in other programming
1721
01:09:56,960 --> 01:09:59,920
languages such as java or c plus plus
1722
01:09:59,920 --> 01:10:02,640
python is not statically typed
1723
01:10:02,640 --> 01:10:04,400
a variable is created the moment we
1724
01:10:04,400 --> 01:10:06,320
assign a value to it
1725
01:10:06,320 --> 01:10:08,640
a variable is a name given to a memory
1726
01:10:08,640 --> 01:10:09,920
location
1727
01:10:09,920 --> 01:10:11,760
so here we can see
1728
01:10:11,760 --> 01:10:14,239
we have a variable called car and it has
1729
01:10:14,239 --> 01:10:17,040
a value called bmw now this value is of
1730
01:10:17,040 --> 01:10:20,000
type string since it is written within
1731
01:10:20,000 --> 01:10:22,159
double quotes
1732
01:10:22,159 --> 01:10:23,840
variables can have values of multiple
1733
01:10:23,840 --> 01:10:25,360
data types
1734
01:10:25,360 --> 01:10:27,440
now python also allows you to assign
1735
01:10:27,440 --> 01:10:30,080
values to multiple variables in one line
1736
01:10:30,080 --> 01:10:32,239
so here you can see i have assigned
1737
01:10:32,239 --> 01:10:36,000
x y z to audi bmw and mercedes in one
1738
01:10:36,000 --> 01:10:38,080
single line
1739
01:10:38,080 --> 01:10:40,400
all right
1740
01:10:40,400 --> 01:10:42,840
now let's look at object references in
1741
01:10:42,840 --> 01:10:45,520
python a python variable is a symbolic
1742
01:10:45,520 --> 01:10:48,320
name that is a reference or pointer to
1743
01:10:48,320 --> 01:10:49,840
an object
1744
01:10:49,840 --> 01:10:51,840
once an object is assigned to a variable
1745
01:10:51,840 --> 01:10:54,480
you can refer to the object by that name
1746
01:10:54,480 --> 01:10:57,600
so here we are assigning an integer
1747
01:10:57,600 --> 01:11:00,239
value of 50 to the variable a
1748
01:11:00,239 --> 01:11:02,239
so assignment creates an integer object
1749
01:11:02,239 --> 01:11:04,320
with a value of 50 and assigns the
1750
01:11:04,320 --> 01:11:06,480
variable a to point to that object as
1751
01:11:06,480 --> 01:11:09,040
you can see here so a is the variable
1752
01:11:09,040 --> 01:11:10,640
and it is pointing to the object that
1753
01:11:10,640 --> 01:11:13,840
has 50 value
1754
01:11:14,000 --> 01:11:16,719
now let's see another example so in this
1755
01:11:16,719 --> 01:11:17,520
case
1756
01:11:17,520 --> 01:11:18,719
we have
1757
01:11:18,719 --> 01:11:20,560
assigned
1758
01:11:20,560 --> 01:11:23,760
a variable a which has a value of 50 and
1759
01:11:23,760 --> 01:11:26,159
then we have created another variable
1760
01:11:26,159 --> 01:11:27,120
called b
1761
01:11:27,120 --> 01:11:29,920
that has the value stored in a
1762
01:11:29,920 --> 01:11:32,080
now python does not create another
1763
01:11:32,080 --> 01:11:33,600
object in this case
1764
01:11:33,600 --> 01:11:35,920
it creates a new symbolic name or
1765
01:11:35,920 --> 01:11:37,520
reference called b
1766
01:11:37,520 --> 01:11:39,840
which points to the same object that a
1767
01:11:39,840 --> 01:11:41,040
points to
1768
01:11:41,040 --> 01:11:42,880
so here you can see
1769
01:11:42,880 --> 01:11:44,960
both a and b
1770
01:11:44,960 --> 01:11:47,120
are pointing to the same object that is
1771
01:11:47,120 --> 01:11:48,960
50.
1772
01:11:48,960 --> 01:11:49,840
okay
1773
01:11:49,840 --> 01:11:51,600
now
1774
01:11:51,600 --> 01:11:53,360
in this example
1775
01:11:53,360 --> 01:11:55,360
python creates a new integer object with
1776
01:11:55,360 --> 01:11:58,239
a value of 100 and b becomes a reference
1777
01:11:58,239 --> 01:12:00,480
to it we will explore all of these in
1778
01:12:00,480 --> 01:12:02,880
our demo
1779
01:12:02,880 --> 01:12:04,000
now
1780
01:12:04,000 --> 01:12:05,440
there are some rules for python
1781
01:12:05,440 --> 01:12:06,560
variables
1782
01:12:06,560 --> 01:12:07,679
let me just
1783
01:12:07,679 --> 01:12:08,960
read all the
1784
01:12:08,960 --> 01:12:10,560
and we will explore all these rules in
1785
01:12:10,560 --> 01:12:11,920
our demo session
1786
01:12:11,920 --> 01:12:14,159
so first a variable name
1787
01:12:14,159 --> 01:12:15,760
must start with the letter or the
1788
01:12:15,760 --> 01:12:17,679
underscore character
1789
01:12:17,679 --> 01:12:20,840
a variable name cannot start with a
1790
01:12:20,840 --> 01:12:23,600
number a variable name can contain only
1791
01:12:23,600 --> 01:12:25,840
alphanumeric characters or underscores
1792
01:12:25,840 --> 01:12:28,880
so you can see from a to z 0 to 9 or
1793
01:12:28,880 --> 01:12:31,440
just underscore
1794
01:12:31,440 --> 01:12:33,679
variable names are case sensitive so
1795
01:12:33,679 --> 01:12:35,760
here you can see i have written
1796
01:12:35,760 --> 01:12:37,040
date
1797
01:12:37,040 --> 01:12:39,520
in three different types and
1798
01:12:39,520 --> 01:12:41,120
these
1799
01:12:41,120 --> 01:12:43,120
are read as different variables and not
1800
01:12:43,120 --> 01:12:45,679
the same variable
1801
01:12:45,679 --> 01:12:46,719
and then
1802
01:12:46,719 --> 01:12:48,320
there are several techniques to write
1803
01:12:48,320 --> 01:12:50,719
multi-word variables you have the camel
1804
01:12:50,719 --> 01:12:52,480
case you have the pascal case and we
1805
01:12:52,480 --> 01:12:55,120
have the snake case don't worry about it
1806
01:12:55,120 --> 01:12:56,719
we'll explore these
1807
01:12:56,719 --> 01:12:57,840
in the demo
1808
01:12:57,840 --> 01:12:59,679
now let me take you to my jupyter
1809
01:12:59,679 --> 01:13:00,880
notebook where we will understand
1810
01:13:00,880 --> 01:13:04,239
variables in detail
1811
01:13:04,239 --> 01:13:06,480
okay so i am on my jupyter notebook and
1812
01:13:06,480 --> 01:13:07,600
i have
1813
01:13:07,600 --> 01:13:11,120
my title as variables in python
1814
01:13:11,120 --> 01:13:13,520
so let's start
1815
01:13:13,520 --> 01:13:16,000
with my first variable let's say i have
1816
01:13:16,000 --> 01:13:21,280
a variable called a and i am assigning
1817
01:13:21,360 --> 01:13:23,199
50 to it
1818
01:13:23,199 --> 01:13:25,520
now you must be aware 50 is of type
1819
01:13:25,520 --> 01:13:26,880
integer
1820
01:13:26,880 --> 01:13:27,600
so
1821
01:13:27,600 --> 01:13:30,000
this a variable is now
1822
01:13:30,000 --> 01:13:32,480
holding an integer value
1823
01:13:32,480 --> 01:13:35,920
now if you want you can just print a
1824
01:13:35,920 --> 01:13:37,679
if i hit shift enter
1825
01:13:37,679 --> 01:13:40,239
it will give me the value of 50.
1826
01:13:40,239 --> 01:13:41,760
now to check
1827
01:13:41,760 --> 01:13:43,920
the data type of a you can use the type
1828
01:13:43,920 --> 01:13:46,560
function now we have covered data types
1829
01:13:46,560 --> 01:13:48,159
in detail in our previous video if you
1830
01:13:48,159 --> 01:13:50,400
want you can have a look at it
1831
01:13:50,400 --> 01:13:53,440
so i will write type of a if i hit shift
1832
01:13:53,440 --> 01:13:57,280
enter it gives me the integer type
1833
01:13:57,280 --> 01:13:59,199
okay
1834
01:13:59,199 --> 01:14:01,360
now
1835
01:14:01,360 --> 01:14:02,880
let me show you another function called
1836
01:14:02,880 --> 01:14:05,679
id if i write id of a
1837
01:14:05,679 --> 01:14:08,480
let's run it it gives me a number this
1838
01:14:08,480 --> 01:14:10,480
is a long number
1839
01:14:10,480 --> 01:14:12,640
now the id function in python returns a
1840
01:14:12,640 --> 01:14:15,199
unique id for the specified object so
1841
01:14:15,199 --> 01:14:18,239
here object is a
1842
01:14:18,239 --> 01:14:20,159
so all objects in python have its own
1843
01:14:20,159 --> 01:14:21,360
unique id
1844
01:14:21,360 --> 01:14:23,280
the id is assigned to the object when it
1845
01:14:23,280 --> 01:14:24,960
is created
1846
01:14:24,960 --> 01:14:26,560
now what if
1847
01:14:26,560 --> 01:14:29,520
i assign
1848
01:14:29,520 --> 01:14:32,960
100 to a this time and let's see
1849
01:14:32,960 --> 01:14:34,800
the id of a
1850
01:14:34,800 --> 01:14:38,239
if i run it you can see here the values
1851
01:14:38,239 --> 01:14:40,719
are different than the previous one
1852
01:14:40,719 --> 01:14:43,040
so every time the id function will
1853
01:14:43,040 --> 01:14:45,040
return a unique id for the specified
1854
01:14:45,040 --> 01:14:46,719
object
1855
01:14:46,719 --> 01:14:48,480
all right
1856
01:14:48,480 --> 01:14:49,280
now
1857
01:14:49,280 --> 01:14:51,360
we learned in our theory section that
1858
01:14:51,360 --> 01:14:53,360
you can assign multiple variables in a
1859
01:14:53,360 --> 01:14:55,520
single line now let me show you how to
1860
01:14:55,520 --> 01:14:56,640
do it
1861
01:14:56,640 --> 01:14:58,640
suppose
1862
01:14:58,640 --> 01:14:59,920
i'll write x
1863
01:14:59,920 --> 01:15:02,400
comma y comma
1864
01:15:02,400 --> 01:15:03,120
c
1865
01:15:03,120 --> 01:15:05,199
so these are the three variables
1866
01:15:05,199 --> 01:15:08,000
and i want to assign
1867
01:15:08,000 --> 01:15:09,600
three values to them
1868
01:15:09,600 --> 01:15:12,480
so let's say x will have 50
1869
01:15:12,480 --> 01:15:15,120
in y i want to store let's say
1870
01:15:15,120 --> 01:15:18,120
168.6
1871
01:15:20,000 --> 01:15:21,760
and
1872
01:15:21,760 --> 01:15:23,440
my z
1873
01:15:23,440 --> 01:15:25,360
will have
1874
01:15:25,360 --> 01:15:28,000
a string or a text value let's say
1875
01:15:28,000 --> 01:15:30,320
simply loan
1876
01:15:30,320 --> 01:15:31,360
all right
1877
01:15:31,360 --> 01:15:32,719
let's run it
1878
01:15:32,719 --> 01:15:33,679
okay
1879
01:15:33,679 --> 01:15:35,199
now you can
1880
01:15:35,199 --> 01:15:37,040
print them one by one
1881
01:15:37,040 --> 01:15:39,920
you can see here x has 50
1882
01:15:39,920 --> 01:15:41,000
y has
1883
01:15:41,000 --> 01:15:42,640
168.6
1884
01:15:42,640 --> 01:15:44,480
and z has
1885
01:15:44,480 --> 01:15:46,880
simply learn of type
1886
01:15:46,880 --> 01:15:50,000
text you can check the
1887
01:15:50,000 --> 01:15:51,760
data types if you want let's say i want
1888
01:15:51,760 --> 01:15:54,000
to check the data type of y
1889
01:15:54,000 --> 01:15:57,040
it gives me a float because 68.6 is a
1890
01:15:57,040 --> 01:15:58,159
float
1891
01:15:58,159 --> 01:16:01,360
you can check the data type of
1892
01:16:01,360 --> 01:16:02,320
z
1893
01:16:02,320 --> 01:16:04,960
it is of type string
1894
01:16:04,960 --> 01:16:05,760
okay
1895
01:16:05,760 --> 01:16:07,280
now you can also perform the same
1896
01:16:07,280 --> 01:16:09,040
operation of assigning
1897
01:16:09,040 --> 01:16:10,960
multiple variables in a single line in
1898
01:16:10,960 --> 01:16:14,719
another way let me show you how to do it
1899
01:16:15,520 --> 01:16:18,000
suppose i want to assign
1900
01:16:18,000 --> 01:16:20,239
the same
1901
01:16:20,239 --> 01:16:22,400
value to three different variables let's
1902
01:16:22,400 --> 01:16:24,800
say i want to assign the value of 10 to
1903
01:16:24,800 --> 01:16:26,159
x y and z
1904
01:16:26,159 --> 01:16:27,600
so i'll write
1905
01:16:27,600 --> 01:16:32,480
x equal to y equal to z equal to
1906
01:16:33,679 --> 01:16:35,760
i'll write 10.
1907
01:16:35,760 --> 01:16:37,360
if i run it
1908
01:16:37,360 --> 01:16:39,280
and let me print them one by one you can
1909
01:16:39,280 --> 01:16:41,920
see here x is 10 y is 10 and
1910
01:16:41,920 --> 01:16:44,880
z is also 10.
1911
01:16:45,199 --> 01:16:46,560
now there is
1912
01:16:46,560 --> 01:16:49,280
another way to do it that is
1913
01:16:49,280 --> 01:16:51,199
i'll pass in my variables x comma y
1914
01:16:51,199 --> 01:16:52,880
comma z
1915
01:16:52,880 --> 01:16:54,719
and then
1916
01:16:54,719 --> 01:16:58,719
i can write 10 comma 10 comma 10
1917
01:16:58,719 --> 01:17:02,000
this will return the same result
1918
01:17:02,000 --> 01:17:04,320
okay
1919
01:17:04,960 --> 01:17:07,040
now let's look at the
1920
01:17:07,040 --> 01:17:09,440
rules for writing variable names
1921
01:17:09,440 --> 01:17:12,000
so these rules we had discussed in the
1922
01:17:12,000 --> 01:17:13,440
pbt section
1923
01:17:13,440 --> 01:17:16,239
now we'll explore them one by one so the
1924
01:17:16,239 --> 01:17:18,800
first rule is a variable name
1925
01:17:18,800 --> 01:17:21,040
must start with a letter or the
1926
01:17:21,040 --> 01:17:23,280
underscore character
1927
01:17:23,280 --> 01:17:25,199
so
1928
01:17:25,199 --> 01:17:27,280
let me just give a comment first
1929
01:17:27,280 --> 01:17:28,840
rules
1930
01:17:28,840 --> 01:17:32,000
for variable
1931
01:17:32,000 --> 01:17:34,400
naming
1932
01:17:34,480 --> 01:17:36,159
so the first rule says a variable name
1933
01:17:36,159 --> 01:17:38,000
must start with a letter or the
1934
01:17:38,000 --> 01:17:40,800
underscore character so if i write let's
1935
01:17:40,800 --> 01:17:41,840
say
1936
01:17:41,840 --> 01:17:44,400
some random variable name abc equal to
1937
01:17:44,400 --> 01:17:45,440
100
1938
01:17:45,440 --> 01:17:46,400
this is
1939
01:17:46,400 --> 01:17:48,640
valid you can print it
1940
01:17:48,640 --> 01:17:51,199
abc it gives me 100.
1941
01:17:51,199 --> 01:17:52,880
now
1942
01:17:52,880 --> 01:17:54,800
you can also write the variable name as
1943
01:17:54,800 --> 01:17:55,679
a
1944
01:17:55,679 --> 01:17:58,719
followed by any number
1945
01:17:58,719 --> 01:18:00,880
it should be integer number
1946
01:18:00,880 --> 01:18:03,760
if i write 102 equal to
1947
01:18:03,760 --> 01:18:04,880
let's say
1948
01:18:04,880 --> 01:18:06,159
this time
1949
01:18:06,159 --> 01:18:08,320
i'll
1950
01:18:08,320 --> 01:18:10,840
give a string let's say
1951
01:18:10,840 --> 01:18:14,560
india and let me print it
1952
01:18:14,560 --> 01:18:17,679
it works perfectly fine
1953
01:18:17,679 --> 01:18:19,920
now we also said the name must start
1954
01:18:19,920 --> 01:18:21,840
with an underscore
1955
01:18:21,840 --> 01:18:24,000
i can start with an underscore and then
1956
01:18:24,000 --> 01:18:26,560
write let's say 123
1957
01:18:26,560 --> 01:18:29,920
is equal to 70.5
1958
01:18:29,920 --> 01:18:32,480
now let me print it
1959
01:18:32,480 --> 01:18:34,560
now this works as well so you can start
1960
01:18:34,560 --> 01:18:36,560
your variable name with an underscore
1961
01:18:36,560 --> 01:18:39,440
followed by a letter or numbers
1962
01:18:39,440 --> 01:18:41,360
let me show you another example this
1963
01:18:41,360 --> 01:18:42,880
time i follow the underscore with
1964
01:18:42,880 --> 01:18:44,960
letters let's say
1965
01:18:44,960 --> 01:18:47,280
dog
1966
01:18:47,280 --> 01:18:50,480
i'll just give
1967
01:18:51,520 --> 01:18:53,520
any random dog name let's say i have a
1968
01:18:53,520 --> 01:18:56,400
dog called puppy
1969
01:18:56,400 --> 01:18:59,520
and let's see i'll print it
1970
01:18:59,520 --> 01:19:00,960
this also works
1971
01:19:00,960 --> 01:19:03,360
okay
1972
01:19:04,880 --> 01:19:05,760
now
1973
01:19:05,760 --> 01:19:07,760
you can have multiple underscores in
1974
01:19:07,760 --> 01:19:09,280
your name as well
1975
01:19:09,280 --> 01:19:10,800
suppose i'll write
1976
01:19:10,800 --> 01:19:12,480
underscore
1977
01:19:12,480 --> 01:19:13,280
name
1978
01:19:13,280 --> 01:19:15,040
underscore each
1979
01:19:15,040 --> 01:19:16,719
this is my variable name
1980
01:19:16,719 --> 01:19:19,199
i'll write
1981
01:19:19,280 --> 01:19:20,800
let's say
1982
01:19:20,800 --> 01:19:23,440
stuart
1983
01:19:23,440 --> 01:19:25,760
36
1984
01:19:25,760 --> 01:19:27,840
and let me print it
1985
01:19:27,840 --> 01:19:29,360
i'll write underscore name underscore
1986
01:19:29,360 --> 01:19:30,239
age
1987
01:19:30,239 --> 01:19:31,760
this is also a
1988
01:19:31,760 --> 01:19:33,840
valid variable name so you can have
1989
01:19:33,840 --> 01:19:36,000
multiple underscores in your variable
1990
01:19:36,000 --> 01:19:37,679
name
1991
01:19:37,679 --> 01:19:39,600
now
1992
01:19:39,600 --> 01:19:41,679
the next rule is a variable name cannot
1993
01:19:41,679 --> 01:19:43,280
start with a number
1994
01:19:43,280 --> 01:19:45,360
so if i write
1995
01:19:45,360 --> 01:19:47,520
let's say 4
1996
01:19:47,520 --> 01:19:51,640
name equal to
1997
01:19:51,679 --> 01:19:54,080
gilly
1998
01:19:54,960 --> 01:19:57,920
and if i try to print it
1999
01:19:57,920 --> 01:20:00,000
it will throw an error so you cannot
2000
01:20:00,000 --> 01:20:01,520
have a variable name starting with a
2001
01:20:01,520 --> 01:20:03,120
number
2002
01:20:03,120 --> 01:20:05,040
all right
2003
01:20:05,040 --> 01:20:06,800
now
2004
01:20:06,800 --> 01:20:07,760
let me
2005
01:20:07,760 --> 01:20:10,400
scroll down
2006
01:20:12,639 --> 01:20:14,880
the next rule is a variable name can
2007
01:20:14,880 --> 01:20:17,199
only contain alphanumeric characters and
2008
01:20:17,199 --> 01:20:18,480
underscores
2009
01:20:18,480 --> 01:20:21,679
so if i write at the rate
2010
01:20:21,679 --> 01:20:23,920
then let's say
2011
01:20:23,920 --> 01:20:25,280
age
2012
01:20:25,280 --> 01:20:26,560
equal to
2013
01:20:26,560 --> 01:20:28,239
40 if i run this
2014
01:20:28,239 --> 01:20:30,159
it throws me an error because at the
2015
01:20:30,159 --> 01:20:32,960
rate is not recognized and it is not
2016
01:20:32,960 --> 01:20:34,960
a valid variable name
2017
01:20:34,960 --> 01:20:37,120
similarly let's say i'll write
2018
01:20:37,120 --> 01:20:39,120
name
2019
01:20:39,120 --> 01:20:40,800
dollar
2020
01:20:40,800 --> 01:20:42,080
age
2021
01:20:42,080 --> 01:20:44,239
equal to let's say 40
2022
01:20:44,239 --> 01:20:46,639
again this is not recognized dollar you
2023
01:20:46,639 --> 01:20:50,480
cannot use it in your variable name
2024
01:20:50,480 --> 01:20:53,199
the next rule is
2025
01:20:53,199 --> 01:20:55,760
variable names are case sensitive
2026
01:20:55,760 --> 01:20:58,400
so suppose i have a variable called car
2027
01:20:58,400 --> 01:21:00,800
which is all in lower case
2028
01:21:00,800 --> 01:21:02,159
and
2029
01:21:02,159 --> 01:21:06,560
my car has a value let's say bmw
2030
01:21:06,560 --> 01:21:09,120
now if i were to print car it gives me
2031
01:21:09,120 --> 01:21:10,719
bmw
2032
01:21:10,719 --> 01:21:12,800
suppose i have another variable which
2033
01:21:12,800 --> 01:21:14,880
starts with a capital letter
2034
01:21:14,880 --> 01:21:18,560
c followed by lowercase ar
2035
01:21:18,560 --> 01:21:20,800
if i were to assign
2036
01:21:20,800 --> 01:21:23,679
car to let's say tata
2037
01:21:23,679 --> 01:21:25,440
and i print it
2038
01:21:25,440 --> 01:21:28,239
it returns me tata and not bmw so you
2039
01:21:28,239 --> 01:21:30,080
should keep in mind that variable names
2040
01:21:30,080 --> 01:21:32,719
are case sensitive
2041
01:21:32,719 --> 01:21:34,719
now similarly i can also have a variable
2042
01:21:34,719 --> 01:21:37,679
in all upper case let's say
2043
01:21:37,679 --> 01:21:39,840
my
2044
01:21:39,840 --> 01:21:42,719
car is of toyota brand
2045
01:21:42,719 --> 01:21:45,840
i made a spelling mistake
2046
01:21:45,840 --> 01:21:48,719
okay now if i print car
2047
01:21:48,719 --> 01:21:50,880
it gives me toyota
2048
01:21:50,880 --> 01:21:52,639
so
2049
01:21:52,639 --> 01:21:56,159
these are the different
2050
01:21:56,159 --> 01:22:01,120
types of using case sensitive variables
2051
01:22:01,679 --> 01:22:03,440
now another important
2052
01:22:03,440 --> 01:22:04,880
thing to note is
2053
01:22:04,880 --> 01:22:07,920
in python reserve words cannot be used
2054
01:22:07,920 --> 01:22:09,520
as a variable name
2055
01:22:09,520 --> 01:22:11,920
so let me open a new tab and here you
2056
01:22:11,920 --> 01:22:15,040
will search for
2057
01:22:15,040 --> 01:22:17,360
reserve
2058
01:22:17,360 --> 01:22:19,040
keywords
2059
01:22:19,040 --> 01:22:21,679
in python
2060
01:22:21,840 --> 01:22:24,400
let me open this link
2061
01:22:24,400 --> 01:22:27,440
or any other link for your reference
2062
01:22:27,440 --> 01:22:29,120
you see here
2063
01:22:29,120 --> 01:22:31,440
these are all
2064
01:22:31,440 --> 01:22:33,520
your keywords in python and these cannot
2065
01:22:33,520 --> 01:22:36,080
be used as variable names let's try
2066
01:22:36,080 --> 01:22:39,600
using a few of them let's say i'll use
2067
01:22:39,600 --> 01:22:41,440
break
2068
01:22:41,440 --> 01:22:42,880
let's assign
2069
01:22:42,880 --> 01:22:45,840
a value to variable break equal to let's
2070
01:22:45,840 --> 01:22:47,199
say
2071
01:22:47,199 --> 01:22:50,080
60 if i run it it gives me an error
2072
01:22:50,080 --> 01:22:52,719
invalid syntax break you can't use
2073
01:22:52,719 --> 01:22:55,199
similarly let's use
2074
01:22:55,199 --> 01:22:57,679
another keyword let's say i'll use for
2075
01:22:57,679 --> 01:22:58,719
this time
2076
01:22:58,719 --> 01:23:00,480
we have a keyword
2077
01:23:00,480 --> 01:23:02,639
called for you can see it here
2078
01:23:02,639 --> 01:23:04,480
suppose i write
2079
01:23:04,480 --> 01:23:06,159
for
2080
01:23:06,159 --> 01:23:07,760
equal to
2081
01:23:07,760 --> 01:23:09,040
i have
2082
01:23:09,040 --> 01:23:12,080
let's say virat
2083
01:23:12,080 --> 01:23:13,920
if i run this it throws me an error
2084
01:23:13,920 --> 01:23:15,199
again
2085
01:23:15,199 --> 01:23:17,920
let's try one more keyword
2086
01:23:17,920 --> 01:23:20,080
do we have try as a keyword yes we have
2087
01:23:20,080 --> 01:23:23,040
you can see here try is a keyword
2088
01:23:23,040 --> 01:23:25,840
if i write try equal to
2089
01:23:25,840 --> 01:23:28,000
any random value i'll pick this time i'm
2090
01:23:28,000 --> 01:23:31,199
giving a floating number let's say 140.5
2091
01:23:31,199 --> 01:23:34,880
if i run it again it throws me a
2092
01:23:34,880 --> 01:23:35,920
error
2093
01:23:35,920 --> 01:23:36,880
but
2094
01:23:36,880 --> 01:23:38,400
you can use
2095
01:23:38,400 --> 01:23:40,639
an underscore and then
2096
01:23:40,639 --> 01:23:43,120
you can give that keyboard let's a try
2097
01:23:43,120 --> 01:23:45,199
i'll assign the same value
2098
01:23:45,199 --> 01:23:48,239
and let's say we'll print it this will
2099
01:23:48,239 --> 01:23:50,639
work fine since
2100
01:23:50,639 --> 01:23:52,960
the variable name is no more a keyword
2101
01:23:52,960 --> 01:23:54,960
because we have an underscore
2102
01:23:54,960 --> 01:23:57,040
in the beginning
2103
01:23:57,040 --> 01:23:58,400
ok
2104
01:23:58,400 --> 01:24:01,360
now let me show you some
2105
01:24:01,360 --> 01:24:04,320
basic operations that you can perform
2106
01:24:04,320 --> 01:24:07,360
on python variables let's say we have
2107
01:24:07,360 --> 01:24:10,080
a equal to 100 and let's say b equal to
2108
01:24:10,080 --> 01:24:11,920
200
2109
01:24:11,920 --> 01:24:14,639
you can assign
2110
01:24:14,639 --> 01:24:17,040
an operation to another variable called
2111
01:24:17,040 --> 01:24:18,880
result let's say i want
2112
01:24:18,880 --> 01:24:20,960
the operation to be an addition
2113
01:24:20,960 --> 01:24:22,159
operation
2114
01:24:22,159 --> 01:24:23,440
and let's
2115
01:24:23,440 --> 01:24:27,760
go ahead and print result
2116
01:24:27,760 --> 01:24:31,520
you can see it has given us 300 because
2117
01:24:31,520 --> 01:24:33,360
in result we are storing
2118
01:24:33,360 --> 01:24:36,719
the addition of a and b so 100 plus 200
2119
01:24:36,719 --> 01:24:38,320
is 300
2120
01:24:38,320 --> 01:24:40,000
now similarly
2121
01:24:40,000 --> 01:24:42,880
you can perform
2122
01:24:43,280 --> 01:24:45,440
many operations like let's say this time
2123
01:24:45,440 --> 01:24:47,760
we'll perform a subtraction operation
2124
01:24:47,760 --> 01:24:50,159
so this time i'm storing
2125
01:24:50,159 --> 01:24:52,639
a minus b in result and let's go ahead
2126
01:24:52,639 --> 01:24:54,800
and print result you can see here 100
2127
01:24:54,800 --> 01:24:57,600
minus 200 is minus 100
2128
01:24:57,600 --> 01:24:59,760
then you can also have
2129
01:24:59,760 --> 01:25:02,080
let's say result equal to
2130
01:25:02,080 --> 01:25:02,960
a
2131
01:25:02,960 --> 01:25:03,760
by
2132
01:25:03,760 --> 01:25:04,719
b
2133
01:25:04,719 --> 01:25:06,639
and let's print it
2134
01:25:06,639 --> 01:25:08,719
fair enough we have 0.5 because 100
2135
01:25:08,719 --> 01:25:11,840
divided by 200 is 0.5 similarly you can
2136
01:25:11,840 --> 01:25:13,199
also have
2137
01:25:13,199 --> 01:25:18,000
integer division suppose i write 100
2138
01:25:18,000 --> 01:25:20,400
divided by
2139
01:25:20,400 --> 01:25:22,159
say 40
2140
01:25:22,159 --> 01:25:23,120
and i'll
2141
01:25:23,120 --> 01:25:24,480
print the result
2142
01:25:24,480 --> 01:25:27,760
you see here it has given us 2.5 but
2143
01:25:27,760 --> 01:25:29,760
what if i want to perform an integer
2144
01:25:29,760 --> 01:25:31,840
division so i'll add
2145
01:25:31,840 --> 01:25:34,560
one more forward slash here and let's
2146
01:25:34,560 --> 01:25:37,280
run it it gives me only two so it has
2147
01:25:37,280 --> 01:25:40,880
truncated the 0.5
2148
01:25:40,880 --> 01:25:42,560
okay
2149
01:25:42,560 --> 01:25:44,800
now moving ahead
2150
01:25:44,800 --> 01:25:48,159
let's say i have a variable called
2151
01:25:48,159 --> 01:25:50,159
country
2152
01:25:50,159 --> 01:25:51,440
and
2153
01:25:51,440 --> 01:25:53,600
i have
2154
01:25:53,600 --> 01:25:56,080
india in it
2155
01:25:56,080 --> 01:25:58,400
so country is my
2156
01:25:58,400 --> 01:26:00,400
variable name and it is pointing to the
2157
01:26:00,400 --> 01:26:02,320
object that has
2158
01:26:02,320 --> 01:26:05,120
india as the value
2159
01:26:05,120 --> 01:26:08,320
i can print the
2160
01:26:08,320 --> 01:26:09,679
variable
2161
01:26:09,679 --> 01:26:11,679
you see here i have india
2162
01:26:11,679 --> 01:26:14,880
and now if you want you can perform some
2163
01:26:14,880 --> 01:26:17,040
extraction operation
2164
01:26:17,040 --> 01:26:20,239
suppose if i write country
2165
01:26:20,239 --> 01:26:23,520
within square brackets if i write
2166
01:26:23,520 --> 01:26:25,199
3
2167
01:26:25,199 --> 01:26:26,639
it gives me
2168
01:26:26,639 --> 01:26:28,639
the character that is present at the
2169
01:26:28,639 --> 01:26:31,199
third index so i is present at the 0th
2170
01:26:31,199 --> 01:26:34,080
index n is at the first index d is at
2171
01:26:34,080 --> 01:26:36,320
the second index and i is at the third
2172
01:26:36,320 --> 01:26:38,239
index let's say i want everything that
2173
01:26:38,239 --> 01:26:39,840
is present
2174
01:26:39,840 --> 01:26:43,840
until the third index it gives me ind
2175
01:26:43,840 --> 01:26:46,560
so this 3 is exclusive it will only
2176
01:26:46,560 --> 01:26:47,760
return us
2177
01:26:47,760 --> 01:26:49,520
the values present at the zeroth index
2178
01:26:49,520 --> 01:26:52,800
the first index and the second index
2179
01:26:52,800 --> 01:26:55,120
okay
2180
01:26:55,920 --> 01:26:57,920
now let me show you another interesting
2181
01:26:57,920 --> 01:26:58,960
example
2182
01:26:58,960 --> 01:27:01,360
suppose i have a list that is called
2183
01:27:01,360 --> 01:27:02,320
cars
2184
01:27:02,320 --> 01:27:05,360
and i have a list of
2185
01:27:05,360 --> 01:27:06,880
car names
2186
01:27:06,880 --> 01:27:08,800
let's say i have
2187
01:27:08,800 --> 01:27:11,440
bmw
2188
01:27:12,719 --> 01:27:15,120
then i have
2189
01:27:15,120 --> 01:27:17,600
ferrari
2190
01:27:17,600 --> 01:27:20,639
and let's say
2191
01:27:20,800 --> 01:27:24,080
i have toyota
2192
01:27:24,560 --> 01:27:27,520
i'll run it and i'll print my
2193
01:27:27,520 --> 01:27:29,600
list that is cars you can see here
2194
01:27:29,600 --> 01:27:32,400
there are three car names
2195
01:27:32,400 --> 01:27:35,280
and now what if i want to
2196
01:27:35,280 --> 01:27:38,320
put each of these car names in different
2197
01:27:38,320 --> 01:27:41,360
variables so i can write x comma y
2198
01:27:41,360 --> 01:27:42,800
comma z
2199
01:27:42,800 --> 01:27:44,400
and
2200
01:27:44,400 --> 01:27:46,719
i'll write equal to cars
2201
01:27:46,719 --> 01:27:48,080
if i run this
2202
01:27:48,080 --> 01:27:49,920
now what it will do is it will assign
2203
01:27:49,920 --> 01:27:52,639
each of these values to
2204
01:27:52,639 --> 01:27:55,120
x y and z respectively now if you want
2205
01:27:55,120 --> 01:27:57,199
to print x you can see here
2206
01:27:57,199 --> 01:28:01,600
x has bmw then y has ferrari and z has
2207
01:28:01,600 --> 01:28:02,880
toyota
2208
01:28:02,880 --> 01:28:06,239
so this is another way to assign
2209
01:28:06,239 --> 01:28:08,480
values to variables
2210
01:28:08,480 --> 01:28:10,880
from a list
2211
01:28:10,880 --> 01:28:13,280
okay
2212
01:28:13,280 --> 01:28:14,880
now
2213
01:28:14,880 --> 01:28:16,880
you can also
2214
01:28:16,880 --> 01:28:19,440
use variables in the print
2215
01:28:19,440 --> 01:28:21,199
function or the print statement suppose
2216
01:28:21,199 --> 01:28:22,880
i write x
2217
01:28:22,880 --> 01:28:26,960
equal to let's say it has simply learned
2218
01:28:26,960 --> 01:28:28,639
now
2219
01:28:28,639 --> 01:28:32,000
i can print a message let's say
2220
01:28:32,000 --> 01:28:34,400
welcome
2221
01:28:36,560 --> 01:28:37,840
to
2222
01:28:37,840 --> 01:28:39,920
i'll give a space
2223
01:28:39,920 --> 01:28:41,760
close the quotations
2224
01:28:41,760 --> 01:28:43,920
give a plus sign and i'll give the
2225
01:28:43,920 --> 01:28:45,440
variable name
2226
01:28:45,440 --> 01:28:47,280
if i
2227
01:28:47,280 --> 01:28:49,120
run it you can see here
2228
01:28:49,120 --> 01:28:51,679
the message is printed welcome to
2229
01:28:51,679 --> 01:28:53,520
and the x which has the value of simply
2230
01:28:53,520 --> 01:28:57,840
loan so welcome to simply loan okay
2231
01:28:57,840 --> 01:28:59,760
now another thing to note in python is
2232
01:28:59,760 --> 01:29:01,760
that you cannot add an integer and a
2233
01:29:01,760 --> 01:29:04,719
string suppose i have x as the variable
2234
01:29:04,719 --> 01:29:07,520
and it has a value let's say 20
2235
01:29:07,520 --> 01:29:08,639
and then
2236
01:29:08,639 --> 01:29:12,080
i have y which has
2237
01:29:12,080 --> 01:29:14,159
the string simply learn
2238
01:29:14,159 --> 01:29:16,239
now string can be put in double quotes
2239
01:29:16,239 --> 01:29:18,000
or in single quotes you can use
2240
01:29:18,000 --> 01:29:20,400
whichever way you want
2241
01:29:20,400 --> 01:29:22,719
suppose i print
2242
01:29:22,719 --> 01:29:23,520
x
2243
01:29:23,520 --> 01:29:26,480
plus y
2244
01:29:26,480 --> 01:29:27,920
let's see the result
2245
01:29:27,920 --> 01:29:30,560
it gives us an error so you cannot
2246
01:29:30,560 --> 01:29:32,480
print
2247
01:29:32,480 --> 01:29:33,679
or add
2248
01:29:33,679 --> 01:29:35,760
an integer and a string so you need to
2249
01:29:35,760 --> 01:29:37,679
convert one of these
2250
01:29:37,679 --> 01:29:39,040
values
2251
01:29:39,040 --> 01:29:40,560
now there are several techniques to
2252
01:29:40,560 --> 01:29:43,120
write multi-words variables
2253
01:29:43,120 --> 01:29:44,719
you can use the camel keys to write
2254
01:29:44,719 --> 01:29:46,480
multi-word variables
2255
01:29:46,480 --> 01:29:49,440
so here each word except the first
2256
01:29:49,440 --> 01:29:51,760
starts with a capital letter
2257
01:29:51,760 --> 01:29:54,080
so i'll just give a comment here and
2258
01:29:54,080 --> 01:29:55,840
write
2259
01:29:55,840 --> 01:29:58,639
camel case
2260
01:29:58,960 --> 01:30:01,040
and
2261
01:30:01,040 --> 01:30:03,199
the way to write is
2262
01:30:03,199 --> 01:30:04,960
apart from the
2263
01:30:04,960 --> 01:30:06,400
first word
2264
01:30:06,400 --> 01:30:08,080
all the words
2265
01:30:08,080 --> 01:30:10,719
start with the capital letter
2266
01:30:10,719 --> 01:30:13,360
suppose i write my department equal to
2267
01:30:13,360 --> 01:30:15,920
marketing
2268
01:30:16,000 --> 01:30:17,280
so here
2269
01:30:17,280 --> 01:30:20,000
the first word my
2270
01:30:20,000 --> 01:30:22,239
the m starts with a small letter while
2271
01:30:22,239 --> 01:30:24,080
the second word department the d starts
2272
01:30:24,080 --> 01:30:26,159
in a capital letter so this is an
2273
01:30:26,159 --> 01:30:28,800
example of a camel case
2274
01:30:28,800 --> 01:30:31,120
if i
2275
01:30:31,120 --> 01:30:33,280
want to print it i can write my
2276
01:30:33,280 --> 01:30:34,639
department
2277
01:30:34,639 --> 01:30:36,800
and run it it gives me marketing
2278
01:30:36,800 --> 01:30:38,719
let's see another example
2279
01:30:38,719 --> 01:30:41,920
i have let's say car brand
2280
01:30:41,920 --> 01:30:43,920
so you see here
2281
01:30:43,920 --> 01:30:47,280
car brand are two words
2282
01:30:47,280 --> 01:30:48,800
but
2283
01:30:48,800 --> 01:30:51,360
the first word starts with a
2284
01:30:51,360 --> 01:30:54,639
small letter while the second word has
2285
01:30:54,639 --> 01:30:55,920
an uppercase
2286
01:30:55,920 --> 01:30:57,280
b
2287
01:30:57,280 --> 01:31:01,280
let's say my car brand is hyundai
2288
01:31:01,280 --> 01:31:04,400
now you can print it
2289
01:31:04,400 --> 01:31:07,679
there you go all right
2290
01:31:10,239 --> 01:31:12,960
moving ahead
2291
01:31:13,360 --> 01:31:14,719
we also have
2292
01:31:14,719 --> 01:31:17,040
pascal case where each word starts with
2293
01:31:17,040 --> 01:31:18,560
a capital letter
2294
01:31:18,560 --> 01:31:19,600
suppose
2295
01:31:19,600 --> 01:31:22,560
i have a variable called my name
2296
01:31:22,560 --> 01:31:23,600
is
2297
01:31:23,600 --> 01:31:25,280
let's say nathan
2298
01:31:25,280 --> 01:31:27,440
here you can see my and name are two
2299
01:31:27,440 --> 01:31:29,040
different words and
2300
01:31:29,040 --> 01:31:31,040
both the words start with a capital
2301
01:31:31,040 --> 01:31:33,840
letter so this is an example of pascal
2302
01:31:33,840 --> 01:31:35,040
keys
2303
01:31:35,040 --> 01:31:37,360
if i run it
2304
01:31:37,360 --> 01:31:39,920
let me just print it first okay you see
2305
01:31:39,920 --> 01:31:43,120
here i have my name similarly let's say
2306
01:31:43,120 --> 01:31:45,199
we have another pascal case
2307
01:31:45,199 --> 01:31:47,360
multi-word variable which is age now
2308
01:31:47,360 --> 01:31:48,239
where
2309
01:31:48,239 --> 01:31:50,800
age has a caps and now
2310
01:31:50,800 --> 01:31:54,239
has n caps so this is also an example
2311
01:31:54,239 --> 01:31:56,960
of a pascal case variable let's say my
2312
01:31:56,960 --> 01:31:58,960
age now equal to 30
2313
01:31:58,960 --> 01:32:01,520
i'll print each now
2314
01:32:01,520 --> 01:32:03,360
it returns me
2315
01:32:03,360 --> 01:32:04,560
30.
2316
01:32:04,560 --> 01:32:07,360
now one thing to note here is if i write
2317
01:32:07,360 --> 01:32:09,760
let's say age now
2318
01:32:09,760 --> 01:32:12,080
if i do this it gives me an error
2319
01:32:12,080 --> 01:32:14,880
because your variables names are case
2320
01:32:14,880 --> 01:32:16,719
sensitive so we haven't created any
2321
01:32:16,719 --> 01:32:18,159
variable which
2322
01:32:18,159 --> 01:32:22,560
has small a and small n in each now
2323
01:32:22,560 --> 01:32:24,800
okay
2324
01:32:24,800 --> 01:32:25,760
now
2325
01:32:25,760 --> 01:32:27,440
the third way to write a multi-word
2326
01:32:27,440 --> 01:32:30,560
variable is called as snake case
2327
01:32:30,560 --> 01:32:31,760
so
2328
01:32:31,760 --> 01:32:34,480
if you want i can
2329
01:32:34,960 --> 01:32:36,239
give a comment
2330
01:32:36,239 --> 01:32:40,159
saying snake case
2331
01:32:40,159 --> 01:32:42,159
so here each word is separated by an
2332
01:32:42,159 --> 01:32:44,000
underscore character
2333
01:32:44,000 --> 01:32:46,159
so let's say i have a multi-word
2334
01:32:46,159 --> 01:32:49,440
variable which is city underscore
2335
01:32:49,440 --> 01:32:51,840
address
2336
01:32:53,199 --> 01:32:55,199
and let's say the city is
2337
01:32:55,199 --> 01:32:57,760
new york
2338
01:32:58,719 --> 01:33:01,920
now if i print it
2339
01:33:03,280 --> 01:33:05,440
you have new york so this is an example
2340
01:33:05,440 --> 01:33:07,679
of a sneak case where
2341
01:33:07,679 --> 01:33:09,920
each word has a
2342
01:33:09,920 --> 01:33:11,840
underscore in between you can have
2343
01:33:11,840 --> 01:33:13,600
multiple underscore between each word
2344
01:33:13,600 --> 01:33:16,639
suppose i write my
2345
01:33:16,639 --> 01:33:18,400
current
2346
01:33:18,400 --> 01:33:20,000
age
2347
01:33:20,000 --> 01:33:22,800
equal to 35 let's say
2348
01:33:22,800 --> 01:33:25,280
so i have
2349
01:33:25,360 --> 01:33:28,400
my current each
2350
01:33:28,400 --> 01:33:30,080
i run it
2351
01:33:30,080 --> 01:33:31,840
okay there is some mistake here i have
2352
01:33:31,840 --> 01:33:33,280
missed an r
2353
01:33:33,280 --> 01:33:36,400
you see here it says 35 so this is also
2354
01:33:36,400 --> 01:33:38,159
an example of a snake case where each
2355
01:33:38,159 --> 01:33:40,560
word is separated by an underscore so
2356
01:33:40,560 --> 01:33:43,199
here we have two underscores
2357
01:33:43,199 --> 01:33:45,600
all right now let me go to the top and
2358
01:33:45,600 --> 01:33:48,880
let me brief you what we did so first we
2359
01:33:48,880 --> 01:33:50,400
saw an
2360
01:33:50,400 --> 01:33:51,760
integer
2361
01:33:51,760 --> 01:33:54,080
value variable
2362
01:33:54,080 --> 01:33:56,960
we saw how you can check the
2363
01:33:56,960 --> 01:33:59,280
data type of the variable using the type
2364
01:33:59,280 --> 01:34:01,120
function then we explored about the id
2365
01:34:01,120 --> 01:34:02,719
function
2366
01:34:02,719 --> 01:34:04,800
we saw how you can
2367
01:34:04,800 --> 01:34:06,320
pass in
2368
01:34:06,320 --> 01:34:08,480
variables or multiple variables in a
2369
01:34:08,480 --> 01:34:10,480
single line
2370
01:34:10,480 --> 01:34:11,600
then
2371
01:34:11,600 --> 01:34:13,840
we saw how you can assign the same value
2372
01:34:13,840 --> 01:34:16,400
to three variables in one line
2373
01:34:16,400 --> 01:34:17,920
now this is another method to do the
2374
01:34:17,920 --> 01:34:20,000
same operation
2375
01:34:20,000 --> 01:34:20,880
now
2376
01:34:20,880 --> 01:34:23,040
then we looked at the
2377
01:34:23,040 --> 01:34:25,600
different rules for naming variables
2378
01:34:25,600 --> 01:34:27,440
your variables can start only with a
2379
01:34:27,440 --> 01:34:30,480
letter or an underscore
2380
01:34:30,480 --> 01:34:33,440
and that letter can be followed up with
2381
01:34:33,440 --> 01:34:35,280
numbers as well and you can have
2382
01:34:35,280 --> 01:34:37,920
similarly numbers after
2383
01:34:37,920 --> 01:34:40,560
underscore you can also have characters
2384
01:34:40,560 --> 01:34:42,400
after underscore
2385
01:34:42,400 --> 01:34:45,440
you can have multiple underscores
2386
01:34:45,440 --> 01:34:47,440
but your variable name cannot start with
2387
01:34:47,440 --> 01:34:49,840
a number
2388
01:34:49,840 --> 01:34:52,560
and it cannot have special characters
2389
01:34:52,560 --> 01:34:53,440
like
2390
01:34:53,440 --> 01:34:56,000
at the rate or
2391
01:34:56,000 --> 01:34:58,800
dollar symbol
2392
01:34:59,119 --> 01:35:02,000
these will throw an error
2393
01:35:02,000 --> 01:35:03,360
now you can see
2394
01:35:03,360 --> 01:35:05,679
your variable names are case sensitives
2395
01:35:05,679 --> 01:35:07,520
so here we defined car in three
2396
01:35:07,520 --> 01:35:09,199
different ways
2397
01:35:09,199 --> 01:35:12,320
and all the three
2398
01:35:12,800 --> 01:35:15,119
give us different results
2399
01:35:15,119 --> 01:35:17,760
another important thing we saw was
2400
01:35:17,760 --> 01:35:20,560
you cannot use
2401
01:35:20,560 --> 01:35:23,440
keywords python keywords as variable
2402
01:35:23,440 --> 01:35:28,000
names we saw break for and try
2403
01:35:28,000 --> 01:35:30,880
now we perform some
2404
01:35:30,880 --> 01:35:34,239
basic mathematical operations we also
2405
01:35:34,239 --> 01:35:35,280
saw how
2406
01:35:35,280 --> 01:35:36,800
you can perform some extraction
2407
01:35:36,800 --> 01:35:40,639
operation using string values in python
2408
01:35:40,639 --> 01:35:43,280
then we saw another way to
2409
01:35:43,280 --> 01:35:45,199
assign the values of a list to different
2410
01:35:45,199 --> 01:35:47,600
variables
2411
01:35:47,600 --> 01:35:49,679
we saw how you can use the variable name
2412
01:35:49,679 --> 01:35:51,920
in a print statement
2413
01:35:51,920 --> 01:35:54,159
then we saw how
2414
01:35:54,159 --> 01:35:57,440
you cannot add an integer
2415
01:35:57,440 --> 01:35:59,280
with a string
2416
01:35:59,280 --> 01:36:01,280
we explored the camel case
2417
01:36:01,280 --> 01:36:02,560
then we saw
2418
01:36:02,560 --> 01:36:04,000
how you can
2419
01:36:04,000 --> 01:36:06,159
write multi-word variable using pascal
2420
01:36:06,159 --> 01:36:08,639
case and finally we looked at
2421
01:36:08,639 --> 01:36:11,440
snake keys
2422
01:36:11,920 --> 01:36:13,360
so in python
2423
01:36:13,360 --> 01:36:14,960
every value
2424
01:36:14,960 --> 01:36:16,800
has a certain data type
2425
01:36:16,800 --> 01:36:18,800
the variables in python hold an object
2426
01:36:18,800 --> 01:36:20,560
of a particular data type
2427
01:36:20,560 --> 01:36:22,880
since everything is an object in python
2428
01:36:22,880 --> 01:36:24,719
data types are actually classes and
2429
01:36:24,719 --> 01:36:27,280
variables and instances of these classes
2430
01:36:27,280 --> 01:36:29,280
now there are various data types in
2431
01:36:29,280 --> 01:36:30,639
python
2432
01:36:30,639 --> 01:36:32,719
first we have the numeric types which
2433
01:36:32,719 --> 01:36:35,360
are essentially numbers so under numeric
2434
01:36:35,360 --> 01:36:37,199
types we have integers floating point
2435
01:36:37,199 --> 01:36:40,560
numbers and complex numbers
2436
01:36:40,719 --> 01:36:43,040
then we have x type data which are in
2437
01:36:43,040 --> 01:36:45,199
the form of strings
2438
01:36:45,199 --> 01:36:46,960
the third type of data that is available
2439
01:36:46,960 --> 01:36:49,199
in python are called as sequence types
2440
01:36:49,199 --> 01:36:52,960
which consists of less tuples and range
2441
01:36:52,960 --> 01:36:54,560
we have already seen about lists and
2442
01:36:54,560 --> 01:36:57,920
tuples in our previous video
2443
01:36:58,080 --> 01:37:00,159
and in python we also have dictionary
2444
01:37:00,159 --> 01:37:02,400
objects as mapping data types
2445
01:37:02,400 --> 01:37:04,880
in the previous video you saw how to map
2446
01:37:04,880 --> 01:37:09,040
key value pairs using dictionaries
2447
01:37:09,360 --> 01:37:10,800
next
2448
01:37:10,800 --> 01:37:13,440
we have set data type now sets are
2449
01:37:13,440 --> 01:37:16,880
unordered collection of unique items
2450
01:37:16,880 --> 01:37:19,360
python also supports boolean data types
2451
01:37:19,360 --> 01:37:21,280
and finally we have
2452
01:37:21,280 --> 01:37:23,840
binary types of data which include bytes
2453
01:37:23,840 --> 01:37:26,880
byte array and memory view
2454
01:37:26,880 --> 01:37:28,400
now let's head over to the jupyter
2455
01:37:28,400 --> 01:37:30,400
notebook where we will learn about these
2456
01:37:30,400 --> 01:37:31,920
data types in detail
2457
01:37:31,920 --> 01:37:34,000
so here i am on my jupyter notebook and
2458
01:37:34,000 --> 01:37:36,000
i have titled my notebook as data types
2459
01:37:36,000 --> 01:37:38,159
in python rec is for recording
2460
01:37:38,159 --> 01:37:40,480
so let's first start with numeric data
2461
01:37:40,480 --> 01:37:41,840
types
2462
01:37:41,840 --> 01:37:44,159
i'll give a comment using
2463
01:37:44,159 --> 01:37:46,159
a hash tag
2464
01:37:46,159 --> 01:37:48,960
i'll write
2465
01:37:48,960 --> 01:37:50,080
numeric
2466
01:37:50,080 --> 01:37:52,239
data type
2467
01:37:52,239 --> 01:37:54,000
okay
2468
01:37:54,000 --> 01:37:57,040
let me define a variable called num
2469
01:37:57,040 --> 01:38:01,119
in which i'll store a value of seven
2470
01:38:01,119 --> 01:38:04,960
now let me go ahead and print
2471
01:38:04,960 --> 01:38:07,360
the data type of
2472
01:38:07,360 --> 01:38:09,679
my variable num so for that i am using
2473
01:38:09,679 --> 01:38:10,960
my
2474
01:38:10,960 --> 01:38:13,040
function as type
2475
01:38:13,040 --> 01:38:14,800
if i
2476
01:38:14,800 --> 01:38:16,880
run this using shift plus enter you can
2477
01:38:16,880 --> 01:38:19,199
see here it tells me that
2478
01:38:19,199 --> 01:38:22,159
num variable is holding a value of type
2479
01:38:22,159 --> 01:38:24,000
integer
2480
01:38:24,000 --> 01:38:26,320
now you can also
2481
01:38:26,320 --> 01:38:28,639
use a print statement
2482
01:38:28,639 --> 01:38:31,600
i can write something like print
2483
01:38:31,600 --> 01:38:33,440
num
2484
01:38:33,440 --> 01:38:34,960
comma
2485
01:38:34,960 --> 01:38:38,800
i can give a message as ease of
2486
01:38:38,800 --> 01:38:40,480
type
2487
01:38:40,480 --> 01:38:42,159
i'll give another comma and then i'll
2488
01:38:42,159 --> 01:38:44,639
use my function type of
2489
01:38:44,639 --> 01:38:45,840
num
2490
01:38:45,840 --> 01:38:47,679
let's see the output
2491
01:38:47,679 --> 01:38:49,679
you can see it here
2492
01:38:49,679 --> 01:38:50,880
it
2493
01:38:50,880 --> 01:38:51,920
first
2494
01:38:51,920 --> 01:38:53,760
gets the value that is stored in the
2495
01:38:53,760 --> 01:38:56,000
variable num so 7
2496
01:38:56,000 --> 01:38:57,920
is of type and then it prints the
2497
01:38:57,920 --> 01:39:00,480
classes of type class integer
2498
01:39:00,480 --> 01:39:02,400
all right
2499
01:39:02,400 --> 01:39:04,639
i've just increased my
2500
01:39:04,639 --> 01:39:07,199
size of the notebook
2501
01:39:07,199 --> 01:39:08,400
next
2502
01:39:08,400 --> 01:39:10,320
let's declare
2503
01:39:10,320 --> 01:39:12,080
the same variable num and this time
2504
01:39:12,080 --> 01:39:16,239
we'll store the value as let's say 10.8
2505
01:39:16,239 --> 01:39:18,400
now you can see here there's a point
2506
01:39:18,400 --> 01:39:19,760
which means
2507
01:39:19,760 --> 01:39:22,400
this is a decimal
2508
01:39:22,400 --> 01:39:24,800
or a floating value if you can check the
2509
01:39:24,800 --> 01:39:27,920
type of num it gives us
2510
01:39:27,920 --> 01:39:31,920
num is holding a value of type float
2511
01:39:31,920 --> 01:39:34,320
now let's say
2512
01:39:34,320 --> 01:39:36,639
i'll take a negative number let's say
2513
01:39:36,639 --> 01:39:41,600
minus 10.8 and we'll see the type of
2514
01:39:41,600 --> 01:39:43,440
num
2515
01:39:43,440 --> 01:39:48,719
again minus 10.8 is also of type float
2516
01:39:48,719 --> 01:39:51,360
now let's say i'll declare
2517
01:39:51,360 --> 01:39:54,719
a complex variable let's say
2518
01:39:54,719 --> 01:39:55,600
8
2519
01:39:55,600 --> 01:39:56,480
plus
2520
01:39:56,480 --> 01:39:57,520
6
2521
01:39:57,520 --> 01:39:58,639
j
2522
01:39:58,639 --> 01:39:59,920
now this is
2523
01:39:59,920 --> 01:40:02,239
a complex number
2524
01:40:02,239 --> 01:40:04,960
which is again of type
2525
01:40:04,960 --> 01:40:07,440
numeric data type
2526
01:40:07,440 --> 01:40:09,840
if i want to check the
2527
01:40:09,840 --> 01:40:12,320
data type of num it gives me complex
2528
01:40:12,320 --> 01:40:14,800
because 8 plus 6 j is of
2529
01:40:14,800 --> 01:40:17,199
complex data type
2530
01:40:17,199 --> 01:40:18,320
okay
2531
01:40:18,320 --> 01:40:20,800
now if you want to extract the
2532
01:40:20,800 --> 01:40:22,800
real and the imaginary
2533
01:40:22,800 --> 01:40:25,440
parts of a complex number you can do it
2534
01:40:25,440 --> 01:40:26,800
something like this
2535
01:40:26,800 --> 01:40:28,800
you need to pass in the variable name
2536
01:40:28,800 --> 01:40:30,400
followed by a dot
2537
01:40:30,400 --> 01:40:31,679
and
2538
01:40:31,679 --> 01:40:33,199
the attribute name
2539
01:40:33,199 --> 01:40:35,600
is real to extract the
2540
01:40:35,600 --> 01:40:37,520
real part of the complex number if i run
2541
01:40:37,520 --> 01:40:39,040
this you can see here it has returned
2542
01:40:39,040 --> 01:40:40,400
the real part of the complex number
2543
01:40:40,400 --> 01:40:43,520
which is 8. now if you want to print the
2544
01:40:43,520 --> 01:40:46,960
imaginary part you can use num dot
2545
01:40:46,960 --> 01:40:49,600
imeg which is for imaginary if i run
2546
01:40:49,600 --> 01:40:51,840
this there you go it gives us the
2547
01:40:51,840 --> 01:40:55,040
imaginary part which is 6
2548
01:40:55,040 --> 01:40:56,320
okay
2549
01:40:56,320 --> 01:40:58,239
now in python you can also perform
2550
01:40:58,239 --> 01:41:00,159
conversion of data types so you can
2551
01:41:00,159 --> 01:41:02,000
convert between different data types by
2552
01:41:02,000 --> 01:41:04,480
using type conversion functions such as
2553
01:41:04,480 --> 01:41:07,920
end float str etc
2554
01:41:07,920 --> 01:41:10,080
so let me define a
2555
01:41:10,080 --> 01:41:12,239
variable called num
2556
01:41:12,239 --> 01:41:15,600
which has a value 7.8
2557
01:41:15,600 --> 01:41:16,560
now
2558
01:41:16,560 --> 01:41:18,960
what i am going to do is
2559
01:41:18,960 --> 01:41:22,719
i'm going to create a variable called b
2560
01:41:22,719 --> 01:41:26,000
which is going to store the
2561
01:41:27,119 --> 01:41:30,080
integer part of num which means
2562
01:41:30,080 --> 01:41:32,880
initially i had defined num as 7.8 which
2563
01:41:32,880 --> 01:41:35,199
is a floating point number i want to
2564
01:41:35,199 --> 01:41:38,400
convert this num to integer type now
2565
01:41:38,400 --> 01:41:39,840
and
2566
01:41:39,840 --> 01:41:42,320
let me go ahead and
2567
01:41:42,320 --> 01:41:44,719
print
2568
01:41:44,800 --> 01:41:49,119
my b and i also print
2569
01:41:49,119 --> 01:41:52,239
the type of num
2570
01:41:52,800 --> 01:41:54,800
and
2571
01:41:54,800 --> 01:41:57,440
i'll then print
2572
01:41:57,440 --> 01:41:59,040
the type
2573
01:41:59,040 --> 01:42:00,800
b
2574
01:42:00,800 --> 01:42:01,760
let's
2575
01:42:01,760 --> 01:42:04,800
run this and see the result okay
2576
01:42:04,800 --> 01:42:07,679
so if you see here clearly
2577
01:42:07,679 --> 01:42:09,040
when i see
2578
01:42:09,040 --> 01:42:12,239
print b it has returned me 7 which is of
2579
01:42:12,239 --> 01:42:14,800
type integer so it has truncated the
2580
01:42:14,800 --> 01:42:16,800
decimal part
2581
01:42:16,800 --> 01:42:20,000
present in 7.8
2582
01:42:20,000 --> 01:42:22,639
again if i print type of num so num is
2583
01:42:22,639 --> 01:42:25,679
holding 7.8 it is of type float after
2584
01:42:25,679 --> 01:42:28,480
converting the float value into integer
2585
01:42:28,480 --> 01:42:32,000
it has given me int
2586
01:42:32,000 --> 01:42:34,320
okay
2587
01:42:36,159 --> 01:42:38,159
similarly
2588
01:42:38,159 --> 01:42:40,159
let's say we have another
2589
01:42:40,159 --> 01:42:41,360
number
2590
01:42:41,360 --> 01:42:44,000
minus 90.5
2591
01:42:44,000 --> 01:42:45,520
and
2592
01:42:45,520 --> 01:42:48,880
if i were to check the
2593
01:42:49,760 --> 01:42:50,960
type
2594
01:42:50,960 --> 01:42:54,880
i would write print type of num
2595
01:42:54,880 --> 01:42:58,880
then let's convert
2596
01:42:58,880 --> 01:43:00,880
this to integer
2597
01:43:00,880 --> 01:43:02,560
so i'll write print
2598
01:43:02,560 --> 01:43:05,920
in top num let's see the results okay so
2599
01:43:05,920 --> 01:43:09,360
initially minus 90.5 is of type float
2600
01:43:09,360 --> 01:43:11,679
after we converted
2601
01:43:11,679 --> 01:43:12,639
our
2602
01:43:12,639 --> 01:43:13,440
num
2603
01:43:13,440 --> 01:43:15,840
to integer it gives us minus 90 so it
2604
01:43:15,840 --> 01:43:19,920
has truncated the decimal point part
2605
01:43:22,000 --> 01:43:25,159
all right
2606
01:43:27,440 --> 01:43:29,199
now let's say i'll create another
2607
01:43:29,199 --> 01:43:32,560
variable let's say c equal to
2608
01:43:32,560 --> 01:43:35,679
let's say i'll write float
2609
01:43:35,679 --> 01:43:37,520
of
2610
01:43:37,520 --> 01:43:39,360
b
2611
01:43:39,360 --> 01:43:40,960
now if you remember
2612
01:43:40,960 --> 01:43:41,920
our b
2613
01:43:41,920 --> 01:43:46,000
holds a value of 7 if i were to convert
2614
01:43:46,000 --> 01:43:48,159
b to float again
2615
01:43:48,159 --> 01:43:50,840
i can use the float type conversion
2616
01:43:50,840 --> 01:43:54,159
function and let's go ahead and print
2617
01:43:54,159 --> 01:43:55,119
c
2618
01:43:55,119 --> 01:43:58,080
if i run this you can see here
2619
01:43:58,080 --> 01:43:59,280
previously
2620
01:43:59,280 --> 01:44:02,159
b was holding an integer of 7 now it has
2621
01:44:02,159 --> 01:44:03,760
converted that integer into a float
2622
01:44:03,760 --> 01:44:06,800
value so it is 7.0 now
2623
01:44:06,800 --> 01:44:09,040
okay
2624
01:44:09,280 --> 01:44:11,360
now moving ahead
2625
01:44:11,360 --> 01:44:13,840
let's see
2626
01:44:13,840 --> 01:44:15,679
about
2627
01:44:15,679 --> 01:44:18,560
boolean data types
2628
01:44:18,560 --> 01:44:22,560
so if i have a variable let's say
2629
01:44:22,560 --> 01:44:23,440
r
2630
01:44:23,440 --> 01:44:25,280
equal to
2631
01:44:25,280 --> 01:44:27,920
let's say it has a value called true
2632
01:44:27,920 --> 01:44:31,840
which is a type of boolean value
2633
01:44:31,840 --> 01:44:34,639
and let me go ahead and print the type
2634
01:44:34,639 --> 01:44:35,920
of r
2635
01:44:35,920 --> 01:44:37,040
it gives me
2636
01:44:37,040 --> 01:44:39,199
boolean
2637
01:44:39,199 --> 01:44:41,840
now let's say i'll write
2638
01:44:41,840 --> 01:44:45,119
num greater than c
2639
01:44:45,119 --> 01:44:46,320
let's
2640
01:44:46,320 --> 01:44:49,440
go to the top if you see here
2641
01:44:49,440 --> 01:44:53,360
num is holding a value of minus 90.5
2642
01:44:53,360 --> 01:44:56,960
and my c is holding a value of 7.0 i am
2643
01:44:56,960 --> 01:44:58,000
checking
2644
01:44:58,000 --> 01:45:00,560
if num is greater than c if i run this
2645
01:45:00,560 --> 01:45:03,280
it gives me false because
2646
01:45:03,280 --> 01:45:07,040
minus 90.5 is less than 7.0 and not
2647
01:45:07,040 --> 01:45:10,159
greater than 7.0
2648
01:45:10,159 --> 01:45:12,560
suppose
2649
01:45:12,560 --> 01:45:14,480
i create a variable
2650
01:45:14,480 --> 01:45:16,880
boolean or b double o l
2651
01:45:16,880 --> 01:45:18,880
and i'll store that value
2652
01:45:18,880 --> 01:45:20,639
num greater than
2653
01:45:20,639 --> 01:45:21,679
c
2654
01:45:21,679 --> 01:45:23,520
and let's
2655
01:45:23,520 --> 01:45:25,920
print
2656
01:45:26,080 --> 01:45:29,719
the type of boolean
2657
01:45:32,159 --> 01:45:34,080
i'll use type
2658
01:45:34,080 --> 01:45:36,639
of my variable that is bull
2659
01:45:36,639 --> 01:45:39,119
let's run it you can see it here it
2660
01:45:39,119 --> 01:45:40,800
tells us that
2661
01:45:40,800 --> 01:45:43,440
this particular operation is now holding
2662
01:45:43,440 --> 01:45:46,080
a data type of boolean
2663
01:45:46,080 --> 01:45:47,920
if you are getting confused we can
2664
01:45:47,920 --> 01:45:49,760
change the
2665
01:45:49,760 --> 01:45:52,560
variable here let's say i'll write
2666
01:45:52,560 --> 01:45:55,600
b underscore bull
2667
01:45:55,600 --> 01:45:57,040
and similarly i'll write here b
2668
01:45:57,040 --> 01:45:59,119
underscore bull
2669
01:45:59,119 --> 01:46:01,280
let's run it
2670
01:46:01,280 --> 01:46:04,080
you get the same thing
2671
01:46:04,080 --> 01:46:05,199
all right
2672
01:46:05,199 --> 01:46:07,119
now moving ahead
2673
01:46:07,119 --> 01:46:08,159
let's
2674
01:46:08,159 --> 01:46:09,840
explore how
2675
01:46:09,840 --> 01:46:12,719
string data types work in python
2676
01:46:12,719 --> 01:46:15,920
i'll give a comment as
2677
01:46:15,920 --> 01:46:18,960
string data types
2678
01:46:19,360 --> 01:46:20,639
scroll down
2679
01:46:20,639 --> 01:46:21,920
ok
2680
01:46:21,920 --> 01:46:24,480
so string data types or string values in
2681
01:46:24,480 --> 01:46:27,760
python are passed within
2682
01:46:27,760 --> 01:46:30,239
quotations suppose i have a string
2683
01:46:30,239 --> 01:46:32,000
called str1
2684
01:46:32,000 --> 01:46:34,960
and it has a value let's say
2685
01:46:34,960 --> 01:46:37,119
python programming so this is a text
2686
01:46:37,119 --> 01:46:38,880
value
2687
01:46:38,880 --> 01:46:42,000
i have another string let's say str2
2688
01:46:42,000 --> 01:46:44,719
which has a value
2689
01:46:44,719 --> 01:46:47,280
let's say simply learn
2690
01:46:47,280 --> 01:46:48,880
now
2691
01:46:48,880 --> 01:46:50,800
let's run this
2692
01:46:50,800 --> 01:46:51,760
okay
2693
01:46:51,760 --> 01:46:55,199
now if i were to check the type
2694
01:46:55,199 --> 01:46:57,119
of str1
2695
01:46:57,119 --> 01:46:59,760
it should give me string so str here is
2696
01:46:59,760 --> 01:47:02,080
a string
2697
01:47:02,080 --> 01:47:04,239
now you can also perform extraction
2698
01:47:04,239 --> 01:47:06,880
operations from the string suppose you
2699
01:47:06,880 --> 01:47:09,840
want to extract
2700
01:47:10,159 --> 01:47:12,159
the value that is present in my first
2701
01:47:12,159 --> 01:47:15,920
string that is str1 at index number five
2702
01:47:15,920 --> 01:47:17,679
let's run it
2703
01:47:17,679 --> 01:47:19,840
okay there is some error here
2704
01:47:19,840 --> 01:47:23,760
okay so this should be str1 and not strl
2705
01:47:23,760 --> 01:47:25,440
let's run it again
2706
01:47:25,440 --> 01:47:27,600
it returns us n
2707
01:47:27,600 --> 01:47:30,560
now let's count what is present at the
2708
01:47:30,560 --> 01:47:31,920
fifth index
2709
01:47:31,920 --> 01:47:33,760
so from our previous introduction to
2710
01:47:33,760 --> 01:47:36,880
python video you might be aware that in
2711
01:47:36,880 --> 01:47:40,239
python the index number starts with 0 so
2712
01:47:40,239 --> 01:47:41,760
if you count
2713
01:47:41,760 --> 01:47:44,239
p is at index 0
2714
01:47:44,239 --> 01:47:46,080
y is at index 1
2715
01:47:46,080 --> 01:47:50,639
t is at index 2 h is at index 3 o is at
2716
01:47:50,639 --> 01:47:52,719
index four and then
2717
01:47:52,719 --> 01:47:55,760
n is at index five so hence str one of
2718
01:47:55,760 --> 01:47:57,760
five returns and
2719
01:47:57,760 --> 01:48:00,800
similarly if you can
2720
01:48:01,600 --> 01:48:03,280
check for
2721
01:48:03,280 --> 01:48:05,199
other string that is string two let's
2722
01:48:05,199 --> 01:48:06,639
say i want to see the value that is
2723
01:48:06,639 --> 01:48:07,840
present
2724
01:48:07,840 --> 01:48:09,360
at minus
2725
01:48:09,360 --> 01:48:11,600
four
2726
01:48:12,400 --> 01:48:13,920
let's run it
2727
01:48:13,920 --> 01:48:16,080
it gives us e
2728
01:48:16,080 --> 01:48:18,000
now if you use a negative indexing
2729
01:48:18,000 --> 01:48:21,600
operation it will start counting from
2730
01:48:21,600 --> 01:48:24,159
the backward position so here
2731
01:48:24,159 --> 01:48:27,360
n is at minus 1 r is at minus 2 then you
2732
01:48:27,360 --> 01:48:28,560
have a
2733
01:48:28,560 --> 01:48:32,239
at minus 3 and e is present at -4 so
2734
01:48:32,239 --> 01:48:36,239
hence str2 of minus 4 returns e
2735
01:48:36,239 --> 01:48:38,480
cool
2736
01:48:39,040 --> 01:48:41,360
now you can also perform slicing
2737
01:48:41,360 --> 01:48:42,480
operation
2738
01:48:42,480 --> 01:48:43,760
suppose
2739
01:48:43,760 --> 01:48:47,199
i write str str1
2740
01:48:47,199 --> 01:48:49,280
i want everything that is present
2741
01:48:49,280 --> 01:48:51,760
between the zeroth index to
2742
01:48:51,760 --> 01:48:54,639
the fifth index
2743
01:48:55,199 --> 01:48:56,800
i'll run it
2744
01:48:56,800 --> 01:48:59,040
it gives me
2745
01:48:59,040 --> 01:49:02,320
h pytho so if you check
2746
01:49:02,320 --> 01:49:04,320
in string one
2747
01:49:04,320 --> 01:49:05,920
it will start counting from the zeroth
2748
01:49:05,920 --> 01:49:08,880
index and go up to
2749
01:49:08,880 --> 01:49:11,119
the fourth index and exclude the fifth
2750
01:49:11,119 --> 01:49:12,080
index
2751
01:49:12,080 --> 01:49:14,560
so in my string 1 if i start counting
2752
01:49:14,560 --> 01:49:18,400
from the 0th index so it is 0 1 2 3 and
2753
01:49:18,400 --> 01:49:20,639
4 so it will
2754
01:49:20,639 --> 01:49:24,560
print from p till o
2755
01:49:24,560 --> 01:49:27,040
and that's the result
2756
01:49:27,040 --> 01:49:28,400
okay
2757
01:49:28,400 --> 01:49:29,920
now you can also use some inbuilt
2758
01:49:29,920 --> 01:49:31,679
functions with string
2759
01:49:31,679 --> 01:49:34,480
suppose i write str2
2760
01:49:34,480 --> 01:49:35,840
dot
2761
01:49:35,840 --> 01:49:37,679
find
2762
01:49:37,679 --> 01:49:41,119
so i want to find the index where my
2763
01:49:41,119 --> 01:49:44,320
m is in simply learn
2764
01:49:44,320 --> 01:49:46,400
so i'll
2765
01:49:46,400 --> 01:49:49,440
write m within quotations and print it
2766
01:49:49,440 --> 01:49:52,159
it returns me 2
2767
01:49:52,159 --> 01:49:53,840
if you check again
2768
01:49:53,840 --> 01:49:56,960
so s is at 0th index 1 is at the first
2769
01:49:56,960 --> 01:49:59,119
index and m is present at the second
2770
01:49:59,119 --> 01:50:00,960
index
2771
01:50:00,960 --> 01:50:03,119
ok
2772
01:50:05,440 --> 01:50:07,199
moving ahead
2773
01:50:07,199 --> 01:50:10,719
let's explore a few sequence data types
2774
01:50:10,719 --> 01:50:12,800
so we'll start with
2775
01:50:12,800 --> 01:50:15,840
list data type
2776
01:50:18,639 --> 01:50:20,480
ok
2777
01:50:20,480 --> 01:50:21,679
now
2778
01:50:21,679 --> 01:50:25,119
first of all let me go ahead and
2779
01:50:25,119 --> 01:50:28,880
declare a variable called split 1
2780
01:50:28,880 --> 01:50:30,480
equal to
2781
01:50:30,480 --> 01:50:34,080
and this will have some
2782
01:50:34,159 --> 01:50:37,440
text let's say this
2783
01:50:37,440 --> 01:50:39,360
is
2784
01:50:39,360 --> 01:50:41,760
orange
2785
01:50:42,560 --> 01:50:44,159
then
2786
01:50:44,159 --> 01:50:45,599
i'm going to create another variable
2787
01:50:45,599 --> 01:50:47,199
called l1
2788
01:50:47,199 --> 01:50:48,960
equal to
2789
01:50:48,960 --> 01:50:52,440
i am going to
2790
01:50:56,639 --> 01:50:58,800
my variable or my
2791
01:50:58,800 --> 01:51:00,719
string that is split 1
2792
01:51:00,719 --> 01:51:03,840
using the split function
2793
01:51:05,679 --> 01:51:08,400
and i'm going to split using the comma
2794
01:51:08,400 --> 01:51:10,560
so you can see here my string has comma
2795
01:51:10,560 --> 01:51:12,320
i'm going to separate each of these
2796
01:51:12,320 --> 01:51:15,440
words with a comma
2797
01:51:15,520 --> 01:51:18,159
and let's print l1
2798
01:51:18,159 --> 01:51:20,719
if i run this you can see it here
2799
01:51:20,719 --> 01:51:24,400
initially my split 1 was of type string
2800
01:51:24,400 --> 01:51:26,800
and now we have converted
2801
01:51:26,800 --> 01:51:29,679
split 1 to a list you can see this this
2802
01:51:29,679 --> 01:51:32,080
is a list and it is present inside
2803
01:51:32,080 --> 01:51:34,560
square brackets to verify
2804
01:51:34,560 --> 01:51:36,639
you can use the type function so i'll
2805
01:51:36,639 --> 01:51:38,719
write type of
2806
01:51:38,719 --> 01:51:40,320
split one
2807
01:51:40,320 --> 01:51:42,400
it should return string you can see it
2808
01:51:42,400 --> 01:51:43,920
here str
2809
01:51:43,920 --> 01:51:46,560
and now if i write
2810
01:51:46,560 --> 01:51:48,800
type of l1
2811
01:51:48,800 --> 01:51:51,119
it gives me a list
2812
01:51:51,119 --> 01:51:53,440
okay
2813
01:51:53,760 --> 01:51:56,320
now let's see a few more examples i'll
2814
01:51:56,320 --> 01:51:58,000
create another list called
2815
01:51:58,000 --> 01:51:59,199
list1
2816
01:51:59,199 --> 01:52:02,719
and let's see it has some values
2817
01:52:02,719 --> 01:52:04,800
the alphabet a
2818
01:52:04,800 --> 01:52:07,280
we have
2819
01:52:07,679 --> 01:52:09,760
the number one which is in terms of
2820
01:52:09,760 --> 01:52:11,760
string again because i have passed in in
2821
01:52:11,760 --> 01:52:12,880
quotes
2822
01:52:12,880 --> 01:52:17,159
let's say i have india
2823
01:52:17,199 --> 01:52:19,360
let's make it consistent with single
2824
01:52:19,360 --> 01:52:21,840
quotations
2825
01:52:21,840 --> 01:52:23,520
and
2826
01:52:23,520 --> 01:52:25,199
let me
2827
01:52:25,199 --> 01:52:28,880
print what is inside list1
2828
01:52:28,880 --> 01:52:31,440
if i run it i get my list
2829
01:52:31,440 --> 01:52:34,639
now you can check the data type
2830
01:52:34,639 --> 01:52:37,599
if i write type of list one it gives me
2831
01:52:37,599 --> 01:52:38,960
it is a type
2832
01:52:38,960 --> 01:52:41,280
list
2833
01:52:41,840 --> 01:52:43,440
now you can also perform certain
2834
01:52:43,440 --> 01:52:45,440
operations on list suppose you want to
2835
01:52:45,440 --> 01:52:48,239
add a few elements to your original list
2836
01:52:48,239 --> 01:52:49,840
for that i'm going to create another
2837
01:52:49,840 --> 01:52:52,080
variable called list 2
2838
01:52:52,080 --> 01:52:54,239
and let's say list2 has some values
2839
01:52:54,239 --> 01:52:56,840
let's say b comma
2840
01:52:56,840 --> 01:52:59,280
2. now
2841
01:52:59,280 --> 01:53:01,199
if i write
2842
01:53:01,199 --> 01:53:03,520
list 1
2843
01:53:03,520 --> 01:53:04,719
plus
2844
01:53:04,719 --> 01:53:07,280
equal to
2845
01:53:07,520 --> 01:53:10,080
list 2
2846
01:53:10,080 --> 01:53:13,360
and let me go ahead and
2847
01:53:13,360 --> 01:53:15,360
print
2848
01:53:15,360 --> 01:53:17,520
list one
2849
01:53:17,520 --> 01:53:19,440
let's see the result
2850
01:53:19,440 --> 01:53:21,840
there you go so our initial list had
2851
01:53:21,840 --> 01:53:24,560
three elements a1 and india and now we
2852
01:53:24,560 --> 01:53:26,880
have merged or concatenated two more
2853
01:53:26,880 --> 01:53:29,599
elements b and two and hence this
2854
01:53:29,599 --> 01:53:33,119
becomes our final list
2855
01:53:33,119 --> 01:53:34,320
okay
2856
01:53:34,320 --> 01:53:35,840
now moving ahead
2857
01:53:35,840 --> 01:53:36,880
let's see
2858
01:53:36,880 --> 01:53:38,880
one more sequence data type that is
2859
01:53:38,880 --> 01:53:42,159
tuple or tuple
2860
01:53:43,840 --> 01:53:45,360
now list and tuples we have already
2861
01:53:45,360 --> 01:53:47,199
covered in our previous video on
2862
01:53:47,199 --> 01:53:49,360
introduction to python if you need to
2863
01:53:49,360 --> 01:53:51,520
brush up what list and tuples are you
2864
01:53:51,520 --> 01:53:54,000
can check the previous video
2865
01:53:54,000 --> 01:53:55,920
so i'm going to create a tuple that is
2866
01:53:55,920 --> 01:53:59,360
top1 and i am going to
2867
01:53:59,360 --> 01:54:01,199
store some values
2868
01:54:01,199 --> 01:54:05,040
tuple you can create using braces
2869
01:54:05,040 --> 01:54:07,360
i have some value let's say a
2870
01:54:07,360 --> 01:54:09,440
b
2871
01:54:09,440 --> 01:54:11,360
c
2872
01:54:11,360 --> 01:54:12,560
d
2873
01:54:12,560 --> 01:54:15,360
and let's say e
2874
01:54:15,520 --> 01:54:18,800
or let's just restrict it to d
2875
01:54:18,800 --> 01:54:23,520
now you can check the type of tup1
2876
01:54:23,520 --> 01:54:28,560
you can see here dup1 is of type tuple
2877
01:54:28,560 --> 01:54:30,239
now
2878
01:54:30,239 --> 01:54:33,679
i'll create another tuple let's say tup2
2879
01:54:33,679 --> 01:54:35,679
and
2880
01:54:35,679 --> 01:54:39,920
it has some values let's say e
2881
01:54:39,920 --> 01:54:41,280
f
2882
01:54:41,280 --> 01:54:43,520
and let's say g
2883
01:54:43,520 --> 01:54:45,119
now
2884
01:54:45,119 --> 01:54:46,400
if i
2885
01:54:46,400 --> 01:54:47,760
write
2886
01:54:47,760 --> 01:54:49,440
dup1
2887
01:54:49,440 --> 01:54:50,719
plus
2888
01:54:50,719 --> 01:54:54,239
equal to dup2
2889
01:54:54,239 --> 01:54:57,360
and if i print
2890
01:54:57,599 --> 01:55:00,639
my tup1
2891
01:55:01,679 --> 01:55:03,520
i get the result
2892
01:55:03,520 --> 01:55:05,679
of a merged
2893
01:55:05,679 --> 01:55:06,719
tuple
2894
01:55:06,719 --> 01:55:09,440
which has values of both top1 and tup2
2895
01:55:09,440 --> 01:55:11,360
you can see here our original tuple had
2896
01:55:11,360 --> 01:55:14,639
abcd and then we merged the second tuple
2897
01:55:14,639 --> 01:55:16,560
and it has
2898
01:55:16,560 --> 01:55:19,520
and it has given us efg as well and
2899
01:55:19,520 --> 01:55:22,880
hence this becomes the final tuple
2900
01:55:22,880 --> 01:55:24,320
you can also perform some basic
2901
01:55:24,320 --> 01:55:26,159
operations on tuple let's say i'll write
2902
01:55:26,159 --> 01:55:29,760
dup1 multiplied by 2.
2903
01:55:29,760 --> 01:55:32,400
you can see the result here
2904
01:55:32,400 --> 01:55:34,560
it has printed
2905
01:55:34,560 --> 01:55:38,560
my original tuple twice
2906
01:55:38,880 --> 01:55:40,639
okay
2907
01:55:40,639 --> 01:55:43,440
now moving ahead
2908
01:55:43,440 --> 01:55:47,280
let's see our mapping data type
2909
01:55:47,280 --> 01:55:50,800
that is dictionary
2910
01:55:51,840 --> 01:55:54,480
again dictionary is also covered
2911
01:55:54,480 --> 01:55:56,320
in our previous
2912
01:55:56,320 --> 01:55:58,560
introduction to python video
2913
01:55:58,560 --> 01:56:01,360
you can also have a look at that video
2914
01:56:01,360 --> 01:56:04,480
now the way to declare a dictionary
2915
01:56:04,480 --> 01:56:05,679
is
2916
01:56:05,679 --> 01:56:07,520
i'll create a variable
2917
01:56:07,520 --> 01:56:09,520
dict 1
2918
01:56:09,520 --> 01:56:12,800
and dictionaries are
2919
01:56:12,800 --> 01:56:15,199
written using curly braces
2920
01:56:15,199 --> 01:56:18,400
and have key value peers
2921
01:56:18,400 --> 01:56:21,520
so i'll write my first key as one and
2922
01:56:21,520 --> 01:56:22,719
let's see
2923
01:56:22,719 --> 01:56:25,520
this dictionary is about animals
2924
01:56:25,520 --> 01:56:28,400
and i have let's say lion
2925
01:56:28,400 --> 01:56:31,119
as my first animal in the dictionary
2926
01:56:31,119 --> 01:56:33,760
then i'll create my second key
2927
01:56:33,760 --> 01:56:37,040
let's see the second animal i have is
2928
01:56:37,040 --> 01:56:39,280
tiger
2929
01:56:39,280 --> 01:56:41,440
and let's see
2930
01:56:41,440 --> 01:56:44,400
my third element
2931
01:56:46,560 --> 01:56:48,639
is
2932
01:56:48,639 --> 01:56:51,040
a dog
2933
01:56:51,280 --> 01:56:53,599
let me run it
2934
01:56:53,599 --> 01:56:55,199
and print
2935
01:56:55,199 --> 01:56:56,400
my dictionary
2936
01:56:56,400 --> 01:56:58,000
you can see this
2937
01:56:58,000 --> 01:57:00,560
we have the dictionary printed
2938
01:57:00,560 --> 01:57:02,719
now if you want to check the type you
2939
01:57:02,719 --> 01:57:04,719
can write type of
2940
01:57:04,719 --> 01:57:06,800
dict 1
2941
01:57:06,800 --> 01:57:08,880
it returns us dict which means
2942
01:57:08,880 --> 01:57:10,400
dictionary
2943
01:57:10,400 --> 01:57:13,840
you can also print the length of
2944
01:57:13,840 --> 01:57:15,840
dict 1
2945
01:57:15,840 --> 01:57:17,599
and it gives us 3 which means our
2946
01:57:17,599 --> 01:57:20,480
dictionary has 3 elements
2947
01:57:20,480 --> 01:57:22,719
ok
2948
01:57:23,199 --> 01:57:24,800
now moving ahead
2949
01:57:24,800 --> 01:57:26,880
we'll explore
2950
01:57:26,880 --> 01:57:30,159
our set data type
2951
01:57:30,880 --> 01:57:33,599
now as we understood that sets are
2952
01:57:33,599 --> 01:57:36,400
unordered collections of
2953
01:57:36,400 --> 01:57:38,960
values
2954
01:57:38,960 --> 01:57:41,520
and these values are always unique
2955
01:57:41,520 --> 01:57:43,520
so i'll write set one
2956
01:57:43,520 --> 01:57:46,480
let me give a space
2957
01:57:46,880 --> 01:57:48,320
equal to
2958
01:57:48,320 --> 01:57:50,480
again sets are written within curly
2959
01:57:50,480 --> 01:57:52,239
braces
2960
01:57:52,239 --> 01:57:53,920
i'll create a set
2961
01:57:53,920 --> 01:57:55,679
let's say it has some duplicate entries
2962
01:57:55,679 --> 01:57:58,000
as well
2963
01:57:58,080 --> 01:58:01,360
and i'll print my set one
2964
01:58:01,360 --> 01:58:03,599
it returns only the unique values so it
2965
01:58:03,599 --> 01:58:05,679
has dropped
2966
01:58:05,679 --> 01:58:08,880
this 5 which was duplicated
2967
01:58:08,880 --> 01:58:11,840
you can check the type of set 1
2968
01:58:11,840 --> 01:58:14,239
it gives us it is of type or the object
2969
01:58:14,239 --> 01:58:17,119
is of type set
2970
01:58:17,119 --> 01:58:17,840
now
2971
01:58:17,840 --> 01:58:19,599
suppose
2972
01:58:19,599 --> 01:58:21,599
i have
2973
01:58:21,599 --> 01:58:24,960
another set called set 2
2974
01:58:24,960 --> 01:58:28,639
which has values let's say
2975
01:58:28,719 --> 01:58:30,000
a
2976
01:58:30,000 --> 01:58:31,760
comma
2977
01:58:31,760 --> 01:58:33,679
b
2978
01:58:33,679 --> 01:58:36,639
and let's say we have some numbers three
2979
01:58:36,639 --> 01:58:38,000
and four
2980
01:58:38,000 --> 01:58:39,599
i'll tell you the operation that i'm
2981
01:58:39,599 --> 01:58:41,920
going to do
2982
01:58:41,920 --> 01:58:44,159
i'm actually going to
2983
01:58:44,159 --> 01:58:46,560
do an or operation
2984
01:58:46,560 --> 01:58:49,840
so i'm going to print
2985
01:58:49,840 --> 01:58:52,320
set 1
2986
01:58:52,320 --> 01:58:54,080
or
2987
01:58:54,080 --> 01:58:55,199
set
2988
01:58:55,199 --> 01:58:56,239
2
2989
01:58:56,239 --> 01:58:59,679
so this is an or operator in python
2990
01:58:59,679 --> 01:59:02,560
let's print the results you can see here
2991
01:59:02,560 --> 01:59:05,119
it has printed
2992
01:59:05,119 --> 01:59:07,119
all the values that are present inside
2993
01:59:07,119 --> 01:59:09,440
set 1 or set 2.
2994
01:59:09,440 --> 01:59:13,040
so in set 1 we have 1 2 3 4 and 5
2995
01:59:13,040 --> 01:59:16,960
and in set 2 we have a and b
2996
01:59:16,960 --> 01:59:19,840
so it has dropped 3 and 4 because these
2997
01:59:19,840 --> 01:59:21,520
are not unique and
2998
01:59:21,520 --> 01:59:25,760
3 and 4 are also present in set 1.
2999
01:59:25,760 --> 01:59:27,840
suppose i were to perform an and
3000
01:59:27,840 --> 01:59:29,520
operation
3001
01:59:29,520 --> 01:59:32,719
i'll write set1
3002
01:59:33,440 --> 01:59:34,800
ampersand
3003
01:59:34,800 --> 01:59:36,560
set 2.
3004
01:59:36,560 --> 01:59:38,560
if i run this
3005
01:59:38,560 --> 01:59:42,880
you can see here it has only printed
3006
01:59:43,199 --> 01:59:45,520
the unique values that are present in
3007
01:59:45,520 --> 01:59:48,239
both set one and set two so three and
3008
01:59:48,239 --> 01:59:49,840
four are the unique values and hence it
3009
01:59:49,840 --> 01:59:52,560
has printed three and four
3010
01:59:52,560 --> 01:59:53,920
let's say
3011
01:59:53,920 --> 01:59:57,280
i'll write set one minus set two
3012
01:59:57,280 --> 01:59:59,119
let's see the results
3013
01:59:59,119 --> 02:00:01,199
it gives us only those values that are
3014
02:00:01,199 --> 02:00:02,480
present
3015
02:00:02,480 --> 02:00:03,360
in
3016
02:00:03,360 --> 02:00:04,960
set 1
3017
02:00:04,960 --> 02:00:08,239
and are not present in set 2 which is
3018
02:00:08,239 --> 02:00:11,239
this
3019
02:00:12,080 --> 02:00:14,000
let's see the result if i write set 2
3020
02:00:14,000 --> 02:00:16,159
minus set 1
3021
02:00:16,159 --> 02:00:17,760
so this will return the unique values
3022
02:00:17,760 --> 02:00:20,080
that are present in set 2 and are not
3023
02:00:20,080 --> 02:00:22,560
present in set 1.
3024
02:00:22,560 --> 02:00:24,320
so a and b are the unique values that
3025
02:00:24,320 --> 02:00:26,000
are present in set two hence it has
3026
02:00:26,000 --> 02:00:29,360
printed as a comma b
3027
02:00:29,360 --> 02:00:31,599
okay
3028
02:00:31,599 --> 02:00:33,040
now
3029
02:00:33,040 --> 02:00:35,679
another data type we had discussed was
3030
02:00:35,679 --> 02:00:37,280
range
3031
02:00:37,280 --> 02:00:39,199
so range is basically a function if i
3032
02:00:39,199 --> 02:00:41,840
write x equal to
3033
02:00:41,840 --> 02:00:43,440
range
3034
02:00:43,440 --> 02:00:44,800
4
3035
02:00:44,800 --> 02:00:46,639
and i'll print x
3036
02:00:46,639 --> 02:00:49,599
it gives us from 0 till 4 because in
3037
02:00:49,599 --> 02:00:51,840
python the first index starts from 0 and
3038
02:00:51,840 --> 02:00:52,960
goes
3039
02:00:52,960 --> 02:00:57,280
till 4 so the range is from 0 to 4.
3040
02:00:57,840 --> 02:01:01,840
now you can also print the type of x
3041
02:01:01,840 --> 02:01:05,520
it returns us a range
3042
02:01:06,840 --> 02:01:09,679
now we'll see
3043
02:01:09,679 --> 02:01:13,040
some binary data types
3044
02:01:14,159 --> 02:01:16,639
binary data types are not very often
3045
02:01:16,639 --> 02:01:18,880
used in python but it is better if you
3046
02:01:18,880 --> 02:01:20,560
know
3047
02:01:20,560 --> 02:01:22,800
binary data types can be written as x
3048
02:01:22,800 --> 02:01:23,840
equal to
3049
02:01:23,840 --> 02:01:26,800
b let's say i have a string
3050
02:01:26,800 --> 02:01:29,199
i'll write within double quotations
3051
02:01:29,199 --> 02:01:31,920
hello world
3052
02:01:33,440 --> 02:01:36,400
and let me go ahead and
3053
02:01:36,400 --> 02:01:37,920
print
3054
02:01:37,920 --> 02:01:40,080
my variable x
3055
02:01:40,080 --> 02:01:41,440
you see here
3056
02:01:41,440 --> 02:01:43,760
we have hello world
3057
02:01:43,760 --> 02:01:45,280
and now
3058
02:01:45,280 --> 02:01:47,760
let's print the type of x
3059
02:01:47,760 --> 02:01:49,599
you see here this is a binary type which
3060
02:01:49,599 --> 02:01:51,520
is bytes
3061
02:01:51,520 --> 02:01:53,040
i'll show you another operation let's
3062
02:01:53,040 --> 02:01:55,920
say you want to extract a specific
3063
02:01:55,920 --> 02:01:57,119
value
3064
02:01:57,119 --> 02:01:59,760
from a particular index
3065
02:01:59,760 --> 02:02:01,040
from this
3066
02:02:01,040 --> 02:02:02,719
binary data
3067
02:02:02,719 --> 02:02:04,560
so again you can use the
3068
02:02:04,560 --> 02:02:06,480
indexing operation
3069
02:02:06,480 --> 02:02:08,880
suppose i write print
3070
02:02:08,880 --> 02:02:10,639
i want to extract the value present at
3071
02:02:10,639 --> 02:02:12,800
the fourth index
3072
02:02:12,800 --> 02:02:15,920
it gives me one one one now this is very
3073
02:02:15,920 --> 02:02:17,040
interesting
3074
02:02:17,040 --> 02:02:18,560
let's
3075
02:02:18,560 --> 02:02:20,480
deep dive into this
3076
02:02:20,480 --> 02:02:22,000
let's check
3077
02:02:22,000 --> 02:02:24,080
what is present at the fourth index of
3078
02:02:24,080 --> 02:02:25,440
my string
3079
02:02:25,440 --> 02:02:28,400
so h is present at the zeroth index
3080
02:02:28,400 --> 02:02:30,560
e is at the first index
3081
02:02:30,560 --> 02:02:33,840
l is at the second index the second l is
3082
02:02:33,840 --> 02:02:37,119
at the third index and we have o
3083
02:02:37,119 --> 02:02:39,280
at the fourth index
3084
02:02:39,280 --> 02:02:41,040
so
3085
02:02:41,040 --> 02:02:43,119
when we are trying to print the index
3086
02:02:43,119 --> 02:02:45,679
position it is actually returning us the
3087
02:02:45,679 --> 02:02:48,239
ascii value of small o
3088
02:02:48,239 --> 02:02:50,239
now here i have a
3089
02:02:50,239 --> 02:02:53,199
doc open which is from ibm documentation
3090
02:02:53,199 --> 02:02:54,719
if you see
3091
02:02:54,719 --> 02:02:56,080
the
3092
02:02:56,080 --> 02:02:59,199
ascii value of small o or lowercase o
3093
02:02:59,199 --> 02:03:00,480
you will find
3094
02:03:00,480 --> 02:03:03,920
that the ascii value is one one one and
3095
02:03:03,920 --> 02:03:06,880
hence it has written us one one one
3096
02:03:06,880 --> 02:03:09,440
let's try for
3097
02:03:09,440 --> 02:03:10,639
another
3098
02:03:10,639 --> 02:03:12,400
index number let's say i want to find
3099
02:03:12,400 --> 02:03:14,400
the value which is present at index
3100
02:03:14,400 --> 02:03:15,760
number
3101
02:03:15,760 --> 02:03:18,239
minus
3102
02:03:19,280 --> 02:03:20,159
2
3103
02:03:20,159 --> 02:03:21,760
and if i run it
3104
02:03:21,760 --> 02:03:23,760
it gives me 108
3105
02:03:23,760 --> 02:03:25,679
so minus 2 means it will start from the
3106
02:03:25,679 --> 02:03:27,360
last index
3107
02:03:27,360 --> 02:03:31,119
so minus 1 is d and minus 2 is l
3108
02:03:31,119 --> 02:03:34,880
let's see what is the ascii value of l
3109
02:03:34,880 --> 02:03:37,280
if you see it here the ascii value of l
3110
02:03:37,280 --> 02:03:39,360
is 108
3111
02:03:39,360 --> 02:03:42,400
and hence it has written as 108. so this
3112
02:03:42,400 --> 02:03:43,360
is how
3113
02:03:43,360 --> 02:03:47,119
the binary data types work in python
3114
02:03:47,119 --> 02:03:50,560
now let's convert some
3115
02:03:50,560 --> 02:03:53,119
sequence objects to other type of
3116
02:03:53,119 --> 02:03:57,440
objects let's say i have a string
3117
02:03:57,440 --> 02:04:00,000
called s which has
3118
02:04:00,000 --> 02:04:01,599
something like
3119
02:04:01,599 --> 02:04:04,480
messy is from
3120
02:04:04,480 --> 02:04:07,040
argentina
3121
02:04:07,040 --> 02:04:09,119
now you can check
3122
02:04:09,119 --> 02:04:10,480
the type of s
3123
02:04:10,480 --> 02:04:12,960
fair enough it tells us that messi is
3124
02:04:12,960 --> 02:04:16,000
from argentina is of type string
3125
02:04:16,000 --> 02:04:17,199
now
3126
02:04:17,199 --> 02:04:19,599
what i'm going to do is i'll use my list
3127
02:04:19,599 --> 02:04:20,560
function
3128
02:04:20,560 --> 02:04:22,960
and pass in my variable that is s so
3129
02:04:22,960 --> 02:04:26,480
that it converts s to a list
3130
02:04:26,480 --> 02:04:29,440
and let me go ahead and print my l
3131
02:04:29,440 --> 02:04:30,880
you can see here
3132
02:04:30,880 --> 02:04:33,599
my string s has now been converted into
3133
02:04:33,599 --> 02:04:34,719
a list
3134
02:04:34,719 --> 02:04:37,440
and this is the result
3135
02:04:37,440 --> 02:04:39,280
see here these are all
3136
02:04:39,280 --> 02:04:41,679
list elements present inside square
3137
02:04:41,679 --> 02:04:44,000
brackets
3138
02:04:44,000 --> 02:04:46,239
now if you want you can check the type
3139
02:04:46,239 --> 02:04:47,840
of file
3140
02:04:47,840 --> 02:04:50,000
it should return us
3141
02:04:50,000 --> 02:04:51,599
list
3142
02:04:51,599 --> 02:04:54,719
and you can see it here
3143
02:04:54,800 --> 02:04:56,639
now suppose
3144
02:04:56,639 --> 02:04:58,079
i have
3145
02:04:58,079 --> 02:05:01,599
another list let's say l1 which has
3146
02:05:01,599 --> 02:05:02,960
some
3147
02:05:02,960 --> 02:05:04,880
numbers let's say
3148
02:05:04,880 --> 02:05:07,280
one 15
3149
02:05:07,280 --> 02:05:08,320
3
3150
02:05:08,320 --> 02:05:09,199
6
3151
02:05:09,199 --> 02:05:10,079
0
3152
02:05:10,079 --> 02:05:12,639
8 4 and 3
3153
02:05:12,639 --> 02:05:15,760
let's see we also have one
3154
02:05:15,920 --> 02:05:18,560
okay so this is a list you can check
3155
02:05:18,560 --> 02:05:21,520
type of l1 fair enough it tells us that
3156
02:05:21,520 --> 02:05:23,599
this is a list
3157
02:05:23,599 --> 02:05:26,239
now i want to convert this list l1 to a
3158
02:05:26,239 --> 02:05:27,280
set
3159
02:05:27,280 --> 02:05:30,960
now as we know that set only contains
3160
02:05:30,960 --> 02:05:33,520
unordered unique elements
3161
02:05:33,520 --> 02:05:36,239
so if i write s equal to
3162
02:05:36,239 --> 02:05:38,400
i'll use the set function and then i'll
3163
02:05:38,400 --> 02:05:41,520
pass in my list variable that is l1
3164
02:05:41,520 --> 02:05:43,679
and let's print s
3165
02:05:43,679 --> 02:05:46,320
it only returns me the unique values
3166
02:05:46,320 --> 02:05:49,040
present in the list so it has dropped
3167
02:05:49,040 --> 02:05:49,760
the
3168
02:05:49,760 --> 02:05:51,679
duplicate elements such as
3169
02:05:51,679 --> 02:05:54,000
three and one
3170
02:05:54,000 --> 02:05:56,480
now you can check the type
3171
02:05:56,480 --> 02:06:00,320
of s it gives us set
3172
02:06:00,400 --> 02:06:01,360
okay
3173
02:06:01,360 --> 02:06:03,280
so that brings us to the end of this
3174
02:06:03,280 --> 02:06:04,800
demo session on
3175
02:06:04,800 --> 02:06:06,719
data types in python let me go to the
3176
02:06:06,719 --> 02:06:08,480
top and
3177
02:06:08,480 --> 02:06:10,400
i'll brief you what we did in this demo
3178
02:06:10,400 --> 02:06:12,079
so first we looked at
3179
02:06:12,079 --> 02:06:14,079
the different numeric data types so we
3180
02:06:14,079 --> 02:06:16,079
saw integer data type then we saw
3181
02:06:16,079 --> 02:06:17,679
floating data type
3182
02:06:17,679 --> 02:06:19,040
we also had
3183
02:06:19,040 --> 02:06:20,719
complex data types
3184
02:06:20,719 --> 02:06:23,119
we saw how to extract a real part in
3185
02:06:23,119 --> 02:06:25,840
imaginary part from a complex number
3186
02:06:25,840 --> 02:06:27,280
then we used
3187
02:06:27,280 --> 02:06:28,880
some type conversion functions to
3188
02:06:28,880 --> 02:06:31,199
convert the numeric data types from one
3189
02:06:31,199 --> 02:06:32,560
data type to another so here we
3190
02:06:32,560 --> 02:06:34,800
converted a floating type data to an
3191
02:06:34,800 --> 02:06:36,719
integer type data
3192
02:06:36,719 --> 02:06:38,719
similarly
3193
02:06:38,719 --> 02:06:42,960
we saw how to use boolean data types
3194
02:06:42,960 --> 02:06:45,199
then we saw about string data types we
3195
02:06:45,199 --> 02:06:47,280
also performed some basic operations of
3196
02:06:47,280 --> 02:06:48,159
using
3197
02:06:48,159 --> 02:06:50,159
indexing method to extract
3198
02:06:50,159 --> 02:06:52,639
different values
3199
02:06:52,639 --> 02:06:54,880
we also saw how you can use slicing to
3200
02:06:54,880 --> 02:06:57,040
extract a range of values from your
3201
02:06:57,040 --> 02:06:59,599
string we use the find function to find
3202
02:06:59,599 --> 02:07:01,760
the index of a particular
3203
02:07:01,760 --> 02:07:03,760
character in the string
3204
02:07:03,760 --> 02:07:06,480
then we learnt the important
3205
02:07:06,480 --> 02:07:09,119
sequence data types like list
3206
02:07:09,119 --> 02:07:10,639
then we had
3207
02:07:10,639 --> 02:07:12,960
tuples
3208
02:07:12,960 --> 02:07:14,639
and then we learned about
3209
02:07:14,639 --> 02:07:17,920
mapping data types such as dictionary
3210
02:07:17,920 --> 02:07:21,440
we also looked at set data type
3211
02:07:21,440 --> 02:07:22,800
now
3212
02:07:22,800 --> 02:07:25,440
later on we explored about
3213
02:07:25,440 --> 02:07:28,320
range data type and we looked at binary
3214
02:07:28,320 --> 02:07:30,639
data types we saw how
3215
02:07:30,639 --> 02:07:33,040
binary data type returns ascii value of
3216
02:07:33,040 --> 02:07:34,719
a certain character
3217
02:07:34,719 --> 02:07:36,719
finally we converted
3218
02:07:36,719 --> 02:07:39,360
strings to list and then we saw how to
3219
02:07:39,360 --> 02:07:41,119
convert
3220
02:07:41,119 --> 02:07:42,719
list to sets
3221
02:07:42,719 --> 02:07:44,719
first we will learn different number
3222
02:07:44,719 --> 02:07:47,599
data types in python then we will learn
3223
02:07:47,599 --> 02:07:49,520
type conversion between number data
3224
02:07:49,520 --> 02:07:52,159
types and after that we will understand
3225
02:07:52,159 --> 02:07:55,119
how to represent binary hexadecimal and
3226
02:07:55,119 --> 02:07:58,079
octal numbers in python and then we will
3227
02:07:58,079 --> 02:08:00,480
understand different number functions in
3228
02:08:00,480 --> 02:08:02,880
python
3229
02:08:02,880 --> 02:08:04,800
a programming language needs to have
3230
02:08:04,800 --> 02:08:06,840
support for numbers to carry out
3231
02:08:06,840 --> 02:08:09,520
calculations in python the numbers are
3232
02:08:09,520 --> 02:08:12,000
categorized into different data types
3233
02:08:12,000 --> 02:08:14,480
types are implemented in python as
3234
02:08:14,480 --> 02:08:15,760
classes
3235
02:08:15,760 --> 02:08:18,320
there are three numeric types in python
3236
02:08:18,320 --> 02:08:21,199
int for integers float for decimal
3237
02:08:21,199 --> 02:08:24,000
numbers and complex for complex numbers
3238
02:08:24,000 --> 02:08:25,920
let's explore these data types in
3239
02:08:25,920 --> 02:08:28,880
jupyter notebook
3240
02:08:28,880 --> 02:08:30,719
i have opened my jupyter notebook and
3241
02:08:30,719 --> 02:08:33,199
now let's see different data types in
3242
02:08:33,199 --> 02:08:35,040
jupyter notebook
3243
02:08:35,040 --> 02:08:38,560
for example first we'll assign a number
3244
02:08:38,560 --> 02:08:41,280
to a variable
3245
02:08:41,280 --> 02:08:44,639
num variable equal to 7
3246
02:08:44,639 --> 02:08:47,360
and now we'll check the type of this num
3247
02:08:47,360 --> 02:08:49,040
variable for that we'll write the
3248
02:08:49,040 --> 02:08:50,880
command type
3249
02:08:50,880 --> 02:08:54,320
round brackets num round bracket close
3250
02:08:54,320 --> 02:08:56,960
and then we'll run this command
3251
02:08:56,960 --> 02:09:01,199
we will get int as the data type
3252
02:09:01,199 --> 02:09:04,400
now we will assign a decimal number to
3253
02:09:04,400 --> 02:09:07,119
the num variable that would be num equal
3254
02:09:07,119 --> 02:09:09,199
to 7.7
3255
02:09:09,199 --> 02:09:10,560
and now
3256
02:09:10,560 --> 02:09:14,320
we will check the type of the num
3257
02:09:16,840 --> 02:09:18,880
variable and
3258
02:09:18,880 --> 02:09:20,639
run this command
3259
02:09:20,639 --> 02:09:22,960
what we get is the float float as the
3260
02:09:22,960 --> 02:09:25,840
data type for the num variable
3261
02:09:25,840 --> 02:09:28,960
now we will assign a complex number to
3262
02:09:28,960 --> 02:09:32,280
the num variable
3263
02:09:37,599 --> 02:09:40,719
and we will check the type for this num
3264
02:09:40,719 --> 02:09:43,280
variable
3265
02:09:44,239 --> 02:09:46,960
and run the command we will get the
3266
02:09:46,960 --> 02:09:50,480
complex data type as the result
3267
02:09:50,480 --> 02:09:52,960
now if you want to get the real part of
3268
02:09:52,960 --> 02:09:55,119
this complex number
3269
02:09:55,119 --> 02:09:57,599
you just have to write the command num
3270
02:09:57,599 --> 02:09:59,599
dot real
3271
02:09:59,599 --> 02:10:01,920
and just execute the command you will
3272
02:10:01,920 --> 02:10:03,679
get 6.0
3273
02:10:03,679 --> 02:10:06,560
as the real part of this complex number
3274
02:10:06,560 --> 02:10:08,719
here you can see you just quote the
3275
02:10:08,719 --> 02:10:11,760
number in float data type
3276
02:10:11,760 --> 02:10:14,719
the complex number in python are stored
3277
02:10:14,719 --> 02:10:17,840
in flow data types whether it's the real
3278
02:10:17,840 --> 02:10:20,320
part or it's the imaginary part now
3279
02:10:20,320 --> 02:10:22,960
we'll see the imaginary part
3280
02:10:22,960 --> 02:10:25,440
num dot image
3281
02:10:25,440 --> 02:10:27,440
and now you will run this command you
3282
02:10:27,440 --> 02:10:29,199
will get 7.0
3283
02:10:29,199 --> 02:10:31,440
here also you can see that the imaginary
3284
02:10:31,440 --> 02:10:34,960
part is also stored as float
3285
02:10:34,960 --> 02:10:38,320
in python for complex number
3286
02:10:38,320 --> 02:10:40,639
now we will perform different arithmetic
3287
02:10:40,639 --> 02:10:44,320
operations with these data types
3288
02:10:44,320 --> 02:10:47,280
first we'll mention
3289
02:10:48,960 --> 02:10:52,719
arithmetic operations
3290
02:10:53,520 --> 02:10:55,840
now we will assign numbers to the
3291
02:10:55,840 --> 02:10:57,360
variables and perform different
3292
02:10:57,360 --> 02:11:00,400
arithmetic operations for that we will
3293
02:11:00,400 --> 02:11:01,920
assign
3294
02:11:01,920 --> 02:11:05,440
17 as a number to the variable num1
3295
02:11:05,440 --> 02:11:08,239
num1 equal to 17
3296
02:11:08,239 --> 02:11:09,199
and
3297
02:11:09,199 --> 02:11:12,000
the second variable num2 we will assign
3298
02:11:12,000 --> 02:11:15,280
the number 25
3299
02:11:15,280 --> 02:11:17,599
and now we will perform
3300
02:11:17,599 --> 02:11:20,000
different arithmetic operations first
3301
02:11:20,000 --> 02:11:22,800
we'll start with addition
3302
02:11:22,800 --> 02:11:24,880
so write the simple command that would
3303
02:11:24,880 --> 02:11:26,320
print
3304
02:11:26,320 --> 02:11:29,599
num1 plus
3305
02:11:30,000 --> 02:11:33,840
num2 and just run that command
3306
02:11:33,840 --> 02:11:37,440
you can see the result as 42. it's as
3307
02:11:37,440 --> 02:11:39,840
simple as that in python you just have
3308
02:11:39,840 --> 02:11:42,480
to write print and in round brackets
3309
02:11:42,480 --> 02:11:44,800
just write the operation you wanna
3310
02:11:44,800 --> 02:11:46,480
execute
3311
02:11:46,480 --> 02:11:49,520
now we will see the subtraction part
3312
02:11:49,520 --> 02:11:53,079
we'll write print
3313
02:11:53,280 --> 02:11:55,360
num2 minus
3314
02:11:55,360 --> 02:11:56,560
num1
3315
02:11:56,560 --> 02:11:59,280
and execute this command and we'll get
3316
02:11:59,280 --> 02:12:01,440
the result as 8.
3317
02:12:01,440 --> 02:12:04,079
now we see multiplication
3318
02:12:04,079 --> 02:12:06,239
print
3319
02:12:06,239 --> 02:12:08,560
num1
3320
02:12:08,560 --> 02:12:09,760
into
3321
02:12:09,760 --> 02:12:11,040
num2
3322
02:12:11,040 --> 02:12:13,040
and execute this command and we'll get
3323
02:12:13,040 --> 02:12:15,360
the result as 425
3324
02:12:15,360 --> 02:12:18,400
now we will divide
3325
02:12:18,480 --> 02:12:19,679
print
3326
02:12:19,679 --> 02:12:20,880
num2
3327
02:12:20,880 --> 02:12:22,159
divided by
3328
02:12:22,159 --> 02:12:23,280
num1
3329
02:12:23,280 --> 02:12:25,360
and execute this command
3330
02:12:25,360 --> 02:12:28,480
and we'll get 1.47
3331
02:12:28,480 --> 02:12:31,040
here you can see as we have assigned int
3332
02:12:31,040 --> 02:12:34,159
values to the num1 and num2 variable we
3333
02:12:34,159 --> 02:12:36,480
got the result for addition subtraction
3334
02:12:36,480 --> 02:12:39,520
and multiplication as in data type only
3335
02:12:39,520 --> 02:12:41,760
but for division we got it
3336
02:12:41,760 --> 02:12:43,599
as float
3337
02:12:43,599 --> 02:12:46,320
this is because python always gives the
3338
02:12:46,320 --> 02:12:48,880
output for division in decimal numbers
3339
02:12:48,880 --> 02:12:49,920
only
3340
02:12:49,920 --> 02:12:53,679
just to have minimum accuracy loss so
3341
02:12:53,679 --> 02:12:56,400
if you want to just get the output of
3342
02:12:56,400 --> 02:12:57,679
the division
3343
02:12:57,679 --> 02:13:00,639
as an end value you just have to perform
3344
02:13:00,639 --> 02:13:02,960
this
3345
02:13:02,960 --> 02:13:05,360
print
3346
02:13:06,480 --> 02:13:07,920
num2
3347
02:13:07,920 --> 02:13:11,360
and two slash signs and then num1 and
3348
02:13:11,360 --> 02:13:13,520
then execute the command
3349
02:13:13,520 --> 02:13:16,239
you will get one as the output so to
3350
02:13:16,239 --> 02:13:18,960
just get into the output of the division
3351
02:13:18,960 --> 02:13:21,920
you can use two slashes
3352
02:13:21,920 --> 02:13:24,320
next command we'll see is to raise a
3353
02:13:24,320 --> 02:13:25,760
number
3354
02:13:25,760 --> 02:13:27,360
to another number
3355
02:13:27,360 --> 02:13:30,719
so to just get 2 to the power 4 we will
3356
02:13:30,719 --> 02:13:33,840
write the command print
3357
02:13:33,840 --> 02:13:36,079
2
3358
02:13:36,079 --> 02:13:37,920
2 asterix symbols
3359
02:13:37,920 --> 02:13:38,719
and
3360
02:13:38,719 --> 02:13:40,000
4.
3361
02:13:40,000 --> 02:13:42,239
and just execute this command you will
3362
02:13:42,239 --> 02:13:45,199
get the output as 16
3363
02:13:45,199 --> 02:13:47,520
we got 2 to the power of 4
3364
02:13:47,520 --> 02:13:50,719
by just using two aztec symbols now to
3365
02:13:50,719 --> 02:13:53,679
get the remainder of the division
3366
02:13:53,679 --> 02:13:57,440
we just have to use the modulus symbol
3367
02:13:57,440 --> 02:13:58,880
so for that
3368
02:13:58,880 --> 02:14:01,040
print
3369
02:14:01,040 --> 02:14:01,920
4
3370
02:14:01,920 --> 02:14:03,119
modulus
3371
02:14:03,119 --> 02:14:06,560
2 and just execute the command
3372
02:14:06,560 --> 02:14:10,079
you will get 0 as the remainder
3373
02:14:10,079 --> 02:14:12,320
you can perform the same operations on
3374
02:14:12,320 --> 02:14:15,040
float numbers and complex numbers
3375
02:14:15,040 --> 02:14:16,480
now we will see
3376
02:14:16,480 --> 02:14:18,400
an arithmetic operation on complex
3377
02:14:18,400 --> 02:14:19,760
number
3378
02:14:19,760 --> 02:14:21,840
so we will assign a complex number to
3379
02:14:21,840 --> 02:14:23,280
variable
3380
02:14:23,280 --> 02:14:24,960
that would be g
3381
02:14:24,960 --> 02:14:27,119
equal to 1
3382
02:14:27,119 --> 02:14:28,719
plus
3383
02:14:28,719 --> 02:14:31,280
5 j
3384
02:14:31,599 --> 02:14:34,079
and another variable we will assign the
3385
02:14:34,079 --> 02:14:35,840
value 2
3386
02:14:35,840 --> 02:14:36,960
plus
3387
02:14:36,960 --> 02:14:38,639
3 j
3388
02:14:38,639 --> 02:14:40,480
and we will assign the operation
3389
02:14:40,480 --> 02:14:42,000
i equal to
3390
02:14:42,000 --> 02:14:42,960
g
3391
02:14:42,960 --> 02:14:45,840
slash h that is the reason
3392
02:14:45,840 --> 02:14:47,360
and
3393
02:14:47,360 --> 02:14:50,480
print the output
3394
02:14:50,560 --> 02:14:52,719
and will execute the command
3395
02:14:52,719 --> 02:14:54,639
we will get the following result for the
3396
02:14:54,639 --> 02:14:58,320
division of two complex numbers
3397
02:14:58,719 --> 02:15:00,880
now we will see type conversions in
3398
02:15:00,880 --> 02:15:04,800
python for that we will move
3399
02:15:06,480 --> 02:15:07,840
there are mainly two types of
3400
02:15:07,840 --> 02:15:10,639
conversions between numbers in python
3401
02:15:10,639 --> 02:15:13,679
first is using arithmetic operations
3402
02:15:13,679 --> 02:15:16,400
that is implicit type conversion and
3403
02:15:16,400 --> 02:15:19,199
second is using built-in functions that
3404
02:15:19,199 --> 02:15:22,560
is explicit type conversion
3405
02:15:22,560 --> 02:15:24,079
let's understand the concept of
3406
02:15:24,079 --> 02:15:25,920
conversion with the help of jupiter
3407
02:15:25,920 --> 02:15:27,280
notebook
3408
02:15:27,280 --> 02:15:28,719
so here
3409
02:15:28,719 --> 02:15:32,679
we will write first convergence
3410
02:15:36,159 --> 02:15:38,960
first we will assign a number
3411
02:15:38,960 --> 02:15:42,000
to a variable that would be a equal to
3412
02:15:42,000 --> 02:15:43,440
196
3413
02:15:43,440 --> 02:15:45,199
and we have seen in the previous
3414
02:15:45,199 --> 02:15:48,000
examples that its type is int
3415
02:15:48,000 --> 02:15:50,719
now if we put this number in two double
3416
02:15:50,719 --> 02:15:53,119
quotes
3417
02:15:53,440 --> 02:15:55,840
and now we'll check its type
3418
02:15:55,840 --> 02:15:58,800
that would be type
3419
02:15:59,440 --> 02:16:00,639
a
3420
02:16:00,639 --> 02:16:02,560
and run the command we'll get it as
3421
02:16:02,560 --> 02:16:03,599
string
3422
02:16:03,599 --> 02:16:07,280
so in python when we just put a number
3423
02:16:07,280 --> 02:16:09,599
in double quotes it will see it as a
3424
02:16:09,599 --> 02:16:11,199
string
3425
02:16:11,199 --> 02:16:14,800
and to get this number as an int
3426
02:16:14,800 --> 02:16:17,599
we just have to use explicit type
3427
02:16:17,599 --> 02:16:20,960
conversion for that we will write
3428
02:16:20,960 --> 02:16:22,400
b
3429
02:16:22,400 --> 02:16:23,840
equal to
3430
02:16:23,840 --> 02:16:25,360
int
3431
02:16:25,360 --> 02:16:26,800
a
3432
02:16:26,800 --> 02:16:30,800
and now we will check its type
3433
02:16:30,840 --> 02:16:33,439
type b
3434
02:16:33,439 --> 02:16:35,120
and run the command
3435
02:16:35,120 --> 02:16:36,638
we will get into
3436
02:16:36,638 --> 02:16:39,920
the data type for the b variable
3437
02:16:39,920 --> 02:16:42,558
the a variable is still the string data
3438
02:16:42,558 --> 02:16:43,439
type
3439
02:16:43,439 --> 02:16:46,638
and b variable has now turned as int
3440
02:16:46,638 --> 02:16:48,000
data type
3441
02:16:48,000 --> 02:16:49,679
and now
3442
02:16:49,679 --> 02:16:53,040
to turn a as a flow data type we can
3443
02:16:53,040 --> 02:16:53,920
write
3444
02:16:53,920 --> 02:16:54,879
c
3445
02:16:54,879 --> 02:16:56,959
equal to
3446
02:16:56,959 --> 02:16:59,438
float
3447
02:16:59,599 --> 02:17:01,840
a
3448
02:17:01,920 --> 02:17:04,160
and check its type
3449
02:17:04,160 --> 02:17:06,479
type
3450
02:17:06,558 --> 02:17:07,439
c
3451
02:17:07,439 --> 02:17:09,920
and run the command and here we can see
3452
02:17:09,920 --> 02:17:11,920
that it has turned into the float data
3453
02:17:11,920 --> 02:17:12,799
type
3454
02:17:12,799 --> 02:17:16,240
so a is still the string data type but c
3455
02:17:16,240 --> 02:17:18,879
has been converted the a variable into
3456
02:17:18,879 --> 02:17:21,919
the float data type
3457
02:17:23,920 --> 02:17:26,240
we can also convert the a string data
3458
02:17:26,240 --> 02:17:28,879
type into complex data type
3459
02:17:28,879 --> 02:17:30,240
just
3460
02:17:30,240 --> 02:17:32,638
we have to write t equal to
3461
02:17:32,638 --> 02:17:35,120
complex
3462
02:17:35,120 --> 02:17:37,840
and in brackets we have to write a
3463
02:17:37,840 --> 02:17:42,200
and we can check its type
3464
02:17:44,799 --> 02:17:47,839
and we can
3465
02:17:48,240 --> 02:17:49,840
print
3466
02:17:49,840 --> 02:17:53,519
the t complex number and we have just
3467
02:17:53,519 --> 02:17:56,558
execute the command
3468
02:17:56,558 --> 02:17:59,438
and we can see
3469
02:17:59,599 --> 02:18:04,558
that the complex number is 196 plus 0j
3470
02:18:04,558 --> 02:18:06,959
and we can also check its type
3471
02:18:06,959 --> 02:18:08,840
by just
3472
02:18:08,840 --> 02:18:12,719
writing print
3473
02:18:14,080 --> 02:18:16,080
type d
3474
02:18:16,080 --> 02:18:18,959
and we will run the command and we can
3475
02:18:18,959 --> 02:18:21,920
see that the class is complex
3476
02:18:21,920 --> 02:18:25,040
for the variable d we can define the
3477
02:18:25,040 --> 02:18:28,318
complex data type by different methods
3478
02:18:28,318 --> 02:18:30,160
one of the method is
3479
02:18:30,160 --> 02:18:33,519
we just have to write print
3480
02:18:33,519 --> 02:18:35,040
complex
3481
02:18:35,040 --> 02:18:37,439
and in round brackets we just have to
3482
02:18:37,439 --> 02:18:39,920
assign both real and the imaginary
3483
02:18:39,920 --> 02:18:44,000
numbers that would be 3 comma 6
3484
02:18:44,000 --> 02:18:46,638
and just execute the command
3485
02:18:46,638 --> 02:18:49,120
and here we got a complex number that is
3486
02:18:49,120 --> 02:18:51,439
3 plus 6 j
3487
02:18:51,439 --> 02:18:53,599
now we have seen the explicit type
3488
02:18:53,599 --> 02:18:55,200
convergence
3489
02:18:55,200 --> 02:18:56,959
now we will see some implicit type
3490
02:18:56,959 --> 02:18:58,959
convergence that is using arithmetic
3491
02:18:58,959 --> 02:19:02,160
operations for that we will assign
3492
02:19:02,160 --> 02:19:05,280
a float number that would be 2.6
3493
02:19:05,280 --> 02:19:06,558
to a variable
3494
02:19:06,558 --> 02:19:09,679
equal to 2.6
3495
02:19:09,679 --> 02:19:10,799
and
3496
02:19:10,799 --> 02:19:12,318
b equal to
3497
02:19:12,318 --> 02:19:13,519
6
3498
02:19:13,519 --> 02:19:15,599
and we will assign an arithmetic
3499
02:19:15,599 --> 02:19:17,760
operation that would be
3500
02:19:17,760 --> 02:19:21,439
c equal to a plus b
3501
02:19:21,439 --> 02:19:22,718
and
3502
02:19:22,718 --> 02:19:25,678
print the output
3503
02:19:26,398 --> 02:19:28,959
and execute the command
3504
02:19:28,959 --> 02:19:31,280
by using just arithmetic operations we
3505
02:19:31,280 --> 02:19:33,200
have seen that the addition of a float
3506
02:19:33,200 --> 02:19:35,200
number and the integer number turns out
3507
02:19:35,200 --> 02:19:36,879
to be a float number
3508
02:19:36,879 --> 02:19:38,879
so now we have seen the implicit type
3509
02:19:38,879 --> 02:19:41,200
conversion example
3510
02:19:41,200 --> 02:19:43,280
now we will see the representation of
3511
02:19:43,280 --> 02:19:46,080
binary hexadecimal and octal numbers in
3512
02:19:46,080 --> 02:19:49,080
python
3513
02:19:50,399 --> 02:19:52,399
all the integer numbers can be
3514
02:19:52,399 --> 02:19:55,040
represented as binary hexadecimal and
3515
02:19:55,040 --> 02:19:56,319
octa
3516
02:19:56,319 --> 02:19:58,800
to represent it as binary in python we
3517
02:19:58,800 --> 02:20:02,640
have to prefix it by using 0 small b or
3518
02:20:02,640 --> 02:20:06,000
0 capital b and for octal
3519
02:20:06,000 --> 02:20:09,040
we can prefix it by using 0
3520
02:20:09,040 --> 02:20:11,920
small o or 0 capital o
3521
02:20:11,920 --> 02:20:16,319
and for hexadecimal we can use 0 x or 0
3522
02:20:16,319 --> 02:20:19,120
capital x let's see this representation
3523
02:20:19,120 --> 02:20:20,800
in jupiter notebook
3524
02:20:20,800 --> 02:20:22,399
first we will write representation of
3525
02:20:22,399 --> 02:20:26,080
binary hexadecimal and octal numbers
3526
02:20:26,080 --> 02:20:27,680
now
3527
02:20:27,680 --> 02:20:29,120
we will first see
3528
02:20:29,120 --> 02:20:31,439
binary to decimal conversion
3529
02:20:31,439 --> 02:20:32,880
for that
3530
02:20:32,880 --> 02:20:35,120
we will first write
3531
02:20:35,120 --> 02:20:36,960
0
3532
02:20:36,960 --> 02:20:38,640
b
3533
02:20:38,640 --> 02:20:40,479
1 1 0
3534
02:20:40,479 --> 02:20:43,680
1 1 0 0 0
3535
02:20:43,680 --> 02:20:46,319
and then we'll just run it
3536
02:20:46,319 --> 02:20:49,680
and see 216 as the decimal conversion of
3537
02:20:49,680 --> 02:20:52,720
this binary number
3538
02:20:52,720 --> 02:20:55,439
now we'll see the same for the octal
3539
02:20:55,439 --> 02:20:57,439
for that we'll write 0
3540
02:20:57,439 --> 02:20:59,280
small o
3541
02:20:59,280 --> 02:21:00,240
12
3542
02:21:00,240 --> 02:21:02,960
and just run this command
3543
02:21:02,960 --> 02:21:06,000
and we can see that it has turned 10
3544
02:21:06,000 --> 02:21:08,720
in the decimal number system
3545
02:21:08,720 --> 02:21:11,520
for hexadecimal we will write
3546
02:21:11,520 --> 02:21:12,560
0
3547
02:21:12,560 --> 02:21:14,720
small x
3548
02:21:14,720 --> 02:21:16,560
and 12
3549
02:21:16,560 --> 02:21:19,439
and just run the command
3550
02:21:19,439 --> 02:21:21,520
and we can see 18 as the decimal
3551
02:21:21,520 --> 02:21:24,240
conversion of the hexadecimal number
3552
02:21:24,240 --> 02:21:26,399
system
3553
02:21:26,399 --> 02:21:30,319
we can also use 0 capital x
3554
02:21:30,319 --> 02:21:32,720
12 and just on the command
3555
02:21:32,720 --> 02:21:36,479
we'll see 18 the same output as of 0
3556
02:21:36,479 --> 02:21:39,439
small x 12.
3557
02:21:39,760 --> 02:21:42,560
let's see some operations with fractions
3558
02:21:42,560 --> 02:21:47,040
for that first we will comment fractions
3559
02:21:50,880 --> 02:21:54,240
now we will import the fraction module
3560
02:21:54,240 --> 02:21:57,280
for that we'll write from
3561
02:21:57,280 --> 02:22:00,080
fractions
3562
02:22:02,240 --> 02:22:03,760
import
3563
02:22:03,760 --> 02:22:05,280
fraction
3564
02:22:05,280 --> 02:22:06,840
s
3565
02:22:06,840 --> 02:22:10,640
f and first i will
3566
02:22:10,640 --> 02:22:12,720
execute the addition operation for
3567
02:22:12,720 --> 02:22:16,720
fractions for that will write print
3568
02:22:17,200 --> 02:22:19,439
f
3569
02:22:20,399 --> 02:22:22,880
1 comma 3
3570
02:22:22,880 --> 02:22:25,120
just for the fraction numerator would be
3571
02:22:25,120 --> 02:22:28,720
1 and the denominator would be 3
3572
02:22:28,720 --> 02:22:32,399
now we'll write the operation addition
3573
02:22:32,399 --> 02:22:33,680
and f
3574
02:22:33,680 --> 02:22:37,520
the second fraction that would be
3575
02:22:37,520 --> 02:22:39,600
okay we will write the same only one
3576
02:22:39,600 --> 02:22:40,800
comma three
3577
02:22:40,800 --> 02:22:42,640
and on execution of the command we will
3578
02:22:42,640 --> 02:22:46,080
get two by three as the output
3579
02:22:46,080 --> 02:22:49,840
we can also perform different operations
3580
02:22:49,840 --> 02:22:52,080
okay we will
3581
02:22:52,080 --> 02:22:54,399
just see the reciprocal of the fraction
3582
02:22:54,399 --> 02:22:57,840
for that we will write print
3583
02:22:59,120 --> 02:23:00,640
one
3584
02:23:00,640 --> 02:23:01,920
upon
3585
02:23:01,920 --> 02:23:04,399
f
3586
02:23:04,399 --> 02:23:07,680
five comma six
3587
02:23:08,319 --> 02:23:10,640
and just run the command
3588
02:23:10,640 --> 02:23:12,880
here we can see the reciprocal function
3589
02:23:12,880 --> 02:23:15,920
that would turn out to be six by five
3590
02:23:15,920 --> 02:23:17,520
now we will explore different number
3591
02:23:17,520 --> 02:23:19,840
functions
3592
02:23:20,880 --> 02:23:23,200
first one that we would explore would be
3593
02:23:23,200 --> 02:23:25,040
absolute
3594
02:23:25,040 --> 02:23:26,800
absolute function would turn any
3595
02:23:26,800 --> 02:23:29,280
negative number into the positive number
3596
02:23:29,280 --> 02:23:30,399
for that
3597
02:23:30,399 --> 02:23:33,520
we will just assign a number that would
3598
02:23:33,520 --> 02:23:36,240
be x equal to minus
3599
02:23:36,240 --> 02:23:38,640
9.8
3600
02:23:38,640 --> 02:23:40,880
and
3601
02:23:40,880 --> 02:23:44,840
we will just apply the absolute function
3602
02:23:44,840 --> 02:23:48,560
abs on the variable x
3603
02:23:48,560 --> 02:23:50,880
and just run the command
3604
02:23:50,880 --> 02:23:53,760
here we can see that has turned x equal
3605
02:23:53,760 --> 02:23:57,439
to minus 9.8 to 9.8
3606
02:23:57,439 --> 02:23:58,800
now
3607
02:23:58,800 --> 02:24:01,600
we will see some different functions for
3608
02:24:01,600 --> 02:24:04,319
that we will import the math
3609
02:24:04,319 --> 02:24:06,800
module
3610
02:24:07,359 --> 02:24:09,040
import
3611
02:24:09,040 --> 02:24:10,640
math
3612
02:24:10,640 --> 02:24:12,800
and we have different values stored in
3613
02:24:12,800 --> 02:24:16,000
the math module for example pi
3614
02:24:16,000 --> 02:24:20,720
we'll print the pi value print
3615
02:24:20,720 --> 02:24:22,880
math dot pi
3616
02:24:22,880 --> 02:24:25,040
and run the command
3617
02:24:25,040 --> 02:24:26,960
here we can see the value of the pi
3618
02:24:26,960 --> 02:24:29,960
3.1415926535893
3619
02:24:32,640 --> 02:24:34,800
we also have exponential value in math
3620
02:24:34,800 --> 02:24:35,760
module
3621
02:24:35,760 --> 02:24:39,760
for that we will write the command print
3622
02:24:39,760 --> 02:24:43,439
math dot e and execute the command
3623
02:24:43,439 --> 02:24:44,880
here we will get the value
3624
02:24:44,880 --> 02:24:47,880
2.718281828459045
3625
02:24:49,760 --> 02:24:52,880
for the value of e in the math module
3626
02:24:52,880 --> 02:24:55,280
now we will also see
3627
02:24:55,280 --> 02:24:57,840
exponential to the power 10
3628
02:24:57,840 --> 02:25:02,240
for that we will write the command print
3629
02:25:02,319 --> 02:25:03,920
math dot
3630
02:25:03,920 --> 02:25:05,280
exp
3631
02:25:05,280 --> 02:25:06,880
round brackets
3632
02:25:06,880 --> 02:25:08,160
10
3633
02:25:08,160 --> 02:25:10,560
and execute the command
3634
02:25:10,560 --> 02:25:14,600
we will get the value 22026.465
3635
02:25:16,399 --> 02:25:20,319
we can also perform log operations
3636
02:25:20,319 --> 02:25:22,800
for that we will write the command
3637
02:25:22,800 --> 02:25:27,040
print and we will get the log of 1000 to
3638
02:25:27,040 --> 02:25:28,560
the base of 10.
3639
02:25:28,560 --> 02:25:30,479
for that we'll write
3640
02:25:30,479 --> 02:25:31,840
math
3641
02:25:31,840 --> 02:25:33,920
dot
3642
02:25:33,920 --> 02:25:34,960
log
3643
02:25:34,960 --> 02:25:37,680
to the base 10
3644
02:25:37,680 --> 02:25:39,280
the would be
3645
02:25:39,280 --> 02:25:42,319
1000 and we will execute the command and
3646
02:25:42,319 --> 02:25:44,880
we will get 3.0
3647
02:25:44,880 --> 02:25:46,560
you can also get the factorial by the
3648
02:25:46,560 --> 02:25:49,200
math module
3649
02:25:49,359 --> 02:25:52,880
all right print
3650
02:25:52,880 --> 02:25:55,200
math
3651
02:25:55,760 --> 02:25:58,479
dot factorial
3652
02:25:58,479 --> 02:26:00,080
factorial of
3653
02:26:00,080 --> 02:26:01,840
6
3654
02:26:01,840 --> 02:26:03,600
and we'll execute the command and we
3655
02:26:03,600 --> 02:26:05,439
quote the factorial of 6
3656
02:26:05,439 --> 02:26:08,560
that is 720
3657
02:26:08,800 --> 02:26:10,479
now if we want the square root of a
3658
02:26:10,479 --> 02:26:12,640
number we will write the command
3659
02:26:12,640 --> 02:26:15,040
print
3660
02:26:15,200 --> 02:26:16,760
math dot
3661
02:26:16,760 --> 02:26:18,479
sqrt
3662
02:26:18,479 --> 02:26:20,640
and in round brackets we will write the
3663
02:26:20,640 --> 02:26:23,280
number for which we need the square root
3664
02:26:23,280 --> 02:26:26,880
we will write 25 and execute the command
3665
02:26:26,880 --> 02:26:30,319
and we will get 5.0
3666
02:26:30,479 --> 02:26:32,880
this square root number gives the float
3667
02:26:32,880 --> 02:26:35,200
number as its output
3668
02:26:35,200 --> 02:26:37,280
and we can also find out the maximum
3669
02:26:37,280 --> 02:26:40,080
numbers from a bunch of numbers for that
3670
02:26:40,080 --> 02:26:42,399
we have to write the command
3671
02:26:42,399 --> 02:26:43,600
max
3672
02:26:43,600 --> 02:26:45,040
round brackets
3673
02:26:45,040 --> 02:26:46,960
and the bunch of numbers from which you
3674
02:26:46,960 --> 02:26:49,120
want the maximum value
3675
02:26:49,120 --> 02:26:53,920
like 1 comma 33 comma 55 comma
3676
02:26:53,920 --> 02:26:58,000
7 comma 99 and just execute the command
3677
02:26:58,000 --> 02:26:59,520
you will get the maximum number that
3678
02:26:59,520 --> 02:27:02,080
would be 99 and if you want the minimum
3679
02:27:02,080 --> 02:27:03,040
number
3680
02:27:03,040 --> 02:27:05,359
you can write the command min
3681
02:27:05,359 --> 02:27:07,359
and in round brackets just write the
3682
02:27:07,359 --> 02:27:09,120
bunch of numbers
3683
02:27:09,120 --> 02:27:16,040
1 comma 3 comma 66 comma 88 comma
3684
02:27:16,720 --> 02:27:18,560
seven
3685
02:27:18,560 --> 02:27:20,880
and just execute the command
3686
02:27:20,880 --> 02:27:22,399
you will get the minimum number that
3687
02:27:22,399 --> 02:27:24,080
would be one
3688
02:27:24,080 --> 02:27:26,080
now you'll see the ceiling function for
3689
02:27:26,080 --> 02:27:28,840
that we'll write the command
3690
02:27:28,840 --> 02:27:30,399
print
3691
02:27:30,399 --> 02:27:32,720
math
3692
02:27:33,200 --> 02:27:34,640
dot c
3693
02:27:34,640 --> 02:27:36,319
and in round brackets we'll write the
3694
02:27:36,319 --> 02:27:38,800
number for which we want to use the same
3695
02:27:38,800 --> 02:27:40,680
function minus
3696
02:27:40,680 --> 02:27:42,479
43.5
3697
02:27:42,479 --> 02:27:44,640
and execute the command
3698
02:27:44,640 --> 02:27:46,560
you will get minus 43
3699
02:27:46,560 --> 02:27:48,800
so what the scenic function does is
3700
02:27:48,800 --> 02:27:51,120
it would give the highest value or the
3701
02:27:51,120 --> 02:27:53,280
greatest value for the number we have
3702
02:27:53,280 --> 02:27:56,399
entered in the ceiling function
3703
02:27:56,399 --> 02:27:58,160
now we will see the floor function for
3704
02:27:58,160 --> 02:27:59,920
that we'll write the command
3705
02:27:59,920 --> 02:28:02,160
print
3706
02:28:02,160 --> 02:28:06,000
bracket math dot floor
3707
02:28:06,000 --> 02:28:07,840
and we'll write the number for which we
3708
02:28:07,840 --> 02:28:10,000
want to use the floor function
3709
02:28:10,000 --> 02:28:11,040
minus
3710
02:28:11,040 --> 02:28:13,280
43.5
3711
02:28:13,280 --> 02:28:15,439
and execute the command
3712
02:28:15,439 --> 02:28:18,080
we will get minus 44.
3713
02:28:18,080 --> 02:28:20,000
so the floor function will give the
3714
02:28:20,000 --> 02:28:21,359
output value
3715
02:28:21,359 --> 02:28:22,319
just
3716
02:28:22,319 --> 02:28:25,600
below the or just the lowest value
3717
02:28:25,600 --> 02:28:27,680
of the value entered by us in the floor
3718
02:28:27,680 --> 02:28:28,960
function
3719
02:28:28,960 --> 02:28:31,359
now we'll see the power function for
3720
02:28:31,359 --> 02:28:35,120
that we'll write the command print
3721
02:28:35,120 --> 02:28:39,040
math dot pow and in round brackets
3722
02:28:39,040 --> 02:28:41,600
will write the number
3723
02:28:41,600 --> 02:28:42,640
2
3724
02:28:42,640 --> 02:28:45,280
and the power which we want that would
3725
02:28:45,280 --> 02:28:46,399
be 6
3726
02:28:46,399 --> 02:28:49,840
and just execute the command
3727
02:28:49,840 --> 02:28:51,200
here we can see
3728
02:28:51,200 --> 02:28:53,840
that 2 to the power 6 we have got the
3729
02:28:53,840 --> 02:28:56,800
output value as 64.
3730
02:28:56,800 --> 02:28:58,240
now we'll see some trigonometric
3731
02:28:58,240 --> 02:29:00,240
functions
3732
02:29:00,240 --> 02:29:03,359
first we'll see cos of x
3733
02:29:03,359 --> 02:29:05,760
like python number method cos returns
3734
02:29:05,760 --> 02:29:09,520
the cosine of x radians for that we will
3735
02:29:09,520 --> 02:29:12,399
write the command
3736
02:29:14,399 --> 02:29:16,800
print
3737
02:29:19,359 --> 02:29:22,640
math dot
3738
02:29:22,640 --> 02:29:26,000
cos of 0
3739
02:29:26,000 --> 02:29:28,640
and just run the command it will give 1
3740
02:29:28,640 --> 02:29:30,640
as the output
3741
02:29:30,640 --> 02:29:31,520
so
3742
02:29:31,520 --> 02:29:33,600
this method returns a numeric value
3743
02:29:33,600 --> 02:29:36,399
between -1 and 1 like which represents
3744
02:29:36,399 --> 02:29:38,880
the cosine of the angle
3745
02:29:38,880 --> 02:29:40,960
we will see another example for course
3746
02:29:40,960 --> 02:29:42,560
for that we'll write
3747
02:29:42,560 --> 02:29:44,960
print
3748
02:29:46,080 --> 02:29:48,319
math dot cos
3749
02:29:48,319 --> 02:29:50,960
and in brackets we'll write the angle
3750
02:29:50,960 --> 02:29:52,319
math
3751
02:29:52,319 --> 02:29:55,200
dot pi
3752
02:29:55,920 --> 02:29:58,640
and we'll execute the command
3753
02:29:58,640 --> 02:30:03,080
and we'll get the output as -1
3754
02:30:04,479 --> 02:30:07,840
now we'll see 10 trigonometric function
3755
02:30:07,840 --> 02:30:10,640
so the python method 10 it returns the
3756
02:30:10,640 --> 02:30:12,960
tangent of x radians
3757
02:30:12,960 --> 02:30:15,680
for that we will write the command
3758
02:30:15,680 --> 02:30:18,680
print
3759
02:30:19,600 --> 02:30:22,600
math.10
3760
02:30:22,640 --> 02:30:24,319
of angle 0
3761
02:30:24,319 --> 02:30:28,160
and we will execute the command
3762
02:30:28,800 --> 02:30:31,520
we will get 0 as output this method
3763
02:30:31,520 --> 02:30:35,280
returns a numeric value between -1 and 1
3764
02:30:35,280 --> 02:30:36,800
which represents the tangent of the
3765
02:30:36,800 --> 02:30:39,040
perimeter x
3766
02:30:39,040 --> 02:30:42,080
we will also see another example for tan
3767
02:30:42,080 --> 02:30:43,840
we'll write
3768
02:30:43,840 --> 02:30:46,840
print
3769
02:30:48,720 --> 02:30:50,319
math dot
3770
02:30:50,319 --> 02:30:52,640
10
3771
02:30:53,520 --> 02:30:54,479
math
3772
02:30:54,479 --> 02:30:55,840
dot
3773
02:30:55,840 --> 02:30:57,680
pi by
3774
02:30:57,680 --> 02:31:01,840
4 and execute the command
3775
02:31:03,920 --> 02:31:05,000
it gives us
3776
02:31:05,000 --> 02:31:09,439
0.999999 that turns out to be 1.
3777
02:31:09,600 --> 02:31:11,520
now we will explore some random number
3778
02:31:11,520 --> 02:31:13,760
functions for that we will write the
3779
02:31:13,760 --> 02:31:15,760
command
3780
02:31:15,760 --> 02:31:16,880
import
3781
02:31:16,880 --> 02:31:18,319
random
3782
02:31:18,319 --> 02:31:21,600
and import the random module
3783
02:31:21,600 --> 02:31:23,520
now we'll see the first function that
3784
02:31:23,520 --> 02:31:26,000
would be rand range for that we'll write
3785
02:31:26,000 --> 02:31:29,520
the command print
3786
02:31:29,520 --> 02:31:31,520
random dot
3787
02:31:31,520 --> 02:31:34,960
rent range and in circular brackets we
3788
02:31:34,960 --> 02:31:37,280
will write the range from which we want
3789
02:31:37,280 --> 02:31:39,600
the random number
3790
02:31:39,600 --> 02:31:43,040
that would be 10 comma 20 and execute
3791
02:31:43,040 --> 02:31:45,920
the command here we go 12 as the random
3792
02:31:45,920 --> 02:31:47,520
number
3793
02:31:47,520 --> 02:31:50,000
now we will define a list that would be
3794
02:31:50,000 --> 02:31:51,040
x
3795
02:31:51,040 --> 02:31:52,319
equal to
3796
02:31:52,319 --> 02:31:54,960
square brackets
3797
02:31:54,960 --> 02:31:57,280
one
3798
02:31:58,160 --> 02:32:00,399
two
3799
02:32:01,520 --> 02:32:03,840
three
3800
02:32:04,240 --> 02:32:06,160
four
3801
02:32:06,160 --> 02:32:08,399
and five
3802
02:32:08,399 --> 02:32:11,840
now we'll see another function
3803
02:32:13,439 --> 02:32:16,160
that would be choice and it would give
3804
02:32:16,160 --> 02:32:19,600
any random number from string
3805
02:32:19,600 --> 02:32:21,760
list or tuple
3806
02:32:21,760 --> 02:32:24,240
so for that we'll write the command
3807
02:32:24,240 --> 02:32:26,640
print
3808
02:32:28,080 --> 02:32:30,960
random dot
3809
02:32:30,960 --> 02:32:33,680
choice and in circular brackets we'll
3810
02:32:33,680 --> 02:32:34,960
write
3811
02:32:34,960 --> 02:32:38,399
or we will pass any list string or tuple
3812
02:32:38,399 --> 02:32:40,479
here we will pass this list
3813
02:32:40,479 --> 02:32:43,439
x and execute this command
3814
02:32:43,439 --> 02:32:46,000
we will get the random number three so
3815
02:32:46,000 --> 02:32:48,080
we are going to talk about object types
3816
02:32:48,080 --> 02:32:50,399
in python then we will see the different
3817
02:32:50,399 --> 02:32:52,080
type conversion functions that are
3818
02:32:52,080 --> 02:32:54,160
available in python
3819
02:32:54,160 --> 02:32:56,240
then we see the different types of type
3820
02:32:56,240 --> 02:32:57,840
conversion
3821
02:32:57,840 --> 02:32:59,760
all right
3822
02:32:59,760 --> 02:33:01,520
now let's look at the different types of
3823
02:33:01,520 --> 02:33:03,760
objects in python
3824
02:33:03,760 --> 02:33:05,760
so we have less
3825
02:33:05,760 --> 02:33:08,640
you have tuples numbers strings
3826
02:33:08,640 --> 02:33:11,840
dictionaries sets you even have file
3827
02:33:11,840 --> 02:33:12,960
objects
3828
02:33:12,960 --> 02:33:15,760
then you can see we also have other code
3829
02:33:15,760 --> 02:33:19,040
types such as boolean and none
3830
02:33:19,040 --> 02:33:20,640
then we have
3831
02:33:20,640 --> 02:33:23,840
program unit types like functions
3832
02:33:23,840 --> 02:33:25,840
modules and classes
3833
02:33:25,840 --> 02:33:27,600
now we will touch upon most of them in
3834
02:33:27,600 --> 02:33:29,600
the demo section of this tutorial then
3835
02:33:29,600 --> 02:33:32,880
we also have implemented related types
3836
02:33:32,880 --> 02:33:34,640
all right
3837
02:33:34,640 --> 02:33:36,720
now let's see how you can convert one
3838
02:33:36,720 --> 02:33:38,479
data type to another using type
3839
02:33:38,479 --> 02:33:39,600
conversion
3840
02:33:39,600 --> 02:33:42,000
so python provides two types of data
3841
02:33:42,000 --> 02:33:44,399
conversion techniques one is called the
3842
02:33:44,399 --> 02:33:46,720
implicit type conversion and the other
3843
02:33:46,720 --> 02:33:49,600
is called the explicit type conversion
3844
02:33:49,600 --> 02:33:51,920
now in implicit data type conversion
3845
02:33:51,920 --> 02:33:54,399
python can automatically
3846
02:33:54,399 --> 02:33:56,319
convert one data type to another data
3847
02:33:56,319 --> 02:33:59,359
type this process does not need any user
3848
02:33:59,359 --> 02:34:01,040
involvement
3849
02:34:01,040 --> 02:34:03,680
while in external conversions
3850
02:34:03,680 --> 02:34:04,640
users
3851
02:34:04,640 --> 02:34:06,640
convert the data type of an object to
3852
02:34:06,640 --> 02:34:08,560
required data type by using predefined
3853
02:34:08,560 --> 02:34:10,319
functions that are listed here so you
3854
02:34:10,319 --> 02:34:13,200
can see here we have the different
3855
02:34:13,200 --> 02:34:15,840
functions so if you want to convert a
3856
02:34:15,840 --> 02:34:18,160
specific value to an integer you can use
3857
02:34:18,160 --> 02:34:19,840
the int function we have the float
3858
02:34:19,840 --> 02:34:21,120
function
3859
02:34:21,120 --> 02:34:23,439
now we have something called as ord so
3860
02:34:23,439 --> 02:34:25,120
the ord function
3861
02:34:25,120 --> 02:34:27,439
accepts a string of unit length as an
3862
02:34:27,439 --> 02:34:29,600
argument and returns the unicode
3863
02:34:29,600 --> 02:34:32,479
equivalence of the past argument now we
3864
02:34:32,479 --> 02:34:33,760
also have
3865
02:34:33,760 --> 02:34:35,840
hex and oct so
3866
02:34:35,840 --> 02:34:37,600
the hex function
3867
02:34:37,600 --> 02:34:39,359
is used to convert an integer number to
3868
02:34:39,359 --> 02:34:41,760
its corresponding hexadecimal value
3869
02:34:41,760 --> 02:34:43,920
similarly the oct function will convert
3870
02:34:43,920 --> 02:34:45,520
the corresponding
3871
02:34:45,520 --> 02:34:47,439
or let's say we'll convert an integer
3872
02:34:47,439 --> 02:34:49,359
number to its corresponding octa decimal
3873
02:34:49,359 --> 02:34:50,399
value
3874
02:34:50,399 --> 02:34:54,479
we have tuple list set dictionary str is
3875
02:34:54,479 --> 02:34:56,720
for string you can convert a number into
3876
02:34:56,720 --> 02:34:59,200
a complex number by providing its real
3877
02:34:59,200 --> 02:35:00,800
and imaginary part
3878
02:35:00,800 --> 02:35:02,880
we also have this chr or the character
3879
02:35:02,880 --> 02:35:04,000
function
3880
02:35:04,000 --> 02:35:06,800
now this is how the basic general syntax
3881
02:35:06,800 --> 02:35:08,319
looks like you have the required data
3882
02:35:08,319 --> 02:35:11,200
type followed by the expression
3883
02:35:11,200 --> 02:35:13,200
okay
3884
02:35:13,200 --> 02:35:14,640
so let's head over to our jupyter
3885
02:35:14,640 --> 02:35:16,160
notebook and learn the different types
3886
02:35:16,160 --> 02:35:18,479
of objects as well as
3887
02:35:18,479 --> 02:35:22,399
look at the type conversions in python
3888
02:35:22,720 --> 02:35:24,640
okay so here i am on my
3889
02:35:24,640 --> 02:35:27,200
jupyter notebook let's first
3890
02:35:27,200 --> 02:35:28,800
look at
3891
02:35:28,800 --> 02:35:31,920
number objects so i'm giving a
3892
02:35:31,920 --> 02:35:33,680
comment
3893
02:35:33,680 --> 02:35:35,520
as number objects
3894
02:35:35,520 --> 02:35:37,920
all right let's see i have a variable
3895
02:35:37,920 --> 02:35:39,520
called num
3896
02:35:39,520 --> 02:35:43,280
which is equal to let's say 2
3897
02:35:43,280 --> 02:35:44,720
now
3898
02:35:44,720 --> 02:35:48,640
it's very simple that 2 is an integer
3899
02:35:48,640 --> 02:35:51,600
so if you want to check the type
3900
02:35:51,600 --> 02:35:53,600
you can just use the type function and
3901
02:35:53,600 --> 02:35:56,000
it gives us int which means integer so
3902
02:35:56,000 --> 02:35:58,160
here 2 is of
3903
02:35:58,160 --> 02:36:01,280
a type called number object
3904
02:36:01,280 --> 02:36:03,040
let's say you want to pass a floating
3905
02:36:03,040 --> 02:36:04,080
value
3906
02:36:04,080 --> 02:36:07,479
that's a 2.0
3907
02:36:08,160 --> 02:36:09,760
see the results
3908
02:36:09,760 --> 02:36:12,439
2.0 is of type float and again
3909
02:36:12,439 --> 02:36:14,960
2.0 or 2.0
3910
02:36:14,960 --> 02:36:17,760
is a number object
3911
02:36:17,760 --> 02:36:20,319
now you can perform various operations
3912
02:36:20,319 --> 02:36:22,640
on number objects suppose you want to
3913
02:36:22,640 --> 02:36:24,640
add a few numbers
3914
02:36:24,640 --> 02:36:27,600
so let's say 321 plus
3915
02:36:27,600 --> 02:36:29,520
365
3916
02:36:29,520 --> 02:36:31,760
and i'll say add
3917
02:36:31,760 --> 02:36:33,760
it gives us the addition of these two
3918
02:36:33,760 --> 02:36:36,399
numbers which is 686.
3919
02:36:36,399 --> 02:36:38,000
now let's say
3920
02:36:38,000 --> 02:36:40,640
you want to
3921
02:36:40,800 --> 02:36:43,920
perform a multiplication operation
3922
02:36:43,920 --> 02:36:48,240
let's say 62 multiplied by 2.5
3923
02:36:48,399 --> 02:36:50,479
if i hit shift enter it gives me the
3924
02:36:50,479 --> 02:36:52,000
multiplication of the product of these
3925
02:36:52,000 --> 02:36:53,399
two numbers that is
3926
02:36:53,399 --> 02:36:55,120
55.0
3927
02:36:55,120 --> 02:36:55,920
now
3928
02:36:55,920 --> 02:36:59,040
python by default will
3929
02:36:59,040 --> 02:37:01,439
convert the result to a floating point
3930
02:37:01,439 --> 02:37:04,080
number if there is one integer and this
3931
02:37:04,080 --> 02:37:06,240
one float
3932
02:37:06,240 --> 02:37:08,240
if you want to return the exponent of a
3933
02:37:08,240 --> 02:37:10,319
number let's say i want to know 2 raised
3934
02:37:10,319 --> 02:37:13,280
to the power 4
3935
02:37:13,280 --> 02:37:15,359
so i'm using double asterisks to
3936
02:37:15,359 --> 02:37:18,080
represent the exponent operator
3937
02:37:18,080 --> 02:37:20,960
if i hit shift enter it gives us 16. so
3938
02:37:20,960 --> 02:37:22,399
it's basically
3939
02:37:22,399 --> 02:37:26,160
two multiplied four times
3940
02:37:26,319 --> 02:37:28,640
now you can also
3941
02:37:28,640 --> 02:37:31,760
import specific libraries so let's say i
3942
02:37:31,760 --> 02:37:34,000
want to import the math library
3943
02:37:34,000 --> 02:37:36,560
and i want to print
3944
02:37:36,560 --> 02:37:39,040
the value of pi so here i have written
3945
02:37:39,040 --> 02:37:41,040
import math and then
3946
02:37:41,040 --> 02:37:44,560
using the module math i am
3947
02:37:44,560 --> 02:37:46,800
trying to find the value of pi let's run
3948
02:37:46,800 --> 02:37:50,560
it you see here it says 3.141 so on and
3949
02:37:50,560 --> 02:37:53,040
so forth
3950
02:37:53,439 --> 02:37:55,600
now you can also
3951
02:37:55,600 --> 02:37:57,840
import the random module let's say i'll
3952
02:37:57,840 --> 02:37:58,840
write
3953
02:37:58,840 --> 02:38:00,960
import random
3954
02:38:00,960 --> 02:38:02,000
and then
3955
02:38:02,000 --> 02:38:04,880
let's say i want to print a random
3956
02:38:04,880 --> 02:38:06,479
number
3957
02:38:06,479 --> 02:38:09,359
you can use the random dot
3958
02:38:09,359 --> 02:38:11,840
random function let's run it
3959
02:38:11,840 --> 02:38:13,439
you see here this is a
3960
02:38:13,439 --> 02:38:15,120
random value that
3961
02:38:15,120 --> 02:38:16,960
python has generated if i run it again
3962
02:38:16,960 --> 02:38:18,960
this value changes let's run it again
3963
02:38:18,960 --> 02:38:21,520
again this value changes
3964
02:38:21,520 --> 02:38:23,439
okay
3965
02:38:23,439 --> 02:38:25,280
now we'll shift our focus to
3966
02:38:25,280 --> 02:38:28,800
understanding string objects in python
3967
02:38:28,800 --> 02:38:31,520
so i'll give a comment string objects
3968
02:38:31,520 --> 02:38:35,200
let's say i have a string
3969
02:38:35,200 --> 02:38:38,160
a is equal to and let's say the string
3970
02:38:38,160 --> 02:38:41,880
has india
3971
02:38:43,040 --> 02:38:44,080
okay
3972
02:38:44,080 --> 02:38:47,200
now if you want you can print a it says
3973
02:38:47,200 --> 02:38:48,960
india
3974
02:38:48,960 --> 02:38:52,080
now suppose i want to
3975
02:38:52,080 --> 02:38:54,479
extract
3976
02:38:54,479 --> 02:38:56,960
the value that is present at the zeroth
3977
02:38:56,960 --> 02:38:59,600
index of the string so the way to do is
3978
02:38:59,600 --> 02:39:01,120
i'll pass in my
3979
02:39:01,120 --> 02:39:03,120
variable name that is a and within
3980
02:39:03,120 --> 02:39:04,720
square brackets i'll give the index
3981
02:39:04,720 --> 02:39:06,720
value that is 0. now one thing to
3982
02:39:06,720 --> 02:39:08,319
remember in python the index value
3983
02:39:08,319 --> 02:39:12,720
starts with 0 and not 1. so if i do
3984
02:39:12,720 --> 02:39:15,439
a of 0 it gives me i because i is
3985
02:39:15,439 --> 02:39:17,600
present at the 0th index
3986
02:39:17,600 --> 02:39:20,960
now let's say i want
3987
02:39:20,960 --> 02:39:22,880
the last value
3988
02:39:22,880 --> 02:39:25,920
in this string india so i need to return
3989
02:39:25,920 --> 02:39:29,120
a for that i can use negative indexing
3990
02:39:29,120 --> 02:39:31,040
by using minus 1
3991
02:39:31,040 --> 02:39:32,479
if i do minus
3992
02:39:32,479 --> 02:39:34,240
1 it gives me
3993
02:39:34,240 --> 02:39:35,600
a which is the
3994
02:39:35,600 --> 02:39:39,040
element present at the last index
3995
02:39:39,040 --> 02:39:40,960
now let's say
3996
02:39:40,960 --> 02:39:42,880
i'll write a
3997
02:39:42,880 --> 02:39:45,120
1 and colon
3998
02:39:45,120 --> 02:39:46,479
let's run it
3999
02:39:46,479 --> 02:39:49,280
you see the result here it says ndia
4000
02:39:49,280 --> 02:39:51,280
which means python has returned
4001
02:39:51,280 --> 02:39:53,680
everything starting from the
4002
02:39:53,680 --> 02:39:55,280
first index
4003
02:39:55,280 --> 02:39:56,960
till the last index
4004
02:39:56,960 --> 02:39:59,680
so here colon means you need to return
4005
02:39:59,680 --> 02:40:01,840
everything that comes after the first
4006
02:40:01,840 --> 02:40:04,240
index
4007
02:40:04,640 --> 02:40:05,760
now
4008
02:40:05,760 --> 02:40:06,960
let's see
4009
02:40:06,960 --> 02:40:08,240
i'll write
4010
02:40:08,240 --> 02:40:10,640
print
4011
02:40:10,640 --> 02:40:12,319
a
4012
02:40:12,319 --> 02:40:15,439
and i'll add
4013
02:40:16,240 --> 02:40:19,640
let's say xyz
4014
02:40:20,160 --> 02:40:21,359
run it
4015
02:40:21,359 --> 02:40:22,560
you see here
4016
02:40:22,560 --> 02:40:25,840
this xyz has been concatenated with the
4017
02:40:25,840 --> 02:40:27,680
value that was present inside a that is
4018
02:40:27,680 --> 02:40:30,800
india so the result is india followed by
4019
02:40:30,800 --> 02:40:32,840
xyz
4020
02:40:32,840 --> 02:40:35,359
okay now
4021
02:40:35,359 --> 02:40:38,000
if i write a multiplied by
4022
02:40:38,000 --> 02:40:39,439
7
4023
02:40:39,439 --> 02:40:40,960
it will print
4024
02:40:40,960 --> 02:40:44,080
india 7 times without a space
4025
02:40:44,080 --> 02:40:46,880
you can see it here
4026
02:40:47,279 --> 02:40:48,960
cool
4027
02:40:48,960 --> 02:40:51,439
now let's say you want to replace
4028
02:40:51,439 --> 02:40:52,960
certain values
4029
02:40:52,960 --> 02:40:54,960
in the string you can use the replace
4030
02:40:54,960 --> 02:40:57,680
function so if i write a dot replace
4031
02:40:57,680 --> 02:41:00,960
let's say i want to replace ia
4032
02:41:00,960 --> 02:41:03,279
that is present in india
4033
02:41:03,279 --> 02:41:05,600
with let's say
4034
02:41:05,600 --> 02:41:07,600
xyz
4035
02:41:07,600 --> 02:41:09,040
let's run it
4036
02:41:09,040 --> 02:41:10,880
you see here
4037
02:41:10,880 --> 02:41:14,640
ind is fine after that we have ia and
4038
02:41:14,640 --> 02:41:18,399
that ia has been replaced by xyz
4039
02:41:18,399 --> 02:41:21,760
now let's move on and understand
4040
02:41:21,760 --> 02:41:24,000
list objects in python so i'll write
4041
02:41:24,000 --> 02:41:26,800
list objects
4042
02:41:27,040 --> 02:41:28,080
okay
4043
02:41:28,080 --> 02:41:32,560
so let's go ahead and declare a list l1
4044
02:41:32,560 --> 02:41:34,800
within square brackets
4045
02:41:34,800 --> 02:41:36,880
let's say we have
4046
02:41:36,880 --> 02:41:39,439
values like 101
4047
02:41:39,439 --> 02:41:40,640
let's say
4048
02:41:40,640 --> 02:41:42,880
america
4049
02:41:42,880 --> 02:41:44,399
and then let's say we have another
4050
02:41:44,399 --> 02:41:48,359
floating value let's say 2.05
4051
02:41:48,800 --> 02:41:50,640
this is my list if you want to print the
4052
02:41:50,640 --> 02:41:52,399
list you can use
4053
02:41:52,399 --> 02:41:54,960
l1 to print it you see here this is my
4054
02:41:54,960 --> 02:41:56,479
list l1
4055
02:41:56,479 --> 02:41:59,120
now if you want to find the
4056
02:41:59,120 --> 02:42:00,960
length of l1 you can use the len
4057
02:42:00,960 --> 02:42:02,160
function
4058
02:42:02,160 --> 02:42:03,600
if i run it
4059
02:42:03,600 --> 02:42:05,840
you get 3 which means there are 3
4060
02:42:05,840 --> 02:42:08,000
elements in the list 101 america and
4061
02:42:08,000 --> 02:42:10,880
2.05
4062
02:42:11,359 --> 02:42:12,800
now if you want to extract certain
4063
02:42:12,800 --> 02:42:14,319
values from the list for that you can
4064
02:42:14,319 --> 02:42:17,040
use the indexing method let's say i'll
4065
02:42:17,040 --> 02:42:18,720
write
4066
02:42:18,720 --> 02:42:20,720
l1 of
4067
02:42:20,720 --> 02:42:21,920
1
4068
02:42:21,920 --> 02:42:24,640
that gives me america because
4069
02:42:24,640 --> 02:42:27,439
at index position 1 we have america so
4070
02:42:27,439 --> 02:42:30,160
101 is at the 0th index america is at
4071
02:42:30,160 --> 02:42:32,880
the first index and 2.05 is at the
4072
02:42:32,880 --> 02:42:35,680
second index
4073
02:42:36,319 --> 02:42:38,240
next
4074
02:42:38,240 --> 02:42:39,760
let's say i write
4075
02:42:39,760 --> 02:42:41,760
l1
4076
02:42:41,760 --> 02:42:43,200
and then
4077
02:42:43,200 --> 02:42:46,800
i give colon -1 let's print it so that
4078
02:42:46,800 --> 02:42:48,160
gives me
4079
02:42:48,160 --> 02:42:51,040
101 in america so what it does is
4080
02:42:51,040 --> 02:42:53,520
this colon minus 1 will exclude the last
4081
02:42:53,520 --> 02:42:56,000
index and print the other two
4082
02:42:56,000 --> 02:42:59,120
elements in the list
4083
02:42:59,279 --> 02:43:00,640
okay
4084
02:43:00,640 --> 02:43:02,399
now if you want to
4085
02:43:02,399 --> 02:43:03,680
append
4086
02:43:03,680 --> 02:43:05,600
some elements to the list you can use
4087
02:43:05,600 --> 02:43:07,920
the append method so i write l1 dot
4088
02:43:07,920 --> 02:43:08,880
append
4089
02:43:08,880 --> 02:43:11,920
within brackets i'll give my value let's
4090
02:43:11,920 --> 02:43:13,200
say
4091
02:43:13,200 --> 02:43:15,760
usa
4092
02:43:16,240 --> 02:43:21,640
now let's go ahead and print l1
4093
02:43:22,560 --> 02:43:24,640
if i run it you can see the difference
4094
02:43:24,640 --> 02:43:25,680
earlier
4095
02:43:25,680 --> 02:43:28,399
my list had three elements 101 america
4096
02:43:28,399 --> 02:43:31,120
and 2.05 now we have added the fourth
4097
02:43:31,120 --> 02:43:34,160
element that is usa
4098
02:43:34,160 --> 02:43:35,439
cool
4099
02:43:35,439 --> 02:43:37,200
now you can also use the
4100
02:43:37,200 --> 02:43:39,359
pop function in
4101
02:43:39,359 --> 02:43:41,600
python let's see what it does
4102
02:43:41,600 --> 02:43:44,720
so i write l1 dot pop and i'll give
4103
02:43:44,720 --> 02:43:46,160
let's say three
4104
02:43:46,160 --> 02:43:48,800
if i run it it returns me
4105
02:43:48,800 --> 02:43:51,520
usa because usa is present at the
4106
02:43:51,520 --> 02:43:54,000
third index of my list
4107
02:43:54,000 --> 02:43:56,720
if i write pop one
4108
02:43:56,720 --> 02:43:59,120
it gives me america because
4109
02:43:59,120 --> 02:44:02,720
america is present at the first index
4110
02:44:02,720 --> 02:44:03,680
all right
4111
02:44:03,680 --> 02:44:05,200
now you can also
4112
02:44:05,200 --> 02:44:06,960
reverse the elements in a list if i
4113
02:44:06,960 --> 02:44:08,880
write l1 dot
4114
02:44:08,880 --> 02:44:11,520
reverse function
4115
02:44:11,520 --> 02:44:14,800
if i run this and then i'll
4116
02:44:14,800 --> 02:44:18,080
print l1
4117
02:44:18,080 --> 02:44:20,720
you see the result here we have 2.05 and
4118
02:44:20,720 --> 02:44:24,399
101 which is printed in reverse order
4119
02:44:24,399 --> 02:44:26,160
the reason being
4120
02:44:26,160 --> 02:44:28,560
we have already popped america and usa
4121
02:44:28,560 --> 02:44:31,040
out and we are only left with 101 and
4122
02:44:31,040 --> 02:44:33,520
205 and if you reverse
4123
02:44:33,520 --> 02:44:34,880
the current list
4124
02:44:34,880 --> 02:44:38,399
it will be printed as follows so 2.05
4125
02:44:38,399 --> 02:44:39,359
comes
4126
02:44:39,359 --> 02:44:41,359
at the beginning and then we have
4127
02:44:41,359 --> 02:44:42,800
101.
4128
02:44:42,800 --> 02:44:44,399
let me just
4129
02:44:44,399 --> 02:44:46,640
rerun my list again to show you the
4130
02:44:46,640 --> 02:44:48,560
difference
4131
02:44:48,560 --> 02:44:51,359
i have run my l1
4132
02:44:51,359 --> 02:44:54,080
let's run this append query
4133
02:44:54,080 --> 02:44:55,120
and now
4134
02:44:55,120 --> 02:44:56,960
let's use the
4135
02:44:56,960 --> 02:44:59,359
reverse function there you go
4136
02:44:59,359 --> 02:45:01,600
so now our original list has been
4137
02:45:01,600 --> 02:45:03,439
reversed so you have us at the beginning
4138
02:45:03,439 --> 02:45:07,359
and 101 towards the end
4139
02:45:07,359 --> 02:45:08,960
okay
4140
02:45:08,960 --> 02:45:11,760
now moving ahead let's see
4141
02:45:11,760 --> 02:45:13,760
the next type of object in python that
4142
02:45:13,760 --> 02:45:16,880
is dictionaries
4143
02:45:17,520 --> 02:45:19,600
okay
4144
02:45:19,600 --> 02:45:21,279
let's see i'll create a dictionary
4145
02:45:21,279 --> 02:45:23,840
called dict 1
4146
02:45:23,840 --> 02:45:25,840
and you might be aware that dictionaries
4147
02:45:25,840 --> 02:45:29,279
are created using the curly braces
4148
02:45:29,279 --> 02:45:30,960
so i'll have my
4149
02:45:30,960 --> 02:45:34,000
key value pairs let's say my key is 1
4150
02:45:34,000 --> 02:45:37,359
and the value it holds is let's say
4151
02:45:37,359 --> 02:45:39,760
the names of animals
4152
02:45:39,760 --> 02:45:41,120
tiger
4153
02:45:41,120 --> 02:45:44,640
comma we'll have the second key and that
4154
02:45:44,640 --> 02:45:46,560
second key will have the value let's say
4155
02:45:46,560 --> 02:45:48,160
lion
4156
02:45:48,160 --> 02:45:49,439
and let's see
4157
02:45:49,439 --> 02:45:52,080
we have another element with the third
4158
02:45:52,080 --> 02:45:53,279
key
4159
02:45:53,279 --> 02:45:54,560
and let's see
4160
02:45:54,560 --> 02:45:58,560
we have mouse
4161
02:45:59,040 --> 02:46:00,880
okay
4162
02:46:00,880 --> 02:46:02,240
now
4163
02:46:02,240 --> 02:46:05,680
let's print our dictionary
4164
02:46:08,160 --> 02:46:12,319
so this is how my dictionary looks like
4165
02:46:12,319 --> 02:46:14,240
now
4166
02:46:14,240 --> 02:46:15,439
you can
4167
02:46:15,439 --> 02:46:17,520
display the keys
4168
02:46:17,520 --> 02:46:20,160
by using the keys function
4169
02:46:20,160 --> 02:46:22,560
so this will return all the keys present
4170
02:46:22,560 --> 02:46:25,200
in the dictionary you see here 1 2 and 3
4171
02:46:25,200 --> 02:46:26,640
are my keys
4172
02:46:26,640 --> 02:46:28,240
similarly
4173
02:46:28,240 --> 02:46:29,840
you can display the values in the
4174
02:46:29,840 --> 02:46:32,000
dictionary
4175
02:46:32,000 --> 02:46:34,560
you need to change this keys to values
4176
02:46:34,560 --> 02:46:36,399
if i run it you see here
4177
02:46:36,399 --> 02:46:38,399
tiger lion and mouse are the
4178
02:46:38,399 --> 02:46:41,040
three values
4179
02:46:41,279 --> 02:46:43,520
now suppose you want to add another
4180
02:46:43,520 --> 02:46:46,000
element to your dictionary for that you
4181
02:46:46,000 --> 02:46:46,960
can write
4182
02:46:46,960 --> 02:46:48,880
dict 1
4183
02:46:48,880 --> 02:46:50,240
and give the
4184
02:46:50,240 --> 02:46:53,120
fourth key equal to
4185
02:46:53,120 --> 02:46:56,720
the value let's say cat
4186
02:46:57,279 --> 02:46:59,680
now let's run
4187
02:46:59,680 --> 02:47:02,319
dict 1 and see the difference
4188
02:47:02,319 --> 02:47:05,359
there you go so here we have added
4189
02:47:05,359 --> 02:47:07,760
another element to the dictionary which
4190
02:47:07,760 --> 02:47:10,240
is cat
4191
02:47:13,359 --> 02:47:14,720
now moving ahead
4192
02:47:14,720 --> 02:47:16,720
let's explore
4193
02:47:16,720 --> 02:47:20,120
double objects
4194
02:47:26,319 --> 02:47:29,200
okay so we'll create a tuple called t1
4195
02:47:29,200 --> 02:47:31,040
equal to
4196
02:47:31,040 --> 02:47:33,200
we'll have the values 1 comma 2 comma 3
4197
02:47:33,200 --> 02:47:34,880
comma 4
4198
02:47:34,880 --> 02:47:36,479
and let's say you want to print the
4199
02:47:36,479 --> 02:47:39,200
length of tuple t1 it gives you 4
4200
02:47:39,200 --> 02:47:41,120
because we have 4 elements in the tuple
4201
02:47:41,120 --> 02:47:42,720
t1
4202
02:47:42,720 --> 02:47:45,359
now let's say i'll declare
4203
02:47:45,359 --> 02:47:48,000
one more tuple called t2 and this time
4204
02:47:48,000 --> 02:47:49,600
we'll have two more elements in the
4205
02:47:49,600 --> 02:47:53,200
tuple so i'll write t1 plus
4206
02:47:53,200 --> 02:47:55,200
followed by the two elements that i want
4207
02:47:55,200 --> 02:47:57,040
let's say 7 comma 9
4208
02:47:57,040 --> 02:48:00,000
and i'll print t2
4209
02:48:00,000 --> 02:48:01,279
if i run it
4210
02:48:01,279 --> 02:48:03,760
you can see here we have added 7 and 9
4211
02:48:03,760 --> 02:48:06,800
to our original tuple that had 1 comma 2
4212
02:48:06,800 --> 02:48:08,560
comma 3 comma 4.
4213
02:48:08,560 --> 02:48:10,479
now you can also use the indexing method
4214
02:48:10,479 --> 02:48:11,920
or the slicing
4215
02:48:11,920 --> 02:48:13,760
technique to extract values from the
4216
02:48:13,760 --> 02:48:15,760
tuple let's say i write
4217
02:48:15,760 --> 02:48:18,560
t2 of 3 it will return the third element
4218
02:48:18,560 --> 02:48:21,200
that is present in tuple
4219
02:48:21,200 --> 02:48:24,160
d2 so if you see here
4220
02:48:24,160 --> 02:48:25,439
my
4221
02:48:25,439 --> 02:48:28,399
third index has the element 4 hence it
4222
02:48:28,399 --> 02:48:30,720
has returned four
4223
02:48:30,720 --> 02:48:32,160
cool
4224
02:48:32,160 --> 02:48:35,040
now moving ahead
4225
02:48:35,600 --> 02:48:37,040
let's see
4226
02:48:37,040 --> 02:48:41,600
how file objects work in python
4227
02:48:42,479 --> 02:48:44,880
all right now
4228
02:48:44,880 --> 02:48:46,960
using python you can create
4229
02:48:46,960 --> 02:48:48,160
files
4230
02:48:48,160 --> 02:48:50,560
let's say i want to create a text file
4231
02:48:50,560 --> 02:48:53,760
in python using the jupyter notebook for
4232
02:48:53,760 --> 02:48:56,840
that i can write f1 equal
4233
02:48:56,840 --> 02:49:01,040
to i'll use a function called open
4234
02:49:01,040 --> 02:49:02,960
and inside this open function i want to
4235
02:49:02,960 --> 02:49:05,920
create a text file and first
4236
02:49:05,920 --> 02:49:09,040
i'll write something into that file
4237
02:49:09,040 --> 02:49:11,439
let me show you so this is a folder that
4238
02:49:11,439 --> 02:49:12,880
i have
4239
02:49:12,880 --> 02:49:14,960
the folder name is called object types
4240
02:49:14,960 --> 02:49:17,760
and it is present in my desktop so i'll
4241
02:49:17,760 --> 02:49:20,319
use this location
4242
02:49:20,319 --> 02:49:22,800
and here you can see now this folder
4243
02:49:22,800 --> 02:49:25,760
contains no files as of now but we are
4244
02:49:25,760 --> 02:49:28,240
going to create text file using python
4245
02:49:28,240 --> 02:49:30,399
let's see how to do it
4246
02:49:30,399 --> 02:49:32,880
so here i'll paste my
4247
02:49:32,880 --> 02:49:35,760
location that i just copied
4248
02:49:35,760 --> 02:49:37,439
now one thing to note is you need to
4249
02:49:37,439 --> 02:49:41,279
convert everything to
4250
02:49:41,279 --> 02:49:42,319
forward
4251
02:49:42,319 --> 02:49:44,800
slash or you can use
4252
02:49:44,800 --> 02:49:47,920
double backslash also
4253
02:49:47,920 --> 02:49:48,960
i'll give
4254
02:49:48,960 --> 02:49:50,960
another forward slash
4255
02:49:50,960 --> 02:49:54,479
and let's say i want to
4256
02:49:54,479 --> 02:49:57,279
create a file in the name of data.txt so
4257
02:49:57,279 --> 02:49:59,359
it is going to be a text file
4258
02:49:59,359 --> 02:50:01,520
make sure everything is
4259
02:50:01,520 --> 02:50:04,319
inside single quotation
4260
02:50:04,319 --> 02:50:07,439
and i want to open the file in
4261
02:50:07,439 --> 02:50:10,479
write mode so i'll write w here let's
4262
02:50:10,479 --> 02:50:12,800
run it and see the difference
4263
02:50:12,800 --> 02:50:15,120
now if we check our folder
4264
02:50:15,120 --> 02:50:17,040
you see here earlier there was no data
4265
02:50:17,040 --> 02:50:19,600
file now we have created a text file in
4266
02:50:19,600 --> 02:50:22,479
the name of data you see here there are
4267
02:50:22,479 --> 02:50:24,080
no
4268
02:50:24,080 --> 02:50:25,840
information or data inside this text
4269
02:50:25,840 --> 02:50:28,240
file next we are going to add some data
4270
02:50:28,240 --> 02:50:30,160
to this text file
4271
02:50:30,160 --> 02:50:31,439
let's
4272
02:50:31,439 --> 02:50:33,600
go back to our jupyter notebook
4273
02:50:33,600 --> 02:50:35,520
okay
4274
02:50:35,520 --> 02:50:37,920
so we'll write some data to that file so
4275
02:50:37,920 --> 02:50:39,920
i'll write f1 dot
4276
02:50:39,920 --> 02:50:42,399
after that i'll use the write function
4277
02:50:42,399 --> 02:50:44,240
and within single quotations i'll write
4278
02:50:44,240 --> 02:50:45,920
let's say hello
4279
02:50:45,920 --> 02:50:48,240
simply learn
4280
02:50:48,240 --> 02:50:49,600
and then
4281
02:50:49,600 --> 02:50:52,319
i'll give backslash n to go to the next
4282
02:50:52,319 --> 02:50:54,080
line
4283
02:50:54,080 --> 02:50:57,359
i'll write again f1.write
4284
02:50:57,359 --> 02:50:59,600
and i'll pass in another message let's
4285
02:50:59,600 --> 02:51:02,720
say welcome to
4286
02:51:02,840 --> 02:51:05,439
python programming
4287
02:51:05,439 --> 02:51:07,279
and we'll give another backslash
4288
02:51:07,279 --> 02:51:09,200
followed by n
4289
02:51:09,200 --> 02:51:11,359
and then finally we'll close the file so
4290
02:51:11,359 --> 02:51:13,279
i'll write f1 dot
4291
02:51:13,279 --> 02:51:15,040
close function
4292
02:51:15,040 --> 02:51:16,560
let's run this
4293
02:51:16,560 --> 02:51:19,040
and we'll see the difference now
4294
02:51:19,040 --> 02:51:21,359
if i open this the yuko
4295
02:51:21,359 --> 02:51:23,120
we have some data in this
4296
02:51:23,120 --> 02:51:25,920
text file that is hello simply learn
4297
02:51:25,920 --> 02:51:28,240
welcome to python programming
4298
02:51:28,240 --> 02:51:31,439
so this is how file objects work in
4299
02:51:31,439 --> 02:51:33,279
python
4300
02:51:33,279 --> 02:51:34,800
all right
4301
02:51:34,800 --> 02:51:37,200
now moving ahead we'll see
4302
02:51:37,200 --> 02:51:39,040
how
4303
02:51:39,040 --> 02:51:42,160
sets work in python
4304
02:51:42,160 --> 02:51:45,279
suppose i create a set called s
4305
02:51:45,279 --> 02:51:47,680
and i'll use the set function
4306
02:51:47,680 --> 02:51:49,760
and i'll pass in
4307
02:51:49,760 --> 02:51:51,200
india
4308
02:51:51,200 --> 02:51:53,279
so this is a string
4309
02:51:53,279 --> 02:51:55,840
and i want to convert the string to a
4310
02:51:55,840 --> 02:51:57,680
set
4311
02:51:57,680 --> 02:51:58,840
let's say i have
4312
02:51:58,840 --> 02:52:02,160
another set called s1
4313
02:52:02,160 --> 02:52:05,439
and here i have
4314
02:52:05,520 --> 02:52:09,279
a list of values let's say u
4315
02:52:09,279 --> 02:52:11,520
s
4316
02:52:12,240 --> 02:52:14,800
and let's say a
4317
02:52:14,800 --> 02:52:16,080
now if i write
4318
02:52:16,080 --> 02:52:17,840
s comma
4319
02:52:17,840 --> 02:52:19,200
s1
4320
02:52:19,200 --> 02:52:21,439
so what set does is
4321
02:52:21,439 --> 02:52:23,359
it will return only the unique values
4322
02:52:23,359 --> 02:52:26,000
that are present inside each set so here
4323
02:52:26,000 --> 02:52:27,120
if you see
4324
02:52:27,120 --> 02:52:28,399
india
4325
02:52:28,399 --> 02:52:31,120
all the elements are unique except i
4326
02:52:31,120 --> 02:52:33,040
because i is repeated twice so it will
4327
02:52:33,040 --> 02:52:36,560
return i only once in the result but usa
4328
02:52:36,560 --> 02:52:39,120
has all the elements present only once
4329
02:52:39,120 --> 02:52:40,560
so
4330
02:52:40,560 --> 02:52:43,439
set will return all the elements
4331
02:52:43,439 --> 02:52:45,840
now what if i'll
4332
02:52:45,840 --> 02:52:47,920
write let's say s
4333
02:52:47,920 --> 02:52:49,600
ampersand
4334
02:52:49,600 --> 02:52:51,840
s1
4335
02:52:51,840 --> 02:52:52,880
so
4336
02:52:52,880 --> 02:52:55,359
this will give me only the
4337
02:52:55,359 --> 02:52:57,359
common element that is present in both
4338
02:52:57,359 --> 02:52:59,680
the sets so if you see here in india and
4339
02:52:59,680 --> 02:53:01,600
in u s and a
4340
02:53:01,600 --> 02:53:03,680
only a is repeating in both the sets so
4341
02:53:03,680 --> 02:53:05,920
it gives me a
4342
02:53:05,920 --> 02:53:07,520
suppose i write
4343
02:53:07,520 --> 02:53:09,200
s
4344
02:53:09,200 --> 02:53:11,680
this is the or operator and i'll write
4345
02:53:11,680 --> 02:53:12,960
s1
4346
02:53:12,960 --> 02:53:16,080
if i run it it gives me
4347
02:53:16,080 --> 02:53:18,560
all the elements that are present inside
4348
02:53:18,560 --> 02:53:20,800
set s and s1
4349
02:53:20,800 --> 02:53:22,880
excluding the repeating elements so here
4350
02:53:22,880 --> 02:53:24,880
i is repeating so it has printed only
4351
02:53:24,880 --> 02:53:27,279
once
4352
02:53:27,680 --> 02:53:29,920
similarly you can write
4353
02:53:29,920 --> 02:53:32,640
s minus s1
4354
02:53:32,640 --> 02:53:34,240
so you get
4355
02:53:34,240 --> 02:53:35,520
only
4356
02:53:35,520 --> 02:53:38,800
those elements that are present inside
4357
02:53:38,800 --> 02:53:44,319
set s and are not present in set s1
4358
02:53:44,319 --> 02:53:47,680
now in set s e is present as well as is
4359
02:53:47,680 --> 02:53:50,960
present in set s1 so it has excluded e
4360
02:53:50,960 --> 02:53:53,840
from the result
4361
02:53:53,840 --> 02:53:56,080
okay
4362
02:53:56,640 --> 02:53:59,120
now we'll see some operations let's say
4363
02:53:59,120 --> 02:54:01,200
i'll write equal to 7
4364
02:54:01,200 --> 02:54:04,840
and be equal to let's say 2.0
4365
02:54:04,840 --> 02:54:06,479
i'll
4366
02:54:06,479 --> 02:54:10,720
add a and b and store the result in
4367
02:54:10,720 --> 02:54:15,040
my variable r which stands for result
4368
02:54:15,040 --> 02:54:17,359
let's see
4369
02:54:17,359 --> 02:54:21,279
the output if i write print r
4370
02:54:21,279 --> 02:54:22,640
it gives me
4371
02:54:22,640 --> 02:54:24,080
9.0
4372
02:54:24,080 --> 02:54:25,840
and if i check
4373
02:54:25,840 --> 02:54:29,520
type of r it gives me
4374
02:54:29,520 --> 02:54:31,040
there's a spelling mistake i'll write
4375
02:54:31,040 --> 02:54:33,920
type of r it gives me float so here
4376
02:54:33,920 --> 02:54:36,240
python is performing
4377
02:54:36,240 --> 02:54:37,520
implicit
4378
02:54:37,520 --> 02:54:40,399
type conversion so if you add an integer
4379
02:54:40,399 --> 02:54:42,640
with a floating point number the result
4380
02:54:42,640 --> 02:54:44,319
will be a floating point number and not
4381
02:54:44,319 --> 02:54:46,640
an integer
4382
02:54:46,640 --> 02:54:48,800
now if you
4383
02:54:48,800 --> 02:54:50,640
say equal to 7
4384
02:54:50,640 --> 02:54:54,319
and let's say b equal to same 2.0
4385
02:54:54,319 --> 02:54:57,040
and this time i am explicitly defining
4386
02:54:57,040 --> 02:54:59,760
my result to be an integer so i am using
4387
02:54:59,760 --> 02:55:03,520
an integer function so if i write a
4388
02:55:03,520 --> 02:55:05,200
plus
4389
02:55:05,200 --> 02:55:06,479
b
4390
02:55:06,479 --> 02:55:08,720
and if i print r
4391
02:55:08,720 --> 02:55:10,720
you see the result it has given me an
4392
02:55:10,720 --> 02:55:12,160
integer which is
4393
02:55:12,160 --> 02:55:13,040
9.
4394
02:55:13,040 --> 02:55:16,479
now to verify you can also use
4395
02:55:16,479 --> 02:55:18,960
type of r
4396
02:55:18,960 --> 02:55:22,160
it gives me integer but earlier
4397
02:55:22,160 --> 02:55:25,200
it returned us float
4398
02:55:25,200 --> 02:55:27,359
so this is how
4399
02:55:27,359 --> 02:55:28,479
implicit
4400
02:55:28,479 --> 02:55:30,640
and explicit conversion
4401
02:55:30,640 --> 02:55:33,040
work in python
4402
02:55:33,040 --> 02:55:34,800
now this example was for numbers
4403
02:55:34,800 --> 02:55:37,680
similarly you can
4404
02:55:37,680 --> 02:55:39,600
also
4405
02:55:39,600 --> 02:55:41,600
perform for strings
4406
02:55:41,600 --> 02:55:45,680
and for less tuples and sets
4407
02:55:45,680 --> 02:55:48,319
moving ahead
4408
02:55:49,439 --> 02:55:51,279
one thing i wanted to tell you was you
4409
02:55:51,279 --> 02:55:53,439
cannot add a string
4410
02:55:53,439 --> 02:55:55,120
and an integer
4411
02:55:55,120 --> 02:55:57,120
suppose
4412
02:55:57,120 --> 02:55:58,399
i'll give a comment
4413
02:55:58,399 --> 02:55:59,600
string
4414
02:55:59,600 --> 02:56:03,520
and integer addition
4415
02:56:05,040 --> 02:56:07,359
suppose i have a variable called a which
4416
02:56:07,359 --> 02:56:10,000
has a value of a 7 so 7 is an integer
4417
02:56:10,000 --> 02:56:12,800
here then i have a variable called p
4418
02:56:12,800 --> 02:56:15,520
which has a value 2 but this time i'll
4419
02:56:15,520 --> 02:56:17,760
assign this to within
4420
02:56:17,760 --> 02:56:20,000
quotes so this becomes
4421
02:56:20,000 --> 02:56:23,200
of type string
4422
02:56:23,200 --> 02:56:24,640
now if you want you can check that as
4423
02:56:24,640 --> 02:56:28,640
well if i write type of 2
4424
02:56:29,200 --> 02:56:31,760
rather type of b it will give us str
4425
02:56:31,760 --> 02:56:33,920
which means string
4426
02:56:33,920 --> 02:56:36,160
now let's see
4427
02:56:36,160 --> 02:56:38,960
i'll write r which stands for result
4428
02:56:38,960 --> 02:56:40,640
equal to
4429
02:56:40,640 --> 02:56:44,240
a plus b
4430
02:56:44,399 --> 02:56:46,640
now if i print r
4431
02:56:46,640 --> 02:56:48,399
it will give us an error because you
4432
02:56:48,399 --> 02:56:50,720
cannot add a string with an
4433
02:56:50,720 --> 02:56:51,840
integer
4434
02:56:51,840 --> 02:56:55,359
so it says unsupported operand
4435
02:56:55,359 --> 02:56:58,319
the way to fix this is you need to
4436
02:56:58,319 --> 02:57:00,560
convert this string into an integer
4437
02:57:00,560 --> 02:57:02,479
explicitly
4438
02:57:02,479 --> 02:57:06,240
so this time we'll say equal to 7
4439
02:57:06,240 --> 02:57:07,600
b
4440
02:57:07,600 --> 02:57:10,160
equal to
4441
02:57:10,640 --> 02:57:12,160
2 so
4442
02:57:12,160 --> 02:57:13,760
it is a string
4443
02:57:13,760 --> 02:57:15,200
now i'll write
4444
02:57:15,200 --> 02:57:18,399
r equal to
4445
02:57:18,800 --> 02:57:19,760
a
4446
02:57:19,760 --> 02:57:20,880
plus
4447
02:57:20,880 --> 02:57:23,200
i'm going to use the integer function or
4448
02:57:23,200 --> 02:57:25,279
the int function to convert my b to an
4449
02:57:25,279 --> 02:57:26,720
integer
4450
02:57:26,720 --> 02:57:29,439
and then i'll print r again let's do
4451
02:57:29,439 --> 02:57:31,920
this there you go so here we have
4452
02:57:31,920 --> 02:57:34,880
converted my string value to
4453
02:57:34,880 --> 02:57:36,640
a numeric value
4454
02:57:36,640 --> 02:57:39,439
of 2 and this time it works fine so 7
4455
02:57:39,439 --> 02:57:42,160
plus 2 is 9
4456
02:57:42,160 --> 02:57:44,479
okay
4457
02:57:44,800 --> 02:57:46,560
now let's see how you can convert a
4458
02:57:46,560 --> 02:57:49,120
float value or a floating number into an
4459
02:57:49,120 --> 02:57:51,600
integer number let's say i have a
4460
02:57:51,600 --> 02:57:53,520
variable called a which has a value
4461
02:57:53,520 --> 02:57:56,160
let's say 77.9
4462
02:57:56,160 --> 02:57:59,040
and i'm going to create another variable
4463
02:57:59,040 --> 02:58:01,520
called int underscore a
4464
02:58:01,520 --> 02:58:03,680
and i'll pass this to my integer
4465
02:58:03,680 --> 02:58:05,680
function
4466
02:58:05,680 --> 02:58:07,680
so it has int of a
4467
02:58:07,680 --> 02:58:09,600
now let's print
4468
02:58:09,600 --> 02:58:11,920
int underscore a
4469
02:58:11,920 --> 02:58:14,960
so you have 77 so
4470
02:58:14,960 --> 02:58:16,720
my integer function has truncated the
4471
02:58:16,720 --> 02:58:19,680
decimal point value that had 0.9
4472
02:58:19,680 --> 02:58:22,080
if you want to check the type you can
4473
02:58:22,080 --> 02:58:23,439
use
4474
02:58:23,439 --> 02:58:27,040
the type function i'll write type of
4475
02:58:27,040 --> 02:58:29,520
hint underscore a
4476
02:58:29,520 --> 02:58:33,840
so it gives us integer
4477
02:58:34,399 --> 02:58:36,640
now you can also convert an integer to a
4478
02:58:36,640 --> 02:58:38,479
floating point value
4479
02:58:38,479 --> 02:58:40,319
so if i have a variable called a that
4480
02:58:40,319 --> 02:58:42,560
has a value of let's say
4481
02:58:42,560 --> 02:58:44,160
50
4482
02:58:44,160 --> 02:58:47,520
and if i write float underscore e equal
4483
02:58:47,520 --> 02:58:49,279
to i'll use the float function to
4484
02:58:49,279 --> 02:58:50,479
convert the
4485
02:58:50,479 --> 02:58:54,000
variable e to a floating point value
4486
02:58:54,000 --> 02:58:55,200
and let's
4487
02:58:55,200 --> 02:58:57,680
print float underscore a
4488
02:58:57,680 --> 02:59:02,479
so it gives us 50 50.0 instead of 50.
4489
02:59:02,479 --> 02:59:05,920
you can also check the type
4490
02:59:05,920 --> 02:59:07,439
of float a
4491
02:59:07,439 --> 02:59:09,840
it gives you float
4492
02:59:09,840 --> 02:59:12,080
okay
4493
02:59:12,080 --> 02:59:15,040
now you can also convert an integer to a
4494
02:59:15,040 --> 02:59:16,800
complex number
4495
02:59:16,800 --> 02:59:18,560
let's say i'll take the same value equal
4496
02:59:18,560 --> 02:59:22,160
to 50 i'll write comp underscore e which
4497
02:59:22,160 --> 02:59:24,319
stands for complex a
4498
02:59:24,319 --> 02:59:26,800
i'll use the complex function
4499
02:59:26,800 --> 02:59:29,920
and pass in my
4500
02:59:29,920 --> 02:59:31,520
variable that is
4501
02:59:31,520 --> 02:59:33,279
e
4502
02:59:33,279 --> 02:59:34,319
now let's
4503
02:59:34,319 --> 02:59:36,640
print
4504
02:59:36,640 --> 02:59:38,800
comp underscore a
4505
02:59:38,800 --> 02:59:40,120
so it gives us
4506
02:59:40,120 --> 02:59:44,960
50.0 j so 50 is the real part and 0 j is
4507
02:59:44,960 --> 02:59:48,080
the imaginary part
4508
02:59:49,600 --> 02:59:51,840
you can also convert an
4509
02:59:51,840 --> 02:59:54,240
integer to a string
4510
02:59:54,240 --> 02:59:57,200
i'll take the same value let's say 50
4511
02:59:57,200 --> 03:00:01,040
i'll write str underscore a equal to
4512
03:00:01,040 --> 03:00:03,520
str of a so i'm using the string
4513
03:00:03,520 --> 03:00:05,040
function to convert my integer to a
4514
03:00:05,040 --> 03:00:06,240
string
4515
03:00:06,240 --> 03:00:09,520
let's print str underscore a
4516
03:00:09,520 --> 03:00:10,960
it should be an underscore and not an
4517
03:00:10,960 --> 03:00:13,439
hyphen
4518
03:00:13,439 --> 03:00:16,479
let's run it there you go you have 50
4519
03:00:16,479 --> 03:00:18,160
within single quotes
4520
03:00:18,160 --> 03:00:22,240
you can also check the type of
4521
03:00:22,840 --> 03:00:28,080
a htr underscore a it gives you string
4522
03:00:28,080 --> 03:00:30,080
cool
4523
03:00:30,080 --> 03:00:31,520
now
4524
03:00:31,520 --> 03:00:34,160
we learn how to convert a string to a
4525
03:00:34,160 --> 03:00:35,600
tuple
4526
03:00:35,600 --> 03:00:39,200
so let's say i have a string that has
4527
03:00:39,200 --> 03:00:42,479
india as its value
4528
03:00:42,479 --> 03:00:43,680
if i write
4529
03:00:43,680 --> 03:00:46,560
ts equal to
4530
03:00:46,560 --> 03:00:48,319
i'll use the tuple function and pass in
4531
03:00:48,319 --> 03:00:50,800
my variable that is s
4532
03:00:50,800 --> 03:00:53,760
and then we'll print ts
4533
03:00:53,760 --> 03:00:55,279
you can see here
4534
03:00:55,279 --> 03:00:57,439
python has converted my string s which
4535
03:00:57,439 --> 03:01:00,319
had india into a tuple of individual
4536
03:01:00,319 --> 03:01:01,279
elements
4537
03:01:01,279 --> 03:01:05,680
and my tuple is present inside brackets
4538
03:01:05,680 --> 03:01:07,279
cool
4539
03:01:07,279 --> 03:01:09,520
now similarly you can convert a string
4540
03:01:09,520 --> 03:01:10,960
to a list
4541
03:01:10,960 --> 03:01:14,640
i'll use the same string as equal to
4542
03:01:14,640 --> 03:01:16,479
india
4543
03:01:16,479 --> 03:01:18,399
and then let's say i want to convert it
4544
03:01:18,399 --> 03:01:21,760
to a list i'll use l1 equal to
4545
03:01:21,760 --> 03:01:24,000
followed by the list function and inside
4546
03:01:24,000 --> 03:01:25,279
the list function i'll pass in my
4547
03:01:25,279 --> 03:01:27,680
variable that is s
4548
03:01:27,680 --> 03:01:30,319
and let's print l1 and see the
4549
03:01:30,319 --> 03:01:31,520
difference
4550
03:01:31,520 --> 03:01:32,720
you see here
4551
03:01:32,720 --> 03:01:35,040
my string has now been converted into a
4552
03:01:35,040 --> 03:01:38,560
list you can also check the data type of
4553
03:01:38,560 --> 03:01:40,880
l1 or the object type of l1
4554
03:01:40,880 --> 03:01:44,319
it returns you list
4555
03:01:44,319 --> 03:01:48,800
similarly you can also check the type of
4556
03:01:48,800 --> 03:01:51,200
ts
4557
03:01:51,279 --> 03:01:53,200
it gives you tuple because we converted
4558
03:01:53,200 --> 03:01:54,160
that
4559
03:01:54,160 --> 03:01:57,279
string to a tuple
4560
03:02:00,319 --> 03:02:02,479
now let's explore how you can convert a
4561
03:02:02,479 --> 03:02:04,640
tuple to a list let's say you have a
4562
03:02:04,640 --> 03:02:06,319
tuple called t
4563
03:02:06,319 --> 03:02:08,560
and it has a list of names let's say
4564
03:02:08,560 --> 03:02:10,720
john
4565
03:02:10,720 --> 03:02:13,600
we have mary
4566
03:02:13,600 --> 03:02:17,720
then we have let's say mark
4567
03:02:18,240 --> 03:02:20,479
now to convert into a list we'll use the
4568
03:02:20,479 --> 03:02:22,960
list function again let's say
4569
03:02:22,960 --> 03:02:24,240
l2
4570
03:02:24,240 --> 03:02:26,640
equal to list of
4571
03:02:26,640 --> 03:02:28,720
t
4572
03:02:28,720 --> 03:02:30,960
and let's print l2
4573
03:02:30,960 --> 03:02:33,279
so this is a list because it is present
4574
03:02:33,279 --> 03:02:34,399
inside
4575
03:02:34,399 --> 03:02:35,840
square brackets
4576
03:02:35,840 --> 03:02:38,399
you can check the type of
4577
03:02:38,399 --> 03:02:40,000
t
4578
03:02:40,000 --> 03:02:42,880
so t is a tuple you see here
4579
03:02:42,880 --> 03:02:45,520
if i change this to l2
4580
03:02:45,520 --> 03:02:47,920
it gives you list
4581
03:02:47,920 --> 03:02:49,680
cool
4582
03:02:49,680 --> 03:02:51,120
now
4583
03:02:51,120 --> 03:02:54,000
moving to the next section where we'll
4584
03:02:54,000 --> 03:02:56,319
see how to convert
4585
03:02:56,319 --> 03:02:59,840
integer to binary
4586
03:03:00,560 --> 03:03:02,720
suppose i have a variable called a equal
4587
03:03:02,720 --> 03:03:03,439
to
4588
03:03:03,439 --> 03:03:04,560
10
4589
03:03:04,560 --> 03:03:06,960
and then
4590
03:03:07,200 --> 03:03:09,439
i have p equal to
4591
03:03:09,439 --> 03:03:11,840
bin of
4592
03:03:11,840 --> 03:03:13,920
let's say we have another variable which
4593
03:03:13,920 --> 03:03:15,520
is c
4594
03:03:15,520 --> 03:03:17,920
and here i want to convert my integer
4595
03:03:17,920 --> 03:03:20,080
into a
4596
03:03:20,080 --> 03:03:22,560
hexadecimal value or a
4597
03:03:22,560 --> 03:03:24,319
octa decimal value for that you can
4598
03:03:24,319 --> 03:03:26,880
write d equal to use the oct function
4599
03:03:26,880 --> 03:03:28,000
for
4600
03:03:28,000 --> 03:03:29,920
octa decimal value
4601
03:03:29,920 --> 03:03:30,880
okay
4602
03:03:30,880 --> 03:03:33,439
now we are going to print them one by
4603
03:03:33,439 --> 03:03:34,240
one
4604
03:03:34,240 --> 03:03:38,080
so i'll write print off b
4605
03:03:38,080 --> 03:03:39,279
print of
4606
03:03:39,279 --> 03:03:40,399
c
4607
03:03:40,399 --> 03:03:42,720
and then we'll print what is there
4608
03:03:42,720 --> 03:03:44,880
inside d
4609
03:03:44,880 --> 03:03:47,120
we'll run it and you can see the results
4610
03:03:47,120 --> 03:03:48,160
here
4611
03:03:48,160 --> 03:03:51,279
so binary of 10 you see here
4612
03:03:51,279 --> 03:03:53,680
hexadecimal of 10 you see the value here
4613
03:03:53,680 --> 03:03:56,720
this is an extra decimal value
4614
03:03:56,720 --> 03:04:00,640
then we have the octa decimal value of a
4615
03:04:00,640 --> 03:04:02,960
okay
4616
03:04:03,279 --> 03:04:04,640
now moving to the
4617
03:04:04,640 --> 03:04:07,520
final section of this demo
4618
03:04:07,520 --> 03:04:09,200
you can convert a number to its
4619
03:04:09,200 --> 03:04:10,560
corresponding
4620
03:04:10,560 --> 03:04:12,240
ascii character
4621
03:04:12,240 --> 03:04:14,080
let me show you how to do it
4622
03:04:14,080 --> 03:04:15,279
suppose
4623
03:04:15,279 --> 03:04:16,640
i have
4624
03:04:16,640 --> 03:04:19,359
n1 equal to
4625
03:04:19,359 --> 03:04:21,200
76
4626
03:04:21,200 --> 03:04:22,800
now if i write
4627
03:04:22,800 --> 03:04:24,080
chr
4628
03:04:24,080 --> 03:04:25,840
of
4629
03:04:25,840 --> 03:04:29,600
n1 and run it it gives me l
4630
03:04:29,600 --> 03:04:32,720
now let's see our ascii table so this is
4631
03:04:32,720 --> 03:04:34,800
my ascii table and
4632
03:04:34,800 --> 03:04:37,680
for 76 you can see here the character is
4633
03:04:37,680 --> 03:04:38,800
l
4634
03:04:38,800 --> 03:04:39,920
so this
4635
03:04:39,920 --> 03:04:42,720
chr of n1 will return
4636
03:04:42,720 --> 03:04:44,319
the corresponding ascii character for
4637
03:04:44,319 --> 03:04:46,720
that particular number so here 76
4638
03:04:46,720 --> 03:04:48,160
belongs to l
4639
03:04:48,160 --> 03:04:50,560
let's say we have another number n2
4640
03:04:50,560 --> 03:04:52,720
equal to let's say 100.
4641
03:04:52,720 --> 03:04:55,600
now if i write chr of
4642
03:04:55,600 --> 03:04:56,880
n2
4643
03:04:56,880 --> 03:04:59,520
and run it it gives me
4644
03:04:59,520 --> 03:05:01,439
lowercase d
4645
03:05:01,439 --> 03:05:03,279
we'll see the table again
4646
03:05:03,279 --> 03:05:06,560
and if you see for 100 we have lowercase
4647
03:05:06,560 --> 03:05:07,359
d
4648
03:05:07,359 --> 03:05:09,359
now here you have a list of characters
4649
03:05:09,359 --> 03:05:12,399
these are special characters you have
4650
03:05:12,399 --> 03:05:14,640
uppercase characters you have add the
4651
03:05:14,640 --> 03:05:16,640
rate you have numbers each of them have
4652
03:05:16,640 --> 03:05:19,600
their own ascii values
4653
03:05:19,600 --> 03:05:21,600
cool
4654
03:05:21,600 --> 03:05:23,840
now
4655
03:05:24,000 --> 03:05:25,040
you can
4656
03:05:25,040 --> 03:05:27,600
convert string character into unicode
4657
03:05:27,600 --> 03:05:30,160
values now let me tell you what unicode
4658
03:05:30,160 --> 03:05:33,200
value is so computers on a basic level
4659
03:05:33,200 --> 03:05:36,080
deal with numbers so letters that appear
4660
03:05:36,080 --> 03:05:38,080
on computers are stored by computers as
4661
03:05:38,080 --> 03:05:39,520
a list of numbers
4662
03:05:39,520 --> 03:05:41,680
so the unicode standard provides a
4663
03:05:41,680 --> 03:05:43,439
unique number for every character and
4664
03:05:43,439 --> 03:05:45,760
supports multiple languages and all
4665
03:05:45,760 --> 03:05:47,760
special characters
4666
03:05:47,760 --> 03:05:52,720
so let's say i have a string s which has
4667
03:05:52,720 --> 03:05:56,560
the value of capital a or uppercase a
4668
03:05:56,560 --> 03:05:58,720
if i write r which stands for result
4669
03:05:58,720 --> 03:05:59,760
equal to
4670
03:05:59,760 --> 03:06:01,760
ord so this is the function which
4671
03:06:01,760 --> 03:06:04,479
converts a string to a unicode value
4672
03:06:04,479 --> 03:06:08,479
i'll write ord of s and will print r
4673
03:06:08,479 --> 03:06:11,920
if i run it it gives me 65
4674
03:06:11,920 --> 03:06:13,120
so
4675
03:06:13,120 --> 03:06:16,880
65 is the unicode value of a
4676
03:06:16,880 --> 03:06:18,960
now you can also have digits let's say i
4677
03:06:18,960 --> 03:06:20,479
have 7
4678
03:06:20,479 --> 03:06:21,680
i'll run it
4679
03:06:21,680 --> 03:06:25,920
so 55 is the unicode value of string 7
4680
03:06:25,920 --> 03:06:28,319
let's try for any other letter let's see
4681
03:06:28,319 --> 03:06:29,040
v
4682
03:06:29,040 --> 03:06:31,840
and run it you have 86
4683
03:06:31,840 --> 03:06:33,760
okay
4684
03:06:33,760 --> 03:06:35,680
so we'll understand what is a string in
4685
03:06:35,680 --> 03:06:36,640
python
4686
03:06:36,640 --> 03:06:39,040
and how to create strings in python
4687
03:06:39,040 --> 03:06:40,560
then we look at the different string
4688
03:06:40,560 --> 03:06:43,200
methods and how to perform string
4689
03:06:43,200 --> 03:06:44,399
indexing
4690
03:06:44,399 --> 03:06:45,359
and
4691
03:06:45,359 --> 03:06:46,960
all these will see with the help of
4692
03:06:46,960 --> 03:06:48,560
hands-on demonstrations on the jupyter
4693
03:06:48,560 --> 03:06:50,960
notebook
4694
03:06:51,600 --> 03:06:54,240
so let's understand what is a string
4695
03:06:54,240 --> 03:06:56,319
so a string in python is an object that
4696
03:06:56,319 --> 03:06:58,800
represents a sequence of characters
4697
03:06:58,800 --> 03:07:02,240
in python we have str as the data type
4698
03:07:02,240 --> 03:07:04,720
of string
4699
03:07:04,880 --> 03:07:07,840
now you can create strings
4700
03:07:07,840 --> 03:07:10,240
using single quotes or double quotes so
4701
03:07:10,240 --> 03:07:12,720
here is an example
4702
03:07:12,720 --> 03:07:15,040
where we have used a single quote to
4703
03:07:15,040 --> 03:07:17,600
write a string so our string is hello
4704
03:07:17,600 --> 03:07:21,120
india which is inside a single quote
4705
03:07:21,120 --> 03:07:23,359
below if you see we have the same string
4706
03:07:23,359 --> 03:07:25,359
written within double quotes so these
4707
03:07:25,359 --> 03:07:26,960
are the two methods in which you can
4708
03:07:26,960 --> 03:07:29,040
create strings in python
4709
03:07:29,040 --> 03:07:31,359
now you can also use triple quotes which
4710
03:07:31,359 --> 03:07:32,800
are generally for
4711
03:07:32,800 --> 03:07:35,760
multi-line strings or dot strings so
4712
03:07:35,760 --> 03:07:37,520
here is an example we have a string
4713
03:07:37,520 --> 03:07:39,120
called str3
4714
03:07:39,120 --> 03:07:39,920
and
4715
03:07:39,920 --> 03:07:42,240
the content it has is human psychology
4716
03:07:42,240 --> 03:07:44,319
is an interesting subject now if you
4717
03:07:44,319 --> 03:07:46,399
print the string it will be printed in
4718
03:07:46,399 --> 03:07:49,840
two lines and not in a single line
4719
03:07:49,840 --> 03:07:51,359
now we'll be exploring much more in
4720
03:07:51,359 --> 03:07:54,000
detail in our demo session so please
4721
03:07:54,000 --> 03:07:56,160
wait for a while next
4722
03:07:56,160 --> 03:07:58,720
we have string indexing so if you want
4723
03:07:58,720 --> 03:08:00,479
to extract a few characters from your
4724
03:08:00,479 --> 03:08:02,960
string you can use the indexing method
4725
03:08:02,960 --> 03:08:05,120
so in python
4726
03:08:05,120 --> 03:08:07,359
a string starts with the
4727
03:08:07,359 --> 03:08:08,960
index number zero
4728
03:08:08,960 --> 03:08:11,359
so here we have a string which is
4729
03:08:11,359 --> 03:08:12,800
healthy
4730
03:08:12,800 --> 03:08:14,399
and
4731
03:08:14,399 --> 03:08:16,319
the first element is present at the
4732
03:08:16,319 --> 03:08:18,560
zeroth index so at the bottom you see
4733
03:08:18,560 --> 03:08:20,399
the forward index on the top we have the
4734
03:08:20,399 --> 03:08:23,040
reverse index or the negative indexes
4735
03:08:23,040 --> 03:08:25,760
so in python as i said the first index
4736
03:08:25,760 --> 03:08:28,240
is at the zeroth position so h is at
4737
03:08:28,240 --> 03:08:31,359
zero e is at the first index a is at the
4738
03:08:31,359 --> 03:08:34,160
second index so on and so forth now
4739
03:08:34,160 --> 03:08:36,479
similarly if you look at negative
4740
03:08:36,479 --> 03:08:39,120
indexing it starts with minus 1 in
4741
03:08:39,120 --> 03:08:41,600
reverse order so we have minus 1
4742
03:08:41,600 --> 03:08:45,279
for y minus 2 for h minus 3 for t and so
4743
03:08:45,279 --> 03:08:46,640
on and so forth
4744
03:08:46,640 --> 03:08:48,160
now let's head over to our jupyter
4745
03:08:48,160 --> 03:08:49,520
notebook where we'll
4746
03:08:49,520 --> 03:08:53,200
learn about strings in python in detail
4747
03:08:53,200 --> 03:08:55,600
okay so i'm on my jupyter notebook first
4748
03:08:55,600 --> 03:08:58,240
i'll create a
4749
03:08:58,240 --> 03:09:01,200
new notebook i'll click on new and then
4750
03:09:01,200 --> 03:09:04,240
select python 3
4751
03:09:04,240 --> 03:09:05,600
ok
4752
03:09:05,600 --> 03:09:07,680
so here let me
4753
03:09:07,680 --> 03:09:10,720
rename my notebook as
4754
03:09:10,720 --> 03:09:12,160
strings in
4755
03:09:12,160 --> 03:09:14,640
python demo
4756
03:09:14,640 --> 03:09:16,720
i'll click on rename
4757
03:09:16,720 --> 03:09:18,560
okay
4758
03:09:18,560 --> 03:09:21,279
now let's start with our first example
4759
03:09:21,279 --> 03:09:23,760
i'll create a variable called str1 that
4760
03:09:23,760 --> 03:09:26,080
will have a string
4761
03:09:26,080 --> 03:09:28,160
you can see here i am using single
4762
03:09:28,160 --> 03:09:31,120
quotes now i'll write hi
4763
03:09:31,120 --> 03:09:33,200
sam
4764
03:09:33,200 --> 03:09:34,479
so here
4765
03:09:34,479 --> 03:09:38,880
str1 is holding a string called hi sam
4766
03:09:38,880 --> 03:09:41,680
and let's go ahead and print the string
4767
03:09:41,680 --> 03:09:44,640
str1
4768
03:09:44,640 --> 03:09:47,279
i'll hit shift enter to run it you see
4769
03:09:47,279 --> 03:09:50,160
here i have my string printed hi sam
4770
03:09:50,160 --> 03:09:52,479
if you want to check the
4771
03:09:52,479 --> 03:09:53,920
data type
4772
03:09:53,920 --> 03:09:55,840
that str1 holds
4773
03:09:55,840 --> 03:09:58,560
you can use the type function and inside
4774
03:09:58,560 --> 03:09:59,760
the type function i'll pass in my
4775
03:09:59,760 --> 03:10:01,520
variable str1
4776
03:10:01,520 --> 03:10:04,399
if i run it you see here it gives us str
4777
03:10:04,399 --> 03:10:05,760
which means
4778
03:10:05,760 --> 03:10:08,479
it is of type string
4779
03:10:08,479 --> 03:10:12,399
now let's create another string str2
4780
03:10:12,399 --> 03:10:14,479
using double quotes this time so i'll
4781
03:10:14,479 --> 03:10:16,560
write let's say
4782
03:10:16,560 --> 03:10:18,080
hello
4783
03:10:18,080 --> 03:10:20,640
angela
4784
03:10:20,960 --> 03:10:23,840
i can give exclamation mark
4785
03:10:23,840 --> 03:10:26,399
okay now let's just print the
4786
03:10:26,399 --> 03:10:28,319
second string you see here this is my
4787
03:10:28,319 --> 03:10:31,200
second string which is hello angela if
4788
03:10:31,200 --> 03:10:32,720
you want you can also use the print
4789
03:10:32,720 --> 03:10:34,880
function here
4790
03:10:34,880 --> 03:10:38,239
and run it you get hello angela
4791
03:10:38,239 --> 03:10:41,359
now let's write a multi-line string
4792
03:10:41,359 --> 03:10:44,319
using triple quotes so i'll write str3
4793
03:10:44,319 --> 03:10:45,760
equal to
4794
03:10:45,760 --> 03:10:49,040
i'll use triple quotes
4795
03:10:49,040 --> 03:10:51,200
and then
4796
03:10:51,200 --> 03:10:52,640
i'll say
4797
03:10:52,640 --> 03:10:55,359
hi everyone
4798
03:10:55,359 --> 03:10:58,720
go to the next line i'll write today
4799
03:10:58,720 --> 03:11:01,040
we will
4800
03:11:01,040 --> 03:11:01,920
learn
4801
03:11:01,920 --> 03:11:03,520
about
4802
03:11:03,520 --> 03:11:06,239
i'll go to the next line and i'll write
4803
03:11:06,239 --> 03:11:09,040
strings in python
4804
03:11:09,040 --> 03:11:11,920
then i'll again give triple quotes to
4805
03:11:11,920 --> 03:11:13,840
close my string
4806
03:11:13,840 --> 03:11:18,080
and let's just print this str3
4807
03:11:18,080 --> 03:11:19,920
if i run it you can see it here i have
4808
03:11:19,920 --> 03:11:22,000
now written a multiline string hi
4809
03:11:22,000 --> 03:11:24,560
everyone today we will learn about
4810
03:11:24,560 --> 03:11:27,680
strings in python let me just add a s
4811
03:11:27,680 --> 03:11:28,880
there you go
4812
03:11:28,880 --> 03:11:30,000
okay
4813
03:11:30,000 --> 03:11:31,920
now coming to the next topic we'll learn
4814
03:11:31,920 --> 03:11:35,399
about indexing
4815
03:11:36,399 --> 03:11:37,600
all right
4816
03:11:37,600 --> 03:11:40,160
let me declare a string str1
4817
03:11:40,160 --> 03:11:42,560
which has
4818
03:11:42,560 --> 03:11:45,680
value as simply loan
4819
03:11:45,680 --> 03:11:47,840
now
4820
03:11:47,840 --> 03:11:51,920
to use indexing and extract certain
4821
03:11:51,920 --> 03:11:54,000
items in a string or values in a string
4822
03:11:54,000 --> 03:11:55,840
you can use square brackets and inside
4823
03:11:55,840 --> 03:11:57,680
the square brackets you can give the
4824
03:11:57,680 --> 03:11:59,840
index position
4825
03:11:59,840 --> 03:12:02,960
for example if i write str1
4826
03:12:02,960 --> 03:12:04,880
and in the square bracket i'll give
4827
03:12:04,880 --> 03:12:05,920
let's say
4828
03:12:05,920 --> 03:12:07,200
i want to
4829
03:12:07,200 --> 03:12:09,120
find the element or the value present at
4830
03:12:09,120 --> 03:12:10,640
the zeroth index
4831
03:12:10,640 --> 03:12:13,200
let's run it it gives me s
4832
03:12:13,200 --> 03:12:16,319
because as we learnt in the slides
4833
03:12:16,319 --> 03:12:18,560
the first index in a string
4834
03:12:18,560 --> 03:12:21,439
starts with the zeroth number
4835
03:12:21,439 --> 03:12:23,680
so i have s in my zeroth index so hence
4836
03:12:23,680 --> 03:12:25,200
it has printed s
4837
03:12:25,200 --> 03:12:27,920
let's explore a few more if i write str
4838
03:12:27,920 --> 03:12:30,000
one of let's say i want to know what is
4839
03:12:30,000 --> 03:12:32,640
present at the third index if i run it
4840
03:12:32,640 --> 03:12:35,760
it gives me p you can check now so s is
4841
03:12:35,760 --> 03:12:37,920
at the zeroth index i is at the first
4842
03:12:37,920 --> 03:12:40,640
index m is at the second index and p
4843
03:12:40,640 --> 03:12:43,279
is at the third index
4844
03:12:43,279 --> 03:12:47,920
let's say i'll write str str1 of
4845
03:12:47,920 --> 03:12:50,000
let's say 12
4846
03:12:50,000 --> 03:12:51,840
we can see the difference now it gives
4847
03:12:51,840 --> 03:12:54,160
us an error it says string index out of
4848
03:12:54,160 --> 03:12:57,680
range the reason being simply learn
4849
03:12:57,680 --> 03:13:00,479
does not have any value at the twelfth
4850
03:13:00,479 --> 03:13:03,840
index so it's printing an error saying
4851
03:13:03,840 --> 03:13:06,560
string index out of range
4852
03:13:06,560 --> 03:13:08,880
now let's change this to
4853
03:13:08,880 --> 03:13:11,040
six and see what we have at the sixth
4854
03:13:11,040 --> 03:13:12,560
index we have l
4855
03:13:12,560 --> 03:13:15,600
if you count here s is at the
4856
03:13:15,600 --> 03:13:17,920
zeroth index i is at the first index m
4857
03:13:17,920 --> 03:13:19,840
is at the second index p is at the third
4858
03:13:19,840 --> 03:13:21,120
index
4859
03:13:21,120 --> 03:13:24,160
the first l is at the fourth index
4860
03:13:24,160 --> 03:13:26,560
again i is at the fifth index and then
4861
03:13:26,560 --> 03:13:28,640
we have the second l at the sixth index
4862
03:13:28,640 --> 03:13:32,239
so we have l printed here
4863
03:13:32,640 --> 03:13:33,600
now
4864
03:13:33,600 --> 03:13:35,680
let's see how to use
4865
03:13:35,680 --> 03:13:38,319
the slicing operator using colon
4866
03:13:38,319 --> 03:13:39,920
i'll write
4867
03:13:39,920 --> 03:13:42,800
slicing operator
4868
03:13:42,800 --> 03:13:44,960
okay
4869
03:13:44,960 --> 03:13:47,279
i'll use the same string as tr1 let me
4870
03:13:47,279 --> 03:13:48,960
print it once again we have simply
4871
03:13:48,960 --> 03:13:50,160
learned
4872
03:13:50,160 --> 03:13:53,120
suppose i write str1
4873
03:13:53,120 --> 03:13:55,439
within brackets i'll use the slicing
4874
03:13:55,439 --> 03:13:57,920
technique now i want everything starting
4875
03:13:57,920 --> 03:14:01,120
from the zeroth index
4876
03:14:01,120 --> 03:14:03,840
i get simply learn so here i am printing
4877
03:14:03,840 --> 03:14:05,600
everything that starts with the zeroth
4878
03:14:05,600 --> 03:14:07,600
index
4879
03:14:07,600 --> 03:14:09,279
next let's see
4880
03:14:09,279 --> 03:14:10,560
what we have
4881
03:14:10,560 --> 03:14:13,600
if i print my string str1
4882
03:14:13,600 --> 03:14:16,640
and write 1s to 6
4883
03:14:16,640 --> 03:14:17,840
i'll run it
4884
03:14:17,840 --> 03:14:22,399
it gives me impli so if you notice here
4885
03:14:22,399 --> 03:14:24,560
i want to extract whatever is present
4886
03:14:24,560 --> 03:14:26,560
from the first index
4887
03:14:26,560 --> 03:14:28,560
till the sixth index but excluding the
4888
03:14:28,560 --> 03:14:30,880
sixth index so in python
4889
03:14:30,880 --> 03:14:32,479
the indexing method or the slicing
4890
03:14:32,479 --> 03:14:35,359
method will exclude the last index so
4891
03:14:35,359 --> 03:14:36,880
here if you see
4892
03:14:36,880 --> 03:14:38,880
i is at the first index m is at the
4893
03:14:38,880 --> 03:14:42,160
second index p is at the third index
4894
03:14:42,160 --> 03:14:44,239
l is at the fourth index
4895
03:14:44,239 --> 03:14:46,960
and then we have i at the fifth index
4896
03:14:46,960 --> 03:14:48,880
and we have excluded the sixth index
4897
03:14:48,880 --> 03:14:50,720
that is the second l so hence it has
4898
03:14:50,720 --> 03:14:53,840
written us impli
4899
03:14:53,840 --> 03:14:56,319
now similarly let's see
4900
03:14:56,319 --> 03:14:58,319
we'll see another example i'll write
4901
03:14:58,319 --> 03:14:59,760
str1
4902
03:14:59,760 --> 03:15:01,600
this time i'll use colon
4903
03:15:01,600 --> 03:15:02,880
6.
4904
03:15:02,880 --> 03:15:05,439
if i run it you get
4905
03:15:05,439 --> 03:15:06,479
simply
4906
03:15:06,479 --> 03:15:08,720
so it has printed everything starting
4907
03:15:08,720 --> 03:15:11,439
from the zeroth index till
4908
03:15:11,439 --> 03:15:13,600
the sixth index but excluding the sixth
4909
03:15:13,600 --> 03:15:16,479
index so you have all these values
4910
03:15:16,479 --> 03:15:20,880
from zeroth index till the fifth index
4911
03:15:20,880 --> 03:15:23,200
okay
4912
03:15:24,479 --> 03:15:25,920
now you can also
4913
03:15:25,920 --> 03:15:28,160
reverse a string for that you can use
4914
03:15:28,160 --> 03:15:30,640
negative slicing using
4915
03:15:30,640 --> 03:15:32,239
two colons
4916
03:15:32,239 --> 03:15:34,399
if i write print
4917
03:15:34,399 --> 03:15:36,880
str1
4918
03:15:36,880 --> 03:15:38,319
and within square brackets i'll give
4919
03:15:38,319 --> 03:15:40,800
colon colon -1
4920
03:15:40,800 --> 03:15:43,120
let's run and see the result
4921
03:15:43,120 --> 03:15:46,080
there you go if you see this it has
4922
03:15:46,080 --> 03:15:47,840
printed my original string which is
4923
03:15:47,840 --> 03:15:51,600
simply learning reverse order now
4924
03:15:52,319 --> 03:15:54,399
so this is a very simple method if
4925
03:15:54,399 --> 03:15:56,319
someone asks you to reverse a string or
4926
03:15:56,319 --> 03:15:57,760
print something in reverse order you can
4927
03:15:57,760 --> 03:16:00,319
use the negative slicing method
4928
03:16:00,319 --> 03:16:02,640
now let's explore a few more example
4929
03:16:02,640 --> 03:16:05,439
if i write str1
4930
03:16:05,439 --> 03:16:08,080
and within brackets i'll give minus 1
4931
03:16:08,080 --> 03:16:09,439
and run it
4932
03:16:09,439 --> 03:16:11,840
it returns me n because in my original
4933
03:16:11,840 --> 03:16:14,800
string if i go on the top i have n at
4934
03:16:14,800 --> 03:16:16,880
the last index which is
4935
03:16:16,880 --> 03:16:20,080
here -1
4936
03:16:21,600 --> 03:16:24,880
similarly let's see what we have at
4937
03:16:24,880 --> 03:16:26,560
minus
4938
03:16:26,560 --> 03:16:28,880
3
4939
03:16:29,040 --> 03:16:32,319
we have a here if i go to the top
4940
03:16:32,319 --> 03:16:34,319
you see here
4941
03:16:34,319 --> 03:16:37,600
n is at minus 1 r is at minus 2 and a is
4942
03:16:37,600 --> 03:16:42,000
at minus 3 and hence it has given us a
4943
03:16:43,600 --> 03:16:47,840
now let's say i'll write str1
4944
03:16:47,840 --> 03:16:50,080
i want to print
4945
03:16:50,080 --> 03:16:51,439
minus 2
4946
03:16:51,439 --> 03:16:53,760
colon and let's see the result it has
4947
03:16:53,760 --> 03:16:54,960
given us
4948
03:16:54,960 --> 03:16:56,479
r and n
4949
03:16:56,479 --> 03:16:58,479
so here we are printing
4950
03:16:58,479 --> 03:17:00,640
only the
4951
03:17:00,640 --> 03:17:03,200
last two elements in the string hence we
4952
03:17:03,200 --> 03:17:06,560
have used -2
4953
03:17:06,640 --> 03:17:08,880
okay
4954
03:17:10,880 --> 03:17:13,920
now python does not allow you to
4955
03:17:13,920 --> 03:17:16,080
reassign values
4956
03:17:16,080 --> 03:17:18,319
in a declared string let me show you
4957
03:17:18,319 --> 03:17:19,600
what i mean
4958
03:17:19,600 --> 03:17:21,920
i'll give a comment here
4959
03:17:21,920 --> 03:17:24,479
reassign
4960
03:17:24,840 --> 03:17:26,479
strings
4961
03:17:26,479 --> 03:17:27,840
suppose
4962
03:17:27,840 --> 03:17:30,640
i have the same string str1 which is
4963
03:17:30,640 --> 03:17:32,319
simply learn
4964
03:17:32,319 --> 03:17:35,120
say i write str
4965
03:17:35,120 --> 03:17:38,319
and i want to replace
4966
03:17:38,319 --> 03:17:41,920
the value present at the third index
4967
03:17:41,920 --> 03:17:44,000
with let's say
4968
03:17:44,000 --> 03:17:46,239
o
4969
03:17:47,040 --> 03:17:48,239
and
4970
03:17:48,239 --> 03:17:50,479
if i were to print
4971
03:17:50,479 --> 03:17:52,239
str1 now
4972
03:17:52,239 --> 03:17:55,439
python will throw an error saying str
4973
03:17:55,439 --> 03:17:58,399
object does not support item assignment
4974
03:17:58,399 --> 03:18:00,640
so it is immutable
4975
03:18:00,640 --> 03:18:03,040
like tuples where you cannot reassign
4976
03:18:03,040 --> 03:18:04,000
any
4977
03:18:04,000 --> 03:18:06,479
values
4978
03:18:06,960 --> 03:18:08,560
you can also
4979
03:18:08,560 --> 03:18:11,439
delete a string
4980
03:18:12,000 --> 03:18:14,960
see i have another string called str2
4981
03:18:14,960 --> 03:18:17,359
which has a value of
4982
03:18:17,359 --> 03:18:20,720
let's say python programming
4983
03:18:21,600 --> 03:18:24,239
you can use the dl keyword
4984
03:18:24,239 --> 03:18:27,439
and write str2
4985
03:18:27,439 --> 03:18:31,840
and now let's print str2
4986
03:18:31,840 --> 03:18:33,040
you will see
4987
03:18:33,040 --> 03:18:33,920
that
4988
03:18:33,920 --> 03:18:36,399
it says name str2 is not defined because
4989
03:18:36,399 --> 03:18:39,520
we have already deleted str2
4990
03:18:39,520 --> 03:18:41,760
the above stuff
4991
03:18:41,760 --> 03:18:44,080
okay
4992
03:18:44,239 --> 03:18:47,600
now let's go ahead and see some of the
4993
03:18:47,600 --> 03:18:48,640
important
4994
03:18:48,640 --> 03:18:51,840
string operations
4995
03:18:53,200 --> 03:18:54,880
okay
4996
03:18:54,880 --> 03:18:56,479
see i have
4997
03:18:56,479 --> 03:18:58,000
str1
4998
03:18:58,000 --> 03:18:59,359
which has
4999
03:18:59,359 --> 03:19:00,479
python
5000
03:19:00,479 --> 03:19:02,640
now one thing to note i am using either
5001
03:19:02,640 --> 03:19:05,040
single quote or double quote you can use
5002
03:19:05,040 --> 03:19:06,479
whatever you want
5003
03:19:06,479 --> 03:19:08,319
let's say i have another string which
5004
03:19:08,319 --> 03:19:10,399
has programming
5005
03:19:10,399 --> 03:19:13,200
so these are my two strings
5006
03:19:13,200 --> 03:19:14,479
let's
5007
03:19:14,479 --> 03:19:16,239
perform a few operations on these
5008
03:19:16,239 --> 03:19:17,600
strings
5009
03:19:17,600 --> 03:19:18,960
see i write
5010
03:19:18,960 --> 03:19:20,640
str1
5011
03:19:20,640 --> 03:19:23,200
multiplied by 3
5012
03:19:23,200 --> 03:19:25,359
and i'll run it
5013
03:19:25,359 --> 03:19:28,239
you can see here it has printed python
5014
03:19:28,239 --> 03:19:31,520
thrice because i have said str1
5015
03:19:31,520 --> 03:19:34,160
multiplied by 3 so in str1 we have
5016
03:19:34,160 --> 03:19:37,120
python and multiplying 3 returns python
5017
03:19:37,120 --> 03:19:39,600
3 times
5018
03:19:41,200 --> 03:19:43,760
now let's see i'll write str1
5019
03:19:43,760 --> 03:19:44,840
plus
5020
03:19:44,840 --> 03:19:46,640
str1
5021
03:19:46,640 --> 03:19:49,840
it has merged python twice so i have
5022
03:19:49,840 --> 03:19:53,920
python and python repeated
5023
03:19:56,479 --> 03:20:01,200
let's say i write str1 plus str2
5024
03:20:01,200 --> 03:20:03,760
we see the result it gives us python
5025
03:20:03,760 --> 03:20:05,840
programming so basically
5026
03:20:05,840 --> 03:20:07,279
if you use
5027
03:20:07,279 --> 03:20:09,760
the plus operator with two strings
5028
03:20:09,760 --> 03:20:12,160
python will perform a concatenation
5029
03:20:12,160 --> 03:20:14,000
operation which means it will merge both
5030
03:20:14,000 --> 03:20:16,560
the strings
5031
03:20:17,120 --> 03:20:20,960
now if you want to find if a specific
5032
03:20:20,960 --> 03:20:23,040
character is present in a string or not
5033
03:20:23,040 --> 03:20:25,520
you can use the in operator for this
5034
03:20:25,520 --> 03:20:26,840
let's say i'll
5035
03:20:26,840 --> 03:20:29,439
print whether
5036
03:20:29,439 --> 03:20:30,560
w
5037
03:20:30,560 --> 03:20:32,319
is
5038
03:20:32,319 --> 03:20:34,399
present in
5039
03:20:34,399 --> 03:20:36,880
str
5040
03:20:36,960 --> 03:20:38,640
1
5041
03:20:38,640 --> 03:20:41,239
but run it it gives us false because in
5042
03:20:41,239 --> 03:20:45,120
str1 we have python and if you see here
5043
03:20:45,120 --> 03:20:47,520
we don't have the letter w in python and
5044
03:20:47,520 --> 03:20:49,680
hence it has given us false
5045
03:20:49,680 --> 03:20:51,439
let's
5046
03:20:51,439 --> 03:20:54,000
now let me copy this
5047
03:20:54,000 --> 03:20:57,439
i'll paste it here let's try whether o
5048
03:20:57,439 --> 03:20:58,560
is there in
5049
03:20:58,560 --> 03:21:00,880
string one or not it gives us true
5050
03:21:00,880 --> 03:21:02,880
because we have
5051
03:21:02,880 --> 03:21:04,960
an o here
5052
03:21:04,960 --> 03:21:07,920
you can see here we have an o
5053
03:21:07,920 --> 03:21:10,080
similarly
5054
03:21:10,080 --> 03:21:11,120
let's say
5055
03:21:11,120 --> 03:21:13,520
i want to find whether
5056
03:21:13,520 --> 03:21:15,439
l a
5057
03:21:15,439 --> 03:21:17,359
is there in string
5058
03:21:17,359 --> 03:21:20,080
2 or not
5059
03:21:21,359 --> 03:21:24,000
but run it it gives us false because in
5060
03:21:24,000 --> 03:21:26,399
string2 we have programming and we don't
5061
03:21:26,399 --> 03:21:28,640
have any la
5062
03:21:28,640 --> 03:21:31,120
present in programming
5063
03:21:31,120 --> 03:21:33,760
let's say we'll see whether r o is
5064
03:21:33,760 --> 03:21:34,960
present in
5065
03:21:34,960 --> 03:21:36,560
string two or not
5066
03:21:36,560 --> 03:21:39,200
this time it returns us true because if
5067
03:21:39,200 --> 03:21:41,600
you see here we have ro in programming
5068
03:21:41,600 --> 03:21:43,520
and hence it has given us
5069
03:21:43,520 --> 03:21:45,840
true
5070
03:21:47,120 --> 03:21:49,920
now you can also
5071
03:21:49,920 --> 03:21:54,239
print a raw string using the
5072
03:21:56,479 --> 03:21:58,080
r alphabet
5073
03:21:58,080 --> 03:22:01,040
so the way to write is i'll write r
5074
03:22:01,040 --> 03:22:03,439
give a
5075
03:22:04,239 --> 03:22:06,640
single quote let's say i'll give a
5076
03:22:06,640 --> 03:22:07,840
location
5077
03:22:07,840 --> 03:22:10,399
c drive
5078
03:22:10,560 --> 03:22:12,720
two forward slash and let's say i'll
5079
03:22:12,720 --> 03:22:13,840
write
5080
03:22:13,840 --> 03:22:17,479
python 3.7
5081
03:22:18,000 --> 03:22:22,319
and run it it gives me a raw string
5082
03:22:22,560 --> 03:22:24,880
now
5083
03:22:25,120 --> 03:22:27,760
let's see some formatting methods i'll
5084
03:22:27,760 --> 03:22:29,840
write
5085
03:22:29,840 --> 03:22:32,880
a comment saying
5086
03:22:32,880 --> 03:22:35,840
format methods
5087
03:22:36,640 --> 03:22:38,960
okay
5088
03:22:42,160 --> 03:22:45,520
i'll write a print statement
5089
03:22:45,600 --> 03:22:48,560
i'll give double quotes and here
5090
03:22:48,560 --> 03:22:50,160
let's see the first example where i'm
5091
03:22:50,160 --> 03:22:51,120
using
5092
03:22:51,120 --> 03:22:54,080
curly braces i'll write
5093
03:22:54,080 --> 03:22:54,880
and
5094
03:22:54,880 --> 03:22:57,439
i'll give another curly braces
5095
03:22:57,439 --> 03:22:59,120
you'll get to know what i'm doing once i
5096
03:22:59,120 --> 03:23:00,800
finish this
5097
03:23:00,800 --> 03:23:03,840
let's say i have a message
5098
03:23:03,840 --> 03:23:06,319
both are
5099
03:23:06,319 --> 03:23:08,000
best
5100
03:23:08,000 --> 03:23:10,399
friends
5101
03:23:11,200 --> 03:23:15,720
then i'm going to use my format
5102
03:23:16,319 --> 03:23:17,520
method
5103
03:23:17,520 --> 03:23:19,520
and inside this i'll pass in my two
5104
03:23:19,520 --> 03:23:22,000
values for these braces
5105
03:23:22,000 --> 03:23:24,080
so these curly braces will be replaced
5106
03:23:24,080 --> 03:23:25,279
by
5107
03:23:25,279 --> 03:23:26,640
the words that i
5108
03:23:26,640 --> 03:23:29,040
pass in or the values that i pass in
5109
03:23:29,040 --> 03:23:31,120
within the format method
5110
03:23:31,120 --> 03:23:34,160
so here i'll write let's say
5111
03:23:34,160 --> 03:23:35,600
james
5112
03:23:35,600 --> 03:23:38,239
and my second person would be
5113
03:23:38,239 --> 03:23:40,080
brad
5114
03:23:40,080 --> 03:23:41,600
now let me run it
5115
03:23:41,600 --> 03:23:43,120
you see here
5116
03:23:43,120 --> 03:23:46,720
what python does is it replaces
5117
03:23:46,720 --> 03:23:49,279
the curly braces with
5118
03:23:49,279 --> 03:23:51,359
the names that have passed within the
5119
03:23:51,359 --> 03:23:53,680
format method so it takes the first
5120
03:23:53,680 --> 03:23:56,399
argument for the first curly brace
5121
03:23:56,399 --> 03:23:58,000
and the second argument for the second
5122
03:23:58,000 --> 03:23:59,439
curly brace
5123
03:23:59,439 --> 03:24:02,479
so my statement becomes
5124
03:24:02,479 --> 03:24:04,479
james and brad
5125
03:24:04,479 --> 03:24:07,760
both are best friends
5126
03:24:09,760 --> 03:24:12,080
now let me just copy this i will show
5127
03:24:12,080 --> 03:24:14,479
you another example
5128
03:24:14,479 --> 03:24:16,399
this time i'll
5129
03:24:16,399 --> 03:24:18,960
give 1 and
5130
03:24:18,960 --> 03:24:22,640
0 inside the curly braces
5131
03:24:22,960 --> 03:24:25,279
now if i run this
5132
03:24:25,279 --> 03:24:26,640
there you go
5133
03:24:26,640 --> 03:24:29,040
so here
5134
03:24:29,040 --> 03:24:31,439
james is at the zeroth index so this is
5135
03:24:31,439 --> 03:24:33,279
basically a list where james is at the
5136
03:24:33,279 --> 03:24:35,600
zeroth index and brad is at the first
5137
03:24:35,600 --> 03:24:39,279
index so if i write 1 and 0 it will take
5138
03:24:39,279 --> 03:24:40,720
brad here
5139
03:24:40,720 --> 03:24:42,800
and then
5140
03:24:42,800 --> 03:24:45,200
for zero it will take james so now my
5141
03:24:45,200 --> 03:24:47,120
message becomes brad and james both are
5142
03:24:47,120 --> 03:24:49,200
best friends
5143
03:24:49,200 --> 03:24:50,960
now let's try out another example i'll
5144
03:24:50,960 --> 03:24:52,880
paste it here
5145
03:24:52,880 --> 03:24:54,720
i'll use
5146
03:24:54,720 --> 03:24:58,080
two variables let's say a and b
5147
03:24:58,080 --> 03:25:00,880
and inside my format method
5148
03:25:00,880 --> 03:25:04,640
i'll write a equal to james
5149
03:25:04,640 --> 03:25:07,680
and i'll assign b as brad
5150
03:25:07,680 --> 03:25:09,040
let's run it
5151
03:25:09,040 --> 03:25:11,520
there you go we have our message printed
5152
03:25:11,520 --> 03:25:13,680
james and brad both are best friends so
5153
03:25:13,680 --> 03:25:17,359
here a refers to james and b refers to
5154
03:25:17,359 --> 03:25:19,680
brad
5155
03:25:20,840 --> 03:25:24,720
cool let's explore another example
5156
03:25:24,720 --> 03:25:27,120
i have a variable called name and that
5157
03:25:27,120 --> 03:25:28,800
has a value
5158
03:25:28,800 --> 03:25:30,800
angela
5159
03:25:30,800 --> 03:25:33,600
and then i have another variable
5160
03:25:33,600 --> 03:25:35,200
it's a salary
5161
03:25:35,200 --> 03:25:36,399
of
5162
03:25:36,399 --> 03:25:39,040
100 000
5163
03:25:39,040 --> 03:25:42,399
i'll use a print statement
5164
03:25:42,479 --> 03:25:44,080
i'll print a message
5165
03:25:44,080 --> 03:25:45,439
her
5166
03:25:45,439 --> 03:25:46,800
name
5167
03:25:46,800 --> 03:25:48,000
is
5168
03:25:48,000 --> 03:25:48,840
i'll
5169
03:25:48,840 --> 03:25:51,279
use percentage
5170
03:25:51,279 --> 03:25:54,399
s for string
5171
03:25:54,640 --> 03:25:56,640
give
5172
03:25:56,640 --> 03:25:59,840
a backslash n for next line and i'll
5173
03:25:59,840 --> 03:26:00,800
write
5174
03:26:00,800 --> 03:26:02,160
her
5175
03:26:02,160 --> 03:26:03,200
salary
5176
03:26:03,200 --> 03:26:04,640
is
5177
03:26:04,640 --> 03:26:06,239
then i'll write
5178
03:26:06,239 --> 03:26:08,080
percentage d
5179
03:26:08,080 --> 03:26:11,040
this is for numbers
5180
03:26:11,040 --> 03:26:13,359
and then i'll give another percentage
5181
03:26:13,359 --> 03:26:15,520
and let's see i'll write
5182
03:26:15,520 --> 03:26:19,840
my two variables niemann's salary
5183
03:26:21,040 --> 03:26:22,319
let's
5184
03:26:22,319 --> 03:26:24,319
see what we have in the
5185
03:26:24,319 --> 03:26:26,000
result
5186
03:26:26,000 --> 03:26:28,239
i need to close my double quotes here
5187
03:26:28,239 --> 03:26:30,560
i'll exclude it from here let's run it
5188
03:26:30,560 --> 03:26:33,439
there you go so i have my message here
5189
03:26:33,439 --> 03:26:36,720
her name is angela her salary is
5190
03:26:36,720 --> 03:26:38,640
hundred thousand dollars
5191
03:26:38,640 --> 03:26:39,680
i'll just
5192
03:26:39,680 --> 03:26:41,600
delete this piece here we'll run it
5193
03:26:41,600 --> 03:26:43,680
again okay
5194
03:26:43,680 --> 03:26:46,479
now this percentage s
5195
03:26:46,479 --> 03:26:48,880
refers to the name
5196
03:26:48,880 --> 03:26:51,040
and we have declared her name is angela
5197
03:26:51,040 --> 03:26:53,279
so my first statement or the message
5198
03:26:53,279 --> 03:26:55,359
becomes her name is angela
5199
03:26:55,359 --> 03:26:57,600
and the next
5200
03:26:57,600 --> 03:27:00,640
message is her salary is percentage t so
5201
03:27:00,640 --> 03:27:03,600
percentage d here refers to the salary
5202
03:27:03,600 --> 03:27:05,920
where we have 100 000
5203
03:27:05,920 --> 03:27:08,319
so the next message is her salary is 100
5204
03:27:08,319 --> 03:27:09,520
000
5205
03:27:09,520 --> 03:27:12,000
so these are the very basic and
5206
03:27:12,000 --> 03:27:13,120
important
5207
03:27:13,120 --> 03:27:15,200
print methods or formats that you can
5208
03:27:15,200 --> 03:27:16,319
use
5209
03:27:16,319 --> 03:27:19,520
while working with strings
5210
03:27:19,760 --> 03:27:22,800
now we'll explore a few
5211
03:27:22,800 --> 03:27:24,960
functions that are readily available in
5212
03:27:24,960 --> 03:27:28,720
python to operate with strings
5213
03:27:28,720 --> 03:27:31,920
i'll take my same string str1 as simply
5214
03:27:31,920 --> 03:27:34,160
learn
5215
03:27:34,160 --> 03:27:35,760
and then
5216
03:27:35,760 --> 03:27:39,359
i'll create another variable called str2
5217
03:27:39,359 --> 03:27:43,040
i'll write str 1 dot i am going to use
5218
03:27:43,040 --> 03:27:43,760
the
5219
03:27:43,760 --> 03:27:44,960
capitalize
5220
03:27:44,960 --> 03:27:47,439
method
5221
03:27:50,080 --> 03:27:52,160
ok
5222
03:27:52,160 --> 03:27:54,239
let me make one correction i am going to
5223
03:27:54,239 --> 03:27:57,279
write everything in lower case so that
5224
03:27:57,279 --> 03:27:59,279
we can see the difference how
5225
03:27:59,279 --> 03:28:02,160
we can capitalize the string
5226
03:28:02,160 --> 03:28:06,720
so i'll write my print statement print
5227
03:28:06,720 --> 03:28:10,040
old value
5228
03:28:10,720 --> 03:28:13,439
this is going to my message
5229
03:28:13,439 --> 03:28:15,520
i'll give a colon
5230
03:28:15,520 --> 03:28:17,359
followed by my double quotes and i'll
5231
03:28:17,359 --> 03:28:20,640
pass in my variable str1
5232
03:28:20,640 --> 03:28:22,239
okay
5233
03:28:22,239 --> 03:28:24,720
now i'll copy this
5234
03:28:24,720 --> 03:28:27,439
in the next line we have
5235
03:28:27,439 --> 03:28:30,640
a new value as str 2.
5236
03:28:30,640 --> 03:28:32,640
let's run it and see the difference
5237
03:28:32,640 --> 03:28:35,840
there you go so here my old value has
5238
03:28:35,840 --> 03:28:37,439
simply learned in all
5239
03:28:37,439 --> 03:28:40,080
lower case but when i used the
5240
03:28:40,080 --> 03:28:42,080
capitalize method
5241
03:28:42,080 --> 03:28:44,800
so python has converted my first letter
5242
03:28:44,800 --> 03:28:46,399
and simply learned to
5243
03:28:46,399 --> 03:28:49,279
uppercase or capital keys
5244
03:28:49,279 --> 03:28:51,520
cool
5245
03:28:51,600 --> 03:28:53,520
now let me copy this
5246
03:28:53,520 --> 03:28:55,680
i'm going to use the same example
5247
03:28:55,680 --> 03:28:57,120
for
5248
03:28:57,120 --> 03:29:00,080
the next three methods
5249
03:29:00,080 --> 03:29:02,880
let me show you how the case fold method
5250
03:29:02,880 --> 03:29:03,920
works
5251
03:29:03,920 --> 03:29:06,399
this time i'm going to pass my input
5252
03:29:06,399 --> 03:29:08,720
string in all upper case i will write
5253
03:29:08,720 --> 03:29:12,000
simply learn in uppercase
5254
03:29:12,720 --> 03:29:15,840
and instead of capitalize i'll write
5255
03:29:15,840 --> 03:29:17,920
case fold
5256
03:29:17,920 --> 03:29:20,080
let's run it and see the difference
5257
03:29:20,080 --> 03:29:21,439
there you go
5258
03:29:21,439 --> 03:29:24,160
initially we had simply learn in
5259
03:29:24,160 --> 03:29:26,000
upper case and now we have converted
5260
03:29:26,000 --> 03:29:27,920
into case fold so
5261
03:29:27,920 --> 03:29:31,840
all the letters are in lower case
5262
03:29:32,880 --> 03:29:34,880
now moving ahead
5263
03:29:34,880 --> 03:29:38,399
you can also use the
5264
03:29:38,399 --> 03:29:40,319
upper method to convert a string into
5265
03:29:40,319 --> 03:29:41,680
upper case
5266
03:29:41,680 --> 03:29:44,720
let's say i have a string
5267
03:29:44,720 --> 03:29:46,080
where
5268
03:29:46,080 --> 03:29:49,359
few of the letters are in upper case and
5269
03:29:49,359 --> 03:29:52,640
few of the letters are in lower case
5270
03:29:52,640 --> 03:29:54,160
this time i am going to convert
5271
03:29:54,160 --> 03:29:55,680
everything into upper case so i'll use
5272
03:29:55,680 --> 03:29:58,560
the upper function instead of case fold
5273
03:29:58,560 --> 03:30:00,560
let's run and see the result there you
5274
03:30:00,560 --> 03:30:02,479
go so this time
5275
03:30:02,479 --> 03:30:05,520
python has converted my original string
5276
03:30:05,520 --> 03:30:08,080
into uppercase
5277
03:30:08,080 --> 03:30:09,680
and likewise you can also use the
5278
03:30:09,680 --> 03:30:11,200
lowercase
5279
03:30:11,200 --> 03:30:13,439
instead of upper if i write lower you
5280
03:30:13,439 --> 03:30:15,680
see here everything has been converted
5281
03:30:15,680 --> 03:30:18,560
to lower case
5282
03:30:18,560 --> 03:30:20,800
cool
5283
03:30:21,279 --> 03:30:22,479
okay
5284
03:30:22,479 --> 03:30:24,239
now i'll show you how the
5285
03:30:24,239 --> 03:30:25,920
title method works
5286
03:30:25,920 --> 03:30:28,880
so in title method all the words
5287
03:30:28,880 --> 03:30:30,479
basically the first letter of all the
5288
03:30:30,479 --> 03:30:33,520
words will be in upper case so suppose i
5289
03:30:33,520 --> 03:30:35,439
have
5290
03:30:35,439 --> 03:30:36,479
a string
5291
03:30:36,479 --> 03:30:37,760
which is
5292
03:30:37,760 --> 03:30:39,680
simply learn
5293
03:30:39,680 --> 03:30:42,319
company so i'll write
5294
03:30:42,319 --> 03:30:44,479
let's say
5295
03:30:44,479 --> 03:30:47,279
this is my string and i'll
5296
03:30:47,279 --> 03:30:51,200
i'll convert my str1 to title case
5297
03:30:51,200 --> 03:30:52,560
let me run it
5298
03:30:52,560 --> 03:30:55,840
so this is the difference you can see
5299
03:30:55,840 --> 03:30:57,439
the first letter of the first word is
5300
03:30:57,439 --> 03:30:58,720
upper case
5301
03:30:58,720 --> 03:30:59,920
again
5302
03:30:59,920 --> 03:31:01,439
the first letter of the second word is
5303
03:31:01,439 --> 03:31:03,680
also in upper case so this is how the
5304
03:31:03,680 --> 03:31:04,800
title
5305
03:31:04,800 --> 03:31:07,600
case works
5306
03:31:07,680 --> 03:31:10,000
you can also count the occurrences of a
5307
03:31:10,000 --> 03:31:11,120
particular
5308
03:31:11,120 --> 03:31:12,960
element in a string
5309
03:31:12,960 --> 03:31:16,160
suppose i have string one
5310
03:31:16,160 --> 03:31:20,080
that has python programming
5311
03:31:23,520 --> 03:31:26,680
in str2
5312
03:31:26,960 --> 03:31:29,439
i'll write str1 dot
5313
03:31:29,439 --> 03:31:31,600
and use the count function to count how
5314
03:31:31,600 --> 03:31:34,880
many times let's say n the letter n is
5315
03:31:34,880 --> 03:31:36,399
present
5316
03:31:36,399 --> 03:31:39,200
then i'll print
5317
03:31:39,200 --> 03:31:41,439
saying
5318
03:31:41,439 --> 03:31:44,239
occurrences
5319
03:31:47,120 --> 03:31:49,040
give a colon here
5320
03:31:49,040 --> 03:31:50,960
and then i'll pass in my variable that
5321
03:31:50,960 --> 03:31:54,239
is str2 so this will count the number of
5322
03:31:54,239 --> 03:31:55,279
times
5323
03:31:55,279 --> 03:31:57,359
n is present in my original string so if
5324
03:31:57,359 --> 03:31:58,720
you see here
5325
03:31:58,720 --> 03:32:01,600
in python programming we have n present
5326
03:32:01,600 --> 03:32:02,960
twice
5327
03:32:02,960 --> 03:32:06,239
similarly let's check for
5328
03:32:06,239 --> 03:32:07,680
g
5329
03:32:07,680 --> 03:32:10,160
the letter g is also present
5330
03:32:10,160 --> 03:32:12,080
twice
5331
03:32:12,080 --> 03:32:14,880
let's see for y
5332
03:32:14,880 --> 03:32:17,520
now y is present only once which you can
5333
03:32:17,520 --> 03:32:19,920
see here
5334
03:32:20,840 --> 03:32:24,239
okay next
5335
03:32:24,239 --> 03:32:25,760
let's see how the
5336
03:32:25,760 --> 03:32:28,640
replace function works in
5337
03:32:28,640 --> 03:32:30,000
python
5338
03:32:30,000 --> 03:32:32,479
i'm giving a comment as replace
5339
03:32:32,479 --> 03:32:35,600
so let's say i have a string str1
5340
03:32:35,600 --> 03:32:37,840
which has
5341
03:32:37,840 --> 03:32:39,359
hamburg
5342
03:32:39,359 --> 03:32:40,720
is
5343
03:32:40,720 --> 03:32:42,080
located in
5344
03:32:42,080 --> 03:32:43,760
france
5345
03:32:43,760 --> 03:32:46,319
so hamburg is a city which is actually
5346
03:32:46,319 --> 03:32:47,520
located in
5347
03:32:47,520 --> 03:32:49,520
germany and not in france so using the
5348
03:32:49,520 --> 03:32:51,920
replace method we are going to replace
5349
03:32:51,920 --> 03:32:53,760
france with germany
5350
03:32:53,760 --> 03:32:55,439
so what i'll do is i'll create another
5351
03:32:55,439 --> 03:32:59,600
string str2 equal to i'll write str1 dot
5352
03:32:59,600 --> 03:33:01,439
and then i'll
5353
03:33:01,439 --> 03:33:03,760
use the replace function
5354
03:33:03,760 --> 03:33:06,800
and i'll say replace
5355
03:33:06,800 --> 03:33:09,040
my string france
5356
03:33:09,040 --> 03:33:10,720
with
5357
03:33:10,720 --> 03:33:11,760
country
5358
03:33:11,760 --> 03:33:13,520
germany
5359
03:33:13,520 --> 03:33:16,239
and then we'll print str2 and see the
5360
03:33:16,239 --> 03:33:18,080
result
5361
03:33:18,080 --> 03:33:19,520
there you go
5362
03:33:19,520 --> 03:33:22,160
so we have changed
5363
03:33:22,160 --> 03:33:24,720
hamburg is located in france to hamburg
5364
03:33:24,720 --> 03:33:27,920
is located in germany
5365
03:33:27,920 --> 03:33:30,640
now coming to the final method that we
5366
03:33:30,640 --> 03:33:32,880
are going to explore in this
5367
03:33:32,880 --> 03:33:35,600
python strings demo which is
5368
03:33:35,600 --> 03:33:38,640
using the split function
5369
03:33:38,640 --> 03:33:39,600
okay
5370
03:33:39,600 --> 03:33:42,239
so you can split the values for each
5371
03:33:42,239 --> 03:33:45,200
word in a string as separate
5372
03:33:45,200 --> 03:33:48,640
words and store it in the form of a list
5373
03:33:48,640 --> 03:33:50,080
so i
5374
03:33:50,080 --> 03:33:52,479
write str1
5375
03:33:52,479 --> 03:33:55,680
i'll just copy this
5376
03:33:55,680 --> 03:33:57,359
i'll paste it here
5377
03:33:57,359 --> 03:33:59,680
and i'll change france to germany
5378
03:33:59,680 --> 03:34:01,439
because hamburg is actually present in
5379
03:34:01,439 --> 03:34:03,520
germany and not in france
5380
03:34:03,520 --> 03:34:05,279
and then i'll create another variable
5381
03:34:05,279 --> 03:34:07,040
called str2
5382
03:34:07,040 --> 03:34:09,279
i'll write str1 dot
5383
03:34:09,279 --> 03:34:10,960
and then use the
5384
03:34:10,960 --> 03:34:12,960
split function
5385
03:34:12,960 --> 03:34:15,359
now let me print both the strings first
5386
03:34:15,359 --> 03:34:17,520
i'll print str1
5387
03:34:17,520 --> 03:34:19,760
and then i'll print
5388
03:34:19,760 --> 03:34:22,160
str2
5389
03:34:22,160 --> 03:34:24,560
if i run it you see
5390
03:34:24,560 --> 03:34:26,640
my original string was hamburg is
5391
03:34:26,640 --> 03:34:28,960
located in germany and then using the
5392
03:34:28,960 --> 03:34:31,040
split function we have
5393
03:34:31,040 --> 03:34:34,560
segregated or divided
5394
03:34:34,640 --> 03:34:36,479
each of the words present in the
5395
03:34:36,479 --> 03:34:38,560
original string into separate words and
5396
03:34:38,560 --> 03:34:40,239
this time it is present in the form of a
5397
03:34:40,239 --> 03:34:42,720
list let's see what operators are in
5398
03:34:42,720 --> 03:34:44,640
python
5399
03:34:44,640 --> 03:34:46,239
now operators are generally used to
5400
03:34:46,239 --> 03:34:47,840
perform operations on values and
5401
03:34:47,840 --> 03:34:49,920
variables in python
5402
03:34:49,920 --> 03:34:52,080
these are standard symbols used for
5403
03:34:52,080 --> 03:34:54,560
logical and arithmetic operations
5404
03:34:54,560 --> 03:34:56,560
the value that the operator operates on
5405
03:34:56,560 --> 03:34:58,319
is called the operand
5406
03:34:58,319 --> 03:35:00,239
so here on your screens you can see the
5407
03:35:00,239 --> 03:35:01,840
different types of operators available
5408
03:35:01,840 --> 03:35:03,040
in python
5409
03:35:03,040 --> 03:35:06,080
first we have arithmetic operators so
5410
03:35:06,080 --> 03:35:08,080
arithmetic operators are used to perform
5411
03:35:08,080 --> 03:35:10,319
mathematical operations
5412
03:35:10,319 --> 03:35:11,920
it returns either true or false
5413
03:35:11,920 --> 03:35:13,760
according to the condition
5414
03:35:13,760 --> 03:35:15,520
so these are the comparison operators
5415
03:35:15,520 --> 03:35:16,479
you have
5416
03:35:16,479 --> 03:35:18,239
less than greater than less than equal
5417
03:35:18,239 --> 03:35:21,040
to greater than equal to we have not
5418
03:35:21,040 --> 03:35:23,439
equal to and we have equal to equal to
5419
03:35:23,439 --> 03:35:24,880
third we have
5420
03:35:24,880 --> 03:35:26,800
assignment operators so assignment
5421
03:35:26,800 --> 03:35:28,560
operators are used in python to assign
5422
03:35:28,560 --> 03:35:30,800
values to variables so these are the
5423
03:35:30,800 --> 03:35:33,359
assignment operators you have equal to
5424
03:35:33,359 --> 03:35:35,439
you have plus equal to
5425
03:35:35,439 --> 03:35:37,439
so for example you have a variable
5426
03:35:37,439 --> 03:35:38,399
called x
5427
03:35:38,399 --> 03:35:41,120
and if you say x plus equal to 7 this
5428
03:35:41,120 --> 03:35:42,319
means
5429
03:35:42,319 --> 03:35:45,120
it is x equal to x plus 7.
5430
03:35:45,120 --> 03:35:48,640
similarly if you say x minus equal to 7
5431
03:35:48,640 --> 03:35:51,680
this means x equal to x minus 7
5432
03:35:51,680 --> 03:35:53,520
and similarly you can
5433
03:35:53,520 --> 03:35:56,960
see the other assignment operators here
5434
03:35:56,960 --> 03:35:58,640
next up we have the
5435
03:35:58,640 --> 03:36:01,120
logical operators so logical operators
5436
03:36:01,120 --> 03:36:03,120
are used in
5437
03:36:03,120 --> 03:36:04,840
the expression evaluation to make a
5438
03:36:04,840 --> 03:36:07,279
decision if both the expressions are
5439
03:36:07,279 --> 03:36:08,239
true
5440
03:36:08,239 --> 03:36:11,040
then the condition will be true for an
5441
03:36:11,040 --> 03:36:13,200
and operator
5442
03:36:13,200 --> 03:36:14,880
for an or operator if one of the
5443
03:36:14,880 --> 03:36:16,800
expressions is true then the or
5444
03:36:16,800 --> 03:36:18,560
condition will be true
5445
03:36:18,560 --> 03:36:20,560
now for not
5446
03:36:20,560 --> 03:36:23,600
if an expression a is true then not e
5447
03:36:23,600 --> 03:36:26,239
will be false
5448
03:36:26,239 --> 03:36:27,520
we also have
5449
03:36:27,520 --> 03:36:30,000
bitwise operators so bitwise operators
5450
03:36:30,000 --> 03:36:32,479
act on operands as if they were strings
5451
03:36:32,479 --> 03:36:35,120
of binary digits it acts on bits and
5452
03:36:35,120 --> 03:36:37,760
performs bit by bit operation so hence
5453
03:36:37,760 --> 03:36:40,880
it is called bitwise operator
5454
03:36:40,880 --> 03:36:43,439
so here each value is evaluated as
5455
03:36:43,439 --> 03:36:45,600
zeros and ones
5456
03:36:45,600 --> 03:36:48,000
then we have identity operators so
5457
03:36:48,000 --> 03:36:50,080
identity operators are used to check if
5458
03:36:50,080 --> 03:36:52,000
two values are located on the same part
5459
03:36:52,000 --> 03:36:53,359
of the memory
5460
03:36:53,359 --> 03:36:55,279
two variables that are equal does not
5461
03:36:55,279 --> 03:36:57,439
imply that they are identical we'll
5462
03:36:57,439 --> 03:36:59,279
understand each of these operators in
5463
03:36:59,279 --> 03:37:01,439
the demo session on the jupyter notebook
5464
03:37:01,439 --> 03:37:04,080
and finally we have membership operators
5465
03:37:04,080 --> 03:37:05,920
so membership operators are used to test
5466
03:37:05,920 --> 03:37:07,840
whether a value or variable is in a
5467
03:37:07,840 --> 03:37:10,479
sequence so you have is
5468
03:37:10,479 --> 03:37:12,800
and is not as identity operators in
5469
03:37:12,800 --> 03:37:14,880
membership operators we have in and not
5470
03:37:14,880 --> 03:37:17,600
in now let's head over to our jupiter
5471
03:37:17,600 --> 03:37:19,359
notebook and we'll see
5472
03:37:19,359 --> 03:37:21,600
and explore all these operators one by
5473
03:37:21,600 --> 03:37:23,120
one
5474
03:37:23,120 --> 03:37:25,200
so i am on my jupiter notebook operators
5475
03:37:25,200 --> 03:37:26,640
in python
5476
03:37:26,640 --> 03:37:28,800
so let's first start with arithmetic
5477
03:37:28,800 --> 03:37:30,960
operators
5478
03:37:30,960 --> 03:37:33,359
i'll just
5479
03:37:33,680 --> 03:37:36,160
give a comment here saying arithmetic
5480
03:37:36,160 --> 03:37:38,720
operators
5481
03:37:39,680 --> 03:37:41,520
okay
5482
03:37:41,520 --> 03:37:44,880
first up we'll see the addition operator
5483
03:37:44,880 --> 03:37:47,439
for that let me assign
5484
03:37:47,439 --> 03:37:48,399
two
5485
03:37:48,399 --> 03:37:51,279
variables so i have assigned a variable
5486
03:37:51,279 --> 03:37:53,200
x to a value 10
5487
03:37:53,200 --> 03:37:54,479
and let's say we have the second
5488
03:37:54,479 --> 03:37:57,760
variable y equal to 4.
5489
03:37:57,760 --> 03:37:58,960
now
5490
03:37:58,960 --> 03:38:01,439
if i say print
5491
03:38:01,439 --> 03:38:02,319
x
5492
03:38:02,319 --> 03:38:03,200
plus
5493
03:38:03,200 --> 03:38:04,720
y
5494
03:38:04,720 --> 03:38:07,200
it will perform an arithmetic operation
5495
03:38:07,200 --> 03:38:08,880
that is addition
5496
03:38:08,880 --> 03:38:10,960
and will sum the result
5497
03:38:10,960 --> 03:38:14,000
of x and y and gives me 14 because 10
5498
03:38:14,000 --> 03:38:16,800
plus 4 is 14.
5499
03:38:16,800 --> 03:38:18,880
then let's say
5500
03:38:18,880 --> 03:38:20,640
i'll write print
5501
03:38:20,640 --> 03:38:21,760
x
5502
03:38:21,760 --> 03:38:23,840
minus y
5503
03:38:23,840 --> 03:38:29,040
that gives me 6 because 10 minus 4 is 6.
5504
03:38:29,040 --> 03:38:31,840
similarly you can have
5505
03:38:31,840 --> 03:38:35,040
multiplication as well so i'll write x
5506
03:38:35,040 --> 03:38:36,800
asterix y
5507
03:38:36,800 --> 03:38:38,160
that gives me
5508
03:38:38,160 --> 03:38:41,520
40 because 10 multiplied by 4 is 40.
5509
03:38:41,520 --> 03:38:42,880
similarly
5510
03:38:42,880 --> 03:38:44,479
let's
5511
03:38:44,479 --> 03:38:45,920
say x
5512
03:38:45,920 --> 03:38:47,279
divided by
5513
03:38:47,279 --> 03:38:48,720
y
5514
03:38:48,720 --> 03:38:51,520
it gives me 2.5 which is basically the
5515
03:38:51,520 --> 03:38:52,880
quotient
5516
03:38:52,880 --> 03:38:56,000
of these two operands
5517
03:38:56,000 --> 03:38:58,399
all right
5518
03:38:58,479 --> 03:39:00,319
now
5519
03:39:00,319 --> 03:39:02,000
if you were to find the
5520
03:39:02,000 --> 03:39:04,080
remainder you can use the modulus
5521
03:39:04,080 --> 03:39:08,239
operator so i'll write x modulo y it
5522
03:39:08,239 --> 03:39:10,080
gives me 2
5523
03:39:10,080 --> 03:39:11,600
so you divide
5524
03:39:11,600 --> 03:39:16,479
10 by 4 the remainder is 2.
5525
03:39:17,520 --> 03:39:19,199
now you can use the
5526
03:39:19,199 --> 03:39:21,120
double asterisk operator which is
5527
03:39:21,120 --> 03:39:22,399
basically
5528
03:39:22,399 --> 03:39:24,080
known as power
5529
03:39:24,080 --> 03:39:26,560
so the left operand is raised to the
5530
03:39:26,560 --> 03:39:28,720
power of right
5531
03:39:28,720 --> 03:39:30,479
i'll write print
5532
03:39:30,479 --> 03:39:31,840
x
5533
03:39:31,840 --> 03:39:35,120
give to asterisks and then say y
5534
03:39:35,120 --> 03:39:37,600
it gives me 10 000 so what basically
5535
03:39:37,600 --> 03:39:39,120
python does is
5536
03:39:39,120 --> 03:39:42,800
it will raise 10 to the power of 4 so 10
5537
03:39:42,800 --> 03:39:45,520
is multiplied 4 times and the result is
5538
03:39:45,520 --> 03:39:48,080
10 000
5539
03:39:48,560 --> 03:39:50,640
you can also try out another example
5540
03:39:50,640 --> 03:39:54,239
let's say 2 raised to the power of 4
5541
03:39:54,239 --> 03:39:55,520
this gives me
5542
03:39:55,520 --> 03:39:56,960
16.
5543
03:39:56,960 --> 03:40:00,160
if i say 2 raised to the power of 3
5544
03:40:00,160 --> 03:40:05,279
that is 2 cube and the result is 8.
5545
03:40:05,279 --> 03:40:08,560
you can also perform flow division
5546
03:40:08,560 --> 03:40:10,399
so flow division results into whole
5547
03:40:10,399 --> 03:40:12,800
number adjusted to the left in the
5548
03:40:12,800 --> 03:40:16,319
number line so if i write print
5549
03:40:16,319 --> 03:40:17,279
x
5550
03:40:17,279 --> 03:40:19,359
and flow division is
5551
03:40:19,359 --> 03:40:21,120
given by 2
5552
03:40:21,120 --> 03:40:22,720
forward slash
5553
03:40:22,720 --> 03:40:24,479
then i'll write y
5554
03:40:24,479 --> 03:40:27,120
that gives me 2 so it returns a whole
5555
03:40:27,120 --> 03:40:28,720
number
5556
03:40:28,720 --> 03:40:30,840
a generic division gives me
5557
03:40:30,840 --> 03:40:32,399
2.5
5558
03:40:32,399 --> 03:40:34,880
that has the floating point value but a
5559
03:40:34,880 --> 03:40:37,199
flow division will give me a whole
5560
03:40:37,199 --> 03:40:38,319
number
5561
03:40:38,319 --> 03:40:42,160
adjusted to the left on the number line
5562
03:40:42,479 --> 03:40:45,040
all right so these are some of the basic
5563
03:40:45,040 --> 03:40:47,040
arithmetic operators that are very
5564
03:40:47,040 --> 03:40:48,960
popular in python
5565
03:40:48,960 --> 03:40:51,040
next we'll move on to
5566
03:40:51,040 --> 03:40:53,120
assignment operators
5567
03:40:53,120 --> 03:40:55,439
i'll give a comment as
5568
03:40:55,439 --> 03:40:59,000
assignment operators
5569
03:41:00,800 --> 03:41:02,640
okay
5570
03:41:02,640 --> 03:41:06,720
let's assign a value x equal to 7
5571
03:41:06,720 --> 03:41:09,840
so here we are assigning the value of
5572
03:41:09,840 --> 03:41:11,760
7 to x
5573
03:41:11,760 --> 03:41:12,560
and
5574
03:41:12,560 --> 03:41:14,720
your equal to is an example of
5575
03:41:14,720 --> 03:41:16,479
assignment operator
5576
03:41:16,479 --> 03:41:21,040
now if i print x it will give me 7.
5577
03:41:21,520 --> 03:41:25,120
let's say i'll again write x equal to 7
5578
03:41:25,120 --> 03:41:29,279
and in the next line i'll write x plus
5579
03:41:29,279 --> 03:41:31,520
equal to 3
5580
03:41:31,520 --> 03:41:35,040
and will print x
5581
03:41:35,840 --> 03:41:37,920
so what python does is
5582
03:41:37,920 --> 03:41:40,000
it adds 3
5583
03:41:40,000 --> 03:41:41,760
to my original value
5584
03:41:41,760 --> 03:41:45,359
that x holes that is 7 so here x plus
5585
03:41:45,359 --> 03:41:47,199
equal to 3 means
5586
03:41:47,199 --> 03:41:51,120
x equal to x plus 3
5587
03:41:51,120 --> 03:41:52,560
so
5588
03:41:52,560 --> 03:41:55,439
x equal to x plus 3 can be written as x
5589
03:41:55,439 --> 03:41:57,600
plus equal to 3
5590
03:41:57,600 --> 03:41:59,520
similarly
5591
03:41:59,520 --> 03:42:02,239
you can also
5592
03:42:02,560 --> 03:42:04,000
write x
5593
03:42:04,000 --> 03:42:05,760
minus equal to something
5594
03:42:05,760 --> 03:42:08,319
say x equal to 7 and then i write x
5595
03:42:08,319 --> 03:42:10,640
minus equal to
5596
03:42:10,640 --> 03:42:12,000
3
5597
03:42:12,000 --> 03:42:15,120
then we'll print x
5598
03:42:15,199 --> 03:42:17,120
this gives me 4
5599
03:42:17,120 --> 03:42:19,120
because
5600
03:42:19,120 --> 03:42:21,760
we are subtracting the original value of
5601
03:42:21,760 --> 03:42:24,160
x by 3
5602
03:42:24,160 --> 03:42:26,960
so here x minus equal to 3 means x equal
5603
03:42:26,960 --> 03:42:28,800
to x minus 3
5604
03:42:28,800 --> 03:42:31,040
okay
5605
03:42:33,680 --> 03:42:37,279
similarly let's say i'll write x
5606
03:42:37,279 --> 03:42:38,399
asterix
5607
03:42:38,399 --> 03:42:40,080
equal to 3
5608
03:42:40,080 --> 03:42:41,840
if i run it
5609
03:42:41,840 --> 03:42:44,239
and print the value of x it gives me 12
5610
03:42:44,239 --> 03:42:46,640
because our previous x value was 4 and
5611
03:42:46,640 --> 03:42:48,880
now we have multiplied x by
5612
03:42:48,880 --> 03:42:50,800
3 so x
5613
03:42:50,800 --> 03:42:54,479
value now becomes 12.
5614
03:42:54,880 --> 03:42:57,520
let's say i'll write x
5615
03:42:57,520 --> 03:42:59,120
divided by
5616
03:42:59,120 --> 03:43:00,560
3
5617
03:43:00,560 --> 03:43:03,760
and i'll print x it gives me 4.0 this is
5618
03:43:03,760 --> 03:43:06,560
a floating point number so we are
5619
03:43:06,560 --> 03:43:07,760
dividing
5620
03:43:07,760 --> 03:43:12,640
12 by 3 and the result is 4.0
5621
03:43:12,640 --> 03:43:14,880
cool
5622
03:43:14,960 --> 03:43:19,160
if you want you can also perform
5623
03:43:20,399 --> 03:43:22,239
an exponent operation
5624
03:43:22,239 --> 03:43:24,960
or a power operation so let's say i have
5625
03:43:24,960 --> 03:43:26,800
x equal to 7
5626
03:43:26,800 --> 03:43:28,720
and then i write x
5627
03:43:28,720 --> 03:43:31,359
double asterisks equal to
5628
03:43:31,359 --> 03:43:33,040
say 2
5629
03:43:33,040 --> 03:43:36,800
and then we'll print x
5630
03:43:37,600 --> 03:43:39,520
okay there is an error here let's debug
5631
03:43:39,520 --> 03:43:41,120
the error
5632
03:43:41,120 --> 03:43:43,359
i have put a space here let's delete
5633
03:43:43,359 --> 03:43:46,160
that space you have to keep this in mind
5634
03:43:46,160 --> 03:43:48,800
now let's run it there you go so 7
5635
03:43:48,800 --> 03:43:50,720
raised to the power of 2 which is
5636
03:43:50,720 --> 03:43:53,760
basically 7 square is 49.
5637
03:43:53,760 --> 03:43:56,479
you can change the values and
5638
03:43:56,479 --> 03:43:58,479
run other examples let's say 10 raised
5639
03:43:58,479 --> 03:43:59,920
to the power of
5640
03:43:59,920 --> 03:44:03,680
3 we have 1000 as the result
5641
03:44:03,680 --> 03:44:04,640
okay
5642
03:44:04,640 --> 03:44:07,439
now moving ahead
5643
03:44:07,920 --> 03:44:09,760
now we'll understand about
5644
03:44:09,760 --> 03:44:11,840
comparison operators so i'll give a
5645
03:44:11,840 --> 03:44:13,840
comment
5646
03:44:13,840 --> 03:44:14,720
as
5647
03:44:14,720 --> 03:44:17,840
comparison operators
5648
03:44:17,840 --> 03:44:19,120
okay
5649
03:44:19,120 --> 03:44:22,399
let's say i have x equal to 7 and then i
5650
03:44:22,399 --> 03:44:23,920
have y equal to
5651
03:44:23,920 --> 03:44:25,439
3
5652
03:44:25,439 --> 03:44:27,920
if i print
5653
03:44:27,920 --> 03:44:31,840
x equal to equal to y
5654
03:44:31,840 --> 03:44:35,359
it will give me false the reason being
5655
03:44:35,359 --> 03:44:38,880
the value that x holds is not equal to
5656
03:44:38,880 --> 03:44:40,960
the value present in y these are two
5657
03:44:40,960 --> 03:44:43,520
different values that x and y hold so x
5658
03:44:43,520 --> 03:44:45,600
is not equal to y
5659
03:44:45,600 --> 03:44:47,680
and if we print x equal to equal to y it
5660
03:44:47,680 --> 03:44:50,479
will give us false
5661
03:44:51,279 --> 03:44:52,479
now
5662
03:44:52,479 --> 03:44:55,680
i'll just copy this code
5663
03:44:55,680 --> 03:44:56,840
i'll print it
5664
03:44:56,840 --> 03:44:58,479
here
5665
03:44:58,479 --> 03:45:00,960
and instead of equal to i'll give not
5666
03:45:00,960 --> 03:45:02,000
equal to
5667
03:45:02,000 --> 03:45:03,920
so not equal to is represented by an
5668
03:45:03,920 --> 03:45:06,560
exclamation mark followed by an equal to
5669
03:45:06,560 --> 03:45:09,199
if i write x not equal to y
5670
03:45:09,199 --> 03:45:11,120
this gives us true because x and y are
5671
03:45:11,120 --> 03:45:12,880
two different values and they are not
5672
03:45:12,880 --> 03:45:15,199
equal
5673
03:45:15,359 --> 03:45:18,000
i'll print
5674
03:45:19,600 --> 03:45:22,239
my code again and this time let's say
5675
03:45:22,239 --> 03:45:23,680
i'll write
5676
03:45:23,680 --> 03:45:25,359
x
5677
03:45:25,359 --> 03:45:27,520
greater than
5678
03:45:27,520 --> 03:45:28,960
y
5679
03:45:28,960 --> 03:45:30,880
it gives us true
5680
03:45:30,880 --> 03:45:34,000
because 7 is greater than 3
5681
03:45:34,000 --> 03:45:36,560
if i write x less than y
5682
03:45:36,560 --> 03:45:39,359
and hit shift enter that gives me false
5683
03:45:39,359 --> 03:45:44,080
because 7 is not less than 3
5684
03:45:46,160 --> 03:45:48,000
i'll print the code again this time
5685
03:45:48,000 --> 03:45:49,840
we'll see
5686
03:45:49,840 --> 03:45:52,960
if x is greater than equal to 3
5687
03:45:52,960 --> 03:45:55,840
if i run it it is true because 7 is
5688
03:45:55,840 --> 03:45:58,080
greater than equal to 3
5689
03:45:58,080 --> 03:46:00,319
and
5690
03:46:00,319 --> 03:46:03,120
now let's check for less than equal to
5691
03:46:03,120 --> 03:46:05,600
this is false because
5692
03:46:05,600 --> 03:46:07,120
7 is not
5693
03:46:07,120 --> 03:46:10,319
less than equal to 3
5694
03:46:10,720 --> 03:46:13,120
okay so these are some of the
5695
03:46:13,120 --> 03:46:14,880
basic comparison operators that are
5696
03:46:14,880 --> 03:46:16,960
available in python
5697
03:46:16,960 --> 03:46:19,600
now moving ahead we'll see
5698
03:46:19,600 --> 03:46:23,840
how logical operators work in python
5699
03:46:26,479 --> 03:46:28,800
okay
5700
03:46:29,040 --> 03:46:29,920
first
5701
03:46:29,920 --> 03:46:33,040
we look at the and operator
5702
03:46:33,040 --> 03:46:36,080
in python
5703
03:46:36,080 --> 03:46:38,720
suppose i have a value
5704
03:46:38,720 --> 03:46:41,520
of 7 stored in x
5705
03:46:41,520 --> 03:46:44,560
and then i have a value of 12 stored in
5706
03:46:44,560 --> 03:46:45,840
y
5707
03:46:45,840 --> 03:46:46,960
if i write
5708
03:46:46,960 --> 03:46:48,560
print
5709
03:46:48,560 --> 03:46:51,439
x greater than
5710
03:46:51,439 --> 03:46:52,800
2
5711
03:46:52,800 --> 03:46:53,920
and
5712
03:46:53,920 --> 03:46:55,199
y
5713
03:46:55,199 --> 03:46:57,520
less than 10
5714
03:46:57,520 --> 03:47:00,080
let's run it and see the result it gives
5715
03:47:00,080 --> 03:47:01,520
us false
5716
03:47:01,520 --> 03:47:03,279
now
5717
03:47:03,279 --> 03:47:05,760
and logical operators
5718
03:47:05,760 --> 03:47:07,920
will return true
5719
03:47:07,920 --> 03:47:09,359
if both the
5720
03:47:09,359 --> 03:47:11,520
conditions are true
5721
03:47:11,520 --> 03:47:13,680
and it will return false even if one of
5722
03:47:13,680 --> 03:47:15,439
the condition is false
5723
03:47:15,439 --> 03:47:16,880
so here
5724
03:47:16,880 --> 03:47:20,080
x is greater than 2 which means 7 is
5725
03:47:20,080 --> 03:47:22,640
greater than 2 is true
5726
03:47:22,640 --> 03:47:24,239
but
5727
03:47:24,239 --> 03:47:24,960
y
5728
03:47:24,960 --> 03:47:28,080
less than 10 which is 12 less than 10 is
5729
03:47:28,080 --> 03:47:29,920
false and hence our entire expression
5730
03:47:29,920 --> 03:47:33,640
becomes false
5731
03:47:34,560 --> 03:47:38,479
next let's see i have x equal to 5
5732
03:47:38,479 --> 03:47:40,160
and then
5733
03:47:40,160 --> 03:47:42,239
i'll print
5734
03:47:42,239 --> 03:47:44,880
x greater than 3
5735
03:47:44,880 --> 03:47:48,000
or x
5736
03:47:48,000 --> 03:47:49,040
less than
5737
03:47:49,040 --> 03:47:50,960
10. if i run this
5738
03:47:50,960 --> 03:47:54,080
it returns as true the reason being
5739
03:47:54,080 --> 03:47:57,279
x greater than 3 is true which means 5
5740
03:47:57,279 --> 03:48:00,399
is greater than 3 that is true
5741
03:48:00,399 --> 03:48:02,319
but 5 is
5742
03:48:02,319 --> 03:48:03,920
not less than 10
5743
03:48:03,920 --> 03:48:04,800
but
5744
03:48:04,800 --> 03:48:07,279
since one of the conditions is true so
5745
03:48:07,279 --> 03:48:09,680
the or operation in python returns us
5746
03:48:09,680 --> 03:48:12,000
true
5747
03:48:12,560 --> 03:48:15,520
now
5748
03:48:15,520 --> 03:48:17,920
let's see another example
5749
03:48:17,920 --> 03:48:20,160
i'll write x equal to 5
5750
03:48:20,160 --> 03:48:21,439
and
5751
03:48:21,439 --> 03:48:23,040
y equal to
5752
03:48:23,040 --> 03:48:24,640
10
5753
03:48:24,640 --> 03:48:26,720
i'll print
5754
03:48:26,720 --> 03:48:27,520
x
5755
03:48:27,520 --> 03:48:29,439
greater than 10
5756
03:48:29,439 --> 03:48:30,960
or
5757
03:48:30,960 --> 03:48:32,479
by
5758
03:48:32,479 --> 03:48:35,359
less than 20
5759
03:48:35,359 --> 03:48:36,880
if i run this
5760
03:48:36,880 --> 03:48:38,960
it returns true
5761
03:48:38,960 --> 03:48:41,439
so let's see our expression
5762
03:48:41,439 --> 03:48:45,439
x greater than 10 that is
5763
03:48:45,439 --> 03:48:49,920
5 greater than 10 is false but
5764
03:48:49,920 --> 03:48:53,439
y less than 20 so 10 is less than 20 is
5765
03:48:53,439 --> 03:48:55,920
true so our or operation will give us
5766
03:48:55,920 --> 03:49:00,080
true since one of the condition is true
5767
03:49:00,640 --> 03:49:03,120
now moving to the next logical operator
5768
03:49:03,120 --> 03:49:05,359
we have not
5769
03:49:05,359 --> 03:49:08,000
i'll given
5770
03:49:08,239 --> 03:49:12,640
comment here as r and now we'll see not
5771
03:49:12,640 --> 03:49:16,160
let's see i have x equal to 5 and then
5772
03:49:16,160 --> 03:49:17,439
i'll
5773
03:49:17,439 --> 03:49:19,840
print
5774
03:49:22,840 --> 03:49:24,960
x than
5775
03:49:24,960 --> 03:49:25,760
3
5776
03:49:25,760 --> 03:49:26,960
and
5777
03:49:26,960 --> 03:49:29,520
x less than 10
5778
03:49:29,520 --> 03:49:31,920
this should ideally give us true the
5779
03:49:31,920 --> 03:49:34,399
reason being x is greater than 3 and x
5780
03:49:34,399 --> 03:49:36,080
is less than 10
5781
03:49:36,080 --> 03:49:38,560
let's run and verify okay so this is
5782
03:49:38,560 --> 03:49:41,439
true now let me copy this and i'll paste
5783
03:49:41,439 --> 03:49:43,199
it in the next cell
5784
03:49:43,199 --> 03:49:45,520
now i'll replace
5785
03:49:45,520 --> 03:49:48,399
my above print statement with a
5786
03:49:48,399 --> 03:49:50,160
not operator
5787
03:49:50,160 --> 03:49:52,640
and we'll close our print function with
5788
03:49:52,640 --> 03:49:54,080
another bracket
5789
03:49:54,080 --> 03:49:55,920
now let's run it
5790
03:49:55,920 --> 03:49:58,000
so this will give us
5791
03:49:58,000 --> 03:50:00,160
false because
5792
03:50:00,160 --> 03:50:00,960
my
5793
03:50:00,960 --> 03:50:03,279
expression evaluates to true
5794
03:50:03,279 --> 03:50:07,120
but not true is false
5795
03:50:07,120 --> 03:50:08,479
okay
5796
03:50:08,479 --> 03:50:10,640
now moving ahead
5797
03:50:10,640 --> 03:50:13,279
let's discuss
5798
03:50:13,279 --> 03:50:17,399
identity operators in python
5799
03:50:20,319 --> 03:50:22,479
all right
5800
03:50:22,479 --> 03:50:25,040
so we'll start by assigning a value of
5801
03:50:25,040 --> 03:50:26,560
let's say
5802
03:50:26,560 --> 03:50:29,040
505 to x
5803
03:50:29,040 --> 03:50:32,239
and then in y i'll have
5804
03:50:32,239 --> 03:50:34,720
500 plus 5
5805
03:50:34,720 --> 03:50:37,520
so which is also 505
5806
03:50:37,520 --> 03:50:39,920
so x and y hold
5807
03:50:39,920 --> 03:50:43,199
the same value of 505
5808
03:50:43,199 --> 03:50:44,960
so i'll print
5809
03:50:44,960 --> 03:50:47,359
x comma y
5810
03:50:47,359 --> 03:50:49,439
so you can see here both the values are
5811
03:50:49,439 --> 03:50:51,680
same 505
5812
03:50:51,680 --> 03:50:54,000
for x and y
5813
03:50:54,000 --> 03:50:56,880
now i'll use the
5814
03:50:58,160 --> 03:51:00,640
logical operator that is
5815
03:51:00,640 --> 03:51:02,800
double equal to or equal to equal to
5816
03:51:02,800 --> 03:51:05,120
i'll write x equal to equal to y
5817
03:51:05,120 --> 03:51:07,199
this will give us true because x and y
5818
03:51:07,199 --> 03:51:08,319
hold the
5819
03:51:08,319 --> 03:51:10,640
same value of 505
5820
03:51:10,640 --> 03:51:12,479
but
5821
03:51:12,479 --> 03:51:14,479
let's check with the identity operator
5822
03:51:14,479 --> 03:51:16,720
so i'll write x is
5823
03:51:16,720 --> 03:51:17,840
y
5824
03:51:17,840 --> 03:51:19,279
if i run this
5825
03:51:19,279 --> 03:51:21,040
it gives me false
5826
03:51:21,040 --> 03:51:22,720
the reason being
5827
03:51:22,720 --> 03:51:25,199
x and y both refer to objects whose
5828
03:51:25,199 --> 03:51:26,560
value is
5829
03:51:26,560 --> 03:51:28,960
505
5830
03:51:28,960 --> 03:51:31,199
they are equal but they are not
5831
03:51:31,199 --> 03:51:33,600
referencing to the same object
5832
03:51:33,600 --> 03:51:36,479
now this you can verify using the id
5833
03:51:36,479 --> 03:51:39,920
function in python if i write id of x
5834
03:51:39,920 --> 03:51:42,479
this will give me the memory location
5835
03:51:42,479 --> 03:51:44,960
that x has you can see this is the
5836
03:51:44,960 --> 03:51:46,720
memory location of x
5837
03:51:46,720 --> 03:51:49,359
now if i write id of y
5838
03:51:49,359 --> 03:51:51,279
there you go so this is a separate
5839
03:51:51,279 --> 03:51:53,279
memory location in which
5840
03:51:53,279 --> 03:51:56,960
y is holding the value of 505 and hence
5841
03:51:56,960 --> 03:51:59,600
x is y is different
5842
03:51:59,600 --> 03:52:02,720
and hence it is false
5843
03:52:03,680 --> 03:52:06,960
suppose i write x equal to 100 and i
5844
03:52:06,960 --> 03:52:09,279
have y equal to 200
5845
03:52:09,279 --> 03:52:11,279
if i write x
5846
03:52:11,279 --> 03:52:14,160
is not y
5847
03:52:14,880 --> 03:52:17,520
this gives us true the reason being
5848
03:52:17,520 --> 03:52:19,439
x and y both are holding
5849
03:52:19,439 --> 03:52:25,160
different values so x is not y is true
5850
03:52:26,479 --> 03:52:28,640
let's see another example this time
5851
03:52:28,640 --> 03:52:31,040
we'll define
5852
03:52:31,040 --> 03:52:32,640
two lists
5853
03:52:32,640 --> 03:52:35,120
so i have a list called a that has value
5854
03:52:35,120 --> 03:52:36,800
let's say
5855
03:52:36,800 --> 03:52:38,319
5 comma
5856
03:52:38,319 --> 03:52:40,399
6 comma
5857
03:52:40,399 --> 03:52:42,960
3 comma 7
5858
03:52:42,960 --> 03:52:46,960
comma 1 comma let's say 9
5859
03:52:46,960 --> 03:52:49,199
and then
5860
03:52:49,199 --> 03:52:50,560
i'll have
5861
03:52:50,560 --> 03:52:52,080
another list
5862
03:52:52,080 --> 03:52:54,560
that will also have the same elements
5863
03:52:54,560 --> 03:52:57,520
i'll copy this and i'll paste it here
5864
03:52:57,520 --> 03:52:58,720
okay
5865
03:52:58,720 --> 03:53:00,560
if i write
5866
03:53:00,560 --> 03:53:02,640
c equal to a
5867
03:53:02,640 --> 03:53:06,920
and then we'll print
5868
03:53:08,399 --> 03:53:11,120
a is c
5869
03:53:11,120 --> 03:53:14,800
if i run this it gives us true because
5870
03:53:14,800 --> 03:53:16,640
we are assigning
5871
03:53:16,640 --> 03:53:21,279
the variable a to c and hence
5872
03:53:21,279 --> 03:53:23,760
the memory locations are the same for
5873
03:53:23,760 --> 03:53:26,640
both and hence
5874
03:53:26,640 --> 03:53:30,399
a c gives us true
5875
03:53:30,399 --> 03:53:32,800
what if i write a
5876
03:53:32,800 --> 03:53:33,840
is
5877
03:53:33,840 --> 03:53:35,120
b
5878
03:53:35,120 --> 03:53:37,840
now this gives us false
5879
03:53:37,840 --> 03:53:41,279
so here a and b are both less
5880
03:53:41,279 --> 03:53:43,920
they are equal but not identical it is
5881
03:53:43,920 --> 03:53:46,479
because the interpreter in python
5882
03:53:46,479 --> 03:53:48,960
allocates them separately in memory
5883
03:53:48,960 --> 03:53:51,840
although they are equal
5884
03:53:53,120 --> 03:53:54,160
okay
5885
03:53:54,160 --> 03:53:57,199
now let's talk about
5886
03:53:57,199 --> 03:53:59,439
membership operators in python i'll give
5887
03:53:59,439 --> 03:54:02,239
a comment as
5888
03:54:02,840 --> 03:54:06,960
membership operators
5889
03:54:09,040 --> 03:54:10,800
okay
5890
03:54:10,800 --> 03:54:14,000
let's see i have a list
5891
03:54:14,000 --> 03:54:16,239
a that has
5892
03:54:16,239 --> 03:54:18,640
the above values five six three seven
5893
03:54:18,640 --> 03:54:21,279
one and nine i'll copy this
5894
03:54:21,279 --> 03:54:23,840
and i'll paste it here
5895
03:54:23,840 --> 03:54:24,800
okay
5896
03:54:24,800 --> 03:54:26,640
i have
5897
03:54:26,640 --> 03:54:28,080
another list
5898
03:54:28,080 --> 03:54:29,680
b that has
5899
03:54:29,680 --> 03:54:33,199
different types of elements we have 4
5900
03:54:33,199 --> 03:54:36,479
next element is python
5901
03:54:36,479 --> 03:54:39,199
then we have
5902
03:54:39,920 --> 03:54:41,760
2
5903
03:54:41,760 --> 03:54:42,720
comma
5904
03:54:42,720 --> 03:54:43,600
7
5905
03:54:43,600 --> 03:54:45,760
comma let's say
5906
03:54:45,760 --> 03:54:47,520
simply learn
5907
03:54:47,520 --> 03:54:49,359
and then i have
5908
03:54:49,359 --> 03:54:51,359
nine
5909
03:54:51,359 --> 03:54:54,160
i'll run this first so i have my
5910
03:54:54,160 --> 03:54:59,040
two lists a and b i have printed them
5911
03:54:59,199 --> 03:55:00,720
let's explore how the membership
5912
03:55:00,720 --> 03:55:03,120
operator works so
5913
03:55:03,120 --> 03:55:06,160
we saw in the slides in and not in our
5914
03:55:06,160 --> 03:55:09,199
two membership operators
5915
03:55:09,199 --> 03:55:11,840
if i write 5 in a
5916
03:55:11,840 --> 03:55:14,800
and run it it returns me true because
5917
03:55:14,800 --> 03:55:16,239
you see here
5918
03:55:16,239 --> 03:55:19,359
5 is present in a and hence 5 in a will
5919
03:55:19,359 --> 03:55:21,760
return true
5920
03:55:21,760 --> 03:55:24,720
what if i write 55 in a
5921
03:55:24,720 --> 03:55:27,520
this gives us false because you see here
5922
03:55:27,520 --> 03:55:29,920
in the list a we don't have
5923
03:55:29,920 --> 03:55:33,439
an item 55 in a
5924
03:55:33,439 --> 03:55:36,720
so it gives us false
5925
03:55:37,600 --> 03:55:41,120
what if i write 4
5926
03:55:41,120 --> 03:55:42,399
in
5927
03:55:42,399 --> 03:55:43,920
b
5928
03:55:43,920 --> 03:55:46,399
this gives me true
5929
03:55:46,399 --> 03:55:49,120
you see here in b we have the element 4
5930
03:55:49,120 --> 03:55:52,960
so 4 and b is true
5931
03:55:54,319 --> 03:55:57,120
one thing i wanted to show you is
5932
03:55:57,120 --> 03:55:58,479
what if i write
5933
03:55:58,479 --> 03:56:00,000
python
5934
03:56:00,000 --> 03:56:00,720
in
5935
03:56:00,720 --> 03:56:02,880
b
5936
03:56:02,960 --> 03:56:04,720
you see here
5937
03:56:04,720 --> 03:56:07,040
it gives us false
5938
03:56:07,040 --> 03:56:08,840
though we have
5939
03:56:08,840 --> 03:56:12,239
python in our list b
5940
03:56:12,239 --> 03:56:13,359
but
5941
03:56:13,359 --> 03:56:15,760
the python i'm trying to check
5942
03:56:15,760 --> 03:56:19,279
is not present in b the reason being
5943
03:56:19,279 --> 03:56:20,239
here
5944
03:56:20,239 --> 03:56:22,840
python is written in lowercase
5945
03:56:22,840 --> 03:56:24,720
but
5946
03:56:24,720 --> 03:56:26,960
what i am checking
5947
03:56:26,960 --> 03:56:28,560
python p is
5948
03:56:28,560 --> 03:56:30,560
in upper case
5949
03:56:30,560 --> 03:56:32,479
so it is case sensitive
5950
03:56:32,479 --> 03:56:36,160
and hence this gives me false
5951
03:56:36,479 --> 03:56:37,840
let's say
5952
03:56:37,840 --> 03:56:40,640
i'll declare a dictionary y
5953
03:56:40,640 --> 03:56:43,279
equal to and within curly braces
5954
03:56:43,279 --> 03:56:44,640
i'll
5955
03:56:44,640 --> 03:56:46,960
give my key a
5956
03:56:46,960 --> 03:56:48,960
and i'll assign the value
5957
03:56:48,960 --> 03:56:51,120
of india
5958
03:56:51,120 --> 03:56:54,239
let's say i have another
5959
03:56:54,479 --> 03:56:55,359
key
5960
03:56:55,359 --> 03:56:56,800
b
5961
03:56:56,800 --> 03:57:00,239
and that has a value let's say usa
5962
03:57:00,239 --> 03:57:04,239
now if i check a in y
5963
03:57:04,239 --> 03:57:07,520
it gives me true because your key a is
5964
03:57:07,520 --> 03:57:09,359
present in y
5965
03:57:09,359 --> 03:57:12,560
now what if i check
5966
03:57:12,800 --> 03:57:15,120
india
5967
03:57:15,120 --> 03:57:16,880
in
5968
03:57:16,880 --> 03:57:18,160
by
5969
03:57:18,160 --> 03:57:22,319
now this gives me false so here
5970
03:57:22,720 --> 03:57:24,800
the membership operator won't work on
5971
03:57:24,800 --> 03:57:27,279
the values it only works on keys in a
5972
03:57:27,279 --> 03:57:29,520
dictionary
5973
03:57:29,520 --> 03:57:31,680
now let me go ahead and change the keys
5974
03:57:31,680 --> 03:57:34,640
to numbers i'll give one
5975
03:57:34,640 --> 03:57:36,720
that has india and the second key i'll
5976
03:57:36,720 --> 03:57:38,479
have two
5977
03:57:38,479 --> 03:57:39,680
now
5978
03:57:39,680 --> 03:57:42,319
if i change e to one
5979
03:57:42,319 --> 03:57:44,560
it will work fine it gives me
5980
03:57:44,560 --> 03:57:45,840
india
5981
03:57:45,840 --> 03:57:50,160
but if i write usa in y it gives me
5982
03:57:50,160 --> 03:57:52,080
false as i said
5983
03:57:52,080 --> 03:57:54,319
the membership operator won't work on
5984
03:57:54,319 --> 03:57:55,680
the
5985
03:57:55,680 --> 03:57:57,760
values it only works on the keys in a
5986
03:57:57,760 --> 03:58:00,160
dictionary
5987
03:58:00,160 --> 03:58:01,199
cool
5988
03:58:01,199 --> 03:58:03,439
now coming to the final section
5989
03:58:03,439 --> 03:58:05,359
of our demo
5990
03:58:05,359 --> 03:58:06,880
we'll explore about
5991
03:58:06,880 --> 03:58:09,920
bitwise operator
5992
03:58:11,920 --> 03:58:14,239
okay
5993
03:58:14,720 --> 03:58:19,199
so python bitwise and operator returns 1
5994
03:58:19,199 --> 03:58:22,160
if both the bits are 1 otherwise it will
5995
03:58:22,160 --> 03:58:25,520
return 0. so first let's see
5996
03:58:25,520 --> 03:58:27,040
7
5997
03:58:27,040 --> 03:58:29,279
ampersand 2.
5998
03:58:29,279 --> 03:58:30,479
if i run it
5999
03:58:30,479 --> 03:58:33,120
it gives me the value of 2.
6000
03:58:33,120 --> 03:58:34,880
so what python does is it will first
6001
03:58:34,880 --> 03:58:38,160
convert the numbers 7 and
6002
03:58:38,160 --> 03:58:41,279
2 to their binary equivalents that is in
6003
03:58:41,279 --> 03:58:43,040
the form of zeros and ones and then it
6004
03:58:43,040 --> 03:58:45,680
will perform the bitwise and operation
6005
03:58:45,680 --> 03:58:46,399
so
6006
03:58:46,399 --> 03:58:49,840
7 ampersand 2 the value is 2
6007
03:58:49,840 --> 03:58:52,479
if i write 3 ampersand
6008
03:58:52,479 --> 03:58:54,800
5
6009
03:58:54,800 --> 03:58:57,359
the value is 1 again it will first
6010
03:58:57,359 --> 03:59:00,800
convert 3 and 5 to binary equivalence in
6011
03:59:00,800 --> 03:59:02,800
the form of zeros and ones
6012
03:59:02,800 --> 03:59:04,560
and then it will perform the and
6013
03:59:04,560 --> 03:59:08,000
operation the bitwise and operation
6014
03:59:08,000 --> 03:59:10,000
now the python bitwise or operator
6015
03:59:10,000 --> 03:59:14,319
returns 1 if any of the bits is 1
6016
03:59:14,319 --> 03:59:18,399
if both the bits are 0 it will return 0.
6017
03:59:18,399 --> 03:59:20,880
so if i write
6018
03:59:20,880 --> 03:59:22,560
10
6019
03:59:22,560 --> 03:59:24,640
or
6020
03:59:24,640 --> 03:59:26,399
5
6021
03:59:26,399 --> 03:59:29,120
it gives me 15 so first it will convert
6022
03:59:29,120 --> 03:59:31,279
10 and 5 to binary equivalence and then
6023
03:59:31,279 --> 03:59:34,239
it will perform the bitwise or operation
6024
03:59:34,239 --> 03:59:37,520
let's see another example i'll write 4
6025
03:59:37,520 --> 03:59:39,840
or
6026
03:59:39,840 --> 03:59:41,279
i'll run it
6027
03:59:41,279 --> 03:59:42,399
so the
6028
03:59:42,399 --> 03:59:45,439
bitwise or operation gives us 12 as the
6029
03:59:45,439 --> 03:59:47,840
result
6030
03:59:48,080 --> 03:59:50,800
in programming languages conditions play
6031
03:59:50,800 --> 03:59:52,479
a very crucial role
6032
03:59:52,479 --> 03:59:54,080
they help us develop programs that can
6033
03:59:54,080 --> 03:59:55,840
satisfy all the business conditions and
6034
03:59:55,840 --> 03:59:57,120
perform well
6035
03:59:57,120 --> 03:59:59,040
so conditional statements are useful
6036
03:59:59,040 --> 04:00:00,880
when we have to decide that a certain
6037
04:00:00,880 --> 04:00:03,040
condition or certain part of a code
6038
04:00:03,040 --> 04:00:06,319
should run only if the condition is true
6039
04:00:06,319 --> 04:00:08,479
so the if statement is an example of
6040
04:00:08,479 --> 04:00:10,160
conditional statements
6041
04:00:10,160 --> 04:00:12,319
the program will first
6042
04:00:12,319 --> 04:00:14,640
evaluate the test condition
6043
04:00:14,640 --> 04:00:16,800
and will only execute the code block if
6044
04:00:16,800 --> 04:00:19,199
the test conditional expression is true
6045
04:00:19,199 --> 04:00:21,760
so here you can see the syntax of
6046
04:00:21,760 --> 04:00:24,160
if statement first we have the
6047
04:00:24,160 --> 04:00:27,279
if keyword and inside
6048
04:00:27,279 --> 04:00:28,960
parenthesis we are going to pass the
6049
04:00:28,960 --> 04:00:30,080
condition
6050
04:00:30,080 --> 04:00:30,880
and
6051
04:00:30,880 --> 04:00:33,439
if this condition evaluates to true then
6052
04:00:33,439 --> 04:00:35,359
we go inside the body of the if
6053
04:00:35,359 --> 04:00:36,640
statement which is this part of the
6054
04:00:36,640 --> 04:00:38,160
syntax
6055
04:00:38,160 --> 04:00:39,040
now
6056
04:00:39,040 --> 04:00:40,800
while developing a code for a business
6057
04:00:40,800 --> 04:00:43,199
problem you might have to use multiple
6058
04:00:43,199 --> 04:00:44,319
conditions
6059
04:00:44,319 --> 04:00:46,319
so the alif statement is used when we
6060
04:00:46,319 --> 04:00:48,640
have to check multiple conditions so
6061
04:00:48,640 --> 04:00:51,439
here you can see
6062
04:00:51,439 --> 04:00:53,760
if the first test expression evaluates
6063
04:00:53,760 --> 04:00:55,840
to false then you have the alif
6064
04:00:55,840 --> 04:00:57,199
condition
6065
04:00:57,199 --> 04:00:58,160
and
6066
04:00:58,160 --> 04:01:00,399
if the lift condition evaluates to true
6067
04:01:00,399 --> 04:01:02,880
then we go inside the body of the
6068
04:01:02,880 --> 04:01:05,359
elif statement which is this part of the
6069
04:01:05,359 --> 04:01:07,359
statement or the syntax that i am
6070
04:01:07,359 --> 04:01:09,520
highlighting
6071
04:01:09,520 --> 04:01:11,279
next
6072
04:01:11,279 --> 04:01:13,040
so sometimes you want to evaluate a
6073
04:01:13,040 --> 04:01:15,040
condition and take one path if it is
6074
04:01:15,040 --> 04:01:15,920
true
6075
04:01:15,920 --> 04:01:18,160
but specify an alternative path if it is
6076
04:01:18,160 --> 04:01:20,880
not so this is done with an
6077
04:01:20,880 --> 04:01:22,960
else clause
6078
04:01:22,960 --> 04:01:25,600
so the else code block will run if the
6079
04:01:25,600 --> 04:01:27,279
expression is false
6080
04:01:27,279 --> 04:01:29,840
so here you can see if the
6081
04:01:29,840 --> 04:01:32,800
condition for the if statement
6082
04:01:32,800 --> 04:01:35,359
returns false then we have the body of
6083
04:01:35,359 --> 04:01:36,239
the
6084
04:01:36,239 --> 04:01:38,080
else statement
6085
04:01:38,080 --> 04:01:39,760
and finally
6086
04:01:39,760 --> 04:01:41,040
after
6087
04:01:41,040 --> 04:01:43,040
all the if alif and else statements have
6088
04:01:43,040 --> 04:01:45,199
been executed we will execute if there
6089
04:01:45,199 --> 04:01:46,319
are any other statements which are
6090
04:01:46,319 --> 04:01:48,479
present outside the if condition now
6091
04:01:48,479 --> 04:01:50,000
moving ahead
6092
04:01:50,000 --> 04:01:52,800
we will use if else and lf statements to
6093
04:01:52,800 --> 04:01:54,399
write a program that will solve a
6094
04:01:54,399 --> 04:01:56,399
business problem
6095
04:01:56,399 --> 04:01:58,880
so based on the location and total
6096
04:01:58,880 --> 04:02:01,120
amount of the items we are going to find
6097
04:02:01,120 --> 04:02:02,800
the sipping cost
6098
04:02:02,800 --> 04:02:04,479
the locations are a few states in the
6099
04:02:04,479 --> 04:02:06,960
united states
6100
04:02:06,960 --> 04:02:09,120
in the if statement project we will ask
6101
04:02:09,120 --> 04:02:10,560
the user to enter the amount of the
6102
04:02:10,560 --> 04:02:12,239
package or the item
6103
04:02:12,239 --> 04:02:14,160
and we'll also ask the user to input the
6104
04:02:14,160 --> 04:02:16,080
state name
6105
04:02:16,080 --> 04:02:17,840
so for the different states we'll define
6106
04:02:17,840 --> 04:02:20,000
the conditions using the total amount of
6107
04:02:20,000 --> 04:02:22,640
the items and print the shipping cost
6108
04:02:22,640 --> 04:02:24,560
so let me take you to my jupyter
6109
04:02:24,560 --> 04:02:26,000
notebook where we'll
6110
04:02:26,000 --> 04:02:27,840
do this project
6111
04:02:27,840 --> 04:02:30,479
okay so i am on my jupyter notebook let
6112
04:02:30,479 --> 04:02:32,479
me just rename this
6113
04:02:32,479 --> 04:02:35,040
notebook i'll say if
6114
04:02:35,040 --> 04:02:37,680
statement
6115
04:02:37,680 --> 04:02:39,199
and
6116
04:02:39,199 --> 04:02:41,359
project
6117
04:02:41,359 --> 04:02:43,680
all right
6118
04:02:43,680 --> 04:02:45,120
so
6119
04:02:45,120 --> 04:02:47,199
let me go ahead and
6120
04:02:47,199 --> 04:02:50,560
give a comment i'll say
6121
04:02:50,560 --> 04:02:53,040
package
6122
04:02:54,840 --> 04:02:58,160
delivery cost
6123
04:02:58,160 --> 04:03:00,239
based on
6124
04:03:00,239 --> 04:03:01,680
the location
6125
04:03:01,680 --> 04:03:02,800
and
6126
04:03:02,800 --> 04:03:05,439
total amount
6127
04:03:05,439 --> 04:03:06,720
all right
6128
04:03:06,720 --> 04:03:09,600
first and foremost i'll go ahead and
6129
04:03:09,600 --> 04:03:12,399
create a variable called total
6130
04:03:12,399 --> 04:03:14,720
now this will be
6131
04:03:14,720 --> 04:03:17,520
a user input so i'm using the input
6132
04:03:17,520 --> 04:03:19,920
function
6133
04:03:20,479 --> 04:03:24,800
and here i'll pass the message as enter
6134
04:03:24,800 --> 04:03:26,880
the
6135
04:03:26,880 --> 04:03:29,359
amount
6136
04:03:30,399 --> 04:03:31,520
for
6137
04:03:31,520 --> 04:03:34,239
the item
6138
04:03:37,199 --> 04:03:39,840
all right
6139
04:03:39,840 --> 04:03:41,439
next
6140
04:03:41,439 --> 04:03:44,160
i'll assign
6141
04:03:45,120 --> 04:03:46,319
another
6142
04:03:46,319 --> 04:03:47,520
variable
6143
04:03:47,520 --> 04:03:49,600
called state this is also going to be a
6144
04:03:49,600 --> 04:03:51,199
user input so i'm using the input
6145
04:03:51,199 --> 04:03:53,920
function and i will say
6146
04:03:53,920 --> 04:03:55,359
enter the
6147
04:03:55,359 --> 04:03:57,040
state name
6148
04:03:57,040 --> 04:03:57,920
in
6149
04:03:57,920 --> 04:04:00,239
usa
6150
04:04:00,239 --> 04:04:02,720
so these are the two
6151
04:04:02,720 --> 04:04:04,720
inputs we are asking from the user that
6152
04:04:04,720 --> 04:04:06,800
is to pass the
6153
04:04:06,800 --> 04:04:09,600
amount for the item and the state in the
6154
04:04:09,600 --> 04:04:11,760
usa
6155
04:04:11,760 --> 04:04:12,800
all right
6156
04:04:12,800 --> 04:04:14,640
now we'll start with our first if
6157
04:04:14,640 --> 04:04:16,399
statement i'll write if
6158
04:04:16,399 --> 04:04:17,840
state
6159
04:04:17,840 --> 04:04:20,319
equaled equal to let's say my first
6160
04:04:20,319 --> 04:04:23,840
state name is texas
6161
04:04:24,399 --> 04:04:26,319
give a colon
6162
04:04:26,319 --> 04:04:28,000
make sure the syntax you are writing is
6163
04:04:28,000 --> 04:04:29,760
correct
6164
04:04:29,760 --> 04:04:32,080
now i am going to use
6165
04:04:32,080 --> 04:04:34,560
another if statement so this becomes a
6166
04:04:34,560 --> 04:04:36,479
nested if statement now
6167
04:04:36,479 --> 04:04:38,239
i'll write if
6168
04:04:38,239 --> 04:04:41,760
total which is my
6169
04:04:41,840 --> 04:04:44,239
item amount so i'm checking if total
6170
04:04:44,239 --> 04:04:45,439
amount
6171
04:04:45,439 --> 04:04:46,399
is
6172
04:04:46,399 --> 04:04:48,880
less than or equal to
6173
04:04:48,880 --> 04:04:50,640
150
6174
04:04:50,640 --> 04:04:51,600
then
6175
04:04:51,600 --> 04:04:53,600
i'm going to print
6176
04:04:53,600 --> 04:04:56,399
shipping cost
6177
04:04:58,479 --> 04:05:00,640
in
6178
04:05:00,640 --> 04:05:02,880
texas
6179
04:05:02,880 --> 04:05:04,080
is
6180
04:05:04,080 --> 04:05:05,439
let's say
6181
04:05:05,439 --> 04:05:08,160
30 dollars
6182
04:05:08,239 --> 04:05:10,399
all right
6183
04:05:10,399 --> 04:05:14,319
now i'll pass my next condition using
6184
04:05:14,319 --> 04:05:16,960
elif statement
6185
04:05:16,960 --> 04:05:19,920
since i am using multiple conditions so
6186
04:05:19,920 --> 04:05:20,800
hence
6187
04:05:20,800 --> 04:05:22,399
i am going to use
6188
04:05:22,399 --> 04:05:24,640
the alif statement this time i will
6189
04:05:24,640 --> 04:05:27,520
write f total
6190
04:05:28,479 --> 04:05:32,800
is greater than 150 and
6191
04:05:32,800 --> 04:05:34,960
if my total amount is
6192
04:05:34,960 --> 04:05:38,560
less than or equal to 250 dollars
6193
04:05:38,560 --> 04:05:41,199
then i'll say
6194
04:05:42,840 --> 04:05:45,120
print made a spelling mistake this
6195
04:05:45,120 --> 04:05:47,520
should be shipping
6196
04:05:47,520 --> 04:05:50,399
i'll say print
6197
04:05:51,680 --> 04:05:52,800
shipping
6198
04:05:52,800 --> 04:05:55,199
cost
6199
04:05:55,600 --> 04:05:57,199
in
6200
04:05:57,199 --> 04:05:58,640
taxes
6201
04:05:58,640 --> 04:05:59,680
is
6202
04:05:59,680 --> 04:06:00,720
let's say
6203
04:06:00,720 --> 04:06:03,040
20
6204
04:06:03,439 --> 04:06:05,680
so usually if your
6205
04:06:05,680 --> 04:06:08,640
amount of the item is high then the
6206
04:06:08,640 --> 04:06:10,880
sipping cost is low
6207
04:06:10,880 --> 04:06:14,080
so here you can see if the total amount
6208
04:06:14,080 --> 04:06:16,399
for the package or the item is less than
6209
04:06:16,399 --> 04:06:18,800
150 dollars then your shipping cost is
6210
04:06:18,800 --> 04:06:20,000
30
6211
04:06:20,000 --> 04:06:23,920
but if it is between 150 and 250 your
6212
04:06:23,920 --> 04:06:26,000
shipping cost is less which is 20
6213
04:06:26,000 --> 04:06:28,319
dollars
6214
04:06:28,319 --> 04:06:31,760
then i'll say else
6215
04:06:31,920 --> 04:06:34,319
print
6216
04:06:34,399 --> 04:06:35,520
shipping
6217
04:06:35,520 --> 04:06:37,920
cost
6218
04:06:38,239 --> 04:06:40,080
in
6219
04:06:40,080 --> 04:06:41,680
texas
6220
04:06:41,680 --> 04:06:42,960
is
6221
04:06:42,960 --> 04:06:45,600
five dollars
6222
04:06:45,920 --> 04:06:49,680
so what basically this means is
6223
04:06:50,399 --> 04:06:52,560
if your total is less than or equal to
6224
04:06:52,560 --> 04:06:55,120
150 dollars then
6225
04:06:55,120 --> 04:06:57,120
the shipping cost would be
6226
04:06:57,120 --> 04:06:58,720
30 dollars
6227
04:06:58,720 --> 04:07:01,359
if your total amount lies between 150 to
6228
04:07:01,359 --> 04:07:05,439
250 dollars your shipping cost is 20
6229
04:07:05,439 --> 04:07:07,279
and if the
6230
04:07:07,279 --> 04:07:09,840
cost of the item or the package is more
6231
04:07:09,840 --> 04:07:11,359
than
6232
04:07:11,359 --> 04:07:13,120
250 dollars
6233
04:07:13,120 --> 04:07:14,399
the cost is
6234
04:07:14,399 --> 04:07:16,880
five dollars
6235
04:07:16,880 --> 04:07:20,479
so this is for texas state
6236
04:07:20,479 --> 04:07:23,040
now moving ahead
6237
04:07:23,040 --> 04:07:25,840
i'll write alif
6238
04:07:25,840 --> 04:07:28,000
this time
6239
04:07:28,000 --> 04:07:29,920
we'll check the condition for state
6240
04:07:29,920 --> 04:07:33,319
let's say florida
6241
04:07:36,560 --> 04:07:37,680
i'll
6242
04:07:37,680 --> 04:07:41,359
use a colon here and say if
6243
04:07:41,359 --> 04:07:42,159
my
6244
04:07:42,159 --> 04:07:46,880
total is less than equal to 150 dollars
6245
04:07:46,880 --> 04:07:51,080
then we'll print
6246
04:07:51,120 --> 04:07:53,600
shipping
6247
04:07:54,000 --> 04:07:55,040
cost
6248
04:07:55,040 --> 04:07:56,800
in
6249
04:07:56,800 --> 04:07:58,560
florida
6250
04:07:58,560 --> 04:08:00,479
is
6251
04:08:00,479 --> 04:08:04,000
let's say 45 dollars
6252
04:08:04,000 --> 04:08:07,600
i'll use the ellipse statement again
6253
04:08:07,600 --> 04:08:10,239
i'll say lf
6254
04:08:10,239 --> 04:08:12,640
total
6255
04:08:13,199 --> 04:08:14,319
if it is
6256
04:08:14,319 --> 04:08:17,680
more than 150 dollars and
6257
04:08:17,680 --> 04:08:19,760
if it is
6258
04:08:19,760 --> 04:08:23,520
less than or equal to 250 dollars
6259
04:08:23,520 --> 04:08:24,800
i'll write
6260
04:08:24,800 --> 04:08:27,040
print
6261
04:08:27,040 --> 04:08:28,640
shipping
6262
04:08:28,640 --> 04:08:30,080
cost
6263
04:08:30,080 --> 04:08:31,439
in
6264
04:08:31,439 --> 04:08:32,800
florida
6265
04:08:32,800 --> 04:08:34,159
is
6266
04:08:34,159 --> 04:08:36,800
let's say 35
6267
04:08:36,800 --> 04:08:38,080
next
6268
04:08:38,080 --> 04:08:39,520
i'll
6269
04:08:39,520 --> 04:08:42,640
write my else statement i'll say
6270
04:08:42,640 --> 04:08:45,640
print
6271
04:08:46,159 --> 04:08:47,680
shipping
6272
04:08:47,680 --> 04:08:50,159
cost
6273
04:08:50,159 --> 04:08:51,600
in
6274
04:08:51,600 --> 04:08:52,880
florida
6275
04:08:52,880 --> 04:08:54,399
is
6276
04:08:54,399 --> 04:08:56,479
let's say 10 dollars
6277
04:08:56,479 --> 04:08:59,199
if the cost of the item is more than 250
6278
04:08:59,199 --> 04:09:01,680
dollars now one thing to note here is
6279
04:09:01,680 --> 04:09:04,159
the shipping cost is different for the
6280
04:09:04,159 --> 04:09:07,439
different states in the u.s
6281
04:09:07,600 --> 04:09:10,000
all right
6282
04:09:11,520 --> 04:09:14,560
now we'll move on to our
6283
04:09:14,560 --> 04:09:18,000
next state let me
6284
04:09:18,080 --> 04:09:20,239
give a space here
6285
04:09:20,239 --> 04:09:23,120
i'll say lf
6286
04:09:24,319 --> 04:09:27,520
state equal to equal to let's see we'll
6287
04:09:27,520 --> 04:09:28,560
take
6288
04:09:28,560 --> 04:09:31,520
virginia now
6289
04:09:33,199 --> 04:09:35,840
i'll see if total
6290
04:09:35,840 --> 04:09:37,199
is
6291
04:09:37,199 --> 04:09:40,000
less than or equal to 150
6292
04:09:40,000 --> 04:09:41,680
dollars
6293
04:09:41,680 --> 04:09:44,479
will print
6294
04:09:44,960 --> 04:09:47,359
shipping
6295
04:09:47,359 --> 04:09:49,520
cost
6296
04:09:49,520 --> 04:09:51,680
in
6297
04:09:51,760 --> 04:09:53,760
virginia
6298
04:09:53,760 --> 04:09:55,359
is
6299
04:09:55,359 --> 04:09:58,720
let's say twenty three dollars
6300
04:09:59,520 --> 04:10:01,199
all right
6301
04:10:01,199 --> 04:10:03,600
just scroll down
6302
04:10:03,600 --> 04:10:05,840
next i'll write my ellip
6303
04:10:05,840 --> 04:10:09,359
statement i'll say lf
6304
04:10:09,359 --> 04:10:10,720
total
6305
04:10:10,720 --> 04:10:12,960
is
6306
04:10:13,920 --> 04:10:18,000
greater than 150 dollars and
6307
04:10:18,000 --> 04:10:19,120
total
6308
04:10:19,120 --> 04:10:20,159
is
6309
04:10:20,159 --> 04:10:23,279
less than equal to 250 dollars
6310
04:10:23,279 --> 04:10:24,960
then
6311
04:10:24,960 --> 04:10:26,720
we'll
6312
04:10:26,720 --> 04:10:28,880
print
6313
04:10:28,880 --> 04:10:32,000
just copy this message
6314
04:10:33,120 --> 04:10:35,359
paste it here
6315
04:10:35,359 --> 04:10:37,439
and we'll see shipping cost in virginia
6316
04:10:37,439 --> 04:10:38,640
is
6317
04:10:38,640 --> 04:10:41,040
15
6318
04:10:44,239 --> 04:10:45,840
next
6319
04:10:45,840 --> 04:10:47,840
we'll write our
6320
04:10:47,840 --> 04:10:51,520
else statement else
6321
04:10:51,520 --> 04:10:53,840
print
6322
04:10:53,840 --> 04:10:55,920
i'll paste the message again i'll say
6323
04:10:55,920 --> 04:10:58,720
shipping cost in virginia is
6324
04:10:58,720 --> 04:11:00,720
five dollars
6325
04:11:00,720 --> 04:11:02,640
in case the cost for the
6326
04:11:02,640 --> 04:11:04,239
items is more than two hundred and fifty
6327
04:11:04,239 --> 04:11:06,159
dollars
6328
04:11:06,159 --> 04:11:09,279
now moving to our final state
6329
04:11:09,279 --> 04:11:12,239
i'll write another alif statement i'll
6330
04:11:12,239 --> 04:11:15,279
say alif state equal to
6331
04:11:15,279 --> 04:11:19,399
equal to let's say new jersey
6332
04:11:22,720 --> 04:11:24,960
if
6333
04:11:26,880 --> 04:11:28,560
total
6334
04:11:28,560 --> 04:11:32,720
is less than equal to 150 dollars
6335
04:11:32,720 --> 04:11:33,600
then
6336
04:11:33,600 --> 04:11:35,840
we'll print
6337
04:11:35,840 --> 04:11:37,920
just paste the message here i'll say
6338
04:11:37,920 --> 04:11:40,560
shipping cost in
6339
04:11:40,560 --> 04:11:43,279
new jersey
6340
04:11:43,279 --> 04:11:45,359
is
6341
04:11:45,359 --> 04:11:49,319
let's say 40 dollars
6342
04:11:51,600 --> 04:11:54,399
now i'll say lf
6343
04:11:54,399 --> 04:11:56,399
total
6344
04:11:56,399 --> 04:11:59,359
if it is greater than 150 dollars and if
6345
04:11:59,359 --> 04:12:03,680
total is less than equal to 250 dollars
6346
04:12:03,680 --> 04:12:06,319
will print
6347
04:12:06,720 --> 04:12:10,960
just put this within double quotes
6348
04:12:10,960 --> 04:12:12,560
and
6349
04:12:12,560 --> 04:12:15,439
i'll say shipping cost in
6350
04:12:15,439 --> 04:12:16,800
new jersey
6351
04:12:16,800 --> 04:12:18,720
is
6352
04:12:18,720 --> 04:12:20,960
let's say
6353
04:12:20,960 --> 04:12:23,600
ten dollars
6354
04:12:26,640 --> 04:12:30,120
i'll say else
6355
04:12:35,120 --> 04:12:38,159
my shipping cost
6356
04:12:39,120 --> 04:12:40,720
is
6357
04:12:40,720 --> 04:12:42,000
free
6358
04:12:42,000 --> 04:12:43,279
in case
6359
04:12:43,279 --> 04:12:44,800
the
6360
04:12:44,800 --> 04:12:47,359
total amount for the items is
6361
04:12:47,359 --> 04:12:50,560
more than 250 dollars
6362
04:12:50,560 --> 04:12:51,680
all right
6363
04:12:51,680 --> 04:12:52,800
finally
6364
04:12:52,800 --> 04:12:55,600
we'll have our
6365
04:12:56,399 --> 04:12:59,920
else statement where i'm going to print
6366
04:12:59,920 --> 04:13:02,479
invalid
6367
04:13:03,199 --> 04:13:04,560
state
6368
04:13:04,560 --> 04:13:05,439
name
6369
04:13:05,439 --> 04:13:08,159
in case the user enters any other state
6370
04:13:08,159 --> 04:13:10,640
apart from new jersey virginia florida
6371
04:13:10,640 --> 04:13:12,399
and texas
6372
04:13:12,399 --> 04:13:13,600
all right
6373
04:13:13,600 --> 04:13:15,040
so
6374
04:13:15,040 --> 04:13:17,120
we have our small project ready let me
6375
04:13:17,120 --> 04:13:18,159
just
6376
04:13:18,159 --> 04:13:20,479
run through it
6377
04:13:20,479 --> 04:13:22,399
so first we are declaring two variables
6378
04:13:22,399 --> 04:13:25,199
total and state and we are taking this
6379
04:13:25,199 --> 04:13:27,279
as user input so the user is going to
6380
04:13:27,279 --> 04:13:28,880
input the
6381
04:13:28,880 --> 04:13:30,720
amount for the item or the amount for
6382
04:13:30,720 --> 04:13:33,120
the package and then
6383
04:13:33,120 --> 04:13:36,159
the user will input the state
6384
04:13:36,159 --> 04:13:39,199
so we have the four states defined here
6385
04:13:39,199 --> 04:13:40,880
and we're checking the
6386
04:13:40,880 --> 04:13:42,399
shipping cost
6387
04:13:42,399 --> 04:13:44,239
for these four states
6388
04:13:44,239 --> 04:13:46,080
based on
6389
04:13:46,080 --> 04:13:47,040
the
6390
04:13:47,040 --> 04:13:49,680
amount for the items
6391
04:13:49,680 --> 04:13:50,720
all right
6392
04:13:50,720 --> 04:13:54,080
now let me go ahead and run it
6393
04:13:54,080 --> 04:13:56,239
so here you can see we have our first
6394
04:13:56,239 --> 04:13:59,040
user input enter the amount for the item
6395
04:13:59,040 --> 04:14:00,159
let's see
6396
04:14:00,159 --> 04:14:02,760
i have my amount as
6397
04:14:02,760 --> 04:14:04,399
125
6398
04:14:04,399 --> 04:14:06,800
dollars
6399
04:14:06,840 --> 04:14:10,399
and the next input is enter the state
6400
04:14:10,399 --> 04:14:13,040
name in usa i'll say
6401
04:14:13,040 --> 04:14:16,000
let's say florida
6402
04:14:16,479 --> 04:14:17,840
if i run this
6403
04:14:17,840 --> 04:14:19,920
it gives me the shipping cost in florida
6404
04:14:19,920 --> 04:14:23,199
is 45 dollars let's go to the top and
6405
04:14:23,199 --> 04:14:25,279
see what we have
6406
04:14:25,279 --> 04:14:28,439
now our total was
6407
04:14:28,439 --> 04:14:31,040
125 dollars which is
6408
04:14:31,040 --> 04:14:34,080
less than 150 and hence
6409
04:14:34,080 --> 04:14:36,239
it has printed shipping cost in florida
6410
04:14:36,239 --> 04:14:38,720
is 45 dollars
6411
04:14:38,720 --> 04:14:39,680
let's
6412
04:14:39,680 --> 04:14:41,199
run this again
6413
04:14:41,199 --> 04:14:43,520
this time i'll say
6414
04:14:43,520 --> 04:14:46,239
let's see my
6415
04:14:48,720 --> 04:14:50,199
item costs
6416
04:14:50,199 --> 04:14:53,120
255 dollars
6417
04:14:53,120 --> 04:14:57,120
in the state let's say new jersey
6418
04:14:59,680 --> 04:15:01,199
let's run it
6419
04:15:01,199 --> 04:15:03,600
you see here it gives us the message
6420
04:15:03,600 --> 04:15:05,520
shipping cost is free now the reason
6421
04:15:05,520 --> 04:15:07,359
being if you see
6422
04:15:07,359 --> 04:15:08,960
this part of the code
6423
04:15:08,960 --> 04:15:11,120
for state new jersey
6424
04:15:11,120 --> 04:15:12,560
if the
6425
04:15:12,560 --> 04:15:13,920
total
6426
04:15:13,920 --> 04:15:16,560
amount for the items is
6427
04:15:16,560 --> 04:15:19,199
more than 250 dollars
6428
04:15:19,199 --> 04:15:22,720
the shipping cost is free
6429
04:15:22,720 --> 04:15:25,439
now let's try for another state
6430
04:15:25,439 --> 04:15:28,080
this time we'll see for virginia
6431
04:15:28,080 --> 04:15:30,159
let's say my
6432
04:15:30,159 --> 04:15:34,920
amount is 190
6433
04:15:35,199 --> 04:15:36,880
and my state is
6434
04:15:36,880 --> 04:15:38,560
virginia
6435
04:15:38,560 --> 04:15:40,640
if i run it it gives me the shipping
6436
04:15:40,640 --> 04:15:43,040
cost in virginia is 15
6437
04:15:43,040 --> 04:15:45,359
let's check our code
6438
04:15:45,359 --> 04:15:48,399
so here i have entered my
6439
04:15:48,399 --> 04:15:51,439
item amount to be 190 dollars so it lies
6440
04:15:51,439 --> 04:15:55,199
between 150 and 250 dollars and hence it
6441
04:15:55,199 --> 04:15:56,640
has printed the message that shipping
6442
04:15:56,640 --> 04:16:00,239
cost in virginia is fifteen dollars
6443
04:16:00,239 --> 04:16:03,040
now let me enter another
6444
04:16:03,040 --> 04:16:07,040
state name that we have not defined
6445
04:16:07,040 --> 04:16:10,319
in our code let's say i have
6446
04:16:10,319 --> 04:16:12,720
my item amount as
6447
04:16:12,720 --> 04:16:14,880
290 dollars
6448
04:16:14,880 --> 04:16:15,840
and
6449
04:16:15,840 --> 04:16:17,439
the state name
6450
04:16:17,439 --> 04:16:21,600
i'll give us let's say colorado
6451
04:16:21,600 --> 04:16:24,399
if i run it it gives us the message as
6452
04:16:24,399 --> 04:16:28,080
invalid state which is present in the
6453
04:16:28,080 --> 04:16:30,720
last part of our code so
6454
04:16:30,720 --> 04:16:33,040
since we haven't defined any
6455
04:16:33,040 --> 04:16:36,080
shipping cost for colorado it has given
6456
04:16:36,080 --> 04:16:38,080
us the message that the state i am
6457
04:16:38,080 --> 04:16:40,880
trying to search for is invalid
6458
04:16:40,880 --> 04:16:43,040
you can check for
6459
04:16:43,040 --> 04:16:46,239
another state or any other country name
6460
04:16:46,239 --> 04:16:48,319
anything that is not
6461
04:16:48,319 --> 04:16:49,920
part of our
6462
04:16:49,920 --> 04:16:51,120
project
6463
04:16:51,120 --> 04:16:53,120
let's say i'll enter my amount as
6464
04:16:53,120 --> 04:16:54,960
hundred dollars
6465
04:16:54,960 --> 04:16:57,359
and this time i'll enter my state name
6466
04:16:57,359 --> 04:16:58,640
as
6467
04:16:58,640 --> 04:17:01,120
let's say ohio
6468
04:17:01,120 --> 04:17:03,600
and we'll run it it again gives us
6469
04:17:03,600 --> 04:17:05,840
invalid state
6470
04:17:05,840 --> 04:17:10,000
let's try out one last time i have my
6471
04:17:10,000 --> 04:17:13,199
package amount as 350 dollars
6472
04:17:13,199 --> 04:17:14,479
and let's see
6473
04:17:14,479 --> 04:17:16,319
my state is
6474
04:17:16,319 --> 04:17:18,640
alaska
6475
04:17:18,640 --> 04:17:20,880
again it says invalid state name because
6476
04:17:20,880 --> 04:17:22,840
we haven't defined
6477
04:17:22,840 --> 04:17:25,359
any condition for
6478
04:17:25,359 --> 04:17:26,800
alaska state
6479
04:17:26,800 --> 04:17:28,479
so this was a very simple project that
6480
04:17:28,479 --> 04:17:29,760
we did using
6481
04:17:29,760 --> 04:17:33,120
arif statement we saw how to write
6482
04:17:33,120 --> 04:17:37,199
nested if and we saw how to use the alif
6483
04:17:37,199 --> 04:17:38,560
and the
6484
04:17:38,560 --> 04:17:40,000
else statement
6485
04:17:40,000 --> 04:17:41,680
we will start by learning the basics of
6486
04:17:41,680 --> 04:17:44,479
loops and look at for loops in python
6487
04:17:44,479 --> 04:17:46,319
then we will understand about while
6488
04:17:46,319 --> 04:17:48,640
loops in python with its syntax and
6489
04:17:48,640 --> 04:17:50,239
coding examples
6490
04:17:50,239 --> 04:17:52,399
finally we will see how to write nested
6491
04:17:52,399 --> 04:17:53,680
loops in python with hands-on
6492
04:17:53,680 --> 04:17:55,680
demonstrations on the jupyter notebook
6493
04:17:55,680 --> 04:17:58,319
so let's get started
6494
04:17:58,319 --> 04:18:01,680
so what is a for loop in python
6495
04:18:01,680 --> 04:18:03,680
a for loop in python is used to iterate
6496
04:18:03,680 --> 04:18:05,840
over the items or elements present in a
6497
04:18:05,840 --> 04:18:07,040
sequence
6498
04:18:07,040 --> 04:18:08,880
the items present in a sequence can be
6499
04:18:08,880 --> 04:18:11,279
in the form of a list tuple dictionary
6500
04:18:11,279 --> 04:18:13,120
or even a string
6501
04:18:13,120 --> 04:18:15,040
a for loop in python uses the range
6502
04:18:15,040 --> 04:18:16,640
function to iterate over a specific
6503
04:18:16,640 --> 04:18:19,199
number of times
6504
04:18:19,199 --> 04:18:20,880
now let's have a look at the syntax of
6505
04:18:20,880 --> 04:18:22,080
for loop
6506
04:18:22,080 --> 04:18:24,239
so here on the left you can see the
6507
04:18:24,239 --> 04:18:26,960
syntax of the for loop so we have the
6508
04:18:26,960 --> 04:18:28,000
for
6509
04:18:28,000 --> 04:18:29,920
keyword followed by the iterator
6510
04:18:29,920 --> 04:18:32,319
variable then we have the in keyword
6511
04:18:32,319 --> 04:18:33,920
followed by the sequence it can be a
6512
04:18:33,920 --> 04:18:35,520
list you can use the range function it
6513
04:18:35,520 --> 04:18:38,000
can be a dictionary or tuple anything
6514
04:18:38,000 --> 04:18:39,920
and after that you have your colon
6515
04:18:39,920 --> 04:18:41,120
followed by
6516
04:18:41,120 --> 04:18:43,439
the statements that you want to execute
6517
04:18:43,439 --> 04:18:45,920
in the for loop
6518
04:18:45,920 --> 04:18:49,120
and below you have the rest of the code
6519
04:18:49,120 --> 04:18:51,600
and here on the right you can see
6520
04:18:51,600 --> 04:18:52,880
this is the
6521
04:18:52,880 --> 04:18:55,600
flowchart of a for loop so you have
6522
04:18:55,600 --> 04:18:58,560
for each item in sequence then we check
6523
04:18:58,560 --> 04:19:01,840
the condition if you have reached the
6524
04:19:01,840 --> 04:19:03,359
last item
6525
04:19:03,359 --> 04:19:05,920
if that is true you exit out of the loop
6526
04:19:05,920 --> 04:19:08,399
but if it is false you keep on
6527
04:19:08,399 --> 04:19:10,000
running or executing the statements that
6528
04:19:10,000 --> 04:19:12,560
are present inside the for loop this
6529
04:19:12,560 --> 04:19:14,159
will continue until and unless you have
6530
04:19:14,159 --> 04:19:16,960
reached the last item in your sequence
6531
04:19:16,960 --> 04:19:18,800
okay
6532
04:19:18,800 --> 04:19:20,720
now let's head over to our jupyter
6533
04:19:20,720 --> 04:19:22,560
notebook where we'll
6534
04:19:22,560 --> 04:19:25,359
see for loop in detail
6535
04:19:25,359 --> 04:19:29,199
and we'll explore a lot of code examples
6536
04:19:29,199 --> 04:19:31,760
okay so here i am on my jupiter notebook
6537
04:19:31,760 --> 04:19:34,159
let's start by writing a simple for loop
6538
04:19:34,159 --> 04:19:36,399
suppose i have a variable
6539
04:19:36,399 --> 04:19:37,600
say s
6540
04:19:37,600 --> 04:19:39,840
and it has a value which is a string
6541
04:19:39,840 --> 04:19:42,880
let's say simply learn
6542
04:19:43,439 --> 04:19:44,720
okay
6543
04:19:44,720 --> 04:19:46,560
now
6544
04:19:46,560 --> 04:19:49,120
the way to write a for loop is i'll say
6545
04:19:49,120 --> 04:19:50,479
for
6546
04:19:50,479 --> 04:19:52,800
i which is my iterator variable it can
6547
04:19:52,800 --> 04:19:56,000
be any valid variable name in python
6548
04:19:56,000 --> 04:19:57,760
in
6549
04:19:57,760 --> 04:20:01,199
my variable that is s
6550
04:20:01,439 --> 04:20:04,880
so here s becomes my sequence
6551
04:20:04,880 --> 04:20:07,359
i'll say print
6552
04:20:07,359 --> 04:20:08,640
i
6553
04:20:08,640 --> 04:20:10,479
so what this for loop is going to do is
6554
04:20:10,479 --> 04:20:13,279
it will iterate over all the characters
6555
04:20:13,279 --> 04:20:14,960
that are present in my string and print
6556
04:20:14,960 --> 04:20:16,800
them one by one
6557
04:20:16,800 --> 04:20:19,439
let me run it by hitting shift enter
6558
04:20:19,439 --> 04:20:22,000
you see here using this for loop we have
6559
04:20:22,000 --> 04:20:23,840
i traded over
6560
04:20:23,840 --> 04:20:27,359
all the letters that were present in my
6561
04:20:27,359 --> 04:20:29,199
string that is simply learn and we have
6562
04:20:29,199 --> 04:20:31,199
printed them one by one
6563
04:20:31,199 --> 04:20:33,040
okay
6564
04:20:33,040 --> 04:20:34,000
now
6565
04:20:34,000 --> 04:20:35,840
the print function in python provides a
6566
04:20:35,840 --> 04:20:39,359
parameter called end so by default
6567
04:20:39,359 --> 04:20:41,760
the value of the parameter is
6568
04:20:41,760 --> 04:20:43,920
backslash n that is the newline
6569
04:20:43,920 --> 04:20:45,760
character now you can end a print
6570
04:20:45,760 --> 04:20:47,439
statement with any character or string
6571
04:20:47,439 --> 04:20:49,600
using this parameter which is end
6572
04:20:49,600 --> 04:20:52,239
let me show you what i
6573
04:20:52,239 --> 04:20:54,399
meant so i'll again
6574
04:20:54,399 --> 04:20:58,000
have my same string simply loan
6575
04:20:58,000 --> 04:21:01,840
i'll say for i in
6576
04:21:02,159 --> 04:21:04,000
my variable
6577
04:21:04,000 --> 04:21:06,000
or my sequence s
6578
04:21:06,000 --> 04:21:09,359
i'll say print i
6579
04:21:09,359 --> 04:21:10,479
comma
6580
04:21:10,479 --> 04:21:13,760
then i'll use my end parameter equal to
6581
04:21:13,760 --> 04:21:16,000
and
6582
04:21:16,159 --> 04:21:19,040
my end parameter will have a value of
6583
04:21:19,040 --> 04:21:20,560
asterisk
6584
04:21:20,560 --> 04:21:23,840
now let me run this you can see here
6585
04:21:23,840 --> 04:21:25,520
this is sort of a pattern that we have
6586
04:21:25,520 --> 04:21:26,640
printed
6587
04:21:26,640 --> 04:21:30,000
so between each letter in my
6588
04:21:30,000 --> 04:21:33,199
string simply learn we have an asterisk
6589
04:21:33,199 --> 04:21:36,199
okay
6590
04:21:37,040 --> 04:21:39,760
moving ahead
6591
04:21:41,840 --> 04:21:44,239
let's see how to
6592
04:21:44,239 --> 04:21:45,120
use
6593
04:21:45,120 --> 04:21:47,120
for loop
6594
04:21:47,120 --> 04:21:50,439
in a list
6595
04:21:50,880 --> 04:21:52,720
all right
6596
04:21:52,720 --> 04:21:55,120
i'll declare a list called
6597
04:21:55,120 --> 04:21:56,800
programming
6598
04:21:56,800 --> 04:22:00,080
and this list will contain
6599
04:22:00,080 --> 04:22:02,159
a few programming languages let's say we
6600
04:22:02,159 --> 04:22:06,319
have java and then
6601
04:22:06,319 --> 04:22:08,080
the next item we have in the list is
6602
04:22:08,080 --> 04:22:10,080
python
6603
04:22:10,080 --> 04:22:13,520
next we have ruby and then
6604
04:22:13,520 --> 04:22:16,000
we'll have html
6605
04:22:16,000 --> 04:22:17,520
okay
6606
04:22:17,520 --> 04:22:18,640
so
6607
04:22:18,640 --> 04:22:21,199
i'm going to print
6608
04:22:21,199 --> 04:22:22,159
all the
6609
04:22:22,159 --> 04:22:23,840
individual items that are present in my
6610
04:22:23,840 --> 04:22:25,600
list using the for loop
6611
04:22:25,600 --> 04:22:27,680
so i'll use the same syntax i'll write
6612
04:22:27,680 --> 04:22:28,720
for
6613
04:22:28,720 --> 04:22:30,880
this time i'll use my iterator variable
6614
04:22:30,880 --> 04:22:34,720
name as iter which means iterator
6615
04:22:34,720 --> 04:22:38,080
for iterator or iter in
6616
04:22:38,080 --> 04:22:41,279
my sequence which is programming
6617
04:22:41,279 --> 04:22:43,760
i'll say print
6618
04:22:43,760 --> 04:22:46,319
iter
6619
04:22:46,319 --> 04:22:48,159
let's run it you see here we have
6620
04:22:48,159 --> 04:22:50,080
printed all the items in my list one by
6621
04:22:50,080 --> 04:22:50,880
one
6622
04:22:50,880 --> 04:22:53,760
java python ruby and html
6623
04:22:53,760 --> 04:22:55,199
all right
6624
04:22:55,199 --> 04:22:56,640
now let's see
6625
04:22:56,640 --> 04:22:58,720
an example how you can find the average
6626
04:22:58,720 --> 04:23:01,199
of a list of numbers using for loop so
6627
04:23:01,199 --> 04:23:03,040
i'll give a
6628
04:23:03,040 --> 04:23:06,239
comment saying find the
6629
04:23:06,239 --> 04:23:08,800
average of a
6630
04:23:08,800 --> 04:23:11,840
list of numbers
6631
04:23:13,199 --> 04:23:14,399
okay
6632
04:23:14,399 --> 04:23:16,000
so here
6633
04:23:16,000 --> 04:23:18,399
first of all we'll declare a
6634
04:23:18,399 --> 04:23:20,880
list of numbers with variable list
6635
04:23:20,880 --> 04:23:23,439
underscore num equal to
6636
04:23:23,439 --> 04:23:26,000
it will have a few numbers let's say 20
6637
04:23:26,000 --> 04:23:29,279
25 10 50 and
6638
04:23:29,279 --> 04:23:31,680
40.
6639
04:23:31,840 --> 04:23:34,960
let's make it 45
6640
04:23:35,040 --> 04:23:37,279
now to find the average of a list of
6641
04:23:37,279 --> 04:23:38,640
numbers
6642
04:23:38,640 --> 04:23:40,159
first i'll declare a
6643
04:23:40,159 --> 04:23:43,120
variable called sum that will have
6644
04:23:43,120 --> 04:23:45,279
a value of zero so we have initialized
6645
04:23:45,279 --> 04:23:47,439
the sum variable to zero
6646
04:23:47,439 --> 04:23:49,920
then i'll start my for loop for i
6647
04:23:49,920 --> 04:23:51,199
in
6648
04:23:51,199 --> 04:23:53,840
my sequence that is list underscore num
6649
04:23:53,840 --> 04:23:55,279
which is
6650
04:23:55,279 --> 04:23:58,319
this variable
6651
04:23:58,720 --> 04:24:01,600
give a colon i'll write sum
6652
04:24:01,600 --> 04:24:02,960
equal to
6653
04:24:02,960 --> 04:24:06,080
sum plus i
6654
04:24:06,560 --> 04:24:08,239
so here
6655
04:24:08,239 --> 04:24:09,520
just give a space to make it more
6656
04:24:09,520 --> 04:24:11,680
readable
6657
04:24:11,680 --> 04:24:13,120
so i'm adding
6658
04:24:13,120 --> 04:24:15,359
all the numbers in my list and we are
6659
04:24:15,359 --> 04:24:19,520
storing the result in the variable sum
6660
04:24:19,520 --> 04:24:21,520
then we'll say
6661
04:24:21,520 --> 04:24:24,520
print
6662
04:24:28,560 --> 04:24:29,920
sum
6663
04:24:29,920 --> 04:24:32,479
equal to
6664
04:24:33,439 --> 04:24:36,159
i'll pass in my variable that is sum
6665
04:24:36,159 --> 04:24:40,159
then i'll say print
6666
04:24:40,560 --> 04:24:43,120
average
6667
04:24:43,120 --> 04:24:45,680
equal to
6668
04:24:46,000 --> 04:24:48,479
now our average will be
6669
04:24:48,479 --> 04:24:51,680
my variable sum divided by
6670
04:24:51,680 --> 04:24:54,000
the length
6671
04:24:54,000 --> 04:24:55,600
of my
6672
04:24:55,600 --> 04:24:58,319
list that has all the numbers
6673
04:24:58,319 --> 04:25:01,279
let's run this you see here
6674
04:25:01,279 --> 04:25:03,120
so a total sum of all the numbers
6675
04:25:03,120 --> 04:25:06,720
present inside the list is 150 and the
6676
04:25:06,720 --> 04:25:08,479
average of
6677
04:25:08,479 --> 04:25:11,279
all these numbers is 30 so 150 divided
6678
04:25:11,279 --> 04:25:14,640
by 5 is 30
6679
04:25:14,640 --> 04:25:17,800
all right
6680
04:25:20,080 --> 04:25:22,399
let's see how you can use a for loop
6681
04:25:22,399 --> 04:25:25,279
using a tuple
6682
04:25:29,279 --> 04:25:33,600
so let's declare a variable called num
6683
04:25:33,680 --> 04:25:37,359
is going to a tuple of numbers
6684
04:25:37,359 --> 04:25:42,319
and we have 30 45 60 50 and let's say 70
6685
04:25:42,319 --> 04:25:45,359
as the numbers inside the tuple
6686
04:25:45,359 --> 04:25:48,399
i'll say sum equal to zero
6687
04:25:48,399 --> 04:25:51,760
then we'll have our for loop for i in
6688
04:25:51,760 --> 04:25:55,040
my sequence that is num
6689
04:25:55,040 --> 04:25:56,800
i'll say
6690
04:25:56,800 --> 04:25:58,239
sum equal to
6691
04:25:58,239 --> 04:25:59,760
sum plus i
6692
04:25:59,760 --> 04:26:02,080
so here we are just adding the values
6693
04:26:02,080 --> 04:26:04,560
that are present inside the tuple
6694
04:26:04,560 --> 04:26:08,000
i'll say print sum
6695
04:26:08,000 --> 04:26:11,359
so here you can see we have
6696
04:26:11,359 --> 04:26:13,920
printed
6697
04:26:13,920 --> 04:26:15,840
all the values present inside the tuple
6698
04:26:15,840 --> 04:26:18,319
one below the other now if i shift my
6699
04:26:18,319 --> 04:26:20,239
print statement to here
6700
04:26:20,239 --> 04:26:23,040
you can see here this is my
6701
04:26:23,040 --> 04:26:24,880
sum of all the numbers that are present
6702
04:26:24,880 --> 04:26:26,720
inside the tuple
6703
04:26:26,720 --> 04:26:28,399
so make sure you have
6704
04:26:28,399 --> 04:26:29,840
the current
6705
04:26:29,840 --> 04:26:32,479
indentation
6706
04:26:33,120 --> 04:26:35,279
now let's see how you can use the range
6707
04:26:35,279 --> 04:26:38,239
function in python
6708
04:26:39,920 --> 04:26:41,359
okay
6709
04:26:41,359 --> 04:26:43,199
so you can use the range function to
6710
04:26:43,199 --> 04:26:46,080
iterate over a range of values
6711
04:26:46,080 --> 04:26:47,600
or numbers
6712
04:26:47,600 --> 04:26:49,439
using a for loop
6713
04:26:49,439 --> 04:26:52,800
the way to do is i'll say for i in
6714
04:26:52,800 --> 04:26:54,800
my function range
6715
04:26:54,800 --> 04:26:57,680
i'll give my first index at 1 and my
6716
04:26:57,680 --> 04:26:59,359
second index at
6717
04:26:59,359 --> 04:27:01,520
or my end index at 10
6718
04:27:01,520 --> 04:27:04,319
so this will go from 1 till
6719
04:27:04,319 --> 04:27:08,159
9 and exclude the 10th index
6720
04:27:08,159 --> 04:27:10,960
i'll say print i
6721
04:27:10,960 --> 04:27:12,880
you see here
6722
04:27:12,880 --> 04:27:15,040
for i equal to range 1 to 10 we have
6723
04:27:15,040 --> 04:27:17,760
printed all the numbers starting from 1
6724
04:27:17,760 --> 04:27:20,159
till 9. so
6725
04:27:20,159 --> 04:27:22,479
the last index that is 10 is
6726
04:27:22,479 --> 04:27:27,000
exclusive in the range function
6727
04:27:27,840 --> 04:27:29,439
now in the range function you can also
6728
04:27:29,439 --> 04:27:31,600
give a third parameter that stands for
6729
04:27:31,600 --> 04:27:32,640
step
6730
04:27:32,640 --> 04:27:34,880
so that you can skip a few steps and
6731
04:27:34,880 --> 04:27:37,120
print the next value let me show you
6732
04:27:37,120 --> 04:27:38,479
what i mean
6733
04:27:38,479 --> 04:27:40,960
say i write for i in
6734
04:27:40,960 --> 04:27:43,279
range
6735
04:27:44,640 --> 04:27:47,439
0 which is my starting index
6736
04:27:47,439 --> 04:27:50,000
10 which is my end index
6737
04:27:50,000 --> 04:27:53,120
and i'll have a step size of 2
6738
04:27:53,120 --> 04:27:54,399
give a colon
6739
04:27:54,399 --> 04:27:56,720
i'll say
6740
04:27:56,720 --> 04:27:59,120
print
6741
04:28:00,159 --> 04:28:02,720
display
6742
04:28:03,680 --> 04:28:06,159
values
6743
04:28:06,159 --> 04:28:07,279
with
6744
04:28:07,279 --> 04:28:08,960
a
6745
04:28:08,960 --> 04:28:11,040
step
6746
04:28:11,040 --> 04:28:13,279
size of
6747
04:28:13,279 --> 04:28:15,600
2.
6748
04:28:17,199 --> 04:28:19,359
give a comma here and my variable that
6749
04:28:19,359 --> 04:28:21,600
is i let's print it
6750
04:28:21,600 --> 04:28:23,520
there you go
6751
04:28:23,520 --> 04:28:25,439
so we have printed
6752
04:28:25,439 --> 04:28:28,800
the values with a
6753
04:28:28,800 --> 04:28:32,000
gap of two so it starts from zero then
6754
04:28:32,000 --> 04:28:34,399
we are jumping two steps and then we are
6755
04:28:34,399 --> 04:28:36,640
printing two then again two steps we
6756
04:28:36,640 --> 04:28:39,120
have four six and eight so we are
6757
04:28:39,120 --> 04:28:40,239
skipping
6758
04:28:40,239 --> 04:28:41,680
one value
6759
04:28:41,680 --> 04:28:44,319
and printing the next value
6760
04:28:44,319 --> 04:28:46,239
in my sequence
6761
04:28:46,239 --> 04:28:48,800
ranging from zero to ten
6762
04:28:48,800 --> 04:28:50,399
all right
6763
04:28:50,399 --> 04:28:52,239
now
6764
04:28:52,239 --> 04:28:54,560
let's write a program to print
6765
04:28:54,560 --> 04:28:57,439
a table of a given number
6766
04:28:57,439 --> 04:29:00,560
so i'll give a comment as
6767
04:29:00,560 --> 04:29:03,040
program
6768
04:29:03,520 --> 04:29:04,960
to
6769
04:29:04,960 --> 04:29:06,560
print
6770
04:29:06,560 --> 04:29:08,319
table of a
6771
04:29:08,319 --> 04:29:09,359
given
6772
04:29:09,359 --> 04:29:10,720
number
6773
04:29:10,720 --> 04:29:12,000
all right
6774
04:29:12,000 --> 04:29:14,960
so i'll pass my
6775
04:29:15,920 --> 04:29:17,760
variable n
6776
04:29:17,760 --> 04:29:20,000
which is going to be a user input so i'm
6777
04:29:20,000 --> 04:29:22,080
using the in function and inside the in
6778
04:29:22,080 --> 04:29:23,760
function i'll use the input function to
6779
04:29:23,760 --> 04:29:25,600
take the user input
6780
04:29:25,600 --> 04:29:28,720
i'll give my message as enter the
6781
04:29:28,720 --> 04:29:31,720
number
6782
04:29:32,239 --> 04:29:33,600
i'll say for
6783
04:29:33,600 --> 04:29:35,120
i in
6784
04:29:35,120 --> 04:29:36,880
range
6785
04:29:36,880 --> 04:29:38,960
from 1 to
6786
04:29:38,960 --> 04:29:41,199
11
6787
04:29:41,199 --> 04:29:43,760
i'll say
6788
04:29:44,239 --> 04:29:48,399
multiply which is mul here
6789
04:29:48,399 --> 04:29:49,279
and
6790
04:29:49,279 --> 04:29:51,199
asterisk i
6791
04:29:51,199 --> 04:29:54,640
so basically what i'm doing here is
6792
04:29:54,640 --> 04:29:57,040
i'll take a user input of the number
6793
04:29:57,040 --> 04:29:58,960
let's say we want to print the table of
6794
04:29:58,960 --> 04:30:00,319
5
6795
04:30:00,319 --> 04:30:02,800
so from
6796
04:30:03,279 --> 04:30:04,159
1
6797
04:30:04,159 --> 04:30:06,720
till 10 will have the product of the
6798
04:30:06,720 --> 04:30:08,159
numbers
6799
04:30:08,159 --> 04:30:10,000
let me print it
6800
04:30:10,000 --> 04:30:11,840
i'll say print
6801
04:30:11,840 --> 04:30:13,120
n
6802
04:30:13,120 --> 04:30:14,880
comma
6803
04:30:14,880 --> 04:30:17,279
within double quotes i'll give my
6804
04:30:17,279 --> 04:30:19,359
asterisks
6805
04:30:19,359 --> 04:30:20,479
comma
6806
04:30:20,479 --> 04:30:22,479
i
6807
04:30:22,479 --> 04:30:24,800
comma
6808
04:30:24,880 --> 04:30:27,279
within double quotes i'll say equal to
6809
04:30:27,279 --> 04:30:30,880
and then we'll have our
6810
04:30:31,359 --> 04:30:33,359
variable mul
6811
04:30:33,359 --> 04:30:35,199
let's run this
6812
04:30:35,199 --> 04:30:37,520
enter the number i'll give 5 and hit
6813
04:30:37,520 --> 04:30:38,560
enter
6814
04:30:38,560 --> 04:30:39,760
there you go
6815
04:30:39,760 --> 04:30:41,760
so here we have the multiplication table
6816
04:30:41,760 --> 04:30:43,040
of
6817
04:30:43,040 --> 04:30:45,600
my number 5 so you see
6818
04:30:45,600 --> 04:30:50,560
5 into 1 which is 5 5 into 2 is 10
6819
04:30:50,560 --> 04:30:53,680
then we have 5 into 7 35 5 into 9 is 45
6820
04:30:53,680 --> 04:30:54,479
then
6821
04:30:54,479 --> 04:30:58,319
our last index which ends at 10 so we
6822
04:30:58,319 --> 04:30:59,760
have 5 into
6823
04:30:59,760 --> 04:31:02,000
10 that is 50
6824
04:31:02,000 --> 04:31:03,840
all right
6825
04:31:03,840 --> 04:31:05,279
this is how you can create a simple
6826
04:31:05,279 --> 04:31:07,120
multiplication table using for loop in
6827
04:31:07,120 --> 04:31:09,520
python
6828
04:31:09,520 --> 04:31:12,159
now we can also use the range function
6829
04:31:12,159 --> 04:31:14,399
with a sequence of items
6830
04:31:14,399 --> 04:31:16,479
the len function can be combined with
6831
04:31:16,479 --> 04:31:18,000
the range function to iterate through a
6832
04:31:18,000 --> 04:31:20,399
sequence using indexing
6833
04:31:20,399 --> 04:31:21,600
i'll show you
6834
04:31:21,600 --> 04:31:24,239
an example here let's say we have a list
6835
04:31:24,239 --> 04:31:26,479
called list 2
6836
04:31:26,479 --> 04:31:28,640
and here we have
6837
04:31:28,640 --> 04:31:30,479
a list of programming languages let's
6838
04:31:30,479 --> 04:31:31,439
say
6839
04:31:31,439 --> 04:31:34,080
c plus you can either use a single quote
6840
04:31:34,080 --> 04:31:36,640
or a double quote to represent the
6841
04:31:36,640 --> 04:31:37,600
text
6842
04:31:37,600 --> 04:31:39,439
values in a list
6843
04:31:39,439 --> 04:31:41,199
the next programming language i have is
6844
04:31:41,199 --> 04:31:43,600
java
6845
04:31:43,680 --> 04:31:47,040
then let's say i have python and we have
6846
04:31:47,040 --> 04:31:48,560
r
6847
04:31:48,560 --> 04:31:49,840
okay
6848
04:31:49,840 --> 04:31:53,040
so i can write for i in
6849
04:31:53,040 --> 04:31:54,319
range
6850
04:31:54,319 --> 04:31:57,040
here i can use the length function
6851
04:31:57,040 --> 04:31:58,720
and i'll say
6852
04:31:58,720 --> 04:31:59,600
list
6853
04:31:59,600 --> 04:32:01,439
2
6854
04:32:01,439 --> 04:32:03,120
give a colon
6855
04:32:03,120 --> 04:32:04,840
then we'll
6856
04:32:04,840 --> 04:32:08,080
print hello
6857
04:32:08,080 --> 04:32:09,279
and i'll say
6858
04:32:09,279 --> 04:32:12,880
list 2 of i
6859
04:32:14,479 --> 04:32:16,159
so what it does is
6860
04:32:16,159 --> 04:32:18,720
my iterator will iterate over
6861
04:32:18,720 --> 04:32:21,439
the list of programming languages i have
6862
04:32:21,439 --> 04:32:22,800
here
6863
04:32:22,800 --> 04:32:26,399
and it will keep on iterating
6864
04:32:26,399 --> 04:32:27,439
till
6865
04:32:27,439 --> 04:32:29,600
the length of the list
6866
04:32:29,600 --> 04:32:31,120
and then i am printing my message as
6867
04:32:31,120 --> 04:32:32,319
hello
6868
04:32:32,319 --> 04:32:34,880
and it will take each item
6869
04:32:34,880 --> 04:32:38,239
from the list using the index number
6870
04:32:38,239 --> 04:32:40,080
if i run it you can see here it says
6871
04:32:40,080 --> 04:32:42,239
hello c plus plus hello java hello
6872
04:32:42,239 --> 04:32:44,399
python and hello r
6873
04:32:44,399 --> 04:32:46,239
cool
6874
04:32:46,239 --> 04:32:49,279
now let's see how you can use
6875
04:32:49,279 --> 04:32:52,560
nested for loops in python
6876
04:32:52,560 --> 04:32:55,439
i'll say nested for loop
6877
04:32:55,439 --> 04:32:56,960
as my comment
6878
04:32:56,960 --> 04:32:58,640
let's see i have a
6879
04:32:58,640 --> 04:32:59,840
list
6880
04:32:59,840 --> 04:33:03,560
of company names
6881
04:33:04,000 --> 04:33:06,160
my first company i have in my list is
6882
04:33:06,160 --> 04:33:08,080
google
6883
04:33:08,080 --> 04:33:10,879
let's say apple
6884
04:33:10,958 --> 04:33:13,118
pwc
6885
04:33:13,118 --> 04:33:14,958
and then we have
6886
04:33:14,958 --> 04:33:17,278
uber
6887
04:33:17,278 --> 04:33:20,879
so here i'll write for i in
6888
04:33:20,879 --> 04:33:23,600
companies
6889
04:33:25,199 --> 04:33:28,320
i'll say print
6890
04:33:28,561 --> 04:33:29,520
we
6891
04:33:29,520 --> 04:33:31,118
will
6892
04:33:31,118 --> 04:33:32,799
display
6893
04:33:32,799 --> 04:33:34,561
each
6894
04:33:34,561 --> 04:33:35,840
letter
6895
04:33:35,840 --> 04:33:38,080
of
6896
04:33:40,400 --> 04:33:43,520
i'll say plus i
6897
04:33:43,520 --> 04:33:47,680
now i'll use my second for loop sync for
6898
04:33:47,680 --> 04:33:50,400
letter in i
6899
04:33:50,400 --> 04:33:54,080
will print the letter
6900
04:33:56,879 --> 04:33:59,438
let's run it and see our result
6901
04:33:59,438 --> 04:34:00,879
there you go
6902
04:34:00,879 --> 04:34:03,199
so here we have used
6903
04:34:03,199 --> 04:34:05,599
two for loops so that becomes a nested
6904
04:34:05,599 --> 04:34:08,000
for loop we have my outer for loop and
6905
04:34:08,000 --> 04:34:08,719
then
6906
04:34:08,719 --> 04:34:09,840
we have our
6907
04:34:09,840 --> 04:34:12,240
inner for loop so in the outer for loop
6908
04:34:12,240 --> 04:34:14,240
we are just printing
6909
04:34:14,240 --> 04:34:15,199
the
6910
04:34:15,199 --> 04:34:17,438
name of the company so here we see we
6911
04:34:17,438 --> 04:34:20,240
will display each letter of
6912
04:34:20,240 --> 04:34:22,561
plus i which stands for
6913
04:34:22,561 --> 04:34:24,958
my company name google so you can see
6914
04:34:24,958 --> 04:34:26,958
below we have
6915
04:34:26,958 --> 04:34:29,118
displayed each letter of google using
6916
04:34:29,118 --> 04:34:30,000
the
6917
04:34:30,000 --> 04:34:32,400
inner for loop similarly we have
6918
04:34:32,400 --> 04:34:35,278
for apple you can see a p p l e we have
6919
04:34:35,278 --> 04:34:38,480
pwc and then we have uber
6920
04:34:38,480 --> 04:34:41,840
so this is how you can use
6921
04:34:41,840 --> 04:34:44,000
multiple for loops
6922
04:34:44,000 --> 04:34:46,639
in python
6923
04:34:46,719 --> 04:34:48,400
now let's see how you can use a for loop
6924
04:34:48,400 --> 04:34:50,160
with else clause
6925
04:34:50,160 --> 04:34:53,840
so i'll give a comment for loop with
6926
04:34:53,840 --> 04:34:57,119
else clause
6927
04:34:59,359 --> 04:35:02,000
so i'll write for i in
6928
04:35:02,000 --> 04:35:03,438
range
6929
04:35:03,438 --> 04:35:06,400
0 comma 10 comma i'll give a step size
6930
04:35:06,400 --> 04:35:08,160
of 3.
6931
04:35:08,160 --> 04:35:09,919
i'll say print
6932
04:35:09,919 --> 04:35:10,958
i
6933
04:35:10,958 --> 04:35:12,639
and after this
6934
04:35:12,639 --> 04:35:16,160
python gives you the flexibility to use
6935
04:35:16,160 --> 04:35:18,480
an else clause
6936
04:35:18,480 --> 04:35:21,278
after the for loop so i'll write
6937
04:35:21,278 --> 04:35:24,160
else print
6938
04:35:24,160 --> 04:35:25,039
the
6939
04:35:25,039 --> 04:35:26,958
loop has
6940
04:35:26,958 --> 04:35:28,958
completed
6941
04:35:28,958 --> 04:35:31,599
execution
6942
04:35:32,080 --> 04:35:33,840
so this is my message
6943
04:35:33,840 --> 04:35:36,240
that we are going to print after we have
6944
04:35:36,240 --> 04:35:38,958
printed all the
6945
04:35:38,958 --> 04:35:40,240
elements
6946
04:35:40,240 --> 04:35:42,000
that are present inside this range
6947
04:35:42,000 --> 04:35:43,118
function
6948
04:35:43,118 --> 04:35:44,879
let's run it you can see here we have
6949
04:35:44,879 --> 04:35:46,480
printed
6950
04:35:46,480 --> 04:35:48,480
the value starting from 0 with a step
6951
04:35:48,480 --> 04:35:52,320
size of 3 so we have 0 3 6 until 9
6952
04:35:52,320 --> 04:35:53,919
and then
6953
04:35:53,919 --> 04:35:56,480
after that we have our else block
6954
04:35:56,480 --> 04:35:57,520
printed
6955
04:35:57,520 --> 04:36:00,000
which has the message the loop has
6956
04:36:00,000 --> 04:36:02,840
completed execution
6957
04:36:02,840 --> 04:36:06,320
cool now let me show you
6958
04:36:06,320 --> 04:36:08,160
two more
6959
04:36:08,160 --> 04:36:11,840
important feature that you can use while
6960
04:36:11,840 --> 04:36:14,000
writing a for loop one is called the
6961
04:36:14,000 --> 04:36:15,438
break statement the other one is called
6962
04:36:15,438 --> 04:36:18,639
the continue statement
6963
04:36:18,639 --> 04:36:20,719
so in python you can break the for loop
6964
04:36:20,719 --> 04:36:22,799
and end it before it has actually run
6965
04:36:22,799 --> 04:36:24,561
all the elements in the i table using
6966
04:36:24,561 --> 04:36:27,118
the break statement
6967
04:36:27,118 --> 04:36:29,039
so let me show you a simple example if i
6968
04:36:29,039 --> 04:36:30,561
write for
6969
04:36:30,561 --> 04:36:33,840
just give a space here
6970
04:36:34,320 --> 04:36:36,958
i in
6971
04:36:36,958 --> 04:36:39,358
range
6972
04:36:39,520 --> 04:36:43,119
let's say from 1 to 10
6973
04:36:43,759 --> 04:36:45,759
i'll use an if condition
6974
04:36:45,759 --> 04:36:50,080
saying if i equal to equal to 6
6975
04:36:50,080 --> 04:36:51,199
then
6976
04:36:51,199 --> 04:36:53,680
break and come out of the loop
6977
04:36:53,680 --> 04:36:56,160
next we'll print i
6978
04:36:56,160 --> 04:36:59,118
so here what we are doing essentially is
6979
04:36:59,118 --> 04:37:01,680
we are iterating over
6980
04:37:01,680 --> 04:37:04,240
all the values from 1 to 10 and whenever
6981
04:37:04,240 --> 04:37:07,039
the i value becomes 6 we come out of the
6982
04:37:07,039 --> 04:37:09,039
loop and print
6983
04:37:09,039 --> 04:37:12,400
all the values we have iterated so far
6984
04:37:12,400 --> 04:37:14,879
let me run it and we'll see the result
6985
04:37:14,879 --> 04:37:17,759
the yuko so here our i value started
6986
04:37:17,759 --> 04:37:19,039
from 1
6987
04:37:19,039 --> 04:37:23,039
we printed 1 2 3 4 5
6988
04:37:23,039 --> 04:37:24,879
and then
6989
04:37:24,879 --> 04:37:28,000
our i value reached 6 then we use the
6990
04:37:28,000 --> 04:37:30,879
break statement to come out of the loop
6991
04:37:30,879 --> 04:37:32,879
all right
6992
04:37:32,879 --> 04:37:34,080
now
6993
04:37:34,080 --> 04:37:35,759
you can also skip the execution of
6994
04:37:35,759 --> 04:37:37,919
further statements in the for loop
6995
04:37:37,919 --> 04:37:39,680
during the titration using the continue
6996
04:37:39,680 --> 04:37:41,278
statement in python
6997
04:37:41,278 --> 04:37:43,359
so when a continue statement is executed
6998
04:37:43,359 --> 04:37:44,719
the for loop continues with the
6999
04:37:44,719 --> 04:37:46,480
execution of the next element in the i
7000
04:37:46,480 --> 04:37:47,520
table
7001
04:37:47,520 --> 04:37:49,359
it does not complete all the statements
7002
04:37:49,359 --> 04:37:51,599
in the for loop body
7003
04:37:51,599 --> 04:37:54,400
so again we'll
7004
04:37:54,400 --> 04:37:56,480
give a comment
7005
04:37:56,480 --> 04:37:58,958
say continue
7006
04:37:58,958 --> 04:37:59,840
okay
7007
04:37:59,840 --> 04:38:02,240
so i'll write for i in
7008
04:38:02,240 --> 04:38:03,359
range
7009
04:38:03,359 --> 04:38:06,240
again from 1 to 10
7010
04:38:06,240 --> 04:38:09,520
give a colon i'll say if
7011
04:38:09,520 --> 04:38:11,599
my i value
7012
04:38:11,599 --> 04:38:13,599
is equal to equal to 6
7013
04:38:13,599 --> 04:38:17,198
then i'll say continue
7014
04:38:17,759 --> 04:38:19,438
then we'll print
7015
04:38:19,438 --> 04:38:21,359
i
7016
04:38:21,359 --> 04:38:23,840
let me run it and we'll see the result
7017
04:38:23,840 --> 04:38:26,561
okay so if you notice this
7018
04:38:26,561 --> 04:38:28,879
output so we have printed values from 1
7019
04:38:28,879 --> 04:38:29,919
to 5
7020
04:38:29,919 --> 04:38:33,359
and when my i value became 6
7021
04:38:33,359 --> 04:38:36,639
we skipped printing 6 and we
7022
04:38:36,639 --> 04:38:38,240
moved on to the next element in the
7023
04:38:38,240 --> 04:38:40,480
iterable which was 7 then we printed 8
7024
04:38:40,480 --> 04:38:41,680
and 9.
7025
04:38:41,680 --> 04:38:43,759
so this is how you can use the break and
7026
04:38:43,759 --> 04:38:46,719
continue statements in python
7027
04:38:46,719 --> 04:38:49,119
all right
7028
04:38:49,438 --> 04:38:51,278
now we'll see a program to display the
7029
04:38:51,278 --> 04:38:54,480
total goals a player has scored
7030
04:38:54,480 --> 04:38:56,160
so here we are going to iterate over a
7031
04:38:56,160 --> 04:38:58,000
dictionary
7032
04:38:58,000 --> 04:39:01,520
so i'll give a comment program to
7033
04:39:01,520 --> 04:39:03,118
display
7034
04:39:03,118 --> 04:39:03,919
the
7035
04:39:03,919 --> 04:39:06,958
total goals
7036
04:39:06,958 --> 04:39:09,999
clear has code
7037
04:39:10,400 --> 04:39:11,599
all right
7038
04:39:11,599 --> 04:39:13,359
first of all
7039
04:39:13,359 --> 04:39:15,680
i'll declare a variable saying clear
7040
04:39:15,680 --> 04:39:16,958
name
7041
04:39:16,958 --> 04:39:19,438
and let's say we have the player name as
7042
04:39:19,438 --> 04:39:22,079
carmelo
7043
04:39:22,799 --> 04:39:25,118
all right
7044
04:39:25,118 --> 04:39:27,118
i'll declare a
7045
04:39:27,118 --> 04:39:29,438
dictionary called ghouls
7046
04:39:29,438 --> 04:39:30,799
and within
7047
04:39:30,799 --> 04:39:33,520
curly braces i'll have
7048
04:39:33,520 --> 04:39:35,599
my first
7049
04:39:35,599 --> 04:39:38,638
clear name addition
7050
04:39:38,638 --> 04:39:41,840
let's see edition has scored
7051
04:39:41,840 --> 04:39:46,000
14 goals then we have let's say
7052
04:39:46,840 --> 04:39:49,840
burnett so burnett is my key here and
7053
04:39:49,840 --> 04:39:51,920
then we'll have
7054
04:39:51,920 --> 04:39:55,040
value in terms of goals let's say three
7055
04:39:55,040 --> 04:39:57,520
and then we have our third player let's
7056
04:39:57,520 --> 04:40:00,240
say carmelo
7057
04:40:00,240 --> 04:40:02,718
and carmelo has scored let's say seven
7058
04:40:02,718 --> 04:40:04,160
goals
7059
04:40:04,160 --> 04:40:06,638
now i'm going to write my for loop so
7060
04:40:06,638 --> 04:40:09,360
i'll write for
7061
04:40:09,360 --> 04:40:12,160
my i table name here is player
7062
04:40:12,160 --> 04:40:12,958
in
7063
04:40:12,958 --> 04:40:16,798
my sequence name is goals so i'll say
7064
04:40:16,798 --> 04:40:18,878
goals
7065
04:40:18,878 --> 04:40:21,520
i'm going to check if
7066
04:40:21,520 --> 04:40:22,560
my
7067
04:40:22,560 --> 04:40:24,400
player
7068
04:40:24,400 --> 04:40:25,120
is
7069
04:40:25,120 --> 04:40:26,638
equal to equal to
7070
04:40:26,638 --> 04:40:30,040
player name
7071
04:40:32,080 --> 04:40:35,120
i'll say print
7072
04:40:35,600 --> 04:40:38,638
the goals scored by the player
7073
04:40:38,638 --> 04:40:40,000
so i'll
7074
04:40:40,000 --> 04:40:41,840
pass my dictionary name and within
7075
04:40:41,840 --> 04:40:43,520
square brackets
7076
04:40:43,520 --> 04:40:44,638
i'll say
7077
04:40:44,638 --> 04:40:47,040
clear
7078
04:40:47,040 --> 04:40:48,080
let's
7079
04:40:48,080 --> 04:40:50,638
use the break statement
7080
04:40:50,638 --> 04:40:51,840
and then
7081
04:40:51,840 --> 04:40:53,360
i'll say
7082
04:40:53,360 --> 04:40:55,680
else
7083
04:40:56,958 --> 04:40:59,840
will print
7084
04:41:00,638 --> 04:41:02,160
my message would be
7085
04:41:02,160 --> 04:41:05,120
no player with
7086
04:41:05,120 --> 04:41:08,240
that name
7087
04:41:08,878 --> 04:41:11,878
found
7088
04:41:12,160 --> 04:41:15,120
let's run and see the result
7089
04:41:15,120 --> 04:41:17,120
all right so here
7090
04:41:17,120 --> 04:41:19,280
what we did was initially i declared a
7091
04:41:19,280 --> 04:41:21,040
variable called player name that had the
7092
04:41:21,040 --> 04:41:22,958
value carmelo
7093
04:41:22,958 --> 04:41:25,600
and since carmelo was present inside my
7094
04:41:25,600 --> 04:41:27,040
dictionary
7095
04:41:27,040 --> 04:41:28,878
named goals you can see here we have
7096
04:41:28,878 --> 04:41:31,920
carmelo who had scored seven goals
7097
04:41:31,920 --> 04:41:34,798
so i i traded over
7098
04:41:34,798 --> 04:41:40,680
my sequence goals and i checked if my
7099
04:41:41,520 --> 04:41:43,680
that is player is equal to equal to the
7100
04:41:43,680 --> 04:41:45,520
player name which is carmelo since this
7101
04:41:45,520 --> 04:41:47,440
carmelo is present inside my dictionary
7102
04:41:47,440 --> 04:41:50,080
name so we printed the goals that were
7103
04:41:50,080 --> 04:41:52,480
scored by carmelo that is 7.
7104
04:41:52,480 --> 04:41:54,400
now let's say i change this player name
7105
04:41:54,400 --> 04:41:55,440
to
7106
04:41:55,440 --> 04:41:58,000
we have let's say messi
7107
04:41:58,000 --> 04:42:00,400
now since messi is not present inside my
7108
04:42:00,400 --> 04:42:03,920
dictionary goals if i run this
7109
04:42:03,920 --> 04:42:06,240
it will print no player with that name
7110
04:42:06,240 --> 04:42:08,240
found the reason being message is not
7111
04:42:08,240 --> 04:42:10,400
present inside
7112
04:42:10,400 --> 04:42:13,680
my dictionary called goals
7113
04:42:13,840 --> 04:42:15,760
cool
7114
04:42:15,760 --> 04:42:17,760
now
7115
04:42:17,760 --> 04:42:19,600
we'll see two more
7116
04:42:19,600 --> 04:42:21,520
examples
7117
04:42:21,520 --> 04:42:22,718
this time
7118
04:42:22,718 --> 04:42:24,160
we are going to
7119
04:42:24,160 --> 04:42:27,040
print the cube of
7120
04:42:27,040 --> 04:42:30,638
numbers using for loop
7121
04:42:31,440 --> 04:42:32,480
all right
7122
04:42:32,480 --> 04:42:34,480
so first of all i'll declare a list
7123
04:42:34,480 --> 04:42:36,718
which will have a list of numbers let's
7124
04:42:36,718 --> 04:42:38,718
say 2 5
7125
04:42:38,718 --> 04:42:43,120
7 4 8 and let's say 6.
7126
04:42:43,120 --> 04:42:45,360
then
7127
04:42:46,080 --> 04:42:47,440
i'll pass in a
7128
04:42:47,440 --> 04:42:51,120
empty list so i'll declare within
7129
04:42:51,120 --> 04:42:53,280
empty square brackets
7130
04:42:53,280 --> 04:42:55,920
i'll say for i in
7131
04:42:55,920 --> 04:43:00,080
my sequence name that is num
7132
04:43:00,400 --> 04:43:03,440
i'm going to use my empty list cube and
7133
04:43:03,440 --> 04:43:06,878
then followed by the append function
7134
04:43:06,878 --> 04:43:08,400
and inside the append function we'll
7135
04:43:08,400 --> 04:43:10,160
have our
7136
04:43:10,160 --> 04:43:13,760
cubes of the number so i'll write i
7137
04:43:13,760 --> 04:43:16,878
i'll use the double asterisk operator
7138
04:43:16,878 --> 04:43:19,840
and then i'll say three
7139
04:43:19,840 --> 04:43:23,360
after that we are going to print
7140
04:43:23,680 --> 04:43:24,560
my
7141
04:43:24,560 --> 04:43:27,280
list name that is cube
7142
04:43:27,280 --> 04:43:28,798
let's run it
7143
04:43:28,798 --> 04:43:30,480
there you go
7144
04:43:30,480 --> 04:43:32,878
so you can see here
7145
04:43:32,878 --> 04:43:35,520
first we have
7146
04:43:35,520 --> 04:43:38,160
the list of the numbers here
7147
04:43:38,160 --> 04:43:39,360
and then
7148
04:43:39,360 --> 04:43:41,440
we have printed the
7149
04:43:41,440 --> 04:43:44,560
cubes of these numbers in a pattern
7150
04:43:44,560 --> 04:43:46,798
let's say
7151
04:43:46,798 --> 04:43:49,200
bring this print statement here and i'll
7152
04:43:49,200 --> 04:43:50,400
run it again
7153
04:43:50,400 --> 04:43:53,520
now this is much more readable you have
7154
04:43:53,520 --> 04:43:58,080
2 cube is 8 5 cube is 125 7 cube is
7155
04:43:58,080 --> 04:43:58,840
3
7156
04:43:58,840 --> 04:44:03,200
43 then 4 cube is 64 8 cube is 512 6
7157
04:44:03,200 --> 04:44:05,680
cube is 216
7158
04:44:05,680 --> 04:44:07,520
all right
7159
04:44:07,520 --> 04:44:09,040
now coming to
7160
04:44:09,040 --> 04:44:11,200
the last example in our demo section of
7161
04:44:11,200 --> 04:44:12,480
this
7162
04:44:12,480 --> 04:44:14,958
for loops in python video
7163
04:44:14,958 --> 04:44:15,920
will
7164
04:44:15,920 --> 04:44:17,200
write
7165
04:44:17,200 --> 04:44:19,680
a program to print a pattern
7166
04:44:19,680 --> 04:44:23,120
i'll say pattern printing
7167
04:44:23,280 --> 04:44:25,680
all right
7168
04:44:25,680 --> 04:44:28,878
so we are going to print
7169
04:44:28,878 --> 04:44:32,240
a right angle triangle
7170
04:44:32,240 --> 04:44:34,638
for that i am going to take
7171
04:44:34,638 --> 04:44:36,798
my first variable called n which is
7172
04:44:36,798 --> 04:44:39,280
going to be
7173
04:44:39,760 --> 04:44:44,240
a user input to enter the number of rows
7174
04:44:44,240 --> 04:44:46,160
so using the input function
7175
04:44:46,160 --> 04:44:48,320
i am passing the message
7176
04:44:48,320 --> 04:44:50,798
enter the
7177
04:44:51,120 --> 04:44:53,520
number of
7178
04:44:53,520 --> 04:44:56,520
rows
7179
04:44:56,638 --> 04:44:59,920
i'll say for i in
7180
04:44:59,920 --> 04:45:01,280
range
7181
04:45:01,280 --> 04:45:03,640
0 till my user input n
7182
04:45:03,640 --> 04:45:05,920
[Music]
7183
04:45:05,920 --> 04:45:08,160
give a colon
7184
04:45:08,160 --> 04:45:09,600
i'll use my
7185
04:45:09,600 --> 04:45:12,160
inner for loop
7186
04:45:12,160 --> 04:45:13,920
this is
7187
04:45:13,920 --> 04:45:16,718
going to be the number of columns so
7188
04:45:16,718 --> 04:45:19,360
i'll say for j in
7189
04:45:19,360 --> 04:45:21,280
range
7190
04:45:21,280 --> 04:45:22,840
0
7191
04:45:22,840 --> 04:45:27,040
comma i plus 1
7192
04:45:27,520 --> 04:45:29,600
give a colon
7193
04:45:29,600 --> 04:45:31,920
and
7194
04:45:32,798 --> 04:45:35,440
i'll print
7195
04:45:36,240 --> 04:45:39,920
my star pattern so i'm using asterix
7196
04:45:39,920 --> 04:45:43,680
and i'll use my end parameter
7197
04:45:43,680 --> 04:45:46,560
i'll have my double quotations
7198
04:45:46,560 --> 04:45:48,958
and then
7199
04:45:49,120 --> 04:45:50,958
we'll say
7200
04:45:50,958 --> 04:45:53,840
print
7201
04:45:57,040 --> 04:45:59,200
the pattern
7202
04:45:59,200 --> 04:46:02,560
so here what i am doing is i am taking
7203
04:46:02,560 --> 04:46:06,240
the number of rows from the user
7204
04:46:06,240 --> 04:46:08,560
and i'm iterating
7205
04:46:08,560 --> 04:46:12,320
from 0 till n which is my user input and
7206
04:46:12,320 --> 04:46:13,440
then
7207
04:46:13,440 --> 04:46:14,958
we are
7208
04:46:14,958 --> 04:46:16,718
assigning another for loop for the
7209
04:46:16,718 --> 04:46:19,760
number of rows so that ranges from 0
7210
04:46:19,760 --> 04:46:22,000
till i plus 1
7211
04:46:22,000 --> 04:46:24,798
then i'm printing my star
7212
04:46:24,798 --> 04:46:27,920
let's run it and see the output
7213
04:46:27,920 --> 04:46:29,520
let's say i'll enter the number of rows
7214
04:46:29,520 --> 04:46:31,200
as 6.
7215
04:46:31,200 --> 04:46:34,400
if i hit enter you can see here we have
7216
04:46:34,400 --> 04:46:37,040
our right angle triangle printed here
7217
04:46:37,040 --> 04:46:38,638
so initially
7218
04:46:38,638 --> 04:46:40,400
my
7219
04:46:40,400 --> 04:46:42,400
i value is
7220
04:46:42,400 --> 04:46:43,840
0
7221
04:46:43,840 --> 04:46:44,958
so here
7222
04:46:44,958 --> 04:46:48,320
my j value becomes from 0 till 1 so we
7223
04:46:48,320 --> 04:46:51,440
have just 1 star printed
7224
04:46:51,440 --> 04:46:54,320
in the next iteration my i value becomes
7225
04:46:54,320 --> 04:46:55,520
1
7226
04:46:55,520 --> 04:46:57,760
so from
7227
04:46:57,760 --> 04:47:01,360
0 to 1 plus 1 that is 2 we have 2 stars
7228
04:47:01,360 --> 04:47:04,080
printed the next iteration my i value
7229
04:47:04,080 --> 04:47:06,480
becomes two so two plus one is three
7230
04:47:06,480 --> 04:47:08,958
again so we have three columns here so
7231
04:47:08,958 --> 04:47:11,920
three stars printed so on and so forth
7232
04:47:11,920 --> 04:47:13,520
all right if getting your learning
7233
04:47:13,520 --> 04:47:15,520
started is half the battle what if you
7234
04:47:15,520 --> 04:47:18,400
could do that for free visit scale up by
7235
04:47:18,400 --> 04:47:20,240
simply learn click on the link in the
7236
04:47:20,240 --> 04:47:22,718
description to know more
7237
04:47:22,718 --> 04:47:25,920
so what is a while loop in python
7238
04:47:25,920 --> 04:47:28,480
a while loop specifies a condition or a
7239
04:47:28,480 --> 04:47:30,480
block of code that is to be executed
7240
04:47:30,480 --> 04:47:32,638
until the condition evaluates to false
7241
04:47:32,638 --> 04:47:35,440
or the loop is explicitly ended with a
7242
04:47:35,440 --> 04:47:37,360
break statement
7243
04:47:37,360 --> 04:47:40,240
moving on to the next slide
7244
04:47:40,240 --> 04:47:42,400
so here you can see the syntax of a
7245
04:47:42,400 --> 04:47:43,520
while loop
7246
04:47:43,520 --> 04:47:45,600
so a while loop is defined using the
7247
04:47:45,600 --> 04:47:47,760
while keyword and then you have the
7248
04:47:47,760 --> 04:47:51,040
expression followed by a colon
7249
04:47:51,040 --> 04:47:53,200
after that you have the statements
7250
04:47:53,200 --> 04:47:54,480
inside the block
7251
04:47:54,480 --> 04:47:56,480
the statements are executed until the
7252
04:47:56,480 --> 04:47:59,600
condition evaluates to false
7253
04:47:59,600 --> 04:48:01,040
now on the right side you can see the
7254
04:48:01,040 --> 04:48:02,638
flowchart to show
7255
04:48:02,638 --> 04:48:04,320
all the steps
7256
04:48:04,320 --> 04:48:06,958
to execute a while loop so you can see
7257
04:48:06,958 --> 04:48:09,600
here we have the test expression if the
7258
04:48:09,600 --> 04:48:12,080
test expression evaluates to true then
7259
04:48:12,080 --> 04:48:13,760
we execute the statements that are
7260
04:48:13,760 --> 04:48:16,240
present inside the while block
7261
04:48:16,240 --> 04:48:18,480
but if the condition is false we will
7262
04:48:18,480 --> 04:48:20,480
exit out of the while loop
7263
04:48:20,480 --> 04:48:22,160
now before i move on
7264
04:48:22,160 --> 04:48:24,400
i have a question for you
7265
04:48:24,400 --> 04:48:27,520
so here is a python quiz question
7266
04:48:27,520 --> 04:48:29,600
i want you to tell me the output of the
7267
04:48:29,600 --> 04:48:33,040
below code so this is regarding a list
7268
04:48:33,040 --> 04:48:36,000
i have declared a list called alpha that
7269
04:48:36,000 --> 04:48:37,280
has d
7270
04:48:37,280 --> 04:48:40,080
f and c as its elements then i am saying
7271
04:48:40,080 --> 04:48:42,878
alpha plus equal to ka which is again a
7272
04:48:42,878 --> 04:48:45,520
string since it is within the single
7273
04:48:45,520 --> 04:48:48,320
quotes then i am printing the list
7274
04:48:48,320 --> 04:48:50,160
now you need to tell me what is the
7275
04:48:50,160 --> 04:48:52,718
right answer you have four options here
7276
04:48:52,718 --> 04:48:56,400
a b c and d the first option is d comma
7277
04:48:56,400 --> 04:48:59,120
f comma c comma k a combined
7278
04:48:59,120 --> 04:49:01,360
then we have k a in the beginning of the
7279
04:49:01,360 --> 04:49:02,160
list
7280
04:49:02,160 --> 04:49:05,280
followed by dfc
7281
04:49:05,520 --> 04:49:08,000
the third option or the c option is d f
7282
04:49:08,000 --> 04:49:10,480
c k a k being
7283
04:49:10,480 --> 04:49:12,080
individual elements
7284
04:49:12,080 --> 04:49:15,360
and the last option the d option is k a
7285
04:49:15,360 --> 04:49:17,120
d f c
7286
04:49:17,120 --> 04:49:19,040
please go ahead and solve this question
7287
04:49:19,040 --> 04:49:20,320
and put your answers in the comment
7288
04:49:20,320 --> 04:49:21,760
section of the video
7289
04:49:21,760 --> 04:49:23,120
we'll be more than happy to know from
7290
04:49:23,120 --> 04:49:24,160
you
7291
04:49:24,160 --> 04:49:25,440
all right
7292
04:49:25,440 --> 04:49:27,040
now let's
7293
04:49:27,040 --> 04:49:30,798
go ahead and start with our demo
7294
04:49:30,798 --> 04:49:33,120
so here i am on my jupyter notebook let
7295
04:49:33,120 --> 04:49:33,840
me
7296
04:49:33,840 --> 04:49:36,718
rename my file i'll say
7297
04:49:36,718 --> 04:49:39,440
while looping python
7298
04:49:39,440 --> 04:49:40,798
demo
7299
04:49:40,798 --> 04:49:42,878
okay
7300
04:49:42,878 --> 04:49:44,320
so
7301
04:49:44,320 --> 04:49:46,400
let me start by writing a simple while
7302
04:49:46,400 --> 04:49:49,280
loop i'll declare a variable called i
7303
04:49:49,280 --> 04:49:52,000
equal to 0
7304
04:49:52,000 --> 04:49:53,840
then i'll start with my
7305
04:49:53,840 --> 04:49:55,280
while loop
7306
04:49:55,280 --> 04:49:57,360
i'll write my while keyword
7307
04:49:57,360 --> 04:50:01,280
and this will run till my i is less than
7308
04:50:01,280 --> 04:50:02,798
10 let's say
7309
04:50:02,798 --> 04:50:04,638
i'll give a colon
7310
04:50:04,638 --> 04:50:07,680
and if i hit enter it will come to the
7311
04:50:07,680 --> 04:50:09,600
next line you can see the indentation
7312
04:50:09,600 --> 04:50:12,080
here which is being maintained i'll say
7313
04:50:12,080 --> 04:50:13,760
i equal to
7314
04:50:13,760 --> 04:50:19,840
i plus 1 so i am incrementing my i value
7315
04:50:20,320 --> 04:50:22,400
then i'll say print
7316
04:50:22,400 --> 04:50:23,520
i
7317
04:50:23,520 --> 04:50:26,240
and i'll use my end parameter
7318
04:50:26,240 --> 04:50:27,680
and i'll say end
7319
04:50:27,680 --> 04:50:29,280
within
7320
04:50:29,280 --> 04:50:31,920
single quotes i'll say hash and give a
7321
04:50:31,920 --> 04:50:34,320
space
7322
04:50:35,440 --> 04:50:38,560
then let me say print again
7323
04:50:38,560 --> 04:50:42,400
so here my while loop will run until my
7324
04:50:42,400 --> 04:50:44,718
i value is less than 10
7325
04:50:44,718 --> 04:50:46,560
so we are continuously incrementing the
7326
04:50:46,560 --> 04:50:48,798
i value and printing the i value
7327
04:50:48,798 --> 04:50:51,200
let me run it there you go you can see
7328
04:50:51,200 --> 04:50:52,560
the result here
7329
04:50:52,560 --> 04:50:54,878
we have printed the i values starting
7330
04:50:54,878 --> 04:50:57,040
from
7331
04:50:57,040 --> 04:50:59,120
1 till 10
7332
04:50:59,120 --> 04:51:00,638
and after that
7333
04:51:00,638 --> 04:51:02,878
we have come out of the loop
7334
04:51:02,878 --> 04:51:04,958
and you can see the
7335
04:51:04,958 --> 04:51:07,200
hash symbol being used here because we
7336
04:51:07,200 --> 04:51:09,600
used our end parameter as has
7337
04:51:09,600 --> 04:51:12,958
so by default it is a new line character
7338
04:51:12,958 --> 04:51:15,680
but we have explicitly defined our
7339
04:51:15,680 --> 04:51:18,560
end parameter as has followed by a space
7340
04:51:18,560 --> 04:51:20,480
cool
7341
04:51:20,480 --> 04:51:22,638
now let's move on to our next example in
7342
04:51:22,638 --> 04:51:25,280
this video on while loop
7343
04:51:25,280 --> 04:51:28,480
i'll define a variable called count
7344
04:51:28,480 --> 04:51:32,000
and i'll assign a value 0
7345
04:51:32,000 --> 04:51:33,360
i'll say while
7346
04:51:33,360 --> 04:51:35,600
my count is
7347
04:51:35,600 --> 04:51:39,680
less than equal to 5 i'll give a colon
7348
04:51:39,680 --> 04:51:42,798
come to the next line and i'll print
7349
04:51:42,798 --> 04:51:44,718
the
7350
04:51:44,718 --> 04:51:47,360
condition
7351
04:51:47,600 --> 04:51:48,320
is
7352
04:51:48,320 --> 04:51:50,638
true
7353
04:51:51,440 --> 04:51:53,920
and then i'll say
7354
04:51:53,920 --> 04:51:55,360
count
7355
04:51:55,360 --> 04:51:56,878
equal to
7356
04:51:56,878 --> 04:51:58,080
count
7357
04:51:58,080 --> 04:52:00,240
plus 1
7358
04:52:00,240 --> 04:52:02,000
and then i'll say
7359
04:52:02,000 --> 04:52:03,600
print
7360
04:52:03,600 --> 04:52:06,320
end of loop
7361
04:52:07,360 --> 04:52:09,600
let's run and see our result
7362
04:52:09,600 --> 04:52:11,360
you can see here we have printed our
7363
04:52:11,360 --> 04:52:14,480
message the condition is true five times
7364
04:52:14,480 --> 04:52:15,680
because
7365
04:52:15,680 --> 04:52:18,000
our expression is being evaluated five
7366
04:52:18,000 --> 04:52:19,600
times here
7367
04:52:19,600 --> 04:52:22,638
in the sixth time our count is no more
7368
04:52:22,638 --> 04:52:24,958
less than equal to 5 so we
7369
04:52:24,958 --> 04:52:26,878
end the loop and here you can see the
7370
04:52:26,878 --> 04:52:28,958
message end of loop
7371
04:52:28,958 --> 04:52:30,160
cool
7372
04:52:30,160 --> 04:52:33,040
now moving ahead
7373
04:52:34,000 --> 04:52:35,920
you can also use
7374
04:52:35,920 --> 04:52:37,760
the break statement
7375
04:52:37,760 --> 04:52:39,520
in a while loop let me show you how to
7376
04:52:39,520 --> 04:52:40,560
do it
7377
04:52:40,560 --> 04:52:43,920
i'll say while true
7378
04:52:43,920 --> 04:52:45,840
so i am
7379
04:52:45,840 --> 04:52:48,560
declaring my
7380
04:52:48,718 --> 04:52:51,360
while loop to be true
7381
04:52:51,360 --> 04:52:53,120
and i'll say
7382
04:52:53,120 --> 04:52:54,638
please
7383
04:52:54,638 --> 04:52:55,520
type
7384
04:52:55,520 --> 04:52:57,440
your
7385
04:52:57,440 --> 04:52:59,760
name
7386
04:53:00,480 --> 04:53:02,560
and i'm going to
7387
04:53:02,560 --> 04:53:03,840
take my
7388
04:53:03,840 --> 04:53:06,480
name as input from the user so i am
7389
04:53:06,480 --> 04:53:08,080
specifying the
7390
04:53:08,080 --> 04:53:10,480
input
7391
04:53:10,480 --> 04:53:12,320
function
7392
04:53:12,320 --> 04:53:14,638
i am going to check if my name is equal
7393
04:53:14,638 --> 04:53:16,400
to equal to let's say
7394
04:53:16,400 --> 04:53:18,798
gilly
7395
04:53:18,798 --> 04:53:22,638
then i should give a colon here
7396
04:53:22,638 --> 04:53:25,360
i'll break out of the loop
7397
04:53:25,360 --> 04:53:27,040
and then we'll
7398
04:53:27,040 --> 04:53:29,440
print
7399
04:53:29,840 --> 04:53:32,320
thank you
7400
04:53:32,840 --> 04:53:34,400
you
7401
04:53:34,400 --> 04:53:36,080
typed
7402
04:53:36,080 --> 04:53:38,000
the
7403
04:53:38,000 --> 04:53:41,040
correct name
7404
04:53:41,520 --> 04:53:42,878
all right
7405
04:53:42,878 --> 04:53:45,760
so what i'm doing here is
7406
04:53:45,760 --> 04:53:48,798
i'm going to run this loop till the user
7407
04:53:48,798 --> 04:53:51,760
enters the correct name so i have
7408
04:53:51,760 --> 04:53:54,480
declared my name as gilly if you enter
7409
04:53:54,480 --> 04:53:56,560
any other name apart from gili
7410
04:53:56,560 --> 04:53:58,560
this while loop will
7411
04:53:58,560 --> 04:54:01,840
continue to run and execute
7412
04:54:01,840 --> 04:54:04,000
run it okay
7413
04:54:04,000 --> 04:54:06,638
so here it says please enter your name
7414
04:54:06,638 --> 04:54:10,240
i'll let's say say john
7415
04:54:11,760 --> 04:54:13,680
if i run it
7416
04:54:13,680 --> 04:54:15,840
it is again asking me please enter your
7417
04:54:15,840 --> 04:54:20,080
name let's say this time i'll say ami
7418
04:54:20,080 --> 04:54:22,000
you can see here it is again asking for
7419
04:54:22,000 --> 04:54:22,958
the
7420
04:54:22,958 --> 04:54:26,958
name so i'll enter my name let's say
7421
04:54:27,760 --> 04:54:30,760
hopkins
7422
04:54:32,878 --> 04:54:33,760
again
7423
04:54:33,760 --> 04:54:35,440
our entered
7424
04:54:35,440 --> 04:54:37,840
user name doesn't match
7425
04:54:37,840 --> 04:54:39,360
the name that we have provided in the
7426
04:54:39,360 --> 04:54:42,400
code let's say this time i'll say gilly
7427
04:54:42,400 --> 04:54:44,400
if i hit enter you can see we have our
7428
04:54:44,400 --> 04:54:47,040
message thank you you typed the correct
7429
04:54:47,040 --> 04:54:48,480
name
7430
04:54:48,480 --> 04:54:50,798
okay
7431
04:54:50,958 --> 04:54:53,680
now moving on to the next example we
7432
04:54:53,680 --> 04:54:55,680
will see how to find the least common
7433
04:54:55,680 --> 04:54:57,920
multiple of 4 and seven
7434
04:54:57,920 --> 04:55:00,160
so i'll give a comment as
7435
04:55:00,160 --> 04:55:02,240
lcm of
7436
04:55:02,240 --> 04:55:04,718
four and seven
7437
04:55:04,718 --> 04:55:06,400
now you would know
7438
04:55:06,400 --> 04:55:08,958
it is 28 but we are going to find out
7439
04:55:08,958 --> 04:55:10,160
with the help of
7440
04:55:10,160 --> 04:55:12,320
a python while loop so first i'll
7441
04:55:12,320 --> 04:55:15,760
declare a variable x equal to 0
7442
04:55:15,760 --> 04:55:18,958
i'll say while
7443
04:55:19,120 --> 04:55:22,878
my expression is true
7444
04:55:23,040 --> 04:55:26,160
i'm going to increment my x value to 1
7445
04:55:26,160 --> 04:55:27,600
i'll say x
7446
04:55:27,600 --> 04:55:29,120
plus equal to 1
7447
04:55:29,120 --> 04:55:32,718
this means x equal to x plus 1
7448
04:55:32,718 --> 04:55:34,320
i'm going to use
7449
04:55:34,320 --> 04:55:38,400
an if statement and say if not
7450
04:55:38,400 --> 04:55:41,440
and here we'll pass the condition
7451
04:55:41,440 --> 04:55:43,040
x
7452
04:55:43,040 --> 04:55:47,440
percentage 4 or modulo 4
7453
04:55:48,160 --> 04:55:49,360
or
7454
04:55:49,360 --> 04:55:50,160
x
7455
04:55:50,160 --> 04:55:53,360
percentage 7
7456
04:55:53,360 --> 04:55:56,798
then i'll say break
7457
04:55:58,080 --> 04:56:00,080
finally we'll
7458
04:56:00,080 --> 04:56:02,958
print our message saying x
7459
04:56:02,958 --> 04:56:04,400
comma
7460
04:56:04,400 --> 04:56:06,798
and i'll say is
7461
04:56:06,798 --> 04:56:09,798
divisible
7462
04:56:10,958 --> 04:56:11,760
by
7463
04:56:11,760 --> 04:56:14,320
four and
7464
04:56:14,400 --> 04:56:16,718
seven
7465
04:56:17,840 --> 04:56:20,000
say both four and seven
7466
04:56:20,000 --> 04:56:20,878
okay
7467
04:56:20,878 --> 04:56:23,360
so what i am doing here is
7468
04:56:23,360 --> 04:56:26,638
i am taking my expression while true
7469
04:56:26,638 --> 04:56:29,360
i'm incrementing the value of x
7470
04:56:29,360 --> 04:56:32,798
each time and checking if
7471
04:56:32,798 --> 04:56:35,120
this is true or not
7472
04:56:35,120 --> 04:56:37,600
now at the first instance when my number
7473
04:56:37,600 --> 04:56:40,718
x is divisible by both 4 and 7 i'm
7474
04:56:40,718 --> 04:56:42,798
coming out of the loop and we are going
7475
04:56:42,798 --> 04:56:45,840
to print this now let's see
7476
04:56:45,840 --> 04:56:48,080
what's the answer there you go you can
7477
04:56:48,080 --> 04:56:51,040
see here 28 is divisible by both
7478
04:56:51,040 --> 04:56:54,320
4 and 7 and hence the lcm of 4 and 7 is
7479
04:56:54,320 --> 04:56:55,440
28.
7480
04:56:55,440 --> 04:56:57,600
so this is the least
7481
04:56:57,600 --> 04:57:01,120
number which is divisible by 4 and 7.
7482
04:57:01,120 --> 04:57:02,878
all right
7483
04:57:02,878 --> 04:57:04,718
now you can also use the continue
7484
04:57:04,718 --> 04:57:06,480
statement
7485
04:57:06,480 --> 04:57:09,920
how you use the break statement to
7486
04:57:09,920 --> 04:57:12,000
come out of the loop you can also use
7487
04:57:12,000 --> 04:57:14,878
the continue statement to continue
7488
04:57:14,878 --> 04:57:16,400
running the loop
7489
04:57:16,400 --> 04:57:17,600
so i'll say
7490
04:57:17,600 --> 04:57:22,878
i equal to 0 while i is less than 10
7491
04:57:22,878 --> 04:57:24,798
so this is my expression my loop is
7492
04:57:24,798 --> 04:57:27,600
going to run until i is less than 10 i
7493
04:57:27,600 --> 04:57:30,638
am going to increment the value of i
7494
04:57:30,638 --> 04:57:34,160
i'll say i plus equal to 1
7495
04:57:34,160 --> 04:57:35,440
if
7496
04:57:35,440 --> 04:57:38,560
let's say i set my condition if i equal
7497
04:57:38,560 --> 04:57:41,200
to equal to let's say 6
7498
04:57:41,200 --> 04:57:42,878
then i'll say
7499
04:57:42,878 --> 04:57:45,600
continue my execution and skip the
7500
04:57:45,600 --> 04:57:47,600
current step where
7501
04:57:47,600 --> 04:57:49,920
i value becomes six
7502
04:57:49,920 --> 04:57:52,080
then i'll say print i
7503
04:57:52,080 --> 04:57:54,560
let's run it and see the result
7504
04:57:54,560 --> 04:57:56,240
okay you can see here we have printed
7505
04:57:56,240 --> 04:57:58,718
one two three four five and here you can
7506
04:57:58,718 --> 04:58:01,760
see we have skipped the execution where
7507
04:58:01,760 --> 04:58:04,798
i value became 6 and continued with the
7508
04:58:04,798 --> 04:58:08,160
next iteration 7 8 9 and 10.
7509
04:58:08,160 --> 04:58:10,320
so this is how you can use the continue
7510
04:58:10,320 --> 04:58:12,080
statement and the break statement in a
7511
04:58:12,080 --> 04:58:13,760
while loop
7512
04:58:13,760 --> 04:58:16,160
all right
7513
04:58:16,958 --> 04:58:20,320
now moving ahead you can also use the
7514
04:58:20,320 --> 04:58:22,560
else statement in a while loop now let
7515
04:58:22,560 --> 04:58:24,638
me show you how to do it
7516
04:58:24,638 --> 04:58:26,840
i'll say i equal to
7517
04:58:26,840 --> 04:58:30,798
1 then i'll write while i is less than
7518
04:58:30,798 --> 04:58:33,360
let's say 5
7519
04:58:33,360 --> 04:58:36,240
i'll say print i
7520
04:58:36,240 --> 04:58:38,000
then i am going to increment the value
7521
04:58:38,000 --> 04:58:38,718
to
7522
04:58:38,718 --> 04:58:42,878
i plus equal to 1.
7523
04:58:42,878 --> 04:58:44,958
now here you can use the
7524
04:58:44,958 --> 04:58:48,958
else statement and say print
7525
04:58:49,760 --> 04:58:50,798
i
7526
04:58:50,798 --> 04:58:51,920
is
7527
04:58:51,920 --> 04:58:53,600
not
7528
04:58:53,600 --> 04:58:55,040
less than
7529
04:58:55,040 --> 04:58:56,718
5
7530
04:58:56,718 --> 04:58:59,200
let's run it you can see here
7531
04:58:59,200 --> 04:59:00,638
we
7532
04:59:00,638 --> 04:59:04,080
ran our loop until i value was less than
7533
04:59:04,080 --> 04:59:07,040
5 so we printed 1 2 3 and 4
7534
04:59:07,040 --> 04:59:09,200
when i value became 5
7535
04:59:09,200 --> 04:59:10,480
we printed
7536
04:59:10,480 --> 04:59:14,000
i value is not less than 5.
7537
04:59:14,000 --> 04:59:16,320
cool
7538
04:59:17,440 --> 04:59:20,320
now moving ahead
7539
04:59:20,400 --> 04:59:22,000
so you can
7540
04:59:22,000 --> 04:59:24,638
use inbuilt functions inside a while
7541
04:59:24,638 --> 04:59:26,400
loop let me show you
7542
04:59:26,400 --> 04:59:27,840
how to use the
7543
04:59:27,840 --> 04:59:30,400
pop function and remove the elements in
7544
04:59:30,400 --> 04:59:32,560
a list using a while loop
7545
04:59:32,560 --> 04:59:34,320
so i'll declare a list
7546
04:59:34,320 --> 04:59:38,560
of numbers let's say 1 comma 2 comma 3 4
7547
04:59:38,560 --> 04:59:40,798
and we have 5 so these are the 5
7548
04:59:40,798 --> 04:59:43,520
elements that are present inside list a
7549
04:59:43,520 --> 04:59:45,200
i'll say while a
7550
04:59:45,200 --> 04:59:49,040
so i'm iterating over all the elements
7551
04:59:49,040 --> 04:59:50,798
in my list
7552
04:59:50,798 --> 04:59:53,440
i'm going to say print a dot i'll use
7553
04:59:53,440 --> 04:59:56,878
the pop function
7554
04:59:57,280 --> 04:59:59,120
so
7555
04:59:59,120 --> 05:00:01,440
what pop does is it is going to remove
7556
05:00:01,440 --> 05:00:03,280
the elements
7557
05:00:03,280 --> 05:00:05,680
from the end of a list
7558
05:00:05,680 --> 05:00:07,680
then i'll say else
7559
05:00:07,680 --> 05:00:10,080
print
7560
05:00:11,360 --> 05:00:13,840
there
7561
05:00:14,080 --> 05:00:15,600
are
7562
05:00:15,600 --> 05:00:17,520
no elements
7563
05:00:17,520 --> 05:00:19,360
left
7564
05:00:19,360 --> 05:00:22,320
in the list
7565
05:00:23,920 --> 05:00:25,360
all right
7566
05:00:25,360 --> 05:00:27,840
let's run it and see the result you can
7567
05:00:27,840 --> 05:00:31,280
see here we are removing
7568
05:00:31,280 --> 05:00:33,280
the elements one by one from the end of
7569
05:00:33,280 --> 05:00:35,360
the list so first we popped out five
7570
05:00:35,360 --> 05:00:38,000
then four three two and one and after
7571
05:00:38,000 --> 05:00:40,400
that we had no more elements left in the
7572
05:00:40,400 --> 05:00:42,638
list so we printed there are no elements
7573
05:00:42,638 --> 05:00:44,560
left in the list
7574
05:00:44,560 --> 05:00:47,560
okay
7575
05:00:49,840 --> 05:00:52,160
now the next program we will take
7576
05:00:52,160 --> 05:00:54,160
numbers as input from the user and
7577
05:00:54,160 --> 05:00:56,080
calculate the average
7578
05:00:56,080 --> 05:00:58,240
as long as the user enters a positive
7579
05:00:58,240 --> 05:00:59,600
number
7580
05:00:59,600 --> 05:01:01,280
so i'll give a comment
7581
05:01:01,280 --> 05:01:03,040
average of
7582
05:01:03,040 --> 05:01:06,000
positive numbers
7583
05:01:08,480 --> 05:01:10,798
cool
7584
05:01:11,120 --> 05:01:13,040
okay
7585
05:01:13,040 --> 05:01:15,920
so i'll start my program i'll declare a
7586
05:01:15,920 --> 05:01:17,440
variable called num
7587
05:01:17,440 --> 05:01:19,520
which has a value 0
7588
05:01:19,520 --> 05:01:21,760
i'll declare a variable called count
7589
05:01:21,760 --> 05:01:25,200
which also has a value 0
7590
05:01:25,200 --> 05:01:27,200
then i have sum
7591
05:01:27,200 --> 05:01:29,600
which has a value of 0
7592
05:01:29,600 --> 05:01:31,520
i'll write while
7593
05:01:31,520 --> 05:01:32,958
my
7594
05:01:32,958 --> 05:01:37,120
number is greater than equal to 0 so
7595
05:01:37,120 --> 05:01:39,760
this condition
7596
05:01:39,760 --> 05:01:42,878
is for accepting only positive numbers
7597
05:01:42,878 --> 05:01:46,320
i'll say num equal to
7598
05:01:46,320 --> 05:01:49,760
i'm going to take the positive numbers
7599
05:01:49,760 --> 05:01:51,360
from the user
7600
05:01:51,360 --> 05:01:53,200
so i'll
7601
05:01:53,200 --> 05:01:56,638
pass an input function i'll say enter
7602
05:01:56,638 --> 05:01:58,400
any
7603
05:01:58,400 --> 05:02:00,000
positive
7604
05:02:00,000 --> 05:02:02,320
number
7605
05:02:02,560 --> 05:02:05,760
then i'm going to say if
7606
05:02:05,760 --> 05:02:09,440
my number is greater than 0
7607
05:02:09,440 --> 05:02:11,040
i'll say
7608
05:02:11,040 --> 05:02:13,600
count equal to
7609
05:02:13,600 --> 05:02:16,000
count plus 1
7610
05:02:16,000 --> 05:02:18,400
let's make this
7611
05:02:18,400 --> 05:02:21,120
greater than equal to 0
7612
05:02:21,120 --> 05:02:23,680
then i'll say sum
7613
05:02:23,680 --> 05:02:24,798
equal to
7614
05:02:24,798 --> 05:02:26,480
sum plus
7615
05:02:26,480 --> 05:02:28,718
my number
7616
05:02:28,718 --> 05:02:31,120
so here i am keeping track of the
7617
05:02:31,120 --> 05:02:33,600
numbers that we have
7618
05:02:33,600 --> 05:02:36,160
entered and here we are calculating this
7619
05:02:36,160 --> 05:02:39,600
sum that is the cumulative sum
7620
05:02:39,600 --> 05:02:42,320
then i'll say
7621
05:02:42,798 --> 05:02:44,320
avg
7622
05:02:44,320 --> 05:02:46,560
which is to find the average
7623
05:02:46,560 --> 05:02:49,760
i'll say sum divided by my variable that
7624
05:02:49,760 --> 05:02:51,600
is count
7625
05:02:51,600 --> 05:02:52,480
then
7626
05:02:52,480 --> 05:02:55,840
i'm going to print
7627
05:02:56,080 --> 05:02:58,480
total
7628
05:02:59,120 --> 05:03:01,680
i'll say total
7629
05:03:01,680 --> 05:03:03,360
sum of
7630
05:03:03,360 --> 05:03:05,920
numbers
7631
05:03:06,958 --> 05:03:08,958
give a colon
7632
05:03:08,958 --> 05:03:11,120
followed by a space
7633
05:03:11,120 --> 05:03:15,120
give a comma i'll say sum
7634
05:03:15,920 --> 05:03:18,160
then
7635
05:03:19,760 --> 05:03:20,840
i'll
7636
05:03:20,840 --> 05:03:22,560
say
7637
05:03:22,560 --> 05:03:25,120
average
7638
05:03:25,840 --> 05:03:27,120
give a colon
7639
05:03:27,120 --> 05:03:28,958
followed by a space
7640
05:03:28,958 --> 05:03:31,920
and i'll pass in the variable avg
7641
05:03:31,920 --> 05:03:33,040
alright
7642
05:03:33,040 --> 05:03:36,240
so here i have my
7643
05:03:36,240 --> 05:03:38,560
program ready where we are using a while
7644
05:03:38,560 --> 05:03:40,320
loop and
7645
05:03:40,320 --> 05:03:42,000
an if statement
7646
05:03:42,000 --> 05:03:42,798
to
7647
05:03:42,798 --> 05:03:44,878
take numbers as input from the user and
7648
05:03:44,878 --> 05:03:46,878
calculate the average as long as the
7649
05:03:46,878 --> 05:03:49,520
user enters a positive number
7650
05:03:49,520 --> 05:03:51,520
so let me run it
7651
05:03:51,520 --> 05:03:54,638
okay it says enter a positive number
7652
05:03:54,638 --> 05:03:57,520
i'll say let's say five
7653
05:03:57,520 --> 05:03:59,600
again it's asking for a positive number
7654
05:03:59,600 --> 05:04:01,440
i'll say
7655
05:04:01,440 --> 05:04:03,120
seven
7656
05:04:03,120 --> 05:04:04,878
let's say 10
7657
05:04:04,878 --> 05:04:06,958
and this time i'm going to enter a
7658
05:04:06,958 --> 05:04:09,840
negative number so i'll say minus
7659
05:04:09,840 --> 05:04:12,240
6.
7660
05:04:12,958 --> 05:04:15,520
if i run it you can see here
7661
05:04:15,520 --> 05:04:17,440
the total sum of numbers
7662
05:04:17,440 --> 05:04:22,160
5 plus 7 plus 10 is 22
7663
05:04:22,160 --> 05:04:24,638
and the average is 22 divided by 3 which
7664
05:04:24,638 --> 05:04:27,040
is 7.33
7665
05:04:27,040 --> 05:04:29,280
all right
7666
05:04:29,280 --> 05:04:30,798
let me give a space here for more
7667
05:04:30,798 --> 05:04:33,120
readability
7668
05:04:33,120 --> 05:04:35,440
we are going to try it again
7669
05:04:35,440 --> 05:04:37,760
i'll say 40
7670
05:04:37,760 --> 05:04:39,520
50
7671
05:04:39,520 --> 05:04:40,798
30
7672
05:04:40,798 --> 05:04:41,680
and
7673
05:04:41,680 --> 05:04:43,840
let's say 100
7674
05:04:43,840 --> 05:04:45,680
this time i'll enter a negative number
7675
05:04:45,680 --> 05:04:47,840
let's say minus 90.
7676
05:04:47,840 --> 05:04:50,878
okay so you can see here
7677
05:04:50,878 --> 05:04:53,680
i have entered four positive numbers 40
7678
05:04:53,680 --> 05:04:55,680
50 30 and 100
7679
05:04:55,680 --> 05:04:58,480
so the sum of numbers is 220
7680
05:04:58,480 --> 05:05:02,480
and the average is 40 plus 50 plus 30
7681
05:05:02,480 --> 05:05:05,520
plus 100 which is 220 divided by 4 which
7682
05:05:05,520 --> 05:05:07,840
is 55
7683
05:05:07,840 --> 05:05:09,440
cool
7684
05:05:09,440 --> 05:05:12,240
now moving on to the last
7685
05:05:12,240 --> 05:05:15,120
demo in this while loop with python
7686
05:05:15,120 --> 05:05:17,040
tutorial
7687
05:05:17,040 --> 05:05:19,280
so here we are going to
7688
05:05:19,280 --> 05:05:23,280
guess a number so the user is going to
7689
05:05:23,280 --> 05:05:26,080
give a number between a certain range
7690
05:05:26,080 --> 05:05:28,798
and based on a random input generated
7691
05:05:28,798 --> 05:05:30,240
from the program
7692
05:05:30,240 --> 05:05:32,000
we are going to guess
7693
05:05:32,000 --> 05:05:34,160
the random number
7694
05:05:34,160 --> 05:05:35,520
all right
7695
05:05:35,520 --> 05:05:36,878
so for that
7696
05:05:36,878 --> 05:05:38,878
i am going to start by importing my
7697
05:05:38,878 --> 05:05:40,840
library called
7698
05:05:40,840 --> 05:05:43,280
random i am going to
7699
05:05:43,280 --> 05:05:44,560
say n
7700
05:05:44,560 --> 05:05:45,680
equal to
7701
05:05:45,680 --> 05:05:46,718
random
7702
05:05:46,718 --> 05:05:48,160
dot
7703
05:05:48,160 --> 05:05:50,798
rand int which is a function to generate
7704
05:05:50,798 --> 05:05:52,718
random integers i want to generate
7705
05:05:52,718 --> 05:05:54,320
between 1
7706
05:05:54,320 --> 05:05:56,798
and 100
7707
05:05:56,798 --> 05:06:01,040
let's go ahead and print that number n
7708
05:06:01,040 --> 05:06:03,280
then
7709
05:06:03,600 --> 05:06:06,958
i'll use a variable called guess
7710
05:06:06,958 --> 05:06:09,040
which is going to be
7711
05:06:09,040 --> 05:06:10,878
a user input
7712
05:06:10,878 --> 05:06:14,878
so i'll say input which is my function
7713
05:06:14,878 --> 05:06:18,798
and i'll give the message enter an
7714
05:06:18,798 --> 05:06:21,200
integer
7715
05:06:21,200 --> 05:06:22,798
between
7716
05:06:22,798 --> 05:06:27,440
1 to let's say 100
7717
05:06:27,440 --> 05:06:29,760
okay
7718
05:06:30,080 --> 05:06:32,400
then i'll start my while loop saying
7719
05:06:32,400 --> 05:06:33,680
while
7720
05:06:33,680 --> 05:06:35,280
n
7721
05:06:35,280 --> 05:06:37,600
not equal to
7722
05:06:37,600 --> 05:06:39,840
my
7723
05:06:42,320 --> 05:06:44,240
variable that is
7724
05:06:44,240 --> 05:06:46,080
guess
7725
05:06:46,080 --> 05:06:48,718
i'll say if
7726
05:06:48,718 --> 05:06:50,958
my
7727
05:06:50,958 --> 05:06:54,560
guest number is less than n
7728
05:06:54,560 --> 05:06:56,638
i'll print the
7729
05:06:56,638 --> 05:06:59,040
message as
7730
05:06:59,040 --> 05:07:01,360
your
7731
05:07:03,120 --> 05:07:06,120
guess
7732
05:07:06,718 --> 05:07:09,840
is let's say low
7733
05:07:10,480 --> 05:07:13,680
then again i'll say guess equal to
7734
05:07:13,680 --> 05:07:15,760
i'll take the
7735
05:07:15,760 --> 05:07:18,560
user input
7736
05:07:18,560 --> 05:07:20,638
just copy this message
7737
05:07:20,638 --> 05:07:23,920
and we'll print it here
7738
05:07:24,080 --> 05:07:26,320
okay
7739
05:07:26,320 --> 05:07:28,320
now
7740
05:07:28,320 --> 05:07:30,160
i'll say
7741
05:07:30,160 --> 05:07:33,160
alif
7742
05:07:35,840 --> 05:07:37,040
if my
7743
05:07:37,040 --> 05:07:39,440
guess is
7744
05:07:39,440 --> 05:07:43,200
more than my random integer
7745
05:07:43,200 --> 05:07:46,840
will print i'll just copy
7746
05:07:46,840 --> 05:07:49,920
this we are going to print
7747
05:07:49,920 --> 05:07:51,280
your guesses
7748
05:07:51,280 --> 05:07:53,840
let's say hi
7749
05:07:55,600 --> 05:07:58,400
then we are again going to
7750
05:07:58,400 --> 05:07:59,440
ask the
7751
05:07:59,440 --> 05:08:00,840
user
7752
05:08:00,840 --> 05:08:02,400
to
7753
05:08:02,400 --> 05:08:04,718
guess a number between
7754
05:08:04,718 --> 05:08:07,520
1 to 100
7755
05:08:08,000 --> 05:08:11,840
and after that i'll say
7756
05:08:12,320 --> 05:08:14,638
else
7757
05:08:14,958 --> 05:08:17,120
will print
7758
05:08:17,120 --> 05:08:19,360
you
7759
05:08:20,638 --> 05:08:24,160
guessed it correctly
7760
05:08:24,878 --> 05:08:26,480
then we'll
7761
05:08:26,480 --> 05:08:29,040
break out of the loop
7762
05:08:29,040 --> 05:08:30,958
and finally i'll just
7763
05:08:30,958 --> 05:08:32,400
print it
7764
05:08:32,400 --> 05:08:33,280
okay
7765
05:08:33,280 --> 05:08:36,160
so what we are doing in this game is
7766
05:08:36,160 --> 05:08:39,840
before that let me just give a
7767
05:08:40,638 --> 05:08:44,000
comment as guessing game in python
7768
05:08:44,000 --> 05:08:45,600
so first of all we are generating a
7769
05:08:45,600 --> 05:08:47,840
random integer using the library called
7770
05:08:47,840 --> 05:08:50,160
random so the integer
7771
05:08:50,160 --> 05:08:52,560
will lie between one and hundred
7772
05:08:52,560 --> 05:08:55,680
i am going to print that integer
7773
05:08:55,680 --> 05:08:58,480
after that i am taking a user input
7774
05:08:58,480 --> 05:09:00,240
so i am asking the user to enter an
7775
05:09:00,240 --> 05:09:02,878
integer between 1 to 100 and i am
7776
05:09:02,878 --> 05:09:06,160
checking if my
7777
05:09:06,400 --> 05:09:08,000
random integer
7778
05:09:08,000 --> 05:09:11,280
is not equal to my
7779
05:09:11,280 --> 05:09:14,320
user input that is guess
7780
05:09:14,320 --> 05:09:16,080
and if that guess
7781
05:09:16,080 --> 05:09:18,080
is less than my random integer i am
7782
05:09:18,080 --> 05:09:20,400
going to print your guess is low
7783
05:09:20,400 --> 05:09:22,718
then i am again asking the user to input
7784
05:09:22,718 --> 05:09:25,280
an integer between 100
7785
05:09:25,280 --> 05:09:27,040
then i'm saying if the
7786
05:09:27,040 --> 05:09:28,240
guest
7787
05:09:28,240 --> 05:09:31,280
value is more than the generated random
7788
05:09:31,280 --> 05:09:32,400
integer
7789
05:09:32,400 --> 05:09:35,200
will print your guesses high
7790
05:09:35,200 --> 05:09:37,280
then after that i'm again asking the
7791
05:09:37,280 --> 05:09:40,958
user to input an integer
7792
05:09:40,958 --> 05:09:42,798
and in case
7793
05:09:42,798 --> 05:09:44,798
the random integer
7794
05:09:44,798 --> 05:09:46,480
matches with the
7795
05:09:46,480 --> 05:09:48,638
guest number will print you guessed it
7796
05:09:48,638 --> 05:09:49,920
correctly
7797
05:09:49,920 --> 05:09:53,040
all right let's try this out
7798
05:09:53,040 --> 05:09:55,920
okay so here you can see
7799
05:09:55,920 --> 05:09:59,840
so my program generated a random integer
7800
05:09:59,840 --> 05:10:03,120
48 that lies between 100
7801
05:10:03,120 --> 05:10:04,080
so
7802
05:10:04,080 --> 05:10:06,798
our loop will only stop executing when
7803
05:10:06,798 --> 05:10:07,600
we
7804
05:10:07,600 --> 05:10:08,718
enter our
7805
05:10:08,718 --> 05:10:10,878
number as 48
7806
05:10:10,878 --> 05:10:12,718
otherwise it will keep on running and
7807
05:10:12,718 --> 05:10:13,840
asking you
7808
05:10:13,840 --> 05:10:15,680
to enter an integer
7809
05:10:15,680 --> 05:10:17,440
let's say i enter
7810
05:10:17,440 --> 05:10:19,920
50.
7811
05:10:20,240 --> 05:10:22,638
you see here
7812
05:10:22,638 --> 05:10:25,840
your guest value that is 50 is
7813
05:10:25,840 --> 05:10:27,360
more than
7814
05:10:27,360 --> 05:10:29,920
the random integer 48 so we have the
7815
05:10:29,920 --> 05:10:30,958
message
7816
05:10:30,958 --> 05:10:33,040
your guess is high
7817
05:10:33,040 --> 05:10:36,320
let's say i'll say 30.
7818
05:10:36,320 --> 05:10:39,280
this time my guess is low you can see it
7819
05:10:39,280 --> 05:10:40,798
here
7820
05:10:40,798 --> 05:10:43,520
let's see i'll
7821
05:10:44,560 --> 05:10:45,760
take my
7822
05:10:45,760 --> 05:10:48,320
number as
7823
05:10:48,840 --> 05:10:51,680
300. you see here
7824
05:10:51,680 --> 05:10:53,440
this time my guess is high which is more
7825
05:10:53,440 --> 05:10:55,200
than 48
7826
05:10:55,200 --> 05:10:58,080
but if i say
7827
05:10:58,320 --> 05:11:01,760
my user input to be 48 and if i
7828
05:11:01,760 --> 05:11:02,840
hit
7829
05:11:02,840 --> 05:11:06,000
enter there you go we have our message
7830
05:11:06,000 --> 05:11:08,638
printed you guessed it correctly
7831
05:11:08,638 --> 05:11:10,080
all right
7832
05:11:10,080 --> 05:11:12,400
choose from over 300 in-demand skills
7833
05:11:12,400 --> 05:11:14,958
and get access to 1000 plus hours of
7834
05:11:14,958 --> 05:11:17,600
video content for free visit skill up by
7835
05:11:17,600 --> 05:11:19,520
simply learn click on the link in the
7836
05:11:19,520 --> 05:11:21,440
description to know more
7837
05:11:21,440 --> 05:11:23,760
what is a nested loop
7838
05:11:23,760 --> 05:11:25,600
python programming language allows you
7839
05:11:25,600 --> 05:11:28,160
to use one loop inside another loop
7840
05:11:28,160 --> 05:11:30,240
this is known as a nested loop it is
7841
05:11:30,240 --> 05:11:31,840
present inside the body of the outer
7842
05:11:31,840 --> 05:11:32,958
loop
7843
05:11:32,958 --> 05:11:35,200
the inner or outer loop can be a for
7844
05:11:35,200 --> 05:11:37,120
loop or a while loop
7845
05:11:37,120 --> 05:11:39,120
the outer for loop can contain a while
7846
05:11:39,120 --> 05:11:41,280
loop or an outer while loop can contain
7847
05:11:41,280 --> 05:11:43,600
a for loop inside it in python nested
7848
05:11:43,600 --> 05:11:45,680
loops the outer loop can have more than
7849
05:11:45,680 --> 05:11:48,000
one inner loop there is no limitation on
7850
05:11:48,000 --> 05:11:50,160
the chaining of loops
7851
05:11:50,160 --> 05:11:51,840
now you can see on your screens the
7852
05:11:51,840 --> 05:11:54,080
basic syntax of writing a nested loop in
7853
05:11:54,080 --> 05:11:55,120
python
7854
05:11:55,120 --> 05:11:58,160
you have the outer loop followed by the
7855
05:11:58,160 --> 05:12:00,080
inner loop and the statements inside the
7856
05:12:00,080 --> 05:12:02,560
inner loop and finally you have the
7857
05:12:02,560 --> 05:12:04,638
statements of the outer loop the last
7858
05:12:04,638 --> 05:12:06,560
three lines consist of the body of the
7859
05:12:06,560 --> 05:12:08,480
outer loop
7860
05:12:08,480 --> 05:12:10,080
now talking about the various ways of
7861
05:12:10,080 --> 05:12:11,120
writing
7862
05:12:11,120 --> 05:12:13,120
nested loops in python
7863
05:12:13,120 --> 05:12:15,360
so with python you can write a for loop
7864
05:12:15,360 --> 05:12:16,320
inside
7865
05:12:16,320 --> 05:12:18,560
another for loop or
7866
05:12:18,560 --> 05:12:20,080
you can have a while loop declared
7867
05:12:20,080 --> 05:12:22,160
inside another while loop you can see it
7868
05:12:22,160 --> 05:12:23,280
here
7869
05:12:23,280 --> 05:12:25,360
so using python you can use both for
7870
05:12:25,360 --> 05:12:27,440
loops and while loops together
7871
05:12:27,440 --> 05:12:29,520
the outer loop can be a for loop and the
7872
05:12:29,520 --> 05:12:31,440
inner loop can be a while loop you can
7873
05:12:31,440 --> 05:12:33,360
also write nested loops having a while
7874
05:12:33,360 --> 05:12:36,000
loop outside and for loop inside so now
7875
05:12:36,000 --> 05:12:37,520
that you have learnt about the basics of
7876
05:12:37,520 --> 05:12:39,680
nested loops let's look at an example of
7877
05:12:39,680 --> 05:12:41,520
printing patterns
7878
05:12:41,520 --> 05:12:43,120
so in the code here
7879
05:12:43,120 --> 05:12:44,480
we have used
7880
05:12:44,480 --> 05:12:46,560
two for loops to get our
7881
05:12:46,560 --> 05:12:48,400
right angle triangle containing
7882
05:12:48,400 --> 05:12:51,360
asterisks or stars
7883
05:12:51,360 --> 05:12:52,160
so
7884
05:12:52,160 --> 05:12:54,560
we have declared rho equal to 7 which
7885
05:12:54,560 --> 05:12:57,120
means we need seven rows to be printed
7886
05:12:57,120 --> 05:12:58,638
you can count it here these are seven
7887
05:12:58,638 --> 05:13:00,000
rows
7888
05:13:00,000 --> 05:13:02,080
hence we have defined it as
7889
05:13:02,080 --> 05:13:03,600
rho equal to seven
7890
05:13:03,600 --> 05:13:05,520
now the outer for loop ensures we have a
7891
05:13:05,520 --> 05:13:07,280
total of seven rows
7892
05:13:07,280 --> 05:13:09,280
so we have defined a range
7893
05:13:09,280 --> 05:13:10,958
from
7894
05:13:10,958 --> 05:13:14,560
1 to row plus 1 that is 8. now the inner
7895
05:13:14,560 --> 05:13:16,320
loop is the total number of columns in
7896
05:13:16,320 --> 05:13:17,520
each row
7897
05:13:17,520 --> 05:13:19,600
so for each iteration of the outer loop
7898
05:13:19,600 --> 05:13:20,480
the
7899
05:13:20,480 --> 05:13:23,600
columns count gets incremented by 1.
7900
05:13:23,600 --> 05:13:25,280
so for the first titration of the outer
7901
05:13:25,280 --> 05:13:27,360
loop the column count is 1 in the next
7902
05:13:27,360 --> 05:13:30,080
iteration it is 2 and so on
7903
05:13:30,080 --> 05:13:32,000
the inner loop iteration is equal to the
7904
05:13:32,000 --> 05:13:34,480
count of the columns and for each
7905
05:13:34,480 --> 05:13:35,600
iteration
7906
05:13:35,600 --> 05:13:38,798
in an inner loop we print a star
7907
05:13:38,798 --> 05:13:40,878
so for the first iteration when i value
7908
05:13:40,878 --> 05:13:43,840
is 0 it will first print a blank
7909
05:13:43,840 --> 05:13:46,718
when i value becomes 1 then your j range
7910
05:13:46,718 --> 05:13:50,160
becomes 1 comma 1 plus 1 which is 2 so
7911
05:13:50,160 --> 05:13:51,360
it will print
7912
05:13:51,360 --> 05:13:54,560
1 star here followed by a space
7913
05:13:54,560 --> 05:13:57,760
when i value becomes 2 your j range
7914
05:13:57,760 --> 05:14:00,560
becomes 1 comma 3 so it will print
7915
05:14:00,560 --> 05:14:01,920
2 stars
7916
05:14:01,920 --> 05:14:05,280
then again when i value becomes 3 your
7917
05:14:05,280 --> 05:14:07,600
range becomes one comma four so it will
7918
05:14:07,600 --> 05:14:12,080
print three stars so so on and so forth
7919
05:14:12,080 --> 05:14:13,440
now let's see
7920
05:14:13,440 --> 05:14:15,200
when you should use a nested loop in
7921
05:14:15,200 --> 05:14:17,440
python so nested loops are mostly used
7922
05:14:17,440 --> 05:14:19,360
to work with multi-dimensional data
7923
05:14:19,360 --> 05:14:20,958
structures such as printing
7924
05:14:20,958 --> 05:14:22,878
two-dimensional arrays
7925
05:14:22,878 --> 05:14:25,120
i treating a list that contains a nested
7926
05:14:25,120 --> 05:14:26,718
list etc
7927
05:14:26,718 --> 05:14:28,878
you can use python nested loops to print
7928
05:14:28,878 --> 05:14:30,638
different star or number patterns as
7929
05:14:30,638 --> 05:14:32,080
well now let's
7930
05:14:32,080 --> 05:14:33,760
head to our jupyter notebook to do some
7931
05:14:33,760 --> 05:14:35,440
practical hands-on demonstrations to
7932
05:14:35,440 --> 05:14:38,080
understand nested loops in python better
7933
05:14:38,080 --> 05:14:40,320
okay you can see here i am on my jupyter
7934
05:14:40,320 --> 05:14:42,798
notebook first of all i'll
7935
05:14:42,798 --> 05:14:44,560
rename my
7936
05:14:44,560 --> 05:14:47,600
notebook as nested loops
7937
05:14:47,600 --> 05:14:49,120
in
7938
05:14:49,120 --> 05:14:51,920
python demo
7939
05:14:53,120 --> 05:14:54,560
click on rename
7940
05:14:54,560 --> 05:14:55,680
all right
7941
05:14:55,680 --> 05:14:58,560
now let's see our first example
7942
05:14:58,560 --> 05:15:00,240
so here
7943
05:15:00,240 --> 05:15:01,840
we are going to print the items in a
7944
05:15:01,840 --> 05:15:04,240
list one by one but each letter in an
7945
05:15:04,240 --> 05:15:07,200
item should be separated by the at sign
7946
05:15:07,200 --> 05:15:09,120
which is basically the add the rate
7947
05:15:09,120 --> 05:15:12,080
symbol that you use in your email ids
7948
05:15:12,080 --> 05:15:13,840
all right
7949
05:15:13,840 --> 05:15:15,280
and for
7950
05:15:15,280 --> 05:15:18,080
the first demo we'll be using nested for
7951
05:15:18,080 --> 05:15:19,040
loop
7952
05:15:19,040 --> 05:15:21,040
so let's start
7953
05:15:21,040 --> 05:15:24,080
i'll create a list
7954
05:15:24,560 --> 05:15:25,920
which will
7955
05:15:25,920 --> 05:15:28,320
have the names of the
7956
05:15:28,320 --> 05:15:29,360
fruits
7957
05:15:29,360 --> 05:15:31,840
let's say we have
7958
05:15:31,840 --> 05:15:33,360
mango
7959
05:15:33,360 --> 05:15:36,160
we'll give a comma
7960
05:15:36,320 --> 05:15:39,360
let's say i have apple
7961
05:15:39,440 --> 05:15:41,920
grapes
7962
05:15:42,320 --> 05:15:45,840
and let's say we have banana
7963
05:15:46,000 --> 05:15:48,080
all right
7964
05:15:48,080 --> 05:15:50,480
now i'll
7965
05:15:50,480 --> 05:15:53,280
create my outer loop so i'll say
7966
05:15:53,280 --> 05:15:54,560
for
7967
05:15:54,560 --> 05:15:55,920
fruit
7968
05:15:55,920 --> 05:15:58,080
now this variable can be any valid
7969
05:15:58,080 --> 05:16:00,080
python variable this is basically known
7970
05:16:00,080 --> 05:16:02,958
as an iterator on an iterator variable
7971
05:16:02,958 --> 05:16:04,878
so i'll say for fruit
7972
05:16:04,878 --> 05:16:06,718
in
7973
05:16:06,718 --> 05:16:10,160
my list which is list underscore fruits
7974
05:16:10,160 --> 05:16:12,400
that i have declared in my
7975
05:16:12,400 --> 05:16:14,320
first line
7976
05:16:14,320 --> 05:16:15,920
i'll give a
7977
05:16:15,920 --> 05:16:18,320
colon
7978
05:16:18,320 --> 05:16:20,718
and then
7979
05:16:21,200 --> 05:16:26,958
declare my inner loop i'll say for i in
7980
05:16:26,958 --> 05:16:29,040
my variable or the iterator that i had
7981
05:16:29,040 --> 05:16:31,360
declared in the previous line which is
7982
05:16:31,360 --> 05:16:33,520
fruit
7983
05:16:33,520 --> 05:16:34,958
i'll say
7984
05:16:34,958 --> 05:16:37,360
print
7985
05:16:37,760 --> 05:16:39,440
i
7986
05:16:39,440 --> 05:16:40,400
and then
7987
05:16:40,400 --> 05:16:41,840
i'll use
7988
05:16:41,840 --> 05:16:45,280
the end parameter that will have
7989
05:16:45,280 --> 05:16:47,920
the add sign
7990
05:16:47,920 --> 05:16:49,840
and then
7991
05:16:49,840 --> 05:16:51,840
i'll print
7992
05:16:51,840 --> 05:16:53,520
a blank
7993
05:16:53,520 --> 05:16:55,840
so this is my first
7994
05:16:55,840 --> 05:16:58,000
nested loop that i have created so i
7995
05:16:58,000 --> 05:17:00,320
want to i trade through all the items
7996
05:17:00,320 --> 05:17:03,040
present in my list underscore
7997
05:17:03,040 --> 05:17:04,718
fruits
7998
05:17:04,718 --> 05:17:06,400
variable
7999
05:17:06,400 --> 05:17:09,040
and for each item
8000
05:17:09,040 --> 05:17:11,760
in the list i want to make sure
8001
05:17:11,760 --> 05:17:13,520
that each letter in an item should be
8002
05:17:13,520 --> 05:17:16,080
separated with the add sign
8003
05:17:16,080 --> 05:17:18,718
so let me go ahead and print it so that
8004
05:17:18,718 --> 05:17:21,440
you can see the result the hugo
8005
05:17:21,440 --> 05:17:24,080
so here you can see we have printed the
8006
05:17:24,080 --> 05:17:26,480
different items that were present in my
8007
05:17:26,480 --> 05:17:29,200
list which was list underscore fruits
8008
05:17:29,200 --> 05:17:30,080
and
8009
05:17:30,080 --> 05:17:32,080
for each item
8010
05:17:32,080 --> 05:17:33,520
the letters
8011
05:17:33,520 --> 05:17:35,440
in the item have been separated by the
8012
05:17:35,440 --> 05:17:38,160
add sign you can see here
8013
05:17:38,160 --> 05:17:39,200
all right
8014
05:17:39,200 --> 05:17:41,920
if you want you can change this symbol
8015
05:17:41,920 --> 05:17:44,080
to let's say
8016
05:17:44,080 --> 05:17:46,240
star
8017
05:17:46,240 --> 05:17:48,080
if i print it
8018
05:17:48,080 --> 05:17:50,798
you can see the difference in the result
8019
05:17:50,798 --> 05:17:53,200
now we'll see our second example where
8020
05:17:53,200 --> 05:17:55,120
we are going to print the values of
8021
05:17:55,120 --> 05:17:56,240
items
8022
05:17:56,240 --> 05:17:59,120
in two lists together
8023
05:17:59,120 --> 05:18:01,120
so let me create a
8024
05:18:01,120 --> 05:18:03,200
list called
8025
05:18:03,200 --> 05:18:06,480
color that will
8026
05:18:06,480 --> 05:18:10,320
have the different colors such as red
8027
05:18:10,320 --> 05:18:12,240
we have green
8028
05:18:12,240 --> 05:18:15,200
and let's say blue
8029
05:18:15,520 --> 05:18:18,160
and the next list i'm going to create
8030
05:18:18,160 --> 05:18:20,958
will have three items
8031
05:18:20,958 --> 05:18:22,878
the first item is
8032
05:18:22,878 --> 05:18:25,200
apple
8033
05:18:25,200 --> 05:18:27,120
then we have
8034
05:18:27,120 --> 05:18:29,200
veggies
8035
05:18:29,200 --> 05:18:30,718
and then we have
8036
05:18:30,718 --> 05:18:33,120
shirt
8037
05:18:33,120 --> 05:18:35,360
now
8038
05:18:35,920 --> 05:18:38,560
now to print the values of the items in
8039
05:18:38,560 --> 05:18:40,718
these two lists together i'm going to
8040
05:18:40,718 --> 05:18:42,798
use two for loops again
8041
05:18:42,798 --> 05:18:44,878
so i'll say for
8042
05:18:44,878 --> 05:18:49,280
my iterator variable let's say x in
8043
05:18:49,280 --> 05:18:50,560
my
8044
05:18:50,560 --> 05:18:53,840
first list which is color
8045
05:18:53,840 --> 05:18:56,000
so i've declared my outer loop and now
8046
05:18:56,000 --> 05:18:58,320
i'll say for
8047
05:18:58,320 --> 05:19:00,878
another iterator variable let's say y in
8048
05:19:00,878 --> 05:19:04,480
my second list items
8049
05:19:04,878 --> 05:19:07,520
i'm going to print
8050
05:19:07,520 --> 05:19:08,320
x
8051
05:19:08,320 --> 05:19:11,120
comma y
8052
05:19:11,120 --> 05:19:13,120
and after that
8053
05:19:13,120 --> 05:19:14,638
will
8054
05:19:14,638 --> 05:19:17,040
print a space
8055
05:19:17,040 --> 05:19:19,520
so first of all
8056
05:19:19,520 --> 05:19:22,000
my outer for loop will
8057
05:19:22,000 --> 05:19:24,718
iterate through the items
8058
05:19:24,718 --> 05:19:27,520
that are present inside
8059
05:19:27,520 --> 05:19:28,878
color list
8060
05:19:28,878 --> 05:19:31,280
and for each color we are going to
8061
05:19:31,280 --> 05:19:33,440
iterate through
8062
05:19:33,440 --> 05:19:36,400
the values that are present inside
8063
05:19:36,400 --> 05:19:38,240
items list
8064
05:19:38,240 --> 05:19:41,040
now let me go ahead and print it
8065
05:19:41,040 --> 05:19:43,600
you can see it here
8066
05:19:43,600 --> 05:19:46,080
first we are printing red apple red
8067
05:19:46,080 --> 05:19:49,040
veggies and red shirt then we have green
8068
05:19:49,040 --> 05:19:51,760
apple green veggies and green shirt
8069
05:19:51,760 --> 05:19:54,240
for scroll down you can see blue apple
8070
05:19:54,240 --> 05:19:57,200
blue veggies and blue shirt
8071
05:19:57,200 --> 05:19:59,040
okay
8072
05:19:59,040 --> 05:20:01,520
now coming to our third example
8073
05:20:01,520 --> 05:20:04,160
here we are going to print a right angle
8074
05:20:04,160 --> 05:20:05,520
triangle with
8075
05:20:05,520 --> 05:20:08,400
10 rows we have seen a similar example
8076
05:20:08,400 --> 05:20:11,760
in the slice where there were seven rows
8077
05:20:11,760 --> 05:20:13,280
now we are going to print a right angle
8078
05:20:13,280 --> 05:20:15,600
triangle with 10 rows
8079
05:20:15,600 --> 05:20:18,400
i'll give a comment
8080
05:20:18,400 --> 05:20:19,440
let's say
8081
05:20:19,440 --> 05:20:21,680
print
8082
05:20:21,680 --> 05:20:25,000
right triangle
8083
05:20:25,120 --> 05:20:26,718
okay
8084
05:20:26,718 --> 05:20:29,200
so i'll start my
8085
05:20:29,200 --> 05:20:31,840
for loop i'll say for
8086
05:20:31,840 --> 05:20:32,718
any
8087
05:20:32,718 --> 05:20:35,760
iterator variable let's say i in
8088
05:20:35,760 --> 05:20:36,798
range
8089
05:20:36,798 --> 05:20:39,920
i'll pass my range as 11 so it will
8090
05:20:39,920 --> 05:20:42,240
go from
8091
05:20:42,240 --> 05:20:44,558
0 to
8092
05:20:44,558 --> 05:20:46,000
10
8093
05:20:46,000 --> 05:20:48,000
then i'll say for
8094
05:20:48,000 --> 05:20:48,798
j
8095
05:20:48,798 --> 05:20:50,840
in
8096
05:20:50,840 --> 05:20:52,958
range i
8097
05:20:52,958 --> 05:20:54,638
so this will
8098
05:20:54,638 --> 05:20:57,040
take the number of columns
8099
05:20:57,040 --> 05:21:00,000
i'll say print
8100
05:21:00,000 --> 05:21:02,480
within double quotes i'll give my star
8101
05:21:02,480 --> 05:21:04,878
because i want to print a star pattern
8102
05:21:04,878 --> 05:21:07,440
for the triangle
8103
05:21:07,440 --> 05:21:09,840
and i'll end with a
8104
05:21:09,840 --> 05:21:11,680
space
8105
05:21:11,680 --> 05:21:13,680
then i'll say
8106
05:21:13,680 --> 05:21:16,080
print
8107
05:21:16,558 --> 05:21:17,680
blank
8108
05:21:17,680 --> 05:21:21,200
so there won't be any space between
8109
05:21:21,200 --> 05:21:23,280
the different rows
8110
05:21:23,280 --> 05:21:26,480
now let's print the result there you go
8111
05:21:26,480 --> 05:21:28,080
so here you can see we have a right
8112
05:21:28,080 --> 05:21:30,320
angle triangle of 10 rows you can count
8113
05:21:30,320 --> 05:21:31,520
the
8114
05:21:31,520 --> 05:21:33,440
number of rows and also we have the
8115
05:21:33,440 --> 05:21:36,480
number of columns as 10.
8116
05:21:36,480 --> 05:21:38,958
so when i value is
8117
05:21:38,958 --> 05:21:41,520
one your g range also becomes one so we
8118
05:21:41,520 --> 05:21:44,000
print one star then when i value is two
8119
05:21:44,000 --> 05:21:46,400
we are going to print two stars when i
8120
05:21:46,400 --> 05:21:47,920
value is three we are going to print 3
8121
05:21:47,920 --> 05:21:49,200
stars
8122
05:21:49,200 --> 05:21:50,718
and
8123
05:21:50,718 --> 05:21:52,480
we are using the end parameter as a
8124
05:21:52,480 --> 05:21:54,000
space
8125
05:21:54,000 --> 05:21:56,240
ok
8126
05:21:56,718 --> 05:21:58,638
now let's go ahead and
8127
05:21:58,638 --> 05:22:00,240
print a right angle triangle using a
8128
05:22:00,240 --> 05:22:03,360
nested while loop so unlike the for loop
8129
05:22:03,360 --> 05:22:05,200
the while loop
8130
05:22:05,200 --> 05:22:07,440
doesn't have a pre-compiled iterable
8131
05:22:07,440 --> 05:22:08,798
sequence
8132
05:22:08,798 --> 05:22:10,958
the while loop keeps executing the code
8133
05:22:10,958 --> 05:22:14,000
until the expression evaluates to true
8134
05:22:14,000 --> 05:22:14,840
so
8135
05:22:14,840 --> 05:22:16,638
i'll
8136
05:22:16,638 --> 05:22:18,320
start with the
8137
05:22:18,320 --> 05:22:22,480
variable i equal to 11 because i want to
8138
05:22:22,480 --> 05:22:24,320
print
8139
05:22:24,320 --> 05:22:25,840
10 rows
8140
05:22:25,840 --> 05:22:27,200
that will contain
8141
05:22:27,200 --> 05:22:28,958
my star pattern
8142
05:22:28,958 --> 05:22:31,120
now i'll declare my first while loop
8143
05:22:31,120 --> 05:22:33,760
i'll say while i is
8144
05:22:33,760 --> 05:22:36,718
greater than 0
8145
05:22:36,878 --> 05:22:38,798
so my loop will continue
8146
05:22:38,798 --> 05:22:41,920
until i is greater than 0
8147
05:22:41,920 --> 05:22:45,600
i'll see z equal to 11
8148
05:22:45,600 --> 05:22:48,160
then i'll use my inner
8149
05:22:48,160 --> 05:22:50,638
while loop i'll say while
8150
05:22:50,638 --> 05:22:53,280
j is greater than
8151
05:22:53,280 --> 05:22:55,520
i
8152
05:22:55,760 --> 05:22:56,878
will
8153
05:22:56,878 --> 05:22:59,200
print
8154
05:22:59,200 --> 05:23:01,600
star
8155
05:23:02,400 --> 05:23:04,798
and i'll use my
8156
05:23:04,798 --> 05:23:08,558
end parameter as space again
8157
05:23:08,558 --> 05:23:11,760
then we are going to decrement
8158
05:23:11,760 --> 05:23:12,958
j by
8159
05:23:12,958 --> 05:23:15,040
1
8160
05:23:15,040 --> 05:23:18,080
i'll come out of the inner while loop
8161
05:23:18,080 --> 05:23:21,280
and i'll say i equal to i minus 1 so we
8162
05:23:21,280 --> 05:23:23,120
are also decrementing
8163
05:23:23,120 --> 05:23:24,558
the first
8164
05:23:24,558 --> 05:23:28,958
variable i by one and now i'll say print
8165
05:23:28,958 --> 05:23:30,240
okay
8166
05:23:30,240 --> 05:23:32,558
so let me run this
8167
05:23:32,558 --> 05:23:35,520
you can see it here we have the same
8168
05:23:35,520 --> 05:23:37,280
pattern printed
8169
05:23:37,280 --> 05:23:39,280
earlier we saw this using
8170
05:23:39,280 --> 05:23:41,440
two for loops and now we have created
8171
05:23:41,440 --> 05:23:43,040
the same pattern using
8172
05:23:43,040 --> 05:23:45,040
two while loops
8173
05:23:45,040 --> 05:23:47,280
all right
8174
05:23:47,280 --> 05:23:48,480
now
8175
05:23:48,480 --> 05:23:50,160
moving to our
8176
05:23:50,160 --> 05:23:52,480
next example where we'll see how to
8177
05:23:52,480 --> 05:23:53,680
append
8178
05:23:53,680 --> 05:23:56,320
tool list so first of all i am going to
8179
05:23:56,320 --> 05:23:57,520
declare
8180
05:23:57,520 --> 05:23:59,440
list 1 and list 2
8181
05:23:59,440 --> 05:24:01,280
and we are going to append the elements
8182
05:24:01,280 --> 05:24:03,120
of both the list into an
8183
05:24:03,120 --> 05:24:06,320
empty list called result
8184
05:24:06,320 --> 05:24:09,120
so i'll give a comment
8185
05:24:09,120 --> 05:24:10,160
append
8186
05:24:10,160 --> 05:24:10,958
two
8187
05:24:10,958 --> 05:24:12,878
lists
8188
05:24:12,878 --> 05:24:14,080
okay
8189
05:24:14,080 --> 05:24:16,000
so first i'll declare my
8190
05:24:16,000 --> 05:24:17,360
list one
8191
05:24:17,360 --> 05:24:18,638
now these are
8192
05:24:18,638 --> 05:24:22,400
list of numbers let's say i have
8193
05:24:22,400 --> 05:24:26,000
10 25 and 30 as my
8194
05:24:26,000 --> 05:24:28,320
elements in the first list
8195
05:24:28,320 --> 05:24:30,558
and then i'll declare my second list
8196
05:24:30,558 --> 05:24:32,878
which will have let's say 60
8197
05:24:32,878 --> 05:24:35,680
15 and 50.
8198
05:24:35,680 --> 05:24:37,120
okay
8199
05:24:37,120 --> 05:24:39,840
now i'll declare
8200
05:24:39,840 --> 05:24:42,958
an empty list called result
8201
05:24:42,958 --> 05:24:46,638
and after that i'm going to start my
8202
05:24:46,638 --> 05:24:50,718
outer for loop i'll say for i in
8203
05:24:50,718 --> 05:24:53,600
my list 1
8204
05:24:54,638 --> 05:24:57,120
then i'll say for
8205
05:24:57,120 --> 05:24:58,320
j
8206
05:24:58,320 --> 05:24:59,520
in
8207
05:24:59,520 --> 05:25:00,400
list
8208
05:25:00,400 --> 05:25:02,638
2
8209
05:25:02,878 --> 05:25:05,840
i'll say result which is my
8210
05:25:05,840 --> 05:25:08,558
empty list that i have declared
8211
05:25:08,558 --> 05:25:10,000
then i'm going to use the append
8212
05:25:10,000 --> 05:25:11,520
function that is
8213
05:25:11,520 --> 05:25:13,440
inbuilt in python
8214
05:25:13,440 --> 05:25:15,760
i'll say i plus
8215
05:25:15,760 --> 05:25:16,718
j
8216
05:25:16,718 --> 05:25:19,600
so this will make sure will append
8217
05:25:19,600 --> 05:25:22,000
the values from both the list into
8218
05:25:22,000 --> 05:25:23,600
my
8219
05:25:23,600 --> 05:25:25,520
empty list which is result
8220
05:25:25,520 --> 05:25:27,200
then i'll say
8221
05:25:27,200 --> 05:25:28,718
print
8222
05:25:28,718 --> 05:25:30,840
result
8223
05:25:30,840 --> 05:25:34,480
okay let's run it there you go
8224
05:25:34,480 --> 05:25:38,080
so what we are doing here is i am adding
8225
05:25:38,080 --> 05:25:40,558
each element in list 1 with all the
8226
05:25:40,558 --> 05:25:43,280
elements in list 2. if you see here 10
8227
05:25:43,280 --> 05:25:45,920
plus 60 is 70 then we have 10 plus 15
8228
05:25:45,920 --> 05:25:49,600
which is 25 10 plus 50 is 60. similarly
8229
05:25:49,600 --> 05:25:52,160
25 plus 60 is 85
8230
05:25:52,160 --> 05:25:55,200
25 plus 15 is 40 then
8231
05:25:55,200 --> 05:25:56,558
25 plus
8232
05:25:56,558 --> 05:26:00,000
50 is 75 and similarly for 30 plus 60 30
8233
05:26:00,000 --> 05:26:02,878
plus 15 and 30 plus 50
8234
05:26:02,878 --> 05:26:04,480
great
8235
05:26:04,480 --> 05:26:06,638
now
8236
05:26:06,638 --> 05:26:09,040
moving to our next example
8237
05:26:09,040 --> 05:26:12,000
we are going to see how you can multiply
8238
05:26:12,000 --> 05:26:13,520
the values in
8239
05:26:13,520 --> 05:26:15,440
two lists
8240
05:26:15,440 --> 05:26:18,480
so i'll give a comment
8241
05:26:18,718 --> 05:26:21,520
saying multiplying
8242
05:26:21,520 --> 05:26:24,080
two lists
8243
05:26:24,080 --> 05:26:26,160
i'll declare my first list which is list
8244
05:26:26,160 --> 05:26:28,320
one
8245
05:26:28,320 --> 05:26:32,718
with 2 comma 4 and let's say 6
8246
05:26:32,718 --> 05:26:36,400
then i'll have my list 2
8247
05:26:36,400 --> 05:26:37,360
with the
8248
05:26:37,360 --> 05:26:41,840
same elements 2 4 and 6.
8249
05:26:41,840 --> 05:26:46,638
start my outer for loop for i in list 1
8250
05:26:47,040 --> 05:26:48,320
then i'll say
8251
05:26:48,320 --> 05:26:49,120
for
8252
05:26:49,120 --> 05:26:49,840
j
8253
05:26:49,840 --> 05:26:51,040
in
8254
05:26:51,040 --> 05:26:51,920
list
8255
05:26:51,920 --> 05:26:54,160
2
8256
05:26:55,440 --> 05:26:57,680
i'm going to
8257
05:26:57,680 --> 05:26:59,280
show you another feature that you can
8258
05:26:59,280 --> 05:27:01,680
use while implementing
8259
05:27:01,680 --> 05:27:03,120
nested lists
8260
05:27:03,120 --> 05:27:04,480
that is to use
8261
05:27:04,480 --> 05:27:06,160
continue or break
8262
05:27:06,160 --> 05:27:08,958
so i'll say if i equal equal to z then
8263
05:27:08,958 --> 05:27:12,400
i'll continue
8264
05:27:13,040 --> 05:27:17,200
after that we are going to print
8265
05:27:17,520 --> 05:27:18,878
the value of
8266
05:27:18,878 --> 05:27:20,878
i
8267
05:27:20,878 --> 05:27:23,120
followed by
8268
05:27:23,120 --> 05:27:24,400
the
8269
05:27:24,400 --> 05:27:26,240
asterisk
8270
05:27:26,240 --> 05:27:27,040
then
8271
05:27:27,040 --> 05:27:29,600
the value of g
8272
05:27:29,600 --> 05:27:31,600
will give a comma
8273
05:27:31,600 --> 05:27:34,240
then within
8274
05:27:34,240 --> 05:27:37,280
single quotes i'll say equal to
8275
05:27:37,280 --> 05:27:39,280
then i'll give the operation i
8276
05:27:39,280 --> 05:27:40,638
multiplied by
8277
05:27:40,638 --> 05:27:42,400
j
8278
05:27:42,400 --> 05:27:44,878
so this is how you can multiply
8279
05:27:44,878 --> 05:27:46,878
the elements in two lists
8280
05:27:46,878 --> 05:27:50,160
so i've declared my list one my list two
8281
05:27:50,160 --> 05:27:52,480
then i'm using two for loops and
8282
05:27:52,480 --> 05:27:54,718
whenever my i value is equal to equal to
8283
05:27:54,718 --> 05:27:58,160
j will skip that step and move on to the
8284
05:27:58,160 --> 05:28:00,160
next iteration
8285
05:28:00,160 --> 05:28:03,280
let me run it and show you the result
8286
05:28:03,280 --> 05:28:06,480
so if you notice here
8287
05:28:06,480 --> 05:28:07,840
i haven't
8288
05:28:07,840 --> 05:28:09,520
multiplied
8289
05:28:09,520 --> 05:28:10,798
2
8290
05:28:10,798 --> 05:28:11,920
and 2
8291
05:28:11,920 --> 05:28:14,320
the reason being i have used this
8292
05:28:14,320 --> 05:28:15,680
if condition
8293
05:28:15,680 --> 05:28:18,718
whenever i was equal to j we continued
8294
05:28:18,718 --> 05:28:20,480
with the next iteration similarly there
8295
05:28:20,480 --> 05:28:21,520
is no
8296
05:28:21,520 --> 05:28:23,040
output for
8297
05:28:23,040 --> 05:28:26,480
4 multiplied by 4 or 6 multiplied by 6
8298
05:28:26,480 --> 05:28:28,558
but for the remaining we have our result
8299
05:28:28,558 --> 05:28:31,520
2 multiplied by 4 is 8 2 multiplied by 6
8300
05:28:31,520 --> 05:28:33,440
is 12 similarly
8301
05:28:33,440 --> 05:28:36,798
4 multiplied by 2 is 8 4 multiplied by 6
8302
05:28:36,798 --> 05:28:38,160
is 24
8303
05:28:38,160 --> 05:28:40,878
6 multiplied by 2 is 12 and 6 multiplied
8304
05:28:40,878 --> 05:28:42,240
by 4 is
8305
05:28:42,240 --> 05:28:43,840
24.
8306
05:28:43,840 --> 05:28:44,798
so
8307
05:28:44,798 --> 05:28:47,280
continue and break our two statements
8308
05:28:47,280 --> 05:28:49,280
that you can use inside nested loops in
8309
05:28:49,280 --> 05:28:50,718
python
8310
05:28:50,718 --> 05:28:52,000
okay
8311
05:28:52,000 --> 05:28:53,440
now
8312
05:28:53,440 --> 05:28:55,440
we are going to
8313
05:28:55,440 --> 05:28:57,840
write another nested loop using a while
8314
05:28:57,840 --> 05:29:00,160
loop and a for loop to check whether a
8315
05:29:00,160 --> 05:29:02,400
number is a perfect number or not
8316
05:29:02,400 --> 05:29:03,840
so basically in this program we are
8317
05:29:03,840 --> 05:29:06,000
going to print the perfect numbers
8318
05:29:06,000 --> 05:29:08,000
between 100
8319
05:29:08,000 --> 05:29:09,760
now a perfect number is a positive
8320
05:29:09,760 --> 05:29:11,680
integer that is equal to the
8321
05:29:11,680 --> 05:29:14,480
sum of its proper divisors
8322
05:29:14,480 --> 05:29:16,480
so the smallest perfect number is 6
8323
05:29:16,480 --> 05:29:19,280
which is the sum of 1 2 and 3 so these
8324
05:29:19,280 --> 05:29:20,160
are the
8325
05:29:20,160 --> 05:29:22,718
perfect divisors of or proper divisions
8326
05:29:22,718 --> 05:29:25,280
of 6 1 2 and 3. if you add them the
8327
05:29:25,280 --> 05:29:29,600
value is 6 another example is 28
8328
05:29:29,600 --> 05:29:32,558
the proper divisors of 28 are 1
8329
05:29:32,558 --> 05:29:34,320
then you have 2
8330
05:29:34,320 --> 05:29:35,200
4
8331
05:29:35,200 --> 05:29:38,000
7 and 14 if you add them the value is
8332
05:29:38,000 --> 05:29:39,360
28.
8333
05:29:39,360 --> 05:29:42,080
so here we will iterate the first 100
8334
05:29:42,080 --> 05:29:44,320
numbers using a while loop so for each
8335
05:29:44,320 --> 05:29:46,638
iteration of the outer while loop the
8336
05:29:46,638 --> 05:29:49,520
inner for loop executes from 1 up to the
8337
05:29:49,520 --> 05:29:52,798
current outer number to check if the
8338
05:29:52,798 --> 05:29:54,480
current number is a perfect number or
8339
05:29:54,480 --> 05:29:56,000
not
8340
05:29:56,000 --> 05:29:56,878
okay
8341
05:29:56,878 --> 05:29:59,920
so i'll give a comment
8342
05:29:59,920 --> 05:30:01,920
saying
8343
05:30:01,920 --> 05:30:03,840
perfect number
8344
05:30:03,840 --> 05:30:06,160
cool
8345
05:30:06,240 --> 05:30:08,878
so i'll first declare a variable called
8346
05:30:08,878 --> 05:30:12,400
a which will have a value of one then
8347
05:30:12,400 --> 05:30:13,920
i'll say
8348
05:30:13,920 --> 05:30:15,680
while
8349
05:30:15,680 --> 05:30:18,798
my a is
8350
05:30:18,878 --> 05:30:22,240
less than equal to
8351
05:30:22,240 --> 05:30:24,798
100
8352
05:30:24,878 --> 05:30:27,760
then i'll declare another variable
8353
05:30:27,760 --> 05:30:31,120
called y underscore sum which will
8354
05:30:31,120 --> 05:30:33,760
have the value of 0
8355
05:30:33,760 --> 05:30:36,160
after that i'll pass in my
8356
05:30:36,160 --> 05:30:38,558
inner loop which is the
8357
05:30:38,558 --> 05:30:41,680
for loop i'll say for
8358
05:30:41,680 --> 05:30:43,600
i in
8359
05:30:43,600 --> 05:30:47,600
range 1 comma a
8360
05:30:47,600 --> 05:30:49,440
i'll say if
8361
05:30:49,440 --> 05:30:50,638
a
8362
05:30:50,638 --> 05:30:51,920
mod
8363
05:30:51,920 --> 05:30:55,840
i is equal to equal to 0
8364
05:30:55,840 --> 05:30:58,000
then we'll say y underscore
8365
05:30:58,000 --> 05:30:59,360
sum
8366
05:30:59,360 --> 05:31:00,958
equal to
8367
05:31:00,958 --> 05:31:03,040
y underscore sum
8368
05:31:03,040 --> 05:31:05,520
plus i
8369
05:31:07,120 --> 05:31:10,320
then i'm going to say if
8370
05:31:10,320 --> 05:31:11,600
my
8371
05:31:11,600 --> 05:31:14,480
y underscore sum value is equal to equal
8372
05:31:14,480 --> 05:31:15,600
to
8373
05:31:15,600 --> 05:31:19,520
my e value then it's a perfect number
8374
05:31:19,520 --> 05:31:21,520
so i'm going to use a print statement
8375
05:31:21,520 --> 05:31:23,760
saying print
8376
05:31:23,760 --> 05:31:26,240
perfect
8377
05:31:26,320 --> 05:31:28,718
number
8378
05:31:29,280 --> 05:31:30,878
give a colon
8379
05:31:30,878 --> 05:31:34,160
then i'll give a comma and
8380
05:31:34,160 --> 05:31:37,680
print my variable e
8381
05:31:37,680 --> 05:31:39,360
and finally we are going to increment
8382
05:31:39,360 --> 05:31:43,040
the value of e to 1.
8383
05:31:43,040 --> 05:31:44,558
now let's run it
8384
05:31:44,558 --> 05:31:46,240
you can see it here
8385
05:31:46,240 --> 05:31:49,920
between 100 we have two perfect numbers
8386
05:31:49,920 --> 05:31:53,360
as i mentioned earlier 6 and 28
8387
05:31:53,360 --> 05:31:55,600
okay
8388
05:31:56,320 --> 05:31:57,920
now coming to the
8389
05:31:57,920 --> 05:32:00,958
final example in our demo on nested
8390
05:32:00,958 --> 05:32:02,320
loops in python
8391
05:32:02,320 --> 05:32:05,520
here we are going to print each element
8392
05:32:05,520 --> 05:32:08,400
in the list five times
8393
05:32:08,400 --> 05:32:10,160
so
8394
05:32:10,160 --> 05:32:13,120
i'll first declare my list let's say
8395
05:32:13,120 --> 05:32:14,320
fruits
8396
05:32:14,320 --> 05:32:16,798
which has
8397
05:32:16,840 --> 05:32:19,360
apple then we have
8398
05:32:19,360 --> 05:32:20,958
orange
8399
05:32:20,958 --> 05:32:24,480
and let's see we have kiwi
8400
05:32:24,480 --> 05:32:27,360
so i'll say for
8401
05:32:27,520 --> 05:32:29,760
a nitrater
8402
05:32:29,760 --> 05:32:32,160
fruit in
8403
05:32:32,160 --> 05:32:34,638
fruits
8404
05:32:34,638 --> 05:32:36,798
i'll declare a variable called count and
8405
05:32:36,798 --> 05:32:39,120
initialize it to 0
8406
05:32:39,120 --> 05:32:41,120
then i'm going to use a while loop
8407
05:32:41,120 --> 05:32:43,600
saying while count is
8408
05:32:43,600 --> 05:32:47,120
less than 6 since i want to print the
8409
05:32:47,120 --> 05:32:49,920
items in the list 5 times
8410
05:32:49,920 --> 05:32:51,600
then i'll say
8411
05:32:51,600 --> 05:32:54,000
print
8412
05:32:54,320 --> 05:32:56,718
fruit
8413
05:32:57,760 --> 05:33:02,240
and i'll use my end parameter as a space
8414
05:33:02,638 --> 05:33:06,320
then we are going to increment count by
8415
05:33:06,320 --> 05:33:07,840
1
8416
05:33:07,840 --> 05:33:10,638
and finally i'll print it
8417
05:33:10,638 --> 05:33:12,878
let's run it you can see it here i
8418
05:33:12,878 --> 05:33:16,638
printed each element in my list fruits
8419
05:33:16,638 --> 05:33:19,520
five times you can see it here okay so
8420
05:33:19,520 --> 05:33:21,920
i'm on my jupyter notebook first of all
8421
05:33:21,920 --> 05:33:25,440
let's rename our notebook to
8422
05:33:25,440 --> 05:33:28,760
looping techniques
8423
05:33:29,280 --> 05:33:31,520
okay
8424
05:33:31,520 --> 05:33:33,600
i'll just increase the
8425
05:33:33,600 --> 05:33:34,638
size
8426
05:33:34,638 --> 05:33:36,000
all right
8427
05:33:36,000 --> 05:33:36,878
okay
8428
05:33:36,878 --> 05:33:38,878
so let's see how you can arrange the
8429
05:33:38,878 --> 05:33:41,360
values in a list using for loops with
8430
05:33:41,360 --> 05:33:43,840
python sorted function
8431
05:33:43,840 --> 05:33:46,480
so i'll create a variable called num
8432
05:33:46,480 --> 05:33:48,558
and
8433
05:33:48,558 --> 05:33:50,480
i'll assign a list let's say it has
8434
05:33:50,480 --> 05:33:52,480
values 2 5
8435
05:33:52,480 --> 05:33:54,718
10
8436
05:33:54,878 --> 05:33:56,798
4
8437
05:33:56,798 --> 05:33:59,280
i'm taking just random values
8438
05:33:59,280 --> 05:34:01,440
let's say 36
8439
05:34:01,440 --> 05:34:02,480
50
8440
05:34:02,480 --> 05:34:05,200
and 49
8441
05:34:05,200 --> 05:34:07,920
so this is my list now i want to arrange
8442
05:34:07,920 --> 05:34:09,040
the
8443
05:34:09,040 --> 05:34:11,360
items in this list
8444
05:34:11,360 --> 05:34:13,360
in either ascending or descending order
8445
05:34:13,360 --> 05:34:15,440
so for that i can use a for loop i'll
8446
05:34:15,440 --> 05:34:19,200
say for i which is my iterator variable
8447
05:34:19,200 --> 05:34:20,558
in
8448
05:34:20,558 --> 05:34:23,680
and after this i'm going to use the
8449
05:34:23,680 --> 05:34:26,000
python sorted function
8450
05:34:26,000 --> 05:34:28,000
and inside the function i will pass in
8451
05:34:28,000 --> 05:34:30,160
my variable which is num
8452
05:34:30,160 --> 05:34:34,480
then i'll give a colon and say print i
8453
05:34:34,480 --> 05:34:35,920
so let's
8454
05:34:35,920 --> 05:34:37,680
hit shift enter to
8455
05:34:37,680 --> 05:34:38,718
run it
8456
05:34:38,718 --> 05:34:39,840
okay
8457
05:34:39,840 --> 05:34:41,600
you can see here
8458
05:34:41,600 --> 05:34:43,760
so python by default
8459
05:34:43,760 --> 05:34:45,920
has sorted the elements in the list in
8460
05:34:45,920 --> 05:34:49,520
ascending order so we have 2 4 5 10 10
8461
05:34:49,520 --> 05:34:51,600
36 49 50 and
8462
05:34:51,600 --> 05:34:54,080
50.
8463
05:34:55,360 --> 05:34:57,360
now let's learn how you can sort the
8464
05:34:57,360 --> 05:34:59,760
values in a list using a for loop in
8465
05:34:59,760 --> 05:35:01,520
descending order
8466
05:35:01,520 --> 05:35:02,878
so
8467
05:35:02,878 --> 05:35:05,360
i'll just
8468
05:35:05,360 --> 05:35:08,000
copy my above cell of code
8469
05:35:08,000 --> 05:35:10,558
we'll paste it here it will help
8470
05:35:10,558 --> 05:35:12,798
save us some time
8471
05:35:12,798 --> 05:35:14,798
so i'll say for i
8472
05:35:14,798 --> 05:35:16,958
in sorted
8473
05:35:16,958 --> 05:35:19,200
i've given my variable num
8474
05:35:19,200 --> 05:35:20,878
after this
8475
05:35:20,878 --> 05:35:22,638
i am going to pass in another parameter
8476
05:35:22,638 --> 05:35:25,680
called reverse
8477
05:35:25,680 --> 05:35:26,798
equal to
8478
05:35:26,798 --> 05:35:28,958
i'll say true
8479
05:35:28,958 --> 05:35:32,080
now reverse equal to true will make sure
8480
05:35:32,080 --> 05:35:34,798
python will sort the list in descending
8481
05:35:34,798 --> 05:35:36,558
order
8482
05:35:36,558 --> 05:35:37,760
if i run it
8483
05:35:37,760 --> 05:35:40,000
you can see the difference we have
8484
05:35:40,000 --> 05:35:43,280
sorted the elements in the list num in
8485
05:35:43,280 --> 05:35:45,920
descending order now so we have 50 50 49
8486
05:35:45,920 --> 05:35:48,958
36 10 5 4 and 2.
8487
05:35:48,958 --> 05:35:51,280
okay so this is one of the
8488
05:35:51,280 --> 05:35:53,440
techniques that you can use
8489
05:35:53,440 --> 05:35:56,000
to sort the elements in a list using a
8490
05:35:56,000 --> 05:35:57,840
for loop
8491
05:35:57,840 --> 05:36:00,558
now similarly you can also sort string
8492
05:36:00,558 --> 05:36:02,160
elements alphabetically in a list and
8493
05:36:02,160 --> 05:36:04,400
ascending or descending order
8494
05:36:04,400 --> 05:36:06,558
so for that i'll create a variable
8495
05:36:06,558 --> 05:36:10,718
called players let me just scroll down
8496
05:36:11,520 --> 05:36:13,520
then i'll create a list
8497
05:36:13,520 --> 05:36:15,040
which will have the
8498
05:36:15,040 --> 05:36:17,520
names of football players let's say we
8499
05:36:17,520 --> 05:36:19,600
have messi
8500
05:36:19,600 --> 05:36:20,400
then
8501
05:36:20,400 --> 05:36:22,080
let's say we have
8502
05:36:22,080 --> 05:36:24,400
ronaldo
8503
05:36:24,400 --> 05:36:26,080
then we have
8504
05:36:26,080 --> 05:36:28,080
salah
8505
05:36:28,080 --> 05:36:31,920
and then we have let's say harry
8506
05:36:31,920 --> 05:36:33,680
okay
8507
05:36:33,680 --> 05:36:36,160
i'm going to say for i
8508
05:36:36,160 --> 05:36:38,320
in
8509
05:36:38,558 --> 05:36:41,120
my function sorted
8510
05:36:41,120 --> 05:36:43,680
i'll say
8511
05:36:43,760 --> 05:36:45,200
i'll give a comma
8512
05:36:45,200 --> 05:36:50,558
and again use reverse equal to true
8513
05:36:50,558 --> 05:36:51,600
so
8514
05:36:51,600 --> 05:36:53,280
this is going to sort
8515
05:36:53,280 --> 05:36:54,400
the
8516
05:36:54,400 --> 05:36:56,878
items in my list players in descending
8517
05:36:56,878 --> 05:36:59,360
order of
8518
05:36:59,360 --> 05:37:01,840
the alphabets
8519
05:37:01,840 --> 05:37:05,280
then i will give a colon and say
8520
05:37:05,280 --> 05:37:07,280
print i
8521
05:37:07,280 --> 05:37:10,558
let me run it you can see here
8522
05:37:10,558 --> 05:37:12,400
the names of the different football
8523
05:37:12,400 --> 05:37:14,320
players have been sorted in descending
8524
05:37:14,320 --> 05:37:15,360
order of
8525
05:37:15,360 --> 05:37:17,280
alphabet so we have s appearing first
8526
05:37:17,280 --> 05:37:20,798
then r m and h so salah ronaldo messi
8527
05:37:20,798 --> 05:37:22,718
and harry
8528
05:37:22,718 --> 05:37:26,080
all right now let's move ahead
8529
05:37:26,080 --> 05:37:27,840
now we are going to create a dictionary
8530
05:37:27,840 --> 05:37:28,558
of
8531
05:37:28,558 --> 05:37:32,480
key value pairs and sort it
8532
05:37:32,718 --> 05:37:36,400
so i'll create a dictionary called d
8533
05:37:36,400 --> 05:37:37,920
which will have
8534
05:37:37,920 --> 05:37:40,320
a few key value pairs so my first key is
8535
05:37:40,320 --> 05:37:43,040
f and the value is let's say 1
8536
05:37:43,040 --> 05:37:45,520
then my second key let's say is b
8537
05:37:45,520 --> 05:37:47,760
and the value is 4
8538
05:37:47,760 --> 05:37:49,920
i'll give a comma
8539
05:37:49,920 --> 05:37:53,120
my next key is a and the value is let's
8540
05:37:53,120 --> 05:37:55,760
say 3
8541
05:37:55,760 --> 05:37:58,480
after that my next key is e
8542
05:37:58,480 --> 05:38:01,040
and the value is let's say 9
8543
05:38:01,040 --> 05:38:02,878
and finally we have our last key which
8544
05:38:02,878 --> 05:38:07,040
is c and the value is 2.
8545
05:38:07,040 --> 05:38:10,718
now to sort this dictionary i'll say for
8546
05:38:10,718 --> 05:38:11,680
i
8547
05:38:11,680 --> 05:38:13,360
in
8548
05:38:13,360 --> 05:38:15,520
sorted
8549
05:38:15,520 --> 05:38:17,200
i'm going to use the
8550
05:38:17,200 --> 05:38:21,760
items method i'll say sorted d dot items
8551
05:38:21,760 --> 05:38:25,200
and then we'll print i
8552
05:38:26,240 --> 05:38:28,320
let me run it and show you the result
8553
05:38:28,320 --> 05:38:29,920
you can see it here
8554
05:38:29,920 --> 05:38:33,520
our dictionary has been sorted in
8555
05:38:33,520 --> 05:38:36,000
ascending order of the dictionary keys
8556
05:38:36,000 --> 05:38:37,920
so we have a b
8557
05:38:37,920 --> 05:38:41,040
then c there is no d here
8558
05:38:41,040 --> 05:38:43,680
after that we have e and then f
8559
05:38:43,680 --> 05:38:45,440
cool
8560
05:38:45,440 --> 05:38:47,920
now you can also sort the
8561
05:38:47,920 --> 05:38:50,400
keys and values separately let me just
8562
05:38:50,400 --> 05:38:54,080
copy this and i'll show you
8563
05:38:54,400 --> 05:38:56,160
i'll paste it here
8564
05:38:56,160 --> 05:38:58,638
and instead of d dot items i'll just say
8565
05:38:58,638 --> 05:39:00,480
d dot keys
8566
05:39:00,480 --> 05:39:01,680
if i run it
8567
05:39:01,680 --> 05:39:03,520
you can see here we have
8568
05:39:03,520 --> 05:39:05,280
sorted only the
8569
05:39:05,280 --> 05:39:08,240
keys in my dictionary
8570
05:39:08,240 --> 05:39:10,160
now to sort the values instead of items
8571
05:39:10,160 --> 05:39:12,160
you can just write values
8572
05:39:12,160 --> 05:39:14,878
and here you can see we have
8573
05:39:14,878 --> 05:39:17,920
sorted the values in the dictionary d in
8574
05:39:17,920 --> 05:39:20,638
ascending order again so we have 1 2 3 4
8575
05:39:20,638 --> 05:39:22,080
and 9.
8576
05:39:22,080 --> 05:39:24,480
all right
8577
05:39:25,520 --> 05:39:26,320
now
8578
05:39:26,320 --> 05:39:27,920
we will understand how to use the
8579
05:39:27,920 --> 05:39:29,840
reverse function in loops to reverse the
8580
05:39:29,840 --> 05:39:32,080
items in a list
8581
05:39:32,080 --> 05:39:33,600
so i'll again
8582
05:39:33,600 --> 05:39:36,400
create my
8583
05:39:36,400 --> 05:39:40,000
list called players which will have
8584
05:39:40,000 --> 05:39:42,240
messy
8585
05:39:42,240 --> 05:39:44,240
let's say again the next player is
8586
05:39:44,240 --> 05:39:47,240
ronaldo
8587
05:39:48,638 --> 05:39:51,280
and then we have
8588
05:39:51,280 --> 05:39:55,760
salah and then we have harry
8589
05:39:55,760 --> 05:39:57,840
okay
8590
05:39:57,840 --> 05:40:00,480
now to
8591
05:40:00,480 --> 05:40:03,200
reverse the elements in this list called
8592
05:40:03,200 --> 05:40:05,360
players i'll say for
8593
05:40:05,360 --> 05:40:07,360
i in
8594
05:40:07,360 --> 05:40:10,080
then i'll use the reversed function and
8595
05:40:10,080 --> 05:40:12,798
pass in my list called players
8596
05:40:12,798 --> 05:40:16,798
give a colon and then say print i
8597
05:40:16,798 --> 05:40:18,000
let's run it
8598
05:40:18,000 --> 05:40:19,520
here you can see
8599
05:40:19,520 --> 05:40:22,798
we have reversed the
8600
05:40:22,798 --> 05:40:25,600
elements in my list called players so we
8601
05:40:25,600 --> 05:40:28,320
have harry first then salah ronaldo and
8602
05:40:28,320 --> 05:40:30,400
messi in the end
8603
05:40:30,400 --> 05:40:32,638
cool
8604
05:40:34,080 --> 05:40:36,718
now moving ahead
8605
05:40:36,718 --> 05:40:38,958
we'll see how to use the filter function
8606
05:40:38,958 --> 05:40:41,200
in loop and filter specific elements in
8607
05:40:41,200 --> 05:40:43,840
a list based on some conditions now to
8608
05:40:43,840 --> 05:40:45,600
give the condition we'll be using an
8609
05:40:45,600 --> 05:40:47,200
anonymous function in python that is
8610
05:40:47,200 --> 05:40:50,480
often called as the lambda function
8611
05:40:50,480 --> 05:40:51,840
to do this
8612
05:40:51,840 --> 05:40:54,400
i am going to create a list
8613
05:40:54,400 --> 05:40:56,718
called animals
8614
05:40:56,718 --> 05:40:59,440
i'll give my square brackets
8615
05:40:59,440 --> 05:41:01,920
and inside these square brackets
8616
05:41:01,920 --> 05:41:03,360
i'm going to
8617
05:41:03,360 --> 05:41:04,878
pass in the elements in the form of a
8618
05:41:04,878 --> 05:41:06,240
dictionary
8619
05:41:06,240 --> 05:41:08,000
so to pass in a dictionary we need to
8620
05:41:08,000 --> 05:41:10,480
have key value peers
8621
05:41:10,480 --> 05:41:13,360
so my first key is name
8622
05:41:13,360 --> 05:41:15,200
let's see
8623
05:41:15,200 --> 05:41:17,120
my animal is
8624
05:41:17,120 --> 05:41:18,320
dogs
8625
05:41:18,320 --> 05:41:21,120
i'll give a comma
8626
05:41:21,360 --> 05:41:24,480
then my next key is the age let's say
8627
05:41:24,480 --> 05:41:28,000
dog's age is 11
8628
05:41:28,718 --> 05:41:31,840
so this is my first
8629
05:41:32,160 --> 05:41:34,000
list element which has been passed in
8630
05:41:34,000 --> 05:41:37,520
the form of a dictionary
8631
05:41:37,600 --> 05:41:41,200
i'll create another curly bracket and
8632
05:41:41,200 --> 05:41:42,958
we'll have the same keys
8633
05:41:42,958 --> 05:41:44,798
this time let's say
8634
05:41:44,798 --> 05:41:47,520
my animal is lion
8635
05:41:47,520 --> 05:41:50,878
let's make it dog instead of dogs
8636
05:41:50,878 --> 05:41:52,400
okay
8637
05:41:52,400 --> 05:41:54,798
give a comma and then say
8638
05:41:54,798 --> 05:41:56,558
age
8639
05:41:56,558 --> 05:41:59,840
let's say the age of the line is 30
8640
05:41:59,840 --> 05:42:02,638
i'll give a comma
8641
05:42:02,878 --> 05:42:04,958
we'll go to the next line
8642
05:42:04,958 --> 05:42:07,120
and this time
8643
05:42:07,120 --> 05:42:10,718
we are going to have another animal
8644
05:42:11,440 --> 05:42:13,520
i'll say
8645
05:42:13,520 --> 05:42:16,520
elephant
8646
05:42:21,280 --> 05:42:22,878
give a comma
8647
05:42:22,878 --> 05:42:25,120
let's see the age is
8648
05:42:25,120 --> 05:42:27,920
63
8649
05:42:28,240 --> 05:42:29,920
i'll give a comma
8650
05:42:29,920 --> 05:42:33,200
and then we'll have our final
8651
05:42:33,200 --> 05:42:35,360
element in the list
8652
05:42:35,360 --> 05:42:37,520
let's say the name of the
8653
05:42:37,520 --> 05:42:41,120
animal is leopard
8654
05:42:42,160 --> 05:42:43,840
give a comma
8655
05:42:43,840 --> 05:42:45,520
then we'll have the age of the leopard
8656
05:42:45,520 --> 05:42:46,718
let's say
8657
05:42:46,718 --> 05:42:50,080
the age is 18
8658
05:42:50,080 --> 05:42:52,878
so we have our list ready
8659
05:42:52,878 --> 05:42:55,120
now
8660
05:42:55,520 --> 05:42:57,520
using a for loop
8661
05:42:57,520 --> 05:42:59,040
will filter
8662
05:42:59,040 --> 05:43:00,958
the elements in the list giving a
8663
05:43:00,958 --> 05:43:03,520
specific condition let me show you
8664
05:43:03,520 --> 05:43:06,638
so i'll write for animals in
8665
05:43:06,638 --> 05:43:08,878
i'm going to use the filter function
8666
05:43:08,878 --> 05:43:10,878
and inside the filter function
8667
05:43:10,878 --> 05:43:12,400
i'll
8668
05:43:12,400 --> 05:43:13,760
give
8669
05:43:13,760 --> 05:43:17,840
an anonymous function called lambda
8670
05:43:18,638 --> 05:43:20,798
i'll say lambda i
8671
05:43:20,798 --> 05:43:22,320
colon
8672
05:43:22,320 --> 05:43:24,160
i of
8673
05:43:24,160 --> 05:43:26,480
age
8674
05:43:27,120 --> 05:43:30,240
if it is divisible by 2
8675
05:43:30,240 --> 05:43:32,718
so i'll say age
8676
05:43:32,718 --> 05:43:36,878
divided by 2 equal to equal to 0
8677
05:43:36,878 --> 05:43:38,638
give a comma
8678
05:43:38,638 --> 05:43:40,480
then we will
8679
05:43:40,480 --> 05:43:41,440
print
8680
05:43:41,440 --> 05:43:43,680
that animal
8681
05:43:43,680 --> 05:43:45,440
give a colon
8682
05:43:45,440 --> 05:43:47,120
i'll say print
8683
05:43:47,120 --> 05:43:49,600
animals
8684
05:43:49,600 --> 05:43:51,520
so what i'm doing here is
8685
05:43:51,520 --> 05:43:53,520
i'm using the filter function
8686
05:43:53,520 --> 05:43:56,080
and passing my condition that is
8687
05:43:56,080 --> 05:43:56,798
if
8688
05:43:56,798 --> 05:43:59,360
the age of the animal is divisible by 2
8689
05:43:59,360 --> 05:44:02,080
then print the animal
8690
05:44:02,080 --> 05:44:04,000
or just print the list
8691
05:44:04,000 --> 05:44:05,200
rest
8692
05:44:05,200 --> 05:44:06,718
we don't need
8693
05:44:06,718 --> 05:44:07,920
all right
8694
05:44:07,920 --> 05:44:10,240
now let me run it
8695
05:44:10,240 --> 05:44:14,958
it says there's an invalid syntax let me
8696
05:44:14,958 --> 05:44:16,480
debug it
8697
05:44:16,480 --> 05:44:18,240
okay there is a spelling mistake here in
8698
05:44:18,240 --> 05:44:20,120
lambda this should be
8699
05:44:20,120 --> 05:44:21,760
mbta
8700
05:44:21,760 --> 05:44:24,718
okay now we'll run it there you go
8701
05:44:24,718 --> 05:44:28,480
so here in the list that we had created
8702
05:44:28,480 --> 05:44:31,280
only the age of lion and leopard were
8703
05:44:31,280 --> 05:44:34,000
divisible by two so 30 and 18 were
8704
05:44:34,000 --> 05:44:35,600
divisible by two
8705
05:44:35,600 --> 05:44:38,080
so python printed us only those two
8706
05:44:38,080 --> 05:44:40,638
elements in the list the rest it ignored
8707
05:44:40,638 --> 05:44:43,760
11 and 63 are not divisible by two so we
8708
05:44:43,760 --> 05:44:46,000
did not get dog and elephant
8709
05:44:46,000 --> 05:44:48,080
cool
8710
05:44:48,080 --> 05:44:49,680
so this is
8711
05:44:49,680 --> 05:44:51,200
another interesting looping technique
8712
05:44:51,200 --> 05:44:52,480
where you can use
8713
05:44:52,480 --> 05:44:55,280
filter as well as an anonymous lambda
8714
05:44:55,280 --> 05:44:56,878
function
8715
05:44:56,878 --> 05:44:58,320
all right
8716
05:44:58,320 --> 05:45:00,240
now in the next example we'll see how to
8717
05:45:00,240 --> 05:45:02,798
remove items in a list using a
8718
05:45:02,798 --> 05:45:05,200
while loop so so far we have been using
8719
05:45:05,200 --> 05:45:06,798
only for loops
8720
05:45:06,798 --> 05:45:09,600
now we are going to see a few examples
8721
05:45:09,600 --> 05:45:11,200
using while loop
8722
05:45:11,200 --> 05:45:12,958
okay
8723
05:45:12,958 --> 05:45:14,878
i am going to create a variable called
8724
05:45:14,878 --> 05:45:18,240
country so this variable will hold a
8725
05:45:18,240 --> 05:45:20,718
list of elements of the different
8726
05:45:20,718 --> 05:45:23,840
country names i'll say india
8727
05:45:23,840 --> 05:45:25,360
let's say my
8728
05:45:25,360 --> 05:45:27,360
second country is
8729
05:45:27,360 --> 05:45:29,200
canada
8730
05:45:29,200 --> 05:45:31,440
and let's see my third country i have is
8731
05:45:31,440 --> 05:45:33,920
brazil
8732
05:45:34,958 --> 05:45:39,200
my fourth country i have is japan
8733
05:45:39,200 --> 05:45:44,080
and then we have let's say israel
8734
05:45:48,320 --> 05:45:51,040
okay
8735
05:45:51,040 --> 05:45:52,400
now
8736
05:45:52,400 --> 05:45:56,160
i'll use a while loop so i'll say while
8737
05:45:56,160 --> 05:45:59,520
my list that is country
8738
05:45:59,520 --> 05:46:00,718
then
8739
05:46:00,718 --> 05:46:02,320
to remove the elements in the list i'm
8740
05:46:02,320 --> 05:46:04,480
going to use the pop function
8741
05:46:04,480 --> 05:46:06,400
so i'll say
8742
05:46:06,400 --> 05:46:07,360
print
8743
05:46:07,360 --> 05:46:09,280
country
8744
05:46:09,280 --> 05:46:12,638
dot i'll say pop and i'll pass
8745
05:46:12,638 --> 05:46:15,440
-1 as the parameter so that
8746
05:46:15,440 --> 05:46:16,840
it removes the
8747
05:46:16,840 --> 05:46:19,920
elements from the
8748
05:46:19,920 --> 05:46:22,798
end of the list if i
8749
05:46:22,798 --> 05:46:24,638
run it you can see here we have israel
8750
05:46:24,638 --> 05:46:27,440
japan brazil canada and india
8751
05:46:27,440 --> 05:46:29,360
now you can just
8752
05:46:29,360 --> 05:46:31,040
print country
8753
05:46:31,040 --> 05:46:33,280
you can see here since we have removed
8754
05:46:33,280 --> 05:46:35,360
all the elements from
8755
05:46:35,360 --> 05:46:37,520
my list country there is no element left
8756
05:46:37,520 --> 05:46:40,798
here so it gives us an empty list
8757
05:46:40,798 --> 05:46:43,120
cool
8758
05:46:43,600 --> 05:46:45,680
now let me show you how you can use
8759
05:46:45,680 --> 05:46:48,320
while loops in just one line
8760
05:46:48,320 --> 05:46:50,160
so for that i'll create a variable
8761
05:46:50,160 --> 05:46:51,680
called num
8762
05:46:51,680 --> 05:46:54,798
and i'll assign a value of 10
8763
05:46:54,798 --> 05:46:57,040
i'll say while
8764
05:46:57,040 --> 05:46:58,638
num is
8765
05:46:58,638 --> 05:47:01,840
greater than 0
8766
05:47:01,840 --> 05:47:03,200
i'll say
8767
05:47:03,200 --> 05:47:04,558
num
8768
05:47:04,558 --> 05:47:05,920
equal to
8769
05:47:05,920 --> 05:47:08,240
num minus 1
8770
05:47:08,240 --> 05:47:10,480
i'll give a
8771
05:47:10,480 --> 05:47:14,320
semicolon this time and then i'll say
8772
05:47:14,320 --> 05:47:15,200
print
8773
05:47:15,200 --> 05:47:16,080
num
8774
05:47:16,080 --> 05:47:18,638
let's run it you can see it here
8775
05:47:18,638 --> 05:47:20,000
we have printed
8776
05:47:20,000 --> 05:47:20,878
nine
8777
05:47:20,878 --> 05:47:25,200
eight seven six up to zero
8778
05:47:25,600 --> 05:47:28,080
so in the first iteration my num value
8779
05:47:28,080 --> 05:47:29,280
was 10
8780
05:47:29,280 --> 05:47:32,798
so 10 greater than 0 was true so we
8781
05:47:32,798 --> 05:47:35,120
decreased the value of num by 1 so num
8782
05:47:35,120 --> 05:47:36,080
became
8783
05:47:36,080 --> 05:47:38,240
9 and we printed 9. in the second
8784
05:47:38,240 --> 05:47:39,280
iteration
8785
05:47:39,280 --> 05:47:41,280
num which was 9
8786
05:47:41,280 --> 05:47:44,000
was greater than 0 then we
8787
05:47:44,000 --> 05:47:46,320
decreased or reduced numb by 1 again so
8788
05:47:46,320 --> 05:47:49,360
it became 8 then we printed it
8789
05:47:49,360 --> 05:47:51,600
and the loop continued till we reached
8790
05:47:51,600 --> 05:47:54,000
0.
8791
05:47:54,840 --> 05:47:58,240
cool now had this condition been
8792
05:47:58,240 --> 05:47:59,360
num
8793
05:47:59,360 --> 05:48:02,638
equal to num plus 1 or num plus equal to
8794
05:48:02,638 --> 05:48:05,360
1 the loop would have run for infinite
8795
05:48:05,360 --> 05:48:07,360
number of times
8796
05:48:07,360 --> 05:48:09,360
all right
8797
05:48:09,360 --> 05:48:10,638
now i'll
8798
05:48:10,638 --> 05:48:12,558
show you an example how to run an
8799
05:48:12,558 --> 05:48:15,120
infinite loop
8800
05:48:15,120 --> 05:48:17,360
so i'll say while
8801
05:48:17,360 --> 05:48:19,680
true
8802
05:48:20,638 --> 05:48:23,440
i'll create a variable called num
8803
05:48:23,440 --> 05:48:25,280
now this num would be
8804
05:48:25,280 --> 05:48:27,760
a user input a user integer input
8805
05:48:27,760 --> 05:48:29,040
actually
8806
05:48:29,040 --> 05:48:32,320
i'll pass in the message as enter an
8807
05:48:32,320 --> 05:48:34,878
integer
8808
05:48:35,600 --> 05:48:39,120
give a colon followed by a space
8809
05:48:39,120 --> 05:48:41,440
and then we'll say
8810
05:48:41,440 --> 05:48:43,680
print
8811
05:48:43,680 --> 05:48:45,760
the
8812
05:48:45,760 --> 05:48:48,240
product
8813
05:48:49,120 --> 05:48:52,120
of
8814
05:48:52,160 --> 05:48:55,920
i'll give a comma say num
8815
05:48:56,558 --> 05:48:58,958
i'll give another comma
8816
05:48:58,958 --> 05:49:00,638
i'll say
8817
05:49:00,638 --> 05:49:02,958
is
8818
05:49:03,600 --> 05:49:07,360
now we'll pass in our multiplication i
8819
05:49:07,360 --> 05:49:08,798
want to
8820
05:49:08,798 --> 05:49:10,400
multiply any
8821
05:49:10,400 --> 05:49:12,958
user given number by 5
8822
05:49:12,958 --> 05:49:16,080
so i'll say 5 multiplied by
8823
05:49:16,080 --> 05:49:17,920
num
8824
05:49:17,920 --> 05:49:18,798
ok
8825
05:49:18,798 --> 05:49:21,200
let's run it and see here enter an
8826
05:49:21,200 --> 05:49:24,958
integer if i say let's say 3 so 3
8827
05:49:24,958 --> 05:49:30,240
multiplied by 5 is 15 the product of 3
8828
05:49:30,718 --> 05:49:34,080
here i can change the message as
8829
05:49:34,080 --> 05:49:36,958
multiplied
8830
05:49:39,360 --> 05:49:42,000
by five
8831
05:49:42,000 --> 05:49:43,760
you can say is
8832
05:49:43,760 --> 05:49:44,718
okay
8833
05:49:44,718 --> 05:49:46,558
now let's run it
8834
05:49:46,558 --> 05:49:47,520
okay
8835
05:49:47,520 --> 05:49:50,718
so let's say i enter my integer as 3 i
8836
05:49:50,718 --> 05:49:52,320
have the message the product of 3
8837
05:49:52,320 --> 05:49:55,440
multiplied by 5 is 15.
8838
05:49:55,440 --> 05:49:58,160
let's enter another value 10
8839
05:49:58,160 --> 05:49:59,920
the product is 50.
8840
05:49:59,920 --> 05:50:02,958
let's say i have 20 the product is 100
8841
05:50:02,958 --> 05:50:05,520
let's see i am taking
8842
05:50:05,520 --> 05:50:09,920
35 multiplied by 5 is 175 so this loop
8843
05:50:09,920 --> 05:50:10,798
will
8844
05:50:10,798 --> 05:50:13,360
continue until and unless you don't
8845
05:50:13,360 --> 05:50:14,958
interrupt it
8846
05:50:14,958 --> 05:50:17,600
so i'm going to interrupt my
8847
05:50:17,600 --> 05:50:20,000
kernel
8848
05:50:20,878 --> 05:50:22,080
to stop
8849
05:50:22,080 --> 05:50:25,040
executing this loop
8850
05:50:25,040 --> 05:50:27,040
okay now we are going to explore the
8851
05:50:27,040 --> 05:50:29,120
enumerate function in python
8852
05:50:29,120 --> 05:50:30,958
but before that let me tell you what
8853
05:50:30,958 --> 05:50:33,120
enumerate function is
8854
05:50:33,120 --> 05:50:34,958
so the enumerate method or the function
8855
05:50:34,958 --> 05:50:36,718
in python allows us to
8856
05:50:36,718 --> 05:50:39,440
number list indices of the items as they
8857
05:50:39,440 --> 05:50:41,600
are coming out of the array python will
8858
05:50:41,600 --> 05:50:43,680
automatically put this indices into a
8859
05:50:43,680 --> 05:50:44,638
tuple
8860
05:50:44,638 --> 05:50:47,200
let me show you how it works
8861
05:50:47,200 --> 05:50:49,760
so i'll directly start with my for loop
8862
05:50:49,760 --> 05:50:52,240
i'll say for i in
8863
05:50:52,240 --> 05:50:55,840
i'll use the enumerate keyword
8864
05:50:57,040 --> 05:50:58,878
and within
8865
05:50:58,878 --> 05:51:02,638
brackets i'll pass in my
8866
05:51:03,760 --> 05:51:06,240
elements in the form of list
8867
05:51:06,240 --> 05:51:07,600
i'll say the
8868
05:51:07,600 --> 05:51:10,638
let's say then we have
8869
05:51:10,638 --> 05:51:13,638
brown
8870
05:51:14,240 --> 05:51:17,920
the next word i have is for
8871
05:51:17,920 --> 05:51:20,798
let's see then we have jumped this is
8872
05:51:20,798 --> 05:51:22,958
basically a sentence broken down into
8873
05:51:22,958 --> 05:51:25,440
different words and those words we have
8874
05:51:25,440 --> 05:51:26,480
put
8875
05:51:26,480 --> 05:51:28,798
as elements in the list
8876
05:51:28,798 --> 05:51:31,680
the next word i have is the
8877
05:51:31,680 --> 05:51:34,480
give a comma i'll say
8878
05:51:34,480 --> 05:51:36,958
lazy
8879
05:51:38,320 --> 05:51:41,840
then we have
8880
05:51:42,000 --> 05:51:44,320
dog
8881
05:51:45,360 --> 05:51:47,200
give a comma
8882
05:51:47,200 --> 05:51:49,760
and finally we'll have our full stop or
8883
05:51:49,760 --> 05:51:51,760
the period
8884
05:51:51,760 --> 05:51:55,040
okay then i'll give a colon
8885
05:51:55,040 --> 05:51:57,360
and i'll say
8886
05:51:57,360 --> 05:51:59,680
print
8887
05:52:00,160 --> 05:52:02,080
i
8888
05:52:02,080 --> 05:52:05,120
now let me run it you can see here using
8889
05:52:05,120 --> 05:52:08,638
the enumerate function we have
8890
05:52:08,638 --> 05:52:10,080
given
8891
05:52:10,080 --> 05:52:10,798
the
8892
05:52:10,798 --> 05:52:13,280
indices to the different items in my
8893
05:52:13,280 --> 05:52:16,160
list so it starts from zero and goes
8894
05:52:16,160 --> 05:52:18,958
till eight so there are total nine
8895
05:52:18,958 --> 05:52:21,280
elements in my list
8896
05:52:21,280 --> 05:52:23,200
okay
8897
05:52:23,200 --> 05:52:25,440
now you can see here the
8898
05:52:25,440 --> 05:52:27,440
result has been printed in the form of a
8899
05:52:27,440 --> 05:52:29,360
tuple or tuple
8900
05:52:29,360 --> 05:52:31,600
i'll just copy the above code and paste
8901
05:52:31,600 --> 05:52:32,878
it here
8902
05:52:32,878 --> 05:52:37,280
now i'm going to replace i with key
8903
05:52:37,280 --> 05:52:38,160
and
8904
05:52:38,160 --> 05:52:40,638
value
8905
05:52:42,000 --> 05:52:44,000
and
8906
05:52:44,000 --> 05:52:50,000
print also i'll say key comma value
8907
05:52:50,000 --> 05:52:52,160
let me show you the result you can see
8908
05:52:52,160 --> 05:52:54,000
here this time
8909
05:52:54,000 --> 05:52:56,320
it is no more at tuple
8910
05:52:56,320 --> 05:52:58,638
and here we have the indices and then we
8911
05:52:58,638 --> 05:53:00,638
have the
8912
05:53:00,638 --> 05:53:02,878
items so basically these indices have
8913
05:53:02,878 --> 05:53:05,360
now become the keys and
8914
05:53:05,360 --> 05:53:08,878
the items are the values
8915
05:53:08,878 --> 05:53:11,200
okay
8916
05:53:11,840 --> 05:53:13,200
now you can
8917
05:53:13,200 --> 05:53:14,240
print
8918
05:53:14,240 --> 05:53:16,320
the different elements in the form of a
8919
05:53:16,320 --> 05:53:18,958
sentence let me show you how to do it
8920
05:53:18,958 --> 05:53:21,040
i'll just paste it here
8921
05:53:21,040 --> 05:53:23,280
and
8922
05:53:24,000 --> 05:53:27,600
instead of key i'll say value
8923
05:53:27,600 --> 05:53:30,240
and i'll use
8924
05:53:30,240 --> 05:53:32,878
the end parameter in the print statement
8925
05:53:32,878 --> 05:53:35,520
i'll say end equal to
8926
05:53:35,520 --> 05:53:37,200
give us peace
8927
05:53:37,200 --> 05:53:39,840
let's run it you can see here
8928
05:53:39,840 --> 05:53:42,320
okay this should be brown fox and not
8929
05:53:42,320 --> 05:53:44,480
for let's run it again
8930
05:53:44,480 --> 05:53:46,718
so we have the sentence the brown fox
8931
05:53:46,718 --> 05:53:49,280
jumped over the lazy dog
8932
05:53:49,280 --> 05:53:50,558
okay
8933
05:53:50,558 --> 05:53:52,718
so similarly you can iterate through the
8934
05:53:52,718 --> 05:53:54,400
letters in a word or a string and print
8935
05:53:54,400 --> 05:53:57,920
the letters along with the indices
8936
05:53:58,320 --> 05:54:00,958
i'll declare a variable called company
8937
05:54:00,958 --> 05:54:02,400
and
8938
05:54:02,400 --> 05:54:04,320
this will have a value let's say simply
8939
05:54:04,320 --> 05:54:05,360
learn
8940
05:54:05,360 --> 05:54:08,000
i'll say for i comma
8941
05:54:08,000 --> 05:54:08,718
j
8942
05:54:08,718 --> 05:54:09,840
in
8943
05:54:09,840 --> 05:54:11,360
i'll use the
8944
05:54:11,360 --> 05:54:15,000
enumerate function
8945
05:54:15,760 --> 05:54:17,760
within brackets i'll say
8946
05:54:17,760 --> 05:54:19,840
company
8947
05:54:19,840 --> 05:54:21,600
give a colon
8948
05:54:21,600 --> 05:54:22,480
then
8949
05:54:22,480 --> 05:54:24,958
i'll say print
8950
05:54:24,958 --> 05:54:28,320
i comma j
8951
05:54:28,320 --> 05:54:30,638
let's see the result there you go so
8952
05:54:30,638 --> 05:54:32,638
here we have
8953
05:54:32,638 --> 05:54:34,558
indices assigned to the different
8954
05:54:34,558 --> 05:54:35,760
letters
8955
05:54:35,760 --> 05:54:38,320
in my word simply learn
8956
05:54:38,320 --> 05:54:41,320
okay
8957
05:54:42,160 --> 05:54:44,958
now moving ahead
8958
05:54:45,120 --> 05:54:47,200
so by default the index position starts
8959
05:54:47,200 --> 05:54:50,558
at 0 as i told you
8960
05:54:50,558 --> 05:54:52,080
now you can also mention the starting
8961
05:54:52,080 --> 05:54:55,280
index position using the start parameter
8962
05:54:55,280 --> 05:54:58,240
so what i'll do is
8963
05:54:58,638 --> 05:55:00,718
i'll copy my
8964
05:55:00,718 --> 05:55:02,638
above code
8965
05:55:02,638 --> 05:55:04,718
and paste it here now we are going to
8966
05:55:04,718 --> 05:55:07,120
make some changes i'll say for
8967
05:55:07,120 --> 05:55:09,280
i in
8968
05:55:09,280 --> 05:55:11,440
enumerate company
8969
05:55:11,440 --> 05:55:13,920
and then i'm going to give my start
8970
05:55:13,920 --> 05:55:15,840
parameter let's say i want to start the
8971
05:55:15,840 --> 05:55:18,480
index position at 10.
8972
05:55:18,480 --> 05:55:20,400
now if i print it
8973
05:55:20,400 --> 05:55:23,120
okay let's say print i
8974
05:55:23,120 --> 05:55:25,840
there you go here you can see
8975
05:55:25,840 --> 05:55:28,480
my first letter starts at index 10 then
8976
05:55:28,480 --> 05:55:30,000
we have 11
8977
05:55:30,000 --> 05:55:34,080
m at 12 p at 13 and finally we have
8978
05:55:34,080 --> 05:55:36,240
the last letter which is n in simply
8979
05:55:36,240 --> 05:55:38,718
learn is at the 20th index
8980
05:55:38,718 --> 05:55:40,320
cool okay
8981
05:55:40,320 --> 05:55:42,638
so now let's explore the zip function in
8982
05:55:42,638 --> 05:55:44,480
python and how you can use it to loop it
8983
05:55:44,480 --> 05:55:46,958
through two arrays at once
8984
05:55:46,958 --> 05:55:50,638
so the zip function returns a zip object
8985
05:55:50,638 --> 05:55:52,718
which is an iterator of tuples where the
8986
05:55:52,718 --> 05:55:54,400
first item in each
8987
05:55:54,400 --> 05:55:56,480
past iterator is paired together and
8988
05:55:56,480 --> 05:55:58,480
then the second item in each parse
8989
05:55:58,480 --> 05:56:00,718
titrator is paired together and so on
8990
05:56:00,718 --> 05:56:02,160
and so forth
8991
05:56:02,160 --> 05:56:04,480
so if the past iterators have different
8992
05:56:04,480 --> 05:56:06,480
lengths the iterator with the
8993
05:56:06,480 --> 05:56:08,558
least items decides the length of the
8994
05:56:08,558 --> 05:56:10,480
new iterator
8995
05:56:10,480 --> 05:56:12,480
let me
8996
05:56:12,480 --> 05:56:13,840
show you
8997
05:56:13,840 --> 05:56:15,680
the first example that will help you
8998
05:56:15,680 --> 05:56:18,400
understand how the zip function works so
8999
05:56:18,400 --> 05:56:19,680
i have a
9000
05:56:19,680 --> 05:56:21,520
list called first
9001
05:56:21,520 --> 05:56:23,120
and i have a
9002
05:56:23,120 --> 05:56:25,600
names of few
9003
05:56:25,600 --> 05:56:27,760
football players we have
9004
05:56:27,760 --> 05:56:29,200
lionel
9005
05:56:29,200 --> 05:56:32,958
then we have cristiano
9006
05:56:33,360 --> 05:56:36,320
give a comma and let's say we have
9007
05:56:36,320 --> 05:56:38,878
mohammed
9008
05:56:40,480 --> 05:56:41,760
i'll declare
9009
05:56:41,760 --> 05:56:44,080
another list called last
9010
05:56:44,080 --> 05:56:46,718
which will have
9011
05:56:46,718 --> 05:56:48,718
values like messi
9012
05:56:48,718 --> 05:56:50,480
then we have
9013
05:56:50,480 --> 05:56:51,680
ronaldo
9014
05:56:51,680 --> 05:56:53,440
basically the
9015
05:56:53,440 --> 05:56:56,080
surnames
9016
05:56:56,080 --> 05:56:58,638
of leonel christiano and mohammed then
9017
05:56:58,638 --> 05:57:02,280
we have salah
9018
05:57:03,440 --> 05:57:08,958
i'll write my for loop i'll say for i in
9019
05:57:09,280 --> 05:57:12,000
my zip function or the method
9020
05:57:12,000 --> 05:57:14,840
i'll pass in first comma last which are
9021
05:57:14,840 --> 05:57:20,000
my list names i'll say print
9022
05:57:20,000 --> 05:57:21,120
i
9023
05:57:21,120 --> 05:57:23,520
let me run it you can see here
9024
05:57:23,520 --> 05:57:24,638
using the
9025
05:57:24,638 --> 05:57:27,040
zip function we have
9026
05:57:27,040 --> 05:57:28,638
iterated through
9027
05:57:28,638 --> 05:57:31,120
both the list so we have lionel messi
9028
05:57:31,120 --> 05:57:32,798
cristiano ronaldo and then mohammad
9029
05:57:32,798 --> 05:57:34,240
salla
9030
05:57:34,240 --> 05:57:35,360
all right
9031
05:57:35,360 --> 05:57:37,920
now you can combine the elements of
9032
05:57:37,920 --> 05:57:39,920
three lists as well
9033
05:57:39,920 --> 05:57:42,080
so i'll just paste the code here and
9034
05:57:42,080 --> 05:57:45,440
we'll create another list called num
9035
05:57:45,440 --> 05:57:47,840
and this is going to be a list of
9036
05:57:47,840 --> 05:57:49,600
numbers
9037
05:57:49,600 --> 05:57:50,638
let's say
9038
05:57:50,638 --> 05:57:51,600
7
9039
05:57:51,600 --> 05:57:53,440
and 8 okay
9040
05:57:53,440 --> 05:57:55,920
now here i'll say for i
9041
05:57:55,920 --> 05:57:56,798
in
9042
05:57:56,798 --> 05:58:01,360
zip first last and num print i
9043
05:58:01,360 --> 05:58:02,718
if i run it
9044
05:58:02,718 --> 05:58:04,638
you see the result here it has zipped
9045
05:58:04,638 --> 05:58:07,040
only the
9046
05:58:07,040 --> 05:58:09,600
first three items in the
9047
05:58:09,600 --> 05:58:12,958
list first last and num so we have
9048
05:58:12,958 --> 05:58:15,280
three items printed
9049
05:58:15,280 --> 05:58:17,520
okay
9050
05:58:18,798 --> 05:58:20,958
now coming to the final section of this
9051
05:58:20,958 --> 05:58:23,360
demo on looping techniques in python
9052
05:58:23,360 --> 05:58:25,200
we'll explore about
9053
05:58:25,200 --> 05:58:27,440
zip underscore longest
9054
05:58:27,440 --> 05:58:30,400
so python's iterator tool or iter tool
9055
05:58:30,400 --> 05:58:32,000
is a module that provides various
9056
05:58:32,000 --> 05:58:34,480
functions that work on iterators to
9057
05:58:34,480 --> 05:58:36,400
produce complex citrators
9058
05:58:36,400 --> 05:58:38,480
so itreaters in python is an object that
9059
05:58:38,480 --> 05:58:40,638
can iterate through sequence data type
9060
05:58:40,638 --> 05:58:44,000
such as list tuple string and so on
9061
05:58:44,000 --> 05:58:46,320
so the zip underscore longest iterator
9062
05:58:46,320 --> 05:58:48,878
falls under terminating iterators it
9063
05:58:48,878 --> 05:58:51,200
prints the values of the i tables
9064
05:58:51,200 --> 05:58:54,240
alternatively in sequence if one of the
9065
05:58:54,240 --> 05:58:56,718
iterables is printed fully the remaining
9066
05:58:56,718 --> 05:58:58,240
values are filled with the values
9067
05:58:58,240 --> 05:59:01,680
assigned in the fill value parameter
9068
05:59:01,680 --> 05:59:03,360
so to use this
9069
05:59:03,360 --> 05:59:05,840
function called zip underscore longest
9070
05:59:05,840 --> 05:59:08,160
we need to
9071
05:59:08,160 --> 05:59:10,638
import zip longest from the item tools
9072
05:59:10,638 --> 05:59:13,280
module so i'll say from
9073
05:59:13,280 --> 05:59:14,798
iter tools
9074
05:59:14,798 --> 05:59:16,718
import
9075
05:59:16,718 --> 05:59:21,400
zip underscore longest
9076
05:59:22,000 --> 05:59:24,240
okay
9077
05:59:25,840 --> 05:59:28,240
then i'll
9078
05:59:28,240 --> 05:59:30,240
create my
9079
05:59:30,240 --> 05:59:33,360
first list
9080
05:59:34,080 --> 05:59:34,840
i'll
9081
05:59:34,840 --> 05:59:37,840
say let me go to the top and copy the
9082
05:59:37,840 --> 05:59:39,600
elements because we are going to use
9083
05:59:39,600 --> 05:59:42,558
these same elements again
9084
05:59:42,558 --> 05:59:45,520
i'll paste it here
9085
05:59:47,120 --> 05:59:48,718
okay
9086
05:59:48,718 --> 05:59:51,440
and let's say i'll give a comma and we
9087
05:59:51,440 --> 05:59:53,280
are going to
9088
05:59:53,280 --> 05:59:55,920
and i'm again going to use the
9089
05:59:55,920 --> 05:59:59,280
three elements okay
9090
05:59:59,280 --> 06:00:02,718
now i'll create another list called last
9091
06:00:02,718 --> 06:00:06,160
and we are going to copy the items
9092
06:00:06,160 --> 06:00:08,798
from the top
9093
06:00:09,280 --> 06:00:12,000
paste it here
9094
06:00:12,878 --> 06:00:13,840
and now
9095
06:00:13,840 --> 06:00:15,200
i'll say
9096
06:00:15,200 --> 06:00:18,718
for let me just scroll down
9097
06:00:18,718 --> 06:00:19,520
i
9098
06:00:19,520 --> 06:00:21,040
in
9099
06:00:21,040 --> 06:00:23,360
zip underscore
9100
06:00:23,360 --> 06:00:25,520
longest
9101
06:00:25,520 --> 06:00:28,638
within brackets i'll say first comma
9102
06:00:28,638 --> 06:00:30,240
last
9103
06:00:30,240 --> 06:00:34,080
we're going to print i
9104
06:00:34,160 --> 06:00:35,200
let me
9105
06:00:35,200 --> 06:00:38,718
run it and show you the result
9106
06:00:39,040 --> 06:00:42,480
this should be zip underscore longest
9107
06:00:42,480 --> 06:00:44,840
okay you can see the result
9108
06:00:44,840 --> 06:00:48,000
here for the first
9109
06:00:48,000 --> 06:00:49,840
three items it has printed lionel messi
9110
06:00:49,840 --> 06:00:51,360
cristiano ronaldo and mohamed salah
9111
06:00:51,360 --> 06:00:54,160
which you can see here but for the
9112
06:00:54,160 --> 06:00:56,160
last three items in the
9113
06:00:56,160 --> 06:00:58,400
first list it has given us
9114
06:00:58,400 --> 06:01:01,200
none because there were no values
9115
06:01:01,200 --> 06:01:03,600
now to overcome this problem you can use
9116
06:01:03,600 --> 06:01:06,160
a parameter called fill value
9117
06:01:06,160 --> 06:01:08,718
let me just copy this
9118
06:01:08,718 --> 06:01:11,840
and i'll paste it here
9119
06:01:11,920 --> 06:01:14,480
and the only change that i'm going to do
9120
06:01:14,480 --> 06:01:16,958
here is
9121
06:01:17,360 --> 06:01:20,240
i'm going to say
9122
06:01:20,480 --> 06:01:22,160
i'll give a comma
9123
06:01:22,160 --> 06:01:26,638
i'll use the parameter called fill value
9124
06:01:26,638 --> 06:01:30,080
equal to i'll say n a
9125
06:01:30,080 --> 06:01:33,040
if i run it you can see here
9126
06:01:33,040 --> 06:01:35,600
for the last three now we have whatever
9127
06:01:35,600 --> 06:01:36,798
we have
9128
06:01:36,798 --> 06:01:38,878
defined within field value parameter you
9129
06:01:38,878 --> 06:01:41,200
can change this to let's say
9130
06:01:41,200 --> 06:01:43,760
not defined
9131
06:01:43,760 --> 06:01:45,520
there you go
9132
06:01:45,520 --> 06:01:46,878
all right
9133
06:01:46,878 --> 06:01:48,000
so
9134
06:01:48,000 --> 06:01:49,760
these were some of the interesting
9135
06:01:49,760 --> 06:01:52,480
examples that we implemented using
9136
06:01:52,480 --> 06:01:54,480
looping techniques in python this is
9137
06:01:54,480 --> 06:01:56,320
going to be a completely hands-on demo
9138
06:01:56,320 --> 06:01:58,400
video and we'll be using the jupyter
9139
06:01:58,400 --> 06:02:00,718
notebook to work on the examples
9140
06:02:00,718 --> 06:02:02,240
so let's head over to our jupyter
9141
06:02:02,240 --> 06:02:04,558
notebook and begin
9142
06:02:04,558 --> 06:02:07,520
okay so i'm on my jupyter notebook first
9143
06:02:07,520 --> 06:02:11,360
of all let's rename our notebook to
9144
06:02:11,360 --> 06:02:14,320
looping techniques
9145
06:02:15,280 --> 06:02:17,520
okay
9146
06:02:17,520 --> 06:02:19,600
i'll just increase the
9147
06:02:19,600 --> 06:02:20,638
size
9148
06:02:20,638 --> 06:02:22,000
all right
9149
06:02:22,000 --> 06:02:22,878
okay
9150
06:02:22,878 --> 06:02:24,878
so let's see how you can arrange the
9151
06:02:24,878 --> 06:02:27,280
values in a list using for loops with
9152
06:02:27,280 --> 06:02:29,840
python sorted function
9153
06:02:29,840 --> 06:02:32,480
so i'll create a variable called num
9154
06:02:32,480 --> 06:02:34,480
and
9155
06:02:34,480 --> 06:02:36,400
i'll assign a list let's say it has
9156
06:02:36,400 --> 06:02:38,480
values 2 5
9157
06:02:38,480 --> 06:02:40,718
10
9158
06:02:40,798 --> 06:02:42,798
4
9159
06:02:42,798 --> 06:02:45,280
i'm taking just random values
9160
06:02:45,280 --> 06:02:47,360
let's say 36
9161
06:02:47,360 --> 06:02:48,400
50
9162
06:02:48,400 --> 06:02:51,120
and 49
9163
06:02:51,120 --> 06:02:53,920
so this is my list now i want to arrange
9164
06:02:53,920 --> 06:02:54,958
the
9165
06:02:54,958 --> 06:02:57,280
items in this list
9166
06:02:57,280 --> 06:02:59,360
in either ascending or descending order
9167
06:02:59,360 --> 06:03:01,360
so for that i can use a for loop i'll
9168
06:03:01,360 --> 06:03:05,120
say for i which is my iterator variable
9169
06:03:05,120 --> 06:03:06,558
in
9170
06:03:06,558 --> 06:03:09,680
and after this i'm going to use the
9171
06:03:09,680 --> 06:03:11,920
python sorted function
9172
06:03:11,920 --> 06:03:14,080
and inside the function i'll pass in my
9173
06:03:14,080 --> 06:03:16,080
variable which is num
9174
06:03:16,080 --> 06:03:20,480
then i'll give a colon and say print i
9175
06:03:20,480 --> 06:03:21,840
so let's
9176
06:03:21,840 --> 06:03:24,638
hit shift enter to run it
9177
06:03:24,638 --> 06:03:25,760
okay
9178
06:03:25,760 --> 06:03:27,520
you can see here
9179
06:03:27,520 --> 06:03:29,680
so python by default
9180
06:03:29,680 --> 06:03:31,840
has sorted the elements in the list in
9181
06:03:31,840 --> 06:03:35,440
ascending order so we have 2 4 5 10 10
9182
06:03:35,440 --> 06:03:37,520
36 49 50 and
9183
06:03:37,520 --> 06:03:40,000
50.
9184
06:03:41,360 --> 06:03:43,360
now let's learn how you can sort the
9185
06:03:43,360 --> 06:03:45,760
values in a list using a for loop in
9186
06:03:45,760 --> 06:03:47,520
descending order
9187
06:03:47,520 --> 06:03:48,798
so
9188
06:03:48,798 --> 06:03:51,280
i'll just
9189
06:03:51,280 --> 06:03:53,920
copy my above cell of code
9190
06:03:53,920 --> 06:03:56,558
we'll paste it here it will help
9191
06:03:56,558 --> 06:03:58,798
save us some time
9192
06:03:58,798 --> 06:04:00,798
so i'll say for i
9193
06:04:00,798 --> 06:04:02,878
in sorted
9194
06:04:02,878 --> 06:04:05,120
i have given my variable num
9195
06:04:05,120 --> 06:04:06,798
after this
9196
06:04:06,798 --> 06:04:08,558
i am going to pass in another parameter
9197
06:04:08,558 --> 06:04:11,680
called reverse
9198
06:04:11,680 --> 06:04:12,798
equal to
9199
06:04:12,798 --> 06:04:14,958
i'll say true
9200
06:04:14,958 --> 06:04:18,000
now reverse equal to true will make sure
9201
06:04:18,000 --> 06:04:20,718
python will sort the list in descending
9202
06:04:20,718 --> 06:04:22,480
order
9203
06:04:22,480 --> 06:04:23,760
if i run it
9204
06:04:23,760 --> 06:04:25,920
you can see the difference we have
9205
06:04:25,920 --> 06:04:29,280
sorted the elements in the list num in
9206
06:04:29,280 --> 06:04:31,840
descending order now so we have 50 50 49
9207
06:04:31,840 --> 06:04:34,958
36 10 5 4 and 2.
9208
06:04:34,958 --> 06:04:37,280
okay so this is one of the
9209
06:04:37,280 --> 06:04:39,360
techniques that you can use
9210
06:04:39,360 --> 06:04:41,920
to sort the elements in a list using a
9211
06:04:41,920 --> 06:04:43,760
for loop
9212
06:04:43,760 --> 06:04:46,558
now similarly you can also sort string
9213
06:04:46,558 --> 06:04:48,080
elements alphabetically in a list in
9214
06:04:48,080 --> 06:04:50,400
ascending or descending order
9215
06:04:50,400 --> 06:04:52,480
so for that i'll create a variable
9216
06:04:52,480 --> 06:04:56,638
called players let me just scroll down
9217
06:04:57,440 --> 06:04:59,440
then i'll create a list
9218
06:04:59,440 --> 06:05:01,040
which will have the
9219
06:05:01,040 --> 06:05:02,160
names of
9220
06:05:02,160 --> 06:05:05,600
football players let's say we have messi
9221
06:05:05,600 --> 06:05:08,080
then let's say we have
9222
06:05:08,080 --> 06:05:10,400
ronaldo
9223
06:05:10,400 --> 06:05:14,080
then we have salah
9224
06:05:14,080 --> 06:05:17,920
and then we have let's say harry
9225
06:05:17,920 --> 06:05:19,600
okay
9226
06:05:19,600 --> 06:05:22,080
i'm going to say for i
9227
06:05:22,080 --> 06:05:24,320
in
9228
06:05:24,558 --> 06:05:27,120
my function sorted
9229
06:05:27,120 --> 06:05:28,160
i'll say
9230
06:05:28,160 --> 06:05:29,760
players
9231
06:05:29,760 --> 06:05:31,120
i'll give a comma
9232
06:05:31,120 --> 06:05:36,480
and again use reverse equal to true
9233
06:05:36,558 --> 06:05:37,600
so
9234
06:05:37,600 --> 06:05:39,280
this is going to sort
9235
06:05:39,280 --> 06:05:40,400
the
9236
06:05:40,400 --> 06:05:42,878
items in my list players in descending
9237
06:05:42,878 --> 06:05:45,280
order of
9238
06:05:45,360 --> 06:05:47,760
the alphabets
9239
06:05:47,760 --> 06:05:48,558
then
9240
06:05:48,558 --> 06:05:51,280
i will give a colon and say
9241
06:05:51,280 --> 06:05:53,280
print i
9242
06:05:53,280 --> 06:05:56,480
let me run it you can see here
9243
06:05:56,480 --> 06:05:58,320
the names of the different football
9244
06:05:58,320 --> 06:06:00,240
players have been sorted in descending
9245
06:06:00,240 --> 06:06:01,360
order of
9246
06:06:01,360 --> 06:06:03,200
alphabet so we have s appearing first
9247
06:06:03,200 --> 06:06:06,798
then r m and h so salah ronaldo messi
9248
06:06:06,798 --> 06:06:08,718
and harry
9249
06:06:08,718 --> 06:06:12,000
alright now let's move ahead
9250
06:06:12,000 --> 06:06:13,840
now we are going to create a dictionary
9251
06:06:13,840 --> 06:06:14,558
of
9252
06:06:14,558 --> 06:06:18,400
key value pairs and sort it
9253
06:06:18,638 --> 06:06:22,320
so i'll create a dictionary called d
9254
06:06:22,320 --> 06:06:23,920
which will have
9255
06:06:23,920 --> 06:06:26,240
a few key value pairs so my first key is
9256
06:06:26,240 --> 06:06:28,958
f and the value is let's say 1
9257
06:06:28,958 --> 06:06:31,440
then my second key let's say is b
9258
06:06:31,440 --> 06:06:33,920
and the value is 4
9259
06:06:33,920 --> 06:06:34,840
give a
9260
06:06:34,840 --> 06:06:38,240
comma my next key is a
9261
06:06:38,240 --> 06:06:41,520
and the value is let's say 3
9262
06:06:41,680 --> 06:06:44,480
after that my next key is e
9263
06:06:44,480 --> 06:06:46,958
and the value is let's say 9
9264
06:06:46,958 --> 06:06:48,878
and finally we have our last key which
9265
06:06:48,878 --> 06:06:53,040
is c and the value is 2.
9266
06:06:53,040 --> 06:06:56,718
now to sort this dictionary i'll say for
9267
06:06:56,718 --> 06:06:57,600
i
9268
06:06:57,600 --> 06:06:59,360
in
9269
06:06:59,360 --> 06:07:01,520
sorted
9270
06:07:01,520 --> 06:07:03,200
i'm going to use the
9271
06:07:03,200 --> 06:07:04,480
items method
9272
06:07:04,480 --> 06:07:07,760
i'll say sorted d dot items
9273
06:07:07,760 --> 06:07:11,200
and then will print i
9274
06:07:12,240 --> 06:07:14,320
let me run it and show you the result
9275
06:07:14,320 --> 06:07:15,840
you can see it here
9276
06:07:15,840 --> 06:07:19,440
our dictionary has been sorted in
9277
06:07:19,440 --> 06:07:21,920
ascending order of the dictionary keys
9278
06:07:21,920 --> 06:07:23,840
so we have a b
9279
06:07:23,840 --> 06:07:26,958
then c there is no d here
9280
06:07:26,958 --> 06:07:29,600
after that we have e and then f
9281
06:07:29,600 --> 06:07:31,360
cool
9282
06:07:31,360 --> 06:07:33,840
now you can also sort the
9283
06:07:33,840 --> 06:07:36,400
keys and values separately let me just
9284
06:07:36,400 --> 06:07:40,080
copy this and i'll show you
9285
06:07:40,320 --> 06:07:42,160
i'll paste it here
9286
06:07:42,160 --> 06:07:44,638
and instead of d dot items i'll just say
9287
06:07:44,638 --> 06:07:46,400
d dot keys
9288
06:07:46,400 --> 06:07:49,520
if i run it you can see here we have
9289
06:07:49,520 --> 06:07:51,200
sorted only the
9290
06:07:51,200 --> 06:07:54,240
keys in my dictionary
9291
06:07:54,240 --> 06:07:56,160
now to sort the values instead of items
9292
06:07:56,160 --> 06:07:58,080
you can just write values
9293
06:07:58,080 --> 06:08:00,878
and here you can see we have
9294
06:08:00,878 --> 06:08:03,920
sorted the values in the dictionary d in
9295
06:08:03,920 --> 06:08:06,000
ascending order again so we have one two
9296
06:08:06,000 --> 06:08:08,000
three four and nine
9297
06:08:08,000 --> 06:08:10,400
all right
9298
06:08:11,520 --> 06:08:13,920
now we will understand how to use the
9299
06:08:13,920 --> 06:08:15,840
reverse function in loops to reverse the
9300
06:08:15,840 --> 06:08:18,080
items in a list
9301
06:08:18,080 --> 06:08:19,520
so i'll again
9302
06:08:19,520 --> 06:08:22,320
create my
9303
06:08:22,320 --> 06:08:26,638
list called players which will have
9304
06:08:26,840 --> 06:08:30,160
messy let's see again the next player is
9305
06:08:30,160 --> 06:08:32,798
ronaldo
9306
06:08:34,638 --> 06:08:35,520
and
9307
06:08:35,520 --> 06:08:37,280
then we have
9308
06:08:37,280 --> 06:08:41,680
salah and then we have harry
9309
06:08:41,680 --> 06:08:43,760
okay
9310
06:08:43,760 --> 06:08:46,240
now to
9311
06:08:46,480 --> 06:08:49,120
reverse the elements in this list called
9312
06:08:49,120 --> 06:08:51,360
players i'll say for
9313
06:08:51,360 --> 06:08:53,280
i in
9314
06:08:53,280 --> 06:08:56,000
then i'll use the reversed function and
9315
06:08:56,000 --> 06:08:58,798
pass in my list called players
9316
06:08:58,798 --> 06:09:02,718
give a colon and then say print i
9317
06:09:02,718 --> 06:09:05,520
let's run it here you can see
9318
06:09:05,520 --> 06:09:08,718
we have reversed the
9319
06:09:08,718 --> 06:09:11,600
elements in my list called players so we
9320
06:09:11,600 --> 06:09:14,320
have harry first then salah ronaldo and
9321
06:09:14,320 --> 06:09:16,320
messi in the end
9322
06:09:16,320 --> 06:09:18,638
cool
9323
06:09:20,000 --> 06:09:22,638
now moving ahead
9324
06:09:22,638 --> 06:09:24,878
we'll see how to use the filter function
9325
06:09:24,878 --> 06:09:27,200
in loop and filter specific elements in
9326
06:09:27,200 --> 06:09:29,840
a list based on some conditions now to
9327
06:09:29,840 --> 06:09:31,600
give the condition we'll be using an
9328
06:09:31,600 --> 06:09:33,200
anonymous function in python that is
9329
06:09:33,200 --> 06:09:36,480
often called as the lambda function
9330
06:09:36,480 --> 06:09:37,760
to do this
9331
06:09:37,760 --> 06:09:40,320
i'm going to create a list
9332
06:09:40,320 --> 06:09:42,718
called animals
9333
06:09:42,718 --> 06:09:45,440
i'll give my square brackets
9334
06:09:45,440 --> 06:09:47,920
and inside these square brackets
9335
06:09:47,920 --> 06:09:49,360
i'm going to
9336
06:09:49,360 --> 06:09:50,798
pass in the elements in the form of a
9337
06:09:50,798 --> 06:09:53,680
dictionary so to pass in a dictionary we
9338
06:09:53,680 --> 06:09:56,400
need to have key value peers
9339
06:09:56,400 --> 06:09:59,360
so my first key is name
9340
06:09:59,360 --> 06:10:01,120
let's see
9341
06:10:01,120 --> 06:10:03,120
my animal is
9342
06:10:03,120 --> 06:10:04,320
dogs
9343
06:10:04,320 --> 06:10:07,280
i'll give a comma
9344
06:10:07,280 --> 06:10:10,480
then my next key is the age let's say
9345
06:10:10,480 --> 06:10:14,000
dog's age is 11.
9346
06:10:14,638 --> 06:10:17,840
so this is my first
9347
06:10:18,160 --> 06:10:19,920
list element which has been passed in
9348
06:10:19,920 --> 06:10:23,520
the form of a dictionary
9349
06:10:23,600 --> 06:10:27,120
i'll create another curly bracket and
9350
06:10:27,120 --> 06:10:28,878
we'll have the same keys
9351
06:10:28,878 --> 06:10:30,718
this time let's say
9352
06:10:30,718 --> 06:10:33,520
my animal is lion
9353
06:10:33,520 --> 06:10:36,878
let's make it dog instead of dogs
9354
06:10:36,878 --> 06:10:38,400
okay
9355
06:10:38,400 --> 06:10:40,798
give a comma and then say
9356
06:10:40,798 --> 06:10:42,480
age
9357
06:10:42,480 --> 06:10:45,760
let's see the age of the line is 30
9358
06:10:45,760 --> 06:10:48,878
i'll give a comma
9359
06:10:48,878 --> 06:10:50,958
we'll go to the next line
9360
06:10:50,958 --> 06:10:53,120
and this time
9361
06:10:53,120 --> 06:10:56,638
we're going to have another animal
9362
06:10:57,440 --> 06:10:59,440
i'll say
9363
06:10:59,440 --> 06:11:02,440
elephant
9364
06:11:07,280 --> 06:11:08,878
give a comma
9365
06:11:08,878 --> 06:11:11,120
let's say the age is
9366
06:11:11,120 --> 06:11:13,840
63
9367
06:11:14,160 --> 06:11:16,638
i'll give a comma and then we'll have
9368
06:11:16,638 --> 06:11:19,200
our final
9369
06:11:19,200 --> 06:11:21,360
element in the list
9370
06:11:21,360 --> 06:11:23,520
let's say the name of the
9371
06:11:23,520 --> 06:11:27,120
animal is leopard
9372
06:11:28,160 --> 06:11:29,840
give a comma
9373
06:11:29,840 --> 06:11:31,440
then we'll have the age of the leopard
9374
06:11:31,440 --> 06:11:32,718
let's say
9375
06:11:32,718 --> 06:11:36,000
the age is 18
9376
06:11:36,000 --> 06:11:38,878
so we have our list ready
9377
06:11:38,878 --> 06:11:41,120
now
9378
06:11:41,440 --> 06:11:43,440
using a for loop
9379
06:11:43,440 --> 06:11:45,040
will filter
9380
06:11:45,040 --> 06:11:46,878
the elements in the list giving a
9381
06:11:46,878 --> 06:11:49,520
specific condition let me show you
9382
06:11:49,520 --> 06:11:52,558
so i'll write for animals in
9383
06:11:52,558 --> 06:11:54,878
i'm going to use the filter function
9384
06:11:54,878 --> 06:11:56,840
and inside the filter function
9385
06:11:56,840 --> 06:11:59,680
i'll give
9386
06:11:59,680 --> 06:12:03,520
an anonymous function called lambda
9387
06:12:04,558 --> 06:12:06,718
i'll say lambda i
9388
06:12:06,718 --> 06:12:08,320
colon
9389
06:12:08,320 --> 06:12:10,080
i of
9390
06:12:10,080 --> 06:12:13,040
age
9391
06:12:13,040 --> 06:12:16,240
if it is divisible by 2
9392
06:12:16,240 --> 06:12:18,718
so i'll say age
9393
06:12:18,718 --> 06:12:22,878
divided by 2 equal to equal to 0
9394
06:12:22,878 --> 06:12:24,638
give a comma
9395
06:12:24,638 --> 06:12:26,480
then we will
9396
06:12:26,480 --> 06:12:27,360
print
9397
06:12:27,360 --> 06:12:29,600
that animal
9398
06:12:29,600 --> 06:12:31,360
give a colon
9399
06:12:31,360 --> 06:12:33,040
i'll say print
9400
06:12:33,040 --> 06:12:35,520
animals
9401
06:12:35,520 --> 06:12:37,520
so what i am doing here is
9402
06:12:37,520 --> 06:12:39,440
i am using the filter function
9403
06:12:39,440 --> 06:12:42,000
and passing my condition that is
9404
06:12:42,000 --> 06:12:42,798
if
9405
06:12:42,798 --> 06:12:45,360
the age of the animal is divisible by 2
9406
06:12:45,360 --> 06:12:48,080
then print the animal
9407
06:12:48,080 --> 06:12:50,000
or just print the list
9408
06:12:50,000 --> 06:12:51,120
rest
9409
06:12:51,120 --> 06:12:52,718
we don't need
9410
06:12:52,718 --> 06:12:56,240
all right now let me run it
9411
06:12:56,240 --> 06:12:58,320
it says there's an
9412
06:12:58,320 --> 06:13:00,958
invalid syntax let me
9413
06:13:00,958 --> 06:13:02,400
debug it
9414
06:13:02,400 --> 06:13:04,240
okay there is a spelling mistake here in
9415
06:13:04,240 --> 06:13:06,120
lambda this should be
9416
06:13:06,120 --> 06:13:07,680
mbta
9417
06:13:07,680 --> 06:13:10,718
okay now we'll run it there you go
9418
06:13:10,718 --> 06:13:11,840
so here
9419
06:13:11,840 --> 06:13:14,400
in the list that we had created
9420
06:13:14,400 --> 06:13:17,280
only the age of lion and leopard were
9421
06:13:17,280 --> 06:13:20,000
divisible by two so 30 and 18 were
9422
06:13:20,000 --> 06:13:21,520
divisible by two
9423
06:13:21,520 --> 06:13:24,080
so python printed us only those two
9424
06:13:24,080 --> 06:13:26,638
elements in the list the rest it ignored
9425
06:13:26,638 --> 06:13:29,680
11 and 63 are not divisible by 2 so we
9426
06:13:29,680 --> 06:13:32,000
did not get dog and elephant
9427
06:13:32,000 --> 06:13:34,080
cool
9428
06:13:34,080 --> 06:13:36,798
so this is another interesting looping
9429
06:13:36,798 --> 06:13:38,480
technique where you can use
9430
06:13:38,480 --> 06:13:41,280
filter as well as an anonymous lambda
9431
06:13:41,280 --> 06:13:42,878
function
9432
06:13:42,878 --> 06:13:44,320
all right
9433
06:13:44,320 --> 06:13:46,160
now in the next example we'll see how to
9434
06:13:46,160 --> 06:13:49,040
remove items in a list using a while
9435
06:13:49,040 --> 06:13:51,520
loop so so far we have been using only
9436
06:13:51,520 --> 06:13:52,718
for loops
9437
06:13:52,718 --> 06:13:55,520
now we are going to see a few examples
9438
06:13:55,520 --> 06:13:57,120
using while loop
9439
06:13:57,120 --> 06:13:58,878
okay
9440
06:13:58,878 --> 06:14:00,798
i'm going to create a variable called
9441
06:14:00,798 --> 06:14:04,160
country so this variable will hold a
9442
06:14:04,160 --> 06:14:06,638
list of elements of the different
9443
06:14:06,638 --> 06:14:09,760
country names i'll say india
9444
06:14:09,760 --> 06:14:11,280
let's say my
9445
06:14:11,280 --> 06:14:13,360
second country is
9446
06:14:13,360 --> 06:14:15,120
canada
9447
06:14:15,120 --> 06:14:17,360
and let's say my third country i have is
9448
06:14:17,360 --> 06:14:19,840
brazil
9449
06:14:20,878 --> 06:14:22,160
my
9450
06:14:22,160 --> 06:14:25,200
fourth country i have is japan
9451
06:14:25,200 --> 06:14:27,600
and then we have let's say
9452
06:14:27,600 --> 06:14:30,600
israel
9453
06:14:34,320 --> 06:14:36,558
okay
9454
06:14:37,040 --> 06:14:38,320
now
9455
06:14:38,320 --> 06:14:42,080
i'll use a while loop so i'll say while
9456
06:14:42,080 --> 06:14:45,520
my list that is country
9457
06:14:45,520 --> 06:14:46,638
then
9458
06:14:46,638 --> 06:14:48,240
to remove the elements in the list i'm
9459
06:14:48,240 --> 06:14:50,400
going to use the pop function
9460
06:14:50,400 --> 06:14:52,320
so i'll say
9461
06:14:52,320 --> 06:14:53,360
print
9462
06:14:53,360 --> 06:14:55,200
country
9463
06:14:55,200 --> 06:14:58,638
dot i'll say pop and i'll pass
9464
06:14:58,638 --> 06:15:01,360
-1 as the parameter so that
9465
06:15:01,360 --> 06:15:03,920
it removes the elements
9466
06:15:03,920 --> 06:15:05,840
from the
9467
06:15:05,840 --> 06:15:08,718
end of the list if i
9468
06:15:08,718 --> 06:15:10,558
run it you can see here we have israel
9469
06:15:10,558 --> 06:15:13,440
japan brazil canada and india
9470
06:15:13,440 --> 06:15:15,280
now you can just
9471
06:15:15,280 --> 06:15:16,958
print country
9472
06:15:16,958 --> 06:15:18,320
you can see here
9473
06:15:18,320 --> 06:15:20,080
since we have removed all the elements
9474
06:15:20,080 --> 06:15:21,360
from
9475
06:15:21,360 --> 06:15:23,440
my list country there is no element left
9476
06:15:23,440 --> 06:15:26,798
here so it gives us an empty list
9477
06:15:26,798 --> 06:15:29,600
cool
9478
06:15:29,600 --> 06:15:31,600
now let me show you how you can use
9479
06:15:31,600 --> 06:15:34,240
while loops in just one line
9480
06:15:34,240 --> 06:15:36,080
so for that i'll create a variable
9481
06:15:36,080 --> 06:15:37,680
called num
9482
06:15:37,680 --> 06:15:40,718
and i'll assign a value of 10
9483
06:15:40,718 --> 06:15:43,040
i'll say while
9484
06:15:43,040 --> 06:15:44,638
num is
9485
06:15:44,638 --> 06:15:45,840
greater than
9486
06:15:45,840 --> 06:15:47,840
0
9487
06:15:47,840 --> 06:15:49,120
i'll say
9488
06:15:49,120 --> 06:15:50,558
num
9489
06:15:50,558 --> 06:15:51,920
equal to
9490
06:15:51,920 --> 06:15:54,240
num minus 1
9491
06:15:54,240 --> 06:15:56,480
i'll give a
9492
06:15:56,480 --> 06:16:00,240
semicolon this time and then i'll say
9493
06:16:00,240 --> 06:16:02,080
print num
9494
06:16:02,080 --> 06:16:04,558
let's run it you can see it here
9495
06:16:04,558 --> 06:16:05,920
we have printed
9496
06:16:05,920 --> 06:16:06,878
nine
9497
06:16:06,878 --> 06:16:11,520
eight seven six up to zero
9498
06:16:11,520 --> 06:16:13,360
so in the first iteration
9499
06:16:13,360 --> 06:16:15,280
my num value was 10
9500
06:16:15,280 --> 06:16:18,718
so 10 greater than 0 was true so we
9501
06:16:18,718 --> 06:16:21,040
decreased the value of num by 1 so num
9502
06:16:21,040 --> 06:16:22,080
became
9503
06:16:22,080 --> 06:16:24,160
9 and we printed nine in the second
9504
06:16:24,160 --> 06:16:25,280
iteration
9505
06:16:25,280 --> 06:16:27,200
num which was nine
9506
06:16:27,200 --> 06:16:29,920
was greater than zero then we
9507
06:16:29,920 --> 06:16:32,240
decreased or reduced num by one again so
9508
06:16:32,240 --> 06:16:35,280
it became eight then we printed it
9509
06:16:35,280 --> 06:16:38,400
and the loop continued till we reached 0
9510
06:16:38,400 --> 06:16:40,718
cool
9511
06:16:41,920 --> 06:16:44,240
now had this condition been
9512
06:16:44,240 --> 06:16:45,360
num
9513
06:16:45,360 --> 06:16:48,558
equal to num plus 1 or num plus equal to
9514
06:16:48,558 --> 06:16:51,280
1 the loop would have run for infinite
9515
06:16:51,280 --> 06:16:53,360
number of times
9516
06:16:53,360 --> 06:16:55,360
all right
9517
06:16:55,360 --> 06:16:56,638
now i'll
9518
06:16:56,638 --> 06:16:58,558
show you an example how to run an
9519
06:16:58,558 --> 06:17:01,120
infinite loop
9520
06:17:01,120 --> 06:17:03,360
so i'll say while
9521
06:17:03,360 --> 06:17:06,360
true
9522
06:17:06,558 --> 06:17:09,360
i'll create a variable called num
9523
06:17:09,360 --> 06:17:11,280
now this num would be
9524
06:17:11,280 --> 06:17:13,760
a user input a user integer input
9525
06:17:13,760 --> 06:17:15,040
actually
9526
06:17:15,040 --> 06:17:18,240
i'll pass in the message as enter an
9527
06:17:18,240 --> 06:17:21,240
integer
9528
06:17:21,600 --> 06:17:25,120
give a colon followed by a space
9529
06:17:25,120 --> 06:17:27,440
and then we'll say
9530
06:17:27,440 --> 06:17:29,600
print
9531
06:17:29,600 --> 06:17:31,760
the
9532
06:17:31,760 --> 06:17:34,760
product
9533
06:17:35,120 --> 06:17:37,440
off
9534
06:17:38,080 --> 06:17:41,840
i'll give a comma say num
9535
06:17:42,480 --> 06:17:44,958
i'll give another comma
9536
06:17:44,958 --> 06:17:46,638
i'll say
9537
06:17:46,638 --> 06:17:49,600
is
9538
06:17:49,600 --> 06:17:52,160
now we'll pass in our
9539
06:17:52,160 --> 06:17:54,798
multiplication i want to
9540
06:17:54,798 --> 06:17:58,878
multiply any user given number by 5
9541
06:17:58,878 --> 06:18:03,920
so i'll say 5 multiplied by num
9542
06:18:03,920 --> 06:18:04,718
okay
9543
06:18:04,718 --> 06:18:06,080
let's run it
9544
06:18:06,080 --> 06:18:08,320
and see here enter an integer if i say
9545
06:18:08,320 --> 06:18:09,600
let's say 3
9546
06:18:09,600 --> 06:18:10,878
so 3
9547
06:18:10,878 --> 06:18:16,718
multiplied by 5 is 15 the product of 3
9548
06:18:16,718 --> 06:18:20,080
here i can change the message as
9549
06:18:20,080 --> 06:18:22,878
multiplied
9550
06:18:25,360 --> 06:18:28,000
by 5
9551
06:18:28,000 --> 06:18:29,760
let's say is
9552
06:18:29,760 --> 06:18:30,718
okay
9553
06:18:30,718 --> 06:18:32,558
now let's run it
9554
06:18:32,558 --> 06:18:33,520
okay
9555
06:18:33,520 --> 06:18:36,638
so let's say i enter my integer as 3 i
9556
06:18:36,638 --> 06:18:38,320
have the message the product of 3
9557
06:18:38,320 --> 06:18:41,360
multiplied by 5 is 15
9558
06:18:41,360 --> 06:18:44,638
let's enter another value 10 the product
9559
06:18:44,638 --> 06:18:45,840
is 50
9560
06:18:45,840 --> 06:18:48,958
let's say i have 20 the product is 100
9561
06:18:48,958 --> 06:18:51,440
let's see i am taking
9562
06:18:51,440 --> 06:18:55,920
35 multiplied by 5 is 175 so this loop
9563
06:18:55,920 --> 06:18:56,840
will
9564
06:18:56,840 --> 06:18:59,360
continue until and unless you don't
9565
06:18:59,360 --> 06:19:00,878
interrupt it
9566
06:19:00,878 --> 06:19:03,520
so i'm going to interrupt my
9567
06:19:03,520 --> 06:19:05,920
kernel
9568
06:19:06,798 --> 06:19:08,080
to stop
9569
06:19:08,080 --> 06:19:11,040
executing this loop
9570
06:19:11,040 --> 06:19:13,040
okay now we are going to explore the
9571
06:19:13,040 --> 06:19:15,600
enumerate function in python but before
9572
06:19:15,600 --> 06:19:17,440
that let me tell you what enumerate
9573
06:19:17,440 --> 06:19:19,120
function is
9574
06:19:19,120 --> 06:19:20,958
so the enumerate method or the function
9575
06:19:20,958 --> 06:19:23,760
in python allows us to number list
9576
06:19:23,760 --> 06:19:26,000
indices of the items as they are coming
9577
06:19:26,000 --> 06:19:27,600
out of the array python will
9578
06:19:27,600 --> 06:19:29,600
automatically put this indices into a
9579
06:19:29,600 --> 06:19:30,558
tuple
9580
06:19:30,558 --> 06:19:33,200
let me show you how it works
9581
06:19:33,200 --> 06:19:35,680
so i'll directly start with my for loop
9582
06:19:35,680 --> 06:19:38,240
i'll say for i in
9583
06:19:38,240 --> 06:19:41,840
i'll use the enumerate keyword
9584
06:19:42,958 --> 06:19:44,878
and within
9585
06:19:44,878 --> 06:19:48,638
brackets i'll pass in my
9586
06:19:49,760 --> 06:19:52,160
elements in the form of list
9587
06:19:52,160 --> 06:19:53,520
i'll say the
9588
06:19:53,520 --> 06:19:56,638
let's say then we have
9589
06:19:56,638 --> 06:19:58,958
brown
9590
06:20:00,240 --> 06:20:03,840
the next word i have is for
9591
06:20:03,840 --> 06:20:06,718
let's say then we have jumped this is
9592
06:20:06,718 --> 06:20:08,878
basically a sentence broken down into
9593
06:20:08,878 --> 06:20:11,360
different words and those words we have
9594
06:20:11,360 --> 06:20:12,478
put
9595
06:20:12,478 --> 06:20:14,718
as elements in the list
9596
06:20:14,718 --> 06:20:17,680
the next word i have is the
9597
06:20:17,680 --> 06:20:20,478
give a comma i'll say
9598
06:20:20,478 --> 06:20:23,478
lazy
9599
06:20:24,320 --> 06:20:27,200
then we have
9600
06:20:28,000 --> 06:20:30,320
dog
9601
06:20:31,360 --> 06:20:33,120
give a comma
9602
06:20:33,120 --> 06:20:35,600
and finally we will have our full stop
9603
06:20:35,600 --> 06:20:37,680
or the period
9604
06:20:37,680 --> 06:20:41,040
okay then i'll give a colon
9605
06:20:41,040 --> 06:20:43,280
and i'll say
9606
06:20:43,280 --> 06:20:45,680
print
9607
06:20:46,080 --> 06:20:48,080
i
9608
06:20:48,080 --> 06:20:49,520
now let me run it
9609
06:20:49,520 --> 06:20:52,320
you can see here using the enumerate
9610
06:20:52,320 --> 06:20:54,558
function we have
9611
06:20:54,558 --> 06:20:56,000
given
9612
06:20:56,000 --> 06:20:59,280
the indices to the different items in my
9613
06:20:59,280 --> 06:21:02,558
list so it starts from 0 and goes till
9614
06:21:02,558 --> 06:21:05,600
8. so there are total 9 elements in my
9615
06:21:05,600 --> 06:21:07,280
list
9616
06:21:07,280 --> 06:21:09,200
okay
9617
06:21:09,200 --> 06:21:11,440
now you can see here the
9618
06:21:11,440 --> 06:21:13,440
result has been printed in the form of a
9619
06:21:13,440 --> 06:21:15,280
tuple or tuple
9620
06:21:15,280 --> 06:21:17,520
i'll just copy the above code and paste
9621
06:21:17,520 --> 06:21:18,798
it here
9622
06:21:18,798 --> 06:21:21,920
now i'm going to replace i with
9623
06:21:21,920 --> 06:21:23,280
key
9624
06:21:23,280 --> 06:21:24,160
and
9625
06:21:24,160 --> 06:21:26,558
value
9626
06:21:27,920 --> 06:21:29,920
and
9627
06:21:29,920 --> 06:21:35,520
print also i'll say key comma value
9628
06:21:36,000 --> 06:21:38,080
let me show you the result you can see
9629
06:21:38,080 --> 06:21:40,000
here this time
9630
06:21:40,000 --> 06:21:42,240
it is no more at tuple
9631
06:21:42,240 --> 06:21:44,638
and here we have the indices and then we
9632
06:21:44,638 --> 06:21:46,638
have the
9633
06:21:46,638 --> 06:21:48,798
items so basically these indices have
9634
06:21:48,798 --> 06:21:51,360
now become the keys and
9635
06:21:51,360 --> 06:21:54,878
the items are the values
9636
06:21:54,878 --> 06:21:57,120
okay
9637
06:21:57,840 --> 06:21:59,120
now you can
9638
06:21:59,120 --> 06:22:02,000
print the different elements in the form
9639
06:22:02,000 --> 06:22:03,920
of a sentence let me show you how to do
9640
06:22:03,920 --> 06:22:04,958
it
9641
06:22:04,958 --> 06:22:07,040
i'll just paste it here
9642
06:22:07,040 --> 06:22:09,280
and
9643
06:22:09,920 --> 06:22:13,520
instead of key i'll say value
9644
06:22:13,520 --> 06:22:16,240
and i'll use
9645
06:22:16,240 --> 06:22:18,878
the end parameter in the print statement
9646
06:22:18,878 --> 06:22:21,440
i'll say end equal to
9647
06:22:21,440 --> 06:22:23,200
give a space
9648
06:22:23,200 --> 06:22:25,840
let's run it you can see here
9649
06:22:25,840 --> 06:22:28,240
okay this should be brown fox and not
9650
06:22:28,240 --> 06:22:30,400
far let's run it again
9651
06:22:30,400 --> 06:22:32,638
so we have the sentence the brown fox
9652
06:22:32,638 --> 06:22:35,280
jumped over the lazy dog
9653
06:22:35,280 --> 06:22:36,558
okay
9654
06:22:36,558 --> 06:22:38,638
so similarly you can iterate through the
9655
06:22:38,638 --> 06:22:40,400
letters in a word or a string and print
9656
06:22:40,400 --> 06:22:43,840
the letters along with the indices
9657
06:22:44,320 --> 06:22:46,958
i'll declare a variable called company
9658
06:22:46,958 --> 06:22:48,400
and
9659
06:22:48,400 --> 06:22:50,320
this will have a value let's say simply
9660
06:22:50,320 --> 06:22:51,280
learn
9661
06:22:51,280 --> 06:22:53,920
i'll say for i comma
9662
06:22:53,920 --> 06:22:54,718
j
9663
06:22:54,718 --> 06:22:55,760
in
9664
06:22:55,760 --> 06:22:57,360
i'll use the
9665
06:22:57,360 --> 06:23:00,400
enumerate function
9666
06:23:01,680 --> 06:23:03,760
within brackets i'll say
9667
06:23:03,760 --> 06:23:05,760
company
9668
06:23:05,760 --> 06:23:07,520
give a colon
9669
06:23:07,520 --> 06:23:08,400
then
9670
06:23:08,400 --> 06:23:10,958
i'll say print
9671
06:23:10,958 --> 06:23:14,080
i comma j
9672
06:23:14,240 --> 06:23:16,558
let's see the result there you go so
9673
06:23:16,558 --> 06:23:18,638
here we have
9674
06:23:18,638 --> 06:23:20,558
indices assigned to the different
9675
06:23:20,558 --> 06:23:21,760
letters
9676
06:23:21,760 --> 06:23:26,558
in my word simply learn okay
9677
06:23:28,160 --> 06:23:30,958
now moving ahead
9678
06:23:31,040 --> 06:23:33,120
so by default the index position starts
9679
06:23:33,120 --> 06:23:36,478
at 0 as i told you
9680
06:23:36,478 --> 06:23:38,080
now you can also mention the starting
9681
06:23:38,080 --> 06:23:41,200
index position using the start parameter
9682
06:23:41,200 --> 06:23:44,240
so what i'll do is
9683
06:23:44,638 --> 06:23:46,718
i'll copy my
9684
06:23:46,718 --> 06:23:48,638
above code
9685
06:23:48,638 --> 06:23:50,638
and paste it here now we are going to
9686
06:23:50,638 --> 06:23:53,040
make some changes i'll say for
9687
06:23:53,040 --> 06:23:55,200
i in
9688
06:23:55,200 --> 06:23:57,440
enumerate company
9689
06:23:57,440 --> 06:23:59,920
and then i'm going to give my start
9690
06:23:59,920 --> 06:24:01,760
parameter let's say i want to start the
9691
06:24:01,760 --> 06:24:04,478
index position at 10.
9692
06:24:04,478 --> 06:24:09,040
now if i print it okay let's say print i
9693
06:24:09,040 --> 06:24:11,840
there you go here you can see
9694
06:24:11,840 --> 06:24:14,400
my first letter starts at index 10 then
9695
06:24:14,400 --> 06:24:16,000
we have 11
9696
06:24:16,000 --> 06:24:20,240
m at 12 p at 13 and finally we have the
9697
06:24:20,240 --> 06:24:22,558
last letter which is n in simply learn
9698
06:24:22,558 --> 06:24:24,718
is that the 20th index
9699
06:24:24,718 --> 06:24:26,240
cool okay
9700
06:24:26,240 --> 06:24:28,558
so now let's explore the zip function in
9701
06:24:28,558 --> 06:24:30,478
python and how you can use it to loop it
9702
06:24:30,478 --> 06:24:32,958
through two arrays at once
9703
06:24:32,958 --> 06:24:36,638
so the zip function returns a zip object
9704
06:24:36,638 --> 06:24:38,638
which is an iterator of tuples where the
9705
06:24:38,638 --> 06:24:40,400
first item in each
9706
06:24:40,400 --> 06:24:42,400
past iterator is paired together and
9707
06:24:42,400 --> 06:24:44,400
then the second item in each pass
9708
06:24:44,400 --> 06:24:46,638
titrator is paired together and so on
9709
06:24:46,638 --> 06:24:49,840
and so forth so if the past iterators
9710
06:24:49,840 --> 06:24:51,760
have different lengths the iterated with
9711
06:24:51,760 --> 06:24:54,320
the least items decides the length of
9712
06:24:54,320 --> 06:24:56,478
the new iterator
9713
06:24:56,478 --> 06:24:58,478
let me
9714
06:24:58,478 --> 06:24:59,760
show you
9715
06:24:59,760 --> 06:25:01,600
the first example that will help you
9716
06:25:01,600 --> 06:25:04,400
understand how the zip function works so
9717
06:25:04,400 --> 06:25:05,680
i have a
9718
06:25:05,680 --> 06:25:07,440
list called first
9719
06:25:07,440 --> 06:25:09,120
and i have a
9720
06:25:09,120 --> 06:25:11,600
names of few
9721
06:25:11,600 --> 06:25:13,760
football players we have
9722
06:25:13,760 --> 06:25:15,200
leonel
9723
06:25:15,200 --> 06:25:18,878
then we have cristiano
9724
06:25:19,280 --> 06:25:22,320
give a comma and let's say we have
9725
06:25:22,320 --> 06:25:24,798
mahmud
9726
06:25:26,478 --> 06:25:27,680
i'll declare
9727
06:25:27,680 --> 06:25:32,718
another list called last which will have
9728
06:25:32,718 --> 06:25:34,638
values like messi
9729
06:25:34,638 --> 06:25:36,478
then we have
9730
06:25:36,478 --> 06:25:39,440
ronaldo basically the
9731
06:25:39,440 --> 06:25:42,000
surnames
9732
06:25:42,000 --> 06:25:44,638
of leonel christiano and muhammad then
9733
06:25:44,638 --> 06:25:47,600
we have salah
9734
06:25:49,440 --> 06:25:52,000
i'll write my for loop i'll say for
9735
06:25:52,000 --> 06:25:52,718
i
9736
06:25:52,718 --> 06:25:54,878
in
9737
06:25:55,280 --> 06:25:58,558
my zip function or the method i'll pass
9738
06:25:58,558 --> 06:26:01,920
in first comma last which are my
9739
06:26:01,920 --> 06:26:06,000
list names i'll say print
9740
06:26:06,000 --> 06:26:07,040
i
9741
06:26:07,040 --> 06:26:10,638
let me run it you can see here using the
9742
06:26:10,638 --> 06:26:13,040
zip function we have
9743
06:26:13,040 --> 06:26:14,638
iterated through
9744
06:26:14,638 --> 06:26:17,040
both the list so we have lionel messi
9745
06:26:17,040 --> 06:26:20,160
cristiano ronaldo and then mohamed salah
9746
06:26:20,160 --> 06:26:21,280
all right
9747
06:26:21,280 --> 06:26:23,920
now you can combine the elements of
9748
06:26:23,920 --> 06:26:26,718
three lists as well so i'll just paste
9749
06:26:26,718 --> 06:26:29,040
the code here and we'll create another
9750
06:26:29,040 --> 06:26:31,360
list called num
9751
06:26:31,360 --> 06:26:33,840
and this is going to be a list of
9752
06:26:33,840 --> 06:26:35,600
numbers
9753
06:26:35,600 --> 06:26:37,520
let's say 7
9754
06:26:37,520 --> 06:26:39,360
and 8 okay
9755
06:26:39,360 --> 06:26:41,840
now here i'll say for i
9756
06:26:41,840 --> 06:26:42,798
in
9757
06:26:42,798 --> 06:26:47,360
zip first last and num print i
9758
06:26:47,360 --> 06:26:48,718
if i run it
9759
06:26:48,718 --> 06:26:50,558
you see the result here it has zipped
9760
06:26:50,558 --> 06:26:53,040
only the
9761
06:26:53,040 --> 06:26:55,520
first three items in the
9762
06:26:55,520 --> 06:26:58,958
list first last and num so we have
9763
06:26:58,958 --> 06:27:01,200
three items printed
9764
06:27:01,200 --> 06:27:03,520
okay
9765
06:27:04,718 --> 06:27:06,878
now coming to the final section of this
9766
06:27:06,878 --> 06:27:09,280
demo on looping techniques in python
9767
06:27:09,280 --> 06:27:11,200
we'll explore about
9768
06:27:11,200 --> 06:27:13,440
zip underscore longest
9769
06:27:13,440 --> 06:27:16,400
so python's iterator tool or iter tool
9770
06:27:16,400 --> 06:27:17,920
is a module that provides various
9771
06:27:17,920 --> 06:27:20,400
functions that work on iterators to
9772
06:27:20,400 --> 06:27:23,040
produce complex iterators so iterators
9773
06:27:23,040 --> 06:27:25,120
in python is an object that can iterate
9774
06:27:25,120 --> 06:27:27,600
through sequence data types such as list
9775
06:27:27,600 --> 06:27:30,000
tuple string and so on
9776
06:27:30,000 --> 06:27:32,320
so the zip underscore longest iterator
9777
06:27:32,320 --> 06:27:34,878
falls under terminating iterators it
9778
06:27:34,878 --> 06:27:37,120
prints the values of the i tables
9779
06:27:37,120 --> 06:27:40,160
alternatively in sequence if one of the
9780
06:27:40,160 --> 06:27:42,638
iterables is printed fully the remaining
9781
06:27:42,638 --> 06:27:44,240
values are filled with the values
9782
06:27:44,240 --> 06:27:47,600
assigned in the fill value parameter
9783
06:27:47,600 --> 06:27:49,360
so to use this
9784
06:27:49,360 --> 06:27:51,760
function called zip underscore longest
9785
06:27:51,760 --> 06:27:52,510
we need to
9786
06:27:52,510 --> 06:27:54,080
[Music]
9787
06:27:54,080 --> 06:27:56,558
import ziplongist from the item tools
9788
06:27:56,558 --> 06:27:59,200
module so i'll say from
9789
06:27:59,200 --> 06:28:00,798
either tools
9790
06:28:00,798 --> 06:28:02,718
import
9791
06:28:02,718 --> 06:28:04,400
zip underscore
9792
06:28:04,400 --> 06:28:06,958
longest
9793
06:28:07,920 --> 06:28:10,240
okay
9794
06:28:11,760 --> 06:28:14,160
then i'll
9795
06:28:14,160 --> 06:28:16,160
create my
9796
06:28:16,160 --> 06:28:19,840
first list
9797
06:28:20,000 --> 06:28:22,160
i'll say
9798
06:28:22,160 --> 06:28:23,760
let me go to the top and copy the
9799
06:28:23,760 --> 06:28:25,600
elements because we are going to use
9800
06:28:25,600 --> 06:28:28,478
these same elements again
9801
06:28:28,478 --> 06:28:32,280
i'll paste it here
9802
06:28:33,120 --> 06:28:34,638
okay
9803
06:28:34,638 --> 06:28:37,280
and let's see i'll give a comma and
9804
06:28:37,280 --> 06:28:39,200
we're going to
9805
06:28:39,200 --> 06:28:41,920
and i'm again going to use the
9806
06:28:41,920 --> 06:28:45,200
three elements okay
9807
06:28:45,200 --> 06:28:48,638
i'll create another list called last
9808
06:28:48,638 --> 06:28:52,080
and we're going to copy the items
9809
06:28:52,080 --> 06:28:54,718
from the top
9810
06:28:55,280 --> 06:28:58,000
paste it here
9811
06:28:58,798 --> 06:28:59,840
and now
9812
06:28:59,840 --> 06:29:01,120
i'll say
9813
06:29:01,120 --> 06:29:04,638
for let me just scroll down
9814
06:29:04,638 --> 06:29:05,520
i
9815
06:29:05,520 --> 06:29:06,958
in
9816
06:29:06,958 --> 06:29:09,280
zip underscore
9817
06:29:09,280 --> 06:29:11,520
longest
9818
06:29:11,520 --> 06:29:14,558
within brackets i'll say first comma
9819
06:29:14,558 --> 06:29:16,160
last
9820
06:29:16,160 --> 06:29:17,840
we're going to print
9821
06:29:17,840 --> 06:29:20,000
i
9822
06:29:20,160 --> 06:29:21,200
let me
9823
06:29:21,200 --> 06:29:24,638
run it and show you the result
9824
06:29:24,958 --> 06:29:28,478
this should be zip underscore longest
9825
06:29:28,478 --> 06:29:31,920
okay you can see the result here
9826
06:29:31,920 --> 06:29:33,920
for the first
9827
06:29:33,920 --> 06:29:35,760
three items it has printed lionel messi
9828
06:29:35,760 --> 06:29:37,280
cristiano ronaldo and mohamed salah
9829
06:29:37,280 --> 06:29:40,080
which you can see here but for the
9830
06:29:40,080 --> 06:29:43,120
last three items in the first list it
9831
06:29:43,120 --> 06:29:44,400
has given us
9832
06:29:44,400 --> 06:29:47,200
none because there were no values
9833
06:29:47,200 --> 06:29:49,600
now to overcome this problem you can use
9834
06:29:49,600 --> 06:29:52,160
a parameter called fill value
9835
06:29:52,160 --> 06:29:54,718
let me just copy this
9836
06:29:54,718 --> 06:29:57,840
and i'll paste it here
9837
06:29:57,840 --> 06:30:00,478
and the only change that i'm going to do
9838
06:30:00,478 --> 06:30:02,878
here is
9839
06:30:03,360 --> 06:30:06,160
i'm going to say
9840
06:30:06,400 --> 06:30:08,160
i'll give a comma
9841
06:30:08,160 --> 06:30:12,558
i'll use the parameter called fill value
9842
06:30:12,558 --> 06:30:16,000
equal to i'll say na
9843
06:30:16,000 --> 06:30:19,040
if i run it you can see here
9844
06:30:19,040 --> 06:30:21,600
for the last three now we have whatever
9845
06:30:21,600 --> 06:30:22,718
we have
9846
06:30:22,718 --> 06:30:24,798
defined within field value parameter you
9847
06:30:24,798 --> 06:30:27,200
can change this to let's say
9848
06:30:27,200 --> 06:30:29,760
not defined
9849
06:30:29,760 --> 06:30:31,520
there you go
9850
06:30:31,520 --> 06:30:32,878
all right
9851
06:30:32,878 --> 06:30:33,920
so
9852
06:30:33,920 --> 06:30:35,760
these were some of the interesting
9853
06:30:35,760 --> 06:30:38,400
examples that we implemented using
9854
06:30:38,400 --> 06:30:40,240
looping techniques in python we're going
9855
06:30:40,240 --> 06:30:42,400
to learn what is the control statement
9856
06:30:42,400 --> 06:30:43,760
and then understand the different types
9857
06:30:43,760 --> 06:30:45,760
of control statements in python so we
9858
06:30:45,760 --> 06:30:47,760
have continuous statement break
9859
06:30:47,760 --> 06:30:50,160
statement and past statement we are
9860
06:30:50,160 --> 06:30:51,280
going to
9861
06:30:51,280 --> 06:30:53,200
understand these in detail with hands-on
9862
06:30:53,200 --> 06:30:54,958
demonstrations in python on the jupiter
9863
06:30:54,958 --> 06:30:57,360
notebook
9864
06:30:57,440 --> 06:31:00,080
so what is the control statement
9865
06:31:00,080 --> 06:31:02,080
python programming language provides
9866
06:31:02,080 --> 06:31:03,920
loop control statements that can be used
9867
06:31:03,920 --> 06:31:06,000
to change the flow of execution and
9868
06:31:06,000 --> 06:31:07,360
execute a statement or group of
9869
06:31:07,360 --> 06:31:09,280
statements multiple times
9870
06:31:09,280 --> 06:31:11,280
in certain circumstances when some
9871
06:31:11,280 --> 06:31:13,200
particular conditions occur
9872
06:31:13,200 --> 06:31:15,600
we may want to exit from a loop or skip
9873
06:31:15,600 --> 06:31:17,120
some statements of the loop before
9874
06:31:17,120 --> 06:31:19,440
continuing further in the loop
9875
06:31:19,440 --> 06:31:21,760
so to achieve this we need the help of
9876
06:31:21,760 --> 06:31:24,798
control statements in python
9877
06:31:24,798 --> 06:31:26,558
now python provides three types of
9878
06:31:26,558 --> 06:31:29,200
control statements first is continue
9879
06:31:29,200 --> 06:31:32,080
then we have break and then we have pass
9880
06:31:32,080 --> 06:31:34,000
so let's look at them one by one in
9881
06:31:34,000 --> 06:31:36,240
detail
9882
06:31:36,240 --> 06:31:38,478
so the continue statement skips the
9883
06:31:38,478 --> 06:31:40,160
execution of the remaining statements in
9884
06:31:40,160 --> 06:31:41,840
the loop and proceeds with the next
9885
06:31:41,840 --> 06:31:44,400
titration in the loop that is it forces
9886
06:31:44,400 --> 06:31:46,240
the loop for the next iteration ignoring
9887
06:31:46,240 --> 06:31:48,400
the subsequent instructions
9888
06:31:48,400 --> 06:31:50,240
now on the right you can see the
9889
06:31:50,240 --> 06:31:52,478
flowchart of continue statement in
9890
06:31:52,478 --> 06:31:54,958
python so on the top we have the
9891
06:31:54,958 --> 06:31:56,558
test expression
9892
06:31:56,558 --> 06:31:58,878
now if the test expression evaluates to
9893
06:31:58,878 --> 06:32:01,200
false we just exit out of the loop
9894
06:32:01,200 --> 06:32:03,920
but if the test expression is true then
9895
06:32:03,920 --> 06:32:06,958
we head to our continue statement now if
9896
06:32:06,958 --> 06:32:07,920
this is
9897
06:32:07,920 --> 06:32:10,638
yes or true we again check the test
9898
06:32:10,638 --> 06:32:11,680
expression
9899
06:32:11,680 --> 06:32:14,478
if it is false or no then we execute the
9900
06:32:14,478 --> 06:32:16,718
statements after the continue statement
9901
06:32:16,718 --> 06:32:19,280
now we'll understand how the continue
9902
06:32:19,280 --> 06:32:20,558
statement works with the help of
9903
06:32:20,558 --> 06:32:22,558
examples in our demo session
9904
06:32:22,558 --> 06:32:24,878
so let's
9905
06:32:24,878 --> 06:32:26,638
see the next
9906
06:32:26,638 --> 06:32:28,558
statement that we have which is the
9907
06:32:28,558 --> 06:32:31,120
break statement
9908
06:32:31,120 --> 06:32:33,360
now in python the break statement is
9909
06:32:33,360 --> 06:32:35,520
used to terminate the loop whenever the
9910
06:32:35,520 --> 06:32:37,760
python interpreter or the compiler comes
9911
06:32:37,760 --> 06:32:40,080
across the break statement it jumps out
9912
06:32:40,080 --> 06:32:42,080
of the loop so it skips the remaining
9913
06:32:42,080 --> 06:32:44,080
code without further testing the test
9914
06:32:44,080 --> 06:32:46,320
expression of that loop
9915
06:32:46,320 --> 06:32:47,920
now here on the right you can see the
9916
06:32:47,920 --> 06:32:49,360
flowchart how the
9917
06:32:49,360 --> 06:32:51,600
break statement works in python
9918
06:32:51,600 --> 06:32:54,798
on the top we have the test expression
9919
06:32:54,798 --> 06:32:55,920
now if the
9920
06:32:55,920 --> 06:32:58,000
test expression evaluates to false we
9921
06:32:58,000 --> 06:33:00,080
exit the loop but
9922
06:33:00,080 --> 06:33:02,080
if it is true then we
9923
06:33:02,080 --> 06:33:04,478
execute the break statement
9924
06:33:04,478 --> 06:33:06,558
if it is yes then we
9925
06:33:06,558 --> 06:33:08,878
just exit the loop but if it is no then
9926
06:33:08,878 --> 06:33:10,400
we execute the remaining statements
9927
06:33:10,400 --> 06:33:12,798
after the break statement now moving to
9928
06:33:12,798 --> 06:33:15,200
the final control statement that we have
9929
06:33:15,200 --> 06:33:17,280
in python
9930
06:33:17,280 --> 06:33:20,000
which is the pass statement so the pass
9931
06:33:20,000 --> 06:33:21,600
statement in python is used as a
9932
06:33:21,600 --> 06:33:24,240
placeholder for future implementation of
9933
06:33:24,240 --> 06:33:26,798
functions loops etc
9934
06:33:26,798 --> 06:33:28,638
when the pass statement is executed
9935
06:33:28,638 --> 06:33:31,040
nothing happens actually but you avoid
9936
06:33:31,040 --> 06:33:33,040
getting an error when an empty code is
9937
06:33:33,040 --> 06:33:34,160
not allowed
9938
06:33:34,160 --> 06:33:36,878
it is also known as a null statement
9939
06:33:36,878 --> 06:33:37,680
now
9940
06:33:37,680 --> 06:33:39,360
let's directly jump to a jupyter
9941
06:33:39,360 --> 06:33:41,120
notebook where we'll explore
9942
06:33:41,120 --> 06:33:43,040
break continue and pass statements in
9943
06:33:43,040 --> 06:33:44,718
python in detail
9944
06:33:44,718 --> 06:33:47,120
okay so here i am on my jupyter notebook
9945
06:33:47,120 --> 06:33:49,120
let me first go ahead and
9946
06:33:49,120 --> 06:33:51,040
rename my notebook to
9947
06:33:51,040 --> 06:33:55,320
control statements in python
9948
06:33:57,040 --> 06:33:58,958
i'll click on rename
9949
06:33:58,958 --> 06:34:00,160
ok
9950
06:34:00,160 --> 06:34:02,000
now let's see how the
9951
06:34:02,000 --> 06:34:04,478
continue statement works in python
9952
06:34:04,478 --> 06:34:05,600
for that
9953
06:34:05,600 --> 06:34:07,360
i'm going to print
9954
06:34:07,360 --> 06:34:08,718
all the letters
9955
06:34:08,718 --> 06:34:11,760
except the letter i in the string simply
9956
06:34:11,760 --> 06:34:13,840
learn
9957
06:34:13,840 --> 06:34:17,040
so i'll give a comment as
9958
06:34:17,040 --> 06:34:18,160
continue
9959
06:34:18,160 --> 06:34:20,718
statement
9960
06:34:20,958 --> 06:34:23,120
okay
9961
06:34:23,120 --> 06:34:25,680
so i'll first write my for loop i'll say
9962
06:34:25,680 --> 06:34:27,360
for
9963
06:34:27,360 --> 06:34:28,958
letters
9964
06:34:28,958 --> 06:34:32,080
in my string that is
9965
06:34:32,080 --> 06:34:33,920
simply learn
9966
06:34:33,920 --> 06:34:35,520
give a colon
9967
06:34:35,520 --> 06:34:36,798
and then
9968
06:34:36,798 --> 06:34:39,200
i'll say if
9969
06:34:39,200 --> 06:34:42,878
letters is equal to equal to
9970
06:34:42,878 --> 06:34:45,920
let's say i
9971
06:34:46,000 --> 06:34:48,000
then i'm going to use the
9972
06:34:48,000 --> 06:34:51,478
continue statement
9973
06:34:54,160 --> 06:34:56,558
after that i'm going to say
9974
06:34:56,558 --> 06:34:58,958
print
9975
06:34:59,280 --> 06:35:01,680
letter
9976
06:35:01,760 --> 06:35:04,958
give a colon give a comma and then
9977
06:35:04,958 --> 06:35:06,240
we are going to print the individual
9978
06:35:06,240 --> 06:35:08,878
letters in the string
9979
06:35:08,878 --> 06:35:11,520
that is simply learn
9980
06:35:11,520 --> 06:35:12,638
so here
9981
06:35:12,638 --> 06:35:15,040
letters is actually the iterator
9982
06:35:15,040 --> 06:35:16,478
variable
9983
06:35:16,478 --> 06:35:18,718
it could be any valid
9984
06:35:18,718 --> 06:35:21,040
python variable
9985
06:35:21,040 --> 06:35:24,558
finally i am going to say print
9986
06:35:24,558 --> 06:35:28,080
end of execution
9987
06:35:28,320 --> 06:35:30,400
so here what i am doing is i want to
9988
06:35:30,400 --> 06:35:33,120
print all the letters except the letter
9989
06:35:33,120 --> 06:35:36,400
i in my string simply learn so whenever
9990
06:35:36,400 --> 06:35:37,280
the
9991
06:35:37,280 --> 06:35:40,000
i occurs in my string simply learn we'll
9992
06:35:40,000 --> 06:35:41,920
use the continue keyword and then move
9993
06:35:41,920 --> 06:35:43,440
on to the next
9994
06:35:43,440 --> 06:35:46,320
letter in the string simply learn if i
9995
06:35:46,320 --> 06:35:48,478
hit shift enter you can see the result
9996
06:35:48,478 --> 06:35:50,080
there you go
9997
06:35:50,080 --> 06:35:52,400
here you can see we have s
9998
06:35:52,400 --> 06:35:54,878
and the next word was i so we
9999
06:35:54,878 --> 06:35:56,638
skipped
10000
06:35:56,638 --> 06:35:58,320
that iteration and then we continued
10001
06:35:58,320 --> 06:36:00,638
with the next iteration so we have m
10002
06:36:00,638 --> 06:36:01,360
p
10003
06:36:01,360 --> 06:36:04,718
l and again here we
10004
06:36:04,718 --> 06:36:06,798
had another i present in the string so
10005
06:36:06,798 --> 06:36:08,878
we skipped this iteration as well and
10006
06:36:08,878 --> 06:36:12,478
moved on with the next so l e a r and n
10007
06:36:12,478 --> 06:36:15,600
finally we have printed end of execution
10008
06:36:15,600 --> 06:36:17,440
so this is how the continue statement
10009
06:36:17,440 --> 06:36:18,558
works
10010
06:36:18,558 --> 06:36:21,120
you can use it in a for loop or you can
10011
06:36:21,120 --> 06:36:23,280
use it in a while loop as well
10012
06:36:23,280 --> 06:36:25,680
now let's see another example where we
10013
06:36:25,680 --> 06:36:27,680
are going to display only odd numbers
10014
06:36:27,680 --> 06:36:30,638
present in a list of numbers
10015
06:36:30,638 --> 06:36:31,920
so
10016
06:36:31,920 --> 06:36:34,798
i'll say for
10017
06:36:34,798 --> 06:36:37,040
num which is my iterator variable i'll
10018
06:36:37,040 --> 06:36:38,958
say for num in
10019
06:36:38,958 --> 06:36:40,840
my list which
10020
06:36:40,840 --> 06:36:44,240
has a few numbers let's say
10021
06:36:44,240 --> 06:36:44,958
10
10022
06:36:44,958 --> 06:36:47,360
7 i have 17
10023
06:36:47,360 --> 06:36:48,958
let's say 42
10024
06:36:48,958 --> 06:36:52,798
33 19 and let's say 60.
10025
06:36:52,798 --> 06:36:54,000
okay
10026
06:36:54,000 --> 06:36:58,798
so for num in my list which is this one
10027
06:36:59,040 --> 06:37:01,840
i'll say if
10028
06:37:01,840 --> 06:37:03,840
num
10029
06:37:03,840 --> 06:37:08,638
mod 2 is equal to equal to 0 so if the
10030
06:37:08,638 --> 06:37:13,600
remainder is 0 i'll say continue
10031
06:37:16,478 --> 06:37:17,840
and then
10032
06:37:17,840 --> 06:37:19,200
we are going to
10033
06:37:19,200 --> 06:37:21,120
print num
10034
06:37:21,120 --> 06:37:24,798
so what we are doing here is for each
10035
06:37:24,798 --> 06:37:26,160
number
10036
06:37:26,160 --> 06:37:28,160
or each
10037
06:37:28,160 --> 06:37:30,958
item in my list which has
10038
06:37:30,958 --> 06:37:33,520
a list of numbers i am going to check if
10039
06:37:33,520 --> 06:37:35,920
that number is divisible by 2 or not if
10040
06:37:35,920 --> 06:37:37,600
it is divisible by 2 we are going to
10041
06:37:37,600 --> 06:37:40,240
skip the titration and we are going to
10042
06:37:40,240 --> 06:37:43,200
move ahead with the next number
10043
06:37:43,200 --> 06:37:45,760
so let's run it okay here you can see we
10044
06:37:45,760 --> 06:37:48,240
have printed or displayed all the odd
10045
06:37:48,240 --> 06:37:50,638
numbers that were present in my
10046
06:37:50,638 --> 06:37:53,840
list which is this one
10047
06:37:53,840 --> 06:37:58,080
so this was another simple example
10048
06:37:58,080 --> 06:38:01,840
of using a continue statement in python
10049
06:38:01,840 --> 06:38:04,400
now let's move on to the
10050
06:38:04,400 --> 06:38:08,400
next control statement which is break
10051
06:38:08,400 --> 06:38:09,840
again we are going to look at a few
10052
06:38:09,840 --> 06:38:12,478
examples here
10053
06:38:12,478 --> 06:38:14,478
all right
10054
06:38:14,478 --> 06:38:16,798
now the first example
10055
06:38:16,798 --> 06:38:18,958
we are going to see is how to break the
10056
06:38:18,958 --> 06:38:20,638
execution when the
10057
06:38:20,638 --> 06:38:24,958
number five comes up in the tuple
10058
06:38:24,958 --> 06:38:26,798
so for that we are going to use a while
10059
06:38:26,798 --> 06:38:28,878
loop now
10060
06:38:28,878 --> 06:38:32,878
first of all let me declare a tuple
10061
06:38:32,878 --> 06:38:36,958
which has let's say one two three four
10062
06:38:36,958 --> 06:38:39,520
five six seven and eight so numbers from
10063
06:38:39,520 --> 06:38:41,840
one till eight
10064
06:38:41,840 --> 06:38:44,320
i'm going to say
10065
06:38:44,320 --> 06:38:46,478
count equal to zero
10066
06:38:46,478 --> 06:38:51,440
and then i'm going to use a while loop
10067
06:38:51,600 --> 06:38:53,520
i'll say while
10068
06:38:53,520 --> 06:38:57,760
my count is less than 9
10069
06:38:58,958 --> 06:39:00,400
then i'm going to write my print
10070
06:39:00,400 --> 06:39:02,718
statement print
10071
06:39:02,718 --> 06:39:05,680
my tuple which is num
10072
06:39:05,680 --> 06:39:09,840
and using square brackets i'll say count
10073
06:39:09,840 --> 06:39:10,718
now
10074
06:39:10,718 --> 06:39:13,520
we're going to increment count by 1 so
10075
06:39:13,520 --> 06:39:14,718
i'll say
10076
06:39:14,718 --> 06:39:16,798
count equal to
10077
06:39:16,798 --> 06:39:17,760
count
10078
06:39:17,760 --> 06:39:19,920
plus one
10079
06:39:19,920 --> 06:39:22,638
next we will check if
10080
06:39:22,638 --> 06:39:25,520
the value of count is equal to equal to
10081
06:39:25,520 --> 06:39:26,878
five
10082
06:39:26,878 --> 06:39:29,120
i'll give a colon and then we are going
10083
06:39:29,120 --> 06:39:31,760
to break out of the loop
10084
06:39:31,760 --> 06:39:33,440
finally
10085
06:39:33,440 --> 06:39:34,958
we'll print
10086
06:39:34,958 --> 06:39:38,080
end of execution
10087
06:39:39,280 --> 06:39:40,478
okay
10088
06:39:40,478 --> 06:39:41,840
so
10089
06:39:41,840 --> 06:39:43,120
this code
10090
06:39:43,120 --> 06:39:45,680
will help break the execution when the
10091
06:39:45,680 --> 06:39:47,760
number five comes up in the
10092
06:39:47,760 --> 06:39:49,920
tuple which is num
10093
06:39:49,920 --> 06:39:51,920
so let's run it
10094
06:39:51,920 --> 06:39:54,718
okay so here you can see
10095
06:39:54,718 --> 06:39:58,080
as soon as the number five
10096
06:39:58,080 --> 06:39:59,600
occurred in the tuple
10097
06:39:59,600 --> 06:40:02,878
we broke out of the loop and
10098
06:40:02,878 --> 06:40:04,558
the break statement terminated the
10099
06:40:04,558 --> 06:40:06,558
execution of the loop so
10100
06:40:06,558 --> 06:40:09,600
you can see here we did not print 6 7
10101
06:40:09,600 --> 06:40:12,320
and 8 and finally we printed our last
10102
06:40:12,320 --> 06:40:13,920
statement which was present after the
10103
06:40:13,920 --> 06:40:16,958
break statement that is end of execution
10104
06:40:16,958 --> 06:40:18,718
okay
10105
06:40:18,718 --> 06:40:20,080
so i hope
10106
06:40:20,080 --> 06:40:21,920
this helped you understand how the break
10107
06:40:21,920 --> 06:40:23,680
statement works in
10108
06:40:23,680 --> 06:40:25,520
python
10109
06:40:25,520 --> 06:40:27,200
we're also going to see another example
10110
06:40:27,200 --> 06:40:28,240
now
10111
06:40:28,240 --> 06:40:30,638
so this is using a for loop
10112
06:40:30,638 --> 06:40:32,320
so we are going to break and terminate
10113
06:40:32,320 --> 06:40:34,718
the execution when the letter p occurs
10114
06:40:34,718 --> 06:40:36,000
in the string
10115
06:40:36,000 --> 06:40:37,520
simply alone
10116
06:40:37,520 --> 06:40:39,840
so earlier we had seen one example which
10117
06:40:39,840 --> 06:40:41,440
was our first
10118
06:40:41,440 --> 06:40:43,920
example where we saw how to use continue
10119
06:40:43,920 --> 06:40:45,840
statement for the same string simply
10120
06:40:45,840 --> 06:40:46,638
loan
10121
06:40:46,638 --> 06:40:48,000
this time
10122
06:40:48,000 --> 06:40:50,400
we are going to use the break statement
10123
06:40:50,400 --> 06:40:51,200
to
10124
06:40:51,200 --> 06:40:52,878
terminate the execution when the letter
10125
06:40:52,878 --> 06:40:55,920
p occurs in the string simply alone
10126
06:40:55,920 --> 06:40:57,760
so i'll start with a for loop i'll say
10127
06:40:57,760 --> 06:40:58,798
for
10128
06:40:58,798 --> 06:41:00,798
letters in
10129
06:41:00,798 --> 06:41:03,040
my string which is
10130
06:41:03,040 --> 06:41:04,638
simply learn
10131
06:41:04,638 --> 06:41:06,320
i'll give a colon
10132
06:41:06,320 --> 06:41:08,718
and then i'll say
10133
06:41:08,718 --> 06:41:11,040
print
10134
06:41:11,600 --> 06:41:13,440
my message that is
10135
06:41:13,440 --> 06:41:15,840
letter
10136
06:41:16,478 --> 06:41:21,280
give another colon i'll give a comma and
10137
06:41:21,280 --> 06:41:23,280
i'll put my
10138
06:41:23,280 --> 06:41:27,360
i treat a variable which is letters
10139
06:41:27,680 --> 06:41:31,120
then i'll say if
10140
06:41:31,200 --> 06:41:33,280
letters
10141
06:41:33,280 --> 06:41:35,360
equal to equal to
10142
06:41:35,360 --> 06:41:38,240
p will give a colon and then
10143
06:41:38,240 --> 06:41:40,878
i want to break out of the loop
10144
06:41:40,878 --> 06:41:42,478
now let me just
10145
06:41:42,478 --> 06:41:43,600
print it
10146
06:41:43,600 --> 06:41:45,120
you can see here
10147
06:41:45,120 --> 06:41:46,240
as soon as
10148
06:41:46,240 --> 06:41:48,240
the letter p occurred in the iteration
10149
06:41:48,240 --> 06:41:49,600
we
10150
06:41:49,600 --> 06:41:52,400
came out of the loop so
10151
06:41:52,400 --> 06:41:54,400
the break statement executed the above
10152
06:41:54,400 --> 06:41:56,638
lines of code and
10153
06:41:56,638 --> 06:41:58,320
the moment is
10154
06:41:58,320 --> 06:42:00,558
saw the letter p it just terminated the
10155
06:42:00,558 --> 06:42:03,200
loop and hence you can see we did not
10156
06:42:03,200 --> 06:42:04,478
print the remaining
10157
06:42:04,478 --> 06:42:07,120
letters in the string simply learn
10158
06:42:07,120 --> 06:42:09,120
okay
10159
06:42:09,120 --> 06:42:11,440
now moving to our
10160
06:42:11,440 --> 06:42:13,200
final section of the demo where we will
10161
06:42:13,200 --> 06:42:14,718
see how to use
10162
06:42:14,718 --> 06:42:17,200
pass statements in python
10163
06:42:17,200 --> 06:42:19,280
so the first example we are going to see
10164
06:42:19,280 --> 06:42:21,520
under past statement is
10165
06:42:21,520 --> 06:42:23,920
to check if the number is odd
10166
06:42:23,920 --> 06:42:25,440
so if the number is odd we are going to
10167
06:42:25,440 --> 06:42:26,718
pass it
10168
06:42:26,718 --> 06:42:27,760
else
10169
06:42:27,760 --> 06:42:29,920
we are going to print only the even
10170
06:42:29,920 --> 06:42:32,080
numbers
10171
06:42:32,080 --> 06:42:34,000
okay
10172
06:42:34,000 --> 06:42:37,360
so for that i'm going to use
10173
06:42:37,760 --> 06:42:39,920
a for loop but before that let me just
10174
06:42:39,920 --> 06:42:41,040
give a
10175
06:42:41,040 --> 06:42:45,040
comment i'll say pass statement
10176
06:42:45,040 --> 06:42:47,360
okay
10177
06:42:47,360 --> 06:42:49,040
so in the first example we'll check if
10178
06:42:49,040 --> 06:42:50,958
the number is odd then we are going to
10179
06:42:50,958 --> 06:42:54,400
pass it and only print the even numbers
10180
06:42:54,400 --> 06:42:56,160
so for that i'm going to use a for loop
10181
06:42:56,160 --> 06:42:58,958
i'll say for number in
10182
06:42:58,958 --> 06:43:02,798
range let's say i'm taking the range
10183
06:43:02,798 --> 06:43:05,840
between 10 and 25
10184
06:43:05,840 --> 06:43:09,360
give a colon i'll say if
10185
06:43:09,760 --> 06:43:11,280
number
10186
06:43:11,280 --> 06:43:12,320
mod
10187
06:43:12,320 --> 06:43:13,760
2
10188
06:43:13,760 --> 06:43:17,120
if it is not equal to
10189
06:43:17,120 --> 06:43:18,718
0
10190
06:43:18,718 --> 06:43:21,280
give a colon then we are going to
10191
06:43:21,280 --> 06:43:22,718
pass it
10192
06:43:22,718 --> 06:43:25,040
so this means if the number is odd then
10193
06:43:25,040 --> 06:43:27,200
just pass it
10194
06:43:27,200 --> 06:43:29,840
else
10195
06:43:31,360 --> 06:43:34,160
i'll say print
10196
06:43:34,160 --> 06:43:36,320
the
10197
06:43:36,320 --> 06:43:37,920
number
10198
06:43:37,920 --> 06:43:39,280
is
10199
06:43:39,280 --> 06:43:41,120
give a colon
10200
06:43:41,120 --> 06:43:43,520
a comma and then i'll pass in my
10201
06:43:43,520 --> 06:43:46,080
iterator variable which is number
10202
06:43:46,080 --> 06:43:48,718
now let's run it
10203
06:43:48,718 --> 06:43:51,120
okay you can see here
10204
06:43:51,120 --> 06:43:52,558
in the range
10205
06:43:52,558 --> 06:43:54,958
10 and 25 we have
10206
06:43:54,958 --> 06:43:57,520
excluded all the
10207
06:43:57,520 --> 06:43:59,840
odd numbers because we used the pass
10208
06:43:59,840 --> 06:44:03,040
statement so it passed printing all the
10209
06:44:03,040 --> 06:44:05,280
odd numbers and it just printed whatever
10210
06:44:05,280 --> 06:44:07,520
was there in the else part that is the
10211
06:44:07,520 --> 06:44:10,798
even numbers so we have 10 12 14 then we
10212
06:44:10,798 --> 06:44:13,600
have 20 22 and 24
10213
06:44:13,600 --> 06:44:15,280
so these are all the even numbers that
10214
06:44:15,280 --> 06:44:16,558
lie between
10215
06:44:16,558 --> 06:44:18,798
10 and 25
10216
06:44:18,798 --> 06:44:19,840
all right
10217
06:44:19,840 --> 06:44:22,798
now coming to the final demo
10218
06:44:22,798 --> 06:44:24,798
so here i'm going to write a user
10219
06:44:24,798 --> 06:44:27,440
defined function
10220
06:44:27,440 --> 06:44:30,080
to print any number other than
10221
06:44:30,080 --> 06:44:31,680
10.
10222
06:44:31,680 --> 06:44:34,080
so again we are going to use a pass
10223
06:44:34,080 --> 06:44:36,638
statement for this
10224
06:44:36,638 --> 06:44:39,120
so let me write my user defined function
10225
06:44:39,120 --> 06:44:41,600
i'll use the def keyword
10226
06:44:41,600 --> 06:44:43,680
and my function name let's say print
10227
06:44:43,680 --> 06:44:45,280
underscore num
10228
06:44:45,280 --> 06:44:47,120
then i'll pass in my parameter let's say
10229
06:44:47,120 --> 06:44:49,120
number
10230
06:44:49,120 --> 06:44:51,200
i'll say if
10231
06:44:51,200 --> 06:44:54,240
number is
10232
06:44:54,240 --> 06:44:55,360
10
10233
06:44:55,360 --> 06:44:57,120
then i'll use the
10234
06:44:57,120 --> 06:45:00,000
pass statement
10235
06:45:00,320 --> 06:45:03,680
otherwise i'll say else
10236
06:45:03,680 --> 06:45:06,680
print
10237
06:45:06,798 --> 06:45:09,120
the number
10238
06:45:09,120 --> 06:45:11,280
is
10239
06:45:11,280 --> 06:45:14,478
i'll give a comma and then i'll say
10240
06:45:14,478 --> 06:45:17,440
number which is there in my parameter
10241
06:45:17,440 --> 06:45:19,360
now let's run it
10242
06:45:19,360 --> 06:45:21,520
now we are going to call the function
10243
06:45:21,520 --> 06:45:23,200
using the
10244
06:45:23,200 --> 06:45:26,240
function name which is print num
10245
06:45:26,240 --> 06:45:27,600
if i say
10246
06:45:27,600 --> 06:45:30,160
print num 20
10247
06:45:30,160 --> 06:45:32,160
it will print my number saying the
10248
06:45:32,160 --> 06:45:35,520
number is 20.
10249
06:45:35,520 --> 06:45:37,120
let's copy this
10250
06:45:37,120 --> 06:45:38,878
i'll paste it here
10251
06:45:38,878 --> 06:45:41,680
let's say i'll say
10252
06:45:41,840 --> 06:45:43,520
25000
10253
06:45:43,520 --> 06:45:45,920
it's going to print 25000
10254
06:45:45,920 --> 06:45:46,798
but
10255
06:45:46,798 --> 06:45:51,200
if i say print num 10
10256
06:45:51,200 --> 06:45:54,080
you can see it here that
10257
06:45:54,080 --> 06:45:56,320
python has just passed it because we had
10258
06:45:56,320 --> 06:45:58,478
given in our condition if number is 10
10259
06:45:58,478 --> 06:46:00,160
then just pass it
10260
06:46:00,160 --> 06:46:03,440
so there was no output for 10
10261
06:46:03,440 --> 06:46:04,558
all right
10262
06:46:04,558 --> 06:46:06,638
so that brings us to the end of this
10263
06:46:06,638 --> 06:46:08,878
demo session on
10264
06:46:08,878 --> 06:46:11,600
control statements in python so we
10265
06:46:11,600 --> 06:46:13,280
looked at how the continue statement
10266
06:46:13,280 --> 06:46:15,520
works then we saw how the
10267
06:46:15,520 --> 06:46:17,600
break statement works we looked at two
10268
06:46:17,600 --> 06:46:20,400
examples one was with while loops and
10269
06:46:20,400 --> 06:46:22,798
the other one was with for loop and
10270
06:46:22,798 --> 06:46:24,400
finally we saw
10271
06:46:24,400 --> 06:46:26,718
two examples on past statements one was
10272
06:46:26,718 --> 06:46:27,680
using
10273
06:46:27,680 --> 06:46:29,840
for and if else and the other one was
10274
06:46:29,840 --> 06:46:32,558
using a user defined function and inside
10275
06:46:32,558 --> 06:46:34,878
that we use the file's condition
10276
06:46:34,878 --> 06:46:36,798
conditional statements in python perform
10277
06:46:36,798 --> 06:46:38,638
specific operations and take actions
10278
06:46:38,638 --> 06:46:40,478
depending on whether a specific boolean
10279
06:46:40,478 --> 06:46:43,200
constraint evaluates to true or false
10280
06:46:43,200 --> 06:46:45,360
conditional statements are handled by
10281
06:46:45,360 --> 06:46:47,920
blocks of if statements in python
10282
06:46:47,920 --> 06:46:49,840
now conditional statements are also
10283
06:46:49,840 --> 06:46:52,798
known as decision making statements
10284
06:46:52,798 --> 06:46:54,558
so python provides different types of
10285
06:46:54,558 --> 06:46:56,558
conditional statements that you can use
10286
06:46:56,558 --> 06:46:58,400
to make decisions
10287
06:46:58,400 --> 06:47:01,200
we have the simple if statement
10288
06:47:01,200 --> 06:47:03,600
then we have if else statement
10289
06:47:03,600 --> 06:47:06,638
elif statement the nested if statement
10290
06:47:06,638 --> 06:47:09,440
as well as nested if statement we will
10291
06:47:09,440 --> 06:47:11,200
learn them one by one with the help of
10292
06:47:11,200 --> 06:47:14,798
the syntax and code examples
10293
06:47:17,600 --> 06:47:19,280
so this is how the syntax of if
10294
06:47:19,280 --> 06:47:21,520
statement looks like so you pass in the
10295
06:47:21,520 --> 06:47:23,920
if condition and then we have
10296
06:47:23,920 --> 06:47:25,920
a set of statements inside the if
10297
06:47:25,920 --> 06:47:28,400
condition
10298
06:47:28,400 --> 06:47:30,240
so on the right side you can see the
10299
06:47:30,240 --> 06:47:32,478
flow diagram how the if statement gets
10300
06:47:32,478 --> 06:47:35,280
evaluated so first we have the test
10301
06:47:35,280 --> 06:47:36,558
expression
10302
06:47:36,558 --> 06:47:39,120
that is the if condition now if this
10303
06:47:39,120 --> 06:47:42,000
expression evaluates to true then we go
10304
06:47:42,000 --> 06:47:44,240
inside the body of the if statement so
10305
06:47:44,240 --> 06:47:46,638
this is basically
10306
06:47:46,638 --> 06:47:48,878
the body of the
10307
06:47:48,878 --> 06:47:51,680
if statement that contains a list of
10308
06:47:51,680 --> 06:47:54,638
statements or blocks of statements
10309
06:47:54,638 --> 06:47:57,120
now if the test expression evaluates to
10310
06:47:57,120 --> 06:47:58,160
false
10311
06:47:58,160 --> 06:47:59,440
we execute
10312
06:47:59,440 --> 06:48:01,600
the statements that are present after
10313
06:48:01,600 --> 06:48:03,280
the if condition
10314
06:48:03,280 --> 06:48:04,478
all right
10315
06:48:04,478 --> 06:48:06,400
now let me take you to my jupyter
10316
06:48:06,400 --> 06:48:08,638
notebook where we will
10317
06:48:08,638 --> 06:48:12,160
understand if statement with an example
10318
06:48:12,160 --> 06:48:15,040
okay so here i am on my
10319
06:48:15,040 --> 06:48:17,600
jupyter notebook i'll just rename this
10320
06:48:17,600 --> 06:48:18,958
to
10321
06:48:18,958 --> 06:48:22,760
conditional statements
10322
06:48:24,400 --> 06:48:26,958
i'll see it as demo
10323
06:48:26,958 --> 06:48:29,920
and we'll rename it okay
10324
06:48:29,920 --> 06:48:33,120
i'll just expand the
10325
06:48:33,120 --> 06:48:34,478
screen size
10326
06:48:34,478 --> 06:48:37,280
all right okay
10327
06:48:37,280 --> 06:48:40,638
so first of all let's
10328
06:48:40,638 --> 06:48:43,200
write a simple if statement
10329
06:48:43,200 --> 06:48:46,320
i have given a comment here
10330
06:48:46,320 --> 06:48:49,040
suppose we have a variable called
10331
06:48:49,040 --> 06:48:51,840
grade
10332
06:48:52,798 --> 06:48:55,600
and the value of grade is 80
10333
06:48:55,600 --> 06:48:57,360
then i'll write my
10334
06:48:57,360 --> 06:49:00,240
if statement as if
10335
06:49:00,240 --> 06:49:01,760
grade
10336
06:49:01,760 --> 06:49:05,600
is greater than equal to
10337
06:49:05,600 --> 06:49:07,280
50
10338
06:49:07,280 --> 06:49:09,760
then i'll give a
10339
06:49:09,760 --> 06:49:11,760
colon
10340
06:49:11,760 --> 06:49:15,360
then i'll write a print statement print
10341
06:49:15,360 --> 06:49:17,760
passed in the
10342
06:49:17,760 --> 06:49:20,160
exam
10343
06:49:20,160 --> 06:49:21,920
now this is how you write a simple if
10344
06:49:21,920 --> 06:49:24,240
statement so you have the if keyword
10345
06:49:24,240 --> 06:49:25,920
followed by
10346
06:49:25,920 --> 06:49:28,878
the condition so here my condition is
10347
06:49:28,878 --> 06:49:30,478
i'm checking whether the grade is
10348
06:49:30,478 --> 06:49:33,760
greater than equal to 50. now here
10349
06:49:33,760 --> 06:49:36,638
i've already defined my grade to 80
10350
06:49:36,638 --> 06:49:38,878
so this if statement or the if condition
10351
06:49:38,878 --> 06:49:40,798
will check whether 80 is greater than
10352
06:49:40,798 --> 06:49:44,080
equal to 50. now this is true
10353
06:49:44,080 --> 06:49:44,958
then
10354
06:49:44,958 --> 06:49:47,760
my print statement gets executed
10355
06:49:47,760 --> 06:49:50,240
let me run it and show you the result
10356
06:49:50,240 --> 06:49:52,558
there you go here you can see we have
10357
06:49:52,558 --> 06:49:54,478
our
10358
06:49:54,478 --> 06:49:56,718
printed message as passed in the exam
10359
06:49:56,718 --> 06:49:59,920
because 80 is greater than equal to 50.
10360
06:49:59,920 --> 06:50:02,160
now what if i change this to let's say
10361
06:50:02,160 --> 06:50:03,520
30
10362
06:50:03,520 --> 06:50:06,080
if i run this there is no output here
10363
06:50:06,080 --> 06:50:07,760
the reason being
10364
06:50:07,760 --> 06:50:09,360
30 is
10365
06:50:09,360 --> 06:50:12,798
greater than equal to 50 is false so
10366
06:50:12,798 --> 06:50:16,558
my print statement did not execute
10367
06:50:16,558 --> 06:50:18,798
ok
10368
06:50:18,798 --> 06:50:21,520
now if you want
10369
06:50:21,760 --> 06:50:23,120
you can
10370
06:50:23,120 --> 06:50:25,600
also print
10371
06:50:25,600 --> 06:50:27,360
another statement outside the if
10372
06:50:27,360 --> 06:50:29,280
condition
10373
06:50:29,280 --> 06:50:32,240
i'll give the message as
10374
06:50:32,240 --> 06:50:34,240
execute
10375
06:50:34,240 --> 06:50:35,760
statement
10376
06:50:35,760 --> 06:50:37,520
after if
10377
06:50:37,520 --> 06:50:39,520
now one thing to remember that
10378
06:50:39,520 --> 06:50:41,680
this print statement is outside the if
10379
06:50:41,680 --> 06:50:42,878
condition
10380
06:50:42,878 --> 06:50:44,400
so if i run it
10381
06:50:44,400 --> 06:50:46,240
only my
10382
06:50:46,240 --> 06:50:48,320
last print statement gets executed which
10383
06:50:48,320 --> 06:50:50,000
is outside the
10384
06:50:50,000 --> 06:50:52,320
if condition
10385
06:50:52,320 --> 06:50:53,200
okay
10386
06:50:53,200 --> 06:50:55,440
now let's write another
10387
06:50:55,440 --> 06:50:56,638
simple
10388
06:50:56,638 --> 06:50:58,400
if statement
10389
06:50:58,400 --> 06:50:59,920
now this time
10390
06:50:59,920 --> 06:51:01,680
we are not going to define the grade
10391
06:51:01,680 --> 06:51:03,280
value or
10392
06:51:03,280 --> 06:51:05,520
predefine the grade value here we are
10393
06:51:05,520 --> 06:51:07,760
going to take the grade value as an
10394
06:51:07,760 --> 06:51:11,120
input that is a user input so i'll write
10395
06:51:11,120 --> 06:51:13,360
grade equal to
10396
06:51:13,360 --> 06:51:15,920
now the way to take a user input is by
10397
06:51:15,920 --> 06:51:18,160
using the input function
10398
06:51:18,160 --> 06:51:21,440
so first of all we need to convert
10399
06:51:21,440 --> 06:51:24,160
our entry into an integer so i'll write
10400
06:51:24,160 --> 06:51:26,000
int function and inside that i'll pass
10401
06:51:26,000 --> 06:51:29,040
in my input function
10402
06:51:29,280 --> 06:51:31,520
then
10403
06:51:35,280 --> 06:51:40,320
i'll give my message as enter the number
10404
06:51:40,400 --> 06:51:42,558
all right so this statement will take
10405
06:51:42,558 --> 06:51:43,680
the
10406
06:51:43,680 --> 06:51:45,680
number from the user
10407
06:51:45,680 --> 06:51:48,718
now i'll write my if statement if
10408
06:51:48,718 --> 06:51:51,840
grade is greater than equal to
10409
06:51:51,840 --> 06:51:53,760
50
10410
06:51:53,760 --> 06:51:56,000
then
10411
06:51:56,080 --> 06:51:58,160
it won't give a space here
10412
06:51:58,160 --> 06:52:00,400
let's delete this piece
10413
06:52:00,400 --> 06:52:03,840
then i'll write print
10414
06:52:03,840 --> 06:52:05,680
you have
10415
06:52:05,680 --> 06:52:08,798
passed in the exam
10416
06:52:08,958 --> 06:52:11,280
okay
10417
06:52:12,878 --> 06:52:16,320
let's run it you see here
10418
06:52:16,320 --> 06:52:18,878
my program is asking for the
10419
06:52:18,878 --> 06:52:21,280
user input it says enter the number i'll
10420
06:52:21,280 --> 06:52:24,400
give my number as let's say 60.
10421
06:52:24,400 --> 06:52:26,638
if i hit enter
10422
06:52:26,638 --> 06:52:28,718
that gives me the result passed in the
10423
06:52:28,718 --> 06:52:31,120
exam
10424
06:52:31,840 --> 06:52:33,680
now let's run it again
10425
06:52:33,680 --> 06:52:36,798
this time i'm going to give as let's say
10426
06:52:36,798 --> 06:52:38,080
35
10427
06:52:38,080 --> 06:52:39,520
if i run it
10428
06:52:39,520 --> 06:52:41,920
there is
10429
06:52:42,958 --> 06:52:45,520
no output here the reason being our
10430
06:52:45,520 --> 06:52:48,638
grade 35 is not greater than equal to 50
10431
06:52:48,638 --> 06:52:51,440
so our print statement did not execute
10432
06:52:51,440 --> 06:52:53,840
all right okay now let me give a space
10433
06:52:53,840 --> 06:52:56,400
here we'll run it again and this time
10434
06:52:56,400 --> 06:53:00,638
we'll pass our grid as let's say
10435
06:53:00,840 --> 06:53:02,478
75
10436
06:53:02,478 --> 06:53:04,958
let me run it you can see here
10437
06:53:04,958 --> 06:53:07,040
it gives me the message passed in the
10438
06:53:07,040 --> 06:53:09,360
exam
10439
06:53:09,360 --> 06:53:11,760
now moving to our next statement we have
10440
06:53:11,760 --> 06:53:14,878
the if else statement so this is how the
10441
06:53:14,878 --> 06:53:17,040
syntax of if else statement looks like
10442
06:53:17,040 --> 06:53:19,440
so first you have the if condition
10443
06:53:19,440 --> 06:53:21,120
followed by a colon
10444
06:53:21,120 --> 06:53:23,440
if this condition evaluates to true then
10445
06:53:23,440 --> 06:53:25,040
we execute the list of statements that
10446
06:53:25,040 --> 06:53:27,920
are present inside this if condition
10447
06:53:27,920 --> 06:53:28,958
else
10448
06:53:28,958 --> 06:53:31,360
we have the else block here
10449
06:53:31,360 --> 06:53:33,760
if this condition returns
10450
06:53:33,760 --> 06:53:36,160
false then we execute the
10451
06:53:36,160 --> 06:53:38,320
list of else statements
10452
06:53:38,320 --> 06:53:40,000
so here is the
10453
06:53:40,000 --> 06:53:42,080
flow of how the
10454
06:53:42,080 --> 06:53:44,320
if statement executes the different
10455
06:53:44,320 --> 06:53:46,638
statements
10456
06:53:46,638 --> 06:53:49,680
so first we check the test expression
10457
06:53:49,680 --> 06:53:51,680
if that evaluates to true
10458
06:53:51,680 --> 06:53:54,400
then we have the body of if
10459
06:53:54,400 --> 06:53:57,600
and if it is false then we have the
10460
06:53:57,600 --> 06:53:59,200
body of else
10461
06:53:59,200 --> 06:54:00,320
and later
10462
06:54:00,320 --> 06:54:01,680
there are any statements that are
10463
06:54:01,680 --> 06:54:03,760
present outside the effects block will
10464
06:54:03,760 --> 06:54:05,520
execute those statements
10465
06:54:05,520 --> 06:54:07,040
now let's head over to our jupyter
10466
06:54:07,040 --> 06:54:09,760
notebook and we'll see
10467
06:54:09,760 --> 06:54:13,280
how the file statement works by example
10468
06:54:13,280 --> 06:54:14,160
okay
10469
06:54:14,160 --> 06:54:15,680
so here
10470
06:54:15,680 --> 06:54:19,040
i'll show you another example of using
10471
06:54:19,040 --> 06:54:21,360
two if statements
10472
06:54:21,360 --> 06:54:24,638
so i'll define my grade as 40
10473
06:54:24,638 --> 06:54:26,638
i'll write if
10474
06:54:26,638 --> 06:54:28,240
grade
10475
06:54:28,240 --> 06:54:31,280
is greater than equal to 50
10476
06:54:31,280 --> 06:54:32,638
then will
10477
06:54:32,638 --> 06:54:35,040
print
10478
06:54:36,000 --> 06:54:38,320
let's say
10479
06:54:38,320 --> 06:54:41,320
pass
10480
06:54:41,920 --> 06:54:43,520
then
10481
06:54:43,520 --> 06:54:45,680
i'll write another if statement
10482
06:54:45,680 --> 06:54:48,478
i'll say grade is
10483
06:54:48,478 --> 06:54:50,160
less than
10484
06:54:50,160 --> 06:54:52,638
50
10485
06:54:52,798 --> 06:54:55,840
then we'll print
10486
06:54:56,400 --> 06:54:58,638
fail
10487
06:54:59,680 --> 06:55:03,280
now here i'm using two if statements let
10488
06:55:03,280 --> 06:55:04,478
me run it
10489
06:55:04,478 --> 06:55:05,760
you see here
10490
06:55:05,760 --> 06:55:07,440
i have my result as fail the reason
10491
06:55:07,440 --> 06:55:09,920
being our grade 40 is less than 50 and
10492
06:55:09,920 --> 06:55:13,280
hence it has printed fail
10493
06:55:14,400 --> 06:55:16,478
now instead of using two if statements
10494
06:55:16,478 --> 06:55:19,200
you can use the if else statement
10495
06:55:19,200 --> 06:55:21,680
so in this case we'll have our if
10496
06:55:21,680 --> 06:55:23,440
statement checked for if grade is
10497
06:55:23,440 --> 06:55:25,680
greater than equal to 50
10498
06:55:25,680 --> 06:55:28,080
if this is true then we'll print
10499
06:55:28,080 --> 06:55:29,360
whatever is there inside the if
10500
06:55:29,360 --> 06:55:31,920
statement but in case this evaluates to
10501
06:55:31,920 --> 06:55:33,840
false we have another
10502
06:55:33,840 --> 06:55:35,840
else block that we are going to write
10503
06:55:35,840 --> 06:55:37,600
let me show you how to write the full
10504
06:55:37,600 --> 06:55:40,080
statement
10505
06:55:40,160 --> 06:55:42,958
so i will give a comment as
10506
06:55:42,958 --> 06:55:44,798
if else
10507
06:55:44,798 --> 06:55:46,878
okay
10508
06:55:46,878 --> 06:55:48,840
i'll say
10509
06:55:48,840 --> 06:55:53,040
grade equal to 40
10510
06:55:53,120 --> 06:55:54,478
then
10511
06:55:54,478 --> 06:55:57,600
we'll check if grade
10512
06:55:57,600 --> 06:56:01,600
is greater than equal to 50
10513
06:56:02,840 --> 06:56:04,558
print
10514
06:56:04,558 --> 06:56:06,798
pass
10515
06:56:06,798 --> 06:56:08,320
then
10516
06:56:08,320 --> 06:56:10,478
instead of using
10517
06:56:10,478 --> 06:56:14,958
the if statement again i'll write else
10518
06:56:14,958 --> 06:56:18,798
followed by a colon and then i'll write
10519
06:56:18,798 --> 06:56:20,240
print
10520
06:56:20,240 --> 06:56:21,680
fill
10521
06:56:21,680 --> 06:56:24,638
make sure this field is inside
10522
06:56:24,638 --> 06:56:26,478
double quotations
10523
06:56:26,478 --> 06:56:27,760
if i run it
10524
06:56:27,760 --> 06:56:30,160
it gives me the result fail the reason
10525
06:56:30,160 --> 06:56:31,280
being
10526
06:56:31,280 --> 06:56:33,600
40 is not less than
10527
06:56:33,600 --> 06:56:36,558
repeat 40 is not greater than equal to
10528
06:56:36,558 --> 06:56:39,600
50 so hence it has printed field
10529
06:56:39,600 --> 06:56:41,760
now instead of taking
10530
06:56:41,760 --> 06:56:43,440
the value as 40
10531
06:56:43,440 --> 06:56:45,120
we can have the
10532
06:56:45,120 --> 06:56:47,440
user give the input so we'll use the
10533
06:56:47,440 --> 06:56:49,200
input function
10534
06:56:49,200 --> 06:56:51,680
i'll say
10535
06:56:51,680 --> 06:56:53,600
enter
10536
06:56:53,600 --> 06:56:55,440
the
10537
06:56:55,440 --> 06:56:57,840
grade
10538
06:56:58,558 --> 06:57:01,440
let's run it again you see here my
10539
06:57:01,440 --> 06:57:03,920
program is asking for the user input
10540
06:57:03,920 --> 06:57:06,000
let's say i'll give 80
10541
06:57:06,000 --> 06:57:07,120
will
10542
06:57:07,120 --> 06:57:08,400
run it
10543
06:57:08,400 --> 06:57:10,558
it says pass because 80 is greater than
10544
06:57:10,558 --> 06:57:13,280
equal to 50 let me run it again this
10545
06:57:13,280 --> 06:57:16,240
time let's see i'll pass
10546
06:57:16,240 --> 06:57:18,320
my grid as 45
10547
06:57:18,320 --> 06:57:20,080
will run it
10548
06:57:20,080 --> 06:57:22,478
it gives us fail
10549
06:57:22,478 --> 06:57:24,878
all right
10550
06:57:25,440 --> 06:57:28,878
now moving to our next
10551
06:57:28,878 --> 06:57:30,400
conditional statement we have the
10552
06:57:30,400 --> 06:57:32,320
ellipse statement
10553
06:57:32,320 --> 06:57:34,080
now this is how the syntax of ellipse
10554
06:57:34,080 --> 06:57:35,760
statement looks like
10555
06:57:35,760 --> 06:57:38,478
so here first we have the if condition
10556
06:57:38,478 --> 06:57:40,320
now if the if condition evaluates to
10557
06:57:40,320 --> 06:57:43,200
true we have the body of if there is
10558
06:57:43,200 --> 06:57:45,600
basically a set of statements
10559
06:57:45,600 --> 06:57:47,840
now if the first test expression or the
10560
06:57:47,840 --> 06:57:50,320
if condition evaluates to false then our
10561
06:57:50,320 --> 06:57:52,958
alif condition gets executed now if our
10562
06:57:52,958 --> 06:57:55,120
l if condition is true then we have the
10563
06:57:55,120 --> 06:57:57,440
body of alif so this is the ellipse
10564
06:57:57,440 --> 06:57:59,040
statement body
10565
06:57:59,040 --> 06:58:00,798
finally
10566
06:58:00,798 --> 06:58:03,120
if the alif condition also evaluates to
10567
06:58:03,120 --> 06:58:05,760
false then we have the block of else
10568
06:58:05,760 --> 06:58:07,600
statements you can see here
10569
06:58:07,600 --> 06:58:08,798
we have
10570
06:58:08,798 --> 06:58:11,360
body of else and later if there are any
10571
06:58:11,360 --> 06:58:13,280
statements outside this
10572
06:58:13,280 --> 06:58:15,520
if alif and else block then those
10573
06:58:15,520 --> 06:58:17,440
statements get executed
10574
06:58:17,440 --> 06:58:19,280
now we'll see this with the help of
10575
06:58:19,280 --> 06:58:21,760
examples
10576
06:58:21,760 --> 06:58:22,638
okay
10577
06:58:22,638 --> 06:58:24,798
so here on my
10578
06:58:24,798 --> 06:58:27,600
cell i'll give my
10579
06:58:27,600 --> 06:58:29,120
comment as
10580
06:58:29,120 --> 06:58:32,000
if statement
10581
06:58:32,240 --> 06:58:33,360
okay
10582
06:58:33,360 --> 06:58:34,798
suppose
10583
06:58:34,798 --> 06:58:37,600
i have a variable called score
10584
06:58:37,600 --> 06:58:38,958
and
10585
06:58:38,958 --> 06:58:41,200
this take the value
10586
06:58:41,200 --> 06:58:44,320
as integer from the user so i am using
10587
06:58:44,320 --> 06:58:46,000
the input function
10588
06:58:46,000 --> 06:58:48,558
i'll say
10589
06:58:48,638 --> 06:58:51,638
enter
10590
06:58:52,000 --> 06:58:53,120
the
10591
06:58:53,120 --> 06:58:55,680
score
10592
06:58:55,760 --> 06:58:57,920
i'll give a space
10593
06:58:57,920 --> 06:58:59,280
then
10594
06:58:59,280 --> 06:59:01,040
i'll write my
10595
06:59:01,040 --> 06:59:04,798
if condition i'll say if score
10596
06:59:04,798 --> 06:59:06,478
is
10597
06:59:06,478 --> 06:59:10,160
let's say greater than equal to 90
10598
06:59:10,160 --> 06:59:12,000
give a colon
10599
06:59:12,000 --> 06:59:14,558
then we'll print
10600
06:59:14,558 --> 06:59:16,320
our message as
10601
06:59:16,320 --> 06:59:18,240
passed the
10602
06:59:18,240 --> 06:59:22,878
exam with let's say a grade
10603
06:59:22,878 --> 06:59:24,878
i'll just copy this statement because we
10604
06:59:24,878 --> 06:59:26,000
are going to
10605
06:59:26,000 --> 06:59:27,280
print
10606
06:59:27,280 --> 06:59:29,680
similar statement
10607
06:59:29,680 --> 06:59:31,120
below as well
10608
06:59:31,120 --> 06:59:34,638
and now i will have my
10609
06:59:34,638 --> 06:59:37,520
alif condition i'll write
10610
06:59:37,520 --> 06:59:39,760
lf
10611
06:59:39,760 --> 06:59:42,718
alif is actually else if
10612
06:59:42,718 --> 06:59:45,840
score is greater than
10613
06:59:45,840 --> 06:59:47,760
equal to
10614
06:59:47,760 --> 06:59:49,440
let's say
10615
06:59:49,440 --> 06:59:51,760
75
10616
06:59:51,760 --> 06:59:53,600
i'll give a colon
10617
06:59:53,600 --> 06:59:55,760
will print
10618
06:59:55,760 --> 06:59:57,760
past the
10619
06:59:57,760 --> 06:59:59,760
exam with
10620
06:59:59,760 --> 07:00:00,840
b
10621
07:00:00,840 --> 07:00:04,638
grade then i'll again have my
10622
07:00:04,638 --> 07:00:05,600
alif
10623
07:00:05,600 --> 07:00:08,558
statement i'll write if score
10624
07:00:08,558 --> 07:00:11,680
is greater than equal to let's say
10625
07:00:11,680 --> 07:00:14,240
60
10626
07:00:15,040 --> 07:00:18,558
will print pass the exam with
10627
07:00:18,558 --> 07:00:21,280
c grade
10628
07:00:21,840 --> 07:00:23,280
next
10629
07:00:23,280 --> 07:00:25,760
let's write another alif condition i'll
10630
07:00:25,760 --> 07:00:27,600
write lf
10631
07:00:27,600 --> 07:00:29,040
if score
10632
07:00:29,040 --> 07:00:30,558
is
10633
07:00:30,558 --> 07:00:32,478
greater than
10634
07:00:32,478 --> 07:00:35,120
this should be greater than equal to
10635
07:00:35,120 --> 07:00:36,240
okay
10636
07:00:36,240 --> 07:00:38,160
is greater than equal to
10637
07:00:38,160 --> 07:00:39,920
let's say
10638
07:00:39,920 --> 07:00:42,320
40
10639
07:00:42,320 --> 07:00:45,760
then we'll print past the exam with
10640
07:00:45,760 --> 07:00:47,360
d grade
10641
07:00:47,360 --> 07:00:48,798
and finally
10642
07:00:48,798 --> 07:00:50,558
we'll have the
10643
07:00:50,558 --> 07:00:52,400
else condition
10644
07:00:52,400 --> 07:00:53,440
i'll write
10645
07:00:53,440 --> 07:00:57,120
print let me just scroll down
10646
07:00:57,200 --> 07:00:58,798
failed
10647
07:00:58,798 --> 07:01:01,600
in the exam
10648
07:01:01,600 --> 07:01:03,440
okay
10649
07:01:03,440 --> 07:01:06,160
so this is how you write a
10650
07:01:06,160 --> 07:01:09,360
life conditional statement so first i am
10651
07:01:09,360 --> 07:01:12,000
taking the user input of the score then
10652
07:01:12,000 --> 07:01:14,160
i am checking if score is greater than
10653
07:01:14,160 --> 07:01:16,478
equal to 90 i am displaying my message
10654
07:01:16,478 --> 07:01:18,798
as pass the exam with a grade then i
10655
07:01:18,798 --> 07:01:20,400
have a list of
10656
07:01:20,400 --> 07:01:22,718
alif statements
10657
07:01:22,718 --> 07:01:25,600
to check for different scores
10658
07:01:25,600 --> 07:01:28,798
if all these evaluates to false i am
10659
07:01:28,798 --> 07:01:31,840
having my else statement printed
10660
07:01:31,840 --> 07:01:32,878
let's
10661
07:01:32,878 --> 07:01:33,920
run it
10662
07:01:33,920 --> 07:01:34,958
okay
10663
07:01:34,958 --> 07:01:37,360
so we'll try out different
10664
07:01:37,360 --> 07:01:38,400
scores
10665
07:01:38,400 --> 07:01:40,718
let's say
10666
07:01:40,718 --> 07:01:44,080
someone scored 95
10667
07:01:44,080 --> 07:01:45,680
if i run it
10668
07:01:45,680 --> 07:01:46,718
so that
10669
07:01:46,718 --> 07:01:48,958
candidate has passed the exam with
10670
07:01:48,958 --> 07:01:51,040
e-grade because
10671
07:01:51,040 --> 07:01:53,680
if you see the condition my score 95 is
10672
07:01:53,680 --> 07:01:55,440
greater than equal to 90 and hence we
10673
07:01:55,440 --> 07:01:57,440
have passed the exam with a grade as the
10674
07:01:57,440 --> 07:01:58,638
message
10675
07:01:58,638 --> 07:02:02,240
run it again let's say i have 80.
10676
07:02:02,240 --> 07:02:04,878
if i run it it says pass the exam with b
10677
07:02:04,878 --> 07:02:07,280
grade the reason being 80 is greater
10678
07:02:07,280 --> 07:02:09,360
than equal to 75 and hence we have this
10679
07:02:09,360 --> 07:02:11,520
message printed
10680
07:02:11,520 --> 07:02:13,280
let's run it again suppose someone
10681
07:02:13,280 --> 07:02:16,558
scored let's say 25.
10682
07:02:16,718 --> 07:02:18,160
if i run it
10683
07:02:18,160 --> 07:02:19,120
okay
10684
07:02:19,120 --> 07:02:22,958
it throws me an error it says name
10685
07:02:22,958 --> 07:02:25,280
score not defined
10686
07:02:25,280 --> 07:02:28,240
okay so this should be score actually
10687
07:02:28,240 --> 07:02:30,240
here there was a spelling mistake
10688
07:02:30,240 --> 07:02:31,920
i'll run it
10689
07:02:31,920 --> 07:02:34,400
i'll pass my score as 25
10690
07:02:34,400 --> 07:02:37,280
it gives me failed in the exam the
10691
07:02:37,280 --> 07:02:40,240
reason being my score
10692
07:02:40,240 --> 07:02:43,040
25 is less than equal to 40 and hence we
10693
07:02:43,040 --> 07:02:46,320
have failed in the exam
10694
07:02:46,320 --> 07:02:48,558
cool
10695
07:02:49,120 --> 07:02:51,840
now you can also use a for loop and then
10696
07:02:51,840 --> 07:02:55,440
have a list of a full block or if else
10697
07:02:55,440 --> 07:02:56,558
statements
10698
07:02:56,558 --> 07:02:58,558
let me show you how to do it
10699
07:02:58,558 --> 07:03:01,440
suppose i write for x in i'll use the
10700
07:03:01,440 --> 07:03:04,400
range function
10701
07:03:04,400 --> 07:03:06,878
i'll go till
10702
07:03:06,878 --> 07:03:10,080
10 which means from 0 to 9
10703
07:03:10,080 --> 07:03:12,400
then i'm checking if
10704
07:03:12,400 --> 07:03:13,920
my x
10705
07:03:13,920 --> 07:03:15,520
is
10706
07:03:15,520 --> 07:03:18,638
less than equal to 3
10707
07:03:18,638 --> 07:03:20,958
i'll print
10708
07:03:20,958 --> 07:03:23,200
x
10709
07:03:23,200 --> 07:03:24,878
is
10710
07:03:24,878 --> 07:03:28,240
less than or
10711
07:03:28,320 --> 07:03:30,958
equal to
10712
07:03:30,958 --> 07:03:33,200
3
10713
07:03:33,200 --> 07:03:34,718
okay
10714
07:03:34,718 --> 07:03:36,240
now
10715
07:03:36,240 --> 07:03:37,760
we have the
10716
07:03:37,760 --> 07:03:41,440
ellipse statement if l if
10717
07:03:41,440 --> 07:03:44,638
x greater than
10718
07:03:44,840 --> 07:03:47,440
5 i'll print
10719
07:03:47,440 --> 07:03:49,680
x
10720
07:03:49,680 --> 07:03:52,000
is
10721
07:03:52,798 --> 07:03:56,200
greater than
10722
07:04:00,400 --> 07:04:02,718
five
10723
07:04:06,958 --> 07:04:08,798
then we have the
10724
07:04:08,798 --> 07:04:11,200
else condition
10725
07:04:11,200 --> 07:04:15,760
will print let me just scroll down
10726
07:04:15,760 --> 07:04:19,120
i'll print x
10727
07:04:20,000 --> 07:04:22,558
must be
10728
07:04:23,600 --> 07:04:24,558
four
10729
07:04:24,558 --> 07:04:25,440
or
10730
07:04:25,440 --> 07:04:27,760
five
10731
07:04:27,840 --> 07:04:30,160
okay
10732
07:04:30,718 --> 07:04:31,600
so
10733
07:04:31,600 --> 07:04:34,200
here i have used a for loop and i am
10734
07:04:34,200 --> 07:04:35,360
[Music]
10735
07:04:35,360 --> 07:04:38,160
checking for each iteration from 0 to 9
10736
07:04:38,160 --> 07:04:40,798
and then we have a set of if elephant
10737
07:04:40,798 --> 07:04:43,520
else statements let's run it
10738
07:04:43,520 --> 07:04:46,160
there you go you can see the output here
10739
07:04:46,160 --> 07:04:47,440
so first
10740
07:04:47,440 --> 07:04:49,760
in my range i have 0
10741
07:04:49,760 --> 07:04:52,320
so it will check if 0 is less than equal
10742
07:04:52,320 --> 07:04:54,798
to 3 which is true so it has printed 0
10743
07:04:54,798 --> 07:04:57,200
is less than or equal to 3
10744
07:04:57,200 --> 07:04:59,280
so it will go till 3
10745
07:04:59,280 --> 07:05:01,040
and then
10746
07:05:01,040 --> 07:05:03,680
when 4 comes it will check if
10747
07:05:03,680 --> 07:05:06,320
4 is greater than 5 now this is also
10748
07:05:06,320 --> 07:05:08,840
false so it will skip executing the lf
10749
07:05:08,840 --> 07:05:11,440
statement and it will print whatever is
10750
07:05:11,440 --> 07:05:13,360
there in the else
10751
07:05:13,360 --> 07:05:15,280
block which is
10752
07:05:15,280 --> 07:05:17,600
four must be four or five so you can see
10753
07:05:17,600 --> 07:05:19,200
the message here four must be four and
10754
07:05:19,200 --> 07:05:21,280
five similarly for five also the same
10755
07:05:21,280 --> 07:05:23,840
message will be printed the reason being
10756
07:05:23,840 --> 07:05:26,638
5 is not greater than 5
10757
07:05:26,638 --> 07:05:28,798
and then for the rest of the
10758
07:05:28,798 --> 07:05:31,920
numbers from 6 to 9 we have
10759
07:05:31,920 --> 07:05:34,958
6 is greater than 5 7 is greater than 5
10760
07:05:34,958 --> 07:05:37,280
8 is greater than 5 and 9 is greater
10761
07:05:37,280 --> 07:05:38,878
than 5
10762
07:05:38,878 --> 07:05:41,280
all right
10763
07:05:42,718 --> 07:05:45,600
now moving ahead
10764
07:05:46,240 --> 07:05:48,000
next we have the
10765
07:05:48,000 --> 07:05:50,478
nested if statement so here is the
10766
07:05:50,478 --> 07:05:52,558
syntax of nested if so you have the if
10767
07:05:52,558 --> 07:05:54,320
condition and inside the if condition
10768
07:05:54,320 --> 07:05:56,240
you have another if condition and hence
10769
07:05:56,240 --> 07:05:57,520
it becomes the
10770
07:05:57,520 --> 07:05:59,920
nested if condition statement
10771
07:05:59,920 --> 07:06:02,240
and inside this condition you have the
10772
07:06:02,240 --> 07:06:04,478
list of statements or the blocks of
10773
07:06:04,478 --> 07:06:05,760
statements
10774
07:06:05,760 --> 07:06:08,000
so here is how the flowchart or the flow
10775
07:06:08,000 --> 07:06:10,320
diagram looks like
10776
07:06:10,320 --> 07:06:11,360
so first
10777
07:06:11,360 --> 07:06:13,440
we are evaluating the expression1 which
10778
07:06:13,440 --> 07:06:15,040
is this condition
10779
07:06:15,040 --> 07:06:17,360
if it is true we are going inside and
10780
07:06:17,360 --> 07:06:19,360
checking the second condition
10781
07:06:19,360 --> 07:06:21,040
if that is also true then we are running
10782
07:06:21,040 --> 07:06:23,600
the body of it which is this part of the
10783
07:06:23,600 --> 07:06:25,040
syntax
10784
07:06:25,040 --> 07:06:27,280
and later if there are any statements
10785
07:06:27,280 --> 07:06:28,638
that are present outside the if
10786
07:06:28,638 --> 07:06:30,798
condition we are executing those
10787
07:06:30,798 --> 07:06:32,638
so in case the first test expression
10788
07:06:32,638 --> 07:06:35,360
evaluates to false we are coming outside
10789
07:06:35,360 --> 07:06:37,440
and directly printing
10790
07:06:37,440 --> 07:06:40,558
whatever is there outside
10791
07:06:40,558 --> 07:06:42,798
the if condition
10792
07:06:42,798 --> 07:06:43,680
let's
10793
07:06:43,680 --> 07:06:47,040
see this with the help of an example
10794
07:06:47,040 --> 07:06:49,840
okay so i'll give my
10795
07:06:49,840 --> 07:06:52,878
comment here as nested if
10796
07:06:52,878 --> 07:06:54,558
cool
10797
07:06:54,558 --> 07:06:57,120
so first of all i'll declare a variable
10798
07:06:57,120 --> 07:06:58,958
called num
10799
07:06:58,958 --> 07:07:01,200
now this
10800
07:07:01,200 --> 07:07:03,120
num will have a
10801
07:07:03,120 --> 07:07:04,878
user input
10802
07:07:04,878 --> 07:07:06,080
value
10803
07:07:06,080 --> 07:07:08,718
so i'll write input
10804
07:07:08,718 --> 07:07:10,320
enter the
10805
07:07:10,320 --> 07:07:12,638
number
10806
07:07:12,638 --> 07:07:13,920
then
10807
07:07:13,920 --> 07:07:15,600
i'll have my first
10808
07:07:15,600 --> 07:07:17,440
if condition
10809
07:07:17,440 --> 07:07:18,718
i'll say if
10810
07:07:18,718 --> 07:07:23,360
number or num is less than 250
10811
07:07:25,120 --> 07:07:26,878
i'll give a
10812
07:07:26,878 --> 07:07:28,718
colon
10813
07:07:28,718 --> 07:07:32,798
then we'll use the print statement
10814
07:07:32,798 --> 07:07:34,638
num
10815
07:07:34,638 --> 07:07:36,478
is
10816
07:07:36,478 --> 07:07:38,798
smaller than
10817
07:07:38,798 --> 07:07:41,440
250.
10818
07:07:42,000 --> 07:07:45,040
i'll write my second if statement
10819
07:07:45,040 --> 07:07:47,600
and we'll check if num is
10820
07:07:47,600 --> 07:07:49,760
less than
10821
07:07:49,760 --> 07:07:52,000
180
10822
07:07:52,000 --> 07:07:54,000
and will print
10823
07:07:54,000 --> 07:07:56,240
num
10824
07:07:56,840 --> 07:07:59,600
is smaller
10825
07:07:59,600 --> 07:08:01,920
than
10826
07:08:02,638 --> 07:08:05,760
180 as well
10827
07:08:06,080 --> 07:08:08,558
finally i'll have my
10828
07:08:08,558 --> 07:08:10,840
else statement as
10829
07:08:10,840 --> 07:08:14,160
print number
10830
07:08:14,160 --> 07:08:16,400
is
10831
07:08:18,478 --> 07:08:21,440
greater than
10832
07:08:21,440 --> 07:08:24,080
180
10833
07:08:24,878 --> 07:08:27,600
all right let's run this
10834
07:08:27,600 --> 07:08:29,440
and first we'll check with the number
10835
07:08:29,440 --> 07:08:30,558
100
10836
07:08:30,558 --> 07:08:32,160
okay there you go
10837
07:08:32,160 --> 07:08:34,400
so here i'm checking whether 100 is less
10838
07:08:34,400 --> 07:08:36,320
than 250 which is true so we have
10839
07:08:36,320 --> 07:08:39,120
printed 100 is smaller than 250 again we
10840
07:08:39,120 --> 07:08:41,520
are checking if 100 is less than 180
10841
07:08:41,520 --> 07:08:42,798
which is true again
10842
07:08:42,798 --> 07:08:45,760
so we are printing 100 is smaller than
10843
07:08:45,760 --> 07:08:49,200
180 as well there's a spelling mistake
10844
07:08:49,200 --> 07:08:50,718
okay
10845
07:08:50,718 --> 07:08:53,600
now let's see for let's see 150
10846
07:08:53,600 --> 07:08:56,798
again we have 150 smaller than 250 150
10847
07:08:56,798 --> 07:08:58,798
also smaller than 180
10848
07:08:58,798 --> 07:09:00,718
let's run it this time let's say we'll
10849
07:09:00,718 --> 07:09:01,520
pass
10850
07:09:01,520 --> 07:09:04,080
300
10851
07:09:04,080 --> 07:09:05,840
okay you can see our
10852
07:09:05,840 --> 07:09:07,760
result here
10853
07:09:07,760 --> 07:09:09,520
300
10854
07:09:09,520 --> 07:09:10,478
is
10855
07:09:10,478 --> 07:09:13,520
less than 250 is false so we'll skip
10856
07:09:13,520 --> 07:09:15,440
printing this again we'll check whether
10857
07:09:15,440 --> 07:09:17,920
300 is less than 180
10858
07:09:17,920 --> 07:09:20,558
which is false again so we'll skip
10859
07:09:20,558 --> 07:09:22,718
printing this statement as well
10860
07:09:22,718 --> 07:09:24,878
we'll only have our else block printed
10861
07:09:24,878 --> 07:09:28,400
which is 300 is greater than 180
10862
07:09:28,400 --> 07:09:29,920
okay
10863
07:09:29,920 --> 07:09:32,000
now coming to the final section where
10864
07:09:32,000 --> 07:09:35,120
we'll see about nested files
10865
07:09:35,120 --> 07:09:37,280
so here you can see we have the syntax
10866
07:09:37,280 --> 07:09:40,000
for nested if else statement
10867
07:09:40,000 --> 07:09:42,958
so we have an outer if condition and
10868
07:09:42,958 --> 07:09:45,920
inside that we have another if condition
10869
07:09:45,920 --> 07:09:47,600
then we have
10870
07:09:47,600 --> 07:09:49,440
the list of
10871
07:09:49,440 --> 07:09:50,798
statements or the body of the if
10872
07:09:50,798 --> 07:09:52,160
condition
10873
07:09:52,160 --> 07:09:54,878
if this is false then we execute the
10874
07:09:54,878 --> 07:09:56,478
else part
10875
07:09:56,478 --> 07:09:58,478
and after that we also have an outer
10876
07:09:58,478 --> 07:09:59,520
else
10877
07:09:59,520 --> 07:10:00,718
statement
10878
07:10:00,718 --> 07:10:02,400
so this is how
10879
07:10:02,400 --> 07:10:04,478
the flow diagram looks like we'll check
10880
07:10:04,478 --> 07:10:06,798
for the test expression one
10881
07:10:06,798 --> 07:10:09,200
if that is true then we'll go inside and
10882
07:10:09,200 --> 07:10:11,360
see the test expression
10883
07:10:11,360 --> 07:10:12,320
too
10884
07:10:12,320 --> 07:10:14,558
and if that is true we'll execute the
10885
07:10:14,558 --> 07:10:16,400
body of if statement which is this part
10886
07:10:16,400 --> 07:10:18,160
of the syntax
10887
07:10:18,160 --> 07:10:19,360
then
10888
07:10:19,360 --> 07:10:21,520
if these are false then we'll see the
10889
07:10:21,520 --> 07:10:23,680
body of else one
10890
07:10:23,680 --> 07:10:25,600
if the second expression is false then
10891
07:10:25,600 --> 07:10:28,160
we go to the body of else two
10892
07:10:28,160 --> 07:10:29,840
and after that if there are any more
10893
07:10:29,840 --> 07:10:33,280
statements we'll execute them as well
10894
07:10:33,280 --> 07:10:34,478
all right
10895
07:10:34,478 --> 07:10:36,798
we'll see an example here
10896
07:10:36,798 --> 07:10:40,958
so i'll give my comment as
10897
07:10:40,958 --> 07:10:42,878
nested
10898
07:10:42,878 --> 07:10:44,958
if else
10899
07:10:44,958 --> 07:10:48,240
okay so i'll declare a variable a which
10900
07:10:48,240 --> 07:10:49,360
will
10901
07:10:49,360 --> 07:10:51,040
take a user input
10902
07:10:51,040 --> 07:10:53,040
of type integer
10903
07:10:53,040 --> 07:10:54,798
i'll say
10904
07:10:54,798 --> 07:10:57,280
enter
10905
07:10:57,760 --> 07:10:59,360
the
10906
07:10:59,360 --> 07:11:01,840
number
10907
07:11:02,240 --> 07:11:07,840
i'll write if e is less than 50
10908
07:11:07,920 --> 07:11:11,200
so this is my first if condition
10909
07:11:11,200 --> 07:11:12,320
if this is
10910
07:11:12,320 --> 07:11:15,280
true this should be less than 50
10911
07:11:15,280 --> 07:11:18,000
okay so if my first if statement is true
10912
07:11:18,000 --> 07:11:19,200
then we'll
10913
07:11:19,200 --> 07:11:21,520
check for the second if statement which
10914
07:11:21,520 --> 07:11:23,680
is let's say if
10915
07:11:23,680 --> 07:11:25,440
a
10916
07:11:25,440 --> 07:11:28,558
modulus 3 equal to equal to 0 which
10917
07:11:28,558 --> 07:11:30,478
means if the remainder of the division
10918
07:11:30,478 --> 07:11:32,000
is 0
10919
07:11:32,000 --> 07:11:34,320
then we say
10920
07:11:34,320 --> 07:11:35,680
print
10921
07:11:35,680 --> 07:11:37,920
a
10922
07:11:38,160 --> 07:11:40,400
is
10923
07:11:40,718 --> 07:11:43,718
divisible
10924
07:11:44,638 --> 07:11:46,558
by
10925
07:11:46,558 --> 07:11:47,680
3
10926
07:11:47,680 --> 07:11:48,958
and
10927
07:11:48,958 --> 07:11:50,080
is
10928
07:11:50,080 --> 07:11:51,360
less than
10929
07:11:51,360 --> 07:11:53,440
50.
10930
07:11:53,440 --> 07:11:56,440
okay
10931
07:11:57,840 --> 07:12:00,240
then we have our
10932
07:12:00,240 --> 07:12:04,240
else statement as else
10933
07:12:04,718 --> 07:12:07,120
print
10934
07:12:07,200 --> 07:12:09,680
my number e
10935
07:12:09,680 --> 07:12:11,040
is
10936
07:12:11,040 --> 07:12:13,360
not
10937
07:12:13,360 --> 07:12:14,958
divisible
10938
07:12:14,958 --> 07:12:17,600
by 3
10939
07:12:17,760 --> 07:12:19,840
but
10940
07:12:19,840 --> 07:12:21,040
is
10941
07:12:21,040 --> 07:12:24,638
less than 50.
10942
07:12:24,638 --> 07:12:26,400
finally
10943
07:12:26,400 --> 07:12:28,400
i'll have my
10944
07:12:28,400 --> 07:12:30,000
last
10945
07:12:30,000 --> 07:12:32,478
else statement
10946
07:12:32,478 --> 07:12:36,558
where i'll print my number e
10947
07:12:36,840 --> 07:12:40,240
is greater
10948
07:12:40,240 --> 07:12:42,558
than 50
10949
07:12:42,558 --> 07:12:45,760
okay so this is how you write a nested
10950
07:12:45,760 --> 07:12:48,400
if full statement so i am taking my
10951
07:12:48,400 --> 07:12:50,240
number as a user input
10952
07:12:50,240 --> 07:12:52,000
first i am checking that number is less
10953
07:12:52,000 --> 07:12:53,520
than 50
10954
07:12:53,520 --> 07:12:55,440
if that is true i'm again checking if
10955
07:12:55,440 --> 07:12:57,840
that number is divisible by 3 if it is
10956
07:12:57,840 --> 07:12:59,760
divisible i am printing my message as
10957
07:12:59,760 --> 07:13:02,000
the number e is divisible by 3 and is
10958
07:13:02,000 --> 07:13:04,798
less than 50.
10959
07:13:04,878 --> 07:13:07,440
if this is false i am saying
10960
07:13:07,440 --> 07:13:10,958
the number e is not divisible by 3 but
10961
07:13:10,958 --> 07:13:12,478
it is less than
10962
07:13:12,478 --> 07:13:13,520
50
10963
07:13:13,520 --> 07:13:15,040
else i am saying the number is greater
10964
07:13:15,040 --> 07:13:17,680
than 50. let's run this
10965
07:13:17,680 --> 07:13:20,558
and we'll try out different
10966
07:13:20,558 --> 07:13:23,440
examples let's say i'll say my number is
10967
07:13:23,440 --> 07:13:25,040
12.
10968
07:13:25,040 --> 07:13:27,440
my message is 12 is divisible by 3 and
10969
07:13:27,440 --> 07:13:29,840
is less than 50 which is
10970
07:13:29,840 --> 07:13:32,798
this part of the code so my number 12 is
10971
07:13:32,798 --> 07:13:34,400
less than 50
10972
07:13:34,400 --> 07:13:36,798
this is true then i'm again checking
10973
07:13:36,798 --> 07:13:38,958
whether 12 is divisible by 3
10974
07:13:38,958 --> 07:13:41,200
it evaluates to true and hence we have
10975
07:13:41,200 --> 07:13:43,360
this result printed
10976
07:13:43,360 --> 07:13:47,040
okay let's say this time i'll say 30
10977
07:13:47,040 --> 07:13:48,958
so i have my message 30 is divisible by
10978
07:13:48,958 --> 07:13:51,440
3 that is less than 50.
10979
07:13:51,440 --> 07:13:53,120
let's run it again and this time let's
10980
07:13:53,120 --> 07:13:55,360
say we'll say 72
10981
07:13:55,360 --> 07:13:58,400
so here i have my message 72 is greater
10982
07:13:58,400 --> 07:14:01,520
than 50. the reason being 72 less than
10983
07:14:01,520 --> 07:14:03,200
50 is false
10984
07:14:03,200 --> 07:14:05,200
hence this part of the code is not
10985
07:14:05,200 --> 07:14:07,360
executed it will directly come to the
10986
07:14:07,360 --> 07:14:08,878
else part
10987
07:14:08,878 --> 07:14:11,680
which says 72 is greater than 50.
10988
07:14:11,680 --> 07:14:13,200
okay
10989
07:14:13,200 --> 07:14:14,958
so that brings us to the end of this
10990
07:14:14,958 --> 07:14:16,718
demonstration on conditional statements
10991
07:14:16,718 --> 07:14:18,958
in python so first we learned how to
10992
07:14:18,958 --> 07:14:21,680
write simple if statement you saw how to
10993
07:14:21,680 --> 07:14:24,718
take inputs from the user
10994
07:14:24,718 --> 07:14:27,200
we also learnt how to write
10995
07:14:27,200 --> 07:14:29,280
if else statement
10996
07:14:29,280 --> 07:14:31,040
then you learnt about
10997
07:14:31,040 --> 07:14:32,558
elif statement
10998
07:14:32,558 --> 07:14:35,200
you also saw how to use a for loop and
10999
07:14:35,200 --> 07:14:36,718
inside that you can use your if
11000
07:14:36,718 --> 07:14:39,760
statement your lf your else
11001
07:14:39,760 --> 07:14:42,320
later on we saw how to write nested if
11002
07:14:42,320 --> 07:14:44,400
statements and finally we learnt how to
11003
07:14:44,400 --> 07:14:46,638
write nested efl statements
11004
07:14:46,638 --> 07:14:49,280
cool now let's see the agenda for today
11005
07:14:49,280 --> 07:14:51,040
first we will learn about the lists in
11006
07:14:51,040 --> 07:14:53,680
python its characteristics and explore
11007
07:14:53,680 --> 07:14:55,920
it in jupyter notebook then we will
11008
07:14:55,920 --> 07:14:57,680
learn about the pulse in python its
11009
07:14:57,680 --> 07:14:59,600
characteristics and we will also explore
11010
07:14:59,600 --> 07:15:02,478
tuples in jupyter notebook after that we
11011
07:15:02,478 --> 07:15:03,840
will have a look at dictionaries in
11012
07:15:03,840 --> 07:15:06,240
python its characteristics and will head
11013
07:15:06,240 --> 07:15:08,160
to jupyter notebook to understand the
11014
07:15:08,160 --> 07:15:09,840
dictionaries
11015
07:15:09,840 --> 07:15:12,240
so lets start with lists in python
11016
07:15:12,240 --> 07:15:13,360
imagine
11017
07:15:13,360 --> 07:15:15,040
you have different values
11018
07:15:15,040 --> 07:15:17,600
you have numbers you have strings now if
11019
07:15:17,600 --> 07:15:19,440
you want to group them together
11020
07:15:19,440 --> 07:15:21,520
you can do this job in python with the
11021
07:15:21,520 --> 07:15:24,000
help of lists or you have work with
11022
07:15:24,000 --> 07:15:26,638
arrays in any other language like c plus
11023
07:15:26,638 --> 07:15:29,040
or java this is the similar stuff in
11024
07:15:29,040 --> 07:15:31,200
here which is list
11025
07:15:31,200 --> 07:15:33,280
a list can be defined as a collection of
11026
07:15:33,280 --> 07:15:35,600
objects values or items of different
11027
07:15:35,600 --> 07:15:37,840
types and these collections are enclosed
11028
07:15:37,840 --> 07:15:39,840
within the square brackets and are
11029
07:15:39,840 --> 07:15:41,520
separated by commas
11030
07:15:41,520 --> 07:15:42,958
let's understand this with the help of
11031
07:15:42,958 --> 07:15:44,558
an example
11032
07:15:44,558 --> 07:15:46,558
we will create a list and name it as
11033
07:15:46,558 --> 07:15:48,718
list1 and insert the elements in square
11034
07:15:48,718 --> 07:15:50,718
brackets and separate them with the help
11035
07:15:50,718 --> 07:15:52,160
of commas
11036
07:15:52,160 --> 07:15:54,958
list 1 equal to square brackets one
11037
07:15:54,958 --> 07:15:56,798
comma adam
11038
07:15:56,798 --> 07:16:00,718
coma 107 comma usa
11039
07:16:00,718 --> 07:16:03,600
and unless we have our first element at
11040
07:16:03,600 --> 07:16:06,798
zeroth indexed and we have one at zero
11041
07:16:06,798 --> 07:16:11,280
index atom at one index 107 at second
11042
07:16:11,280 --> 07:16:14,320
index usa at third index
11043
07:16:14,320 --> 07:16:16,320
and talking about the reverse index it
11044
07:16:16,320 --> 07:16:19,440
starts with -1 index value we have usa
11045
07:16:19,440 --> 07:16:22,958
at -1 index value 107 at minus 2 index
11046
07:16:22,958 --> 07:16:26,400
value adam at minus 3 index value and 1
11047
07:16:26,400 --> 07:16:28,718
at -4 index value
11048
07:16:28,718 --> 07:16:30,400
we will explore the list with various
11049
07:16:30,400 --> 07:16:32,478
examples in jupyter notebook in a while
11050
07:16:32,478 --> 07:16:34,080
till then let's look at the
11051
07:16:34,080 --> 07:16:36,638
characteristics of lists
11052
07:16:36,638 --> 07:16:39,200
first the lists are ordered when we say
11053
07:16:39,200 --> 07:16:41,680
that lists are ordered it means that the
11054
07:16:41,680 --> 07:16:43,840
items have a defined order and that
11055
07:16:43,840 --> 07:16:46,080
order will not change if you add new
11056
07:16:46,080 --> 07:16:48,240
items to a list the new items will be
11057
07:16:48,240 --> 07:16:50,478
placed at the end of the list
11058
07:16:50,478 --> 07:16:52,638
second elements of the list can be
11059
07:16:52,638 --> 07:16:54,878
accessed by index values
11060
07:16:54,878 --> 07:16:57,360
third list can store various types of
11061
07:16:57,360 --> 07:17:00,320
elements for example numbers strings or
11062
07:17:00,320 --> 07:17:02,478
lists itself
11063
07:17:02,478 --> 07:17:04,718
fourth lists are mutable
11064
07:17:04,718 --> 07:17:06,878
the list is changeable meaning that we
11065
07:17:06,878 --> 07:17:09,360
can change add and remove items in a
11066
07:17:09,360 --> 07:17:12,160
list after it has been created
11067
07:17:12,160 --> 07:17:14,320
lists allow duplicate elements since
11068
07:17:14,320 --> 07:17:16,958
lists are indexed list can have items
11069
07:17:16,958 --> 07:17:19,200
with the same value let's have a look at
11070
07:17:19,200 --> 07:17:22,958
some methods of lists in python
11071
07:17:24,798 --> 07:17:26,798
the first one is append it adds an
11072
07:17:26,798 --> 07:17:29,360
element at the end of the list
11073
07:17:29,360 --> 07:17:32,558
for syntax we can write list1 dot append
11074
07:17:32,558 --> 07:17:34,558
and in round brackets we have to write
11075
07:17:34,558 --> 07:17:36,878
the element which we want to append
11076
07:17:36,878 --> 07:17:39,600
the next is insert it adds an element at
11077
07:17:39,600 --> 07:17:42,240
the specified position its syntax is
11078
07:17:42,240 --> 07:17:44,718
name of the list dot insert and in round
11079
07:17:44,718 --> 07:17:47,520
brackets we'll write the index at which
11080
07:17:47,520 --> 07:17:49,760
we want to insert the element and comma
11081
07:17:49,760 --> 07:17:51,440
and after data element which we want to
11082
07:17:51,440 --> 07:17:52,558
insert
11083
07:17:52,558 --> 07:17:54,398
third is extend
11084
07:17:54,398 --> 07:17:56,878
it adds the element of a list to the end
11085
07:17:56,878 --> 07:17:58,398
of the current list
11086
07:17:58,398 --> 07:18:01,200
its syntax is list1 dot extend and in
11087
07:18:01,200 --> 07:18:04,000
round brackets we will add the element
11088
07:18:04,000 --> 07:18:05,280
like list2
11089
07:18:05,280 --> 07:18:07,440
which we want to extend in the list
11090
07:18:07,440 --> 07:18:09,840
the next is index it returns the index
11091
07:18:09,840 --> 07:18:12,840
of the first element with the specified
11092
07:18:12,840 --> 07:18:16,478
value and its syntax is list1 dot index
11093
07:18:16,478 --> 07:18:18,878
and the element which you want to fetch
11094
07:18:18,878 --> 07:18:21,520
the index
11095
07:18:21,760 --> 07:18:23,840
the next is remove it removes the item
11096
07:18:23,840 --> 07:18:25,840
with the specified value
11097
07:18:25,840 --> 07:18:27,440
its syntax is
11098
07:18:27,440 --> 07:18:29,680
dot remove and in round brackets we
11099
07:18:29,680 --> 07:18:31,040
write the element which we want to
11100
07:18:31,040 --> 07:18:32,798
remove from the list
11101
07:18:32,798 --> 07:18:34,638
the next is sort
11102
07:18:34,638 --> 07:18:37,360
this method is used to sort the list
11103
07:18:37,360 --> 07:18:40,798
its syntax is list1 dot sort
11104
07:18:40,798 --> 07:18:42,478
the next is reverse
11105
07:18:42,478 --> 07:18:45,280
it reverses the order of the list
11106
07:18:45,280 --> 07:18:48,638
its syntax is list1 dot reverse
11107
07:18:48,638 --> 07:18:50,398
let's move to jupyter notebook to
11108
07:18:50,398 --> 07:18:52,798
explore these methods
11109
07:18:52,798 --> 07:18:54,958
let's start with creating a list
11110
07:18:54,958 --> 07:18:57,680
we'll name the list as list1
11111
07:18:57,680 --> 07:18:59,680
and we'll create
11112
07:18:59,680 --> 07:19:01,040
equal to
11113
07:19:01,040 --> 07:19:05,520
and we'll start with square brackets
11114
07:19:05,680 --> 07:19:07,360
and we'll write our first element that
11115
07:19:07,360 --> 07:19:10,000
would be apple
11116
07:19:10,000 --> 07:19:11,040
space
11117
07:19:11,040 --> 07:19:13,760
next element
11118
07:19:14,080 --> 07:19:15,280
orange
11119
07:19:15,280 --> 07:19:19,440
coma space the next element
11120
07:19:19,440 --> 07:19:21,760
banana
11121
07:19:21,760 --> 07:19:24,718
coma space the next element
11122
07:19:24,718 --> 07:19:27,120
key v
11123
07:19:28,320 --> 07:19:30,558
now we will get this list printed for
11124
07:19:30,558 --> 07:19:32,638
that we'll write the syntax
11125
07:19:32,638 --> 07:19:34,638
print and in round brackets we'll write
11126
07:19:34,638 --> 07:19:36,878
the list name that would be list1 and
11127
07:19:36,878 --> 07:19:38,878
run the command you can see here that
11128
07:19:38,878 --> 07:19:41,920
the list is printed
11129
07:19:41,920 --> 07:19:44,478
now let's create a different list with
11130
07:19:44,478 --> 07:19:46,558
some different elements
11131
07:19:46,558 --> 07:19:47,680
list 2
11132
07:19:47,680 --> 07:19:49,440
equal to
11133
07:19:49,440 --> 07:19:52,958
and in square brackets will write
11134
07:19:52,958 --> 07:19:55,440
apple
11135
07:19:55,600 --> 07:19:57,840
now we will insert a list
11136
07:19:57,840 --> 07:19:59,600
in a list
11137
07:19:59,600 --> 07:20:00,798
for that
11138
07:20:00,798 --> 07:20:03,200
we will write a list in square brackets
11139
07:20:03,200 --> 07:20:05,360
8 comma 4 comma
11140
07:20:05,360 --> 07:20:07,680
6
11141
07:20:07,680 --> 07:20:10,240
and coma the next element that will
11142
07:20:10,240 --> 07:20:12,958
write orange
11143
07:20:14,878 --> 07:20:18,600
the next element banana
11144
07:20:25,040 --> 07:20:27,360
now print this list
11145
07:20:27,360 --> 07:20:29,200
to print this will write print round
11146
07:20:29,200 --> 07:20:31,440
brackets will write list2
11147
07:20:31,440 --> 07:20:33,520
and run the command here we will get the
11148
07:20:33,520 --> 07:20:34,398
list
11149
07:20:34,398 --> 07:20:36,240
to print it
11150
07:20:36,240 --> 07:20:39,360
we can access any element in the list so
11151
07:20:39,360 --> 07:20:41,520
for that we will write the syntax
11152
07:20:41,520 --> 07:20:43,040
print
11153
07:20:43,040 --> 07:20:45,280
we want to access the second element
11154
07:20:45,280 --> 07:20:47,040
that will be the third element from the
11155
07:20:47,040 --> 07:20:49,600
list one for that we'll write the syntax
11156
07:20:49,600 --> 07:20:50,958
print
11157
07:20:50,958 --> 07:20:53,040
list one and in square brackets we'll
11158
07:20:53,040 --> 07:20:56,000
write the index two and run the command
11159
07:20:56,000 --> 07:20:58,000
we will get the third element from the
11160
07:20:58,000 --> 07:21:01,040
list one that is banana
11161
07:21:01,040 --> 07:21:04,718
now we will access an element from the
11162
07:21:04,718 --> 07:21:05,760
list2
11163
07:21:05,760 --> 07:21:09,440
for that we'll write the command print
11164
07:21:09,440 --> 07:21:11,200
list2
11165
07:21:11,200 --> 07:21:13,840
and in square bracket we will write 1
11166
07:21:13,840 --> 07:21:16,478
and in square bracket we'll write 2 and
11167
07:21:16,478 --> 07:21:19,840
run the command what we get is 6
11168
07:21:19,840 --> 07:21:22,958
so to access an element from a list that
11169
07:21:22,958 --> 07:21:25,280
is already inserted in and list we have
11170
07:21:25,280 --> 07:21:27,120
to write this syntax
11171
07:21:27,120 --> 07:21:29,120
so this syntax tells us that we will
11172
07:21:29,120 --> 07:21:31,760
first access the first index element
11173
07:21:31,760 --> 07:21:35,280
that is 846 the list and in that we will
11174
07:21:35,280 --> 07:21:37,440
access the second index value that would
11175
07:21:37,440 --> 07:21:40,080
be 0 1 and 2 that is 6.
11176
07:21:40,080 --> 07:21:42,240
so this is the syntax to access an
11177
07:21:42,240 --> 07:21:44,240
element in a list that is already
11178
07:21:44,240 --> 07:21:46,558
inserted in a list
11179
07:21:46,558 --> 07:21:49,840
now we'll see a reverse index order for
11180
07:21:49,840 --> 07:21:53,120
that we'll write the command print
11181
07:21:53,120 --> 07:21:55,520
list 1 and in square brackets we'll
11182
07:21:55,520 --> 07:21:57,840
write the index minus
11183
07:21:57,840 --> 07:22:00,080
and we'll run this command and we get
11184
07:22:00,080 --> 07:22:02,718
kv as output
11185
07:22:02,718 --> 07:22:05,120
now we'll see slicing in python
11186
07:22:05,120 --> 07:22:07,440
we can access a range of items in a list
11187
07:22:07,440 --> 07:22:08,558
by using
11188
07:22:08,558 --> 07:22:10,878
the slicing operator
11189
07:22:10,878 --> 07:22:12,798
now we will see an example
11190
07:22:12,798 --> 07:22:13,760
to see
11191
07:22:13,760 --> 07:22:16,558
the slicing operator work in python
11192
07:22:16,558 --> 07:22:21,440
for that we will write the syntax print
11193
07:22:22,000 --> 07:22:24,160
list1
11194
07:22:24,160 --> 07:22:27,200
and in square brackets we'll write 1
11195
07:22:27,200 --> 07:22:32,160
slicing operator 3 and run the command
11196
07:22:32,240 --> 07:22:35,120
what we say is orange and banana as an
11197
07:22:35,120 --> 07:22:38,160
output from the list 1.
11198
07:22:38,160 --> 07:22:39,360
when you write
11199
07:22:39,360 --> 07:22:41,920
one ratio 3 that is one slicing operator
11200
07:22:41,920 --> 07:22:45,840
3 it gives you the elements at index 1
11201
07:22:45,840 --> 07:22:47,760
2 and note 3
11202
07:22:47,760 --> 07:22:50,798
so we get the output as orange coma
11203
07:22:50,798 --> 07:22:52,718
banana
11204
07:22:52,718 --> 07:22:55,520
now we will
11205
07:22:55,520 --> 07:22:57,600
see another example for the slicing
11206
07:22:57,600 --> 07:23:01,360
operator we'll write print
11207
07:23:01,520 --> 07:23:04,000
list 1
11208
07:23:04,000 --> 07:23:07,360
and in square brackets we'll write 3
11209
07:23:07,360 --> 07:23:09,680
and slicing operator
11210
07:23:09,680 --> 07:23:13,600
run the command here we can get only tv
11211
07:23:13,600 --> 07:23:15,440
when we write a single number before
11212
07:23:15,440 --> 07:23:16,878
slicing operator
11213
07:23:16,878 --> 07:23:19,440
it would print the value at that index
11214
07:23:19,440 --> 07:23:21,440
value and all the other elements that
11215
07:23:21,440 --> 07:23:23,600
are present in the list but in this list
11216
07:23:23,600 --> 07:23:26,320
we have only kv as the last element so
11217
07:23:26,320 --> 07:23:29,440
it would print third index value and not
11218
07:23:29,440 --> 07:23:31,600
more than that
11219
07:23:31,600 --> 07:23:33,680
now we'll see append function for that
11220
07:23:33,680 --> 07:23:37,200
we'll write the syntax list1 dot
11221
07:23:37,200 --> 07:23:39,600
append and in round brackets we will
11222
07:23:39,600 --> 07:23:41,040
write the element which we want to
11223
07:23:41,040 --> 07:23:44,240
insert at the last of the list
11224
07:23:44,240 --> 07:23:47,520
we will write qova and
11225
07:23:47,520 --> 07:23:50,320
run it and now we will print the list to
11226
07:23:50,320 --> 07:23:53,120
see the result
11227
07:23:54,160 --> 07:23:56,958
here we can see that java is added at
11228
07:23:56,958 --> 07:23:59,120
the last of the list
11229
07:23:59,120 --> 07:24:01,280
now we'll see extend function
11230
07:24:01,280 --> 07:24:03,520
for that we'll write the index list one
11231
07:24:03,520 --> 07:24:04,958
dot
11232
07:24:04,958 --> 07:24:08,798
extend and in round brackets
11233
07:24:09,120 --> 07:24:11,600
we will write
11234
07:24:11,600 --> 07:24:13,680
in square brackets the particular
11235
07:24:13,680 --> 07:24:16,398
elements we want to insert in the list
11236
07:24:16,398 --> 07:24:20,080
so we will write watermelon
11237
07:24:20,080 --> 07:24:21,600
and the next element we want to insert
11238
07:24:21,600 --> 07:24:24,638
would be muskmelon
11239
07:24:25,840 --> 07:24:30,280
that would be in inverted commas
11240
07:24:32,638 --> 07:24:36,638
and we also want this list to be printed
11241
07:24:36,638 --> 07:24:38,958
so we'll write print list one and
11242
07:24:38,958 --> 07:24:40,718
execute this command
11243
07:24:40,718 --> 07:24:42,080
here
11244
07:24:42,080 --> 07:24:44,638
we can get watermelon and muskmelon add
11245
07:24:44,638 --> 07:24:46,878
it just after the list
11246
07:24:46,878 --> 07:24:50,718
just at the last elements of the list
11247
07:24:50,718 --> 07:24:52,558
now if you want to delete a particular
11248
07:24:52,558 --> 07:24:54,798
element from the list you can delete it
11249
07:24:54,798 --> 07:24:56,958
with the syntax
11250
07:24:56,958 --> 07:24:58,240
dl
11251
07:24:58,240 --> 07:24:59,440
space
11252
07:24:59,440 --> 07:25:01,360
the list name you want to delete the
11253
07:25:01,360 --> 07:25:02,558
element
11254
07:25:02,558 --> 07:25:04,718
and in square brackets you will write
11255
07:25:04,718 --> 07:25:06,718
the index for the element you want to
11256
07:25:06,718 --> 07:25:07,840
delete
11257
07:25:07,840 --> 07:25:10,558
we have written the fifth index value
11258
07:25:10,558 --> 07:25:12,398
so we'll run this command
11259
07:25:12,398 --> 07:25:16,240
and we'll get the list printed
11260
07:25:19,040 --> 07:25:20,558
we can see
11261
07:25:20,558 --> 07:25:22,160
that the fifth index value that is
11262
07:25:22,160 --> 07:25:24,718
watermelon is not there in list one it's
11263
07:25:24,718 --> 07:25:26,798
been deleted
11264
07:25:26,798 --> 07:25:27,760
now
11265
07:25:27,760 --> 07:25:30,718
you can also do this with another method
11266
07:25:30,718 --> 07:25:32,718
that is removed for that we will write
11267
07:25:32,718 --> 07:25:34,878
list1 dot
11268
07:25:34,878 --> 07:25:37,360
remove and in round brackets
11269
07:25:37,360 --> 07:25:39,440
we'll write the particular element we
11270
07:25:39,440 --> 07:25:42,478
want to remove we'll write that that is
11271
07:25:42,478 --> 07:25:43,840
kv
11272
07:25:43,840 --> 07:25:46,398
and we also want the list to be printed
11273
07:25:46,398 --> 07:25:47,600
so we'll write
11274
07:25:47,600 --> 07:25:50,558
print in round brackets list one
11275
07:25:50,558 --> 07:25:52,320
and run this command
11276
07:25:52,320 --> 07:25:55,120
we get that the key is been removed from
11277
07:25:55,120 --> 07:25:57,680
the list one
11278
07:25:57,680 --> 07:25:58,398
now
11279
07:25:58,398 --> 07:26:00,718
there is another method that is pop
11280
07:26:00,718 --> 07:26:04,320
we will write that command print
11281
07:26:04,638 --> 07:26:07,440
in round brackets we will write list one
11282
07:26:07,440 --> 07:26:08,878
dot
11283
07:26:08,878 --> 07:26:11,200
pop
11284
07:26:11,200 --> 07:26:13,440
and we'll write the index value
11285
07:26:13,440 --> 07:26:15,920
what we want to remove from the list
11286
07:26:15,920 --> 07:26:17,520
that is one
11287
07:26:17,520 --> 07:26:20,080
and we also want the list to be printed
11288
07:26:20,080 --> 07:26:21,760
so for that we'll write the command
11289
07:26:21,760 --> 07:26:24,760
print
11290
07:26:25,040 --> 07:26:26,558
list one
11291
07:26:26,558 --> 07:26:29,600
will run this command
11292
07:26:30,958 --> 07:26:33,680
okay we have not written the name of the
11293
07:26:33,680 --> 07:26:37,280
list correctly so now list one
11294
07:26:37,280 --> 07:26:38,478
now
11295
07:26:38,478 --> 07:26:41,200
so at the one index that was orange that
11296
07:26:41,200 --> 07:26:43,600
has been removed from the list
11297
07:26:43,600 --> 07:26:47,120
and now this is the new list
11298
07:26:47,840 --> 07:26:48,718
now
11299
07:26:48,718 --> 07:26:51,440
we can see another method that is clear
11300
07:26:51,440 --> 07:26:53,040
that removes all the elements from the
11301
07:26:53,040 --> 07:26:53,920
list
11302
07:26:53,920 --> 07:26:54,718
so
11303
07:26:54,718 --> 07:26:57,040
this we would apply on the list to
11304
07:26:57,040 --> 07:26:59,120
list2 dot
11305
07:26:59,120 --> 07:27:01,920
clear method
11306
07:27:03,440 --> 07:27:05,360
and we'll also write another command
11307
07:27:05,360 --> 07:27:06,958
that would be print
11308
07:27:06,958 --> 07:27:08,320
list 2
11309
07:27:08,320 --> 07:27:10,558
and execute this command we can see that
11310
07:27:10,558 --> 07:27:13,040
the list 2 is empty now all the elements
11311
07:27:13,040 --> 07:27:15,200
from the list 2 have been removed
11312
07:27:15,200 --> 07:27:16,798
now we'll see another method that is
11313
07:27:16,798 --> 07:27:19,040
reverse for that we'll write the command
11314
07:27:19,040 --> 07:27:20,160
list one
11315
07:27:20,160 --> 07:27:22,080
thought reverse
11316
07:27:22,080 --> 07:27:24,558
and round brackets and we also want the
11317
07:27:24,558 --> 07:27:27,200
list one to be printed for that we will
11318
07:27:27,200 --> 07:27:29,040
write the command
11319
07:27:29,040 --> 07:27:32,798
print list one and execute this command
11320
07:27:32,798 --> 07:27:35,200
here we can see that the list one has
11321
07:27:35,200 --> 07:27:37,600
been reversed muskmelon
11322
07:27:37,600 --> 07:27:41,360
guava banana apple
11323
07:27:41,360 --> 07:27:42,398
now
11324
07:27:42,398 --> 07:27:44,558
if you want a particular index element
11325
07:27:44,558 --> 07:27:47,280
to be accessed you can
11326
07:27:47,280 --> 07:27:49,840
have the syntax for that you can write
11327
07:27:49,840 --> 07:27:52,398
that print
11328
07:27:52,398 --> 07:27:54,478
list1 dot
11329
07:27:54,478 --> 07:27:56,958
index
11330
07:27:57,040 --> 07:27:58,958
and in round brackets you just need to
11331
07:27:58,958 --> 07:28:01,280
write the element for what you want to
11332
07:28:01,280 --> 07:28:03,680
get the index
11333
07:28:03,680 --> 07:28:06,000
and just run this command you will get
11334
07:28:06,000 --> 07:28:08,000
the index of the element
11335
07:28:08,000 --> 07:28:11,200
guava that is one
11336
07:28:11,200 --> 07:28:13,280
we have another method and list that is
11337
07:28:13,280 --> 07:28:15,760
count so count is used to give the
11338
07:28:15,760 --> 07:28:18,000
number of times the particular element
11339
07:28:18,000 --> 07:28:20,398
is present in the list so for that we
11340
07:28:20,398 --> 07:28:24,080
will write the syntax print
11341
07:28:24,160 --> 07:28:26,160
so print
11342
07:28:26,160 --> 07:28:28,000
and in round brackets will write the
11343
07:28:28,000 --> 07:28:31,200
list name list1 dot count
11344
07:28:31,200 --> 07:28:32,718
and round brackets will write the
11345
07:28:32,718 --> 07:28:34,240
particular element
11346
07:28:34,240 --> 07:28:36,320
for which we want to get the number of
11347
07:28:36,320 --> 07:28:39,520
times that has been present in the list
11348
07:28:39,520 --> 07:28:42,958
write musk melon
11349
07:28:43,520 --> 07:28:45,840
and run this command it would give us
11350
07:28:45,840 --> 07:28:47,200
the value 1
11351
07:28:47,200 --> 07:28:49,200
because it's been present in the list
11352
07:28:49,200 --> 07:28:51,440
for only a single time
11353
07:28:51,440 --> 07:28:53,680
now let's move to the tuples
11354
07:28:53,680 --> 07:28:56,798
so tuple tuple is almost same as list we
11355
07:28:56,798 --> 07:28:58,558
can have different types of values in
11356
07:28:58,558 --> 07:28:59,680
tuples
11357
07:28:59,680 --> 07:29:01,680
the difference in list
11358
07:29:01,680 --> 07:29:04,558
is we can change value because list is
11359
07:29:04,558 --> 07:29:07,200
mutable and tuple is immutable that
11360
07:29:07,200 --> 07:29:09,760
means you cannot change the value
11361
07:29:09,760 --> 07:29:11,680
a tuple can be defined as a collection
11362
07:29:11,680 --> 07:29:13,920
of objects values or items of different
11363
07:29:13,920 --> 07:29:16,000
types and these collections are enclosed
11364
07:29:16,000 --> 07:29:18,160
within the circle brackets and separated
11365
07:29:18,160 --> 07:29:19,440
by commas
11366
07:29:19,440 --> 07:29:20,958
let's understand this with the help of
11367
07:29:20,958 --> 07:29:23,280
an example we will create a tuple and
11368
07:29:23,280 --> 07:29:25,920
name it as tap1 and insert the elements
11369
07:29:25,920 --> 07:29:27,600
in round brackets and separate them with
11370
07:29:27,600 --> 07:29:29,120
the help of commas
11371
07:29:29,120 --> 07:29:32,080
so w1 equal to round brackets the first
11372
07:29:32,080 --> 07:29:34,638
element is one comma and the second
11373
07:29:34,638 --> 07:29:37,600
element is adam third element is 107 and
11374
07:29:37,600 --> 07:29:40,000
the fourth element is usa
11375
07:29:40,000 --> 07:29:42,718
now in tuples we have our first element
11376
07:29:42,718 --> 07:29:45,200
at zeroth index and we have one at zero
11377
07:29:45,200 --> 07:29:49,360
index and atom at one index and 107 at
11378
07:29:49,360 --> 07:29:52,398
second index and usa at third index
11379
07:29:52,398 --> 07:29:54,478
and for the reverse index it starts with
11380
07:29:54,478 --> 07:29:57,040
minus one index value we have usa at
11381
07:29:57,040 --> 07:30:00,240
minus one value 107 at minus two sec
11382
07:30:00,240 --> 07:30:02,878
index value an atom at minus three next
11383
07:30:02,878 --> 07:30:05,680
value and one at minus four index value
11384
07:30:05,680 --> 07:30:07,440
we will explore the tuples with various
11385
07:30:07,440 --> 07:30:09,440
examples in jupyter notebook
11386
07:30:09,440 --> 07:30:11,040
but till then let's look at the
11387
07:30:11,040 --> 07:30:14,240
characteristics of tuple
11388
07:30:14,558 --> 07:30:16,798
so the first is the tuples are ordered
11389
07:30:16,798 --> 07:30:18,638
when we say that tuples are ordered it
11390
07:30:18,638 --> 07:30:20,398
means that the items have a defined
11391
07:30:20,398 --> 07:30:22,958
order and that order will not change
11392
07:30:22,958 --> 07:30:24,798
elements of the tuples can be accessed
11393
07:30:24,798 --> 07:30:25,920
by index
11394
07:30:25,920 --> 07:30:28,080
double items are indexed the first item
11395
07:30:28,080 --> 07:30:31,200
has index 0 the second index
11396
07:30:31,200 --> 07:30:32,000
has
11397
07:30:32,000 --> 07:30:33,200
1.
11398
07:30:33,200 --> 07:30:35,440
third tuples can store various type of
11399
07:30:35,440 --> 07:30:39,200
elements that is strings arrays and many
11400
07:30:39,200 --> 07:30:40,958
other elements
11401
07:30:40,958 --> 07:30:43,280
the next is tuples are immutable tuples
11402
07:30:43,280 --> 07:30:45,360
are unchangeable meaning that we cannot
11403
07:30:45,360 --> 07:30:47,680
change add or remove items after the
11404
07:30:47,680 --> 07:30:50,958
tuple has been created next is tuples
11405
07:30:50,958 --> 07:30:53,280
allowed duplicate elements since tuples
11406
07:30:53,280 --> 07:30:55,600
are indexed they can have items with the
11407
07:30:55,600 --> 07:30:57,360
same value
11408
07:30:57,360 --> 07:30:59,200
let's have a look at some methods of
11409
07:30:59,200 --> 07:31:01,200
tuples in python
11410
07:31:01,200 --> 07:31:03,520
first that is index we can use the index
11411
07:31:03,520 --> 07:31:06,080
operator to access an item in a tuple
11412
07:31:06,080 --> 07:31:08,558
where the index starts from zero
11413
07:31:08,558 --> 07:31:11,280
its syntax is the name of the tuple dot
11414
07:31:11,280 --> 07:31:13,120
index and in round brackets will write
11415
07:31:13,120 --> 07:31:14,718
the element for which we want to access
11416
07:31:14,718 --> 07:31:16,000
the index
11417
07:31:16,000 --> 07:31:17,920
the second is slicing we can access the
11418
07:31:17,920 --> 07:31:19,680
range of items in a tuple by using the
11419
07:31:19,680 --> 07:31:22,000
slicing operator for that we'll write
11420
07:31:22,000 --> 07:31:24,878
the syntax top one and the range for
11421
07:31:24,878 --> 07:31:27,840
which we want to slice the tuple
11422
07:31:27,840 --> 07:31:30,160
the third is concatenation here we will
11423
07:31:30,160 --> 07:31:33,440
add two tuples that is we just have to
11424
07:31:33,440 --> 07:31:35,280
write the name of the tuples to add them
11425
07:31:35,280 --> 07:31:37,840
and we'll place the plus operator just
11426
07:31:37,840 --> 07:31:39,120
between them
11427
07:31:39,120 --> 07:31:41,200
the next is repetition we can have the
11428
07:31:41,200 --> 07:31:44,240
same item multiple times in a tuple for
11429
07:31:44,240 --> 07:31:46,558
that we'll just have to write the syntax
11430
07:31:46,558 --> 07:31:48,160
the name of the tuple
11431
07:31:48,160 --> 07:31:51,680
and the multiplication operator into the
11432
07:31:51,680 --> 07:31:53,680
times we want that
11433
07:31:53,680 --> 07:31:56,878
value to be present in the tuple
11434
07:31:56,878 --> 07:31:59,040
the next is count it returns the number
11435
07:31:59,040 --> 07:32:01,600
of items a specified value occurs in a
11436
07:32:01,600 --> 07:32:03,200
tuple
11437
07:32:03,200 --> 07:32:05,120
so for that we have the syntax step one
11438
07:32:05,120 --> 07:32:06,958
dot count and in round brackets we will
11439
07:32:06,958 --> 07:32:09,520
just write the element for which we want
11440
07:32:09,520 --> 07:32:11,040
to count the number of times that
11441
07:32:11,040 --> 07:32:13,360
specified value has been occurred
11442
07:32:13,360 --> 07:32:15,040
we also have another method that is
11443
07:32:15,040 --> 07:32:16,000
indexed
11444
07:32:16,000 --> 07:32:17,920
it searches the tuple for a specified
11445
07:32:17,920 --> 07:32:20,638
value and returns the position of
11446
07:32:20,638 --> 07:32:22,798
that where it was found
11447
07:32:22,798 --> 07:32:24,638
now let's move to jupiter notebook to
11448
07:32:24,638 --> 07:32:27,440
explore these methods so let's start
11449
07:32:27,440 --> 07:32:29,280
first we will comment
11450
07:32:29,280 --> 07:32:31,840
that would be tuples
11451
07:32:31,840 --> 07:32:33,280
and we'll
11452
07:32:33,280 --> 07:32:35,440
move to the next column and first we'll
11453
07:32:35,440 --> 07:32:37,360
create a tuple
11454
07:32:37,360 --> 07:32:40,718
for that we'll write the command to p 1
11455
07:32:40,718 --> 07:32:43,040
that would be the name of the tuple and
11456
07:32:43,040 --> 07:32:45,600
in round brackets we'll
11457
07:32:45,600 --> 07:32:48,638
insert the elements that would be
11458
07:32:48,638 --> 07:32:50,320
apple
11459
07:32:50,320 --> 07:32:52,638
coma
11460
07:32:55,200 --> 07:32:57,360
orange
11461
07:32:57,360 --> 07:32:59,760
coma
11462
07:33:00,798 --> 07:33:02,638
banana
11463
07:33:02,638 --> 07:33:05,040
coma
11464
07:33:05,360 --> 07:33:07,760
kv
11465
07:33:08,320 --> 07:33:12,080
now we will print this tuple
11466
07:33:12,240 --> 07:33:14,398
we'll just have to write print and in
11467
07:33:14,398 --> 07:33:16,718
round brackets the name of the topper
11468
07:33:16,718 --> 07:33:18,558
and run the command
11469
07:33:18,558 --> 07:33:20,878
will get the tuple
11470
07:33:20,878 --> 07:33:22,240
printed
11471
07:33:22,240 --> 07:33:23,120
now
11472
07:33:23,120 --> 07:33:25,360
if you just want to insert a single
11473
07:33:25,360 --> 07:33:27,920
element in a tuple for that you will
11474
07:33:27,920 --> 07:33:30,798
write the command tab 2
11475
07:33:30,798 --> 07:33:32,718
equal to in round brackets
11476
07:33:32,718 --> 07:33:34,160
you will write a single element that
11477
07:33:34,160 --> 07:33:36,878
would be apple
11478
07:33:37,120 --> 07:33:39,760
and run that command
11479
07:33:39,760 --> 07:33:42,160
the tuple has been created but this
11480
07:33:42,160 --> 07:33:44,160
tuple has not been considered as tuple
11481
07:33:44,160 --> 07:33:46,718
in python it's been considered as string
11482
07:33:46,718 --> 07:33:47,520
so
11483
07:33:47,520 --> 07:33:50,000
let's check the type of this tuple for
11484
07:33:50,000 --> 07:33:52,958
that we'll write print
11485
07:33:52,958 --> 07:33:54,320
type
11486
07:33:54,320 --> 07:33:56,638
and noun brackets will write the name of
11487
07:33:56,638 --> 07:33:58,878
the tuple we want to check
11488
07:33:58,878 --> 07:34:00,398
and run this command
11489
07:34:00,398 --> 07:34:02,478
here we can see that it is showing a
11490
07:34:02,478 --> 07:34:03,440
string
11491
07:34:03,440 --> 07:34:06,638
so a single element is been shown as
11492
07:34:06,638 --> 07:34:08,398
string
11493
07:34:08,398 --> 07:34:11,040
in python if you want that it should be
11494
07:34:11,040 --> 07:34:12,000
tuple
11495
07:34:12,000 --> 07:34:14,638
for that you just have to
11496
07:34:14,638 --> 07:34:16,798
insert a single comma
11497
07:34:16,798 --> 07:34:19,680
in the tuple and now run this command
11498
07:34:19,680 --> 07:34:22,958
you will get this type as tuple
11499
07:34:22,958 --> 07:34:25,520
now if you want to access any element in
11500
07:34:25,520 --> 07:34:27,440
a tupper for that you just have to write
11501
07:34:27,440 --> 07:34:30,638
the command print
11502
07:34:31,200 --> 07:34:32,320
top one
11503
07:34:32,320 --> 07:34:35,440
and in square brackets the index value
11504
07:34:35,440 --> 07:34:36,878
that is zero
11505
07:34:36,878 --> 07:34:39,280
and here you quote the value apple that
11506
07:34:39,280 --> 07:34:40,240
is at
11507
07:34:40,240 --> 07:34:43,280
zeroth index value in top one
11508
07:34:43,280 --> 07:34:44,000
now
11509
07:34:44,000 --> 07:34:46,160
you can also access the element in the
11510
07:34:46,160 --> 07:34:48,320
reverse order for that we'll write the
11511
07:34:48,320 --> 07:34:51,200
command print
11512
07:34:52,000 --> 07:34:53,600
tap one
11513
07:34:53,600 --> 07:34:55,840
and in square brackets we'll write the
11514
07:34:55,840 --> 07:34:58,240
index value -1
11515
07:34:58,240 --> 07:35:00,958
and run the command here we can get the
11516
07:35:00,958 --> 07:35:04,798
value kv from the reverse index order
11517
07:35:04,798 --> 07:35:07,360
now we also have the slicing operator in
11518
07:35:07,360 --> 07:35:10,160
tuples also
11519
07:35:10,638 --> 07:35:14,398
for that we'll write the command print
11520
07:35:14,398 --> 07:35:15,760
tap1
11521
07:35:15,760 --> 07:35:17,680
in square brackets
11522
07:35:17,680 --> 07:35:19,280
the index values
11523
07:35:19,280 --> 07:35:21,600
will
11524
07:35:26,798 --> 07:35:29,680
run this command print top1 index value
11525
07:35:29,680 --> 07:35:32,000
1 slicing operator 3
11526
07:35:32,000 --> 07:35:35,040
so it has printed orange and banana
11527
07:35:35,040 --> 07:35:37,200
so what does it means it prints the
11528
07:35:37,200 --> 07:35:41,360
index value 1 and 2 and node 3.
11529
07:35:41,360 --> 07:35:43,120
we also have a repetition method in
11530
07:35:43,120 --> 07:35:46,080
tuples for that we just have to write a
11531
07:35:46,080 --> 07:35:48,160
simple syntax
11532
07:35:48,160 --> 07:35:49,840
like print
11533
07:35:49,840 --> 07:35:52,160
in square brackets the element you want
11534
07:35:52,160 --> 07:35:53,600
to repeat
11535
07:35:53,600 --> 07:35:56,840
we will write the element as repeat only
11536
07:35:56,840 --> 07:35:58,638
and
11537
07:35:58,638 --> 07:36:01,520
comma we just considered as tuple
11538
07:36:01,520 --> 07:36:02,718
and just
11539
07:36:02,718 --> 07:36:04,878
after the bracket we would have to write
11540
07:36:04,878 --> 07:36:06,798
the multiply operator and the number of
11541
07:36:06,798 --> 07:36:08,320
times the element should be repeated in
11542
07:36:08,320 --> 07:36:09,440
the tuple
11543
07:36:09,440 --> 07:36:11,520
now we'll run this command
11544
07:36:11,520 --> 07:36:13,120
you can see that the tuple has been
11545
07:36:13,120 --> 07:36:15,200
created with the element repeat and the
11546
07:36:15,200 --> 07:36:17,040
number of times is 3 that we have
11547
07:36:17,040 --> 07:36:19,040
written just after the
11548
07:36:19,040 --> 07:36:20,398
element
11549
07:36:20,398 --> 07:36:22,478
so the tuple has been created now we'll
11550
07:36:22,478 --> 07:36:24,240
see the count method
11551
07:36:24,240 --> 07:36:28,240
for that we'll write the command print
11552
07:36:28,240 --> 07:36:30,080
the name of the tuple
11553
07:36:30,080 --> 07:36:31,040
dot
11554
07:36:31,040 --> 07:36:32,080
count
11555
07:36:32,080 --> 07:36:34,160
and in round brackets we'll write the
11556
07:36:34,160 --> 07:36:36,958
element for which we want to
11557
07:36:36,958 --> 07:36:38,320
know the number of times it's been
11558
07:36:38,320 --> 07:36:40,478
present in the tuple
11559
07:36:40,478 --> 07:36:42,320
and we'll execute the command
11560
07:36:42,320 --> 07:36:44,000
it gives us zero
11561
07:36:44,000 --> 07:36:46,000
as our top one
11562
07:36:46,000 --> 07:36:47,680
doesn't consist
11563
07:36:47,680 --> 07:36:49,840
guava as an element
11564
07:36:49,840 --> 07:36:52,558
so it gives us the count as zero
11565
07:36:52,558 --> 07:36:53,600
now you see
11566
07:36:53,600 --> 07:36:56,000
another method that would be index
11567
07:36:56,000 --> 07:36:58,080
for that we'll write the command
11568
07:36:58,080 --> 07:37:00,478
print
11569
07:37:01,120 --> 07:37:03,440
top one dot
11570
07:37:03,440 --> 07:37:04,878
index
11571
07:37:04,878 --> 07:37:06,638
and in round brackets we'll write the
11572
07:37:06,638 --> 07:37:08,798
element for which we want to fetch the
11573
07:37:08,798 --> 07:37:11,280
index of that element
11574
07:37:11,280 --> 07:37:14,080
banana and we'll run this command
11575
07:37:14,080 --> 07:37:16,080
we will get the index of the element
11576
07:37:16,080 --> 07:37:19,440
banana that is 2
11577
07:37:20,000 --> 07:37:23,360
0 1 2.
11578
07:37:23,360 --> 07:37:25,120
so we get the index of the element
11579
07:37:25,120 --> 07:37:26,798
banana
11580
07:37:26,798 --> 07:37:29,360
now if we want to use the append method
11581
07:37:29,360 --> 07:37:32,080
in tuples we have to first create the
11582
07:37:32,080 --> 07:37:34,878
tuple into the list for that we'll write
11583
07:37:34,878 --> 07:37:37,440
the command y equal to list
11584
07:37:37,440 --> 07:37:40,478
and in round brackets we will write
11585
07:37:40,478 --> 07:37:43,520
the name of the tuple
11586
07:37:44,000 --> 07:37:46,080
which we want to convert in the list
11587
07:37:46,080 --> 07:37:48,638
now we will use the append method
11588
07:37:48,638 --> 07:37:51,680
to the list that is why
11589
07:37:51,680 --> 07:37:53,440
put append
11590
07:37:53,440 --> 07:37:56,478
and will insert an element that would be
11591
07:37:56,478 --> 07:37:59,040
cova
11592
07:38:00,478 --> 07:38:03,718
and now
11593
07:38:07,920 --> 07:38:11,200
we would just assign
11594
07:38:13,280 --> 07:38:18,760
the y list to a variable top one
11595
07:38:18,798 --> 07:38:22,080
we have now converted
11596
07:38:22,080 --> 07:38:25,680
the y list to the tuple with this syntax
11597
07:38:25,680 --> 07:38:26,958
that is
11598
07:38:26,958 --> 07:38:30,160
tuple y and we have assigned it to the
11599
07:38:30,160 --> 07:38:31,680
top one value
11600
07:38:31,680 --> 07:38:36,160
now we'll print this tuple
11601
07:38:36,320 --> 07:38:38,958
and execute the command here
11602
07:38:38,958 --> 07:38:41,280
we can see that we have used the append
11603
07:38:41,280 --> 07:38:42,798
method
11604
07:38:42,798 --> 07:38:44,398
in the tuple
11605
07:38:44,398 --> 07:38:46,398
first we have changed the tuple to the
11606
07:38:46,398 --> 07:38:47,280
list
11607
07:38:47,280 --> 07:38:49,920
by this command y equal to list tab one
11608
07:38:49,920 --> 07:38:51,920
then we have used the append method y
11609
07:38:51,920 --> 07:38:53,120
dot append
11610
07:38:53,120 --> 07:38:55,600
and appended the element cover
11611
07:38:55,600 --> 07:38:58,000
then we have changed the y list to the
11612
07:38:58,000 --> 07:39:00,398
tuple that is stuff one equal to tuple
11613
07:39:00,398 --> 07:39:02,000
the name of the list and then we have
11614
07:39:02,000 --> 07:39:05,440
printed the tuple
11615
07:39:06,320 --> 07:39:08,718
we can also add two tuples by another
11616
07:39:08,718 --> 07:39:09,680
method
11617
07:39:09,680 --> 07:39:11,040
for that
11618
07:39:11,040 --> 07:39:12,638
we will
11619
07:39:12,638 --> 07:39:15,440
write another tuple that would be top
11620
07:39:15,440 --> 07:39:16,718
three
11621
07:39:16,718 --> 07:39:19,120
equal to we will insert an element that
11622
07:39:19,120 --> 07:39:20,240
would be
11623
07:39:20,240 --> 07:39:22,638
cherry
11624
07:39:23,440 --> 07:39:24,718
and now
11625
07:39:24,718 --> 07:39:25,760
we want
11626
07:39:25,760 --> 07:39:30,000
it to be added in top one so we'll write
11627
07:39:30,000 --> 07:39:33,920
top one plus equal to
11628
07:39:34,000 --> 07:39:36,638
top three
11629
07:39:36,718 --> 07:39:38,398
and now we'll
11630
07:39:38,398 --> 07:39:39,920
print
11631
07:39:39,920 --> 07:39:43,280
the final tuple that would be top one
11632
07:39:43,280 --> 07:39:45,760
and run the command here we can see that
11633
07:39:45,760 --> 07:39:47,520
we have added
11634
07:39:47,520 --> 07:39:50,320
two tuples that is tuple one and tuple
11635
07:39:50,320 --> 07:39:52,638
three which consists of cherry and here
11636
07:39:52,638 --> 07:39:55,760
we get the whole tuple as double one
11637
07:39:55,760 --> 07:39:56,958
now
11638
07:39:56,958 --> 07:39:58,240
to execute
11639
07:39:58,240 --> 07:40:01,200
loops in tuples we'll write the command
11640
07:40:01,200 --> 07:40:03,520
for that we'll write the syntax
11641
07:40:03,520 --> 07:40:04,718
for
11642
07:40:04,718 --> 07:40:06,320
space
11643
07:40:06,320 --> 07:40:07,920
a variable x
11644
07:40:07,920 --> 07:40:09,520
and
11645
07:40:09,520 --> 07:40:11,760
the name of the tuple
11646
07:40:11,760 --> 07:40:13,520
colon
11647
07:40:13,520 --> 07:40:16,240
and then we will print
11648
07:40:16,240 --> 07:40:19,040
the particular element one by one and
11649
07:40:19,040 --> 07:40:21,840
we'll run this command here we can see
11650
07:40:21,840 --> 07:40:24,398
that all the elements have been printed
11651
07:40:24,398 --> 07:40:26,878
from the tuple
11652
07:40:26,878 --> 07:40:28,798
now let's understand dictionaries in
11653
07:40:28,798 --> 07:40:31,040
python
11654
07:40:31,040 --> 07:40:33,040
in list you can fetch the elements with
11655
07:40:33,040 --> 07:40:35,440
the help of index numbers what if you
11656
07:40:35,440 --> 07:40:37,280
want to specify a different type of
11657
07:40:37,280 --> 07:40:40,240
index for it example if you have a list
11658
07:40:40,240 --> 07:40:42,320
of eight values and you want to fetch
11659
07:40:42,320 --> 07:40:44,878
the fifth one you will use the index but
11660
07:40:44,878 --> 07:40:47,360
if you have key for each element then
11661
07:40:47,360 --> 07:40:49,280
you can use the key to access that
11662
07:40:49,280 --> 07:40:51,760
element example phone book if you want
11663
07:40:51,760 --> 07:40:53,920
to fetch a number you will use a name
11664
07:40:53,920 --> 07:40:56,320
and you get a number and officially if
11665
07:40:56,320 --> 07:40:58,558
we talk about dictionary if you want to
11666
07:40:58,558 --> 07:41:00,558
understand the meaning of a word you go
11667
07:41:00,558 --> 07:41:02,798
to that page and you look at the word
11668
07:41:02,798 --> 07:41:05,600
and understand the meaning of it so this
11669
07:41:05,600 --> 07:41:07,680
type of concept where you have a key and
11670
07:41:07,680 --> 07:41:10,080
corresponding to that you have a value
11671
07:41:10,080 --> 07:41:11,840
we can achieve this with the help of
11672
07:41:11,840 --> 07:41:14,000
dictionaries
11673
07:41:14,000 --> 07:41:15,840
python dictionary can be defined as a
11674
07:41:15,840 --> 07:41:17,920
collection of objects values or items of
11675
07:41:17,920 --> 07:41:19,920
different types stored in key value pair
11676
07:41:19,920 --> 07:41:22,320
format these multiple key value pairs
11677
07:41:22,320 --> 07:41:24,240
created are enclosed within the curly
11678
07:41:24,240 --> 07:41:26,718
braces and each key is separated from
11679
07:41:26,718 --> 07:41:29,200
its value by the colon let's understand
11680
07:41:29,200 --> 07:41:31,680
this with the help of an example
11681
07:41:31,680 --> 07:41:34,718
dict 1 equal to curly braces the first
11682
07:41:34,718 --> 07:41:37,120
key is a and corresponding to that we
11683
07:41:37,120 --> 07:41:38,958
have value and that is separated by
11684
07:41:38,958 --> 07:41:41,760
colon and after the key value pair that
11685
07:41:41,760 --> 07:41:43,600
is separated by a comma
11686
07:41:43,600 --> 07:41:46,000
the next is b key and that has a
11687
07:41:46,000 --> 07:41:47,760
corresponding value too
11688
07:41:47,760 --> 07:41:50,878
after that we have inserted a comma
11689
07:41:50,878 --> 07:41:53,360
to separate it from other values now we
11690
07:41:53,360 --> 07:41:55,200
have a third key that is c and
11691
07:41:55,200 --> 07:41:57,920
corresponding to that we have a value 3.
11692
07:41:57,920 --> 07:41:59,200
let's see some characteristics of
11693
07:41:59,200 --> 07:42:01,360
dictionaries
11694
07:42:01,360 --> 07:42:03,680
the dictionaries are ordered from python
11695
07:42:03,680 --> 07:42:05,920
version 3.7 when we say that
11696
07:42:05,920 --> 07:42:07,840
dictionaries are ordered it means that
11697
07:42:07,840 --> 07:42:10,000
the items have a defined order and that
11698
07:42:10,000 --> 07:42:12,080
order will not change
11699
07:42:12,080 --> 07:42:13,760
second that is elements of the
11700
07:42:13,760 --> 07:42:16,320
dictionaries cannot be accessed by index
11701
07:42:16,320 --> 07:42:18,240
third dictionaries can store various
11702
07:42:18,240 --> 07:42:19,680
types of elements
11703
07:42:19,680 --> 07:42:21,520
fourth dictionaries are mutable
11704
07:42:21,520 --> 07:42:23,840
dictionaries are changeable meaning that
11705
07:42:23,840 --> 07:42:26,160
we can change add or remove items after
11706
07:42:26,160 --> 07:42:28,240
the dictionary has been created
11707
07:42:28,240 --> 07:42:30,160
fifth one is dictionaries doesn't allow
11708
07:42:30,160 --> 07:42:32,320
duplicate elements dictionaries cannot
11709
07:42:32,320 --> 07:42:34,958
have two items with the same key
11710
07:42:34,958 --> 07:42:36,398
now let's see some methods in
11711
07:42:36,398 --> 07:42:37,600
dictionaries
11712
07:42:37,600 --> 07:42:39,920
the first is clear method
11713
07:42:39,920 --> 07:42:41,520
it removes all the elements from the
11714
07:42:41,520 --> 07:42:43,760
dictionary and its syntax is the name of
11715
07:42:43,760 --> 07:42:46,398
the dictionary dot clear
11716
07:42:46,398 --> 07:42:48,000
the second is get
11717
07:42:48,000 --> 07:42:49,760
it returns the value of the specified
11718
07:42:49,760 --> 07:42:50,718
key
11719
07:42:50,718 --> 07:42:53,440
the syntax is dict1 dot key and in
11720
07:42:53,440 --> 07:42:55,040
square brackets we have to write the key
11721
07:42:55,040 --> 07:42:56,478
name
11722
07:42:56,478 --> 07:42:58,718
the third is keys it returns a list
11723
07:42:58,718 --> 07:43:00,718
containing the dictionaries key
11724
07:43:00,718 --> 07:43:03,600
its syntax is dict1 dot keys
11725
07:43:03,600 --> 07:43:05,920
the next is pop it removes the element
11726
07:43:05,920 --> 07:43:08,478
with the specified key its syntax is
11727
07:43:08,478 --> 07:43:10,240
dict1 dot pop
11728
07:43:10,240 --> 07:43:11,680
and in round brackets you would write
11729
07:43:11,680 --> 07:43:14,160
the particular key name
11730
07:43:14,160 --> 07:43:16,160
and the next method is pop item it
11731
07:43:16,160 --> 07:43:19,200
removes the last inserted key value pair
11732
07:43:19,200 --> 07:43:21,200
from the dictionary and its syntax is
11733
07:43:21,200 --> 07:43:23,920
dict1 dot pop item let's move to the
11734
07:43:23,920 --> 07:43:26,000
jupyter notebook and execute these
11735
07:43:26,000 --> 07:43:28,398
methods
11736
07:43:28,398 --> 07:43:30,240
first we'll add a command that would be
11737
07:43:30,240 --> 07:43:32,320
dictionaries
11738
07:43:32,320 --> 07:43:35,280
and now we'll create
11739
07:43:35,280 --> 07:43:37,520
our dictionary
11740
07:43:37,520 --> 07:43:40,240
for that we'll write the syntax name of
11741
07:43:40,240 --> 07:43:42,000
the dictionary
11742
07:43:42,000 --> 07:43:43,680
equal to
11743
07:43:43,680 --> 07:43:46,558
curly braces
11744
07:43:46,798 --> 07:43:49,120
first we will write the key value
11745
07:43:49,120 --> 07:43:50,638
then colon
11746
07:43:50,638 --> 07:43:52,958
and the value corresponding to it
11747
07:43:52,958 --> 07:43:55,680
will write apple
11748
07:43:55,680 --> 07:43:58,478
then separate it by comma
11749
07:43:58,478 --> 07:44:00,798
and then the second key value
11750
07:44:00,798 --> 07:44:02,160
colon
11751
07:44:02,160 --> 07:44:03,360
and
11752
07:44:03,360 --> 07:44:05,760
the value corresponding to that key
11753
07:44:05,760 --> 07:44:08,798
that would be orange
11754
07:44:08,798 --> 07:44:10,798
when it would be separated by comma then
11755
07:44:10,798 --> 07:44:12,878
we'll assign another key value that
11756
07:44:12,878 --> 07:44:14,080
would be 3
11757
07:44:14,080 --> 07:44:15,360
colon
11758
07:44:15,360 --> 07:44:17,440
and
11759
07:44:17,440 --> 07:44:19,360
the value corresponding to that would be
11760
07:44:19,360 --> 07:44:21,600
banana
11761
07:44:21,600 --> 07:44:23,440
comma
11762
07:44:23,440 --> 07:44:25,040
next with
11763
07:44:25,040 --> 07:44:28,798
the key value would be 4 colon and in
11764
07:44:28,798 --> 07:44:30,718
inverted commas we'll write the value
11765
07:44:30,718 --> 07:44:33,200
corresponding to it
11766
07:44:33,200 --> 07:44:37,558
and we'll print this dictionary
11767
07:44:38,160 --> 07:44:41,600
print text one and execute this command
11768
07:44:41,600 --> 07:44:43,440
here we can see that the dictionary is
11769
07:44:43,440 --> 07:44:44,878
printed
11770
07:44:44,878 --> 07:44:46,240
with one key value we have the
11771
07:44:46,240 --> 07:44:48,558
corresponding value apple with 2k value
11772
07:44:48,558 --> 07:44:49,840
we have the corresponding value orange
11773
07:44:49,840 --> 07:44:51,520
with 3k value as the corresponding value
11774
07:44:51,520 --> 07:44:53,360
banana and with 4 key value we have the
11775
07:44:53,360 --> 07:44:55,520
corresponding value kv
11776
07:44:55,520 --> 07:44:59,920
now to access a particular element
11777
07:45:00,240 --> 07:45:02,798
we can't do that in dictionary but we
11778
07:45:02,798 --> 07:45:05,280
can access a particular value with the
11779
07:45:05,280 --> 07:45:07,360
help of key we can do that in dictionary
11780
07:45:07,360 --> 07:45:10,798
for that we would write the syntax print
11781
07:45:10,798 --> 07:45:13,120
the name of the dictionary
11782
07:45:13,120 --> 07:45:14,718
and in
11783
07:45:14,718 --> 07:45:16,718
square brackets we'll write the key
11784
07:45:16,718 --> 07:45:19,360
value that would be 1 and run the
11785
07:45:19,360 --> 07:45:20,718
command would get the value
11786
07:45:20,718 --> 07:45:22,798
corresponding to the key value
11787
07:45:22,798 --> 07:45:24,878
that is apple that is corresponding to
11788
07:45:24,878 --> 07:45:27,200
the key value 1.
11789
07:45:27,200 --> 07:45:28,160
now
11790
07:45:28,160 --> 07:45:31,040
we will see a cat method in dictionary
11791
07:45:31,040 --> 07:45:34,638
for that we'll write the command print
11792
07:45:34,798 --> 07:45:37,040
name of the dictionary dot
11793
07:45:37,040 --> 07:45:38,080
get
11794
07:45:38,080 --> 07:45:40,558
and in round brackets we'll just write
11795
07:45:40,558 --> 07:45:42,398
the key value
11796
07:45:42,398 --> 07:45:44,798
and run this command we'll get the
11797
07:45:44,798 --> 07:45:47,840
corresponding value to the key value
11798
07:45:47,840 --> 07:45:49,680
that is apple
11799
07:45:49,680 --> 07:45:50,638
now
11800
07:45:50,638 --> 07:45:52,878
we will use another method that would be
11801
07:45:52,878 --> 07:45:54,878
pop for that we will write the command
11802
07:45:54,878 --> 07:45:56,798
print
11803
07:45:56,798 --> 07:45:58,718
deck 1 dot
11804
07:45:58,718 --> 07:45:59,680
pop
11805
07:45:59,680 --> 07:46:01,600
and in round brackets we'll write the
11806
07:46:01,600 --> 07:46:03,040
key value
11807
07:46:03,040 --> 07:46:04,798
for the value
11808
07:46:04,798 --> 07:46:07,600
we want the element to be removed from
11809
07:46:07,600 --> 07:46:09,120
the dictionary
11810
07:46:09,120 --> 07:46:11,120
we will execute this command
11811
07:46:11,120 --> 07:46:13,920
and we get kv as the output now if we
11812
07:46:13,920 --> 07:46:16,478
print the dictionary
11813
07:46:16,478 --> 07:46:18,558
print dict 1
11814
07:46:18,558 --> 07:46:19,760
we can see
11815
07:46:19,760 --> 07:46:22,240
that the fourth key value and the value
11816
07:46:22,240 --> 07:46:23,920
corresponding to that key value has been
11817
07:46:23,920 --> 07:46:26,958
removed by the pop method
11818
07:46:26,958 --> 07:46:29,040
now
11819
07:46:29,040 --> 07:46:30,718
we can also print the length of the
11820
07:46:30,718 --> 07:46:32,958
dictionary for that we'll write the
11821
07:46:32,958 --> 07:46:35,760
command print
11822
07:46:35,840 --> 07:46:37,120
len
11823
07:46:37,120 --> 07:46:38,080
and in
11824
07:46:38,080 --> 07:46:39,920
round brackets we'll write the name of
11825
07:46:39,920 --> 07:46:41,360
the dictionary
11826
07:46:41,360 --> 07:46:43,680
and just execute the command we will get
11827
07:46:43,680 --> 07:46:46,000
three as the output as it has been
11828
07:46:46,000 --> 07:46:48,558
considered one unit second unit and
11829
07:46:48,558 --> 07:46:51,520
third unit so the length is three
11830
07:46:51,520 --> 07:46:52,878
and we have
11831
07:46:52,878 --> 07:46:56,558
another method that is sorted for that
11832
07:46:56,558 --> 07:46:59,680
write the command print
11833
07:46:59,680 --> 07:47:00,718
sorted
11834
07:47:00,718 --> 07:47:03,200
and the name of the dictionary
11835
07:47:03,200 --> 07:47:06,878
dict 1 will execute this command
11836
07:47:06,878 --> 07:47:09,520
it would give us all the key values in a
11837
07:47:09,520 --> 07:47:11,600
sorted manner
11838
07:47:11,600 --> 07:47:14,320
when executed with this command
11839
07:47:14,320 --> 07:47:16,240
now we can see another method that is
11840
07:47:16,240 --> 07:47:17,920
keys for that we have to write the
11841
07:47:17,920 --> 07:47:19,760
command print
11842
07:47:19,760 --> 07:47:22,240
round brackets name of the dictionary
11843
07:47:22,240 --> 07:47:23,440
dot
11844
07:47:23,440 --> 07:47:25,040
keys
11845
07:47:25,040 --> 07:47:26,320
round brackets
11846
07:47:26,320 --> 07:47:28,558
and we'll run this command and we'll get
11847
07:47:28,558 --> 07:47:30,478
all the keys that are present in
11848
07:47:30,478 --> 07:47:32,000
dictionary
11849
07:47:32,000 --> 07:47:34,240
there is another method that is values
11850
07:47:34,240 --> 07:47:37,600
for that we'll write the command print
11851
07:47:37,600 --> 07:47:39,680
deck one dot
11852
07:47:39,680 --> 07:47:42,240
values
11853
07:47:43,280 --> 07:47:45,600
round brackets and we'll execute this
11854
07:47:45,600 --> 07:47:46,638
command
11855
07:47:46,638 --> 07:47:48,320
we'll get all the values that are
11856
07:47:48,320 --> 07:47:50,320
present in the dictionary now we'll see
11857
07:47:50,320 --> 07:47:52,638
another method that is update
11858
07:47:52,638 --> 07:47:54,878
that is used to update the particular
11859
07:47:54,878 --> 07:47:57,440
value corresponding to the key value or
11860
07:47:57,440 --> 07:47:59,760
update the dictionary with
11861
07:47:59,760 --> 07:48:01,360
more values to be inserted in the
11862
07:48:01,360 --> 07:48:02,398
dictionary
11863
07:48:02,398 --> 07:48:05,040
for that we'll write the command dict1
11864
07:48:05,040 --> 07:48:06,398
dot
11865
07:48:06,398 --> 07:48:08,958
update
11866
07:48:09,040 --> 07:48:11,120
in round brackets first we'll
11867
07:48:11,120 --> 07:48:13,840
write the curly braces
11868
07:48:13,840 --> 07:48:15,520
the key value
11869
07:48:15,520 --> 07:48:18,000
semicolon colon
11870
07:48:18,000 --> 07:48:19,920
and corresponding to that will write its
11871
07:48:19,920 --> 07:48:23,120
value which you want to update
11872
07:48:23,120 --> 07:48:25,840
watermelon
11873
07:48:25,840 --> 07:48:28,000
and we'll also
11874
07:48:28,000 --> 07:48:31,120
print the dictionary
11875
07:48:31,840 --> 07:48:35,200
we'll execute this command we will get
11876
07:48:35,200 --> 07:48:36,398
that the
11877
07:48:36,398 --> 07:48:38,798
second value that is watermelon that's
11878
07:48:38,798 --> 07:48:40,638
been updated
11879
07:48:40,638 --> 07:48:42,718
previously it was orange and now it's
11880
07:48:42,718 --> 07:48:44,478
watermelon
11881
07:48:44,478 --> 07:48:46,718
so it's been updated by this command and
11882
07:48:46,718 --> 07:48:48,878
we can also use this command to add more
11883
07:48:48,878 --> 07:48:51,040
elements to the dictionary
11884
07:48:51,040 --> 07:48:53,440
for that we'll write the syntax dict1
11885
07:48:53,440 --> 07:48:56,398
dot update
11886
07:48:57,440 --> 07:49:00,160
round brackets curly braces
11887
07:49:00,160 --> 07:49:03,520
and the key value colon corresponding to
11888
07:49:03,520 --> 07:49:04,320
that
11889
07:49:04,320 --> 07:49:07,600
we will write the value
11890
07:49:07,680 --> 07:49:10,320
muskmelon
11891
07:49:10,320 --> 07:49:14,680
and we will print the dictionary
11892
07:49:16,080 --> 07:49:19,440
and we'll execute this command
11893
07:49:21,840 --> 07:49:24,718
sorry we just
11894
07:49:24,718 --> 07:49:27,120
commented the name of the dictionary
11895
07:49:27,120 --> 07:49:28,798
dict one dot update
11896
07:49:28,798 --> 07:49:31,200
now we will execute this command
11897
07:49:31,200 --> 07:49:33,280
here we can see that the value has been
11898
07:49:33,280 --> 07:49:36,558
added to the dictionary
11899
07:49:37,520 --> 07:49:39,760
now we'll see another method that is pop
11900
07:49:39,760 --> 07:49:43,840
for that we'll write the command dict 1
11901
07:49:43,840 --> 07:49:47,280
pop and in round brackets the key value
11902
07:49:47,280 --> 07:49:49,920
for which we want the value
11903
07:49:49,920 --> 07:49:52,080
to be removed from the dictionary and we
11904
07:49:52,080 --> 07:49:55,360
will print the dictionary
11905
07:49:55,360 --> 07:49:57,360
and execute this command
11906
07:49:57,360 --> 07:50:00,000
here we can see that the second key
11907
07:50:00,000 --> 07:50:01,200
value has been removed from the
11908
07:50:01,200 --> 07:50:03,200
dictionary
11909
07:50:03,200 --> 07:50:05,040
and we will see another method that
11910
07:50:05,040 --> 07:50:07,360
would be text one
11911
07:50:07,360 --> 07:50:08,638
dot
11912
07:50:08,638 --> 07:50:10,840
pop
11913
07:50:10,840 --> 07:50:13,840
item and then we will print the
11914
07:50:13,840 --> 07:50:16,398
dictionary
11915
07:50:18,080 --> 07:50:19,840
and execute the command
11916
07:50:19,840 --> 07:50:21,520
here we can see that the pope item has
11917
07:50:21,520 --> 07:50:23,040
removed the last element from the
11918
07:50:23,040 --> 07:50:25,360
dictionary and we are left with only two
11919
07:50:25,360 --> 07:50:28,080
elements in the dictionary
11920
07:50:28,080 --> 07:50:28,878
now
11921
07:50:28,878 --> 07:50:31,840
we will see some loops in the dictionary
11922
07:50:31,840 --> 07:50:35,200
for that we'll write the command for
11923
07:50:35,200 --> 07:50:36,080
x
11924
07:50:36,080 --> 07:50:38,320
in the name of the dictionary that is
11925
07:50:38,320 --> 07:50:39,440
text one
11926
07:50:39,440 --> 07:50:40,958
colon
11927
07:50:40,958 --> 07:50:44,638
and then we will write print
11928
07:50:45,440 --> 07:50:46,878
dict one
11929
07:50:46,878 --> 07:50:48,958
and in square brackets
11930
07:50:48,958 --> 07:50:51,600
the variable x and we'll execute this
11931
07:50:51,600 --> 07:50:53,360
command
11932
07:50:53,360 --> 07:50:55,520
so with this loop we can print all the
11933
07:50:55,520 --> 07:50:57,600
values corresponding to the key values
11934
07:50:57,600 --> 07:50:59,040
in the dictionary
11935
07:50:59,040 --> 07:51:00,878
now to print both the
11936
07:51:00,878 --> 07:51:03,280
even values corresponding to it we can
11937
07:51:03,280 --> 07:51:05,200
run a loop for that we'll write the
11938
07:51:05,200 --> 07:51:08,320
command for x comma y
11939
07:51:08,320 --> 07:51:10,000
in
11940
07:51:10,000 --> 07:51:12,160
dict1
11941
07:51:12,160 --> 07:51:15,200
dot items
11942
07:51:16,080 --> 07:51:18,558
colon
11943
07:51:19,040 --> 07:51:20,558
print
11944
07:51:20,558 --> 07:51:23,280
x comma y
11945
07:51:23,280 --> 07:51:25,040
and run this command
11946
07:51:25,040 --> 07:51:26,878
we can get the key and the values
11947
07:51:26,878 --> 07:51:29,760
corresponding to it
11948
07:51:30,718 --> 07:51:32,798
now if you want to copy the particular
11949
07:51:32,798 --> 07:51:35,360
dictionary in another dictionary you can
11950
07:51:35,360 --> 07:51:37,120
write the command
11951
07:51:37,120 --> 07:51:39,440
first you will name the dictionary you
11952
07:51:39,440 --> 07:51:42,240
want to create and copy the elements
11953
07:51:42,240 --> 07:51:44,958
we will name it as my dict
11954
07:51:44,958 --> 07:51:47,040
equal to
11955
07:51:47,040 --> 07:51:50,798
now i will write the command dict 1 dot
11956
07:51:50,798 --> 07:51:53,120
copy
11957
07:51:53,120 --> 07:51:54,638
and then
11958
07:51:54,638 --> 07:51:57,760
we will print the new dictionary
11959
07:51:57,760 --> 07:51:59,840
that is
11960
07:51:59,840 --> 07:52:02,080
my
11961
07:52:02,840 --> 07:52:07,040
dict we'll run this command
11962
07:52:07,360 --> 07:52:09,040
okay we have
11963
07:52:09,040 --> 07:52:11,280
not inserted the wrong bracket here now
11964
07:52:11,280 --> 07:52:13,920
then this command here we can see that
11965
07:52:13,920 --> 07:52:16,160
we have printed the my dick dictionary
11966
07:52:16,160 --> 07:52:19,920
and it has the same values as dict one
11967
07:52:19,920 --> 07:52:20,718
now
11968
07:52:20,718 --> 07:52:23,200
we'll see another method
11969
07:52:23,200 --> 07:52:25,520
to delete
11970
07:52:25,520 --> 07:52:28,080
the whole dictionary
11971
07:52:28,080 --> 07:52:30,638
for that we'll write
11972
07:52:30,638 --> 07:52:33,280
text one dot
11973
07:52:33,280 --> 07:52:35,040
clear
11974
07:52:35,040 --> 07:52:36,878
it would remove all the values from the
11975
07:52:36,878 --> 07:52:39,440
dictionary and will also
11976
07:52:39,440 --> 07:52:40,398
print
11977
07:52:40,398 --> 07:52:43,120
the dictionary after that
11978
07:52:43,120 --> 07:52:45,040
then this command here we can see all
11979
07:52:45,040 --> 07:52:46,718
the elements from the dictionary have
11980
07:52:46,718 --> 07:52:48,080
been removed
11981
07:52:48,080 --> 07:52:50,958
so what is list comprehension
11982
07:52:50,958 --> 07:52:53,280
in python programming list comprehension
11983
07:52:53,280 --> 07:52:56,398
provides a short as syntax
11984
07:52:56,398 --> 07:52:58,240
when you want to create a new list based
11985
07:52:58,240 --> 07:53:00,478
on the values of an existing list
11986
07:53:00,478 --> 07:53:02,798
so you can build a new list with a
11987
07:53:02,798 --> 07:53:05,040
compact syntax based on the values of
11988
07:53:05,040 --> 07:53:06,558
the existing list using python
11989
07:53:06,558 --> 07:53:08,398
programming
11990
07:53:08,398 --> 07:53:10,958
now you can define a list comprehension
11991
07:53:10,958 --> 07:53:13,120
inside the square brackets and execute
11992
07:53:13,120 --> 07:53:15,120
each element using a for loop that will
11993
07:53:15,120 --> 07:53:18,000
iterate over all the elements
11994
07:53:18,000 --> 07:53:20,080
now below you can see the syntax of
11995
07:53:20,080 --> 07:53:22,958
writing a list comprehension i have my
11996
07:53:22,958 --> 07:53:25,120
variable declared as new list that will
11997
07:53:25,120 --> 07:53:27,440
store the list comprehension
11998
07:53:27,440 --> 07:53:29,200
so using square brackets i have
11999
07:53:29,200 --> 07:53:30,958
mentioned expression
12000
07:53:30,958 --> 07:53:32,718
and i'm using the for loop which is
12001
07:53:32,718 --> 07:53:34,958
going to iterate over all the elements
12002
07:53:34,958 --> 07:53:36,160
in my
12003
07:53:36,160 --> 07:53:38,638
old list and after that i have my if
12004
07:53:38,638 --> 07:53:41,120
condition
12005
07:53:41,200 --> 07:53:43,040
now let's look at the advantages of
12006
07:53:43,040 --> 07:53:45,440
using list comprehension
12007
07:53:45,440 --> 07:53:48,000
so it helps to accomplish your task with
12008
07:53:48,000 --> 07:53:49,840
fewer lines of code
12009
07:53:49,840 --> 07:53:52,000
now it is more time efficient compared
12010
07:53:52,000 --> 07:53:54,080
to running multiple loops for the same
12011
07:53:54,080 --> 07:53:55,040
task
12012
07:53:55,040 --> 07:53:56,878
and you can use iterative statements
12013
07:53:56,878 --> 07:53:59,680
that appear as formula now let's head
12014
07:53:59,680 --> 07:54:01,360
over to our jupyter notebook where we'll
12015
07:54:01,360 --> 07:54:04,718
learn list comprehension in detail
12016
07:54:04,718 --> 07:54:07,440
so i am on my jupyter notebook so let's
12017
07:54:07,440 --> 07:54:10,080
start with our first example
12018
07:54:10,080 --> 07:54:12,240
suppose you want to
12019
07:54:12,240 --> 07:54:14,240
find the squares of all the numbers that
12020
07:54:14,240 --> 07:54:16,638
are present inside a list
12021
07:54:16,638 --> 07:54:18,478
now let me show you the traditional way
12022
07:54:18,478 --> 07:54:20,080
of solving this problem
12023
07:54:20,080 --> 07:54:22,638
so first of all i'll declare a
12024
07:54:22,638 --> 07:54:24,718
list called num
12025
07:54:24,718 --> 07:54:26,958
and within square brackets i'll
12026
07:54:26,958 --> 07:54:29,760
have some numbers let's say 1 2 3 4
12027
07:54:29,760 --> 07:54:31,120
and now
12028
07:54:31,120 --> 07:54:32,958
i'll create an
12029
07:54:32,958 --> 07:54:35,280
empty list called sq that will
12030
07:54:35,280 --> 07:54:36,798
hold the
12031
07:54:36,798 --> 07:54:39,120
squares of my four numbers that i have
12032
07:54:39,120 --> 07:54:41,360
put
12033
07:54:42,558 --> 07:54:45,200
so i'll say for
12034
07:54:45,200 --> 07:54:48,398
n which is my iterator variable in
12035
07:54:48,398 --> 07:54:51,360
my list num i'll give a colon
12036
07:54:51,360 --> 07:54:53,280
and then i am going to
12037
07:54:53,280 --> 07:54:55,520
append the squares to my variable called
12038
07:54:55,520 --> 07:54:58,000
sq so i'll write sq dot
12039
07:54:58,000 --> 07:54:59,520
append
12040
07:54:59,520 --> 07:55:02,080
and to get the squares i'll say n
12041
07:55:02,080 --> 07:55:03,600
i'll use the
12042
07:55:03,600 --> 07:55:05,280
double asterisks operator to get the
12043
07:55:05,280 --> 07:55:08,240
squares i'll say double asterisks 2.
12044
07:55:08,240 --> 07:55:09,120
now
12045
07:55:09,120 --> 07:55:11,920
i'll just say print sq
12046
07:55:11,920 --> 07:55:14,638
if i run this i get the result which is
12047
07:55:14,638 --> 07:55:16,240
fair enough so the
12048
07:55:16,240 --> 07:55:21,520
squares of 1 2 3 and 4 are 1 4 9 and 16.
12049
07:55:21,520 --> 07:55:22,718
now
12050
07:55:22,718 --> 07:55:26,080
the same task you can do using
12051
07:55:26,080 --> 07:55:28,638
list comprehension in just
12052
07:55:28,638 --> 07:55:30,000
two lines
12053
07:55:30,000 --> 07:55:32,798
first of all i'll just declare my list
12054
07:55:32,798 --> 07:55:35,360
again say num
12055
07:55:35,360 --> 07:55:37,600
which will have the values 1 2 3 4 and
12056
07:55:37,600 --> 07:55:39,280
let's say 5.
12057
07:55:39,280 --> 07:55:40,798
now i'm going to write my list
12058
07:55:40,798 --> 07:55:42,798
comprehension
12059
07:55:42,798 --> 07:55:45,920
like this i'll say sq equal to
12060
07:55:45,920 --> 07:55:50,240
i'll have my expression
12061
07:55:50,240 --> 07:55:52,000
which is going to be n
12062
07:55:52,000 --> 07:55:54,878
asterisk asterisks 2
12063
07:55:54,878 --> 07:55:57,360
then i'll say for
12064
07:55:57,360 --> 07:55:59,280
my iterator variable n
12065
07:55:59,280 --> 07:56:02,478
in my list num that's all
12066
07:56:02,478 --> 07:56:06,240
so this is how you can write a
12067
07:56:07,440 --> 07:56:10,398
list comprehension method
12068
07:56:10,398 --> 07:56:12,080
and then let's print
12069
07:56:12,080 --> 07:56:14,478
sq
12070
07:56:15,040 --> 07:56:16,478
now if i run this
12071
07:56:16,478 --> 07:56:18,398
we get the same result you can see the
12072
07:56:18,398 --> 07:56:22,878
squares of 1 2 3 4 and 5 are 1 4 9 16
12073
07:56:22,878 --> 07:56:24,240
and 25
12074
07:56:24,240 --> 07:56:24,958
so
12075
07:56:24,958 --> 07:56:26,958
this is a simple example how you can use
12076
07:56:26,958 --> 07:56:31,040
list comprehension in python it is very
12077
07:56:31,040 --> 07:56:33,280
short and you can write
12078
07:56:33,280 --> 07:56:34,558
the same
12079
07:56:34,558 --> 07:56:37,120
task in a very compact manner as you can
12080
07:56:37,120 --> 07:56:38,558
see here
12081
07:56:38,558 --> 07:56:40,080
cool
12082
07:56:40,080 --> 07:56:42,798
now moving ahead let's say you wanted to
12083
07:56:42,798 --> 07:56:45,280
print all the letters in the word simply
12084
07:56:45,280 --> 07:56:46,240
don't
12085
07:56:46,240 --> 07:56:47,840
so for that
12086
07:56:47,840 --> 07:56:51,120
let me show you how to
12087
07:56:51,120 --> 07:56:53,440
do it in a traditional way first
12088
07:56:53,440 --> 07:56:55,680
so i'll create a
12089
07:56:55,680 --> 07:56:56,840
empty
12090
07:56:56,840 --> 07:57:00,080
list called let's say n underscore
12091
07:57:00,080 --> 07:57:01,920
letters
12092
07:57:01,920 --> 07:57:04,320
i'll just give square brackets so this
12093
07:57:04,320 --> 07:57:05,120
means
12094
07:57:05,120 --> 07:57:07,680
your end underscore letters is now
12095
07:57:07,680 --> 07:57:09,920
an empty list so i'll say for my
12096
07:57:09,920 --> 07:57:12,558
iterator variable letter in
12097
07:57:12,558 --> 07:57:13,440
my
12098
07:57:13,440 --> 07:57:15,360
word that is
12099
07:57:15,360 --> 07:57:18,398
simply learn i'll give a colon
12100
07:57:18,398 --> 07:57:19,840
then i'll say
12101
07:57:19,840 --> 07:57:21,840
n underscore
12102
07:57:21,840 --> 07:57:23,040
letters
12103
07:57:23,040 --> 07:57:25,200
dot
12104
07:57:25,200 --> 07:57:27,760
append
12105
07:57:27,760 --> 07:57:30,240
my data variable letter
12106
07:57:30,240 --> 07:57:32,240
now let me print
12107
07:57:32,240 --> 07:57:33,840
and underscore
12108
07:57:33,840 --> 07:57:35,360
letters
12109
07:57:35,360 --> 07:57:36,718
if i run this
12110
07:57:36,718 --> 07:57:38,638
you see here we have
12111
07:57:38,638 --> 07:57:40,878
printed all the
12112
07:57:40,878 --> 07:57:42,878
individual letters that are present in
12113
07:57:42,878 --> 07:57:45,840
the word simply learn
12114
07:57:45,840 --> 07:57:49,200
now the same task you can do using
12115
07:57:49,200 --> 07:57:52,240
list comprehension in just one line
12116
07:57:52,240 --> 07:57:53,920
so create a variable called end
12117
07:57:53,920 --> 07:57:55,440
underscore letters
12118
07:57:55,440 --> 07:57:57,840
and then i'll write my syntax as
12119
07:57:57,840 --> 07:58:00,558
letter for
12120
07:58:00,558 --> 07:58:05,440
letter in my word simply learn
12121
07:58:06,958 --> 07:58:09,440
and then i'll just say print and
12122
07:58:09,440 --> 07:58:11,760
underscore letters
12123
07:58:11,760 --> 07:58:14,320
run this there you go
12124
07:58:14,320 --> 07:58:16,240
you can use the
12125
07:58:16,240 --> 07:58:18,718
list comprehension technique to
12126
07:58:18,718 --> 07:58:20,878
implement the above task in just one
12127
07:58:20,878 --> 07:58:24,160
line all right
12128
07:58:24,160 --> 07:58:25,680
now moving ahead
12129
07:58:25,680 --> 07:58:28,000
we'll see how to use
12130
07:58:28,000 --> 07:58:29,440
a for loop
12131
07:58:29,440 --> 07:58:30,558
and
12132
07:58:30,558 --> 07:58:32,478
we are going to display
12133
07:58:32,478 --> 07:58:33,840
the cars
12134
07:58:33,840 --> 07:58:37,440
that are present in a list and have a
12135
07:58:37,440 --> 07:58:40,000
letter s in it
12136
07:58:40,000 --> 07:58:42,798
so first of all i am going to
12137
07:58:42,798 --> 07:58:45,360
declare a list called cars
12138
07:58:45,360 --> 07:58:47,680
and it will have
12139
07:58:47,680 --> 07:58:49,760
the names of a few cars let's say we
12140
07:58:49,760 --> 07:58:51,840
have
12141
07:58:52,558 --> 07:58:54,958
jaguar
12142
07:58:54,958 --> 07:58:57,680
then we have
12143
07:58:57,680 --> 07:59:00,398
land rover
12144
07:59:00,958 --> 07:59:03,280
next let's say we have
12145
07:59:03,280 --> 07:59:05,840
tesla
12146
07:59:06,718 --> 07:59:09,120
after that let's say we have
12147
07:59:09,120 --> 07:59:11,680
toyota
12148
07:59:11,760 --> 07:59:13,680
and then we have
12149
07:59:13,680 --> 07:59:16,080
tata
12150
07:59:17,360 --> 07:59:18,398
now
12151
07:59:18,398 --> 07:59:22,160
i'm going to create an empty list called
12152
07:59:22,160 --> 07:59:24,718
new list
12153
07:59:24,718 --> 07:59:27,440
i'll just give
12154
07:59:27,440 --> 07:59:30,240
square brackets
12155
07:59:30,638 --> 07:59:33,840
now i'll say for my i treated variable x
12156
07:59:33,840 --> 07:59:35,120
in
12157
07:59:35,120 --> 07:59:36,718
my
12158
07:59:36,718 --> 07:59:39,520
list cars i'll say if
12159
07:59:39,520 --> 07:59:43,360
we have the letter s in
12160
07:59:43,360 --> 07:59:44,240
x
12161
07:59:44,240 --> 07:59:45,600
then
12162
07:59:45,600 --> 07:59:47,200
i'll say
12163
07:59:47,200 --> 07:59:50,878
new list dot append
12164
07:59:50,878 --> 07:59:53,120
x
12165
07:59:53,440 --> 07:59:56,320
after that i'm going to say print
12166
07:59:56,320 --> 07:59:58,240
new list
12167
07:59:58,240 --> 07:59:59,600
so what
12168
07:59:59,600 --> 08:00:02,478
these lines of code will do is my for
12169
08:00:02,478 --> 08:00:04,080
loop is going to iterate through each of
12170
08:00:04,080 --> 08:00:05,680
the elements that are present inside the
12171
08:00:05,680 --> 08:00:09,200
list cars and will check if any of the
12172
08:00:09,200 --> 08:00:11,600
car brands have the letter s it is going
12173
08:00:11,600 --> 08:00:13,040
to print
12174
08:00:13,040 --> 08:00:14,958
that car brand
12175
08:00:14,958 --> 08:00:16,958
if i run it you can see here
12176
08:00:16,958 --> 08:00:21,040
we have tesla which has a letter s in it
12177
08:00:21,040 --> 08:00:23,600
the rest of the car brands don't have
12178
08:00:23,600 --> 08:00:27,280
the letter s and hence we have got tesla
12179
08:00:27,280 --> 08:00:30,080
now the same
12180
08:00:30,958 --> 08:00:33,680
task you can do using list comprehension
12181
08:00:33,680 --> 08:00:36,000
i'll just copy this line of code because
12182
08:00:36,000 --> 08:00:38,638
we are going to use the same car brands
12183
08:00:38,638 --> 08:00:41,200
and the same
12184
08:00:41,440 --> 08:00:43,200
list name called cars
12185
08:00:43,200 --> 08:00:45,280
now i'm going to write my
12186
08:00:45,280 --> 08:00:47,760
list comprehension technique i'll say
12187
08:00:47,760 --> 08:00:49,600
new list equal to
12188
08:00:49,600 --> 08:00:52,958
as x for x in
12189
08:00:52,958 --> 08:00:54,160
cars
12190
08:00:54,160 --> 08:00:56,958
if i'm going to use the if condition now
12191
08:00:56,958 --> 08:00:59,040
i'll say s
12192
08:00:59,040 --> 08:01:00,240
in
12193
08:01:00,240 --> 08:01:02,478
x
12194
08:01:02,718 --> 08:01:05,200
now let's go ahead and print my
12195
08:01:05,200 --> 08:01:08,080
new list but on this there you go you
12196
08:01:08,080 --> 08:01:11,760
can see that we have tesla printed
12197
08:01:11,760 --> 08:01:15,200
now let me just change this s to
12198
08:01:15,200 --> 08:01:18,240
small a or lowercase e
12199
08:01:18,240 --> 08:01:20,240
if i run it
12200
08:01:20,240 --> 08:01:22,000
you see here
12201
08:01:22,000 --> 08:01:24,478
all the five brands of car have the
12202
08:01:24,478 --> 08:01:26,558
letter
12203
08:01:26,558 --> 08:01:29,280
small a you can see jaguar has it land
12204
08:01:29,280 --> 08:01:31,520
rover also we have small a tesla has
12205
08:01:31,520 --> 08:01:32,558
small a
12206
08:01:32,558 --> 08:01:34,080
we have toyota
12207
08:01:34,080 --> 08:01:38,798
and tata as well now what if i change
12208
08:01:38,798 --> 08:01:41,440
the name of data to all upper case
12209
08:01:41,440 --> 08:01:44,320
so i write d8ta all in uppercase now if
12210
08:01:44,320 --> 08:01:45,520
i run this
12211
08:01:45,520 --> 08:01:46,558
there you go
12212
08:01:46,558 --> 08:01:49,680
since python is case sensitive
12213
08:01:49,680 --> 08:01:51,520
and here we are checking only for
12214
08:01:51,520 --> 08:01:54,160
lowercase a so we did not have
12215
08:01:54,160 --> 08:01:56,000
data in our result since we converted
12216
08:01:56,000 --> 08:01:57,920
into an upper case
12217
08:01:57,920 --> 08:02:00,240
all right now moving ahead
12218
08:02:00,240 --> 08:02:02,398
i'm going to show you how you can
12219
08:02:02,398 --> 08:02:03,520
filter
12220
08:02:03,520 --> 08:02:06,398
only those values and a list of integers
12221
08:02:06,398 --> 08:02:09,280
that are greater than 50.
12222
08:02:09,280 --> 08:02:11,520
so i'll say
12223
08:02:11,520 --> 08:02:15,520
new underscore list equal to
12224
08:02:15,520 --> 08:02:16,798
i'll say
12225
08:02:16,798 --> 08:02:22,320
x for x in range
12226
08:02:22,320 --> 08:02:24,320
i'm using the range function so i want
12227
08:02:24,320 --> 08:02:26,958
to print the list of integers
12228
08:02:26,958 --> 08:02:29,040
the first hundred integers
12229
08:02:29,040 --> 08:02:31,440
and i'll use my condition f
12230
08:02:31,440 --> 08:02:35,040
x is greater than 50
12231
08:02:35,040 --> 08:02:37,840
now let me go ahead and print my new
12232
08:02:37,840 --> 08:02:41,040
list which is new underscore list
12233
08:02:41,040 --> 08:02:42,878
but on this there you go
12234
08:02:42,878 --> 08:02:45,520
so here you can see
12235
08:02:45,520 --> 08:02:47,360
our
12236
08:02:47,360 --> 08:02:49,440
range is from
12237
08:02:49,440 --> 08:02:50,958
0 to 100
12238
08:02:50,958 --> 08:02:52,638
which means from 0
12239
08:02:52,638 --> 08:02:56,000
till 99 you can see it here
12240
08:02:56,000 --> 08:02:57,920
but we are only trying to print only
12241
08:02:57,920 --> 08:02:59,680
those integers that are greater than 50
12242
08:02:59,680 --> 08:03:02,958
so we have values from 51 till 99
12243
08:03:02,958 --> 08:03:05,120
suppose i'll change this to
12244
08:03:05,120 --> 08:03:07,360
let's say less than
12245
08:03:07,360 --> 08:03:11,680
45 or let's say less than 30
12246
08:03:11,680 --> 08:03:13,360
and if i run this
12247
08:03:13,360 --> 08:03:15,760
you see here you have all the integers
12248
08:03:15,760 --> 08:03:19,200
present between 0 till 30
12249
08:03:19,200 --> 08:03:21,440
cool
12250
08:03:21,600 --> 08:03:23,360
now moving ahead
12251
08:03:23,360 --> 08:03:25,040
let's see another example i am going to
12252
08:03:25,040 --> 08:03:27,760
use the same list which we had
12253
08:03:27,760 --> 08:03:29,760
declared before that is the list of car
12254
08:03:29,760 --> 08:03:30,958
brands
12255
08:03:30,958 --> 08:03:32,000
now
12256
08:03:32,000 --> 08:03:33,920
i am going to
12257
08:03:33,920 --> 08:03:35,520
print
12258
08:03:35,520 --> 08:03:37,760
all the car brands
12259
08:03:37,760 --> 08:03:38,958
if it is
12260
08:03:38,958 --> 08:03:40,638
not tesla
12261
08:03:40,638 --> 08:03:43,600
but if the car brand is tesla we are
12262
08:03:43,600 --> 08:03:46,240
going to change this to let's say audi
12263
08:03:46,240 --> 08:03:48,878
now let me show you how to do it using
12264
08:03:48,878 --> 08:03:50,958
list comprehension
12265
08:03:50,958 --> 08:03:52,478
i'll say
12266
08:03:52,478 --> 08:03:53,920
new underscore
12267
08:03:53,920 --> 08:03:55,360
cars
12268
08:03:55,360 --> 08:03:56,718
equal to
12269
08:03:56,718 --> 08:03:59,840
i use the square brackets i'll say x
12270
08:03:59,840 --> 08:04:01,040
if
12271
08:04:01,040 --> 08:04:02,240
x
12272
08:04:02,240 --> 08:04:04,320
not equal to so i'm going to use the
12273
08:04:04,320 --> 08:04:08,160
exclamation mark equal to
12274
08:04:08,398 --> 08:04:10,878
tesla
12275
08:04:11,120 --> 08:04:13,440
then
12276
08:04:13,440 --> 08:04:16,638
we should print x
12277
08:04:16,638 --> 08:04:18,000
else
12278
08:04:18,000 --> 08:04:19,920
i'm going to use the else clause as well
12279
08:04:19,920 --> 08:04:22,638
and say else
12280
08:04:22,638 --> 08:04:24,558
if the car print is
12281
08:04:24,558 --> 08:04:28,080
tesla then changes to audi
12282
08:04:28,080 --> 08:04:32,160
and i'll say for x in
12283
08:04:32,160 --> 08:04:34,558
cars
12284
08:04:34,718 --> 08:04:39,120
now let me go ahead and print new cars
12285
08:04:39,120 --> 08:04:40,478
there you go
12286
08:04:40,478 --> 08:04:41,840
so
12287
08:04:41,840 --> 08:04:44,798
since all these elements were not tesla
12288
08:04:44,798 --> 08:04:47,680
so we printed the car brands as it is so
12289
08:04:47,680 --> 08:04:51,120
jaguar land rover toyota and
12290
08:04:51,120 --> 08:04:52,080
tata
12291
08:04:52,080 --> 08:04:53,280
now since
12292
08:04:53,280 --> 08:04:57,360
we encountered one tesla here so
12293
08:04:57,360 --> 08:04:58,718
with the help of
12294
08:04:58,718 --> 08:05:01,280
list comprehension we changed tesla to
12295
08:05:01,280 --> 08:05:02,478
audi
12296
08:05:02,478 --> 08:05:04,718
cool
12297
08:05:05,440 --> 08:05:09,000
now moving ahead
12298
08:05:10,478 --> 08:05:13,440
so using function will print true for
12299
08:05:13,440 --> 08:05:15,440
even numbers and false for
12300
08:05:15,440 --> 08:05:18,080
odd numbers let me show you how to do it
12301
08:05:18,080 --> 08:05:20,718
so i'll create a
12302
08:05:20,718 --> 08:05:23,440
empty list called numbers
12303
08:05:23,440 --> 08:05:25,280
so this is an empty list
12304
08:05:25,280 --> 08:05:28,080
now i'll say for i in
12305
08:05:28,080 --> 08:05:29,360
range
12306
08:05:29,360 --> 08:05:31,600
let's say i'll set the range to 0 comma
12307
08:05:31,600 --> 08:05:33,840
20 i'll give a colon
12308
08:05:33,840 --> 08:05:35,360
i'll say
12309
08:05:35,360 --> 08:05:37,440
numbers dot
12310
08:05:37,440 --> 08:05:39,280
append
12311
08:05:39,280 --> 08:05:41,920
i'm going to give my condition
12312
08:05:41,920 --> 08:05:43,040
i
12313
08:05:43,040 --> 08:05:46,000
percentage 2 should be equal to equal to
12314
08:05:46,000 --> 08:05:48,638
0 so this means
12315
08:05:48,638 --> 08:05:51,360
within the range 0 to 20 i am checking
12316
08:05:51,360 --> 08:05:53,440
for numbers that are divisible by 2 so i
12317
08:05:53,440 --> 08:05:55,920
am seeing if the
12318
08:05:55,920 --> 08:05:57,520
reminder is 0
12319
08:05:57,520 --> 08:05:58,958
then i'll say
12320
08:05:58,958 --> 08:06:01,280
print
12321
08:06:01,520 --> 08:06:04,520
numbers
12322
08:06:05,520 --> 08:06:07,120
now let me run it
12323
08:06:07,120 --> 08:06:08,478
there you go
12324
08:06:08,478 --> 08:06:10,718
so there are total
12325
08:06:10,718 --> 08:06:14,478
20 numbers starting from 0 till 19
12326
08:06:14,478 --> 08:06:15,360
now
12327
08:06:15,360 --> 08:06:17,120
wherever the number is
12328
08:06:17,120 --> 08:06:19,920
even we are printing true else we are
12329
08:06:19,920 --> 08:06:22,558
printing false so this number starts
12330
08:06:22,558 --> 08:06:23,840
from the zero
12331
08:06:23,840 --> 08:06:25,520
place so
12332
08:06:25,520 --> 08:06:28,878
we have 0 as true 1 is false again 2 is
12333
08:06:28,878 --> 08:06:31,440
true because 2 is an even number 3 is an
12334
08:06:31,440 --> 08:06:33,920
odd number so we are false 4 is an even
12335
08:06:33,920 --> 08:06:36,398
number we have true so here
12336
08:06:36,398 --> 08:06:38,638
5 is an odd number so we have false so
12337
08:06:38,638 --> 08:06:41,520
on and so forth and the last 3
12338
08:06:41,520 --> 08:06:44,160
so 17 is an odd number so we are false
12339
08:06:44,160 --> 08:06:46,398
18 is an even number we have true and 19
12340
08:06:46,398 --> 08:06:50,160
again is an odd number so we have false
12341
08:06:50,160 --> 08:06:52,638
all right
12342
08:06:53,040 --> 08:06:54,958
now moving ahead
12343
08:06:54,958 --> 08:06:56,638
we'll see
12344
08:06:56,638 --> 08:06:59,520
another example
12345
08:06:59,520 --> 08:07:02,398
let me create
12346
08:07:02,398 --> 08:07:04,798
a variable called numbers
12347
08:07:04,798 --> 08:07:05,920
i'll say
12348
08:07:05,920 --> 08:07:07,520
i
12349
08:07:07,520 --> 08:07:11,040
percentage 2 equal to equal to 0
12350
08:07:11,040 --> 08:07:11,840
for
12351
08:07:11,840 --> 08:07:13,600
i
12352
08:07:13,600 --> 08:07:15,040
in
12353
08:07:15,040 --> 08:07:17,440
range
12354
08:07:17,920 --> 08:07:21,760
0 comma 21
12355
08:07:21,760 --> 08:07:26,320
let's print numbers and see the result
12356
08:07:26,398 --> 08:07:29,760
so here you can see we have the
12357
08:07:29,760 --> 08:07:32,320
same result this time
12358
08:07:32,320 --> 08:07:34,718
using
12359
08:07:34,878 --> 08:07:37,760
the list comprehension technique
12360
08:07:37,760 --> 08:07:39,360
now coming to the
12361
08:07:39,360 --> 08:07:40,558
final
12362
08:07:40,558 --> 08:07:42,958
example in this demo this time i am
12363
08:07:42,958 --> 08:07:45,520
going to create a four course four
12364
08:07:45,520 --> 08:07:49,360
matrix using the numpy library in python
12365
08:07:49,360 --> 08:07:51,840
and from the matrix we are going to find
12366
08:07:51,840 --> 08:07:54,718
the maximum element present in
12367
08:07:54,718 --> 08:07:56,558
each of the rows
12368
08:07:56,558 --> 08:07:59,520
so let me first import my
12369
08:07:59,520 --> 08:08:02,478
numpy library i'll say import numpy as
12370
08:08:02,478 --> 08:08:04,718
np
12371
08:08:04,718 --> 08:08:07,280
i'm going to
12372
08:08:07,840 --> 08:08:09,840
create a matrix
12373
08:08:09,840 --> 08:08:12,080
that will have random integers so i'm
12374
08:08:12,080 --> 08:08:13,520
using the
12375
08:08:13,520 --> 08:08:16,398
random module and inside that we have
12376
08:08:16,398 --> 08:08:19,280
the random function
12377
08:08:19,280 --> 08:08:21,040
i'll say
12378
08:08:21,040 --> 08:08:23,440
let's say
12379
08:08:23,440 --> 08:08:25,840
10
12380
08:08:25,840 --> 08:08:28,638
so i want the numbers
12381
08:08:28,638 --> 08:08:33,200
within 10 and i'll set the size to
12382
08:08:33,200 --> 08:08:36,000
4 comma 4
12383
08:08:36,000 --> 08:08:37,120
i'll say
12384
08:08:37,120 --> 08:08:39,520
print a now if i run it
12385
08:08:39,520 --> 08:08:42,398
you see here we have our
12386
08:08:42,398 --> 08:08:45,920
randomly generated 4 cross 4 matrix
12387
08:08:45,920 --> 08:08:49,920
where the elements lie within 0 to 10.
12388
08:08:49,920 --> 08:08:52,478
now from this randomly generated matrix
12389
08:08:52,478 --> 08:08:54,080
i am going to find the
12390
08:08:54,080 --> 08:08:55,680
maximum elements
12391
08:08:55,680 --> 08:09:00,080
so i will say max element equal to
12392
08:09:00,080 --> 08:09:02,398
i am going to use the list comprehension
12393
08:09:02,398 --> 08:09:05,040
technique so i say
12394
08:09:05,040 --> 08:09:06,398
max of i
12395
08:09:06,398 --> 08:09:07,680
for
12396
08:09:07,680 --> 08:09:08,878
i in
12397
08:09:08,878 --> 08:09:11,680
my matrix a
12398
08:09:11,680 --> 08:09:15,280
let's print max element
12399
08:09:15,280 --> 08:09:18,080
if i run it you see here the maximum
12400
08:09:18,080 --> 08:09:20,958
element from each of the rows so in the
12401
08:09:20,958 --> 08:09:23,840
first row we have eight as the maximum
12402
08:09:23,840 --> 08:09:26,240
element and then in the second row we
12403
08:09:26,240 --> 08:09:27,440
have
12404
08:09:27,440 --> 08:09:29,840
again eight as the maximum element
12405
08:09:29,840 --> 08:09:31,600
in the third row we have seven as the
12406
08:09:31,600 --> 08:09:33,680
maximum element and in the fourth row we
12407
08:09:33,680 --> 08:09:35,040
have eight again
12408
08:09:35,040 --> 08:09:38,798
now if i run this again my
12409
08:09:38,958 --> 08:09:42,000
randomly generated matrix changes
12410
08:09:42,000 --> 08:09:45,760
now if i run it you can see 5596
12411
08:09:45,760 --> 08:09:47,840
are now the maximum elements
12412
08:09:47,840 --> 08:09:49,200
cool
12413
08:09:49,200 --> 08:09:50,718
so we are going to look at what is a
12414
08:09:50,718 --> 08:09:52,718
function in python then we will see the
12415
08:09:52,718 --> 08:09:55,280
different types of python functions
12416
08:09:55,280 --> 08:09:56,638
then you will understand the various
12417
08:09:56,638 --> 08:09:58,878
advantages of using python functions and
12418
08:09:58,878 --> 08:10:00,398
finally we will head out to our jupyter
12419
08:10:00,398 --> 08:10:02,558
notebook to understand python functions
12420
08:10:02,558 --> 08:10:05,360
in detail with hands-on demonstrations
12421
08:10:05,360 --> 08:10:06,718
okay
12422
08:10:06,718 --> 08:10:09,360
so what is a function in python
12423
08:10:09,360 --> 08:10:11,040
so a function is a bunch of statements
12424
08:10:11,040 --> 08:10:12,638
that are defined to perform specific
12425
08:10:12,638 --> 08:10:13,840
operations
12426
08:10:13,840 --> 08:10:15,520
a function in python helps to divide
12427
08:10:15,520 --> 08:10:17,360
your program into smaller and modular
12428
08:10:17,360 --> 08:10:19,120
chunks thus allowing you to make
12429
08:10:19,120 --> 08:10:20,878
extensive programs more organized and
12430
08:10:20,878 --> 08:10:23,360
manageable it also avoids repetition and
12431
08:10:23,360 --> 08:10:25,680
makes the code reusable
12432
08:10:25,680 --> 08:10:27,760
so let's understand the types of python
12433
08:10:27,760 --> 08:10:29,600
functions first we have
12434
08:10:29,600 --> 08:10:31,520
built-in functions so these are the
12435
08:10:31,520 --> 08:10:34,160
functions that are built into python
12436
08:10:34,160 --> 08:10:36,638
so by default the inbuilt functions are
12437
08:10:36,638 --> 08:10:39,280
available in python then we have user
12438
08:10:39,280 --> 08:10:41,280
defined functions so functions defined
12439
08:10:41,280 --> 08:10:43,280
by users to carry out certain tasks are
12440
08:10:43,280 --> 08:10:45,680
called user defined functions they don't
12441
08:10:45,680 --> 08:10:47,360
have a fixed name
12442
08:10:47,360 --> 08:10:49,840
so here on this slide you can see the
12443
08:10:49,840 --> 08:10:51,680
different built-in python functions that
12444
08:10:51,680 --> 08:10:54,718
are available so you have power length
12445
08:10:54,718 --> 08:10:58,558
next max absolute we have print range
12446
08:10:58,558 --> 08:11:01,520
slice sorted round and there are many
12447
08:11:01,520 --> 08:11:03,440
more so these we will explore in our
12448
08:11:03,440 --> 08:11:04,958
demo session
12449
08:11:04,958 --> 08:11:07,520
so wait for a while
12450
08:11:07,520 --> 08:11:08,558
now
12451
08:11:08,558 --> 08:11:10,558
let's understand about the user defined
12452
08:11:10,558 --> 08:11:12,320
functions so these functions are defined
12453
08:11:12,320 --> 08:11:16,000
by users to perform specific tasks
12454
08:11:16,000 --> 08:11:16,878
and
12455
08:11:16,878 --> 08:11:19,040
these are the steps that you need
12456
08:11:19,040 --> 08:11:21,120
to create a user-defined function so
12457
08:11:21,120 --> 08:11:22,878
first you need to create the function
12458
08:11:22,878 --> 08:11:25,280
and then call the function
12459
08:11:25,280 --> 08:11:26,478
now moving to the
12460
08:11:26,478 --> 08:11:28,638
next slide so here you can see the
12461
08:11:28,638 --> 08:11:30,320
syntax of writing a user defined
12462
08:11:30,320 --> 08:11:32,558
function so we use the def keyword to
12463
08:11:32,558 --> 08:11:34,478
define the function followed by the
12464
08:11:34,478 --> 08:11:36,478
function name and within brackets you
12465
08:11:36,478 --> 08:11:38,000
have the parameters or the arguments of
12466
08:11:38,000 --> 08:11:39,040
the function
12467
08:11:39,040 --> 08:11:41,040
then you have the function block
12468
08:11:41,040 --> 08:11:42,958
and if you want to return a value from
12469
08:11:42,958 --> 08:11:44,478
the function you can use the return
12470
08:11:44,478 --> 08:11:46,878
function followed by the expression
12471
08:11:46,878 --> 08:11:49,360
so here is an example where we are just
12472
08:11:49,360 --> 08:11:51,360
printing hello python using a user
12473
08:11:51,360 --> 08:11:53,840
defined function and in this case my
12474
08:11:53,840 --> 08:11:55,920
function name is called my underscore
12475
08:11:55,920 --> 08:11:58,160
function if i call my underscore
12476
08:11:58,160 --> 08:12:01,520
function it will return me hello python
12477
08:12:01,520 --> 08:12:03,360
cool
12478
08:12:03,360 --> 08:12:04,160
okay
12479
08:12:04,160 --> 08:12:05,840
now let's understand the advantages of
12480
08:12:05,840 --> 08:12:07,120
using
12481
08:12:07,120 --> 08:12:08,878
python functions
12482
08:12:08,878 --> 08:12:10,638
so with the help of functions you can
12483
08:12:10,638 --> 08:12:12,798
avoid rewriting the same logic or code
12484
08:12:12,798 --> 08:12:14,558
again and again in a program
12485
08:12:14,558 --> 08:12:16,478
so in python you can call functions
12486
08:12:16,478 --> 08:12:18,080
anywhere multiple times in a single
12487
08:12:18,080 --> 08:12:19,280
program
12488
08:12:19,280 --> 08:12:21,200
you can also track an extensive python
12489
08:12:21,200 --> 08:12:23,680
program easily when it is divided into
12490
08:12:23,680 --> 08:12:25,520
multiple functions
12491
08:12:25,520 --> 08:12:27,120
and the most crucial feature or the
12492
08:12:27,120 --> 08:12:29,520
advantages of python functions is that
12493
08:12:29,520 --> 08:12:33,120
of its reusability
12494
08:12:33,120 --> 08:12:35,520
cool now let's head over to our jupyter
12495
08:12:35,520 --> 08:12:37,520
notebook and we'll explore the inbuilt
12496
08:12:37,520 --> 08:12:41,040
and user defined functions in detail
12497
08:12:41,120 --> 08:12:43,600
okay so i'm on my jupyter notebook first
12498
08:12:43,600 --> 08:12:44,958
i'll go ahead and
12499
08:12:44,958 --> 08:12:46,398
rename my
12500
08:12:46,398 --> 08:12:49,440
notebook as functions in
12501
08:12:49,440 --> 08:12:51,760
python
12502
08:12:51,760 --> 08:12:53,520
i click on rename
12503
08:12:53,520 --> 08:12:55,680
okay
12504
08:12:55,680 --> 08:12:58,080
first of all let me just increase the
12505
08:12:58,080 --> 08:12:59,280
size
12506
08:12:59,280 --> 08:13:00,798
okay
12507
08:13:00,798 --> 08:13:02,840
i'll give my comment
12508
08:13:02,840 --> 08:13:06,878
as built-in functions
12509
08:13:07,680 --> 08:13:09,920
okay
12510
08:13:10,398 --> 08:13:12,478
so we'll start by looking at the
12511
08:13:12,478 --> 08:13:14,558
absolute function in python
12512
08:13:14,558 --> 08:13:16,798
so absolute or abs function returns the
12513
08:13:16,798 --> 08:13:19,680
absolute value of a number
12514
08:13:19,680 --> 08:13:23,680
so i'll say let's say e equal to
12515
08:13:23,680 --> 08:13:25,680
i'll use the absolute
12516
08:13:25,680 --> 08:13:28,398
function which is represented as ebs in
12517
08:13:28,398 --> 08:13:29,360
python
12518
08:13:29,360 --> 08:13:30,798
suppose i write
12519
08:13:30,798 --> 08:13:34,080
abs of let's say 10.75
12520
08:13:34,080 --> 08:13:35,680
and if i print a
12521
08:13:35,680 --> 08:13:39,200
it gives me 10.75 which is fair enough
12522
08:13:39,200 --> 08:13:41,200
let me just copy this code i'll paste it
12523
08:13:41,200 --> 08:13:42,478
here
12524
08:13:42,478 --> 08:13:45,200
i'll say b equal to aps
12525
08:13:45,200 --> 08:13:47,920
this time i am going to use the
12526
08:13:47,920 --> 08:13:49,600
negative symbol or the negative sign
12527
08:13:49,600 --> 08:13:54,000
i'll say minus 10.75 and will print b
12528
08:13:54,000 --> 08:13:56,558
let me run it you can see here
12529
08:13:56,558 --> 08:13:58,958
python has automatically removed
12530
08:13:58,958 --> 08:14:02,000
the negative sign and it has just given
12531
08:14:02,000 --> 08:14:04,958
us 10.75 so this is how the absolute
12532
08:14:04,958 --> 08:14:05,920
function
12533
08:14:05,920 --> 08:14:08,478
works so abs returns the absolute value
12534
08:14:08,478 --> 08:14:12,000
of a number which is always positive
12535
08:14:12,000 --> 08:14:14,320
now let's see another inbuilt function
12536
08:14:14,320 --> 08:14:15,920
very popular which is called the length
12537
08:14:15,920 --> 08:14:19,120
function or len i just give a comment
12538
08:14:19,120 --> 08:14:20,638
saying len
12539
08:14:20,638 --> 08:14:23,440
above also let me just give a comment as
12540
08:14:23,440 --> 08:14:25,360
aps
12541
08:14:25,360 --> 08:14:26,240
okay
12542
08:14:26,240 --> 08:14:29,040
so later we can have a track of whatever
12543
08:14:29,040 --> 08:14:31,920
functions we have learnt in the demo
12544
08:14:31,920 --> 08:14:34,000
so the length function returns the
12545
08:14:34,000 --> 08:14:35,920
length of an object it could be an i
12546
08:14:35,920 --> 08:14:38,638
table such as a list it could be a tuple
12547
08:14:38,638 --> 08:14:41,200
or even a dictionary let me show you how
12548
08:14:41,200 --> 08:14:42,958
it works i'll say
12549
08:14:42,958 --> 08:14:44,240
list 1
12550
08:14:44,240 --> 08:14:45,680
equal to
12551
08:14:45,680 --> 08:14:47,360
i'll create a list within square
12552
08:14:47,360 --> 08:14:49,440
brackets let's say
12553
08:14:49,440 --> 08:14:51,520
i have
12554
08:14:51,520 --> 08:14:53,280
a few letters
12555
08:14:53,280 --> 08:14:54,878
let's say
12556
08:14:54,878 --> 08:14:57,280
i n then i have
12557
08:14:57,280 --> 08:14:59,360
d
12558
08:14:59,760 --> 08:15:01,760
i again
12559
08:15:01,760 --> 08:15:03,200
and a
12560
08:15:03,200 --> 08:15:04,558
so i have
12561
08:15:04,558 --> 08:15:07,280
all the letters in my word india now if
12562
08:15:07,280 --> 08:15:10,558
i say x equal to
12563
08:15:10,558 --> 08:15:13,760
ln of list one
12564
08:15:13,760 --> 08:15:15,680
then i'll print x
12565
08:15:15,680 --> 08:15:18,318
it gives me five because the length of
12566
08:15:18,318 --> 08:15:21,440
all the characters in my
12567
08:15:21,440 --> 08:15:22,318
list
12568
08:15:22,318 --> 08:15:26,080
is 5 you can count here i and dia
12569
08:15:26,080 --> 08:15:28,398
so there are only 5 elements and hence
12570
08:15:28,398 --> 08:15:30,558
it is written as 5.
12571
08:15:30,558 --> 08:15:32,478
now we'll explore another function which
12572
08:15:32,478 --> 08:15:34,798
is called as all
12573
08:15:34,798 --> 08:15:37,280
so the all function returns true if any
12574
08:15:37,280 --> 08:15:41,360
item in an iterable object is true
12575
08:15:42,958 --> 08:15:44,958
so i'll say
12576
08:15:44,958 --> 08:15:47,120
my underscore list
12577
08:15:47,120 --> 08:15:48,160
equal to
12578
08:15:48,160 --> 08:15:49,520
let's say i have
12579
08:15:49,520 --> 08:15:50,558
0
12580
08:15:50,558 --> 08:15:53,840
1 and 0
12581
08:15:54,080 --> 08:15:55,760
i'll say x equal to
12582
08:15:55,760 --> 08:15:58,000
then i'll use the all function
12583
08:15:58,000 --> 08:15:59,600
and inside the function i'll pass in my
12584
08:15:59,600 --> 08:16:02,000
list which is my underscore list
12585
08:16:02,000 --> 08:16:04,958
and will print x
12586
08:16:05,200 --> 08:16:07,440
hold on for a second and can you please
12587
08:16:07,440 --> 08:16:09,600
guess what this
12588
08:16:09,600 --> 08:16:12,160
x will be
12589
08:16:12,160 --> 08:16:15,120
it gives us false the reason being
12590
08:16:15,120 --> 08:16:17,040
we have
12591
08:16:17,040 --> 08:16:20,878
two false values 0 and 0 here and there
12592
08:16:20,878 --> 08:16:22,798
is only one true value which is 1
12593
08:16:22,798 --> 08:16:25,200
suppose i had 1 1 and 1
12594
08:16:25,200 --> 08:16:26,718
if i run this it would have given us
12595
08:16:26,718 --> 08:16:28,478
true because all the elements in my list
12596
08:16:28,478 --> 08:16:30,718
or the i triple are true
12597
08:16:30,718 --> 08:16:32,958
if any one of the elements is false it
12598
08:16:32,958 --> 08:16:35,680
will give us false
12599
08:16:35,680 --> 08:16:37,440
okay
12600
08:16:37,440 --> 08:16:40,318
now moving to our next function which is
12601
08:16:40,318 --> 08:16:41,680
called sum
12602
08:16:41,680 --> 08:16:43,840
so python sum function is used to get
12603
08:16:43,840 --> 08:16:47,040
the sum of numbers of an i table
12604
08:16:47,040 --> 08:16:49,840
let's say i have
12605
08:16:50,080 --> 08:16:51,760
s equal to
12606
08:16:51,760 --> 08:16:54,160
i'm using the function sum
12607
08:16:54,160 --> 08:16:55,920
and inside the sum function i'm going to
12608
08:16:55,920 --> 08:16:58,000
declare a list of numbers let's say we
12609
08:16:58,000 --> 08:16:59,280
have 20
12610
08:16:59,280 --> 08:17:01,200
35 and let's say
12611
08:17:01,200 --> 08:17:03,840
70
12612
08:17:04,000 --> 08:17:06,398
let me create another
12613
08:17:06,398 --> 08:17:07,760
list called
12614
08:17:07,760 --> 08:17:09,680
s1
12615
08:17:09,680 --> 08:17:12,318
i'll say sum
12616
08:17:12,318 --> 08:17:13,920
and now
12617
08:17:13,920 --> 08:17:16,240
let's say we'll pass in a tuple now so
12618
08:17:16,240 --> 08:17:17,760
i'm using another
12619
08:17:17,760 --> 08:17:18,958
bracket
12620
08:17:18,958 --> 08:17:20,240
we have let's say
12621
08:17:20,240 --> 08:17:22,080
7 5 and 9
12622
08:17:22,080 --> 08:17:23,360
now if i write
12623
08:17:23,360 --> 08:17:24,638
print
12624
08:17:24,638 --> 08:17:25,360
s
12625
08:17:25,360 --> 08:17:28,000
and let's say i want to print s1
12626
08:17:28,000 --> 08:17:29,360
you can see here
12627
08:17:29,360 --> 08:17:31,120
my sum function has
12628
08:17:31,120 --> 08:17:33,040
returned the sum of all the numbers in
12629
08:17:33,040 --> 08:17:34,958
the i treble the first one was a list
12630
08:17:34,958 --> 08:17:38,000
and next we have a tuple so
12631
08:17:38,000 --> 08:17:41,440
20 plus 35 plus 70 is 125 and then
12632
08:17:41,440 --> 08:17:44,160
7 plus 5 plus 9 is 21 so this is how the
12633
08:17:44,160 --> 08:17:46,638
sum function works
12634
08:17:46,638 --> 08:17:48,958
moving ahead we have the
12635
08:17:48,958 --> 08:17:51,680
max function next
12636
08:17:51,680 --> 08:17:54,638
as well as the main function
12637
08:17:54,638 --> 08:17:57,040
so as the name suggests the max function
12638
08:17:57,040 --> 08:17:59,120
will return the maximum number of an
12639
08:17:59,120 --> 08:18:01,680
iterable
12640
08:18:01,840 --> 08:18:05,040
so i'll create a variable called m1
12641
08:18:05,040 --> 08:18:07,360
and i'll use the max function
12642
08:18:07,360 --> 08:18:11,040
let's say we have a few values 7 10
12643
08:18:11,040 --> 08:18:14,160
3 12 61 and
12644
08:18:14,160 --> 08:18:16,718
40.
12645
08:18:17,360 --> 08:18:20,958
now let's go ahead and print m1
12646
08:18:20,958 --> 08:18:23,040
now if i run it
12647
08:18:23,040 --> 08:18:25,120
it returns me the maximum number that is
12648
08:18:25,120 --> 08:18:27,760
present inside my list of numbers or the
12649
08:18:27,760 --> 08:18:29,840
tuple that i have passed in
12650
08:18:29,840 --> 08:18:33,360
here you have the maximum number as 61.
12651
08:18:33,360 --> 08:18:36,160
now let's use the max function over a
12652
08:18:36,160 --> 08:18:37,760
dictionary so i'll create another
12653
08:18:37,760 --> 08:18:40,638
variable called m2 and i'll say
12654
08:18:40,638 --> 08:18:41,920
max
12655
08:18:41,920 --> 08:18:45,200
and inside the max function i'm going to
12656
08:18:45,200 --> 08:18:46,798
pass in my values in the form of a
12657
08:18:46,798 --> 08:18:48,878
dictionary so i'm using curly brackets
12658
08:18:48,878 --> 08:18:50,160
or braces
12659
08:18:50,160 --> 08:18:52,638
i'll say let's say 7
12660
08:18:52,638 --> 08:18:54,718
90 we have 3
12661
08:18:54,718 --> 08:18:57,120
let's say 150
12662
08:18:57,120 --> 08:19:01,040
67 and 44.
12663
08:19:01,040 --> 08:19:03,840
now let me go ahead and print m2
12664
08:19:03,840 --> 08:19:05,760
if i run it you can see it here it has
12665
08:19:05,760 --> 08:19:08,000
given us 50 which is the highest element
12666
08:19:08,000 --> 08:19:09,680
in my
12667
08:19:09,680 --> 08:19:11,200
dictionary
12668
08:19:11,200 --> 08:19:12,398
okay
12669
08:19:12,398 --> 08:19:14,718
now similarly you can use the
12670
08:19:14,718 --> 08:19:18,638
main function let me just copy this
12671
08:19:18,718 --> 08:19:21,120
and i'll paste it here
12672
08:19:21,120 --> 08:19:22,718
i'll say m3
12673
08:19:22,718 --> 08:19:24,398
instead of max function we are going to
12674
08:19:24,398 --> 08:19:26,318
use the main function so the mean
12675
08:19:26,318 --> 08:19:28,000
function will return the
12676
08:19:28,000 --> 08:19:30,638
minimum number in an i table
12677
08:19:30,638 --> 08:19:33,600
let's say i'll pass in
12678
08:19:33,600 --> 08:19:35,440
m3 as a
12679
08:19:35,440 --> 08:19:38,878
list this time and if i say print m3
12680
08:19:38,878 --> 08:19:40,958
it gives me three which is the lowest
12681
08:19:40,958 --> 08:19:43,920
number of the lowest value in my list
12682
08:19:43,920 --> 08:19:46,000
and similarly you can
12683
08:19:46,000 --> 08:19:49,280
have the lowest value printed from a
12684
08:19:49,280 --> 08:19:51,280
dictionary i'll just
12685
08:19:51,280 --> 08:19:53,600
change the variable to m4
12686
08:19:53,600 --> 08:19:55,840
and instead of max will use
12687
08:19:55,840 --> 08:19:58,240
the min function
12688
08:19:58,240 --> 08:20:00,638
and here also i'll use the
12689
08:20:00,638 --> 08:20:02,558
variable as m4
12690
08:20:02,558 --> 08:20:04,958
if i run it you can see here
12691
08:20:04,958 --> 08:20:06,638
we have printed the
12692
08:20:06,638 --> 08:20:09,360
minimum value in my dictionary which is
12693
08:20:09,360 --> 08:20:10,718
three
12694
08:20:10,718 --> 08:20:11,600
okay
12695
08:20:11,600 --> 08:20:13,200
now moving ahead
12696
08:20:13,200 --> 08:20:16,840
we'll explore another function called
12697
08:20:16,840 --> 08:20:18,478
iter
12698
08:20:18,478 --> 08:20:20,478
so python item function returns an
12699
08:20:20,478 --> 08:20:22,958
iterator for the given object so the
12700
08:20:22,958 --> 08:20:24,798
iter function creates an object which
12701
08:20:24,798 --> 08:20:27,680
can be iterated one element at a time
12702
08:20:27,680 --> 08:20:29,920
and the next function returns the next
12703
08:20:29,920 --> 08:20:33,040
item in an iterator let's use both iter
12704
08:20:33,040 --> 08:20:35,840
and next function in our next example
12705
08:20:35,840 --> 08:20:39,360
so i'll create a list called my list
12706
08:20:39,360 --> 08:20:40,638
and
12707
08:20:40,638 --> 08:20:42,718
is equal to
12708
08:20:42,718 --> 08:20:44,878
followed by using the iter function and
12709
08:20:44,878 --> 08:20:46,318
inside the item function i am going to
12710
08:20:46,318 --> 08:20:47,680
pass in a
12711
08:20:47,680 --> 08:20:49,680
list which will have
12712
08:20:49,680 --> 08:20:53,120
letters of india
12713
08:20:53,200 --> 08:20:55,200
i have n
12714
08:20:55,200 --> 08:20:57,040
d
12715
08:20:57,040 --> 08:20:57,920
i
12716
08:20:57,920 --> 08:21:00,798
and then i have a
12717
08:21:00,798 --> 08:21:01,600
now
12718
08:21:01,600 --> 08:21:04,798
i'll create another variable called x
12719
08:21:04,798 --> 08:21:07,920
and then i'll pass in the next function
12720
08:21:07,920 --> 08:21:10,160
and inside the next function i'll pass
12721
08:21:10,160 --> 08:21:11,040
in
12722
08:21:11,040 --> 08:21:13,280
my list and
12723
08:21:13,280 --> 08:21:15,600
we'll go ahead and print x
12724
08:21:15,600 --> 08:21:16,558
so
12725
08:21:16,558 --> 08:21:18,080
it will print the
12726
08:21:18,080 --> 08:21:19,120
first
12727
08:21:19,120 --> 08:21:20,718
element in my
12728
08:21:20,718 --> 08:21:23,120
iterable that is i here
12729
08:21:23,120 --> 08:21:25,280
if i copy this
12730
08:21:25,280 --> 08:21:26,558
and i'll
12731
08:21:26,558 --> 08:21:29,040
paste it here if i run it again
12732
08:21:29,040 --> 08:21:31,360
you can see it here the iter function
12733
08:21:31,360 --> 08:21:32,958
returns the
12734
08:21:32,958 --> 08:21:36,080
second i table which is n in my list i n
12735
08:21:36,080 --> 08:21:37,440
d i a
12736
08:21:37,440 --> 08:21:39,920
i'll paste it again if i run it
12737
08:21:39,920 --> 08:21:41,440
we have the
12738
08:21:41,440 --> 08:21:44,958
third item in the list which is d
12739
08:21:44,958 --> 08:21:47,040
okay
12740
08:21:47,040 --> 08:21:49,440
now coming to using the
12741
08:21:49,440 --> 08:21:52,160
power function in python so python pow
12742
08:21:52,160 --> 08:21:54,318
the power function returns the power of
12743
08:21:54,318 --> 08:21:55,840
the given numbers
12744
08:21:55,840 --> 08:22:00,080
let's say x raised to the power 3
12745
08:22:00,080 --> 08:22:01,920
so i'll
12746
08:22:01,920 --> 08:22:05,920
declare a variable x1 and say pow
12747
08:22:05,920 --> 08:22:07,600
let's say i want
12748
08:22:07,600 --> 08:22:11,760
the value of 3 raised to the power 3
12749
08:22:11,760 --> 08:22:13,760
and let's say i have another variable
12750
08:22:13,760 --> 08:22:15,200
called x2
12751
08:22:15,200 --> 08:22:17,360
i'll use the pow function and this time
12752
08:22:17,360 --> 08:22:19,280
i want to know what is 2
12753
08:22:19,280 --> 08:22:20,878
raised to power
12754
08:22:20,878 --> 08:22:22,478
4.
12755
08:22:22,478 --> 08:22:26,398
we'll go ahead and print x1 and x2
12756
08:22:27,280 --> 08:22:29,120
and as you guessed it 3 raised to the
12757
08:22:29,120 --> 08:22:31,920
power 3 or 3 cube is 27 and 2 raised to
12758
08:22:31,920 --> 08:22:34,718
the power 4 is 16.
12759
08:22:34,718 --> 08:22:36,240
cool
12760
08:22:36,240 --> 08:22:40,638
now coming to using the range function
12761
08:22:40,638 --> 08:22:42,958
so the python range function returns a
12762
08:22:42,958 --> 08:22:45,120
sequence of numbers starting from 0 and
12763
08:22:45,120 --> 08:22:47,520
increments by 1 so this is by default
12764
08:22:47,520 --> 08:22:49,600
and stops before the
12765
08:22:49,600 --> 08:22:50,958
given number
12766
08:22:50,958 --> 08:22:52,558
so let me show you
12767
08:22:52,558 --> 08:22:54,958
if i say x equal to i'll use the range
12768
08:22:54,958 --> 08:22:56,878
function and inside the range function
12769
08:22:56,878 --> 08:22:59,040
i'll have my value as 6
12770
08:22:59,040 --> 08:23:02,798
then i'll say for n
12771
08:23:02,798 --> 08:23:06,558
now here n is my iterator variable
12772
08:23:06,558 --> 08:23:09,120
i'll say for n in x
12773
08:23:09,120 --> 08:23:10,240
print
12774
08:23:10,240 --> 08:23:12,398
n
12775
08:23:12,798 --> 08:23:15,520
let's run it and we'll see the result
12776
08:23:15,520 --> 08:23:18,240
you can see here the range function has
12777
08:23:18,240 --> 08:23:21,280
returned us the numbers starting from
12778
08:23:21,280 --> 08:23:23,680
zero and it has gradually incremented
12779
08:23:23,680 --> 08:23:24,638
the
12780
08:23:24,638 --> 08:23:26,958
iterator by one so we have from zero
12781
08:23:26,958 --> 08:23:29,040
till five
12782
08:23:29,040 --> 08:23:33,318
now i'll show you another example
12783
08:23:34,318 --> 08:23:36,558
i want to change
12784
08:23:36,558 --> 08:23:38,638
the default starting value that is 0 so
12785
08:23:38,638 --> 08:23:39,840
i will save
12786
08:23:39,840 --> 08:23:41,280
x equal to
12787
08:23:41,280 --> 08:23:44,240
range i'm going to start from 40
12788
08:23:44,240 --> 08:23:46,000
till 50
12789
08:23:46,000 --> 08:23:48,478
with a step size of 2. so you can pass
12790
08:23:48,478 --> 08:23:50,638
it as the third argument so it will skip
12791
08:23:50,638 --> 08:23:53,440
one value and print the next value so
12792
08:23:53,440 --> 08:23:55,600
then i'll say for n
12793
08:23:55,600 --> 08:23:56,718
in
12794
08:23:56,718 --> 08:23:59,040
x i'll give a colon
12795
08:23:59,040 --> 08:24:00,558
let's print n
12796
08:24:00,558 --> 08:24:02,000
and we'll see the result you can see
12797
08:24:02,000 --> 08:24:03,360
here it starts
12798
08:24:03,360 --> 08:24:04,798
with 40
12799
08:24:04,798 --> 08:24:08,318
and has a step size of 2 so we have 40
12800
08:24:08,318 --> 08:24:10,000
42 44
12801
08:24:10,000 --> 08:24:13,360
46 and 48 cool
12802
08:24:13,360 --> 08:24:15,600
now let's move ahead and we'll explore
12803
08:24:15,600 --> 08:24:16,718
the
12804
08:24:16,718 --> 08:24:18,798
round function in python
12805
08:24:18,798 --> 08:24:20,478
so the round function rounds off a
12806
08:24:20,478 --> 08:24:23,280
number to the given number of digits
12807
08:24:23,280 --> 08:24:27,360
let's say i have x equal to i'll use the
12808
08:24:27,360 --> 08:24:29,120
round function
12809
08:24:29,120 --> 08:24:31,600
and then i'll have a decimal value let's
12810
08:24:31,600 --> 08:24:33,280
say
12811
08:24:33,280 --> 08:24:35,360
eight point six
12812
08:24:35,360 --> 08:24:36,240
two
12813
08:24:36,240 --> 08:24:37,760
four
12814
08:24:37,760 --> 08:24:40,000
seven five
12815
08:24:40,000 --> 08:24:42,080
and i want to round it to two decimal
12816
08:24:42,080 --> 08:24:43,600
places
12817
08:24:43,600 --> 08:24:46,080
let's print x
12818
08:24:46,080 --> 08:24:48,798
if i run it you can see here my
12819
08:24:48,798 --> 08:24:51,440
python has returned a rounded up value
12820
08:24:51,440 --> 08:24:53,958
to two decimal places so we have
12821
08:24:53,958 --> 08:24:59,478
8.62 and the rest it has just skipped
12822
08:25:00,160 --> 08:25:02,638
moving ahead we'll explore the
12823
08:25:02,638 --> 08:25:05,200
sorted function in python so the sorted
12824
08:25:05,200 --> 08:25:07,200
function returns a sorted list of the
12825
08:25:07,200 --> 08:25:09,200
specified iterable object you can
12826
08:25:09,200 --> 08:25:12,638
specify ascending or descending order
12827
08:25:12,638 --> 08:25:15,280
so i'll create a list called a
12828
08:25:15,280 --> 08:25:18,080
and we have
12829
08:25:18,638 --> 08:25:21,600
the elements let's say again india so i
12830
08:25:21,600 --> 08:25:23,040
have i
12831
08:25:23,040 --> 08:25:24,840
n
12832
08:25:24,840 --> 08:25:27,280
d i
12833
08:25:27,280 --> 08:25:29,680
and i have e again
12834
08:25:29,680 --> 08:25:33,200
if i say x equal to
12835
08:25:33,200 --> 08:25:36,080
sorted a and
12836
08:25:36,080 --> 08:25:38,080
if i print x
12837
08:25:38,080 --> 08:25:40,160
you can see it here python has sorted
12838
08:25:40,160 --> 08:25:42,878
the elements in the list in
12839
08:25:42,878 --> 08:25:44,558
ascending order of the
12840
08:25:44,558 --> 08:25:46,318
letters so we have a appearing first
12841
08:25:46,318 --> 08:25:48,638
then d again i and i and then finally we
12842
08:25:48,638 --> 08:25:49,920
have n
12843
08:25:49,920 --> 08:25:50,798
now
12844
08:25:50,798 --> 08:25:52,878
suppose you wanted to print the letters
12845
08:25:52,878 --> 08:25:54,398
in descending order
12846
08:25:54,398 --> 08:25:56,718
you can use another argument here i'll
12847
08:25:56,718 --> 08:25:59,680
give a comma and say
12848
08:25:59,680 --> 08:26:01,280
reverse
12849
08:26:01,280 --> 08:26:02,478
equal to
12850
08:26:02,478 --> 08:26:04,558
true
12851
08:26:04,558 --> 08:26:06,080
so this
12852
08:26:06,080 --> 08:26:09,040
line will ensure
12853
08:26:09,040 --> 08:26:11,520
my list is sorted in descending order
12854
08:26:11,520 --> 08:26:13,600
you can see here it has just reversed
12855
08:26:13,600 --> 08:26:16,160
the above result so we have n appearing
12856
08:26:16,160 --> 08:26:19,920
first and i i d and e
12857
08:26:20,160 --> 08:26:22,000
you can do the same for a list of
12858
08:26:22,000 --> 08:26:24,718
numbers let's say we have 12
12859
08:26:24,718 --> 08:26:26,478
50
12860
08:26:26,478 --> 08:26:28,240
38
12861
08:26:28,240 --> 08:26:30,000
76
12862
08:26:30,000 --> 08:26:32,080
let's say 27
12863
08:26:32,080 --> 08:26:34,878
80 and 5.
12864
08:26:34,878 --> 08:26:37,760
i'll say x equal to
12865
08:26:37,760 --> 08:26:39,280
sorted a
12866
08:26:39,280 --> 08:26:42,398
and will print x
12867
08:26:42,398 --> 08:26:45,440
if i run it you see it here we have
12868
08:26:45,440 --> 08:26:46,878
sorted the numbers in the list in
12869
08:26:46,878 --> 08:26:48,398
ascending order now
12870
08:26:48,398 --> 08:26:51,040
if you want to sort it in descending
12871
08:26:51,040 --> 08:26:55,360
order just add reverse equal to true
12872
08:26:55,360 --> 08:26:57,120
there you go here
12873
08:26:57,120 --> 08:26:58,718
my list has now been sorted in
12874
08:26:58,718 --> 08:27:02,000
descending order of elements
12875
08:27:02,718 --> 08:27:05,840
now we are going to look at the final
12876
08:27:05,840 --> 08:27:08,000
built-in python function which is
12877
08:27:08,000 --> 08:27:10,318
slice
12878
08:27:10,318 --> 08:27:12,398
so the slice function returns a portion
12879
08:27:12,398 --> 08:27:14,558
of an iterable as an object of the slice
12880
08:27:14,558 --> 08:27:17,840
class based on the specified range
12881
08:27:17,840 --> 08:27:19,840
suppose i have
12882
08:27:19,840 --> 08:27:22,878
a list called a
12883
08:27:24,080 --> 08:27:26,878
okay let's not define a list we'll
12884
08:27:26,878 --> 08:27:30,240
define a tuple
12885
08:27:30,398 --> 08:27:33,920
so this has the letters
12886
08:27:33,920 --> 08:27:37,440
in the word simply learn
12887
08:27:37,440 --> 08:27:39,920
so i have s i m
12888
08:27:39,920 --> 08:27:41,760
p
12889
08:27:41,760 --> 08:27:43,520
l
12890
08:27:43,520 --> 08:27:46,160
i
12891
08:27:46,160 --> 08:27:48,638
again l
12892
08:27:48,798 --> 08:27:51,360
i have e
12893
08:27:51,360 --> 08:27:54,718
comma i have a
12894
08:27:54,718 --> 08:27:58,558
then we have r and finally i have n
12895
08:27:58,558 --> 08:28:00,478
so these are the letters which have been
12896
08:28:00,478 --> 08:28:03,760
represented as a tuple
12897
08:28:04,478 --> 08:28:05,760
simply learn
12898
08:28:05,760 --> 08:28:09,120
then i'll use the slice function
12899
08:28:09,120 --> 08:28:10,478
and
12900
08:28:10,478 --> 08:28:16,160
i'll give my range as 5 and will print
12901
08:28:16,160 --> 08:28:18,478
a of x
12902
08:28:18,478 --> 08:28:21,280
let's run it you see it here
12903
08:28:21,280 --> 08:28:24,160
the slice function has sliced the
12904
08:28:24,160 --> 08:28:25,120
first
12905
08:28:25,120 --> 08:28:27,920
five elements in my tuple so we have s i
12906
08:28:27,920 --> 08:28:30,160
m p and l
12907
08:28:30,160 --> 08:28:31,280
cool
12908
08:28:31,280 --> 08:28:33,440
so we have explored
12909
08:28:33,440 --> 08:28:35,520
all the important
12910
08:28:35,520 --> 08:28:37,440
built-in python functions
12911
08:28:37,440 --> 08:28:41,718
now let's head over to using
12912
08:28:42,160 --> 08:28:43,520
defined functions or learning user
12913
08:28:43,520 --> 08:28:45,760
defined functions in python i'll give a
12914
08:28:45,760 --> 08:28:48,398
comment first user defined functions
12915
08:28:48,398 --> 08:28:50,718
cool
12916
08:28:51,520 --> 08:28:54,638
so as we had learnt in our slides
12917
08:28:54,638 --> 08:28:56,718
to define a user defined function we
12918
08:28:56,718 --> 08:28:59,760
will use the def keyword
12919
08:28:59,760 --> 08:29:02,080
and
12920
08:29:02,798 --> 08:29:05,600
a function name this should be a valid
12921
08:29:05,600 --> 08:29:08,878
python name so i'm using func one
12922
08:29:08,878 --> 08:29:11,600
followed by parentheses and then we'll
12923
08:29:11,600 --> 08:29:13,680
have a colon
12924
08:29:13,680 --> 08:29:16,080
and now we'll define the body of the
12925
08:29:16,080 --> 08:29:17,680
function i'll say
12926
08:29:17,680 --> 08:29:19,200
print
12927
08:29:19,200 --> 08:29:20,478
hello world
12928
08:29:20,478 --> 08:29:22,718
this is the same example that we had
12929
08:29:22,718 --> 08:29:24,398
seen in our
12930
08:29:24,398 --> 08:29:26,878
slide
12931
08:29:26,878 --> 08:29:27,680
now
12932
08:29:27,680 --> 08:29:30,638
i am going to call the function
12933
08:29:30,638 --> 08:29:33,440
using the function name which is
12934
08:29:33,440 --> 08:29:36,318
fu and c1 if i run it you can see it
12935
08:29:36,318 --> 08:29:37,120
here
12936
08:29:37,120 --> 08:29:39,760
i have my message printed hello world
12937
08:29:39,760 --> 08:29:41,360
so this is a very
12938
08:29:41,360 --> 08:29:42,318
simple
12939
08:29:42,318 --> 08:29:43,920
user defined function
12940
08:29:43,920 --> 08:29:45,840
and it does not
12941
08:29:45,840 --> 08:29:50,160
carry any arguments or parameters
12942
08:29:50,160 --> 08:29:52,878
now let me show you another
12943
08:29:52,878 --> 08:29:56,080
user defined function i'll say def
12944
08:29:56,080 --> 08:29:57,680
hello
12945
08:29:57,680 --> 08:30:00,958
again we are not giving any parameter
12946
08:30:00,958 --> 08:30:02,958
to this function or arguments to this
12947
08:30:02,958 --> 08:30:04,080
function
12948
08:30:04,080 --> 08:30:05,280
i'll say
12949
08:30:05,280 --> 08:30:06,958
name equal to
12950
08:30:06,958 --> 08:30:09,440
str this time we are going to take the
12951
08:30:09,440 --> 08:30:11,840
input from the user so i'm using the
12952
08:30:11,840 --> 08:30:14,398
input function i'll say
12953
08:30:14,398 --> 08:30:17,040
enter your
12954
08:30:17,040 --> 08:30:20,520
country name
12955
08:30:24,240 --> 08:30:25,680
give a
12956
08:30:25,680 --> 08:30:28,318
colon and a space
12957
08:30:28,318 --> 08:30:30,718
and then we'll say
12958
08:30:30,718 --> 08:30:31,840
print
12959
08:30:31,840 --> 08:30:33,600
i'll print the message
12960
08:30:33,600 --> 08:30:35,120
hello
12961
08:30:35,120 --> 08:30:37,120
followed by a space
12962
08:30:37,120 --> 08:30:38,798
give a plus sign
12963
08:30:38,798 --> 08:30:42,398
and we are going to convert our name to
12964
08:30:42,398 --> 08:30:44,318
string
12965
08:30:44,318 --> 08:30:47,280
so this is my function let me run it
12966
08:30:47,280 --> 08:30:50,718
now i'll call hello
12967
08:30:52,240 --> 08:30:54,398
it is asking me for
12968
08:30:54,398 --> 08:30:55,600
entering
12969
08:30:55,600 --> 08:30:59,280
country name i'll say japan
12970
08:30:59,280 --> 08:31:00,160
if i
12971
08:31:00,160 --> 08:31:03,120
hit enter it says hello
12972
08:31:03,120 --> 08:31:04,000
then
12973
08:31:04,000 --> 08:31:06,080
my country name which is japan so hello
12974
08:31:06,080 --> 08:31:07,360
japan
12975
08:31:07,360 --> 08:31:09,440
let me run it again this time i'll say
12976
08:31:09,440 --> 08:31:11,120
usa
12977
08:31:11,120 --> 08:31:12,798
i'll hit enter you can see here my
12978
08:31:12,798 --> 08:31:15,040
message is hello usa
12979
08:31:15,040 --> 08:31:16,638
cool
12980
08:31:16,638 --> 08:31:18,478
now moving ahead to our next example
12981
08:31:18,478 --> 08:31:20,558
where we'll see
12982
08:31:20,558 --> 08:31:22,878
using functions with
12983
08:31:22,878 --> 08:31:25,760
different parameters
12984
08:31:25,760 --> 08:31:27,760
so we'll start with a very simple
12985
08:31:27,760 --> 08:31:29,200
example
12986
08:31:29,200 --> 08:31:30,718
i'll say
12987
08:31:30,718 --> 08:31:31,920
def
12988
08:31:31,920 --> 08:31:34,318
i want to convert
12989
08:31:34,318 --> 08:31:37,600
feet to centimeter
12990
08:31:37,600 --> 08:31:40,240
i'll say my function name as ft
12991
08:31:40,240 --> 08:31:43,120
underscore 2 underscore cm
12992
08:31:43,120 --> 08:31:45,520
so i'll pass in a
12993
08:31:45,520 --> 08:31:47,840
value for feet and that feed will be
12994
08:31:47,840 --> 08:31:50,638
converted to centimeters
12995
08:31:50,638 --> 08:31:52,718
and this time inside parentheses i am
12996
08:31:52,718 --> 08:31:55,440
going to pass in
12997
08:31:55,440 --> 08:31:58,798
my parameter as f t
12998
08:31:58,798 --> 08:32:01,360
i'll use the formula for centimeter i'll
12999
08:32:01,360 --> 08:32:03,040
say
13000
08:32:03,040 --> 08:32:06,638
my variable centimeter equal to
13001
08:32:06,638 --> 08:32:08,000
feet
13002
08:32:08,000 --> 08:32:10,398
multiplied by
13003
08:32:10,398 --> 08:32:13,840
30 point
13004
08:32:15,040 --> 08:32:17,600
so 1 feet is actually
13005
08:32:17,600 --> 08:32:19,200
30.48
13006
08:32:19,200 --> 08:32:21,680
centimeters so centimeter becomes feet
13007
08:32:21,680 --> 08:32:23,760
into 30.48
13008
08:32:23,760 --> 08:32:25,920
then we are going to use the return
13009
08:32:25,920 --> 08:32:28,240
keyword and i am going to return the
13010
08:32:28,240 --> 08:32:29,760
output which is
13011
08:32:29,760 --> 08:32:31,600
centimeter
13012
08:32:31,600 --> 08:32:33,760
let's run it
13013
08:32:33,760 --> 08:32:35,920
now to call the function i'll use my
13014
08:32:35,920 --> 08:32:38,080
function name which is ft underscore 2
13015
08:32:38,080 --> 08:32:39,600
underscore cm
13016
08:32:39,600 --> 08:32:42,638
this is my function name
13017
08:32:42,638 --> 08:32:46,080
and then i'm going to pass in a
13018
08:32:46,080 --> 08:32:48,398
value for feet let's say i want to
13019
08:32:48,398 --> 08:32:50,558
convert six feet
13020
08:32:50,558 --> 08:32:52,798
into centimeters if i run it there you
13021
08:32:52,798 --> 08:32:55,040
go you can see it here
13022
08:32:55,040 --> 08:32:57,478
6 feet is actually
13023
08:32:57,478 --> 08:32:59,760
182.88 centimeters
13024
08:32:59,760 --> 08:33:01,360
let's see
13025
08:33:01,360 --> 08:33:03,920
5.8
13026
08:33:03,920 --> 08:33:05,600
if i run it it is
13027
08:33:05,600 --> 08:33:08,478
176.7
13028
08:33:08,478 --> 08:33:09,920
centimeters
13029
08:33:09,920 --> 08:33:12,638
so here you saw how to pass in arguments
13030
08:33:12,638 --> 08:33:15,200
to a user defined function
13031
08:33:15,200 --> 08:33:17,040
now again you can pass in multiple
13032
08:33:17,040 --> 08:33:18,160
arguments
13033
08:33:18,160 --> 08:33:19,440
let's say
13034
08:33:19,440 --> 08:33:22,000
i'm going to find the sum of two values
13035
08:33:22,000 --> 08:33:23,440
for that i'll create a function called
13036
08:33:23,440 --> 08:33:24,958
tef
13037
08:33:24,958 --> 08:33:27,520
my function name is add i'm going to add
13038
08:33:27,520 --> 08:33:29,680
two values x and y
13039
08:33:29,680 --> 08:33:31,120
so i'm passing in
13040
08:33:31,120 --> 08:33:34,240
two parameters x comma y
13041
08:33:34,240 --> 08:33:35,920
i'll create a variable called sum that
13042
08:33:35,920 --> 08:33:39,760
will store the value of x plus y
13043
08:33:39,760 --> 08:33:43,280
and then let's print sum
13044
08:33:43,280 --> 08:33:46,240
okay let me just scroll down
13045
08:33:46,240 --> 08:33:48,398
i'll hit shift enter to run it okay we
13046
08:33:48,398 --> 08:33:51,520
have created my function
13047
08:33:51,520 --> 08:33:54,160
that is add now let's add
13048
08:33:54,160 --> 08:33:55,360
40
13049
08:33:55,360 --> 08:33:56,398
and
13050
08:33:56,398 --> 08:33:58,318
50.
13051
08:33:58,318 --> 08:33:59,840
you have to pass in two arguments
13052
08:33:59,840 --> 08:34:01,120
because we have
13053
08:34:01,120 --> 08:34:02,318
declared
13054
08:34:02,318 --> 08:34:05,120
x and y in the function so it needs at
13055
08:34:05,120 --> 08:34:07,760
least two arguments if i run it you have
13056
08:34:07,760 --> 08:34:09,280
the sum of
13057
08:34:09,280 --> 08:34:12,240
40 and 50 which is 90.
13058
08:34:12,240 --> 08:34:14,558
let's say i have
13059
08:34:14,558 --> 08:34:16,798
minus 20 and
13060
08:34:16,798 --> 08:34:20,558
30 if i run it you have the result
13061
08:34:20,558 --> 08:34:22,638
minus 20 plus 30 is
13062
08:34:22,638 --> 08:34:24,398
10
13063
08:34:24,398 --> 08:34:26,798
all right
13064
08:34:27,120 --> 08:34:30,080
now similarly you can also multiply two
13065
08:34:30,080 --> 08:34:34,398
numbers let me just copy this
13066
08:34:34,398 --> 08:34:35,840
i'll
13067
08:34:35,840 --> 08:34:37,600
paste it here
13068
08:34:37,600 --> 08:34:40,000
and instead of add
13069
08:34:40,000 --> 08:34:42,160
i'll give my user defined function name
13070
08:34:42,160 --> 08:34:43,280
as
13071
08:34:43,280 --> 08:34:45,200
multiply
13072
08:34:45,200 --> 08:34:46,958
i'll say
13073
08:34:46,958 --> 08:34:49,520
product equal to x
13074
08:34:49,520 --> 08:34:53,520
asterisk y and i'll print
13075
08:34:53,520 --> 08:34:55,680
product let's run it
13076
08:34:55,680 --> 08:34:57,520
i'll call my function using the function
13077
08:34:57,520 --> 08:34:59,360
name multiply let's say
13078
08:34:59,360 --> 08:35:00,638
4
13079
08:35:00,638 --> 08:35:03,360
multiplied by 6
13080
08:35:03,360 --> 08:35:05,840
so the result is 24
13081
08:35:05,840 --> 08:35:07,280
cool
13082
08:35:07,280 --> 08:35:10,160
now moving ahead
13083
08:35:10,160 --> 08:35:11,920
we will explore another example let's
13084
08:35:11,920 --> 08:35:13,680
say i want to print the absolute value
13085
08:35:13,680 --> 08:35:16,000
of a number
13086
08:35:16,000 --> 08:35:18,718
so i am creating a function called def
13087
08:35:18,718 --> 08:35:20,878
my function name is absolute and i'll
13088
08:35:20,878 --> 08:35:22,080
pass in
13089
08:35:22,080 --> 08:35:24,160
one parameter which is x
13090
08:35:24,160 --> 08:35:26,398
i'm going to use
13091
08:35:26,398 --> 08:35:30,398
i felt's statement i'll say if x is
13092
08:35:30,398 --> 08:35:32,558
greater than or equal to 0
13093
08:35:32,558 --> 08:35:34,958
i'll give a colon
13094
08:35:34,958 --> 08:35:38,080
then i'll say return x
13095
08:35:38,080 --> 08:35:39,920
else
13096
08:35:39,920 --> 08:35:40,840
give a
13097
08:35:40,840 --> 08:35:44,718
colon i'll say return
13098
08:35:45,040 --> 08:35:47,280
minus x
13099
08:35:47,280 --> 08:35:49,120
i'll run it
13100
08:35:49,120 --> 08:35:51,520
now if i print
13101
08:35:51,520 --> 08:35:53,840
absolute
13102
08:35:53,840 --> 08:35:55,440
of let's say
13103
08:35:55,440 --> 08:35:56,878
56
13104
08:35:56,878 --> 08:35:59,600
the result is as expected since 56 is
13105
08:35:59,600 --> 08:36:02,958
greater than 0 we have just returned 56
13106
08:36:02,958 --> 08:36:05,120
let me just copy this
13107
08:36:05,120 --> 08:36:07,120
i'll paste it here let's say absolute of
13108
08:36:07,120 --> 08:36:10,000
minus 56 is again 56 because we are
13109
08:36:10,000 --> 08:36:11,040
returning
13110
08:36:11,040 --> 08:36:13,600
minus x so minus of minus 56 becomes
13111
08:36:13,600 --> 08:36:14,958
plus 56
13112
08:36:14,958 --> 08:36:17,680
now if you wanted to return minus 56
13113
08:36:17,680 --> 08:36:19,200
you need to
13114
08:36:19,200 --> 08:36:21,520
make some modifications i'll put another
13115
08:36:21,520 --> 08:36:24,318
minus and keep this within brackets let
13116
08:36:24,318 --> 08:36:26,478
me run the function again
13117
08:36:26,478 --> 08:36:29,120
and now if i print absolute of minus 56
13118
08:36:29,120 --> 08:36:32,080
you have the result as -56
13119
08:36:32,080 --> 08:36:34,398
okay
13120
08:36:34,558 --> 08:36:37,360
now let's see another example how to
13121
08:36:37,360 --> 08:36:40,798
find the factorial of a number using
13122
08:36:40,798 --> 08:36:42,718
a user defined function so we'll define
13123
08:36:42,718 --> 08:36:46,240
our function as def i'll use the
13124
08:36:46,240 --> 08:36:48,958
function name as factorial
13125
08:36:48,958 --> 08:36:52,240
i'm going to pass in a
13126
08:36:52,240 --> 08:36:55,360
parameter called x i'll say if x is
13127
08:36:55,360 --> 08:36:57,600
equal to equal to 0
13128
08:36:57,600 --> 08:37:02,558
then we'll just return x or return 1.
13129
08:37:03,040 --> 08:37:04,080
i'll say
13130
08:37:04,080 --> 08:37:05,200
else
13131
08:37:05,200 --> 08:37:06,718
give a colon
13132
08:37:06,718 --> 08:37:09,120
return
13133
08:37:11,280 --> 08:37:13,600
i'll say x
13134
08:37:13,600 --> 08:37:16,080
asterix or multiplied by i'll use the
13135
08:37:16,080 --> 08:37:19,958
function name factorial
13136
08:37:20,000 --> 08:37:23,120
i'll say x minus 1
13137
08:37:23,120 --> 08:37:25,360
now let's run it
13138
08:37:25,360 --> 08:37:27,360
i'll call my function
13139
08:37:27,360 --> 08:37:29,680
factorial
13140
08:37:29,680 --> 08:37:31,200
let's say i want to find the factorial
13141
08:37:31,200 --> 08:37:34,240
of 5 the result is 120 let's say the
13142
08:37:34,240 --> 08:37:37,200
factorial of 10 the result is
13143
08:37:37,200 --> 08:37:39,360
36 lakhs 28
13144
08:37:39,360 --> 08:37:40,718
800
13145
08:37:40,718 --> 08:37:41,760
cool
13146
08:37:41,760 --> 08:37:44,558
now let's see how you can pass in
13147
08:37:44,558 --> 08:37:46,798
default arguments to your function or
13148
08:37:46,798 --> 08:37:49,840
default parameters
13149
08:37:50,240 --> 08:37:51,440
okay
13150
08:37:51,440 --> 08:37:52,398
suppose
13151
08:37:52,398 --> 08:37:54,080
i have
13152
08:37:54,080 --> 08:37:57,040
a function defined called welcome
13153
08:37:57,040 --> 08:37:59,600
here i am going to pass my argument
13154
08:37:59,600 --> 08:38:00,840
as name
13155
08:38:00,840 --> 08:38:03,680
and in the argument itself i'll pass in
13156
08:38:03,680 --> 08:38:08,080
the value a default value let's say paul
13157
08:38:08,080 --> 08:38:11,040
give a colon and i'll print
13158
08:38:11,040 --> 08:38:12,718
hello
13159
08:38:12,718 --> 08:38:14,398
this would be within
13160
08:38:14,398 --> 08:38:15,680
quotes
13161
08:38:15,680 --> 08:38:16,958
and the
13162
08:38:16,958 --> 08:38:19,040
name
13163
08:38:19,040 --> 08:38:21,280
cool let me just run it
13164
08:38:21,280 --> 08:38:22,878
now i'm going to call the function
13165
08:38:22,878 --> 08:38:25,360
welcome
13166
08:38:27,200 --> 08:38:30,160
this time i'm not giving any value to my
13167
08:38:30,160 --> 08:38:32,478
function if i run it it has
13168
08:38:32,478 --> 08:38:34,398
automatically taken the default argument
13169
08:38:34,398 --> 08:38:36,240
which was paul so my message becomes
13170
08:38:36,240 --> 08:38:38,240
hello paul
13171
08:38:38,240 --> 08:38:40,240
now
13172
08:38:40,240 --> 08:38:41,840
let's say i'll pass in
13173
08:38:41,840 --> 08:38:44,638
a value
13174
08:38:44,638 --> 08:38:46,000
welcome
13175
08:38:46,000 --> 08:38:49,600
and within quotes i'll say c
13176
08:38:49,600 --> 08:38:52,080
now the message becomes hello ac so if
13177
08:38:52,080 --> 08:38:53,600
you pass in
13178
08:38:53,600 --> 08:38:55,840
a value to your
13179
08:38:55,840 --> 08:38:57,360
function welcome
13180
08:38:57,360 --> 08:38:59,280
explicitly it will take that value
13181
08:38:59,280 --> 08:39:02,240
instead of the default argument
13182
08:39:02,240 --> 08:39:04,478
cool
13183
08:39:04,558 --> 08:39:07,120
now you can also have multiple arguments
13184
08:39:07,120 --> 08:39:09,280
in your
13185
08:39:09,280 --> 08:39:12,958
function as we saw above for the
13186
08:39:12,958 --> 08:39:15,360
addition and multiplication
13187
08:39:15,360 --> 08:39:17,280
functions
13188
08:39:17,280 --> 08:39:20,318
i'll create a function called
13189
08:39:20,318 --> 08:39:21,760
message
13190
08:39:21,760 --> 08:39:24,478
and we'll have
13191
08:39:25,120 --> 08:39:27,760
the arguments as name 1 name 2 and name
13192
08:39:27,760 --> 08:39:30,080
3
13193
08:39:30,638 --> 08:39:32,160
give a colon
13194
08:39:32,160 --> 08:39:33,200
i'll say
13195
08:39:33,200 --> 08:39:34,958
print
13196
08:39:34,958 --> 08:39:37,520
a message that is
13197
08:39:37,520 --> 08:39:38,558
hello
13198
08:39:38,558 --> 08:39:41,280
and give space give a comma
13199
08:39:41,280 --> 08:39:43,920
we have name one
13200
08:39:43,920 --> 08:39:47,760
give a comma and then i'll say
13201
08:39:48,160 --> 08:39:49,120
comma
13202
08:39:49,120 --> 08:39:50,718
please
13203
08:39:50,718 --> 08:39:52,558
tell
13204
08:39:52,558 --> 08:39:54,398
give another comma
13205
08:39:54,398 --> 08:39:56,478
i'll have my name 2
13206
08:39:56,478 --> 08:39:58,160
give a comma
13207
08:39:58,160 --> 08:40:01,600
followed by the message that
13208
08:40:01,600 --> 08:40:03,840
then i have name 3
13209
08:40:03,840 --> 08:40:05,440
this should be a comma instead of a
13210
08:40:05,440 --> 08:40:07,840
period
13211
08:40:07,840 --> 08:40:09,680
then i'll see is
13212
08:40:09,680 --> 08:40:12,160
waiting
13213
08:40:13,440 --> 08:40:15,360
make sure this is within quotations
13214
08:40:15,360 --> 08:40:17,360
again
13215
08:40:17,360 --> 08:40:20,318
just keep a exclamation mark
13216
08:40:20,318 --> 08:40:21,360
okay
13217
08:40:21,360 --> 08:40:24,398
so i have my message let's run it
13218
08:40:24,398 --> 08:40:27,120
now i'll call my function
13219
08:40:27,120 --> 08:40:29,600
message
13220
08:40:29,680 --> 08:40:30,638
we need
13221
08:40:30,638 --> 08:40:32,558
three arguments now so i'll pass in
13222
08:40:32,558 --> 08:40:34,958
three names let's say my first name is
13223
08:40:34,958 --> 08:40:40,240
gilly my second name is rachel
13224
08:40:41,120 --> 08:40:45,280
and my third name is let's say shaw
13225
08:40:45,520 --> 08:40:48,478
i'll run it and you can see here we have
13226
08:40:48,478 --> 08:40:49,440
my
13227
08:40:49,440 --> 08:40:51,840
message printed hello followed by name
13228
08:40:51,840 --> 08:40:53,920
one which is kili so hello ghillie
13229
08:40:53,920 --> 08:40:56,478
please tell name to which is rachel so
13230
08:40:56,478 --> 08:40:58,478
please tell rachel
13231
08:40:58,478 --> 08:40:59,360
that
13232
08:40:59,360 --> 08:41:01,840
name three which is shaw that saw is
13233
08:41:01,840 --> 08:41:02,958
waiting
13234
08:41:02,958 --> 08:41:05,840
cool
13235
08:41:06,240 --> 08:41:08,718
now we'll understand about
13236
08:41:08,718 --> 08:41:12,638
scope of variables in a function
13237
08:41:12,638 --> 08:41:15,680
now your scope could be a global scope
13238
08:41:15,680 --> 08:41:19,120
or a local scope let's
13239
08:41:19,120 --> 08:41:20,958
define a function called
13240
08:41:20,958 --> 08:41:23,280
func
13241
08:41:23,600 --> 08:41:25,920
3
13242
08:41:26,798 --> 08:41:28,318
inside the function i am going to create
13243
08:41:28,318 --> 08:41:30,398
a variable called
13244
08:41:30,398 --> 08:41:33,120
z which has a value of 7
13245
08:41:33,120 --> 08:41:35,600
now i'll just print
13246
08:41:35,600 --> 08:41:36,638
z
13247
08:41:36,638 --> 08:41:40,318
and i'll call in my function
13248
08:41:40,318 --> 08:41:42,000
3
13249
08:41:42,000 --> 08:41:44,240
if i run it you have the value printed
13250
08:41:44,240 --> 08:41:48,160
of z that is 7 now if i want to access
13251
08:41:48,160 --> 08:41:50,718
my variable that is z outside the
13252
08:41:50,718 --> 08:41:52,638
function if i run this
13253
08:41:52,638 --> 08:41:54,718
it throws me an error because
13254
08:41:54,718 --> 08:41:55,840
this
13255
08:41:55,840 --> 08:41:58,798
variable z was defined locally that is
13256
08:41:58,798 --> 08:42:01,040
inside the function and you cannot
13257
08:42:01,040 --> 08:42:04,398
access z outside the function
13258
08:42:04,398 --> 08:42:07,200
now to overcome this issue let me just
13259
08:42:07,200 --> 08:42:08,958
copy this
13260
08:42:08,958 --> 08:42:11,280
we are going to declare z
13261
08:42:11,280 --> 08:42:13,120
as a global variable
13262
08:42:13,120 --> 08:42:15,600
so i'll have z outside the function i'll
13263
08:42:15,600 --> 08:42:16,958
say
13264
08:42:16,958 --> 08:42:20,478
z equal to 7 and then i have
13265
08:42:20,478 --> 08:42:22,160
my function defined i'll just change the
13266
08:42:22,160 --> 08:42:24,240
name of the function to
13267
08:42:24,240 --> 08:42:25,680
function 4
13268
08:42:25,680 --> 08:42:27,200
and then we'll
13269
08:42:27,200 --> 08:42:29,920
print z and i'll call the function
13270
08:42:29,920 --> 08:42:31,280
function 4
13271
08:42:31,280 --> 08:42:32,718
if i run it
13272
08:42:32,718 --> 08:42:34,958
you can see it here i have the
13273
08:42:34,958 --> 08:42:38,240
value of z printed that is 7. now if i
13274
08:42:38,240 --> 08:42:41,200
want to access z outside the function
13275
08:42:41,200 --> 08:42:43,040
then also it's fine because we have
13276
08:42:43,040 --> 08:42:44,878
defined z
13277
08:42:44,878 --> 08:42:46,638
outside the function that is
13278
08:42:46,638 --> 08:42:49,360
z is now a global variable so its scope
13279
08:42:49,360 --> 08:42:52,398
is throughout the function
13280
08:42:52,718 --> 08:42:54,398
now another thing to note here is you
13281
08:42:54,398 --> 08:42:56,718
can't change the value from
13282
08:42:56,718 --> 08:42:58,798
inside a local scope
13283
08:42:58,798 --> 08:42:59,520
so
13284
08:42:59,520 --> 08:43:02,558
if i say z equal to 7
13285
08:43:02,558 --> 08:43:05,520
and then i'll define my function f u n c
13286
08:43:05,520 --> 08:43:07,520
let's say 5
13287
08:43:07,520 --> 08:43:10,798
i'll give a colon and i'll say let's see
13288
08:43:10,798 --> 08:43:13,360
i want to alter the value of z to let's
13289
08:43:13,360 --> 08:43:16,080
say z plus 7
13290
08:43:16,080 --> 08:43:17,520
i'll print
13291
08:43:17,520 --> 08:43:19,200
z
13292
08:43:19,200 --> 08:43:21,600
and let me now call
13293
08:43:21,600 --> 08:43:24,000
func5
13294
08:43:24,000 --> 08:43:25,280
if i
13295
08:43:25,280 --> 08:43:26,398
run this
13296
08:43:26,398 --> 08:43:28,398
it throws me an error
13297
08:43:28,398 --> 08:43:30,798
as i said it says local variable z
13298
08:43:30,798 --> 08:43:32,878
referenced before assignment
13299
08:43:32,878 --> 08:43:35,120
so you can't change its value from
13300
08:43:35,120 --> 08:43:37,280
inside a local scope for that you're
13301
08:43:37,280 --> 08:43:39,440
going to explicitly define the variable
13302
08:43:39,440 --> 08:43:42,240
z as global let me show you how to do it
13303
08:43:42,240 --> 08:43:44,318
i've just copied this code and paste it
13304
08:43:44,318 --> 08:43:45,360
here
13305
08:43:45,360 --> 08:43:46,398
i'll say
13306
08:43:46,398 --> 08:43:48,160
z equal to 7
13307
08:43:48,160 --> 08:43:49,840
i'll define my function let's say if you
13308
08:43:49,840 --> 08:43:50,798
nc
13309
08:43:50,798 --> 08:43:52,398
6
13310
08:43:52,398 --> 08:43:55,680
here i'm going to say
13311
08:43:55,680 --> 08:43:57,760
global
13312
08:43:57,760 --> 08:43:59,040
c
13313
08:43:59,040 --> 08:44:01,200
so i've explicitly defined my z as a
13314
08:44:01,200 --> 08:44:02,718
global variable
13315
08:44:02,718 --> 08:44:04,398
now if i
13316
08:44:04,398 --> 08:44:07,280
call the function 6 you can see here we
13317
08:44:07,280 --> 08:44:10,318
have altered the value of z from 7 to z
13318
08:44:10,318 --> 08:44:13,520
plus 1 that is 7 plus 1 which is 8.
13319
08:44:13,520 --> 08:44:15,760
okay
13320
08:44:16,398 --> 08:44:18,798
now asterix arcs in function definitions
13321
08:44:18,798 --> 08:44:20,798
in python are used to pass a variable
13322
08:44:20,798 --> 08:44:22,718
number of arguments to a function
13323
08:44:22,718 --> 08:44:24,558
suppose you don't know the exact number
13324
08:44:24,558 --> 08:44:27,440
of arguments you need so you can use
13325
08:44:27,440 --> 08:44:29,200
the argument with an
13326
08:44:29,200 --> 08:44:31,040
asterisk symbol let me show you how to
13327
08:44:31,040 --> 08:44:32,398
do it
13328
08:44:32,398 --> 08:44:36,000
i'll say def welcome
13329
08:44:36,000 --> 08:44:36,958
and
13330
08:44:36,958 --> 08:44:39,120
here i'm going to pass in
13331
08:44:39,120 --> 08:44:41,680
asterix followed by the
13332
08:44:41,680 --> 08:44:45,920
name of my argument which is names
13333
08:44:45,920 --> 08:44:48,718
i'll say print
13334
08:44:48,718 --> 08:44:52,558
i'll print my message as hello
13335
08:44:52,718 --> 08:44:54,318
comma
13336
08:44:54,318 --> 08:44:57,680
then i'm going to say
13337
08:44:57,680 --> 08:45:00,080
names
13338
08:45:00,318 --> 08:45:04,718
the first name which is with
13339
08:45:04,718 --> 08:45:07,840
index value of 0
13340
08:45:08,318 --> 08:45:09,360
comma
13341
08:45:09,360 --> 08:45:11,120
followed by a space
13342
08:45:11,120 --> 08:45:12,958
then i have
13343
08:45:12,958 --> 08:45:15,200
name
13344
08:45:15,360 --> 08:45:17,680
one
13345
08:45:18,240 --> 08:45:19,920
will give a
13346
08:45:19,920 --> 08:45:22,478
comma followed by a space and then we'll
13347
08:45:22,478 --> 08:45:23,520
have
13348
08:45:23,520 --> 08:45:25,120
names
13349
08:45:25,120 --> 08:45:26,160
two
13350
08:45:26,160 --> 08:45:28,840
this should be names
13351
08:45:28,840 --> 08:45:31,840
okay let me run this
13352
08:45:31,840 --> 08:45:34,638
now i'll call my function welcome
13353
08:45:34,638 --> 08:45:37,840
now you can see here i have passed in my
13354
08:45:37,840 --> 08:45:39,840
argument that is names three times so
13355
08:45:39,840 --> 08:45:42,080
let's
13356
08:45:42,840 --> 08:45:46,958
call three names let's see we have steve
13357
08:45:46,958 --> 08:45:48,798
then we have
13358
08:45:48,798 --> 08:45:49,680
bill
13359
08:45:49,680 --> 08:45:53,840
and then we have let's see yes
13360
08:45:54,318 --> 08:45:56,318
now if i run this
13361
08:45:56,318 --> 08:45:58,478
there you go we have a message printed
13362
08:45:58,478 --> 08:46:01,440
hello steve bill yes
13363
08:46:01,440 --> 08:46:02,478
okay
13364
08:46:02,478 --> 08:46:04,160
now coming to the final section of our
13365
08:46:04,160 --> 08:46:05,120
demo
13366
08:46:05,120 --> 08:46:07,280
here we are going to find out the area
13367
08:46:07,280 --> 08:46:09,280
of a circle and a sphere so we are going
13368
08:46:09,280 --> 08:46:11,840
to define two functions one for circle
13369
08:46:11,840 --> 08:46:14,160
and another for spear
13370
08:46:14,160 --> 08:46:16,398
so first of all let me define the value
13371
08:46:16,398 --> 08:46:19,440
of pi which is 3.14
13372
08:46:19,440 --> 08:46:22,080
i'll start
13373
08:46:22,398 --> 08:46:24,840
with def
13374
08:46:24,840 --> 08:46:28,240
i'll give my
13375
08:46:28,240 --> 08:46:30,638
function name as circle area
13376
08:46:30,638 --> 08:46:33,760
and inside that i'll pass in my argument
13377
08:46:33,760 --> 08:46:36,398
that is radius
13378
08:46:36,398 --> 08:46:38,638
i'll give a colon and we are going to
13379
08:46:38,638 --> 08:46:41,360
return the area of a circle
13380
08:46:41,360 --> 08:46:44,000
which is pi
13381
08:46:44,000 --> 08:46:46,240
r square so i'll have
13382
08:46:46,240 --> 08:46:47,840
radius
13383
08:46:47,840 --> 08:46:50,318
again an asterisk symbol and then radius
13384
08:46:50,318 --> 08:46:52,000
so this becomes pi
13385
08:46:52,000 --> 08:46:53,760
into radius into radius which is pi r
13386
08:46:53,760 --> 08:46:55,440
square
13387
08:46:55,440 --> 08:46:58,000
okay now let's define another function
13388
08:46:58,000 --> 08:47:00,478
called def i'll say
13389
08:47:00,478 --> 08:47:02,398
sphere
13390
08:47:02,398 --> 08:47:04,638
area
13391
08:47:04,638 --> 08:47:07,280
i'll have my
13392
08:47:07,280 --> 08:47:08,558
argument as
13393
08:47:08,558 --> 08:47:10,398
radius
13394
08:47:10,398 --> 08:47:11,760
give a colon
13395
08:47:11,760 --> 08:47:13,440
and say
13396
08:47:13,440 --> 08:47:15,920
return
13397
08:47:16,160 --> 08:47:18,398
4 pi r square which is the area of a
13398
08:47:18,398 --> 08:47:21,840
sphere so i'll have 4 multiplied by pi
13399
08:47:21,840 --> 08:47:23,840
multiplied by
13400
08:47:23,840 --> 08:47:26,638
radius multiplied by radius so this
13401
08:47:26,638 --> 08:47:29,680
becomes 4 pi r square
13402
08:47:29,680 --> 08:47:31,760
now we are going to define
13403
08:47:31,760 --> 08:47:34,318
the main function so i'll say define
13404
08:47:34,318 --> 08:47:35,280
mean
13405
08:47:35,280 --> 08:47:37,200
where we'll just have
13406
08:47:37,200 --> 08:47:41,120
our print statement so i'll say print
13407
08:47:41,200 --> 08:47:42,638
circle
13408
08:47:42,638 --> 08:47:44,080
area
13409
08:47:44,080 --> 08:47:45,360
with
13410
08:47:45,360 --> 08:47:47,920
radius
13411
08:47:49,760 --> 08:47:52,240
15
13412
08:47:53,600 --> 08:47:55,040
i'll give a
13413
08:47:55,040 --> 08:47:56,558
comma
13414
08:47:56,558 --> 08:47:59,040
and say
13415
08:47:59,120 --> 08:48:00,718
circle area
13416
08:48:00,718 --> 08:48:03,120
which is my function name
13417
08:48:03,120 --> 08:48:05,040
and we'll pass in the value of the
13418
08:48:05,040 --> 08:48:07,200
radius which is 15
13419
08:48:07,200 --> 08:48:10,080
i'll just copy this message
13420
08:48:10,080 --> 08:48:13,200
and we'll print it here i'll say
13421
08:48:13,200 --> 08:48:16,760
spear area
13422
08:48:19,200 --> 08:48:21,760
with radius 15 as
13423
08:48:21,760 --> 08:48:25,520
i'll just change this to spear
13424
08:48:26,638 --> 08:48:28,958
and let's see we'll have radius for 10
13425
08:48:28,958 --> 08:48:31,360
instead of 15
13426
08:48:31,360 --> 08:48:32,878
okay
13427
08:48:32,878 --> 08:48:35,200
finally i'm going to call my main
13428
08:48:35,200 --> 08:48:38,160
function so i'll just say
13429
08:48:38,160 --> 08:48:40,398
main
13430
08:48:40,478 --> 08:48:42,478
now let's run it
13431
08:48:42,478 --> 08:48:45,440
okay there is one error here
13432
08:48:45,440 --> 08:48:47,520
this is an indentation problem now we'll
13433
08:48:47,520 --> 08:48:49,440
run it again there you go
13434
08:48:49,440 --> 08:48:52,958
so the circle area with radius 15 is
13435
08:48:52,958 --> 08:48:55,520
3.14 which is the pi value multiplied by
13436
08:48:55,520 --> 08:48:58,318
radius into radius that is 15 into 15 so
13437
08:48:58,318 --> 08:49:01,760
the value is 706.5
13438
08:49:01,760 --> 08:49:03,440
and similarly the
13439
08:49:03,440 --> 08:49:06,798
spear area with radius 10 is 4 into 3.14
13440
08:49:06,798 --> 08:49:09,200
into 10 into 10 that is
13441
08:49:09,200 --> 08:49:11,440
1256
13442
08:49:11,440 --> 08:49:15,520
all right so what is the lambda function
13443
08:49:15,520 --> 08:49:17,040
a lambda function
13444
08:49:17,040 --> 08:49:18,958
is one of the simplest ways to define a
13445
08:49:18,958 --> 08:49:21,200
function in python a lambda function is
13446
08:49:21,200 --> 08:49:23,280
an anonymous function defined without a
13447
08:49:23,280 --> 08:49:26,638
name and does not have the def keyword
13448
08:49:26,638 --> 08:49:28,878
it can accept any number of arguments
13449
08:49:28,878 --> 08:49:31,360
but can only have one expression
13450
08:49:31,360 --> 08:49:33,440
so the function is always defined using
13451
08:49:33,440 --> 08:49:35,600
the lambda keyword which you can see it
13452
08:49:35,600 --> 08:49:36,478
here
13453
08:49:36,478 --> 08:49:39,120
so here we have the syntax for lambda
13454
08:49:39,120 --> 08:49:41,280
function we use the lambda keyword
13455
08:49:41,280 --> 08:49:43,760
followed by the arguments and then we
13456
08:49:43,760 --> 08:49:46,240
have a colon after that we have the
13457
08:49:46,240 --> 08:49:49,040
expression that we want to evaluate
13458
08:49:49,040 --> 08:49:50,478
cool
13459
08:49:50,478 --> 08:49:53,200
now moving ahead let's
13460
08:49:53,200 --> 08:49:54,958
look at an example of using lambda
13461
08:49:54,958 --> 08:49:56,878
function in python
13462
08:49:56,878 --> 08:49:57,680
now
13463
08:49:57,680 --> 08:49:59,840
if you were to add two numbers in python
13464
08:49:59,840 --> 08:50:02,080
using a function you would ideally use
13465
08:50:02,080 --> 08:50:02,798
the
13466
08:50:02,798 --> 08:50:05,120
def keyword followed by the
13467
08:50:05,120 --> 08:50:07,520
function name and then pass in the
13468
08:50:07,520 --> 08:50:09,280
arguments you can see the arguments here
13469
08:50:09,280 --> 08:50:10,638
a and b
13470
08:50:10,638 --> 08:50:12,080
then
13471
08:50:12,080 --> 08:50:14,160
use the return statement to get the
13472
08:50:14,160 --> 08:50:16,240
value of the addition so we have written
13473
08:50:16,240 --> 08:50:17,680
a plus b
13474
08:50:17,680 --> 08:50:19,840
and finally you will call the add
13475
08:50:19,840 --> 08:50:21,520
function and pass the argument values to
13476
08:50:21,520 --> 08:50:24,840
get the result
13477
08:50:25,120 --> 08:50:27,600
now this operation can be done using the
13478
08:50:27,600 --> 08:50:31,040
lambda function as shown here so we have
13479
08:50:31,040 --> 08:50:33,840
defined a variable called add which is
13480
08:50:33,840 --> 08:50:35,920
equal to and then we have used the
13481
08:50:35,920 --> 08:50:38,000
lambda function followed by the two
13482
08:50:38,000 --> 08:50:40,080
parameters or arguments so here a and b
13483
08:50:40,080 --> 08:50:42,000
are the arguments and then i have my
13484
08:50:42,000 --> 08:50:44,798
expression a plus b see all this you can
13485
08:50:44,798 --> 08:50:47,920
do it in one line
13486
08:50:50,080 --> 08:50:51,840
and then to call the function you use
13487
08:50:51,840 --> 08:50:53,040
print
13488
08:50:53,040 --> 08:50:55,760
followed by the variable which is add
13489
08:50:55,760 --> 08:50:58,000
and then you have the arguments 3 comma
13490
08:50:58,000 --> 08:51:00,558
8 so the addition of 3 plus 8 would be
13491
08:51:00,558 --> 08:51:01,920
11.
13492
08:51:01,920 --> 08:51:03,440
now you can see that instead of using
13493
08:51:03,440 --> 08:51:06,878
the df keyword we have used the
13494
08:51:06,878 --> 08:51:08,718
lambda keyword and there is no
13495
08:51:08,718 --> 08:51:10,958
parenthesis required anything after the
13496
08:51:10,958 --> 08:51:12,878
lambda keyboard is treated as a
13497
08:51:12,878 --> 08:51:15,040
parameter or argument
13498
08:51:15,040 --> 08:51:16,398
so
13499
08:51:16,398 --> 08:51:18,878
we use a colon to separate parameters
13500
08:51:18,878 --> 08:51:20,558
and the expression in our case the
13501
08:51:20,558 --> 08:51:23,200
expression is a plus b
13502
08:51:23,200 --> 08:51:25,760
now moving ahead we look at
13503
08:51:25,760 --> 08:51:27,920
the pros and cons of
13504
08:51:27,920 --> 08:51:30,240
lambda functions so lambda functions are
13505
08:51:30,240 --> 08:51:33,520
suitable for simple logical operations
13506
08:51:33,520 --> 08:51:36,160
so when we want one expression in return
13507
08:51:36,160 --> 08:51:38,638
we can use the lambda function a lambda
13508
08:51:38,638 --> 08:51:41,280
function is used as a parameter to
13509
08:51:41,280 --> 08:51:43,760
different functions such as map filter
13510
08:51:43,760 --> 08:51:46,798
reduce we will see all this in our demo
13511
08:51:46,798 --> 08:51:48,878
session
13512
08:51:48,878 --> 08:51:51,440
now coming to the cons of using
13513
08:51:51,440 --> 08:51:53,040
a lambda function
13514
08:51:53,040 --> 08:51:54,080
so
13515
08:51:54,080 --> 08:51:56,240
lambda functions can only evaluate one
13516
08:51:56,240 --> 08:51:59,200
expression as i told you earlier
13517
08:51:59,200 --> 08:52:00,798
you cannot write a docs string to
13518
08:52:00,798 --> 08:52:02,718
explain all the inputs operations and
13519
08:52:02,718 --> 08:52:04,798
outputs as you would do in a normal user
13520
08:52:04,798 --> 08:52:07,120
defined function that uses the df
13521
08:52:07,120 --> 08:52:08,240
keyword
13522
08:52:08,240 --> 08:52:10,160
and finally lambda functions are
13523
08:52:10,160 --> 08:52:12,080
generally not preferred when you have to
13524
08:52:12,080 --> 08:52:14,398
declare a function with multiple lines
13525
08:52:14,398 --> 08:52:15,600
of code
13526
08:52:15,600 --> 08:52:17,120
now let's head over to our jupyter
13527
08:52:17,120 --> 08:52:18,878
notebook where we will learn about
13528
08:52:18,878 --> 08:52:20,558
lambda functions and how to use it in
13529
08:52:20,558 --> 08:52:23,600
different ways
13530
08:52:23,600 --> 08:52:25,360
so i'm on my jupyter notebook let me
13531
08:52:25,360 --> 08:52:27,920
just go ahead and we'll
13532
08:52:27,920 --> 08:52:30,798
rename the notebook as
13533
08:52:30,798 --> 08:52:32,718
lambda
13534
08:52:32,718 --> 08:52:36,000
functions in python
13535
08:52:36,000 --> 08:52:39,360
okay i'll just click on rename
13536
08:52:39,360 --> 08:52:40,240
cool
13537
08:52:40,240 --> 08:52:42,718
first of all i'll write a
13538
08:52:42,718 --> 08:52:45,040
general user defined function using the
13539
08:52:45,040 --> 08:52:47,680
def keyword let's say i want to add two
13540
08:52:47,680 --> 08:52:49,200
numbers
13541
08:52:49,200 --> 08:52:51,200
so this example we have already seen in
13542
08:52:51,200 --> 08:52:53,120
our slide
13543
08:52:53,120 --> 08:52:55,200
so the way to do is i'll use the df
13544
08:52:55,200 --> 08:52:56,958
keyword followed by the
13545
08:52:56,958 --> 08:52:58,398
function name
13546
08:52:58,398 --> 08:53:00,160
and then i'm giving my
13547
08:53:00,160 --> 08:53:02,080
arguments or the parameters
13548
08:53:02,080 --> 08:53:03,360
after that
13549
08:53:03,360 --> 08:53:06,558
let's say i want to store
13550
08:53:06,558 --> 08:53:08,798
the addition of a plus b in a variable
13551
08:53:08,798 --> 08:53:11,200
called result so i'll say result equal
13552
08:53:11,200 --> 08:53:12,958
to e plus b
13553
08:53:12,958 --> 08:53:14,318
and then
13554
08:53:14,318 --> 08:53:16,798
i want to return my output that is
13555
08:53:16,798 --> 08:53:19,360
result so i'll say return result
13556
08:53:19,360 --> 08:53:21,200
so this is how you will normally create
13557
08:53:21,200 --> 08:53:23,600
a function in python
13558
08:53:23,600 --> 08:53:26,398
if i run it and then
13559
08:53:26,398 --> 08:53:28,558
i'm going to call my function using the
13560
08:53:28,558 --> 08:53:31,200
function name that is add and then i'll
13561
08:53:31,200 --> 08:53:32,558
pass in
13562
08:53:32,558 --> 08:53:34,798
my parameters a and b let's say my a is
13563
08:53:34,798 --> 08:53:37,280
3 and b is 4.
13564
08:53:37,280 --> 08:53:39,200
if i run this
13565
08:53:39,200 --> 08:53:40,958
i will get the result 7 which is fair
13566
08:53:40,958 --> 08:53:41,760
enough
13567
08:53:41,760 --> 08:53:44,240
now if i want to
13568
08:53:44,240 --> 08:53:47,200
do the same operation using
13569
08:53:47,200 --> 08:53:48,878
a lambda function i would do something
13570
08:53:48,878 --> 08:53:50,398
like this let's say i'll create a
13571
08:53:50,398 --> 08:53:51,840
variable called add
13572
08:53:51,840 --> 08:53:54,478
and then i'm going to use the lambda
13573
08:53:54,478 --> 08:53:55,600
function
13574
08:53:55,600 --> 08:53:57,280
followed by the arguments
13575
08:53:57,280 --> 08:53:59,440
a comma p
13576
08:53:59,440 --> 08:54:03,440
then i'll give a colon and say a plus p
13577
08:54:03,440 --> 08:54:06,318
after that i'll just say add
13578
08:54:06,318 --> 08:54:08,798
and then pass in my parameters
13579
08:54:08,798 --> 08:54:11,280
3 comma 4 if i run it you have the same
13580
08:54:11,280 --> 08:54:14,638
result that is 7.
13581
08:54:14,958 --> 08:54:17,920
now let's see another example using
13582
08:54:17,920 --> 08:54:19,680
lambda functions to add
13583
08:54:19,680 --> 08:54:22,000
a number this time i am going to pass in
13584
08:54:22,000 --> 08:54:24,240
only one argument
13585
08:54:24,240 --> 08:54:27,600
so i will say add equal to
13586
08:54:27,600 --> 08:54:29,760
lambda
13587
08:54:29,760 --> 08:54:34,478
my argument is x i'll say x colon x plus
13588
08:54:34,478 --> 08:54:37,040
let's say i want to add x with
13589
08:54:37,040 --> 08:54:38,558
100
13590
08:54:38,558 --> 08:54:40,240
i'll say
13591
08:54:40,240 --> 08:54:41,680
print
13592
08:54:41,680 --> 08:54:44,160
add let's say i want to add 50 with 100
13593
08:54:44,160 --> 08:54:47,360
if i run it i have the result 150
13594
08:54:47,360 --> 08:54:49,120
all right
13595
08:54:49,120 --> 08:54:51,360
now in python there is no need to assign
13596
08:54:51,360 --> 08:54:53,760
a lambda to a variable you can directly
13597
08:54:53,760 --> 08:54:56,798
call it let me show you how to do it
13598
08:54:56,798 --> 08:54:58,318
i'll say
13599
08:54:58,318 --> 08:54:59,920
lambda
13600
08:54:59,920 --> 08:55:03,040
a comma b which are my arguments
13601
08:55:03,040 --> 08:55:06,240
again this time i am going to use
13602
08:55:06,240 --> 08:55:09,040
a multiplication
13603
08:55:09,040 --> 08:55:10,478
operator
13604
08:55:10,478 --> 08:55:12,160
since i want to do a
13605
08:55:12,160 --> 08:55:14,240
product of a and b
13606
08:55:14,240 --> 08:55:16,160
and after that
13607
08:55:16,160 --> 08:55:17,680
using another parenthesis you can
13608
08:55:17,680 --> 08:55:19,520
directly pass in the
13609
08:55:19,520 --> 08:55:21,680
arguments so there is
13610
08:55:21,680 --> 08:55:23,920
no need to explicitly assign a lambda to
13611
08:55:23,920 --> 08:55:25,360
a variable
13612
08:55:25,360 --> 08:55:26,958
so if i want to find the product of
13613
08:55:26,958 --> 08:55:29,680
let's say 15 comma 30
13614
08:55:29,680 --> 08:55:33,280
if i run it i have the result 4 t
13615
08:55:33,280 --> 08:55:35,280
let's change the arguments let's say i
13616
08:55:35,280 --> 08:55:38,398
want to find the product of 5 comma 7
13617
08:55:38,398 --> 08:55:41,520
the answer is 35
13618
08:55:42,240 --> 08:55:44,398
so this is known as an immediately
13619
08:55:44,398 --> 08:55:47,360
invoked function expression or iife
13620
08:55:47,360 --> 08:55:50,718
which is often pronounced as iffy
13621
08:55:50,718 --> 08:55:53,200
now you can also use keyword arguments
13622
08:55:53,200 --> 08:55:55,360
using lambda functions let me show you
13623
08:55:55,360 --> 08:55:57,920
what i mean
13624
08:55:58,318 --> 08:56:00,878
i will create my lambda function and
13625
08:56:00,878 --> 08:56:02,798
assign it to a variable called product
13626
08:56:02,798 --> 08:56:04,318
i'll say
13627
08:56:04,318 --> 08:56:06,638
lambda
13628
08:56:06,638 --> 08:56:10,080
let's say my arguments are x y and z
13629
08:56:10,080 --> 08:56:12,878
after that i'll give a colon and say
13630
08:56:12,878 --> 08:56:18,798
x multiplied by y multiplied by z
13631
08:56:19,760 --> 08:56:23,520
next i'll say print
13632
08:56:23,520 --> 08:56:25,840
my variable that is product
13633
08:56:25,840 --> 08:56:27,680
and here i am going to use keyword
13634
08:56:27,680 --> 08:56:30,878
arguments so i will say let's say
13635
08:56:30,878 --> 08:56:32,958
z equal to 5
13636
08:56:32,958 --> 08:56:34,558
x equal to 10
13637
08:56:34,558 --> 08:56:38,000
and y equal to 4.
13638
08:56:38,000 --> 08:56:39,600
you can see here
13639
08:56:39,600 --> 08:56:41,920
this need not be in an order as you have
13640
08:56:41,920 --> 08:56:44,638
defined the arguments
13641
08:56:44,638 --> 08:56:46,718
if i run this i have the result the
13642
08:56:46,718 --> 08:56:48,958
product of 5 multiplied by 10 multiplied
13643
08:56:48,958 --> 08:56:51,680
by 4 is 200.
13644
08:56:51,680 --> 08:56:53,600
now you can also
13645
08:56:53,600 --> 08:56:54,840
use default
13646
08:56:54,840 --> 08:56:58,000
arguments suppose i say add equal to
13647
08:56:58,000 --> 08:57:00,240
i'll use the lambda function
13648
08:57:00,240 --> 08:57:03,840
and say x comma
13649
08:57:03,840 --> 08:57:07,040
why i'm assigning a default
13650
08:57:07,040 --> 08:57:10,000
value of let's say 15 and z let's say
13651
08:57:10,000 --> 08:57:12,718
i'll assign a default value of 24
13652
08:57:12,718 --> 08:57:15,440
then i'll give my colon followed by the
13653
08:57:15,440 --> 08:57:17,600
expression that is x plus
13654
08:57:17,600 --> 08:57:19,920
y plus z
13655
08:57:19,920 --> 08:57:22,840
now i'll say print
13656
08:57:22,840 --> 08:57:25,920
add now here i'm going to only pass in
13657
08:57:25,920 --> 08:57:28,638
my value for x since i've already
13658
08:57:28,638 --> 08:57:30,718
given a default value for
13659
08:57:30,718 --> 08:57:32,718
argument y and z
13660
08:57:32,718 --> 08:57:33,840
so
13661
08:57:33,840 --> 08:57:36,160
my function only needs
13662
08:57:36,160 --> 08:57:39,440
one argument that is x so let's say i
13663
08:57:39,440 --> 08:57:41,840
want x to be
13664
08:57:41,840 --> 08:57:42,958
20.
13665
08:57:42,958 --> 08:57:44,240
if i run this
13666
08:57:44,240 --> 08:57:46,638
you see the result here so 20 plus 15
13667
08:57:46,638 --> 08:57:50,318
plus 24 is 59.
13668
08:57:50,478 --> 08:57:52,638
now you can also use the
13669
08:57:52,638 --> 08:57:54,398
asterix
13670
08:57:54,398 --> 08:57:57,600
args parameter or args
13671
08:57:57,600 --> 08:58:00,000
so this allows you to define as many
13672
08:58:00,000 --> 08:58:02,240
arguments as you want
13673
08:58:02,240 --> 08:58:04,398
so i'll say addition
13674
08:58:04,398 --> 08:58:05,760
equal to
13675
08:58:05,760 --> 08:58:07,040
lambda
13676
08:58:07,040 --> 08:58:10,000
then i'll say asterix ergs
13677
08:58:10,000 --> 08:58:11,440
so this will
13678
08:58:11,440 --> 08:58:13,760
allow us to pass in as many arguments as
13679
08:58:13,760 --> 08:58:14,798
you want
13680
08:58:14,798 --> 08:58:17,440
and i want to find these sum of
13681
08:58:17,440 --> 08:58:20,080
args so i'm using the inbuilt sum
13682
08:58:20,080 --> 08:58:21,840
function in python
13683
08:58:21,840 --> 08:58:24,558
now i'll say print
13684
08:58:24,558 --> 08:58:27,280
my variable name that is addition
13685
08:58:27,280 --> 08:58:30,240
and let's say i want to find the sum for
13686
08:58:30,240 --> 08:58:31,120
four
13687
08:58:31,120 --> 08:58:35,200
values or four arguments let's say 20 to
13688
08:58:35,200 --> 08:58:37,520
40 and let's say
13689
08:58:37,520 --> 08:58:38,798
50
13690
08:58:38,798 --> 08:58:42,080
if i run it so the value is 112 so 20
13691
08:58:42,080 --> 08:58:46,478
plus 2 plus 40 plus 50 is 112
13692
08:58:46,840 --> 08:58:49,520
cool now lambda functions are also used
13693
08:58:49,520 --> 08:58:51,600
with higher order functions which take
13694
08:58:51,600 --> 08:58:54,080
one or more functions as arguments
13695
08:58:54,080 --> 08:58:56,318
or return one or more functions let me
13696
08:58:56,318 --> 08:58:58,240
show you
13697
08:58:58,240 --> 08:59:00,478
so i will define a variable
13698
08:59:00,478 --> 08:59:02,558
called higher underscore
13699
08:59:02,558 --> 08:59:06,000
ord underscore f u n which stands for
13700
08:59:06,000 --> 08:59:09,920
function and y d stands for order
13701
08:59:10,478 --> 08:59:14,240
so i'll declare my lambda function
13702
08:59:14,718 --> 08:59:17,360
suppose the parameters are x
13703
08:59:17,360 --> 08:59:19,680
and then i have a
13704
08:59:19,680 --> 08:59:22,080
function
13705
08:59:22,080 --> 08:59:24,958
then followed by a colon i have
13706
08:59:24,958 --> 08:59:28,318
expression as x plus
13707
08:59:28,318 --> 08:59:31,200
function of x
13708
08:59:31,200 --> 08:59:33,920
now i'll say
13709
08:59:34,160 --> 08:59:38,080
higher underscore ord underscore
13710
08:59:38,080 --> 08:59:40,478
f u n
13711
08:59:40,478 --> 08:59:43,600
and i'll pass in my two
13712
08:59:43,600 --> 08:59:46,638
arguments let's say my x is 20
13713
08:59:46,638 --> 08:59:49,120
and for this
13714
08:59:49,120 --> 08:59:50,798
function
13715
08:59:50,798 --> 08:59:52,878
i'll have
13716
08:59:52,878 --> 08:59:54,558
a lambda function defined i'll say
13717
08:59:54,558 --> 08:59:56,080
lambda x
13718
08:59:56,080 --> 08:59:57,360
colon
13719
08:59:57,360 --> 09:00:00,878
x multiplied by x
13720
09:00:00,878 --> 09:00:03,120
now
13721
09:00:03,600 --> 09:00:05,600
let me just run this and i'll show you
13722
09:00:05,600 --> 09:00:10,080
the result okay so what it did was
13723
09:00:10,080 --> 09:00:11,600
it added
13724
09:00:11,600 --> 09:00:13,840
my value of x that is
13725
09:00:13,840 --> 09:00:15,920
20 with
13726
09:00:15,920 --> 09:00:18,478
function of x or f u n of x what is f u
13727
09:00:18,478 --> 09:00:21,040
n of x fun of x is
13728
09:00:21,040 --> 09:00:24,160
x multiplied by x so 20 multiplied by 20
13729
09:00:24,160 --> 09:00:27,840
is 400 so this becomes 400
13730
09:00:27,840 --> 09:00:29,280
plus x
13731
09:00:29,280 --> 09:00:32,318
which is 400 plus 20 hence the result is
13732
09:00:32,318 --> 09:00:34,878
420
13733
09:00:35,600 --> 09:00:38,840
so this is known as a higher order
13734
09:00:38,840 --> 09:00:41,120
function now let me show you another
13735
09:00:41,120 --> 09:00:42,638
example
13736
09:00:42,638 --> 09:00:46,398
let's say i want to display order event
13737
09:00:46,398 --> 09:00:48,318
by
13738
09:00:48,318 --> 09:00:50,718
taking the value of x
13739
09:00:50,718 --> 09:00:55,360
i'll say lambda x colon
13740
09:00:55,360 --> 09:00:58,240
here i am going to pass in my expression
13741
09:00:58,240 --> 09:00:59,760
if x
13742
09:00:59,760 --> 09:01:00,718
mod
13743
09:01:00,718 --> 09:01:02,478
2
13744
09:01:02,478 --> 09:01:05,520
will give a space here
13745
09:01:05,520 --> 09:01:08,638
i'll say and
13746
09:01:08,638 --> 09:01:10,798
odd
13747
09:01:10,798 --> 09:01:12,160
or
13748
09:01:12,160 --> 09:01:14,478
even
13749
09:01:20,080 --> 09:01:23,360
now i'll give my argument value let's
13750
09:01:23,360 --> 09:01:24,558
say 30
13751
09:01:24,558 --> 09:01:26,798
the result is even because 30 is
13752
09:01:26,798 --> 09:01:28,398
divisible by 2
13753
09:01:28,398 --> 09:01:31,760
i'll just change this to let's say 15.
13754
09:01:31,760 --> 09:01:34,798
if i run it the answer is odd because 15
13755
09:01:34,798 --> 09:01:36,798
is not divisible by 2
13756
09:01:36,798 --> 09:01:41,200
let's say i have 150 the answer is even
13757
09:01:41,200 --> 09:01:43,360
now moving ahead
13758
09:01:43,360 --> 09:01:45,040
so let's write a lambda function that
13759
09:01:45,040 --> 09:01:47,520
checks if a string is a substring of the
13760
09:01:47,520 --> 09:01:49,360
given string
13761
09:01:49,360 --> 09:01:52,718
so i'll create a variable called sub
13762
09:01:52,718 --> 09:01:54,000
underscore
13763
09:01:54,000 --> 09:01:56,000
string
13764
09:01:56,000 --> 09:01:58,398
now i'll assign this
13765
09:01:58,398 --> 09:02:01,360
variable with a lambda function i'll say
13766
09:02:01,360 --> 09:02:03,440
lambda
13767
09:02:03,440 --> 09:02:05,200
my
13768
09:02:05,200 --> 09:02:07,440
argument let's say string
13769
09:02:07,440 --> 09:02:09,360
i'll give the colon and pass in my
13770
09:02:09,360 --> 09:02:13,040
expression string in
13771
09:02:13,360 --> 09:02:15,680
let's say my string
13772
09:02:15,680 --> 09:02:19,840
has this message welcome to
13773
09:02:19,840 --> 09:02:21,760
python
13774
09:02:21,760 --> 09:02:22,878
functions
13775
09:02:22,878 --> 09:02:25,440
tutorial
13776
09:02:28,318 --> 09:02:29,680
okay
13777
09:02:29,680 --> 09:02:32,638
so we are going to check if a string
13778
09:02:32,638 --> 09:02:36,000
let's say in to welcome python java
13779
09:02:36,000 --> 09:02:37,840
anything whether it is present inside
13780
09:02:37,840 --> 09:02:40,558
the string which is welcome to python
13781
09:02:40,558 --> 09:02:42,718
functions tutorial
13782
09:02:42,718 --> 09:02:45,520
now let me go to the next line
13783
09:02:45,520 --> 09:02:47,120
i'm
13784
09:02:47,120 --> 09:02:49,920
going to print
13785
09:02:50,080 --> 09:02:52,398
sub underscore string
13786
09:02:52,398 --> 09:02:54,558
and let's say i want to check for my
13787
09:02:54,558 --> 09:02:56,478
string
13788
09:02:56,478 --> 09:02:58,478
let's say java
13789
09:02:58,478 --> 09:03:01,840
if i run it it gives me false because
13790
09:03:01,840 --> 09:03:04,080
the word java is not present inside my
13791
09:03:04,080 --> 09:03:05,920
string welcome to python functions
13792
09:03:05,920 --> 09:03:07,200
tutorial
13793
09:03:07,200 --> 09:03:08,958
suppose i write
13794
09:03:08,958 --> 09:03:10,478
python
13795
09:03:10,478 --> 09:03:12,398
now one thing to note here is
13796
09:03:12,398 --> 09:03:15,760
the p is small and here the p is in
13797
09:03:15,760 --> 09:03:17,280
capital letter
13798
09:03:17,280 --> 09:03:19,280
if i run it it again gives me false
13799
09:03:19,280 --> 09:03:20,398
because
13800
09:03:20,398 --> 09:03:22,798
python is case sensitive now if i make
13801
09:03:22,798 --> 09:03:26,318
this as capital p it will result in
13802
09:03:26,318 --> 09:03:27,440
true
13803
09:03:27,440 --> 09:03:30,318
let's say i want to say
13804
09:03:30,318 --> 09:03:32,318
instead of tutorial let's say i have
13805
09:03:32,318 --> 09:03:34,718
training
13806
09:03:37,120 --> 09:03:39,520
if i run it it tells me false because
13807
09:03:39,520 --> 09:03:41,360
the support training is not
13808
09:03:41,360 --> 09:03:43,040
there in the string welcome to python
13809
09:03:43,040 --> 09:03:44,478
functions tutorial
13810
09:03:44,478 --> 09:03:48,240
i'll change this to tutorial
13811
09:03:48,638 --> 09:03:52,638
you see here i have my result as true
13812
09:03:53,520 --> 09:03:56,680
all right
13813
09:03:57,360 --> 09:03:58,878
now let's see how to use lambda
13814
09:03:58,878 --> 09:04:01,600
functions using filter function so we
13815
09:04:01,600 --> 09:04:03,120
are going to filter a list of numbers
13816
09:04:03,120 --> 09:04:05,760
using a specific condition
13817
09:04:05,760 --> 09:04:07,280
so i'll create a
13818
09:04:07,280 --> 09:04:09,600
list called num
13819
09:04:09,600 --> 09:04:12,798
let's say it has values 40
13820
09:04:12,798 --> 09:04:14,478
10 40
13821
09:04:14,478 --> 09:04:19,520
then we have 56 27 let's say 33 15 and
13822
09:04:19,520 --> 09:04:20,798
70.
13823
09:04:20,798 --> 09:04:22,398
so this is my list
13824
09:04:22,398 --> 09:04:25,840
now i'm going to use
13825
09:04:25,920 --> 09:04:28,080
my lambda function
13826
09:04:28,080 --> 09:04:30,958
along with the filter function
13827
09:04:30,958 --> 09:04:33,440
so i will pass in my variable as greater
13828
09:04:33,440 --> 09:04:36,318
i'll say list
13829
09:04:36,840 --> 09:04:40,000
then i'm going to
13830
09:04:40,000 --> 09:04:41,680
pass in the filter function and inside
13831
09:04:41,680 --> 09:04:43,200
the filter function i'll use the lambda
13832
09:04:43,200 --> 09:04:45,200
function
13833
09:04:45,200 --> 09:04:47,360
and i'll have my
13834
09:04:47,360 --> 09:04:49,280
argument as num
13835
09:04:49,280 --> 09:04:51,120
and my expression let's say i want to
13836
09:04:51,120 --> 09:04:53,040
filter only those values
13837
09:04:53,040 --> 09:04:55,360
which are greater than 30
13838
09:04:55,360 --> 09:04:57,840
so i'll say num greater than 30 this is
13839
09:04:57,840 --> 09:04:59,520
my expression
13840
09:04:59,520 --> 09:05:01,600
or the condition that we want to check
13841
09:05:01,600 --> 09:05:03,120
i'll give a comma
13842
09:05:03,120 --> 09:05:04,398
and
13843
09:05:04,398 --> 09:05:07,680
i'll have my next argument as num
13844
09:05:07,680 --> 09:05:10,000
and let's go ahead and print the
13845
09:05:10,000 --> 09:05:11,760
variable greater
13846
09:05:11,760 --> 09:05:14,398
if i run this there you go you can see
13847
09:05:14,398 --> 09:05:16,478
here we have all the values from the
13848
09:05:16,478 --> 09:05:18,318
list or the numbers from the list that
13849
09:05:18,318 --> 09:05:20,638
are greater than 30 so we have
13850
09:05:20,638 --> 09:05:24,558
40 56 33 and 70. so
13851
09:05:24,558 --> 09:05:25,680
10
13852
09:05:25,680 --> 09:05:27,840
27 and 15 are
13853
09:05:27,840 --> 09:05:30,398
less than 30 and hence those three
13854
09:05:30,398 --> 09:05:33,200
values were not printed
13855
09:05:33,200 --> 09:05:35,280
now let's say you want to filter a list
13856
09:05:35,280 --> 09:05:38,398
of numbers that are divisible by four
13857
09:05:38,398 --> 09:05:40,398
so i'll create another list called list
13858
09:05:40,398 --> 09:05:41,680
one
13859
09:05:41,680 --> 09:05:43,280
and
13860
09:05:43,280 --> 09:05:45,360
let's say i'll have the
13861
09:05:45,360 --> 09:05:46,398
same
13862
09:05:46,398 --> 09:05:48,878
values in the list so i'll just copy it
13863
09:05:48,878 --> 09:05:51,920
and i'll paste it here
13864
09:05:53,040 --> 09:05:55,200
and i'll create my variable let's say
13865
09:05:55,200 --> 09:05:56,638
divide
13866
09:05:56,638 --> 09:05:59,440
i'll say divide equal to
13867
09:05:59,440 --> 09:06:01,600
list one
13868
09:06:01,600 --> 09:06:03,200
use the filter function
13869
09:06:03,200 --> 09:06:05,280
followed by
13870
09:06:05,280 --> 09:06:08,240
the lambda function
13871
09:06:08,800 --> 09:06:10,640
i'll say x
13872
09:06:10,640 --> 09:06:13,040
colon
13873
09:06:14,398 --> 09:06:16,557
then i'll have my expression
13874
09:06:16,557 --> 09:06:17,680
x
13875
09:06:17,680 --> 09:06:22,640
mod 4 should be equal to equal to 0
13876
09:06:23,199 --> 09:06:26,877
i'll give a comma and pass in my
13877
09:06:27,040 --> 09:06:30,480
next argument as my list 1.
13878
09:06:30,480 --> 09:06:32,237
now
13879
09:06:32,237 --> 09:06:34,799
i'm going to
13880
09:06:35,199 --> 09:06:37,199
say print
13881
09:06:37,199 --> 09:06:39,199
divide
13882
09:06:39,199 --> 09:06:40,800
okay
13883
09:06:40,800 --> 09:06:42,398
okay i'll remove the bracket here there
13884
09:06:42,398 --> 09:06:45,519
is no need of a bracket here cool
13885
09:06:45,519 --> 09:06:48,480
and we'll delete here as well
13886
09:06:48,480 --> 09:06:50,237
now let's run it
13887
09:06:50,237 --> 09:06:52,000
okay there is one error here this should
13888
09:06:52,000 --> 09:06:54,640
be list one
13889
09:06:54,640 --> 09:06:57,122
let's run it again
13890
09:06:57,122 --> 09:07:01,122
okay there is some error let's debug it
13891
09:07:01,122 --> 09:07:04,480
okay so this should be the function list
13892
09:07:04,480 --> 09:07:05,839
cool
13893
09:07:05,839 --> 09:07:07,040
there you go
13894
09:07:07,040 --> 09:07:10,000
so here you can see we have two numbers
13895
09:07:10,000 --> 09:07:13,199
40 and 56 that are divisible by 4.
13896
09:07:13,199 --> 09:07:14,800
the remaining
13897
09:07:14,800 --> 09:07:16,800
numbers are not divisible by 4 and hence
13898
09:07:16,800 --> 09:07:18,877
were not printed
13899
09:07:18,877 --> 09:07:20,557
all right
13900
09:07:20,557 --> 09:07:22,960
now let's go ahead and explore how you
13901
09:07:22,960 --> 09:07:25,598
can use the map function along with the
13902
09:07:25,598 --> 09:07:26,877
lambda function
13903
09:07:26,877 --> 09:07:29,278
so the map function accepts two
13904
09:07:29,278 --> 09:07:32,000
arguments a function and a list it takes
13905
09:07:32,000 --> 09:07:33,680
that function and applies it to every
13906
09:07:33,680 --> 09:07:35,122
element of the list and returns the
13907
09:07:35,122 --> 09:07:36,960
modified list
13908
09:07:36,960 --> 09:07:39,598
so we'll create a list called
13909
09:07:39,598 --> 09:07:41,278
list2
13910
09:07:41,278 --> 09:07:43,199
let's say equal to
13911
09:07:43,199 --> 09:07:45,839
and again we are going to have the
13912
09:07:45,839 --> 09:07:49,199
same values in the list as above
13913
09:07:49,199 --> 09:07:50,800
so i'll just
13914
09:07:50,800 --> 09:07:52,718
copy those values
13915
09:07:52,718 --> 09:07:56,320
or the numbers and i'll paste it here
13916
09:07:56,320 --> 09:07:57,598
okay
13917
09:07:57,598 --> 09:07:59,840
now
13918
09:08:01,519 --> 09:08:02,877
let's say
13919
09:08:02,877 --> 09:08:04,800
i want to find
13920
09:08:04,800 --> 09:08:07,680
the value of a number multiplied by 2 so
13921
09:08:07,680 --> 09:08:09,839
i'll say
13922
09:08:09,839 --> 09:08:11,519
let's say the variable name is double
13923
09:08:11,519 --> 09:08:14,718
the num or double the number
13924
09:08:14,718 --> 09:08:16,480
i'm going to use the list function
13925
09:08:16,480 --> 09:08:19,278
followed by the map function this time
13926
09:08:19,278 --> 09:08:21,122
and then we'll have our
13927
09:08:21,122 --> 09:08:22,960
lambda function
13928
09:08:22,960 --> 09:08:26,000
i'll pass in my argument as x
13929
09:08:26,000 --> 09:08:27,122
colon
13930
09:08:27,122 --> 09:08:29,199
followed by the expression
13931
09:08:29,199 --> 09:08:32,000
so x multiplied by 2
13932
09:08:32,000 --> 09:08:35,278
i'll give a comma and then we'll have
13933
09:08:35,278 --> 09:08:38,557
the list that is list 2
13934
09:08:38,557 --> 09:08:39,917
there's a spelling mistake here this
13935
09:08:39,917 --> 09:08:43,278
should be list 2 all right
13936
09:08:43,278 --> 09:08:44,320
now
13937
09:08:44,320 --> 09:08:47,438
i'm going to say print
13938
09:08:47,438 --> 09:08:48,398
double
13939
09:08:48,398 --> 09:08:50,557
underscore the
13940
09:08:50,557 --> 09:08:52,718
underscore num
13941
09:08:52,718 --> 09:08:56,000
make sure there is no space
13942
09:08:56,000 --> 09:08:56,960
in the
13943
09:08:56,960 --> 09:08:58,480
variable name
13944
09:08:58,480 --> 09:09:00,078
now if i run it
13945
09:09:00,078 --> 09:09:02,839
you can see here we have
13946
09:09:02,839 --> 09:09:06,237
multiplied every number in the list by 2
13947
09:09:06,237 --> 09:09:07,438
so
13948
09:09:07,438 --> 09:09:10,000
10 multiplied by 2 is 20
13949
09:09:10,000 --> 09:09:12,640
40 multiplied by 2 is 80. similarly 30
13950
09:09:12,640 --> 09:09:14,800
multiplied by 2 is 66
13951
09:09:14,800 --> 09:09:17,598
15 multiplied by 2 is 30 and again we
13952
09:09:17,598 --> 09:09:18,398
have
13953
09:09:18,398 --> 09:09:21,360
70 multiplied by 2 is 140
13954
09:09:21,360 --> 09:09:23,758
so this is how you can use
13955
09:09:23,758 --> 09:09:25,839
map function with
13956
09:09:25,839 --> 09:09:28,717
lambda functions
13957
09:09:30,160 --> 09:09:32,237
now suppose you want to find the power
13958
09:09:32,237 --> 09:09:34,000
of a number let's say i want to find the
13959
09:09:34,000 --> 09:09:37,199
power of a number raised to 3
13960
09:09:37,199 --> 09:09:39,438
so i'll create a list called list
13961
09:09:39,438 --> 09:09:41,519
3
13962
09:09:41,519 --> 09:09:45,598
let's say i have values 2 comma 5 comma
13963
09:09:45,598 --> 09:09:48,557
10 comma 6 comma 4 and then let's see we
13964
09:09:48,557 --> 09:09:50,320
have 12
13965
09:09:50,320 --> 09:09:52,800
i'll say
13966
09:09:53,122 --> 09:09:54,640
cube
13967
09:09:54,640 --> 09:09:56,398
so i want to find
13968
09:09:56,398 --> 09:09:57,758
the value of a number raised to the
13969
09:09:57,758 --> 09:09:59,122
power 3
13970
09:09:59,122 --> 09:10:01,122
so for that i am going to use the map
13971
09:10:01,122 --> 09:10:03,360
function followed by the
13972
09:10:03,360 --> 09:10:05,199
lambda function
13973
09:10:05,199 --> 09:10:07,438
i will pass in my argument x
13974
09:10:07,438 --> 09:10:09,122
and then
13975
09:10:09,122 --> 09:10:11,360
here i am going to use the inbuilt
13976
09:10:11,360 --> 09:10:14,557
power function in python so pow
13977
09:10:14,557 --> 09:10:16,078
my
13978
09:10:16,078 --> 09:10:17,519
argument x
13979
09:10:17,519 --> 09:10:19,438
and this x i want to raise it to the
13980
09:10:19,438 --> 09:10:22,000
power of 3
13981
09:10:22,000 --> 09:10:24,480
and then i'll have my
13982
09:10:24,480 --> 09:10:26,960
next argument which is
13983
09:10:26,960 --> 09:10:30,000
list underscore 3
13984
09:10:30,000 --> 09:10:32,078
or list 3
13985
09:10:32,078 --> 09:10:33,519
okay
13986
09:10:33,519 --> 09:10:35,438
now
13987
09:10:35,438 --> 09:10:36,877
let's
13988
09:10:36,877 --> 09:10:39,438
print the list so i'll say list of
13989
09:10:39,438 --> 09:10:42,438
cube
13990
09:10:42,877 --> 09:10:44,237
i'll run it
13991
09:10:44,237 --> 09:10:45,598
you can see here
13992
09:10:45,598 --> 09:10:48,078
so 2 raised to the power 3 or 2 cube is
13993
09:10:48,078 --> 09:10:50,480
8 5 cube is 125
13994
09:10:50,480 --> 09:10:52,160
10 cube is 1000
13995
09:10:52,160 --> 09:10:56,237
6 cube is 216 4 cube is 64 and 12 cube
13996
09:10:56,237 --> 09:10:57,438
is
13997
09:10:57,438 --> 09:11:00,160
1728.
13998
09:11:00,160 --> 09:11:01,917
now coming to the final section of the
13999
09:11:01,917 --> 09:11:04,960
demo where we'll see how to use
14000
09:11:04,960 --> 09:11:06,640
the reduced function in python along
14001
09:11:06,640 --> 09:11:08,718
with the lambda function and then we'll
14002
09:11:08,718 --> 09:11:10,480
use a lambda function within a user
14003
09:11:10,480 --> 09:11:12,718
defined function
14004
09:11:12,718 --> 09:11:15,519
so first let's look at how to use reduce
14005
09:11:15,519 --> 09:11:16,718
in python
14006
09:11:16,718 --> 09:11:19,040
so reduce applies a ruling calculation
14007
09:11:19,040 --> 09:11:21,360
to all items in a list you can use it to
14008
09:11:21,360 --> 09:11:23,278
calculate the total sum or to multiply
14009
09:11:23,278 --> 09:11:25,278
all the numbers together so reduce
14010
09:11:25,278 --> 09:11:26,877
function is a part of
14011
09:11:26,877 --> 09:11:30,557
func tools module so to use the
14012
09:11:30,557 --> 09:11:32,960
reduce function you need to import the
14013
09:11:32,960 --> 09:11:37,438
func tools module first so i'll say from
14014
09:11:37,438 --> 09:11:40,000
funk tools
14015
09:11:40,000 --> 09:11:41,839
import
14016
09:11:41,839 --> 09:11:43,839
my function that is
14017
09:11:43,839 --> 09:11:45,438
reduce
14018
09:11:45,438 --> 09:11:48,480
and then i'll say
14019
09:11:48,480 --> 09:11:51,438
let's say i have my list 4 this time
14020
09:11:51,438 --> 09:11:54,237
i'll create a list
14021
09:11:54,237 --> 09:11:56,640
i'm going to take the same values as
14022
09:11:56,640 --> 09:11:58,000
above
14023
09:11:58,000 --> 09:12:00,877
and i'll pass in within a list
14024
09:12:00,877 --> 09:12:02,480
and now
14025
09:12:02,480 --> 09:12:04,718
i want to find the sum of all the
14026
09:12:04,718 --> 09:12:06,160
numbers that are present inside this
14027
09:12:06,160 --> 09:12:08,398
list called list 4
14028
09:12:08,398 --> 09:12:11,680
so i'll do that using my reduce function
14029
09:12:11,680 --> 09:12:14,960
followed by the lambda function
14030
09:12:14,960 --> 09:12:16,557
so i'll say x
14031
09:12:16,557 --> 09:12:18,718
comma
14032
09:12:18,718 --> 09:12:21,598
and i'll give my expression as
14033
09:12:21,598 --> 09:12:23,122
y colon
14034
09:12:23,122 --> 09:12:24,160
x
14035
09:12:24,160 --> 09:12:25,199
plus
14036
09:12:25,199 --> 09:12:27,359
y
14037
09:12:27,917 --> 09:12:30,160
give a comma and pass in my last
14038
09:12:30,160 --> 09:12:32,320
argument which is my list itself which
14039
09:12:32,320 --> 09:12:35,122
is list four all right
14040
09:12:35,122 --> 09:12:37,680
and then i'll say print
14041
09:12:37,680 --> 09:12:38,960
sum
14042
09:12:38,960 --> 09:12:40,237
let's run it
14043
09:12:40,237 --> 09:12:43,278
there you go so 2 plus 5 plus 10 plus 6
14044
09:12:43,278 --> 09:12:45,758
plus 4 plus 12 is 39
14045
09:12:45,758 --> 09:12:50,000
so this is how you can use the
14046
09:12:50,000 --> 09:12:51,917
reduce function in python suppose you
14047
09:12:51,917 --> 09:12:53,438
wanted to find the
14048
09:12:53,438 --> 09:12:55,040
product of all the numbers present
14049
09:12:55,040 --> 09:12:57,519
inside the list so you can just
14050
09:12:57,519 --> 09:12:59,839
use the
14051
09:12:59,839 --> 09:13:01,917
asterisk instead of the addition
14052
09:13:01,917 --> 09:13:04,078
operator now if i run this
14053
09:13:04,078 --> 09:13:06,160
you have the product of all the numbers
14054
09:13:06,160 --> 09:13:08,237
that were present inside the list that
14055
09:13:08,237 --> 09:13:10,237
is list four we have twenty eight
14056
09:13:10,237 --> 09:13:12,320
thousand eight hundred
14057
09:13:12,320 --> 09:13:13,519
now
14058
09:13:13,519 --> 09:13:15,360
let me show you the final demo that is
14059
09:13:15,360 --> 09:13:17,680
using a lambda function in a user
14060
09:13:17,680 --> 09:13:19,040
defined function
14061
09:13:19,040 --> 09:13:22,398
so first of all i'll create a
14062
09:13:22,398 --> 09:13:25,122
user defined function to calculate the
14063
09:13:25,122 --> 09:13:27,598
value of a quadratic equation
14064
09:13:27,598 --> 09:13:29,519
so
14065
09:13:29,519 --> 09:13:31,040
for some of you who don't know the
14066
09:13:31,040 --> 09:13:33,598
formula quadratic equation it is
14067
09:13:33,598 --> 09:13:36,320
a x square plus b x plus c so we are
14068
09:13:36,320 --> 09:13:38,237
going to use that formula in our
14069
09:13:38,237 --> 09:13:39,360
function
14070
09:13:39,360 --> 09:13:41,598
and find the value using
14071
09:13:41,598 --> 09:13:43,278
lambda function
14072
09:13:43,278 --> 09:13:45,040
so i'll create a user defined function
14073
09:13:45,040 --> 09:13:47,758
called quadratic
14074
09:13:47,758 --> 09:13:50,078
and it will have
14075
09:13:50,078 --> 09:13:54,320
three arguments a comma b comma c
14076
09:13:54,320 --> 09:13:55,758
i'll say
14077
09:13:55,758 --> 09:13:57,598
return
14078
09:13:57,598 --> 09:13:59,680
and here in the return expression i am
14079
09:13:59,680 --> 09:14:01,438
going to pass in my lambda function i'll
14080
09:14:01,438 --> 09:14:02,718
say lambda
14081
09:14:02,718 --> 09:14:04,960
x which is my
14082
09:14:04,960 --> 09:14:07,199
argument i'll say x
14083
09:14:07,199 --> 09:14:08,800
colon
14084
09:14:08,800 --> 09:14:12,398
then i'll have my equation which is a
14085
09:14:12,398 --> 09:14:15,917
x square so i am saying a multiplied by
14086
09:14:15,917 --> 09:14:17,278
x
14087
09:14:17,278 --> 09:14:19,758
raised to the power 2 so i am using
14088
09:14:19,758 --> 09:14:21,040
double
14089
09:14:21,040 --> 09:14:22,640
astrix
14090
09:14:22,640 --> 09:14:24,078
plus
14091
09:14:24,078 --> 09:14:25,519
b
14092
09:14:25,519 --> 09:14:28,078
multiplied by x so this becomes ax
14093
09:14:28,078 --> 09:14:30,320
square plus bx
14094
09:14:30,320 --> 09:14:31,917
plus
14095
09:14:31,917 --> 09:14:35,360
i have my constant that is c
14096
09:14:35,360 --> 09:14:37,598
now
14097
09:14:38,237 --> 09:14:40,557
just go to the next line
14098
09:14:40,557 --> 09:14:42,160
i'm going to create a variable let's say
14099
09:14:42,160 --> 09:14:43,199
f
14100
09:14:43,199 --> 09:14:45,199
i'll have my
14101
09:14:45,199 --> 09:14:48,000
user defined function name quadratic
14102
09:14:48,000 --> 09:14:50,078
i'll pass in the values for a b and c
14103
09:14:50,078 --> 09:14:53,438
let's say a is 2 b is 3
14104
09:14:53,438 --> 09:14:56,320
and c is let's say 5
14105
09:14:56,320 --> 09:14:58,640
and i'll call a of 1
14106
09:14:58,640 --> 09:15:01,278
the value of x is 1
14107
09:15:01,278 --> 09:15:03,839
the value of a is 2
14108
09:15:03,839 --> 09:15:06,800
the value of b is 3 and the value of c
14109
09:15:06,800 --> 09:15:08,237
is 5
14110
09:15:08,237 --> 09:15:10,877
now let's run it and see the result
14111
09:15:10,877 --> 09:15:12,160
so
14112
09:15:12,160 --> 09:15:15,040
ax square plus bx plus c
14113
09:15:15,040 --> 09:15:16,640
for
14114
09:15:16,640 --> 09:15:20,718
equal to 2 b equal to 3 and c equal to
14115
09:15:20,718 --> 09:15:23,519
5 the result is 10.
14116
09:15:23,519 --> 09:15:25,917
now let's change it to let's say i want
14117
09:15:25,917 --> 09:15:28,718
the value of x as 2 if i run it
14118
09:15:28,718 --> 09:15:31,122
you have the
14119
09:15:31,122 --> 09:15:34,557
equation evaluated and the result is 19
14120
09:15:34,557 --> 09:15:37,598
all right
14121
09:15:37,598 --> 09:15:40,320
so let me first just give you a
14122
09:15:40,320 --> 09:15:42,320
brief of what are the
14123
09:15:42,320 --> 09:15:44,000
syntax for these functions what are the
14124
09:15:44,000 --> 09:15:46,320
signatures what are the input parameters
14125
09:15:46,320 --> 09:15:48,718
required and then we will jump into the
14126
09:15:48,718 --> 09:15:52,160
jupyter notebook and run some examples
14127
09:15:52,160 --> 09:15:54,557
so what is a map function the map
14128
09:15:54,557 --> 09:15:56,557
function takes another function as an
14129
09:15:56,557 --> 09:15:59,438
input and the second input is a sequence
14130
09:15:59,438 --> 09:16:01,839
of iterables it could be a list it could
14131
09:16:01,839 --> 09:16:03,519
be a set it could be an array or
14132
09:16:03,519 --> 09:16:05,917
anything you want and it returns the
14133
09:16:05,917 --> 09:16:08,640
output after executing the function
14134
09:16:08,640 --> 09:16:11,040
which you have passed as a parameter on
14135
09:16:11,040 --> 09:16:13,519
each of the element of that iterable so
14136
09:16:13,519 --> 09:16:16,078
the function could add the values could
14137
09:16:16,078 --> 09:16:17,598
multiply could divide or any
14138
09:16:17,598 --> 09:16:20,000
mathematical operation you want to do or
14139
09:16:20,000 --> 09:16:22,160
even you can perform certain string
14140
09:16:22,160 --> 09:16:23,917
operations which we will take a look in
14141
09:16:23,917 --> 09:16:27,122
the examples but the idea is that you
14142
09:16:27,122 --> 09:16:29,438
want to apply the function on each and
14143
09:16:29,438 --> 09:16:32,480
every iterable item individually so the
14144
09:16:32,480 --> 09:16:36,398
map function helps you achieve that
14145
09:16:36,398 --> 09:16:39,040
also in our examples we will take a look
14146
09:16:39,040 --> 09:16:41,360
that you don't need to explicitly define
14147
09:16:41,360 --> 09:16:43,438
a function you can also use lambda
14148
09:16:43,438 --> 09:16:46,398
functions which are inline functions uh
14149
09:16:46,398 --> 09:16:48,160
so you don't need to
14150
09:16:48,160 --> 09:16:50,237
increase the line of code you can reduce
14151
09:16:50,237 --> 09:16:52,557
the lines of code by just writing the
14152
09:16:52,557 --> 09:16:54,398
function in line and that way it will
14153
09:16:54,398 --> 09:16:56,489
make it more simple
14154
09:16:56,489 --> 09:16:59,619
[Music]
14155
09:17:01,917 --> 09:17:03,199
the next function which we are going to
14156
09:17:03,199 --> 09:17:05,519
talk about is the filter function
14157
09:17:05,519 --> 09:17:07,122
now what is the purpose
14158
09:17:07,122 --> 09:17:09,839
the purpose of the filter function is to
14159
09:17:09,839 --> 09:17:12,160
reduce the size of the iterables based
14160
09:17:12,160 --> 09:17:14,480
on the condition which you will pass to
14161
09:17:14,480 --> 09:17:15,917
the filter function
14162
09:17:15,917 --> 09:17:18,160
so syntactically it is very similar to
14163
09:17:18,160 --> 09:17:20,640
map function but the only difference is
14164
09:17:20,640 --> 09:17:22,718
that you need to pass a function which
14165
09:17:22,718 --> 09:17:25,519
carries a condition for example you have
14166
09:17:25,519 --> 09:17:27,680
a set of i tables one two three four
14167
09:17:27,680 --> 09:17:30,557
five six seven eight nine and you want
14168
09:17:30,557 --> 09:17:32,960
the function to return values which are
14169
09:17:32,960 --> 09:17:34,480
greater than phi
14170
09:17:34,480 --> 09:17:35,278
so
14171
09:17:35,278 --> 09:17:37,680
your filter function should have a
14172
09:17:37,680 --> 09:17:39,917
condition which says
14173
09:17:39,917 --> 09:17:43,278
that the i tables greater than 5 should
14174
09:17:43,278 --> 09:17:45,598
only be returned by this function
14175
09:17:45,598 --> 09:17:48,557
so that's why you have to make sure that
14176
09:17:48,557 --> 09:17:50,800
the filter function always have a
14177
09:17:50,800 --> 09:17:53,917
condition which is either true or false
14178
09:17:53,917 --> 09:17:56,320
so based on whatever condition matches
14179
09:17:56,320 --> 09:17:57,438
as true
14180
09:17:57,438 --> 09:17:59,839
the filter function will only return
14181
09:17:59,839 --> 09:18:02,320
those values as an output so we will
14182
09:18:02,320 --> 09:18:03,438
take a look
14183
09:18:03,438 --> 09:18:05,122
and the same
14184
09:18:05,122 --> 09:18:08,078
concept applies either you can make a
14185
09:18:08,078 --> 09:18:10,000
explicit function or you can apply
14186
09:18:10,000 --> 09:18:13,278
lambda function inline
14187
09:18:14,877 --> 09:18:16,398
the third function
14188
09:18:16,398 --> 09:18:18,237
which we are going to talk about is the
14189
09:18:18,237 --> 09:18:19,758
reduce function
14190
09:18:19,758 --> 09:18:21,680
the only difference between reduce
14191
09:18:21,680 --> 09:18:23,122
function and the other functions which
14192
09:18:23,122 --> 09:18:24,960
we talked about
14193
09:18:24,960 --> 09:18:27,122
the map and filter is that reduce
14194
09:18:27,122 --> 09:18:30,480
function always returns a single value
14195
09:18:30,480 --> 09:18:33,122
so it combines all the
14196
09:18:33,122 --> 09:18:35,519
values in the iterable your list your
14197
09:18:35,519 --> 09:18:38,000
array your set and returns a single
14198
09:18:38,000 --> 09:18:40,640
value so you can apply the mathematical
14199
09:18:40,640 --> 09:18:42,557
functions like for example you want to
14200
09:18:42,557 --> 09:18:45,438
add all the values in your write table
14201
09:18:45,438 --> 09:18:47,360
or you want to multiply so it will
14202
09:18:47,360 --> 09:18:49,839
multiply sequentially and give you the
14203
09:18:49,839 --> 09:18:51,199
output
14204
09:18:51,199 --> 09:18:53,040
and the same
14205
09:18:53,040 --> 09:18:55,199
concept applies over here you can have
14206
09:18:55,199 --> 09:18:58,237
lambda functions or explicit functions
14207
09:18:58,237 --> 09:18:59,917
also with one thing which you need to
14208
09:18:59,917 --> 09:19:02,640
remember is that you need to use
14209
09:19:02,640 --> 09:19:04,557
function tools module
14210
09:19:04,557 --> 09:19:07,598
to to import the reduce function
14211
09:19:07,598 --> 09:19:10,078
so we will take a look at that while we
14212
09:19:10,078 --> 09:19:13,520
will be running certain examples
14213
09:19:19,120 --> 09:19:20,400
okay
14214
09:19:20,400 --> 09:19:22,960
so here we have so we will start by
14215
09:19:22,960 --> 09:19:25,680
running our examples so first we will
14216
09:19:25,680 --> 09:19:29,320
start with map
14217
09:19:34,160 --> 09:19:37,436
so let's start by
14218
09:19:37,436 --> 09:19:40,160
defining a very simple function
14219
09:19:40,160 --> 09:19:43,120
which adds
14220
09:19:44,480 --> 09:19:46,400
two values
14221
09:19:46,400 --> 09:19:48,400
right and returns you the added value
14222
09:19:48,400 --> 09:19:49,680
right
14223
09:19:49,680 --> 09:19:52,640
so in the next step
14224
09:19:53,360 --> 09:19:55,200
we will define a
14225
09:19:55,200 --> 09:19:57,200
list of iterables so here we have taken
14226
09:19:57,200 --> 09:19:59,680
an iterable list of
14227
09:19:59,680 --> 09:20:03,840
four items four six eight nine numbers
14228
09:20:05,520 --> 09:20:06,480
now
14229
09:20:06,480 --> 09:20:09,520
in this step we will apply the map
14230
09:20:09,520 --> 09:20:11,040
function now
14231
09:20:11,040 --> 09:20:13,596
please notice that we are sending two
14232
09:20:13,596 --> 09:20:15,596
parameters to this map function one is
14233
09:20:15,596 --> 09:20:16,400
the
14234
09:20:16,400 --> 09:20:19,200
function name which is add and the
14235
09:20:19,200 --> 09:20:22,320
list which is numbers
14236
09:20:24,720 --> 09:20:25,680
okay
14237
09:20:25,680 --> 09:20:27,680
now i'll print
14238
09:20:27,680 --> 09:20:30,720
the out
14239
09:20:30,720 --> 09:20:33,040
but it's an object so right it's not in
14240
09:20:33,040 --> 09:20:35,916
a readable format so i need to convert
14241
09:20:35,916 --> 09:20:41,160
the out object back to a list
14242
09:20:44,080 --> 09:20:48,400
and now i'll be able to print
14243
09:20:56,480 --> 09:20:59,040
and here is my expected output so i had
14244
09:20:59,040 --> 09:21:01,360
four numbers in my list which is four
14245
09:21:01,360 --> 09:21:04,240
six eight time and the map function have
14246
09:21:04,240 --> 09:21:07,120
has added the numbers to itself four
14247
09:21:07,120 --> 09:21:09,520
plus four six plus six eight plus eight
14248
09:21:09,520 --> 09:21:11,680
nine plus nine and so that's how you're
14249
09:21:11,680 --> 09:21:14,600
getting the output as expected
14250
09:21:14,600 --> 09:21:16,000
[Music]
14251
09:21:16,000 --> 09:21:20,240
now let's take a look at another example
14252
09:21:23,040 --> 09:21:25,360
we're going to redefine our list
14253
09:21:25,360 --> 09:21:28,000
and we are going to use now a lambda
14254
09:21:28,000 --> 09:21:28,960
function
14255
09:21:28,960 --> 09:21:31,596
instead of an explicit function which is
14256
09:21:31,596 --> 09:21:33,120
going to multiply
14257
09:21:33,120 --> 09:21:34,800
the numbers in my list
14258
09:21:34,800 --> 09:21:37,916
within with themselves
14259
09:21:38,480 --> 09:21:39,276
and
14260
09:21:39,276 --> 09:21:41,916
we're gonna
14261
09:21:44,720 --> 09:21:47,436
uh print my out object
14262
09:21:47,436 --> 09:21:50,720
convert it back to a list
14263
09:21:50,720 --> 09:21:53,680
and then i'll print
14264
09:21:53,680 --> 09:21:56,880
converted list object
14265
09:21:59,436 --> 09:22:02,000
and here you go this is my output 4
14266
09:22:02,000 --> 09:22:05,200
multiplied by 4 6 multiplied by 6 8
14267
09:22:05,200 --> 09:22:09,436
multiplied by 8 and 9 multiplied by 9.
14268
09:22:09,436 --> 09:22:10,400
okay
14269
09:22:10,400 --> 09:22:13,596
so here in this example we have seen
14270
09:22:13,596 --> 09:22:15,276
about lambda function
14271
09:22:15,276 --> 09:22:16,160
now
14272
09:22:16,160 --> 09:22:18,400
we have been up performing mathematical
14273
09:22:18,400 --> 09:22:21,040
operation as i mentioned earlier we can
14274
09:22:21,040 --> 09:22:24,080
also perform certain string operations
14275
09:22:24,080 --> 09:22:27,520
so let's take an example
14276
09:22:28,000 --> 09:22:29,916
what i'm doing in this example is i'm
14277
09:22:29,916 --> 09:22:32,960
creating a map and i'm just converting
14278
09:22:32,960 --> 09:22:37,640
all my lowercase strings to uppercase
14279
09:22:38,400 --> 09:22:41,916
and i'm just printing my list now
14280
09:22:41,916 --> 09:22:44,640
after conversion
14281
09:22:44,640 --> 09:22:47,276
so such kind of uh you know
14282
09:22:47,276 --> 09:22:51,120
string operations are also possible
14283
09:22:51,120 --> 09:22:53,436
let's take another example where we are
14284
09:22:53,436 --> 09:22:55,436
going to print the length
14285
09:22:55,436 --> 09:22:57,360
of each of the
14286
09:22:57,360 --> 09:22:59,916
string which is there in my list
14287
09:22:59,916 --> 09:23:04,120
and i'm using lambda function
14288
09:23:08,640 --> 09:23:10,400
and here it is
14289
09:23:10,400 --> 09:23:13,120
so now you can see the list
14290
09:23:13,120 --> 09:23:15,360
the length of each and every string
14291
09:23:15,360 --> 09:23:19,520
variable is 3 5 6 and seven
14292
09:23:19,520 --> 09:23:20,480
okay
14293
09:23:20,480 --> 09:23:22,880
so now these were the examples of map
14294
09:23:22,880 --> 09:23:23,840
object
14295
09:23:23,840 --> 09:23:25,680
map function now we will
14296
09:23:25,680 --> 09:23:28,240
talk about
14297
09:23:30,480 --> 09:23:33,480
filter
14298
09:23:34,080 --> 09:23:36,000
so let's start with a simple example so
14299
09:23:36,000 --> 09:23:38,560
as i mentioned the filter function
14300
09:23:38,560 --> 09:23:41,200
should help always have a condition
14301
09:23:41,200 --> 09:23:43,596
so let's create a
14302
09:23:43,596 --> 09:23:46,640
function which has a condition saying if
14303
09:23:46,640 --> 09:23:49,520
a is greater than 5 then return a
14304
09:23:49,520 --> 09:23:52,320
it means that you need
14305
09:23:52,320 --> 09:23:54,800
to only return numbers which are greater
14306
09:23:54,800 --> 09:23:58,480
than 5 in my list
14307
09:23:58,480 --> 09:24:00,480
so now i'm going to call this in my
14308
09:24:00,480 --> 09:24:03,276
filter function
14309
09:24:03,276 --> 09:24:05,200
so if you see the syntax here please
14310
09:24:05,200 --> 09:24:08,640
notice filter you pass the fill and the
14311
09:24:08,640 --> 09:24:10,160
list
14312
09:24:10,160 --> 09:24:13,520
and then you print the out object after
14313
09:24:13,520 --> 09:24:15,360
converting into the list
14314
09:24:15,360 --> 09:24:17,360
and only the numbers which are greater
14315
09:24:17,360 --> 09:24:20,960
than 5 are printed
14316
09:24:21,200 --> 09:24:23,520
now let's take a look at
14317
09:24:23,520 --> 09:24:25,596
a lambda function example rather than an
14318
09:24:25,596 --> 09:24:27,200
explicit function
14319
09:24:27,200 --> 09:24:30,240
the same example but now instead i am
14320
09:24:30,240 --> 09:24:33,360
using a lambda function
14321
09:24:33,916 --> 09:24:35,520
and i am going to print it
14322
09:24:35,520 --> 09:24:38,560
output should be the same
14323
09:24:38,880 --> 09:24:39,840
okay
14324
09:24:39,840 --> 09:24:40,720
so the
14325
09:24:40,720 --> 09:24:43,120
the only change is in the syntax of the
14326
09:24:43,120 --> 09:24:45,040
function definition here we have
14327
09:24:45,040 --> 09:24:47,360
explicit function and here we have a
14328
09:24:47,360 --> 09:24:51,276
lambda function output is the same
14329
09:24:51,276 --> 09:24:54,400
now let's take an example of
14330
09:24:54,400 --> 09:24:57,040
a string operation string filter what
14331
09:24:57,040 --> 09:24:59,436
kind of string filtering we can do
14332
09:24:59,436 --> 09:25:00,160
so
14333
09:25:00,160 --> 09:25:03,756
i have an example over here
14334
09:25:04,480 --> 09:25:06,400
so what i'm going to do is i'm going to
14335
09:25:06,400 --> 09:25:09,200
have a list of string arrays
14336
09:25:09,200 --> 09:25:11,596
which is going to return me only those
14337
09:25:11,596 --> 09:25:14,080
strings which have e in them occurrence
14338
09:25:14,080 --> 09:25:15,040
of e
14339
09:25:15,040 --> 09:25:17,360
okay
14340
09:25:19,436 --> 09:25:22,160
so i've created a function veggies with
14341
09:25:22,160 --> 09:25:23,040
e
14342
09:25:23,040 --> 09:25:26,240
and within this list of veggies peas
14343
09:25:26,240 --> 09:25:28,960
garlic ginger beetroot carrot i will
14344
09:25:28,960 --> 09:25:31,756
only get those string variable values
14345
09:25:31,756 --> 09:25:35,880
which have e in it
14346
09:25:39,276 --> 09:25:41,756
okay so i've got the result p is ginger
14347
09:25:41,756 --> 09:25:43,040
and beetroot
14348
09:25:43,040 --> 09:25:46,000
right and garlic and carrot have been
14349
09:25:46,000 --> 09:25:48,640
discarded
14350
09:25:49,276 --> 09:25:52,480
now let's run this similar example with
14351
09:25:52,480 --> 09:25:54,000
lambda function
14352
09:25:54,000 --> 09:25:56,320
where i'm saying that okay give me those
14353
09:25:56,320 --> 09:25:59,040
vegetables which have r in it
14354
09:25:59,040 --> 09:26:01,916
so i'm just gonna run this
14355
09:26:01,916 --> 09:26:03,916
and print my
14356
09:26:03,916 --> 09:26:05,070
map
14357
09:26:05,070 --> 09:26:08,199
[Music]
14358
09:26:08,400 --> 09:26:10,480
okay and here is my expected output
14359
09:26:10,480 --> 09:26:13,276
garlic ginger beet root and carrot
14360
09:26:13,276 --> 09:26:15,520
so what have we noticed is that similar
14361
09:26:15,520 --> 09:26:17,436
to map filter function of course the
14362
09:26:17,436 --> 09:26:19,120
only difference is the function should
14363
09:26:19,120 --> 09:26:20,400
have a condition
14364
09:26:20,400 --> 09:26:22,160
which should return a true or false and
14365
09:26:22,160 --> 09:26:24,720
based on that the iterable list get
14366
09:26:24,720 --> 09:26:26,960
filters out
14367
09:26:26,960 --> 09:26:28,320
now
14368
09:26:28,320 --> 09:26:29,756
let's talk about
14369
09:26:29,756 --> 09:26:32,756
reduce
14370
09:26:37,040 --> 09:26:38,560
now as i mentioned
14371
09:26:38,560 --> 09:26:41,040
the first important thing is that
14372
09:26:41,040 --> 09:26:43,276
we
14373
09:26:43,596 --> 09:26:45,680
import the reduce function from function
14374
09:26:45,680 --> 09:26:47,596
tools okay
14375
09:26:47,596 --> 09:26:49,360
and then
14376
09:26:49,360 --> 09:26:52,560
we apply a
14377
09:26:52,560 --> 09:26:54,240
lambda function so we're going to start
14378
09:26:54,240 --> 09:26:55,680
off with just taking an example of
14379
09:26:55,680 --> 09:26:56,720
lambda
14380
09:26:56,720 --> 09:26:59,040
so here is my list of four elements 10
14381
09:26:59,040 --> 09:27:03,040
35 50 and 20 and i'm adding
14382
09:27:03,040 --> 09:27:06,480
the numbers in this list okay so so the
14383
09:27:06,480 --> 09:27:08,880
map function is not gonna the reduce
14384
09:27:08,880 --> 09:27:10,560
function is not going to add the numbers
14385
09:27:10,560 --> 09:27:14,160
to itself but to the subsequent number
14386
09:27:14,160 --> 09:27:15,756
in the list right
14387
09:27:15,756 --> 09:27:18,000
so what is the expected output right
14388
09:27:18,000 --> 09:27:20,160
expected output is 10
14389
09:27:20,160 --> 09:27:22,400
plus 35 45
14390
09:27:22,400 --> 09:27:26,880
plus 50 95 plus 20 115 so
14391
09:27:26,880 --> 09:27:28,880
what the reduce function has done is it
14392
09:27:28,880 --> 09:27:31,840
has added the numbers in the list
14393
09:27:31,840 --> 09:27:34,160
amongst themselves right in the orderly
14394
09:27:34,160 --> 09:27:36,560
fashion
14395
09:27:39,200 --> 09:27:41,520
now let's take another example instead
14396
09:27:41,520 --> 09:27:45,276
of addition we'll do a multiplication
14397
09:27:45,276 --> 09:27:47,276
okay
14398
09:27:47,276 --> 09:27:48,640
so here what we have done is we have
14399
09:27:48,640 --> 09:27:52,000
multiplied 9 by into 7 into 23 into 4
14400
09:27:52,000 --> 09:27:53,916
and we've got this answer
14401
09:27:53,916 --> 09:27:58,520
so this is another way of doing it
14402
09:28:01,120 --> 09:28:03,040
now instead of lambda function i'm just
14403
09:28:03,040 --> 09:28:05,436
going to show you a simple
14404
09:28:05,436 --> 09:28:06,960
um
14405
09:28:06,960 --> 09:28:09,436
adding the numbers
14406
09:28:09,436 --> 09:28:11,360
and define a function where you add the
14407
09:28:11,360 --> 09:28:14,960
first and second number
14408
09:28:15,276 --> 09:28:17,520
and then
14409
09:28:17,520 --> 09:28:20,880
you pass the function name
14410
09:28:21,756 --> 09:28:24,320
to reduce so custom sum and the list
14411
09:28:24,320 --> 09:28:26,240
similar to what we had done for map and
14412
09:28:26,240 --> 09:28:27,916
filter
14413
09:28:27,916 --> 09:28:31,436
and now you can print the result
14414
09:28:32,080 --> 09:28:33,680
now one thing to notice is that this
14415
09:28:33,680 --> 09:28:35,520
result is a single number you don't need
14416
09:28:35,520 --> 09:28:37,756
to convert it back to a list so you
14417
09:28:37,756 --> 09:28:39,520
don't need to do that transformation you
14418
09:28:39,520 --> 09:28:40,560
just
14419
09:28:40,560 --> 09:28:43,840
simply print the result
14420
09:28:44,080 --> 09:28:46,960
and here it is 68 3 plus 4 plus 6
14421
09:28:46,960 --> 09:28:50,240
plus 9 plus 34 plus 12.
14422
09:28:50,240 --> 09:28:52,160
so what is the class in python
14423
09:28:52,160 --> 09:28:53,840
a class is a virtual entity or a
14424
09:28:53,840 --> 09:28:56,880
blueprint for creating objects
14425
09:28:56,880 --> 09:28:58,800
a class in python provides a means of
14426
09:28:58,800 --> 09:29:01,120
bundling data and functionality together
14427
09:29:01,120 --> 09:29:03,276
you can think of a class
14428
09:29:03,276 --> 09:29:05,840
as a school so here school becomes your
14429
09:29:05,840 --> 09:29:08,800
class a school has attributes about the
14430
09:29:08,800 --> 09:29:12,720
students classrooms labs playgrounds etc
14431
09:29:12,720 --> 09:29:14,480
hence the school can be called a class
14432
09:29:14,480 --> 09:29:17,276
and classrooms labs desks playground
14433
09:29:17,276 --> 09:29:19,436
assembly are the attributes or objects
14434
09:29:19,436 --> 09:29:20,560
of a class
14435
09:29:20,560 --> 09:29:23,360
a class comes into existence when it is
14436
09:29:23,360 --> 09:29:25,916
instantiated or initiated
14437
09:29:25,916 --> 09:29:27,596
we can create as many objects as
14438
09:29:27,596 --> 09:29:30,240
possible in a class now in python a
14439
09:29:30,240 --> 09:29:31,916
class is created using the keyword
14440
09:29:31,916 --> 09:29:33,436
called class
14441
09:29:33,436 --> 09:29:36,240
followed by the class name the first
14442
09:29:36,240 --> 09:29:38,160
string inside the class is called
14443
09:29:38,160 --> 09:29:40,880
docstring and has a brief description of
14444
09:29:40,880 --> 09:29:42,000
the class
14445
09:29:42,000 --> 09:29:44,400
below you can see the syntax of creating
14446
09:29:44,400 --> 09:29:46,720
a class in python we have the class
14447
09:29:46,720 --> 09:29:48,960
keyword followed by the class name this
14448
09:29:48,960 --> 09:29:52,640
should be a valid python name and then
14449
09:29:52,640 --> 09:29:54,240
you have a colon
14450
09:29:54,240 --> 09:29:56,480
below that you have the statements that
14451
09:29:56,480 --> 09:29:58,560
should be present inside the class
14452
09:29:58,560 --> 09:30:01,436
now let's look at an example of a class
14453
09:30:01,436 --> 09:30:02,840
in python
14454
09:30:02,840 --> 09:30:06,080
so here you can see we have created a
14455
09:30:06,080 --> 09:30:07,596
class called student
14456
09:30:07,596 --> 09:30:10,640
and we have two variables the student id
14457
09:30:10,640 --> 09:30:12,880
and the name of the student
14458
09:30:12,880 --> 09:30:14,480
and after that we have created a
14459
09:30:14,480 --> 09:30:16,560
function using the def keyword so this
14460
09:30:16,560 --> 09:30:18,160
is a user defined function and the name
14461
09:30:18,160 --> 09:30:20,400
of the function is called display
14462
09:30:20,400 --> 09:30:21,840
and inside
14463
09:30:21,840 --> 09:30:23,680
the display function we are
14464
09:30:23,680 --> 09:30:26,000
giving a parameter called self so here
14465
09:30:26,000 --> 09:30:28,160
the self parameter refers to the
14466
09:30:28,160 --> 09:30:30,160
current class object and accesses the
14467
09:30:30,160 --> 09:30:32,240
class variables we can use anything
14468
09:30:32,240 --> 09:30:33,596
instead of self
14469
09:30:33,596 --> 09:30:34,880
but
14470
09:30:34,880 --> 09:30:36,720
it must be the first parameter of any
14471
09:30:36,720 --> 09:30:39,276
function which belongs to the class
14472
09:30:39,276 --> 09:30:41,276
now moving ahead
14473
09:30:41,276 --> 09:30:43,040
so let's look at what is an object in
14474
09:30:43,040 --> 09:30:44,400
python
14475
09:30:44,400 --> 09:30:46,400
so an object is also called an instance
14476
09:30:46,400 --> 09:30:48,240
of a class and the process of creating
14477
09:30:48,240 --> 09:30:51,120
this object is called instantiation
14478
09:30:51,120 --> 09:30:52,720
now you need to create an object of the
14479
09:30:52,720 --> 09:30:54,240
class if you want to use the class
14480
09:30:54,240 --> 09:30:56,640
attributes in another class or method
14481
09:30:56,640 --> 09:30:58,960
now the procedure to create an object is
14482
09:30:58,960 --> 09:31:01,680
similar to a function call so
14483
09:31:01,680 --> 09:31:03,680
below you can see we have created a
14484
09:31:03,680 --> 09:31:05,916
object called stu and we have called the
14485
09:31:05,916 --> 09:31:08,240
class student using parenthesis so this
14486
09:31:08,240 --> 09:31:10,000
is how you create a class and after that
14487
09:31:10,000 --> 09:31:12,000
we are displaying
14488
09:31:12,000 --> 09:31:15,040
the message to be printed by calling the
14489
09:31:15,040 --> 09:31:17,680
class name followed by the function
14490
09:31:17,680 --> 09:31:20,240
which is present inside the class
14491
09:31:20,240 --> 09:31:22,800
so this example will create a new object
14492
09:31:22,800 --> 09:31:25,120
instance named stu
14493
09:31:25,120 --> 09:31:26,640
we can access the attributes of the
14494
09:31:26,640 --> 09:31:29,916
objects using the object name prefix
14495
09:31:29,916 --> 09:31:31,436
now moving ahead
14496
09:31:31,436 --> 09:31:33,200
let's understand the initialization or
14497
09:31:33,200 --> 09:31:35,360
init method in a python class
14498
09:31:35,360 --> 09:31:36,640
the init method is similar to
14499
09:31:36,640 --> 09:31:39,520
constructors in c plus and java
14500
09:31:39,520 --> 09:31:41,436
all classes in python have an init
14501
09:31:41,436 --> 09:31:43,520
function it is always executed when the
14502
09:31:43,520 --> 09:31:46,480
class has been initiated you can use the
14503
09:31:46,480 --> 09:31:48,560
init function to assign values to object
14504
09:31:48,560 --> 09:31:50,560
properties or other operations that are
14505
09:31:50,560 --> 09:31:52,160
necessary to do when the object has been
14506
09:31:52,160 --> 09:31:53,520
created
14507
09:31:53,520 --> 09:31:54,880
so the init function is called
14508
09:31:54,880 --> 09:31:56,560
automatically every time the class has
14509
09:31:56,560 --> 09:31:58,640
been used to create a new object
14510
09:31:58,640 --> 09:32:01,040
so here you can see we have used
14511
09:32:01,040 --> 09:32:03,520
the init method we have
14512
09:32:03,520 --> 09:32:06,640
defined using the def keyword then we
14513
09:32:06,640 --> 09:32:09,520
have parameter self name and age
14514
09:32:09,520 --> 09:32:11,916
so we are calling
14515
09:32:11,916 --> 09:32:12,800
the
14516
09:32:12,800 --> 09:32:15,200
class using this object called s1 and
14517
09:32:15,200 --> 09:32:16,640
then we have passed in the values for
14518
09:32:16,640 --> 09:32:20,480
neiman age that is delay and his age is
14519
09:32:20,480 --> 09:32:22,720
27 years and after that we are printing
14520
09:32:22,720 --> 09:32:25,120
the age and the name as well
14521
09:32:25,120 --> 09:32:27,360
all right now let's get started with our
14522
09:32:27,360 --> 09:32:28,400
demo on
14523
09:32:28,400 --> 09:32:29,916
classes and objects in python on the
14524
09:32:29,916 --> 09:32:31,596
jupyter notebook
14525
09:32:31,596 --> 09:32:33,360
so i am on my jupyter notebook let me
14526
09:32:33,360 --> 09:32:34,960
first go ahead and
14527
09:32:34,960 --> 09:32:37,200
rename my notebook to classes and
14528
09:32:37,200 --> 09:32:39,840
objects in python
14529
09:32:41,120 --> 09:32:43,756
all right i'll click on rename
14530
09:32:43,756 --> 09:32:45,756
first of all
14531
09:32:45,756 --> 09:32:48,400
let me show you how to create a simple
14532
09:32:48,400 --> 09:32:49,916
class
14533
09:32:49,916 --> 09:32:52,560
so i'm creating a new class
14534
09:32:52,560 --> 09:32:55,596
using the class keyword so this is
14535
09:32:55,596 --> 09:32:58,000
the keyword class and
14536
09:32:58,000 --> 09:33:00,160
followed by the keyword i have my class
14537
09:33:00,160 --> 09:33:02,480
name called new class
14538
09:33:02,480 --> 09:33:04,880
one thing to note here is the
14539
09:33:04,880 --> 09:33:07,680
class name should always be a valid
14540
09:33:07,680 --> 09:33:09,520
python name
14541
09:33:09,520 --> 09:33:11,680
i'll give a colon and in the next line
14542
09:33:11,680 --> 09:33:13,276
i'll just say
14543
09:33:13,276 --> 09:33:14,880
x equal to 50
14544
09:33:14,880 --> 09:33:16,880
so this x is a variable which is present
14545
09:33:16,880 --> 09:33:18,080
inside my
14546
09:33:18,080 --> 09:33:21,756
class called new class now if i run it
14547
09:33:21,756 --> 09:33:24,640
i have my new class created now if you
14548
09:33:24,640 --> 09:33:26,640
want to display the
14549
09:33:26,640 --> 09:33:29,680
value of x you can just say new class
14550
09:33:29,680 --> 09:33:32,880
dot x now if i run this you can see here
14551
09:33:32,880 --> 09:33:36,640
i have my value printed which is 50.
14552
09:33:36,640 --> 09:33:37,916
now
14553
09:33:37,916 --> 09:33:39,916
we'll see another simple example to
14554
09:33:39,916 --> 09:33:42,480
create a class and to call that class
14555
09:33:42,480 --> 09:33:44,560
using
14556
09:33:44,560 --> 09:33:46,640
an object
14557
09:33:46,640 --> 09:33:48,640
so i'll say class
14558
09:33:48,640 --> 09:33:50,400
then i'll say
14559
09:33:50,400 --> 09:33:53,276
new class let's say 1
14560
09:33:53,276 --> 09:33:54,560
give a colon
14561
09:33:54,560 --> 09:33:56,400
and i'll create a variable called x
14562
09:33:56,400 --> 09:33:58,800
equal to 100 so my x is a variable which
14563
09:33:58,800 --> 09:34:01,276
is present inside new class 1 and holds
14564
09:34:01,276 --> 09:34:03,916
a value 100
14565
09:34:03,916 --> 09:34:06,640
now i'll give a comment
14566
09:34:06,640 --> 09:34:09,276
create object
14567
09:34:09,276 --> 09:34:12,720
so to create object i'll
14568
09:34:12,720 --> 09:34:15,040
define a variable called r let's say r
14569
09:34:15,040 --> 09:34:17,040
stands for result and then
14570
09:34:17,040 --> 09:34:18,240
i'll say
14571
09:34:18,240 --> 09:34:20,880
new class 1 so i'm calling my class
14572
09:34:20,880 --> 09:34:23,120
and then i'll say print
14573
09:34:23,120 --> 09:34:25,840
r which is my new object dot
14574
09:34:25,840 --> 09:34:26,960
x
14575
09:34:26,960 --> 09:34:30,160
let's run it you see here i have my
14576
09:34:30,160 --> 09:34:33,756
value printed of x which is 100 so this
14577
09:34:33,756 --> 09:34:35,436
is how you can
14578
09:34:35,436 --> 09:34:37,756
create or initiate a class using an
14579
09:34:37,756 --> 09:34:38,960
object
14580
09:34:38,960 --> 09:34:41,040
now let's see how you can use the self
14581
09:34:41,040 --> 09:34:43,596
parameter in a class
14582
09:34:43,596 --> 09:34:45,360
so i'll go ahead and create a class
14583
09:34:45,360 --> 09:34:46,560
called
14584
09:34:46,560 --> 09:34:48,400
student
14585
09:34:48,400 --> 09:34:49,916
give a colon
14586
09:34:49,916 --> 09:34:52,480
then i'll have some variables
14587
09:34:52,480 --> 09:34:54,000
or the
14588
09:34:54,000 --> 09:34:55,596
attributes of a student let's say the
14589
09:34:55,596 --> 09:34:59,120
student id is 10 and the name is
14590
09:34:59,120 --> 09:35:01,680
anderson
14591
09:35:02,240 --> 09:35:03,436
now
14592
09:35:03,436 --> 09:35:06,480
inside the class i'm going to
14593
09:35:06,480 --> 09:35:08,560
create a user defined function called
14594
09:35:08,560 --> 09:35:10,720
display
14595
09:35:10,720 --> 09:35:13,200
so i've used my df keyword followed by
14596
09:35:13,200 --> 09:35:14,160
the
14597
09:35:14,160 --> 09:35:16,240
function name that is display and inside
14598
09:35:16,240 --> 09:35:18,960
the display function i'll use self as my
14599
09:35:18,960 --> 09:35:20,800
parameter
14600
09:35:20,800 --> 09:35:22,800
and then i'm going to say
14601
09:35:22,800 --> 09:35:24,480
print
14602
09:35:24,480 --> 09:35:27,436
self dot id i'll give a comma
14603
09:35:27,436 --> 09:35:29,360
and i'm going to print
14604
09:35:29,360 --> 09:35:33,276
my name so i've written self dot name
14605
09:35:33,276 --> 09:35:34,720
so i'll run this
14606
09:35:34,720 --> 09:35:36,320
and then
14607
09:35:36,320 --> 09:35:38,800
we're going to create an object called s
14608
09:35:38,800 --> 09:35:41,680
and we'll call our class
14609
09:35:41,680 --> 09:35:44,000
that is student and i'll say s dot
14610
09:35:44,000 --> 09:35:46,480
display
14611
09:35:48,160 --> 09:35:50,160
now let's run it and see the result you
14612
09:35:50,160 --> 09:35:51,436
can see it here
14613
09:35:51,436 --> 09:35:54,640
we have called the
14614
09:35:54,640 --> 09:35:57,436
class using this object called s so it's
14615
09:35:57,436 --> 09:35:58,800
displaying
14616
09:35:58,800 --> 09:36:01,916
the id that is 10 and the name that is
14617
09:36:01,916 --> 09:36:03,276
anderson
14618
09:36:03,276 --> 09:36:05,120
cool
14619
09:36:05,120 --> 09:36:07,756
now we are going to see an example where
14620
09:36:07,756 --> 09:36:09,916
we'll pass a dog string so in the slide
14621
09:36:09,916 --> 09:36:12,320
we discussed that the first
14622
09:36:12,320 --> 09:36:14,160
string inside the class is called a dog
14623
09:36:14,160 --> 09:36:16,320
string and has a brief description of
14624
09:36:16,320 --> 09:36:17,596
the class
14625
09:36:17,596 --> 09:36:19,680
so let me show you an example how to
14626
09:36:19,680 --> 09:36:21,360
pass in a doc string
14627
09:36:21,360 --> 09:36:23,360
i'll create a class again let's say
14628
09:36:23,360 --> 09:36:25,360
called student
14629
09:36:25,360 --> 09:36:26,880
give a colon
14630
09:36:26,880 --> 09:36:29,680
and then we'll have our message
14631
09:36:29,680 --> 09:36:31,520
which is essentially a doc string i'll
14632
09:36:31,520 --> 09:36:33,120
say this is a
14633
09:36:33,120 --> 09:36:35,040
student class
14634
09:36:35,040 --> 09:36:37,120
then i'll create a variable called age
14635
09:36:37,120 --> 09:36:39,916
let's say the student's age is 14
14636
09:36:39,916 --> 09:36:41,120
and then
14637
09:36:41,120 --> 09:36:42,800
i'll create a user defined function
14638
09:36:42,800 --> 09:36:45,120
called welcome
14639
09:36:45,120 --> 09:36:46,000
and
14640
09:36:46,000 --> 09:36:47,840
have my self
14641
09:36:47,840 --> 09:36:49,200
parameter
14642
09:36:49,200 --> 09:36:51,596
after that i'll say print
14643
09:36:51,596 --> 09:36:54,000
my message let's say hi
14644
09:36:54,000 --> 09:36:55,840
comma
14645
09:36:55,840 --> 09:36:58,400
welcome to
14646
09:37:00,400 --> 09:37:01,840
section
14647
09:37:01,840 --> 09:37:04,720
let's say b
14648
09:37:04,720 --> 09:37:07,120
and then
14649
09:37:07,120 --> 09:37:09,120
i'll come out of the class and we are
14650
09:37:09,120 --> 09:37:11,520
going to
14651
09:37:11,756 --> 09:37:14,080
print
14652
09:37:16,320 --> 09:37:18,000
class name that is student i'll say
14653
09:37:18,000 --> 09:37:19,840
student dot age
14654
09:37:19,840 --> 09:37:22,960
and then i'll say print i'll just scroll
14655
09:37:22,960 --> 09:37:25,120
down
14656
09:37:25,120 --> 09:37:27,916
student dot
14657
09:37:28,080 --> 09:37:30,400
then i'm going to use the
14658
09:37:30,400 --> 09:37:33,120
function name that is welcome
14659
09:37:33,120 --> 09:37:34,480
and after that
14660
09:37:34,480 --> 09:37:36,240
to print the
14661
09:37:36,240 --> 09:37:39,596
docstring message i'm going to say
14662
09:37:39,596 --> 09:37:42,880
my class name that is student
14663
09:37:42,880 --> 09:37:44,480
dot i'm going to use underscore
14664
09:37:44,480 --> 09:37:48,160
underscore doc underscore underscore
14665
09:37:48,160 --> 09:37:50,000
now we have created a class called
14666
09:37:50,000 --> 09:37:52,480
student that has a variable called age
14667
09:37:52,480 --> 09:37:54,000
so we are going to display the age of
14668
09:37:54,000 --> 09:37:56,160
the student and then
14669
09:37:56,160 --> 09:37:57,916
we are going to display the
14670
09:37:57,916 --> 09:38:00,320
dot string so here we are not going to
14671
09:38:00,320 --> 09:38:02,240
print the message which is present
14672
09:38:02,240 --> 09:38:04,720
inside the function called welcome
14673
09:38:04,720 --> 09:38:06,080
let's run it
14674
09:38:06,080 --> 09:38:07,840
you can see it here
14675
09:38:07,840 --> 09:38:09,680
we
14676
09:38:09,680 --> 09:38:11,520
have the message printed so first we are
14677
09:38:11,520 --> 09:38:13,200
printing the age of the student and then
14678
09:38:13,200 --> 09:38:15,596
we have the doctrine message that is
14679
09:38:15,596 --> 09:38:17,756
this is a student class
14680
09:38:17,756 --> 09:38:19,596
all right
14681
09:38:19,596 --> 09:38:21,840
now let's move ahead we are going to see
14682
09:38:21,840 --> 09:38:23,200
how to use the
14683
09:38:23,200 --> 09:38:24,880
init function
14684
09:38:24,880 --> 09:38:26,720
in a class
14685
09:38:26,720 --> 09:38:31,596
so i'll say my comment init function
14686
09:38:32,160 --> 09:38:33,120
all right
14687
09:38:33,120 --> 09:38:35,916
i'll scroll down
14688
09:38:36,080 --> 09:38:37,756
i'll create another
14689
09:38:37,756 --> 09:38:39,756
class called student
14690
09:38:39,756 --> 09:38:42,240
this time we are going to use the init
14691
09:38:42,240 --> 09:38:44,800
function so i am writing def
14692
09:38:44,800 --> 09:38:45,916
space
14693
09:38:45,916 --> 09:38:47,840
underscore underscore init
14694
09:38:47,840 --> 09:38:49,596
underscore underscore
14695
09:38:49,596 --> 09:38:52,000
then we'll have our self parameter let's
14696
09:38:52,000 --> 09:38:54,000
say the next parameter is
14697
09:38:54,000 --> 09:38:54,880
name
14698
09:38:54,880 --> 09:38:57,200
and then we have the date of birth of
14699
09:38:57,200 --> 09:38:58,240
the
14700
09:38:58,240 --> 09:39:00,480
student
14701
09:39:00,480 --> 09:39:02,000
i'll say self
14702
09:39:02,000 --> 09:39:02,960
dot
14703
09:39:02,960 --> 09:39:06,240
name equal to
14704
09:39:06,960 --> 09:39:09,960
name
14705
09:39:10,400 --> 09:39:11,916
then i'll say
14706
09:39:11,916 --> 09:39:13,840
self dot
14707
09:39:13,840 --> 09:39:16,240
dob or d to birth equal to
14708
09:39:16,240 --> 09:39:17,840
a parameter that is
14709
09:39:17,840 --> 09:39:19,680
dob
14710
09:39:19,680 --> 09:39:21,520
and then
14711
09:39:21,520 --> 09:39:23,200
i'll say
14712
09:39:23,200 --> 09:39:26,400
my object let's say s1
14713
09:39:26,400 --> 09:39:29,200
i'm going to call my class student
14714
09:39:29,200 --> 09:39:31,360
and it will have
14715
09:39:31,360 --> 09:39:33,200
values of name and date of birth let's
14716
09:39:33,200 --> 09:39:36,320
say the name of the student is harry
14717
09:39:36,320 --> 09:39:37,680
and
14718
09:39:37,680 --> 09:39:41,120
the date of birth let's say it is
14719
09:39:41,120 --> 09:39:43,436
29
14720
09:39:43,436 --> 09:39:44,560
october
14721
09:39:44,560 --> 09:39:47,436
1992
14722
09:39:47,680 --> 09:39:49,200
all right
14723
09:39:49,200 --> 09:39:53,040
finally we are going to print
14724
09:39:53,040 --> 09:39:54,720
s1 dot name
14725
09:39:54,720 --> 09:39:58,560
and then i'll say print
14726
09:39:58,880 --> 09:40:00,640
s1 dot
14727
09:40:00,640 --> 09:40:02,560
date of birth
14728
09:40:02,560 --> 09:40:04,000
let's run it
14729
09:40:04,000 --> 09:40:06,320
you can see it here we have the name and
14730
09:40:06,320 --> 09:40:09,276
the date of birth of the student printed
14731
09:40:09,276 --> 09:40:10,320
cool
14732
09:40:10,320 --> 09:40:12,800
now you can also modify the objects that
14733
09:40:12,800 --> 09:40:15,200
you have created suppose i want to
14734
09:40:15,200 --> 09:40:16,400
change the
14735
09:40:16,400 --> 09:40:19,596
name from harry to virat because my
14736
09:40:19,596 --> 09:40:22,080
object was s1 dot name
14737
09:40:22,080 --> 09:40:24,960
harry i want to change this to virat let
14738
09:40:24,960 --> 09:40:26,880
me show you how to do it
14739
09:40:26,880 --> 09:40:28,800
this is very simple i'll just give a
14740
09:40:28,800 --> 09:40:33,360
comment that is modify objects
14741
09:40:33,756 --> 09:40:37,200
the way to do is you just
14742
09:40:37,200 --> 09:40:39,596
see the object name that is s1 followed
14743
09:40:39,596 --> 09:40:42,400
by the parameter that is name
14744
09:40:42,400 --> 09:40:43,436
equal to
14745
09:40:43,436 --> 09:40:45,436
let's say virat
14746
09:40:45,436 --> 09:40:47,840
now if i print
14747
09:40:47,840 --> 09:40:50,080
s1 dot name
14748
09:40:50,080 --> 09:40:52,800
i'll have virat instead of
14749
09:40:52,800 --> 09:40:54,240
harry
14750
09:40:54,240 --> 09:40:56,080
okay
14751
09:40:56,080 --> 09:40:57,120
now
14752
09:40:57,120 --> 09:40:58,960
if you want to delete the object
14753
09:40:58,960 --> 09:41:00,640
properties let's say i want to delete
14754
09:41:00,640 --> 09:41:02,000
one of the properties let's say i want
14755
09:41:02,000 --> 09:41:05,120
to delete name property of the student
14756
09:41:05,120 --> 09:41:06,800
to do that
14757
09:41:06,800 --> 09:41:09,200
i can use the del keyword followed by
14758
09:41:09,200 --> 09:41:11,520
the object name that is s1 and say s1
14759
09:41:11,520 --> 09:41:13,040
dot name
14760
09:41:13,040 --> 09:41:14,720
now if i print
14761
09:41:14,720 --> 09:41:16,880
s1 dot name it will throw an error
14762
09:41:16,880 --> 09:41:19,200
saying student object has no attribute
14763
09:41:19,200 --> 09:41:21,276
name because we are first deleting it
14764
09:41:21,276 --> 09:41:22,800
and then we are trying to print it and
14765
09:41:22,800 --> 09:41:24,480
hence
14766
09:41:24,480 --> 09:41:26,960
python cannot recognize this attribute
14767
09:41:26,960 --> 09:41:28,960
name
14768
09:41:28,960 --> 09:41:30,560
now you can also
14769
09:41:30,560 --> 09:41:32,960
delete objects that is all the objects
14770
09:41:32,960 --> 09:41:35,276
using the dl keyword
14771
09:41:35,276 --> 09:41:37,120
so this means we can delete both the
14772
09:41:37,120 --> 09:41:39,040
objects that we created above which is
14773
09:41:39,040 --> 09:41:40,880
name and date of birth
14774
09:41:40,880 --> 09:41:45,276
so the way to do is i'll just say dl s1
14775
09:41:45,276 --> 09:41:49,276
so it will delete all the objects
14776
09:41:49,276 --> 09:41:52,400
that is s1 in this case
14777
09:41:52,400 --> 09:41:53,680
i'll run this
14778
09:41:53,680 --> 09:41:56,080
and then let's say i want to print
14779
09:41:56,080 --> 09:41:57,520
s1 dot
14780
09:41:57,520 --> 09:42:00,240
date of birth
14781
09:42:00,400 --> 09:42:01,916
you see it here
14782
09:42:01,916 --> 09:42:04,160
python says name s1 is not defined
14783
09:42:04,160 --> 09:42:07,040
because we deleted it
14784
09:42:07,040 --> 09:42:09,360
cool
14785
09:42:09,436 --> 09:42:11,276
now coming to the final section of the
14786
09:42:11,276 --> 09:42:14,240
demo we're going to create a simple
14787
09:42:14,240 --> 09:42:17,276
class with init method again
14788
09:42:17,276 --> 09:42:19,596
so this time my class name is let's say
14789
09:42:19,596 --> 09:42:21,360
animal
14790
09:42:21,360 --> 09:42:24,400
give a colon and then i'll write my
14791
09:42:24,400 --> 09:42:26,480
init method using the
14792
09:42:26,480 --> 09:42:28,160
df keyword
14793
09:42:28,160 --> 09:42:31,756
i'll say self comma
14794
09:42:31,840 --> 09:42:33,680
name that is the name of the animal as
14795
09:42:33,680 --> 09:42:34,960
the parameter
14796
09:42:34,960 --> 09:42:39,880
i'll say self dot name equal to name
14797
09:42:41,520 --> 09:42:42,880
and then
14798
09:42:42,880 --> 09:42:44,800
i'm going to create another function
14799
09:42:44,800 --> 09:42:46,240
called tef
14800
09:42:46,240 --> 09:42:48,560
which is essentially a welcome or agreed
14801
09:42:48,560 --> 09:42:49,916
message
14802
09:42:49,916 --> 09:42:53,040
and that has a parameter that itself
14803
09:42:53,040 --> 09:42:55,436
i'm going to print the greet message or
14804
09:42:55,436 --> 09:42:56,640
the greeting
14805
09:42:56,640 --> 09:42:59,360
i'll say hi
14806
09:42:59,360 --> 09:43:01,756
my name is
14807
09:43:01,756 --> 09:43:06,880
give a comma and then say self dot name
14808
09:43:06,880 --> 09:43:09,596
now i'm going to create
14809
09:43:09,596 --> 09:43:11,010
my object a1 to
14810
09:43:11,010 --> 09:43:12,400
[Music]
14811
09:43:12,400 --> 09:43:14,560
initiate my class
14812
09:43:14,560 --> 09:43:16,160
so i'll say animal which is the class
14813
09:43:16,160 --> 09:43:20,160
name let's say the name of the animal is
14814
09:43:20,160 --> 09:43:23,160
puppy
14815
09:43:24,480 --> 09:43:27,120
then i'm going to say even dot
14816
09:43:27,120 --> 09:43:28,800
greet
14817
09:43:28,800 --> 09:43:31,520
i'll use parenthesis let's run it
14818
09:43:31,520 --> 09:43:32,640
there you go
14819
09:43:32,640 --> 09:43:35,040
you see here we have a message printed
14820
09:43:35,040 --> 09:43:36,720
hi my name is
14821
09:43:36,720 --> 09:43:37,756
then
14822
09:43:37,756 --> 09:43:40,800
using the self dot name
14823
09:43:40,800 --> 09:43:43,200
argument of the parameter we are passing
14824
09:43:43,200 --> 09:43:46,720
in the value that is poppy all right so
14825
09:43:46,720 --> 09:43:48,880
that was a demo session on
14826
09:43:48,880 --> 09:43:51,360
python classes and objects so we saw how
14827
09:43:51,360 --> 09:43:53,436
to create a simple class in python and
14828
09:43:53,436 --> 09:43:55,360
then we saw how to create
14829
09:43:55,360 --> 09:43:56,960
objects to
14830
09:43:56,960 --> 09:43:58,720
call a class
14831
09:43:58,720 --> 09:44:00,400
and then
14832
09:44:00,400 --> 09:44:01,596
we
14833
09:44:01,596 --> 09:44:04,640
saw how to use the self parameter
14834
09:44:04,640 --> 09:44:06,480
after that we saw
14835
09:44:06,480 --> 09:44:08,560
how to
14836
09:44:08,560 --> 09:44:10,480
print a message that is present in the
14837
09:44:10,480 --> 09:44:12,720
docstring of a class
14838
09:44:12,720 --> 09:44:15,360
and then we
14839
09:44:15,360 --> 09:44:17,436
checked a few examples on using the init
14840
09:44:17,436 --> 09:44:20,080
method in a class then we learned how to
14841
09:44:20,080 --> 09:44:21,360
modify
14842
09:44:21,360 --> 09:44:23,680
objects and delete objects in a class
14843
09:44:23,680 --> 09:44:26,320
and finally we saw another example
14844
09:44:26,320 --> 09:44:28,560
on init method
14845
09:44:28,560 --> 09:44:29,756
all right
14846
09:44:29,756 --> 09:44:31,360
we are going to talk about the concept
14847
09:44:31,360 --> 09:44:32,480
of inheritance
14848
09:44:32,480 --> 09:44:33,916
the types of inheritance which are
14849
09:44:33,916 --> 09:44:35,916
applicable so the different kind of
14850
09:44:35,916 --> 09:44:37,680
inheritance which we are going to talk
14851
09:44:37,680 --> 09:44:39,436
about are single level inheritance
14852
09:44:39,436 --> 09:44:42,000
multi-level hierarchical and multiple
14853
09:44:42,000 --> 09:44:44,320
inheritance and we are going to support
14854
09:44:44,320 --> 09:44:47,120
each concept with an example
14855
09:44:47,120 --> 09:44:48,160
before
14856
09:44:48,160 --> 09:44:50,720
we begin this session i just want to
14857
09:44:50,720 --> 09:44:52,960
highlight that inheritance is not a new
14858
09:44:52,960 --> 09:44:54,800
concept in any of the programming
14859
09:44:54,800 --> 09:44:56,000
languages
14860
09:44:56,000 --> 09:44:58,880
just like in python this concept is
14861
09:44:58,880 --> 09:45:00,960
applicable across multiple programming
14862
09:45:00,960 --> 09:45:03,916
languages like java c
14863
09:45:03,916 --> 09:45:06,480
etc
14864
09:45:08,320 --> 09:45:10,960
now what is inheritance inheritance
14865
09:45:10,960 --> 09:45:13,680
provides the reusability of a code
14866
09:45:13,680 --> 09:45:16,480
it allows you to inherit what you have
14867
09:45:16,480 --> 09:45:18,720
written in a particular class or a piece
14868
09:45:18,720 --> 09:45:21,120
of code and you don't need to rewrite it
14869
09:45:21,120 --> 09:45:22,640
you can inherit it
14870
09:45:22,640 --> 09:45:24,320
so that is the main purpose of
14871
09:45:24,320 --> 09:45:27,360
inheritance it is creating a new class
14872
09:45:27,360 --> 09:45:29,596
that utilizes the properties
14873
09:45:29,596 --> 09:45:32,400
and behavior of an existing class
14874
09:45:32,400 --> 09:45:34,960
so we will support this with an example
14875
09:45:34,960 --> 09:45:37,436
so if you take a look at the syntax the
14876
09:45:37,436 --> 09:45:40,160
sample syntax over here you have a base
14877
09:45:40,160 --> 09:45:42,800
class and you have certain attributes
14878
09:45:42,800 --> 09:45:44,000
functions
14879
09:45:44,000 --> 09:45:46,240
uh associated with the base class and
14880
09:45:46,240 --> 09:45:49,120
then you have a derived class in which
14881
09:45:49,120 --> 09:45:52,400
you extend basically you inherit the
14882
09:45:52,400 --> 09:45:55,200
base class and you inherit the
14883
09:45:55,200 --> 09:45:58,720
properties of the base class
14884
09:45:59,040 --> 09:46:01,840
now the new class formed is called the
14885
09:46:01,840 --> 09:46:04,160
derived class or the child class you can
14886
09:46:04,160 --> 09:46:06,080
call it anything derived child or
14887
09:46:06,080 --> 09:46:09,120
subclass and the existing class whose
14888
09:46:09,120 --> 09:46:12,080
properties are inherited is known as the
14889
09:46:12,080 --> 09:46:14,800
base class parent or super class these
14890
09:46:14,800 --> 09:46:17,276
are synonymous so any
14891
09:46:17,276 --> 09:46:19,596
where you hear that okay you know you're
14892
09:46:19,596 --> 09:46:21,276
working on a child class or derived
14893
09:46:21,276 --> 09:46:22,560
class you can
14894
09:46:22,560 --> 09:46:24,000
easily figure out that you're talking
14895
09:46:24,000 --> 09:46:25,840
about a derived class and whenever there
14896
09:46:25,840 --> 09:46:26,720
is a
14897
09:46:26,720 --> 09:46:28,640
reference to a parent or a super class
14898
09:46:28,640 --> 09:46:30,160
it means we're talking about the base
14899
09:46:30,160 --> 09:46:31,120
class
14900
09:46:31,120 --> 09:46:32,000
right
14901
09:46:32,000 --> 09:46:33,436
now
14902
09:46:33,436 --> 09:46:34,640
with this
14903
09:46:34,640 --> 09:46:37,916
concept with this idea let's move ahead
14904
09:46:37,916 --> 09:46:39,200
and
14905
09:46:39,200 --> 09:46:41,680
look at the different types of
14906
09:46:41,680 --> 09:46:46,160
inheritance which are possible in python
14907
09:46:49,120 --> 09:46:51,520
now here we are trying to
14908
09:46:51,520 --> 09:46:53,320
display pictographically
14909
09:46:53,320 --> 09:46:55,596
diagrammatically the different kinds of
14910
09:46:55,596 --> 09:46:57,756
inheritance and this will make
14911
09:46:57,756 --> 09:47:00,160
understanding more clear
14912
09:47:00,160 --> 09:47:02,640
now let's talk about first single level
14913
09:47:02,640 --> 09:47:05,360
inheritance you have a class a and you
14914
09:47:05,360 --> 09:47:08,480
have a class b so basically only till
14915
09:47:08,480 --> 09:47:11,596
class b only single level child is
14916
09:47:11,596 --> 09:47:13,596
inheriting the
14917
09:47:13,596 --> 09:47:15,756
attributes the properties of classes so
14918
09:47:15,756 --> 09:47:17,596
that is what we call as single level
14919
09:47:17,596 --> 09:47:18,800
inheritance
14920
09:47:18,800 --> 09:47:21,276
multi-level is you have
14921
09:47:21,276 --> 09:47:23,756
multiple level of inheritance
14922
09:47:23,756 --> 09:47:26,400
in for example class b here inherits
14923
09:47:26,400 --> 09:47:29,520
class a properties and class c inherits
14924
09:47:29,520 --> 09:47:32,240
class b properties so ultimately you can
14925
09:47:32,240 --> 09:47:33,120
see
14926
09:47:33,120 --> 09:47:35,756
that the the properties gets trickled
14927
09:47:35,756 --> 09:47:38,160
down till class c right so that is the
14928
09:47:38,160 --> 09:47:41,040
example of a multi-level
14929
09:47:41,040 --> 09:47:42,400
right
14930
09:47:42,400 --> 09:47:45,360
next is hierarchical in which
14931
09:47:45,360 --> 09:47:47,916
two classes are inheriting from a single
14932
09:47:47,916 --> 09:47:49,436
class class b
14933
09:47:49,436 --> 09:47:52,080
is also inediting from class a and class
14934
09:47:52,080 --> 09:47:54,320
c is also inheriting from class a so
14935
09:47:54,320 --> 09:47:57,276
both class b and class c
14936
09:47:57,276 --> 09:48:00,240
are kind of siblings to the parent class
14937
09:48:00,240 --> 09:48:03,800
a right
14938
09:48:03,916 --> 09:48:06,800
multiple inheritance it means that a
14939
09:48:06,800 --> 09:48:09,120
child class in is inheriting properties
14940
09:48:09,120 --> 09:48:11,040
from two parent classes
14941
09:48:11,040 --> 09:48:13,520
so in this example the class c is
14942
09:48:13,520 --> 09:48:16,240
inherited the properties of class a also
14943
09:48:16,240 --> 09:48:17,916
and class b also
14944
09:48:17,916 --> 09:48:21,120
so we will be looking at these examples
14945
09:48:21,120 --> 09:48:24,720
when we will be running in python
14946
09:48:29,436 --> 09:48:31,040
now let's
14947
09:48:31,040 --> 09:48:32,800
take this example with a real world
14948
09:48:32,800 --> 09:48:36,160
object right let's first try to
14949
09:48:36,160 --> 09:48:37,436
give you a
14950
09:48:37,436 --> 09:48:39,120
view of how
14951
09:48:39,120 --> 09:48:41,120
we are going what we want to explain in
14952
09:48:41,120 --> 09:48:43,596
the inheritance with real world objects
14953
09:48:43,596 --> 09:48:46,240
right now here we are taking an example
14954
09:48:46,240 --> 09:48:48,800
of a black and white tv and a color tv
14955
09:48:48,800 --> 09:48:51,120
now single level inheritance in this
14956
09:48:51,120 --> 09:48:53,596
type of inheritance there is only one
14957
09:48:53,596 --> 09:48:56,720
single derived class from a single base
14958
09:48:56,720 --> 09:48:59,276
class as i mentioned earlier so for
14959
09:48:59,276 --> 09:49:01,680
example you can assume there is a class
14960
09:49:01,680 --> 09:49:03,520
called black and white tv and what are
14961
09:49:03,520 --> 09:49:05,596
the attributes of a simple black and
14962
09:49:05,596 --> 09:49:07,756
white tb it has a regulator it has a
14963
09:49:07,756 --> 09:49:09,360
sound adjustment
14964
09:49:09,360 --> 09:49:10,160
now
14965
09:49:10,160 --> 09:49:12,960
there is a child class called color tv
14966
09:49:12,960 --> 09:49:15,120
which inherits the basic attributes of
14967
09:49:15,120 --> 09:49:17,276
course a color tv also has a regulator
14968
09:49:17,276 --> 09:49:19,640
or sound adjustments but
14969
09:49:19,640 --> 09:49:22,000
additionally on top of it it also
14970
09:49:22,000 --> 09:49:24,560
provides new features like color picture
14971
09:49:24,560 --> 09:49:27,360
and cable connection so now
14972
09:49:27,360 --> 09:49:29,120
what do you understand by this is that
14973
09:49:29,120 --> 09:49:31,276
the child inherits the basic properties
14974
09:49:31,276 --> 09:49:32,880
from the print class
14975
09:49:32,880 --> 09:49:35,596
but it also adds on the new additional
14976
09:49:35,596 --> 09:49:38,400
features so the color tv does not need
14977
09:49:38,400 --> 09:49:39,756
to rewrite
14978
09:49:39,756 --> 09:49:40,480
or
14979
09:49:40,480 --> 09:49:41,276
the
14980
09:49:41,276 --> 09:49:42,800
functionality of a regulator and the
14981
09:49:42,800 --> 09:49:46,400
sound adjustment it has inherited
14982
09:49:49,756 --> 09:49:51,276
multi-level
14983
09:49:51,276 --> 09:49:53,596
very similar to single level but now we
14984
09:49:53,596 --> 09:49:55,520
are adding more features
14985
09:49:55,520 --> 09:49:58,320
so like in the previous example color tv
14986
09:49:58,320 --> 09:50:00,400
inherited the features of black from
14987
09:50:00,400 --> 09:50:02,000
black and white tv
14988
09:50:02,000 --> 09:50:04,000
now we move a step ahead now there is
14989
09:50:04,000 --> 09:50:05,840
another classification another
14990
09:50:05,840 --> 09:50:08,960
advancement of wi-fi led tv so that has
14991
09:50:08,960 --> 09:50:11,436
also narrated the features from color tv
14992
09:50:11,436 --> 09:50:14,080
right so it's a multi-level inheritance
14993
09:50:14,080 --> 09:50:16,320
so just to reiterate the black and white
14994
09:50:16,320 --> 09:50:19,040
tv had channel regulator dish antenna
14995
09:50:19,040 --> 09:50:21,200
the color tv added additional features
14996
09:50:21,200 --> 09:50:22,880
of color picture quality and cable
14997
09:50:22,880 --> 09:50:24,000
connection
14998
09:50:24,000 --> 09:50:27,276
and the wi-fi enabled tv added the new
14999
09:50:27,276 --> 09:50:29,596
smart features to the tv which it
15000
09:50:29,596 --> 09:50:33,040
inherited from the color tv
15001
09:50:35,200 --> 09:50:37,756
multiple inheritance now in multiple
15002
09:50:37,756 --> 09:50:40,320
inheritance the derived class is
15003
09:50:40,320 --> 09:50:42,880
inherited from more than one base class
15004
09:50:42,880 --> 09:50:45,680
right so now for example over here you
15005
09:50:45,680 --> 09:50:48,080
have a class called android which is an
15006
09:50:48,080 --> 09:50:50,720
android operating system based class
15007
09:50:50,720 --> 09:50:53,120
and one is the wi-fi led class which has
15008
09:50:53,120 --> 09:50:55,756
the wi-fi features of like sound picture
15009
09:50:55,756 --> 09:50:58,640
wi-fi enablement and if you combine all
15010
09:50:58,640 --> 09:51:01,040
these features and bring it into another
15011
09:51:01,040 --> 09:51:03,360
child class that class is the smart tv
15012
09:51:03,360 --> 09:51:05,680
class which has the features of android
15013
09:51:05,680 --> 09:51:08,080
and the features of wi-fi led tv and
15014
09:51:08,080 --> 09:51:10,000
then you call it as a smart tv it has
15015
09:51:10,000 --> 09:51:12,480
all the android applications it has the
15016
09:51:12,480 --> 09:51:14,880
wi-fi features and etc so this is the
15017
09:51:14,880 --> 09:51:17,040
example of multiple inheritance getting
15018
09:51:17,040 --> 09:51:18,480
the best of
15019
09:51:18,480 --> 09:51:21,200
all the parents
15020
09:51:24,400 --> 09:51:25,840
here kill
15021
09:51:25,840 --> 09:51:28,240
if more than one class is inherited from
15022
09:51:28,240 --> 09:51:30,960
the base class right so now if we just
15023
09:51:30,960 --> 09:51:33,756
tweak our prior example and you know
15024
09:51:33,756 --> 09:51:34,720
just
15025
09:51:34,720 --> 09:51:37,520
rephrase it what we are saying is now
15026
09:51:37,520 --> 09:51:39,916
from the black and white tv you can also
15027
09:51:39,916 --> 09:51:42,400
make a color tv you can also make a
15028
09:51:42,400 --> 09:51:44,640
smart tv or you can also make a wi-fi
15029
09:51:44,640 --> 09:51:47,200
led tv right so there is no restriction
15030
09:51:47,200 --> 09:51:50,240
as such so basically the color tv is
15031
09:51:50,240 --> 09:51:52,480
also inheriting the features of a black
15032
09:51:52,480 --> 09:51:55,436
and white tv smart tv is also inheriting
15033
09:51:55,436 --> 09:51:56,640
the features
15034
09:51:56,640 --> 09:51:59,360
of a black and white tv and the wi-fi
15035
09:51:59,360 --> 09:52:01,680
led tv also is narrating the features
15036
09:52:01,680 --> 09:52:03,680
from black and white tv so it's a very
15037
09:52:03,680 --> 09:52:06,320
typical example of a hierarchical in
15038
09:52:06,320 --> 09:52:07,596
inheritance
15039
09:52:07,596 --> 09:52:08,960
so now let's
15040
09:52:08,960 --> 09:52:13,320
quickly look at the examples
15041
09:52:17,040 --> 09:52:19,756
okay so let's start off
15042
09:52:19,756 --> 09:52:20,880
with a
15043
09:52:20,880 --> 09:52:22,320
very simple
15044
09:52:22,320 --> 09:52:23,360
single
15045
09:52:23,360 --> 09:52:26,720
level inheritance
15046
09:52:26,720 --> 09:52:29,276
let's make a class
15047
09:52:29,276 --> 09:52:30,320
and
15048
09:52:30,320 --> 09:52:33,200
create a function
15049
09:52:34,320 --> 09:52:37,276
called speak
15050
09:52:40,400 --> 09:52:43,840
and print something
15051
09:52:45,436 --> 09:52:47,756
okay
15052
09:52:48,960 --> 09:52:52,720
now let's make another class
15053
09:52:55,040 --> 09:52:57,040
so what we did was we created a class
15054
09:52:57,040 --> 09:52:59,916
called homo sapiens and in the and now
15055
09:52:59,916 --> 09:53:01,916
we are creating a clause called man
15056
09:53:01,916 --> 09:53:03,756
which inherits
15057
09:53:03,756 --> 09:53:05,680
uh the attributes
15058
09:53:05,680 --> 09:53:07,840
of homo sapiens
15059
09:53:07,840 --> 09:53:10,320
and we say
15060
09:53:10,320 --> 09:53:13,040
create a function which is specific
15061
09:53:13,040 --> 09:53:16,400
to man so homo sapiens has a function
15062
09:53:16,400 --> 09:53:18,560
which called speak and man has a
15063
09:53:18,560 --> 09:53:21,596
function called talk
15064
09:53:23,680 --> 09:53:27,276
and let's print it
15065
09:53:32,160 --> 09:53:33,040
now
15066
09:53:33,040 --> 09:53:35,916
let's create an object of
15067
09:53:35,916 --> 09:53:36,960
man
15068
09:53:36,960 --> 09:53:39,596
now what does this signify
15069
09:53:39,596 --> 09:53:40,720
the man
15070
09:53:40,720 --> 09:53:43,756
class is also inheriting whatever is
15071
09:53:43,756 --> 09:53:45,756
there in homo sapiens so man can even
15072
09:53:45,756 --> 09:53:50,000
talk and he can even speak right
15073
09:53:51,520 --> 09:53:53,520
so what we are going to do is
15074
09:53:53,520 --> 09:53:55,040
first we are going to execute the
15075
09:53:55,040 --> 09:53:56,560
function
15076
09:53:56,560 --> 09:53:58,240
speak
15077
09:53:58,240 --> 09:53:59,596
so the
15078
09:53:59,596 --> 09:54:02,880
the base class function got called speak
15079
09:54:02,880 --> 09:54:06,080
and it is printing homo sapiens speaking
15080
09:54:06,080 --> 09:54:11,596
and also we can call the child function
15081
09:54:12,800 --> 09:54:15,916
man talking okay so so if you see
15082
09:54:15,916 --> 09:54:17,520
this is the basic difference which we
15083
09:54:17,520 --> 09:54:18,720
want to highlight
15084
09:54:18,720 --> 09:54:19,596
that
15085
09:54:19,596 --> 09:54:21,756
just you have created the object only
15086
09:54:21,756 --> 09:54:24,240
for the child class not the parent so
15087
09:54:24,240 --> 09:54:26,560
now making by making the object of the
15088
09:54:26,560 --> 09:54:28,320
child class
15089
09:54:28,320 --> 09:54:30,560
you can invoke the parent class function
15090
09:54:30,560 --> 09:54:32,480
as well as the child because the child
15091
09:54:32,480 --> 09:54:36,080
has inherited the parent features
15092
09:54:38,480 --> 09:54:39,436
now
15093
09:54:39,436 --> 09:54:42,400
let's take an example of
15094
09:54:42,400 --> 09:54:45,120
multi-level
15095
09:54:46,320 --> 09:54:47,680
so we will
15096
09:54:47,680 --> 09:54:50,240
continue from our previous example we
15097
09:54:50,240 --> 09:54:54,160
will create class homo sapiens
15098
09:54:54,160 --> 09:54:56,480
define in
15099
09:54:56,480 --> 09:54:59,276
functions peak
15100
09:54:59,276 --> 09:55:00,880
and
15101
09:55:00,880 --> 09:55:03,040
print
15102
09:55:03,040 --> 09:55:06,960
now we will make another class
15103
09:55:09,596 --> 09:55:11,680
man
15104
09:55:11,680 --> 09:55:14,960
now we are saying
15105
09:55:15,200 --> 09:55:16,400
man can
15106
09:55:16,400 --> 09:55:18,400
walk
15107
09:55:18,400 --> 09:55:22,320
so we are saying man walking
15108
09:55:26,720 --> 09:55:29,120
now this is the third level this is the
15109
09:55:29,120 --> 09:55:31,840
multi-level
15110
09:55:33,040 --> 09:55:34,640
man child
15111
09:55:34,640 --> 09:55:35,680
and
15112
09:55:35,680 --> 09:55:40,320
we are saying that a man child can eat
15113
09:55:43,360 --> 09:55:46,160
so ultimately the man child can speak
15114
09:55:46,160 --> 09:55:48,480
can walk can eat all three operations
15115
09:55:48,480 --> 09:55:51,200
can be performed
15116
09:55:52,480 --> 09:55:54,240
so we are gonna make a object of
15117
09:55:54,240 --> 09:55:56,560
man-child
15118
09:55:56,560 --> 09:55:59,360
and now
15119
09:55:59,360 --> 09:56:01,120
python will allow us
15120
09:56:01,120 --> 09:56:02,560
invoke
15121
09:56:02,560 --> 09:56:05,360
walk function
15122
09:56:05,360 --> 09:56:08,400
speak function
15123
09:56:09,360 --> 09:56:10,240
and
15124
09:56:10,240 --> 09:56:13,560
eat function
15125
09:56:14,240 --> 09:56:16,480
so this is a typical example of
15126
09:56:16,480 --> 09:56:17,916
multi-level
15127
09:56:17,916 --> 09:56:20,560
hierarchy
15128
09:56:22,400 --> 09:56:24,160
now
15129
09:56:24,160 --> 09:56:25,596
moving to our
15130
09:56:25,596 --> 09:56:26,400
next
15131
09:56:26,400 --> 09:56:28,400
type of inheritance which is the
15132
09:56:28,400 --> 09:56:30,160
multiple inheritance
15133
09:56:30,160 --> 09:56:31,120
where
15134
09:56:31,120 --> 09:56:32,320
the child
15135
09:56:32,320 --> 09:56:34,960
is deriving
15136
09:56:34,960 --> 09:56:37,680
attributes properties from
15137
09:56:37,680 --> 09:56:40,916
two parents
15138
09:56:42,080 --> 09:56:44,560
so we will now create a class called
15139
09:56:44,560 --> 09:56:46,800
calculation1
15140
09:56:46,800 --> 09:56:48,480
in our example
15141
09:56:48,480 --> 09:56:51,200
and let's make a function
15142
09:56:51,200 --> 09:56:54,200
sum
15143
09:56:54,240 --> 09:56:55,040
and
15144
09:56:55,040 --> 09:56:57,120
it will return
15145
09:56:57,120 --> 09:57:00,720
calculation x plus y
15146
09:57:02,560 --> 09:57:04,080
second
15147
09:57:04,080 --> 09:57:06,640
calculation two
15148
09:57:06,640 --> 09:57:09,640
and
15149
09:57:09,680 --> 09:57:11,200
let's create
15150
09:57:11,200 --> 09:57:13,680
operation subtraction
15151
09:57:13,680 --> 09:57:18,080
and it will return x minus y
15152
09:57:19,756 --> 09:57:22,756
okay
15153
09:57:23,040 --> 09:57:24,400
now
15154
09:57:24,400 --> 09:57:26,160
we will create
15155
09:57:26,160 --> 09:57:29,200
the derived class which is inheriting
15156
09:57:29,200 --> 09:57:32,880
both calculation one and calculation two
15157
09:57:32,880 --> 09:57:34,400
and
15158
09:57:34,400 --> 09:57:37,120
let's create its own
15159
09:57:37,120 --> 09:57:38,640
function called
15160
09:57:38,640 --> 09:57:41,520
multiplication
15161
09:57:47,200 --> 09:57:49,840
okay so derived class now has a sum
15162
09:57:49,840 --> 09:57:51,756
function subtraction function and
15163
09:57:51,756 --> 09:57:54,960
multiplication okay
15164
09:57:57,276 --> 09:58:00,640
so let's create an object for derived
15165
09:58:00,640 --> 09:58:02,000
and
15166
09:58:02,000 --> 09:58:03,756
now let's
15167
09:58:03,756 --> 09:58:06,080
invoke the relevant functions
15168
09:58:06,080 --> 09:58:08,560
so sum
15169
09:58:08,560 --> 09:58:11,200
120
15170
09:58:12,080 --> 09:58:14,880
subtraction
15171
09:58:15,756 --> 09:58:18,960
it should say minus 40
15172
09:58:18,960 --> 09:58:20,640
and
15173
09:58:20,640 --> 09:58:23,520
multiplication
15174
09:58:28,160 --> 09:58:29,680
so if you see
15175
09:58:29,680 --> 09:58:31,840
it's it's different from multi-level
15176
09:58:31,840 --> 09:58:35,040
it's multiple it means that the child
15177
09:58:35,040 --> 09:58:36,240
class
15178
09:58:36,240 --> 09:58:38,480
has inherited from two parents it's
15179
09:58:38,480 --> 09:58:40,960
relevant functions right
15180
09:58:40,960 --> 09:58:42,640
and that's how we are able to call them
15181
09:58:42,640 --> 09:58:46,276
in the derived class
15182
09:58:48,320 --> 09:58:51,756
now let's take another type hierarchical
15183
09:58:51,756 --> 09:58:54,400
inheritance
15184
09:58:57,200 --> 09:58:58,080
okay
15185
09:58:58,080 --> 09:58:59,596
so in hierarchy
15186
09:58:59,596 --> 09:59:01,040
inheritance
15187
09:59:01,040 --> 09:59:04,960
let's go back to our example of
15188
09:59:04,960 --> 09:59:08,560
first we have the black and white tv and
15189
09:59:08,560 --> 09:59:11,680
we will create a function called
15190
09:59:11,680 --> 09:59:14,680
antenna
15191
09:59:16,960 --> 09:59:20,080
print channels are telecasted through
15192
09:59:20,080 --> 09:59:22,640
antenna
15193
09:59:24,000 --> 09:59:26,640
then we will create
15194
09:59:26,640 --> 09:59:29,840
color tv
15195
09:59:32,800 --> 09:59:34,480
and
15196
09:59:34,480 --> 09:59:36,480
we will define its own function called
15197
09:59:36,480 --> 09:59:38,960
cable connection plus it has an edited
15198
09:59:38,960 --> 09:59:40,960
the function called antenna from black
15199
09:59:40,960 --> 09:59:43,840
and white tv
15200
09:59:50,400 --> 09:59:52,320
here it is okay
15201
09:59:52,320 --> 09:59:54,800
now we are going to create another
15202
09:59:54,800 --> 09:59:57,916
class called as
15203
09:59:58,800 --> 10:00:01,840
wi-fi led tv which is also in editing
15204
10:00:01,840 --> 10:00:04,800
from black and white
15205
10:00:06,640 --> 10:00:09,680
now in this we are defining bluetooth
15206
10:00:09,680 --> 10:00:12,640
tethering function
15207
10:00:14,360 --> 10:00:16,480
[Applause]
15208
10:00:16,480 --> 10:00:18,000
okay
15209
10:00:18,000 --> 10:00:23,000
and let's make another smart tv
15210
10:00:32,160 --> 10:00:36,756
okay and then we are gonna
15211
10:00:38,640 --> 10:00:41,276
you can watch
15212
10:00:41,596 --> 10:00:44,000
android applications on otp platform
15213
10:00:44,000 --> 10:00:45,680
right so this is the
15214
10:00:45,680 --> 10:00:48,880
class of smart tv
15215
10:00:50,000 --> 10:00:52,480
okay so what we have done eventually
15216
10:00:52,480 --> 10:00:54,400
over here is we have created a black and
15217
10:00:54,400 --> 10:00:55,596
white tv
15218
10:00:55,596 --> 10:00:57,436
color tv also inherits from black and
15219
10:00:57,436 --> 10:00:59,520
white tv wi-fi led also in edits from
15220
10:00:59,520 --> 10:01:02,080
black and white tv smart tv also edits
15221
10:01:02,080 --> 10:01:05,120
from black and white tv
15222
10:01:05,120 --> 10:01:07,756
okay now
15223
10:01:07,756 --> 10:01:09,200
let's try to
15224
10:01:09,200 --> 10:01:11,756
create the
15225
10:01:13,200 --> 10:01:16,160
relevant objects
15226
10:01:17,040 --> 10:01:19,040
we will first create an object of color
15227
10:01:19,040 --> 10:01:20,800
tv
15228
10:01:20,800 --> 10:01:23,840
then we will create
15229
10:01:24,160 --> 10:01:26,080
wi-fi led tv
15230
10:01:26,080 --> 10:01:28,080
and then we will create
15231
10:01:28,080 --> 10:01:30,800
smart tv
15232
10:01:31,840 --> 10:01:33,200
okay
15233
10:01:33,200 --> 10:01:35,120
now we will have the relevant functions
15234
10:01:35,120 --> 10:01:37,680
available to invoke
15235
10:01:37,680 --> 10:01:40,000
so both all the child all the children
15236
10:01:40,000 --> 10:01:41,680
objects now will have an antenna
15237
10:01:41,680 --> 10:01:44,720
function a1
15238
10:01:44,880 --> 10:01:47,276
a2
15239
10:01:47,680 --> 10:01:49,520
and a3
15240
10:01:49,520 --> 10:01:52,160
all three are having the antenna
15241
10:01:52,160 --> 10:01:55,276
function so we can just try to print it
15242
10:01:55,276 --> 10:01:56,720
okay
15243
10:01:56,720 --> 10:01:58,400
and then
15244
10:01:58,400 --> 10:02:00,960
we can invoke the relevant
15245
10:02:00,960 --> 10:02:02,400
child functions
15246
10:02:02,400 --> 10:02:04,800
a1
15247
10:02:05,916 --> 10:02:08,480
cable connection
15248
10:02:08,480 --> 10:02:11,520
bluetooth triggering
15249
10:02:12,160 --> 10:02:13,040
and
15250
10:02:13,040 --> 10:02:16,596
android applications
15251
10:02:20,080 --> 10:02:22,000
so if you see this is an example of
15252
10:02:22,000 --> 10:02:23,276
hierarchy
15253
10:02:23,276 --> 10:02:26,640
you have color wi-fi led in smart tv all
15254
10:02:26,640 --> 10:02:28,240
inheriting the black and white tv
15255
10:02:28,240 --> 10:02:29,276
features
15256
10:02:29,276 --> 10:02:31,360
and its own
15257
10:02:31,360 --> 10:02:35,240
its own feature
15258
10:02:38,620 --> 10:02:43,519
[Music]
15259
10:02:44,400 --> 10:02:45,916
okay
15260
10:02:45,916 --> 10:02:48,640
now along with this concept let me just
15261
10:02:48,640 --> 10:02:51,040
also introduce
15262
10:02:51,040 --> 10:02:52,560
some relevant concepts which are
15263
10:02:52,560 --> 10:02:53,756
applicable
15264
10:02:53,756 --> 10:02:55,040
along with
15265
10:02:55,040 --> 10:02:56,880
the inheritance concept which will be
15266
10:02:56,880 --> 10:02:58,640
helpful
15267
10:02:58,640 --> 10:03:00,560
i also want to show you
15268
10:03:00,560 --> 10:03:02,400
the example of
15269
10:03:02,400 --> 10:03:05,200
init function init function
15270
10:03:05,200 --> 10:03:08,240
is a constructor in python language
15271
10:03:08,240 --> 10:03:11,040
and it is called by default whenever you
15272
10:03:11,040 --> 10:03:13,200
instantiate an object
15273
10:03:13,200 --> 10:03:15,916
so let me give you an example over here
15274
10:03:15,916 --> 10:03:17,596
so here you have a
15275
10:03:17,596 --> 10:03:19,120
class person
15276
10:03:19,120 --> 10:03:20,720
and you have
15277
10:03:20,720 --> 10:03:22,960
we are just defining the init function
15278
10:03:22,960 --> 10:03:24,960
the constructor
15279
10:03:24,960 --> 10:03:26,720
okay and
15280
10:03:26,720 --> 10:03:29,360
we are just
15281
10:03:29,360 --> 10:03:30,880
defining certain attributes
15282
10:03:30,880 --> 10:03:32,960
instantiating certain attributes in the
15283
10:03:32,960 --> 10:03:36,880
person first name and the last name
15284
10:03:36,880 --> 10:03:38,800
and another function which i am calling
15285
10:03:38,800 --> 10:03:41,120
is the
15286
10:03:41,120 --> 10:03:43,756
print name
15287
10:03:49,520 --> 10:03:52,320
and i'll print the name it's a
15288
10:03:52,320 --> 10:03:54,880
straightforward function
15289
10:03:54,880 --> 10:03:58,480
okay so what i'm doing is i am creating
15290
10:03:58,480 --> 10:04:00,480
a child class now
15291
10:04:00,480 --> 10:04:02,320
called as student
15292
10:04:02,320 --> 10:04:03,916
which is inherited
15293
10:04:03,916 --> 10:04:07,756
uh the features attributes of the person
15294
10:04:07,756 --> 10:04:10,400
base class
15295
10:04:10,640 --> 10:04:12,880
and
15296
10:04:13,200 --> 10:04:15,276
i'm also making a constructor for
15297
10:04:15,276 --> 10:04:17,756
student
15298
10:04:17,756 --> 10:04:21,680
and from student i am invoking the
15299
10:04:21,680 --> 10:04:25,680
constructor of the person class right
15300
10:04:28,080 --> 10:04:30,560
so now if you see i'm going to make an
15301
10:04:30,560 --> 10:04:32,240
object of student
15302
10:04:32,240 --> 10:04:34,800
and initialize it with
15303
10:04:34,800 --> 10:04:36,880
the self the first name and the last
15304
10:04:36,880 --> 10:04:37,840
name
15305
10:04:37,840 --> 10:04:40,400
and in eventually
15306
10:04:40,400 --> 10:04:41,916
the
15307
10:04:41,916 --> 10:04:44,080
parent class init function is being
15308
10:04:44,080 --> 10:04:47,436
called first name and the last name and
15309
10:04:47,436 --> 10:04:50,080
this function diff print name it will
15310
10:04:50,080 --> 10:04:52,960
print james anderson right as i have
15311
10:04:52,960 --> 10:04:56,240
already initiated the
15312
10:04:56,560 --> 10:04:59,276
parameters
15313
10:05:00,960 --> 10:05:04,160
okay so if you see now what have i done
15314
10:05:04,160 --> 10:05:05,276
i have
15315
10:05:05,276 --> 10:05:08,240
the attribute values in the parent class
15316
10:05:08,240 --> 10:05:09,840
are getting printed
15317
10:05:09,840 --> 10:05:12,720
by invoking the constructor of the child
15318
10:05:12,720 --> 10:05:14,560
class
15319
10:05:14,560 --> 10:05:17,040
so what happened when you invoke student
15320
10:05:17,040 --> 10:05:18,640
by default this init function got
15321
10:05:18,640 --> 10:05:21,040
invoked the parameters of james and
15322
10:05:21,040 --> 10:05:24,080
anderson got passed then from person you
15323
10:05:24,080 --> 10:05:26,560
invoke the constructor in it and here
15324
10:05:26,560 --> 10:05:29,120
now the first name and last name got
15325
10:05:29,120 --> 10:05:30,400
initialized
15326
10:05:30,400 --> 10:05:32,800
and then it got printed
15327
10:05:32,800 --> 10:05:35,200
so this is the basic feature of a init
15328
10:05:35,200 --> 10:05:37,596
function
15329
10:05:38,240 --> 10:05:39,040
now
15330
10:05:39,040 --> 10:05:41,436
like the way i have called person dot
15331
10:05:41,436 --> 10:05:44,400
init over here i i can also make a call
15332
10:05:44,400 --> 10:05:46,880
to super there is a keyword called super
15333
10:05:46,880 --> 10:05:48,080
keyword
15334
10:05:48,080 --> 10:05:51,276
so let's just take an example of super
15335
10:05:51,276 --> 10:05:53,520
keyword
15336
10:05:53,520 --> 10:05:54,880
okay
15337
10:05:54,880 --> 10:05:58,000
let me have my child class like the
15338
10:05:58,000 --> 10:06:00,160
previous one parent class the person
15339
10:06:00,160 --> 10:06:01,680
class
15340
10:06:01,680 --> 10:06:03,756
and
15341
10:06:03,756 --> 10:06:06,880
i'll have to just make a change in my
15342
10:06:06,880 --> 10:06:09,756
student class
15343
10:06:12,080 --> 10:06:16,000
in the init function instead of parent
15344
10:06:16,000 --> 10:06:19,360
dot init i will call
15345
10:06:19,360 --> 10:06:21,596
super dot in it
15346
10:06:21,596 --> 10:06:23,520
this is the keyword
15347
10:06:23,520 --> 10:06:25,120
super function
15348
10:06:25,120 --> 10:06:26,720
which will always
15349
10:06:26,720 --> 10:06:29,916
invoke the parent class
15350
10:06:29,916 --> 10:06:31,840
gives you the handle to the parent class
15351
10:06:31,840 --> 10:06:33,680
and then you can invoke the relevant
15352
10:06:33,680 --> 10:06:35,040
method
15353
10:06:35,040 --> 10:06:36,080
and
15354
10:06:36,080 --> 10:06:38,160
the invocation methodology will be the
15355
10:06:38,160 --> 10:06:39,120
same
15356
10:06:39,120 --> 10:06:40,560
and you can
15357
10:06:40,560 --> 10:06:42,400
check the output it's going to be the
15358
10:06:42,400 --> 10:06:44,720
same but it's that the syntax is
15359
10:06:44,720 --> 10:06:46,720
different there is another keyword which
15360
10:06:46,720 --> 10:06:48,320
is available for you to invoke the
15361
10:06:48,320 --> 10:06:49,756
parent so you don't need to know the
15362
10:06:49,756 --> 10:06:50,880
class
15363
10:06:50,880 --> 10:06:52,480
of the parent you can just make a call
15364
10:06:52,480 --> 10:06:55,960
to the super
15365
10:07:00,640 --> 10:07:04,720
now let's also take a look at another
15366
10:07:04,720 --> 10:07:07,040
utility utility function called is
15367
10:07:07,040 --> 10:07:09,360
subclass right
15368
10:07:09,360 --> 10:07:11,840
so
15369
10:07:12,960 --> 10:07:14,800
what we are going to do is
15370
10:07:14,800 --> 10:07:17,520
we are going to create a function
15371
10:07:17,520 --> 10:07:20,880
calculation one
15372
10:07:23,596 --> 10:07:25,840
calculation two okay so we have created
15373
10:07:25,840 --> 10:07:27,840
two classes calculation one calculation
15374
10:07:27,840 --> 10:07:30,000
two independent classes no one
15375
10:07:30,000 --> 10:07:31,680
inheriting each other
15376
10:07:31,680 --> 10:07:33,276
now we are going to create a derived
15377
10:07:33,276 --> 10:07:35,596
class
15378
10:07:36,320 --> 10:07:38,320
now this is the example what we had done
15379
10:07:38,320 --> 10:07:40,400
earlier very similar to multiple
15380
10:07:40,400 --> 10:07:42,800
inheritance so we are just actually
15381
10:07:42,800 --> 10:07:45,360
creating a
15382
10:07:45,436 --> 10:07:47,520
multiple inheritance
15383
10:07:47,520 --> 10:07:48,880
example
15384
10:07:48,880 --> 10:07:50,880
okay so you have a derived class which
15385
10:07:50,880 --> 10:07:52,240
has multiple inheritance from
15386
10:07:52,240 --> 10:07:55,200
calculation 1 and calculation 2
15387
10:07:55,200 --> 10:07:58,160
summation multiplication and divide
15388
10:07:58,160 --> 10:08:00,080
relevant functions are there
15389
10:08:00,080 --> 10:08:02,960
now we will check
15390
10:08:02,960 --> 10:08:06,560
which class is a sub class of which one
15391
10:08:06,560 --> 10:08:07,520
right
15392
10:08:07,520 --> 10:08:11,840
so let's create an object of derived
15393
10:08:13,680 --> 10:08:15,756
now what we want to print is that
15394
10:08:15,756 --> 10:08:17,756
whether derived
15395
10:08:17,756 --> 10:08:21,200
is a sub class of calculation 2
15396
10:08:21,200 --> 10:08:24,240
and it should return true
15397
10:08:24,240 --> 10:08:27,360
and let's see
15398
10:08:28,480 --> 10:08:31,276
another is calculation 1 a sub class of
15399
10:08:31,276 --> 10:08:34,160
calculation 2 it should return false
15400
10:08:34,160 --> 10:08:36,400
right because these two are independent
15401
10:08:36,400 --> 10:08:38,880
classes so is subclass is another
15402
10:08:38,880 --> 10:08:40,560
function which is available in python
15403
10:08:40,560 --> 10:08:43,436
which helps you
15404
10:08:43,436 --> 10:08:45,360
decide you know you can check whether
15405
10:08:45,360 --> 10:08:46,800
which is a derived class and which is
15406
10:08:46,800 --> 10:08:49,840
not a derived class
15407
10:08:52,880 --> 10:08:54,160
next
15408
10:08:54,160 --> 10:08:56,800
let's take an example of another
15409
10:08:56,800 --> 10:09:01,800
function called is instance okay
15410
10:09:02,000 --> 10:09:04,720
will take the same
15411
10:09:04,720 --> 10:09:07,596
set of functions
15412
10:09:08,800 --> 10:09:12,480
create an object of derived
15413
10:09:12,560 --> 10:09:14,400
and check
15414
10:09:14,400 --> 10:09:15,520
whether
15415
10:09:15,520 --> 10:09:19,360
the object d is of which
15416
10:09:19,360 --> 10:09:20,960
is it is it of a
15417
10:09:20,960 --> 10:09:23,756
instance of class derived or not it's
15418
10:09:23,756 --> 10:09:26,480
true so instead of derived if i make it
15419
10:09:26,480 --> 10:09:29,680
as calculation 2 for example now if i
15420
10:09:29,680 --> 10:09:31,040
say c
15421
10:09:31,040 --> 10:09:32,320
equal to
15422
10:09:32,320 --> 10:09:35,436
calculation 1
15423
10:09:35,680 --> 10:09:38,800
and say c
15424
10:09:39,276 --> 10:09:42,160
is instance of derived
15425
10:09:42,160 --> 10:09:46,160
it should say false but if i say
15426
10:09:46,160 --> 10:09:48,240
calculation 1
15427
10:09:48,240 --> 10:09:50,000
it should say true
15428
10:09:50,000 --> 10:09:51,520
and if i say
15429
10:09:51,520 --> 10:09:53,200
calculation 2
15430
10:09:53,200 --> 10:09:54,960
should say false because
15431
10:09:54,960 --> 10:09:56,880
calculation class is an independent
15432
10:09:56,880 --> 10:09:59,120
class d is a derived class from
15433
10:09:59,120 --> 10:10:01,520
calculation one and calculation two so
15434
10:10:01,520 --> 10:10:04,160
basically d is in is instance of both
15435
10:10:04,160 --> 10:10:06,960
calculation one calculation two so it
15436
10:10:06,960 --> 10:10:09,040
will always return true
15437
10:10:09,040 --> 10:10:10,480
we are going to talk about what is a
15438
10:10:10,480 --> 10:10:13,680
python nitrater what are iterables right
15439
10:10:13,680 --> 10:10:15,596
what are the iterable objects
15440
10:10:15,596 --> 10:10:17,680
how an iterator works
15441
10:10:17,680 --> 10:10:20,240
and then we will also discuss about how
15442
10:10:20,240 --> 10:10:22,800
to throw a stop iteration exception
15443
10:10:22,800 --> 10:10:25,916
basically when you reach the end of a
15444
10:10:25,916 --> 10:10:26,720
list
15445
10:10:26,720 --> 10:10:28,640
then you can throw a stop iteration
15446
10:10:28,640 --> 10:10:30,480
exception and we will see that with an
15447
10:10:30,480 --> 10:10:32,240
example
15448
10:10:32,240 --> 10:10:34,560
now what is a python iterator
15449
10:10:34,560 --> 10:10:36,720
python nitrator is an object that can
15450
10:10:36,720 --> 10:10:39,276
traverse through iterable objects in
15451
10:10:39,276 --> 10:10:43,120
python like lists tuples dictionaries
15452
10:10:43,120 --> 10:10:44,080
sets
15453
10:10:44,080 --> 10:10:47,040
and returns data one element at a time
15454
10:10:47,040 --> 10:10:49,120
so basically you can traverse through
15455
10:10:49,120 --> 10:10:51,520
these layers one at a time and then you
15456
10:10:51,520 --> 10:10:54,000
can use it as per your custom
15457
10:10:54,000 --> 10:10:58,000
requirement but iterator as an object is
15458
10:10:58,000 --> 10:11:00,080
available to you is a tool available to
15459
10:11:00,080 --> 10:11:02,240
you in order to traverse through each
15460
10:11:02,240 --> 10:11:04,000
and every element
15461
10:11:04,000 --> 10:11:04,880
now
15462
10:11:04,880 --> 10:11:07,276
they are implemented within for loops
15463
10:11:07,276 --> 10:11:10,000
comprehensions generators in python
15464
10:11:10,000 --> 10:11:14,080
implicitly they are not visible up front
15465
10:11:14,080 --> 10:11:16,640
but they are hidden in plain sight so
15466
10:11:16,640 --> 10:11:18,240
basically when you're using loops when
15467
10:11:18,240 --> 10:11:20,960
you're using generators comprehensions
15468
10:11:20,960 --> 10:11:23,840
implicitly python is actually converting
15469
10:11:23,840 --> 10:11:25,916
them in the back end in an iterator
15470
10:11:25,916 --> 10:11:28,720
object and giving you the results
15471
10:11:28,720 --> 10:11:32,160
now what are iterables on which you can
15472
10:11:32,160 --> 10:11:33,680
iterate
15473
10:11:33,680 --> 10:11:36,960
lists tuples dictionaries sets as i
15474
10:11:36,960 --> 10:11:40,080
mentioned are all iterables or iterable
15475
10:11:40,080 --> 10:11:42,720
objects other than these you can't
15476
10:11:42,720 --> 10:11:45,040
perform any iteration if you have not
15477
10:11:45,040 --> 10:11:47,200
created an object of list tuple or
15478
10:11:47,200 --> 10:11:49,200
dictionary or said then of course your
15479
10:11:49,200 --> 10:11:52,000
right variable will not be of any use
15480
10:11:52,000 --> 10:11:52,960
and
15481
10:11:52,960 --> 10:11:56,400
these objects internally possess an item
15482
10:11:56,400 --> 10:12:00,160
method iter method which is used to get
15483
10:12:00,160 --> 10:12:02,160
an iterator a handle to an iterator and
15484
10:12:02,160 --> 10:12:05,520
then you can start looping it or you can
15485
10:12:05,520 --> 10:12:07,276
start accessing the elements of the
15486
10:12:07,276 --> 10:12:09,200
iterator
15487
10:12:09,200 --> 10:12:11,360
now how iterator works
15488
10:12:11,360 --> 10:12:15,520
python iterator implements internally
15489
10:12:15,520 --> 10:12:17,520
these two methods which you are seeing
15490
10:12:17,520 --> 10:12:20,560
on the screen underscore underscore item
15491
10:12:20,560 --> 10:12:23,360
underscore underscore brackets and
15492
10:12:23,360 --> 10:12:25,756
underscore underscore next underscore
15493
10:12:25,756 --> 10:12:27,200
underscore bracket
15494
10:12:27,200 --> 10:12:30,080
now these two methods are implicitly
15495
10:12:30,080 --> 10:12:33,520
implemented by python for any internal
15496
10:12:33,520 --> 10:12:35,200
implicit iterator
15497
10:12:35,200 --> 10:12:36,320
object
15498
10:12:36,320 --> 10:12:38,960
but if you have to write your own custom
15499
10:12:38,960 --> 10:12:40,080
iterator
15500
10:12:40,080 --> 10:12:42,800
then you need to override these methods
15501
10:12:42,800 --> 10:12:44,960
and i'll show you in my examples in
15502
10:12:44,960 --> 10:12:46,640
jupiter notebook
15503
10:12:46,640 --> 10:12:49,680
the iterator object is initialized using
15504
10:12:49,680 --> 10:12:51,916
the either method so basically whenever
15505
10:12:51,916 --> 10:12:53,756
you have to create an iterator object
15506
10:12:53,756 --> 10:12:56,400
you need to initialize it with iter and
15507
10:12:56,400 --> 10:12:59,040
then use next method in order to
15508
10:12:59,040 --> 10:13:01,276
traverse through it
15509
10:13:01,276 --> 10:13:03,680
now stop iteration exception so as i
15510
10:13:03,680 --> 10:13:05,756
mentioned when we reach the end of an
15511
10:13:05,756 --> 10:13:07,680
iterable right like suppose you have a
15512
10:13:07,680 --> 10:13:10,720
list or a set which has a length of four
15513
10:13:10,720 --> 10:13:12,960
when you have ended the fourth element
15514
10:13:12,960 --> 10:13:15,200
and you're trying to access something
15515
10:13:15,200 --> 10:13:17,360
beyond fourth element which is fifth
15516
10:13:17,360 --> 10:13:19,916
then of course you can implicitly raise
15517
10:13:19,916 --> 10:13:22,000
a stop titration exception or python
15518
10:13:22,000 --> 10:13:24,000
will throw it so we will see that with
15519
10:13:24,000 --> 10:13:25,840
an example but you will get a stop
15520
10:13:25,840 --> 10:13:27,756
iteration exception
15521
10:13:27,756 --> 10:13:30,800
now so to prevent in order to uh handle
15522
10:13:30,800 --> 10:13:31,596
it
15523
10:13:31,596 --> 10:13:32,400
or
15524
10:13:32,400 --> 10:13:34,640
manage it in your course we use top
15525
10:13:34,640 --> 10:13:37,040
iteration statement to raise an error
15526
10:13:37,040 --> 10:13:39,120
you can raise your customer error so you
15527
10:13:39,120 --> 10:13:41,200
can catch this exception
15528
10:13:41,200 --> 10:13:43,596
and then raise your custom error in the
15529
10:13:43,596 --> 10:13:46,400
underscore underscore next method
15530
10:13:46,400 --> 10:13:49,840
so now let's uh support everything with
15531
10:13:49,840 --> 10:13:51,756
our examples
15532
10:13:51,756 --> 10:13:54,160
let's start with a very basic
15533
10:13:54,160 --> 10:13:57,680
example of an iterator now let me create
15534
10:13:57,680 --> 10:13:59,276
a list
15535
10:13:59,276 --> 10:14:00,720
with just four
15536
10:14:00,720 --> 10:14:01,840
numbers
15537
10:14:01,840 --> 10:14:02,800
and
15538
10:14:02,800 --> 10:14:05,200
as i said i'm gonna initialize my
15539
10:14:05,200 --> 10:14:08,080
iterator with item method so now i have
15540
10:14:08,080 --> 10:14:10,960
created a list and now i am initializing
15541
10:14:10,960 --> 10:14:13,756
it with item okay
15542
10:14:13,756 --> 10:14:17,360
now using next i'm gonna print my first
15543
10:14:17,360 --> 10:14:21,436
element four okay so it means the iter
15544
10:14:21,436 --> 10:14:23,520
internally has given me handle to the
15545
10:14:23,520 --> 10:14:25,200
first element
15546
10:14:25,200 --> 10:14:28,720
and now as many times i move print next
15547
10:14:28,720 --> 10:14:32,800
i am moving to the next element
15548
10:14:33,596 --> 10:14:35,040
so now i'm also going to show you a
15549
10:14:35,040 --> 10:14:38,000
different syntax this is a syntax but
15550
10:14:38,000 --> 10:14:39,916
the output will be same it's a different
15551
10:14:39,916 --> 10:14:43,436
syntax to invoke underscore next so it's
15552
10:14:43,436 --> 10:14:44,960
output is going to be same but the
15553
10:14:44,960 --> 10:14:46,800
syntax is only the difference over here
15554
10:14:46,800 --> 10:14:48,800
so you're going to get access to the
15555
10:14:48,800 --> 10:14:50,320
next element
15556
10:14:50,320 --> 10:14:53,040
and then
15557
10:14:53,040 --> 10:14:56,320
my fourth element just 33 now i have
15558
10:14:56,320 --> 10:14:58,960
traversed through all my four elements
15559
10:14:58,960 --> 10:15:03,596
now what will happen if i again do next
15560
10:15:03,596 --> 10:15:06,160
i have ended i am at the end of my list
15561
10:15:06,160 --> 10:15:08,240
so i will get an exception of stop ice
15562
10:15:08,240 --> 10:15:10,640
iteration right and this is thrown by
15563
10:15:10,640 --> 10:15:13,040
python
15564
10:15:13,596 --> 10:15:15,360
now let's take another interesting
15565
10:15:15,360 --> 10:15:17,916
example if i want to perform iteration
15566
10:15:17,916 --> 10:15:18,840
on
15567
10:15:18,840 --> 10:15:20,800
strings
15568
10:15:20,800 --> 10:15:22,960
so i have created a variable and i have
15569
10:15:22,960 --> 10:15:24,960
initialized it with geeks
15570
10:15:24,960 --> 10:15:27,200
and now i have created an iterator on
15571
10:15:27,200 --> 10:15:29,520
top of this uh
15572
10:15:29,520 --> 10:15:31,436
string variable and i'm going to
15573
10:15:31,436 --> 10:15:32,720
traverse it
15574
10:15:32,720 --> 10:15:36,000
till the time i end reach the end of my
15575
10:15:36,000 --> 10:15:37,276
string
15576
10:15:37,276 --> 10:15:39,916
so let me
15577
10:15:41,120 --> 10:15:43,040
hydrate it and i'm going to iterate till
15578
10:15:43,040 --> 10:15:45,200
the time i get the stop iteration
15579
10:15:45,200 --> 10:15:47,436
exception
15580
10:15:47,436 --> 10:15:50,160
so also i might explaining here how to
15581
10:15:50,160 --> 10:15:53,840
cleanly handle the exception
15582
10:15:54,080 --> 10:15:55,520
accept
15583
10:15:55,520 --> 10:15:57,756
and then break if i get the exception
15584
10:15:57,756 --> 10:16:01,120
i'll break out of this program
15585
10:16:01,596 --> 10:16:03,436
and here you get the result
15586
10:16:03,436 --> 10:16:06,640
all the letters of my string geeks has
15587
10:16:06,640 --> 10:16:09,916
been printed individually
15588
10:16:09,916 --> 10:16:11,200
and i think it's a very important
15589
10:16:11,200 --> 10:16:13,276
example interesting example that how a
15590
10:16:13,276 --> 10:16:15,680
string has been converted now
15591
10:16:15,680 --> 10:16:17,840
point to be noted over here is
15592
10:16:17,840 --> 10:16:20,560
that python has internally invoked
15593
10:16:20,560 --> 10:16:22,960
underscore underscore iter and
15594
10:16:22,960 --> 10:16:25,436
underscore underscore next methods which
15595
10:16:25,436 --> 10:16:27,840
it has implicitly implemented for string
15596
10:16:27,840 --> 10:16:29,276
iterators so you don't need to do
15597
10:16:29,276 --> 10:16:31,120
anything
15598
10:16:31,120 --> 10:16:33,756
now let's take another example to how to
15599
10:16:33,756 --> 10:16:35,436
traverse through a list
15600
10:16:35,436 --> 10:16:37,276
using for
15601
10:16:37,276 --> 10:16:38,640
loop
15602
10:16:38,640 --> 10:16:40,960
so i'm going to leave for object in list
15603
10:16:40,960 --> 10:16:44,480
and just print the list this is without
15604
10:16:44,480 --> 10:16:47,120
a nitrater i'm just printing my list so
15605
10:16:47,120 --> 10:16:48,800
i'm just trying to show the difference
15606
10:16:48,800 --> 10:16:50,720
that if you use a nitrater or you do not
15607
10:16:50,720 --> 10:16:54,400
but how you can access the
15608
10:16:54,400 --> 10:16:56,800
elements in the list
15609
10:16:56,800 --> 10:16:58,640
now let's take an example
15610
10:16:58,640 --> 10:17:01,680
of how i can write my own custom
15611
10:17:01,680 --> 10:17:03,436
iterator
15612
10:17:03,436 --> 10:17:05,360
okay so i'm
15613
10:17:05,360 --> 10:17:07,840
creating a class where i'm overriding
15614
10:17:07,840 --> 10:17:09,360
the init method
15615
10:17:09,360 --> 10:17:11,276
basically initializing in is a
15616
10:17:11,276 --> 10:17:12,400
initialization
15617
10:17:12,400 --> 10:17:15,120
of my class where i'm gonna initialize a
15618
10:17:15,120 --> 10:17:18,640
variable max with zero
15619
10:17:18,880 --> 10:17:19,916
and then
15620
10:17:19,916 --> 10:17:22,000
i am overriding this is the most
15621
10:17:22,000 --> 10:17:23,756
important point over here i am
15622
10:17:23,756 --> 10:17:27,756
overriding i t e r method
15623
10:17:27,756 --> 10:17:30,080
and then
15624
10:17:30,080 --> 10:17:32,320
i am writing another overriding the
15625
10:17:32,320 --> 10:17:34,720
underscore underscore next method
15626
10:17:34,720 --> 10:17:36,720
where i am adding
15627
10:17:36,720 --> 10:17:39,200
6 to my number and then
15628
10:17:39,200 --> 10:17:41,200
incrementing my loop
15629
10:17:41,200 --> 10:17:42,880
so that
15630
10:17:42,880 --> 10:17:43,840
i
15631
10:17:43,840 --> 10:17:45,520
can move further
15632
10:17:45,520 --> 10:17:47,840
so in this underscore next method i am
15633
10:17:47,840 --> 10:17:48,640
just
15634
10:17:48,640 --> 10:17:51,596
moving beyond adding 6 and till the time
15635
10:17:51,596 --> 10:17:54,160
i reach the max variable so when i'll
15636
10:17:54,160 --> 10:17:56,240
invoke my class
15637
10:17:56,240 --> 10:17:59,276
i am passing 9 so it means that i should
15638
10:17:59,276 --> 10:18:00,400
not
15639
10:18:00,400 --> 10:18:03,120
go beyond
15640
10:18:03,200 --> 10:18:05,680
10 basically i should start my iteration
15641
10:18:05,680 --> 10:18:08,720
from 6 and will not go beyond 10 9 plus
15642
10:18:08,720 --> 10:18:09,520
1
15643
10:18:09,520 --> 10:18:11,596
so i should get numbers which are
15644
10:18:11,596 --> 10:18:16,000
between 6 to 10 as per my expectation
15645
10:18:16,000 --> 10:18:19,040
now before we diagnose the output most
15646
10:18:19,040 --> 10:18:20,800
important point is that how you have
15647
10:18:20,800 --> 10:18:23,436
converted your class to an iterator over
15648
10:18:23,436 --> 10:18:24,320
here
15649
10:18:24,320 --> 10:18:27,680
you in order to invoke make an a class
15650
10:18:27,680 --> 10:18:30,240
in nitrater it is mandatory you override
15651
10:18:30,240 --> 10:18:32,480
it ar and next method which you have
15652
10:18:32,480 --> 10:18:35,200
done over here
15653
10:18:35,520 --> 10:18:38,720
so now let's print
15654
10:18:42,480 --> 10:18:43,276
okay
15655
10:18:43,276 --> 10:18:45,276
so six so my
15656
10:18:45,276 --> 10:18:47,680
number started with 0 initialized with 0
15657
10:18:47,680 --> 10:18:49,040
and i added
15658
10:18:49,040 --> 10:18:51,840
six to eight so my first result is six
15659
10:18:51,840 --> 10:18:54,480
and now if i keep printing i will get
15660
10:18:54,480 --> 10:18:57,916
one value incremented
15661
10:18:59,916 --> 10:19:02,916
seven
15662
10:19:04,320 --> 10:19:06,640
eight
15663
10:19:09,040 --> 10:19:11,276
nine
15664
10:19:11,276 --> 10:19:13,520
and
15665
10:19:16,240 --> 10:19:19,200
ten right so this is the expected output
15666
10:19:19,200 --> 10:19:21,436
right
15667
10:19:21,916 --> 10:19:22,720
now
15668
10:19:22,720 --> 10:19:24,960
another example on the same lines i'm
15669
10:19:24,960 --> 10:19:27,596
gonna print the first 20 numbers using
15670
10:19:27,596 --> 10:19:28,960
my iterator
15671
10:19:28,960 --> 10:19:30,720
so i am
15672
10:19:30,720 --> 10:19:34,000
going to overwrite the iter method and
15673
10:19:34,000 --> 10:19:36,320
underscore underscore next method my
15674
10:19:36,320 --> 10:19:38,560
numbers now in this
15675
10:19:38,560 --> 10:19:40,560
underscore underscore next method i'm
15676
10:19:40,560 --> 10:19:44,080
just incrementing my number y one
15677
10:19:44,080 --> 10:19:47,040
not by six and i'm gonna stop at the
15678
10:19:47,040 --> 10:19:49,276
limit 20
15679
10:19:49,276 --> 10:19:51,840
so like i showed in my previous example
15680
10:19:51,840 --> 10:19:55,680
i'm gonna initialize first my class
15681
10:19:55,680 --> 10:19:58,240
and then
15682
10:19:58,960 --> 10:20:01,916
uh initialize my iterator and just then
15683
10:20:01,916 --> 10:20:05,520
gonna run a for loop on it
15684
10:20:06,640 --> 10:20:08,800
and here is the response output i have
15685
10:20:08,800 --> 10:20:11,120
printed the first 20 numbers because i
15686
10:20:11,120 --> 10:20:13,276
have created this limit
15687
10:20:13,276 --> 10:20:15,360
so i am initializing with one
15688
10:20:15,360 --> 10:20:18,560
and going till the max 20 number
15689
10:20:18,560 --> 10:20:20,800
but the point is it's not the output
15690
10:20:20,800 --> 10:20:22,640
which is more important over here the
15691
10:20:22,640 --> 10:20:24,560
most important concept over here is that
15692
10:20:24,560 --> 10:20:26,720
you have created your own iterator and
15693
10:20:26,720 --> 10:20:29,120
leveraged it we will talk about what is
15694
10:20:29,120 --> 10:20:31,436
a generator in python we will talk about
15695
10:20:31,436 --> 10:20:34,080
the advantages of using generators
15696
10:20:34,080 --> 10:20:37,200
and the utility of the next function
15697
10:20:37,200 --> 10:20:38,240
which
15698
10:20:38,240 --> 10:20:39,680
allows us to
15699
10:20:39,680 --> 10:20:43,756
operate with generators and print the
15700
10:20:43,756 --> 10:20:45,596
values within the generator so we will
15701
10:20:45,596 --> 10:20:46,960
see these
15702
10:20:46,960 --> 10:20:50,720
all these concepts with an example
15703
10:20:51,200 --> 10:20:53,756
now what is a generator in python
15704
10:20:53,756 --> 10:20:56,480
python generators lets you create your
15705
10:20:56,480 --> 10:20:59,360
own iterator functions that return the
15706
10:20:59,360 --> 10:21:01,840
traversal object with a sequence of
15707
10:21:01,840 --> 10:21:03,040
values
15708
10:21:03,040 --> 10:21:05,840
so basically even with iterators you can
15709
10:21:05,840 --> 10:21:10,080
only work on list tuples or sets but
15710
10:21:10,080 --> 10:21:12,400
with generators you can convert your
15711
10:21:12,400 --> 10:21:15,596
functions into iterator functions itself
15712
10:21:15,596 --> 10:21:17,436
and we will see that when we will be
15713
10:21:17,436 --> 10:21:20,320
working on a jupiter notebook
15714
10:21:20,320 --> 10:21:22,720
now another important feature in a
15715
10:21:22,720 --> 10:21:25,120
generator function is that a yield
15716
10:21:25,120 --> 10:21:27,436
statement is used rather than a return
15717
10:21:27,436 --> 10:21:28,640
statement
15718
10:21:28,640 --> 10:21:30,480
so basically
15719
10:21:30,480 --> 10:21:32,480
the return statement terminates a
15720
10:21:32,480 --> 10:21:34,560
function entirely if you use it within
15721
10:21:34,560 --> 10:21:36,640
the function and you know basically
15722
10:21:36,640 --> 10:21:38,880
return from the statement while the
15723
10:21:38,880 --> 10:21:41,120
yield statement pauses the function
15724
10:21:41,120 --> 10:21:42,320
there it is
15725
10:21:42,320 --> 10:21:44,720
and holding all its states all its
15726
10:21:44,720 --> 10:21:47,200
variables the values in the variables
15727
10:21:47,200 --> 10:21:48,800
and then continue
15728
10:21:48,800 --> 10:21:51,680
thereafter on the successive calls
15729
10:21:51,680 --> 10:21:53,840
so basically in a generator function you
15730
10:21:53,840 --> 10:21:56,480
use yield functions and i will talk
15731
10:21:56,480 --> 10:21:59,840
about the utility of this yield
15732
10:21:59,840 --> 10:22:02,400
statement within the generator function
15733
10:22:02,400 --> 10:22:05,200
how it helps in generating pipeline
15734
10:22:05,200 --> 10:22:07,436
but just to hold a thought over here and
15735
10:22:07,436 --> 10:22:09,596
understand the concept the yield
15736
10:22:09,596 --> 10:22:12,000
statement basically holds
15737
10:22:12,000 --> 10:22:14,080
the processing of the function at that
15738
10:22:14,080 --> 10:22:16,560
point and if you make an invocation of
15739
10:22:16,560 --> 10:22:20,480
that generator function again it starts
15740
10:22:20,480 --> 10:22:23,520
from there itself where you left it
15741
10:22:23,520 --> 10:22:26,160
as compared to return statement return
15742
10:22:26,160 --> 10:22:28,160
is a general exit
15743
10:22:28,160 --> 10:22:29,916
if you make an invocation of the return
15744
10:22:29,916 --> 10:22:31,680
statement from a function it will
15745
10:22:31,680 --> 10:22:34,400
restart again from the beginning if you
15746
10:22:34,400 --> 10:22:36,160
invoke it the next time and that's the
15747
10:22:36,160 --> 10:22:38,640
basic difference
15748
10:22:38,640 --> 10:22:40,800
so what are the advantages of using
15749
10:22:40,800 --> 10:22:44,160
generators it's very easy to implement
15750
10:22:44,160 --> 10:22:47,436
the code syntactically it's very simple
15751
10:22:47,436 --> 10:22:49,120
within the few line of codes you are
15752
10:22:49,120 --> 10:22:51,120
able to achieve
15753
10:22:51,120 --> 10:22:51,916
the
15754
10:22:51,916 --> 10:22:54,400
you know functions and able to implement
15755
10:22:54,400 --> 10:22:55,756
the logic
15756
10:22:55,756 --> 10:22:58,240
you can generate infinite sequence you
15757
10:22:58,240 --> 10:23:00,560
don't need to provide the range values
15758
10:23:00,560 --> 10:23:02,480
the starting and ending you can generate
15759
10:23:02,480 --> 10:23:04,240
an infinite value and i'll show you that
15760
10:23:04,240 --> 10:23:06,000
with an example
15761
10:23:06,000 --> 10:23:08,000
and
15762
10:23:08,000 --> 10:23:10,800
generators are very memory efficient
15763
10:23:10,800 --> 10:23:13,040
they are the most memory efficient ways
15764
10:23:13,040 --> 10:23:15,596
of processing huge data sets
15765
10:23:15,596 --> 10:23:18,240
they process the data incrementally and
15766
10:23:18,240 --> 10:23:20,880
do not allocate memory to all the
15767
10:23:20,880 --> 10:23:23,360
results at the same time they do that
15768
10:23:23,360 --> 10:23:24,800
incrementally
15769
10:23:24,800 --> 10:23:26,720
they really come in handy when
15770
10:23:26,720 --> 10:23:28,880
implementing data science pipelines for
15771
10:23:28,880 --> 10:23:31,276
huge data sets in a resource constraint
15772
10:23:31,276 --> 10:23:33,596
environment where you have
15773
10:23:33,596 --> 10:23:35,680
you know constraint on the ram and the
15774
10:23:35,680 --> 10:23:37,916
other resources of the hardware
15775
10:23:37,916 --> 10:23:40,240
now another biggest advantage is
15776
10:23:40,240 --> 10:23:42,560
pipelining with generators
15777
10:23:42,560 --> 10:23:44,960
generator pipelines are a great way to
15778
10:23:44,960 --> 10:23:48,080
break apart complex processing into
15779
10:23:48,080 --> 10:23:50,720
smaller pieces when processing list of
15780
10:23:50,720 --> 10:23:53,276
items so basically you can
15781
10:23:53,276 --> 10:23:55,436
break your complex code into smaller
15782
10:23:55,436 --> 10:23:57,360
pieces and then
15783
10:23:57,360 --> 10:24:00,320
invoke them as a generator
15784
10:24:00,320 --> 10:24:03,520
recursively and we will see that how
15785
10:24:03,520 --> 10:24:06,240
but basically you are making the complex
15786
10:24:06,240 --> 10:24:09,436
code look simple and making it invoke in
15787
10:24:09,436 --> 10:24:12,720
a pipeline fashion
15788
10:24:12,720 --> 10:24:15,360
now the another most important function
15789
10:24:15,360 --> 10:24:17,916
in order to deal and operate with the
15790
10:24:17,916 --> 10:24:20,000
generator is the next function
15791
10:24:20,000 --> 10:24:22,160
the next function is used to retrieve
15792
10:24:22,160 --> 10:24:24,480
elements from a generator object and
15793
10:24:24,480 --> 10:24:26,640
then print it so that's how you
15794
10:24:26,640 --> 10:24:27,756
basically
15795
10:24:27,756 --> 10:24:30,080
interact with your generate function
15796
10:24:30,080 --> 10:24:32,320
you cannot do that until unless you
15797
10:24:32,320 --> 10:24:35,520
don't use next function
15798
10:24:35,520 --> 10:24:37,120
so now let's quickly
15799
10:24:37,120 --> 10:24:39,756
jump to our examples
15800
10:24:39,756 --> 10:24:41,436
which we are going to support for each
15801
10:24:41,436 --> 10:24:44,000
of these concepts now let's start by
15802
10:24:44,000 --> 10:24:45,200
basically
15803
10:24:45,200 --> 10:24:46,800
creating our
15804
10:24:46,800 --> 10:24:47,680
first
15805
10:24:47,680 --> 10:24:50,720
generator function
15806
10:24:51,200 --> 10:24:52,720
i am going to
15807
10:24:52,720 --> 10:24:55,916
create a variable n initialize it with
15808
10:24:55,916 --> 10:24:57,596
one
15809
10:24:57,596 --> 10:25:00,400
and i'm gonna do a print
15810
10:25:00,400 --> 10:25:02,560
this is just for my
15811
10:25:02,560 --> 10:25:03,520
logging
15812
10:25:03,520 --> 10:25:05,520
and showing on the console that where we
15813
10:25:05,520 --> 10:25:06,640
have reached
15814
10:25:06,640 --> 10:25:10,560
and then i'm gonna say yield n
15815
10:25:10,560 --> 10:25:12,320
okay
15816
10:25:12,320 --> 10:25:15,200
so now here we are not exiting the a
15817
10:25:15,200 --> 10:25:16,880
function we are just
15818
10:25:16,880 --> 10:25:17,840
saying
15819
10:25:17,840 --> 10:25:20,640
that just uh you know this is the point
15820
10:25:20,640 --> 10:25:22,320
if you invoke the function again the
15821
10:25:22,320 --> 10:25:26,480
function will start from this point on
15822
10:25:27,120 --> 10:25:28,480
now here
15823
10:25:28,480 --> 10:25:29,916
you will
15824
10:25:29,916 --> 10:25:32,560
increment
15825
10:25:34,320 --> 10:25:36,800
and again if this is the second
15826
10:25:36,800 --> 10:25:39,436
checkpoint in the code
15827
10:25:39,436 --> 10:25:41,680
and similarly
15828
10:25:41,680 --> 10:25:43,916
this is the we're gonna have a last
15829
10:25:43,916 --> 10:25:46,560
checkpoint in the code
15830
10:25:46,560 --> 10:25:50,240
now in order to
15831
10:25:50,240 --> 10:25:53,436
create my generator now this is the
15832
10:25:53,436 --> 10:25:58,160
my generator initialization my gel
15833
10:25:58,320 --> 10:25:59,840
now if i
15834
10:25:59,840 --> 10:26:02,160
use the next function and invoke my
15835
10:26:02,160 --> 10:26:05,120
generator
15836
10:26:05,120 --> 10:26:06,320
now
15837
10:26:06,320 --> 10:26:08,160
my
15838
10:26:08,160 --> 10:26:10,480
generator has
15839
10:26:10,480 --> 10:26:13,520
been executed till this point right
15840
10:26:13,520 --> 10:26:15,840
n equal to 1
15841
10:26:15,840 --> 10:26:17,680
now if i
15842
10:26:17,680 --> 10:26:21,360
make a second invocation
15843
10:26:30,000 --> 10:26:33,756
now it has moved ahead and printed the
15844
10:26:33,756 --> 10:26:36,720
second so it is behaving like a loop it
15845
10:26:36,720 --> 10:26:38,320
is now behaving like a loop it is
15846
10:26:38,320 --> 10:26:41,120
behaving like a nitrater uh but i have
15847
10:26:41,120 --> 10:26:44,000
not created any list i have not created
15848
10:26:44,000 --> 10:26:47,276
any tuple but i am basically traversing
15849
10:26:47,276 --> 10:26:49,520
through the variables through the
15850
10:26:49,520 --> 10:26:52,160
sequence of the variables within my
15851
10:26:52,160 --> 10:26:54,160
generator
15852
10:26:54,160 --> 10:26:57,040
and if i again
15853
10:26:57,680 --> 10:26:58,960
print it
15854
10:26:58,960 --> 10:27:00,800
this is my last this is printed at the
15855
10:27:00,800 --> 10:27:02,160
last so all the
15856
10:27:02,160 --> 10:27:04,720
checkpoints within my generator have
15857
10:27:04,720 --> 10:27:06,640
been executed
15858
10:27:06,640 --> 10:27:07,680
now
15859
10:27:07,680 --> 10:27:11,040
the same example which i have over here
15860
10:27:11,040 --> 10:27:12,560
let me
15861
10:27:12,560 --> 10:27:14,800
call it in a loop
15862
10:27:14,800 --> 10:27:15,680
okay
15863
10:27:15,680 --> 10:27:18,560
so what i'm gonna do is i'm gonna
15864
10:27:18,560 --> 10:27:20,880
remove these invocations and just
15865
10:27:20,880 --> 10:27:23,520
replace it
15866
10:27:24,720 --> 10:27:28,320
with a loop call right so this generator
15867
10:27:28,320 --> 10:27:30,240
functions remain as is
15868
10:27:30,240 --> 10:27:33,916
now instead of writing my next
15869
10:27:33,916 --> 10:27:35,596
function
15870
10:27:35,596 --> 10:27:36,960
three times
15871
10:27:36,960 --> 10:27:39,436
i am going to just invoke my generator
15872
10:27:39,436 --> 10:27:41,680
in the for loop and this will get
15873
10:27:41,680 --> 10:27:45,040
invoked the number of times i have yield
15874
10:27:45,040 --> 10:27:47,200
in my generator
15875
10:27:47,200 --> 10:27:50,640
and my output should be
15876
10:27:50,640 --> 10:27:53,520
the you know these logs which i have
15877
10:27:53,520 --> 10:27:54,560
created
15878
10:27:54,560 --> 10:27:58,400
and of course the variables
15879
10:28:00,000 --> 10:28:03,200
yeah so see now what has happened is
15880
10:28:03,200 --> 10:28:06,240
now the output is all these three print
15881
10:28:06,240 --> 10:28:08,720
commands have been executed plus the
15882
10:28:08,720 --> 10:28:12,400
variable values of n has been imprinted
15883
10:28:12,400 --> 10:28:14,320
at each at point 1
15884
10:28:14,320 --> 10:28:17,596
2 and 3.
15885
10:28:18,160 --> 10:28:20,720
so when i initialize my item variable
15886
10:28:20,720 --> 10:28:23,596
during the for loop this item consists
15887
10:28:23,596 --> 10:28:25,520
of the variable values which are there
15888
10:28:25,520 --> 10:28:28,000
in my generator
15889
10:28:28,000 --> 10:28:30,720
now i'm going to show you
15890
10:28:30,720 --> 10:28:33,680
example of if i use a return statement
15891
10:28:33,680 --> 10:28:35,360
in my generator
15892
10:28:35,360 --> 10:28:37,200
so i am going to create another
15893
10:28:37,200 --> 10:28:39,680
generator
15894
10:28:39,680 --> 10:28:40,560
and
15895
10:28:40,560 --> 10:28:43,436
provide a value
15896
10:28:48,640 --> 10:28:51,040
now in during this in middle of this
15897
10:28:51,040 --> 10:28:54,560
generator i am just invoking return
15898
10:28:54,560 --> 10:28:57,040
and then i am
15899
10:28:57,040 --> 10:29:00,240
printing my second checkpoint
15900
10:29:00,240 --> 10:29:02,240
and then the
15901
10:29:02,240 --> 10:29:05,120
last checkpoint
15902
10:29:05,520 --> 10:29:07,840
now
15903
10:29:10,960 --> 10:29:15,160
i'm gonna initialize my generator
15904
10:29:20,960 --> 10:29:22,640
now if i invoke
15905
10:29:22,640 --> 10:29:23,520
my
15906
10:29:23,520 --> 10:29:26,320
first element of the generator i'll get
15907
10:29:26,320 --> 10:29:28,960
it there's no problem because i printed
15908
10:29:28,960 --> 10:29:31,916
the first item i called the value of my
15909
10:29:31,916 --> 10:29:34,160
yield and then i return
15910
10:29:34,160 --> 10:29:37,520
but now if i invoke my second element in
15911
10:29:37,520 --> 10:29:39,040
the generator
15912
10:29:39,040 --> 10:29:40,960
but before invoking second i have
15913
10:29:40,960 --> 10:29:43,200
already given the return statement
15914
10:29:43,200 --> 10:29:44,080
my
15915
10:29:44,080 --> 10:29:47,120
generator will exit
15916
10:29:47,120 --> 10:29:49,840
and i'll get the stop iteration error
15917
10:29:49,840 --> 10:29:52,960
now this is a python generated
15918
10:29:52,960 --> 10:29:56,000
exception which is denoting that you
15919
10:29:56,000 --> 10:29:58,640
have you cannot move beyond the first
15920
10:29:58,640 --> 10:30:01,520
item because you have returned from the
15921
10:30:01,520 --> 10:30:03,200
generator over here
15922
10:30:03,200 --> 10:30:06,080
so that's the difference if you just
15923
10:30:06,080 --> 10:30:08,400
remove this comment this out
15924
10:30:08,400 --> 10:30:10,000
and then run it
15925
10:30:10,000 --> 10:30:11,916
you will get your second item so it's
15926
10:30:11,916 --> 10:30:14,880
not the code exits if you write return
15927
10:30:14,880 --> 10:30:17,360
over here so i hope you are able to
15928
10:30:17,360 --> 10:30:20,320
understand the difference
15929
10:30:20,560 --> 10:30:22,720
now let's take another example now here
15930
10:30:22,720 --> 10:30:25,840
we are going to create a list and how
15931
10:30:25,840 --> 10:30:28,720
a implicit list like the way legacy way
15932
10:30:28,720 --> 10:30:30,960
we have been creating in python
15933
10:30:30,960 --> 10:30:33,756
differentiates like the way we are
15934
10:30:33,756 --> 10:30:35,680
creating a generator
15935
10:30:35,680 --> 10:30:37,360
so what i am doing is i am creating a
15936
10:30:37,360 --> 10:30:40,000
list with random numbers
15937
10:30:40,000 --> 10:30:42,960
and then another list in which i am just
15938
10:30:42,960 --> 10:30:44,400
square rooting
15939
10:30:44,400 --> 10:30:47,276
the numbers in that list
15940
10:30:47,276 --> 10:30:50,080
and similarly i am also creating a
15941
10:30:50,080 --> 10:30:51,436
generator
15942
10:30:51,436 --> 10:30:52,400
which
15943
10:30:52,400 --> 10:30:54,480
i am expecting the same output in the
15944
10:30:54,480 --> 10:30:56,400
new list as the generator
15945
10:30:56,400 --> 10:30:58,480
but i am going to show you different
15946
10:30:58,480 --> 10:30:59,596
ways
15947
10:30:59,596 --> 10:31:02,160
of traversing the generator as compared
15948
10:31:02,160 --> 10:31:03,680
to a list
15949
10:31:03,680 --> 10:31:06,240
now if you see this list underscore if i
15950
10:31:06,240 --> 10:31:10,560
just make a print call to this list
15951
10:31:10,560 --> 10:31:12,400
it's going to straightforwardly
15952
10:31:12,400 --> 10:31:14,480
uh square root
15953
10:31:14,480 --> 10:31:17,360
uh the numbers and give me the output
15954
10:31:17,360 --> 10:31:20,080
four nine sixty four and hundred
15955
10:31:20,080 --> 10:31:24,000
but if i print the generator
15956
10:31:24,000 --> 10:31:26,160
i will not get the expected output what
15957
10:31:26,160 --> 10:31:28,160
i'm expecting i will just get the
15958
10:31:28,160 --> 10:31:30,160
generator object which is not what i
15959
10:31:30,160 --> 10:31:32,960
desire so basically you can't just print
15960
10:31:32,960 --> 10:31:34,960
the generator like the list
15961
10:31:34,960 --> 10:31:36,640
you have to
15962
10:31:36,640 --> 10:31:40,400
either use the next or you have to use
15963
10:31:40,400 --> 10:31:43,276
the for loop which i had shown earlier
15964
10:31:43,276 --> 10:31:45,436
and print each and every item within the
15965
10:31:45,436 --> 10:31:48,000
generator
15966
10:31:48,800 --> 10:31:50,400
now this is the output which i was
15967
10:31:50,400 --> 10:31:53,120
expecting i was expecting the
15968
10:31:53,120 --> 10:31:54,560
line by line
15969
10:31:54,560 --> 10:31:57,120
output of each and every element in my
15970
10:31:57,120 --> 10:31:58,240
generator
15971
10:31:58,240 --> 10:32:02,160
4 9 64 100
15972
10:32:02,240 --> 10:32:04,080
okay
15973
10:32:04,080 --> 10:32:06,560
now this for loop is taking care that it
15974
10:32:06,560 --> 10:32:08,400
is not exceeding
15975
10:32:08,400 --> 10:32:10,400
the number of items the length of the
15976
10:32:10,400 --> 10:32:12,400
items in the generator so this is
15977
10:32:12,400 --> 10:32:15,840
automatically taken care by this syntax
15978
10:32:15,840 --> 10:32:18,000
but if you don't take care of it then
15979
10:32:18,000 --> 10:32:19,840
you will get an exception let me show
15980
10:32:19,840 --> 10:32:21,520
you another example
15981
10:32:21,520 --> 10:32:23,756
if i create a list
15982
10:32:23,756 --> 10:32:27,040
and now i'm creating a generator
15983
10:32:27,040 --> 10:32:29,200
which is square
15984
10:32:29,200 --> 10:32:33,680
of square of all my elements
15985
10:32:34,560 --> 10:32:36,640
so now one by one
15986
10:32:36,640 --> 10:32:38,800
instead of the loop i'm one by one
15987
10:32:38,800 --> 10:32:42,160
printing the output
15988
10:32:44,800 --> 10:32:48,000
second output
15989
10:32:49,040 --> 10:32:52,480
third element output
15990
10:32:52,480 --> 10:32:56,240
fourth element output
15991
10:32:57,520 --> 10:33:00,560
okay i have four now if i try to again
15992
10:33:00,560 --> 10:33:01,520
print
15993
10:33:01,520 --> 10:33:03,840
i'm exceeding the
15994
10:33:03,840 --> 10:33:04,960
length of
15995
10:33:04,960 --> 10:33:07,200
the
15996
10:33:07,276 --> 10:33:10,240
list i'm going to get an exception stop
15997
10:33:10,240 --> 10:33:12,240
iteration exception because i'm
15998
10:33:12,240 --> 10:33:14,400
exceeding the length right either i use
15999
10:33:14,400 --> 10:33:15,436
the 4
16000
10:33:15,436 --> 10:33:18,480
or if i'm using individual print and
16001
10:33:18,480 --> 10:33:20,080
next function then i have to take care
16002
10:33:20,080 --> 10:33:23,200
that okay how many elements are there in
16003
10:33:23,200 --> 10:33:26,160
the list and i cannot exceed beyond that
16004
10:33:26,160 --> 10:33:27,840
else the python will
16005
10:33:27,840 --> 10:33:31,240
raise an exception
16006
10:33:32,320 --> 10:33:34,320
okay
16007
10:33:34,320 --> 10:33:37,040
now i'll show another example of
16008
10:33:37,040 --> 10:33:39,596
highlighting the difference between next
16009
10:33:39,596 --> 10:33:40,560
and
16010
10:33:40,560 --> 10:33:44,560
the for loop printing i'm gonna create a
16011
10:33:44,560 --> 10:33:48,400
uh generator for 2 to the power n
16012
10:33:48,400 --> 10:33:50,800
so i'm initializing n with 0
16013
10:33:50,800 --> 10:33:53,360
and then just
16014
10:33:53,360 --> 10:33:55,120
raising it
16015
10:33:55,120 --> 10:33:57,680
2 to the power n
16016
10:33:57,680 --> 10:34:01,040
now here i'm initializing my generator
16017
10:34:01,040 --> 10:34:04,160
and printing it
16018
10:34:07,360 --> 10:34:09,360
and i am getting the expected output as
16019
10:34:09,360 --> 10:34:12,080
2 raised to the power 0 1 okay
16020
10:34:12,080 --> 10:34:16,640
now another way of doing this job is
16021
10:34:16,640 --> 10:34:17,680
i
16022
10:34:17,680 --> 10:34:20,320
loop over this
16023
10:34:20,320 --> 10:34:23,200
and i get the same output so if you see
16024
10:34:23,200 --> 10:34:24,800
it's only
16025
10:34:24,800 --> 10:34:28,720
i do not have only one element so either
16026
10:34:28,720 --> 10:34:32,080
you use for loop or you print using next
16027
10:34:32,080 --> 10:34:33,840
the output is going to be
16028
10:34:33,840 --> 10:34:35,520
same
16029
10:34:35,520 --> 10:34:37,916
now another important concept which i
16030
10:34:37,916 --> 10:34:38,960
talked about
16031
10:34:38,960 --> 10:34:42,880
if you want to print infinite infinite
16032
10:34:42,880 --> 10:34:44,000
numbers
16033
10:34:44,000 --> 10:34:45,200
so
16034
10:34:45,200 --> 10:34:46,640
or you want to do some infinite
16035
10:34:46,640 --> 10:34:48,080
calculations
16036
10:34:48,080 --> 10:34:50,400
i'm going to use print in
16037
10:34:50,400 --> 10:34:51,360
all
16038
10:34:51,360 --> 10:34:54,680
even numbers
16039
10:34:56,960 --> 10:34:58,240
so i'm gonna
16040
10:34:58,240 --> 10:34:59,436
write an
16041
10:34:59,436 --> 10:35:00,640
generator
16042
10:35:00,640 --> 10:35:02,320
which is gonna print
16043
10:35:02,320 --> 10:35:06,560
infinite number of even numbers
16044
10:35:08,160 --> 10:35:10,400
so i've created a generator
16045
10:35:10,400 --> 10:35:12,640
and now i'm going to invoke this
16046
10:35:12,640 --> 10:35:14,640
and mind you i have to kill this job
16047
10:35:14,640 --> 10:35:17,916
because it's going to keep running
16048
10:35:19,756 --> 10:35:21,596
it's going to keep running keep running
16049
10:35:21,596 --> 10:35:23,916
because it's constantly generating the
16050
10:35:23,916 --> 10:35:25,840
numbers even number
16051
10:35:25,840 --> 10:35:28,560
so i would have to stop this
16052
10:35:28,560 --> 10:35:30,880
but if you see
16053
10:35:30,880 --> 10:35:33,360
otherwise this is not possible in any
16054
10:35:33,360 --> 10:35:35,360
for loop the basic for loop you have to
16055
10:35:35,360 --> 10:35:38,160
give a range start and end of the length
16056
10:35:38,160 --> 10:35:40,640
but using a generator you can do
16057
10:35:40,640 --> 10:35:42,720
infinite
16058
10:35:42,720 --> 10:35:45,200
looping
16059
10:35:45,596 --> 10:35:47,436
right because here i have to kill this
16060
10:35:47,436 --> 10:35:50,480
job i have to do a keyboard interrupt
16061
10:35:50,480 --> 10:35:53,120
now i'm going to show you an example of
16062
10:35:53,120 --> 10:35:55,756
how you can use generators for creating
16063
10:35:55,756 --> 10:35:58,000
a pipeline
16064
10:35:58,000 --> 10:35:59,680
so i'm going to take example of
16065
10:35:59,680 --> 10:36:02,800
fibonacci series
16066
10:36:03,040 --> 10:36:05,120
create an example of fibonacci two
16067
10:36:05,120 --> 10:36:08,560
variables initialized with zero and one
16068
10:36:08,560 --> 10:36:10,000
and now
16069
10:36:10,000 --> 10:36:11,120
i'm going to
16070
10:36:11,120 --> 10:36:13,436
write the logic of fibonacci adding the
16071
10:36:13,436 --> 10:36:16,000
two numbers
16072
10:36:16,000 --> 10:36:18,840
to itself
16073
10:36:18,840 --> 10:36:22,400
subsequently and i'm gonna add yield
16074
10:36:22,400 --> 10:36:24,800
now this is my generator this is my one
16075
10:36:24,800 --> 10:36:26,000
generator
16076
10:36:26,000 --> 10:36:28,720
and i'm gonna create second generator of
16077
10:36:28,720 --> 10:36:30,960
square
16078
10:36:30,960 --> 10:36:32,320
so i'm gonna
16079
10:36:32,320 --> 10:36:34,400
show you in excel
16080
10:36:34,400 --> 10:36:36,480
what logic i'm going to apply and what
16081
10:36:36,480 --> 10:36:39,916
will be my expected output
16082
10:36:41,276 --> 10:36:44,480
so i am now squaring the numbers which
16083
10:36:44,480 --> 10:36:46,880
i'll get out as an output of
16084
10:36:46,880 --> 10:36:49,840
fibonacci
16085
10:36:49,840 --> 10:36:52,160
okay
16086
10:36:52,400 --> 10:36:53,360
now
16087
10:36:53,360 --> 10:36:55,680
i'm gonna
16088
10:36:55,680 --> 10:36:58,080
i'm creating a pipeline
16089
10:36:58,080 --> 10:36:59,916
like this
16090
10:36:59,916 --> 10:37:03,200
first i'm gonna generate fibonacci
16091
10:37:03,200 --> 10:37:06,400
series for the first 10 numbers
16092
10:37:06,400 --> 10:37:07,276
then
16093
10:37:07,276 --> 10:37:10,000
i'm gonna find the square of each of the
16094
10:37:10,000 --> 10:37:12,480
fibonacci numbers and add it
16095
10:37:12,480 --> 10:37:14,240
so if you see this
16096
10:37:14,240 --> 10:37:16,400
representation in excel
16097
10:37:16,400 --> 10:37:18,080
i'm going to generate
16098
10:37:18,080 --> 10:37:19,520
and these are the two variables which
16099
10:37:19,520 --> 10:37:21,520
i've already initialized x and y and
16100
10:37:21,520 --> 10:37:22,320
from
16101
10:37:22,320 --> 10:37:24,240
here on these
16102
10:37:24,240 --> 10:37:25,200
10
16103
10:37:25,200 --> 10:37:28,400
are my first 10 fibonacci numbers
16104
10:37:28,400 --> 10:37:31,436
i'm going to find calculate the square
16105
10:37:31,436 --> 10:37:35,520
of these numbers 1 4 9 25
16106
10:37:35,520 --> 10:37:39,360
and till 55 the square of 3 0 2 5 and
16107
10:37:39,360 --> 10:37:41,916
then add these numbers and i should get
16108
10:37:41,916 --> 10:37:46,480
the output of 4895 so if you see one
16109
10:37:46,480 --> 10:37:48,720
function generator second generator and
16110
10:37:48,720 --> 10:37:52,480
the third calculation now this hole
16111
10:37:52,480 --> 10:37:55,520
has been done in one single line of code
16112
10:37:55,520 --> 10:37:57,436
you have generated created two
16113
10:37:57,436 --> 10:37:58,640
generators
16114
10:37:58,640 --> 10:38:00,960
and just invoked them in a single line
16115
10:38:00,960 --> 10:38:02,080
pipeline
16116
10:38:02,080 --> 10:38:04,240
so entire looping
16117
10:38:04,240 --> 10:38:05,520
and you know
16118
10:38:05,520 --> 10:38:06,880
you don't have to write the looping
16119
10:38:06,880 --> 10:38:09,040
operators the for loop and you do not
16120
10:38:09,040 --> 10:38:11,200
worry about writing all that complex
16121
10:38:11,200 --> 10:38:14,800
logic it is all encapsulated within this
16122
10:38:14,800 --> 10:38:16,320
now let me run and i'm going to expect
16123
10:38:16,320 --> 10:38:19,916
this output 4895
16124
10:38:21,520 --> 10:38:23,756
and here is my output right so in the
16125
10:38:23,756 --> 10:38:26,560
hindsight what python has done is it has
16126
10:38:26,560 --> 10:38:29,276
generated these fibonacci numbers found
16127
10:38:29,276 --> 10:38:30,800
the square of these numbers and then
16128
10:38:30,800 --> 10:38:33,120
added it in single line
16129
10:38:33,120 --> 10:38:35,120
and now this is the convenience
16130
10:38:35,120 --> 10:38:36,880
simplicity which i was talking about
16131
10:38:36,880 --> 10:38:38,320
earlier
16132
10:38:38,320 --> 10:38:40,320
now let's take another example of
16133
10:38:40,320 --> 10:38:41,916
fibonacci
16134
10:38:41,916 --> 10:38:45,360
i'm going to create a generator again
16135
10:38:45,360 --> 10:38:47,600
initializing a and b with 0 1
16136
10:38:47,600 --> 10:38:49,040
[Music]
16137
10:38:49,040 --> 10:38:54,360
and this time i'm taking an example of
16138
10:38:55,360 --> 10:38:57,360
printing the fibonacci in two different
16139
10:38:57,360 --> 10:38:59,680
ways
16140
10:39:00,000 --> 10:39:01,040
okay
16141
10:39:01,040 --> 10:39:02,560
i'm gonna print
16142
10:39:02,560 --> 10:39:04,080
the first
16143
10:39:04,080 --> 10:39:07,596
five fibonacci series number
16144
10:39:07,596 --> 10:39:10,400
the first way i'm gonna do is i'm gonna
16145
10:39:10,400 --> 10:39:11,520
print
16146
10:39:11,520 --> 10:39:13,840
the next function five times because i'm
16147
10:39:13,840 --> 10:39:15,360
generating five
16148
10:39:15,360 --> 10:39:17,276
so let me run this
16149
10:39:17,276 --> 10:39:19,520
these are the first five fibonacci
16150
10:39:19,520 --> 10:39:22,160
number zero one one two and 3
16151
10:39:22,160 --> 10:39:24,960
and i am also going to use a
16152
10:39:24,960 --> 10:39:27,200
loop
16153
10:39:29,120 --> 10:39:32,000
in order to print
16154
10:39:32,480 --> 10:39:35,680
so now in the loop i am invoking the 5
16155
10:39:35,680 --> 10:39:37,840
fibonacci numbers
16156
10:39:37,840 --> 10:39:39,680
and run it
16157
10:39:39,680 --> 10:39:42,000
so either you can
16158
10:39:42,000 --> 10:39:44,240
make individual five individual calls or
16159
10:39:44,240 --> 10:39:46,960
you can use the for loop but the output
16160
10:39:46,960 --> 10:39:47,916
is the
16161
10:39:47,916 --> 10:39:49,040
same
16162
10:39:49,040 --> 10:39:51,436
and you are using a generator so what is
16163
10:39:51,436 --> 10:39:53,840
string formatting in python formatting a
16164
10:39:53,840 --> 10:39:55,916
string is something you will do all the
16165
10:39:55,916 --> 10:39:58,560
time when you are coding in python
16166
10:39:58,560 --> 10:40:01,120
string formatting also known as string
16167
10:40:01,120 --> 10:40:03,040
interpolation is the process of
16168
10:40:03,040 --> 10:40:05,120
inserting a custom string or variable in
16169
10:40:05,120 --> 10:40:06,640
predefined text
16170
10:40:06,640 --> 10:40:08,640
the string formatting methods
16171
10:40:08,640 --> 10:40:10,480
format the given string into a more
16172
10:40:10,480 --> 10:40:12,400
precise output in python
16173
10:40:12,400 --> 10:40:14,560
so it is the process of infusing things
16174
10:40:14,560 --> 10:40:16,640
in the string dynamically and presenting
16175
10:40:16,640 --> 10:40:19,120
the string now let's look at the
16176
10:40:19,120 --> 10:40:21,200
different types of string formatting in
16177
10:40:21,200 --> 10:40:23,916
python first one we have is using the
16178
10:40:23,916 --> 10:40:26,720
modulo operator so python uses c
16179
10:40:26,720 --> 10:40:28,800
formatting style string formatting to
16180
10:40:28,800 --> 10:40:31,680
create new formatted strings python does
16181
10:40:31,680 --> 10:40:33,436
that with the help of the modulo or the
16182
10:40:33,436 --> 10:40:34,880
percentage sign
16183
10:40:34,880 --> 10:40:36,480
this is the old way of formatting
16184
10:40:36,480 --> 10:40:38,880
strings this approach was more often
16185
10:40:38,880 --> 10:40:40,560
used in python 2.
16186
10:40:40,560 --> 10:40:41,916
so let's head out to our jupyter
16187
10:40:41,916 --> 10:40:44,080
notebook and see how this works
16188
10:40:44,080 --> 10:40:46,320
so this is my jupiter notebook that i'm
16189
10:40:46,320 --> 10:40:48,800
going to use for our demo
16190
10:40:48,800 --> 10:40:50,800
so let's start with our first example
16191
10:40:50,800 --> 10:40:53,200
that is formatting a string using the
16192
10:40:53,200 --> 10:40:54,960
modulo operator first i'll create a
16193
10:40:54,960 --> 10:40:57,276
variable called name let's see the name
16194
10:40:57,276 --> 10:41:00,960
is sean and i have another variable
16195
10:41:00,960 --> 10:41:04,800
which has the age of sean let's say 32
16196
10:41:04,800 --> 10:41:07,040
now i can create a variable called
16197
10:41:07,040 --> 10:41:08,480
string
16198
10:41:08,480 --> 10:41:10,400
equal to i'll say
16199
10:41:10,400 --> 10:41:11,116
hi
16200
10:41:11,116 --> 10:41:13,680
then use the modulo operator followed by
16201
10:41:13,680 --> 10:41:14,480
s
16202
10:41:14,480 --> 10:41:16,960
and i'll give an exclamation mark
16203
10:41:16,960 --> 10:41:18,480
give a space
16204
10:41:18,480 --> 10:41:20,080
i'll say
16205
10:41:20,080 --> 10:41:21,436
percentage
16206
10:41:21,436 --> 10:41:22,560
name
16207
10:41:22,560 --> 10:41:24,640
and then let's
16208
10:41:24,640 --> 10:41:27,276
print the string
16209
10:41:27,276 --> 10:41:28,480
if i run this
16210
10:41:28,480 --> 10:41:30,560
we have the output hi
16211
10:41:30,560 --> 10:41:33,116
shawn so what i did here was
16212
10:41:33,116 --> 10:41:35,360
we have defined the variables that we
16213
10:41:35,360 --> 10:41:38,240
want to format into the strings
16214
10:41:38,240 --> 10:41:41,040
then we have created the string with the
16215
10:41:41,040 --> 10:41:43,116
placeholders for the variables so this
16216
10:41:43,116 --> 10:41:44,640
is a placeholder
16217
10:41:44,640 --> 10:41:47,276
now the percentage s format specified
16218
10:41:47,276 --> 10:41:48,880
here tells python to substitute the
16219
10:41:48,880 --> 10:41:50,080
value of the
16220
10:41:50,080 --> 10:41:50,960
name
16221
10:41:50,960 --> 10:41:54,000
so hence we have high sean printed
16222
10:41:54,000 --> 10:41:56,480
now let's see another example
16223
10:41:56,480 --> 10:41:58,080
this time i'll directly use a print
16224
10:41:58,080 --> 10:41:59,360
statement
16225
10:41:59,360 --> 10:42:00,560
i'll say
16226
10:42:00,560 --> 10:42:02,000
i like
16227
10:42:02,000 --> 10:42:03,276
to
16228
10:42:03,276 --> 10:42:06,240
then i'll say percentage s
16229
10:42:06,240 --> 10:42:07,756
or modulo s
16230
10:42:07,756 --> 10:42:08,880
chocolate
16231
10:42:08,880 --> 10:42:11,840
ice creams i'll give a full stop
16232
10:42:11,840 --> 10:42:15,116
and then using my modular operator i'll
16233
10:42:15,116 --> 10:42:18,560
pass in my value that is eat so here
16234
10:42:18,560 --> 10:42:20,960
percentage s will be replaced by the
16235
10:42:20,960 --> 10:42:22,800
value 8.
16236
10:42:22,800 --> 10:42:25,360
now if i run this you have the message
16237
10:42:25,360 --> 10:42:27,680
here i like to eat chocolate ice creams
16238
10:42:27,680 --> 10:42:29,276
now you can also add
16239
10:42:29,276 --> 10:42:31,756
multiple strings at a time and you can
16240
10:42:31,756 --> 10:42:35,040
use variables to insert objects in the
16241
10:42:35,040 --> 10:42:38,080
string let's see one more example i'll
16242
10:42:38,080 --> 10:42:41,916
say string equal to say percentage s
16243
10:42:41,916 --> 10:42:43,756
is i'll say
16244
10:42:43,756 --> 10:42:45,436
percentage d
16245
10:42:45,436 --> 10:42:47,436
years old
16246
10:42:47,436 --> 10:42:48,320
then
16247
10:42:48,320 --> 10:42:50,720
i'll give a percentage symbol and say
16248
10:42:50,720 --> 10:42:52,800
name comma age
16249
10:42:52,800 --> 10:42:54,480
and let's
16250
10:42:54,480 --> 10:42:57,200
print the string
16251
10:42:57,200 --> 10:42:58,880
if i run this
16252
10:42:58,880 --> 10:43:01,200
you see here above we had defined the
16253
10:43:01,200 --> 10:43:05,040
variables name in age to be sean and 32
16254
10:43:05,040 --> 10:43:08,560
so here percentage s is for name so so
16255
10:43:08,560 --> 10:43:09,680
on
16256
10:43:09,680 --> 10:43:10,640
is
16257
10:43:10,640 --> 10:43:12,480
percentage d
16258
10:43:12,480 --> 10:43:14,800
is for age so son is
16259
10:43:14,800 --> 10:43:17,276
32 years old
16260
10:43:17,276 --> 10:43:20,480
so in this example percentage d format
16261
10:43:20,480 --> 10:43:22,000
specifier tells python where to
16262
10:43:22,000 --> 10:43:24,720
substitute the value for h
16263
10:43:24,720 --> 10:43:27,200
and it is represented as a string so
16264
10:43:27,200 --> 10:43:29,040
percentage s
16265
10:43:29,040 --> 10:43:31,436
is used to inject strings similarly
16266
10:43:31,436 --> 10:43:34,400
percentage d is used to inject integers
16267
10:43:34,400 --> 10:43:36,720
so here 32 is an integer and you can
16268
10:43:36,720 --> 10:43:39,116
also use percentage f for floating point
16269
10:43:39,116 --> 10:43:42,960
values and you can also use percentage b
16270
10:43:42,960 --> 10:43:45,596
for binary format
16271
10:43:45,596 --> 10:43:46,840
all right
16272
10:43:46,840 --> 10:43:50,480
now let's see another example
16273
10:43:50,480 --> 10:43:52,320
let's say i have a variable called x
16274
10:43:52,320 --> 10:43:53,276
which has
16275
10:43:53,276 --> 10:43:55,596
a value dance
16276
10:43:55,596 --> 10:43:56,720
i'll say
16277
10:43:56,720 --> 10:43:58,080
print
16278
10:43:58,080 --> 10:43:59,276
nancy
16279
10:43:59,276 --> 10:44:00,320
can
16280
10:44:00,320 --> 10:44:03,040
i'll say percentage s and
16281
10:44:03,040 --> 10:44:05,040
percentage
16282
10:44:05,040 --> 10:44:06,000
s
16283
10:44:06,000 --> 10:44:07,040
2
16284
10:44:07,040 --> 10:44:09,116
after this
16285
10:44:09,116 --> 10:44:10,240
we'll
16286
10:44:10,240 --> 10:44:11,596
specify
16287
10:44:11,596 --> 10:44:15,116
the parameters let's say x and
16288
10:44:15,116 --> 10:44:16,560
sing
16289
10:44:16,560 --> 10:44:17,756
if i run this
16290
10:44:17,756 --> 10:44:20,160
you have the output nancy can
16291
10:44:20,160 --> 10:44:23,116
so here percentage s is for x which
16292
10:44:23,116 --> 10:44:26,400
means dance so nancy can dance and
16293
10:44:26,400 --> 10:44:28,960
percentage s is for the second
16294
10:44:28,960 --> 10:44:31,276
parameter which is saying so my result
16295
10:44:31,276 --> 10:44:34,880
becomes nancy can dance and sing two
16296
10:44:34,880 --> 10:44:37,040
now similarly we can have another
16297
10:44:37,040 --> 10:44:38,720
example using
16298
10:44:38,720 --> 10:44:41,840
percentage f let's say i'll say print
16299
10:44:41,840 --> 10:44:43,116
the
16300
10:44:43,116 --> 10:44:45,436
value of
16301
10:44:45,436 --> 10:44:46,160
e
16302
10:44:46,160 --> 10:44:47,680
is
16303
10:44:47,680 --> 10:44:50,160
now i'll specify my format which is
16304
10:44:50,160 --> 10:44:53,436
going to be let's say percentage 5 dot
16305
10:44:53,436 --> 10:44:56,400
2 f close the quotation and then i'll
16306
10:44:56,400 --> 10:44:59,596
pass in my value for e let's say
16307
10:44:59,596 --> 10:45:02,080
2 4 5 5
16308
10:45:02,080 --> 10:45:02,880
3
16309
10:45:02,880 --> 10:45:03,756
dot
16310
10:45:03,756 --> 10:45:06,720
i am just taking random floating values
16311
10:45:06,720 --> 10:45:10,560
7 1 8 2 8 1. if i run this you see the
16312
10:45:10,560 --> 10:45:13,360
result my value for e is
16313
10:45:13,360 --> 10:45:16,360
24553.72
16314
10:45:17,200 --> 10:45:19,756
now let me tell you what
16315
10:45:19,756 --> 10:45:20,720
5
16316
10:45:20,720 --> 10:45:23,916
dot 2f means so here 5
16317
10:45:23,916 --> 10:45:25,756
would be the minimum number of digits to
16318
10:45:25,756 --> 10:45:28,240
be present in the string these might be
16319
10:45:28,240 --> 10:45:30,240
padded with white spaces if the whole
16320
10:45:30,240 --> 10:45:32,800
number doesn't have this many digits
16321
10:45:32,800 --> 10:45:35,596
and here 2f represents
16322
10:45:35,596 --> 10:45:37,116
how many digits are to be displayed
16323
10:45:37,116 --> 10:45:39,116
after the decimal point so after the
16324
10:45:39,116 --> 10:45:41,200
decimal point we wanted only two digits
16325
10:45:41,200 --> 10:45:43,436
so we have rounded it up and the value
16326
10:45:43,436 --> 10:45:44,240
is
16327
10:45:44,240 --> 10:45:46,160
seven two
16328
10:45:46,160 --> 10:45:47,116
all right
16329
10:45:47,116 --> 10:45:48,960
so coming to the
16330
10:45:48,960 --> 10:45:52,000
next type of string formatting in python
16331
10:45:52,000 --> 10:45:54,720
we have formatting with the format
16332
10:45:54,720 --> 10:45:56,720
string method
16333
10:45:56,720 --> 10:45:59,040
so python string dot format method was
16334
10:45:59,040 --> 10:46:02,240
introduced in version 2.6 now there are
16335
10:46:02,240 --> 10:46:04,400
two ways to do it one is using
16336
10:46:04,400 --> 10:46:06,240
positional arguments and the other is
16337
10:46:06,240 --> 10:46:08,400
using keyword arguments
16338
10:46:08,400 --> 10:46:09,756
so let's head over to our jupyter
16339
10:46:09,756 --> 10:46:11,200
notebook and we'll explore a few
16340
10:46:11,200 --> 10:46:15,116
examples on this before that let me just
16341
10:46:15,116 --> 10:46:16,800
give a
16342
10:46:16,800 --> 10:46:19,840
few comments so the first one we saw was
16343
10:46:19,840 --> 10:46:20,880
using
16344
10:46:20,880 --> 10:46:23,436
modulo operator now the second one is
16345
10:46:23,436 --> 10:46:25,436
not an operator this time we are going
16346
10:46:25,436 --> 10:46:26,960
to use the
16347
10:46:26,960 --> 10:46:29,520
format method cool
16348
10:46:29,520 --> 10:46:31,360
if getting your learning started is half
16349
10:46:31,360 --> 10:46:33,436
the battle what if you could do that for
16350
10:46:33,436 --> 10:46:36,240
free visit skill up by simply learn
16351
10:46:36,240 --> 10:46:38,160
click on the link in the description to
16352
10:46:38,160 --> 10:46:39,596
know more
16353
10:46:39,596 --> 10:46:43,436
so under format method we'll use empty
16354
10:46:43,436 --> 10:46:46,240
placeholder and replace them with a
16355
10:46:46,240 --> 10:46:48,080
string value
16356
10:46:48,080 --> 10:46:49,680
so
16357
10:46:49,680 --> 10:46:53,116
i'll write a print statement
16358
10:46:53,436 --> 10:46:55,756
let's say my
16359
10:46:55,756 --> 10:46:57,436
message is
16360
10:46:57,436 --> 10:47:00,400
welcome to now i'm creating
16361
10:47:00,400 --> 10:47:02,800
one placeholder which is empty you can
16362
10:47:02,800 --> 10:47:05,680
see here and let's say after that i have
16363
10:47:05,680 --> 10:47:07,276
tutorials
16364
10:47:07,276 --> 10:47:09,756
i'll give a full stop and then we are
16365
10:47:09,756 --> 10:47:11,200
going to use the
16366
10:47:11,200 --> 10:47:14,080
format method and inside the format
16367
10:47:14,080 --> 10:47:15,916
method we are going to
16368
10:47:15,916 --> 10:47:20,640
pass in the value let's say simply loan
16369
10:47:20,640 --> 10:47:21,596
so this
16370
10:47:21,596 --> 10:47:25,040
simply learned value will be substituted
16371
10:47:25,040 --> 10:47:27,360
in this empty placeholder
16372
10:47:27,360 --> 10:47:29,200
let's run it you can see the result we
16373
10:47:29,200 --> 10:47:30,160
have
16374
10:47:30,160 --> 10:47:32,880
welcome to simply learn python tutorial
16375
10:47:32,880 --> 10:47:33,916
so
16376
10:47:33,916 --> 10:47:36,560
this empty placeholder was replaced with
16377
10:47:36,560 --> 10:47:40,080
a string value that is simply alone
16378
10:47:40,080 --> 10:47:41,276
cool
16379
10:47:41,276 --> 10:47:42,880
now moving to the
16380
10:47:42,880 --> 10:47:46,080
next example so we'll be using index or
16381
10:47:46,080 --> 10:47:47,596
positional arguments inside the
16382
10:47:47,596 --> 10:47:49,040
placeholder
16383
10:47:49,040 --> 10:47:52,080
let me create a variable called s1
16384
10:47:52,080 --> 10:47:55,840
and i'll type in a message let's say
16385
10:47:55,840 --> 10:47:57,840
my message is let's
16386
10:47:57,840 --> 10:48:01,360
i'm using my first placeholder and i'm
16387
10:48:01,360 --> 10:48:02,640
giving a
16388
10:48:02,640 --> 10:48:04,640
position or an index
16389
10:48:04,640 --> 10:48:06,720
now in python you must be aware that the
16390
10:48:06,720 --> 10:48:08,400
index starts from zero
16391
10:48:08,400 --> 10:48:10,880
so i've used curly braces and passed in
16392
10:48:10,880 --> 10:48:13,116
zero i'll create another placeholder and
16393
10:48:13,116 --> 10:48:14,480
this time i'll put
16394
10:48:14,480 --> 10:48:16,880
index as one let's say my message
16395
10:48:16,880 --> 10:48:19,436
continues like this with
16396
10:48:19,436 --> 10:48:21,756
simply loan
16397
10:48:21,756 --> 10:48:22,960
and then
16398
10:48:22,960 --> 10:48:25,520
we'll use the format method
16399
10:48:25,520 --> 10:48:27,436
and we'll pass in the values for these
16400
10:48:27,436 --> 10:48:29,596
two placeholders
16401
10:48:29,596 --> 10:48:31,916
it should be width
16402
10:48:31,916 --> 10:48:34,480
let's say my value for the
16403
10:48:34,480 --> 10:48:36,400
zeroth index is
16404
10:48:36,400 --> 10:48:37,436
learn
16405
10:48:37,436 --> 10:48:38,560
and
16406
10:48:38,560 --> 10:48:42,320
my value for the first index is upscale
16407
10:48:42,320 --> 10:48:43,840
i'll go ahead and
16408
10:48:43,840 --> 10:48:46,960
print s1 and you can see the output
16409
10:48:46,960 --> 10:48:49,116
let's learn and up skill with simply
16410
10:48:49,116 --> 10:48:52,240
learn so here the zeroth and the first
16411
10:48:52,240 --> 10:48:55,040
index were replaced by learn and upscale
16412
10:48:55,040 --> 10:48:58,080
respectively so this is how you can use
16413
10:48:58,080 --> 10:49:00,320
index or positional arguments inside the
16414
10:49:00,320 --> 10:49:02,720
placeholders now you can also
16415
10:49:02,720 --> 10:49:05,360
add index numbers into the placeholders
16416
10:49:05,360 --> 10:49:08,160
to reorder values now this will affect
16417
10:49:08,160 --> 10:49:09,916
the order in which the method replaces
16418
10:49:09,916 --> 10:49:12,400
the placeholders let me show you so i'll
16419
10:49:12,400 --> 10:49:14,240
create a print statement
16420
10:49:14,240 --> 10:49:16,080
so let me create my first empty
16421
10:49:16,080 --> 10:49:18,080
placeholder and then i'll continue with
16422
10:49:18,080 --> 10:49:19,916
my message
16423
10:49:19,916 --> 10:49:21,680
let's say called
16424
10:49:21,680 --> 10:49:24,720
then i'll have my second placeholder
16425
10:49:24,720 --> 10:49:26,720
and i'll continue with my message to
16426
10:49:26,720 --> 10:49:28,800
inform that
16427
10:49:28,800 --> 10:49:31,360
then i'll have my third placeholder
16428
10:49:31,360 --> 10:49:33,916
will be absent
16429
10:49:33,916 --> 10:49:34,960
okay
16430
10:49:34,960 --> 10:49:36,160
now
16431
10:49:36,160 --> 10:49:39,680
let's use the dot
16432
10:49:39,680 --> 10:49:42,320
format method and we'll
16433
10:49:42,320 --> 10:49:44,160
have the values for all the three
16434
10:49:44,160 --> 10:49:47,200
placeholders let's see my first value is
16435
10:49:47,200 --> 10:49:48,640
luke
16436
10:49:48,640 --> 10:49:50,400
my second value is
16437
10:49:50,400 --> 10:49:53,040
dina and my third value is
16438
10:49:53,040 --> 10:49:56,080
john now if i print okay there is some
16439
10:49:56,080 --> 10:49:57,756
error here okay
16440
10:49:57,756 --> 10:49:59,520
there has to be another
16441
10:49:59,520 --> 10:50:02,240
bracket here now if i run it you can see
16442
10:50:02,240 --> 10:50:03,756
the message here
16443
10:50:03,756 --> 10:50:06,720
so the first placeholder was for luke so
16444
10:50:06,720 --> 10:50:08,080
luke called
16445
10:50:08,080 --> 10:50:10,480
dyna which is my second value to inform
16446
10:50:10,480 --> 10:50:11,596
that
16447
10:50:11,596 --> 10:50:14,080
my third placeholders value that john
16448
10:50:14,080 --> 10:50:15,840
will be absent
16449
10:50:15,840 --> 10:50:16,640
cool
16450
10:50:16,640 --> 10:50:19,276
now you can
16451
10:50:19,360 --> 10:50:21,360
add index numbers into the placeholders
16452
10:50:21,360 --> 10:50:23,756
to reorder the values now let me show
16453
10:50:23,756 --> 10:50:26,080
you how to do it i'll just
16454
10:50:26,080 --> 10:50:27,200
copy this
16455
10:50:27,200 --> 10:50:30,480
message and i'll print it here
16456
10:50:30,480 --> 10:50:33,200
and here i can now pass in my
16457
10:50:33,200 --> 10:50:36,800
index values let's say i'll add 2
16458
10:50:36,800 --> 10:50:38,560
0 and 1
16459
10:50:38,560 --> 10:50:40,800
so here
16460
10:50:40,800 --> 10:50:42,480
luke is for 0
16461
10:50:42,480 --> 10:50:47,116
dyna is for 1 and john is for second
16462
10:50:47,116 --> 10:50:50,240
index value now if i run it
16463
10:50:50,240 --> 10:50:51,756
now you can see the
16464
10:50:51,756 --> 10:50:54,320
difference in my message so here first
16465
10:50:54,320 --> 10:50:57,360
two comes which means john called
16466
10:50:57,360 --> 10:51:00,160
the value at zeroth index which is luke
16467
10:51:00,160 --> 10:51:03,276
so john called luke to inform that dinah
16468
10:51:03,276 --> 10:51:05,360
will be absent
16469
10:51:05,360 --> 10:51:07,756
all right
16470
10:51:07,916 --> 10:51:10,640
now let's see one more example this time
16471
10:51:10,640 --> 10:51:13,276
we'll be using name or keyword arguments
16472
10:51:13,276 --> 10:51:15,680
inside the placeholder in the earlier
16473
10:51:15,680 --> 10:51:17,916
examples we saw how to use positional
16474
10:51:17,916 --> 10:51:19,040
arguments
16475
10:51:19,040 --> 10:51:20,400
or index
16476
10:51:20,400 --> 10:51:22,000
this time we are going to use name or
16477
10:51:22,000 --> 10:51:23,116
keyword arguments inside the
16478
10:51:23,116 --> 10:51:24,960
placeholders
16479
10:51:24,960 --> 10:51:26,720
so i'll create
16480
10:51:26,720 --> 10:51:29,276
another variable called s2
16481
10:51:29,276 --> 10:51:30,800
and
16482
10:51:30,800 --> 10:51:34,480
i'll have my message let's see v
16483
10:51:34,480 --> 10:51:37,116
in my first placeholder i'll have the
16484
10:51:37,116 --> 10:51:39,520
keyboard as a then i'll have my keyword
16485
10:51:39,520 --> 10:51:43,116
as b my message will continue
16486
10:51:43,116 --> 10:51:46,240
i'll have my third place holder as c
16487
10:51:46,240 --> 10:51:47,840
with
16488
10:51:47,840 --> 10:51:50,480
simply loan
16489
10:51:50,640 --> 10:51:53,276
and then we'll use the
16490
10:51:53,276 --> 10:51:54,640
format
16491
10:51:54,640 --> 10:51:55,840
method
16492
10:51:55,840 --> 10:51:58,800
and then i'm going to pass in my
16493
10:51:58,800 --> 10:52:00,400
keyword arguments
16494
10:52:00,400 --> 10:52:03,040
so here we have to assign the value for
16495
10:52:03,040 --> 10:52:04,320
a b and c
16496
10:52:04,320 --> 10:52:07,276
so let's see the value of a is
16497
10:52:07,276 --> 10:52:08,880
loan
16498
10:52:08,880 --> 10:52:09,840
for b
16499
10:52:09,840 --> 10:52:10,840
let's say it
16500
10:52:10,840 --> 10:52:13,040
is upscale
16501
10:52:13,040 --> 10:52:14,560
and for c
16502
10:52:14,560 --> 10:52:16,960
let's say the value is grow
16503
10:52:16,960 --> 10:52:17,756
now
16504
10:52:17,756 --> 10:52:19,680
we need to
16505
10:52:19,680 --> 10:52:21,436
print s2
16506
10:52:21,436 --> 10:52:24,160
if i run it you see the message here we
16507
10:52:24,160 --> 10:52:27,360
have v learn
16508
10:52:27,360 --> 10:52:31,276
upscale and grow with simply loan
16509
10:52:31,276 --> 10:52:34,160
let's say we'll replace v with
16510
10:52:34,160 --> 10:52:35,680
let's
16511
10:52:35,680 --> 10:52:36,800
so the
16512
10:52:36,800 --> 10:52:39,116
message is more appropriate
16513
10:52:39,116 --> 10:52:41,276
so now you can see let's learn upscale
16514
10:52:41,276 --> 10:52:43,916
and grow with simply loan so this is how
16515
10:52:43,916 --> 10:52:45,680
you can use
16516
10:52:45,680 --> 10:52:48,000
keyword or name arguments inside
16517
10:52:48,000 --> 10:52:51,520
placeholders in the format method
16518
10:52:51,520 --> 10:52:53,756
cool
16519
10:52:53,756 --> 10:52:55,360
now let's look at the
16520
10:52:55,360 --> 10:52:58,000
third method we have
16521
10:52:58,000 --> 10:52:59,360
formatting with
16522
10:52:59,360 --> 10:53:02,400
string literals called f strings so
16523
10:53:02,400 --> 10:53:04,960
strings in python are usually enclosed
16524
10:53:04,960 --> 10:53:07,596
within double quotes or single quotes to
16525
10:53:07,596 --> 10:53:10,880
create f strings you only need to add an
16526
10:53:10,880 --> 10:53:14,320
f before the opening codes of our string
16527
10:53:14,320 --> 10:53:15,916
so this type of formatting is also
16528
10:53:15,916 --> 10:53:19,276
called as a formatted string literal
16529
10:53:19,276 --> 10:53:20,640
now when you
16530
10:53:20,640 --> 10:53:22,960
use f strings you need to display
16531
10:53:22,960 --> 10:53:25,436
variables so for that you need to
16532
10:53:25,436 --> 10:53:27,360
specify the names of the variables
16533
10:53:27,360 --> 10:53:30,080
inside a set of curly braces and at
16534
10:53:30,080 --> 10:53:32,240
runtime all variable names will be
16535
10:53:32,240 --> 10:53:34,880
replaced with their respective values
16536
10:53:34,880 --> 10:53:37,200
so let's go to our jupyter notebook and
16537
10:53:37,200 --> 10:53:40,400
we'll see a few examples
16538
10:53:40,400 --> 10:53:41,436
all right
16539
10:53:41,436 --> 10:53:45,200
now let me give a comment as
16540
10:53:45,200 --> 10:53:46,720
f
16541
10:53:46,720 --> 10:53:49,720
strings
16542
10:53:50,480 --> 10:53:52,160
okay
16543
10:53:52,160 --> 10:53:54,480
so
16544
10:53:54,720 --> 10:53:58,080
let's see our first example under
16545
10:53:58,080 --> 10:54:00,480
f strings
16546
10:54:00,480 --> 10:54:01,360
type
16547
10:54:01,360 --> 10:54:03,360
so i'll say
16548
10:54:03,360 --> 10:54:05,116
tutorial
16549
10:54:05,116 --> 10:54:07,520
equal to python
16550
10:54:07,520 --> 10:54:09,116
then i'll have another variable called
16551
10:54:09,116 --> 10:54:11,360
subject which has the
16552
10:54:11,360 --> 10:54:13,436
value
16553
10:54:13,436 --> 10:54:15,680
programming
16554
10:54:15,680 --> 10:54:18,480
next we have place equal to
16555
10:54:18,480 --> 10:54:21,116
simply loan
16556
10:54:21,840 --> 10:54:24,480
and then i am going to
16557
10:54:24,480 --> 10:54:26,880
use a print statement
16558
10:54:26,880 --> 10:54:29,916
and then i'll use my
16559
10:54:29,916 --> 10:54:31,040
f
16560
10:54:31,040 --> 10:54:34,640
keyword for f strings
16561
10:54:34,640 --> 10:54:37,200
and i'll say my
16562
10:54:37,200 --> 10:54:39,436
message as let's
16563
10:54:39,436 --> 10:54:41,680
learn
16564
10:54:41,680 --> 10:54:43,276
then using
16565
10:54:43,276 --> 10:54:46,560
curly braces i'll pass in my
16566
10:54:46,560 --> 10:54:48,720
variable name that i have defined here
16567
10:54:48,720 --> 10:54:52,240
first is tutorial then we have subject
16568
10:54:52,240 --> 10:54:55,200
from then i'll have the place
16569
10:54:55,200 --> 10:54:56,960
let's give a full stop and we'll close
16570
10:54:56,960 --> 10:54:59,520
the double quotes and let's run it so
16571
10:54:59,520 --> 10:55:02,160
here you can see we have our message
16572
10:55:02,160 --> 10:55:04,960
let's learn python programming from
16573
10:55:04,960 --> 10:55:06,720
simply learn so here tutorial and
16574
10:55:06,720 --> 10:55:07,756
subject
16575
10:55:07,756 --> 10:55:09,680
were replaced by python and programming
16576
10:55:09,680 --> 10:55:11,756
because we have defined tutorial as
16577
10:55:11,756 --> 10:55:14,480
python and subject as programming and
16578
10:55:14,480 --> 10:55:16,560
please we have a simply learn and hence
16579
10:55:16,560 --> 10:55:18,560
my message is let's learn python
16580
10:55:18,560 --> 10:55:21,596
programming from simply loan so
16581
10:55:21,596 --> 10:55:24,720
this is how you can use f strings so
16582
10:55:24,720 --> 10:55:26,800
when using f strings to display
16583
10:55:26,800 --> 10:55:28,880
variables you only need to specify the
16584
10:55:28,880 --> 10:55:30,880
names of the variables inside a set of
16585
10:55:30,880 --> 10:55:32,560
curly braces
16586
10:55:32,560 --> 10:55:35,360
you can also evaluate expressions
16587
10:55:35,360 --> 10:55:40,080
with python f strings let's see a simple
16588
10:55:40,080 --> 10:55:42,560
multiplication i'll create a variable
16589
10:55:42,560 --> 10:55:45,360
called n1 which has a value of 60 let's
16590
10:55:45,360 --> 10:55:47,276
say n2
16591
10:55:47,276 --> 10:55:50,000
has a value of 12
16592
10:55:50,000 --> 10:55:52,960
and i'll use my print statement
16593
10:55:52,960 --> 10:55:54,840
i'll give my
16594
10:55:54,840 --> 10:55:58,320
f keyword for f strings
16595
10:55:58,320 --> 10:56:00,480
i'll say the
16596
10:56:00,480 --> 10:56:02,160
product of
16597
10:56:02,160 --> 10:56:05,200
then i'll create my placeholders n1
16598
10:56:05,200 --> 10:56:06,480
and
16599
10:56:06,480 --> 10:56:09,596
next placeholder is n2 is
16600
10:56:09,596 --> 10:56:12,000
and then using
16601
10:56:12,000 --> 10:56:14,880
curly braces i'll do a multiplication
16602
10:56:14,880 --> 10:56:16,080
operation
16603
10:56:16,080 --> 10:56:20,720
that is n1 multiplied by n2
16604
10:56:21,116 --> 10:56:22,800
i'll close my
16605
10:56:22,800 --> 10:56:24,880
double quotes let's run it
16606
10:56:24,880 --> 10:56:26,960
so you can see here the product of 60
16607
10:56:26,960 --> 10:56:29,000
and 12 is
16608
10:56:29,000 --> 10:56:33,116
720 let's say i'll make it 10 and
16609
10:56:33,116 --> 10:56:37,436
40 if i run it the product is 400
16610
10:56:37,436 --> 10:56:39,840
cool
16611
10:56:39,840 --> 10:56:41,596
now you can
16612
10:56:41,596 --> 10:56:43,116
also use
16613
10:56:43,116 --> 10:56:44,720
f strings
16614
10:56:44,720 --> 10:56:46,800
in conditional statements
16615
10:56:46,800 --> 10:56:48,720
so you can use conditionals in python f
16616
10:56:48,720 --> 10:56:50,320
strings let's see
16617
10:56:50,320 --> 10:56:52,080
an example i'll create a variable called
16618
10:56:52,080 --> 10:56:56,720
num which will have a value let's say 55
16619
10:56:56,720 --> 10:56:57,596
then
16620
10:56:57,596 --> 10:57:00,800
i'm going to use a print statement
16621
10:57:00,800 --> 10:57:04,320
and use my f letter
16622
10:57:04,320 --> 10:57:07,360
and then i'll pass in my message is
16623
10:57:07,360 --> 10:57:09,276
number even
16624
10:57:09,276 --> 10:57:13,040
give a question mark then using
16625
10:57:13,040 --> 10:57:16,320
curly braces i'll have my placeholder as
16626
10:57:16,320 --> 10:57:18,000
true if
16627
10:57:18,000 --> 10:57:19,116
num
16628
10:57:19,116 --> 10:57:23,040
modulo 2 equal to equal to 0
16629
10:57:23,040 --> 10:57:24,640
else
16630
10:57:24,640 --> 10:57:26,480
display false
16631
10:57:26,480 --> 10:57:28,480
i close my double quotes
16632
10:57:28,480 --> 10:57:30,160
now let's print it
16633
10:57:30,160 --> 10:57:31,916
you see here is
16634
10:57:31,916 --> 10:57:34,560
number even the output is false because
16635
10:57:34,560 --> 10:57:37,680
55 is not even let's say i'll make it 50
16636
10:57:37,680 --> 10:57:40,160
and if i run it as number even the value
16637
10:57:40,160 --> 10:57:42,560
is true because 50 is divisible by 2 and
16638
10:57:42,560 --> 10:57:45,596
hence it is even all right
16639
10:57:45,596 --> 10:57:47,276
now coming to the
16640
10:57:47,276 --> 10:57:48,640
final type of
16641
10:57:48,640 --> 10:57:50,880
stream formatting in python we have
16642
10:57:50,880 --> 10:57:54,560
formatting with string template class
16643
10:57:54,560 --> 10:57:56,800
now template strings are provided as a
16644
10:57:56,800 --> 10:57:58,880
class within the string module of the
16645
10:57:58,880 --> 10:58:01,200
python library it is imported as a
16646
10:58:01,200 --> 10:58:02,640
module and the
16647
10:58:02,640 --> 10:58:05,200
dot substitute method is used to replace
16648
10:58:05,200 --> 10:58:06,640
the placeholders
16649
10:58:06,640 --> 10:58:09,436
now the format uses placeholder names
16650
10:58:09,436 --> 10:58:12,400
formed by the dollar symbol with valid
16651
10:58:12,400 --> 10:58:14,160
python identifiers
16652
10:58:14,160 --> 10:58:15,680
let's go to our jupyter notebook and
16653
10:58:15,680 --> 10:58:18,560
we'll see two examples on this all right
16654
10:58:18,560 --> 10:58:20,560
so this is the final section of our demo
16655
10:58:20,560 --> 10:58:23,916
let me give my comment as
16656
10:58:23,916 --> 10:58:25,200
template
16657
10:58:25,200 --> 10:58:26,160
string
16658
10:58:26,160 --> 10:58:27,200
okay
16659
10:58:27,200 --> 10:58:30,720
so first and foremost we need to import
16660
10:58:30,720 --> 10:58:32,480
the template
16661
10:58:32,480 --> 10:58:36,640
module so i'll say from string import
16662
10:58:36,640 --> 10:58:39,520
template now let me first run it i'll
16663
10:58:39,520 --> 10:58:41,520
create two variables the first variable
16664
10:58:41,520 --> 10:58:43,756
is a1 which has a value of
16665
10:58:43,756 --> 10:58:44,880
python
16666
10:58:44,880 --> 10:58:47,520
the next variable is a2 let's see the
16667
10:58:47,520 --> 10:58:49,200
value is
16668
10:58:49,200 --> 10:58:51,436
simply loan
16669
10:58:51,436 --> 10:58:54,000
then i'll create another variable called
16670
10:58:54,000 --> 10:58:55,436
n
16671
10:58:55,436 --> 10:58:58,400
which will have my template
16672
10:58:58,400 --> 10:58:59,756
function
16673
10:58:59,756 --> 10:59:01,916
and inside the function will pass in our
16674
10:59:01,916 --> 10:59:02,880
message
16675
10:59:02,880 --> 10:59:04,240
let's say
16676
10:59:04,240 --> 10:59:05,436
hello
16677
10:59:05,436 --> 10:59:06,560
welcome
16678
10:59:06,560 --> 10:59:07,596
to
16679
10:59:07,596 --> 10:59:09,436
now i'll use my
16680
10:59:09,436 --> 10:59:12,880
dollar sign and say let's say
16681
10:59:12,880 --> 10:59:15,276
n1
16682
10:59:15,276 --> 10:59:17,040
programming
16683
10:59:17,040 --> 10:59:19,436
so this is my template that i'm creating
16684
10:59:19,436 --> 10:59:20,240
by
16685
10:59:20,240 --> 10:59:22,480
again i'll say dollar
16686
10:59:22,480 --> 10:59:23,436
and two
16687
10:59:23,436 --> 10:59:25,436
give a full stop
16688
10:59:25,436 --> 10:59:27,840
now i'll say print
16689
10:59:27,840 --> 10:59:28,560
and
16690
10:59:28,560 --> 10:59:31,040
dot we are going to use the
16691
10:59:31,040 --> 10:59:34,080
substitute method so i'll say
16692
10:59:34,080 --> 10:59:36,800
substitute
16693
10:59:37,116 --> 10:59:38,320
n
16694
10:59:38,320 --> 10:59:41,596
1 equal to a1 so here i'm going to
16695
10:59:41,596 --> 10:59:44,480
replace n1 with the value of a1
16696
10:59:44,480 --> 10:59:46,800
and then i'll say
16697
10:59:46,800 --> 10:59:50,400
replace n2 with a2 all right
16698
10:59:50,400 --> 10:59:52,840
let's run it and we'll see the
16699
10:59:52,840 --> 10:59:56,800
result there you go so here my
16700
10:59:56,800 --> 10:59:57,840
output
16701
10:59:57,840 --> 11:00:00,480
becomes hello welcome to
16702
11:00:00,480 --> 11:00:03,436
now n1 we have replaced with a1 so hello
16703
11:00:03,436 --> 11:00:04,560
welcome to
16704
11:00:04,560 --> 11:00:07,520
python programming by n2
16705
11:00:07,520 --> 11:00:10,400
now n2 we have referred to a2 so
16706
11:00:10,400 --> 11:00:12,240
it becomes simply loan so hello welcome
16707
11:00:12,240 --> 11:00:14,880
to python programming by simply loan
16708
11:00:14,880 --> 11:00:16,000
now let's
16709
11:00:16,000 --> 11:00:18,880
see our last demo
16710
11:00:18,880 --> 11:00:20,240
again this is on
16711
11:00:20,240 --> 11:00:21,360
template
16712
11:00:21,360 --> 11:00:22,640
strings
16713
11:00:22,640 --> 11:00:24,720
so i'll create a variable called stu
16714
11:00:24,720 --> 11:00:27,436
name which essentially stands for
16715
11:00:27,436 --> 11:00:29,276
student name let's say the name of the
16716
11:00:29,276 --> 11:00:30,800
student is
16717
11:00:30,800 --> 11:00:33,360
johnson
16718
11:00:33,360 --> 11:00:36,160
then i'll create a variable called s and
16719
11:00:36,160 --> 11:00:37,756
i'll use the
16720
11:00:37,756 --> 11:00:39,116
template
16721
11:00:39,116 --> 11:00:43,116
function or the method i'll say hey
16722
11:00:43,116 --> 11:00:45,916
using the dollar sign i'll say
16723
11:00:45,916 --> 11:00:47,436
name
16724
11:00:47,436 --> 11:00:48,840
how are
16725
11:00:48,840 --> 11:00:52,960
you how are you followed by a question
16726
11:00:52,960 --> 11:00:54,080
mark
16727
11:00:54,080 --> 11:00:56,880
i'll say s dot
16728
11:00:56,880 --> 11:00:59,520
substitute
16729
11:00:59,520 --> 11:01:01,360
i want to substitute
16730
11:01:01,360 --> 11:01:05,360
this dollar name with sdu underscore
16731
11:01:05,360 --> 11:01:06,160
name
16732
11:01:06,160 --> 11:01:08,160
which has johnson
16733
11:01:08,160 --> 11:01:09,680
now if i print it
16734
11:01:09,680 --> 11:01:12,000
you can see the message here hey johnson
16735
11:01:12,000 --> 11:01:13,436
how are you
16736
11:01:13,436 --> 11:01:14,800
great
16737
11:01:14,800 --> 11:01:16,000
so
16738
11:01:16,000 --> 11:01:18,320
that brings us to the end of our demo
16739
11:01:18,320 --> 11:01:21,520
session on string formatting in python
16740
11:01:21,520 --> 11:01:23,596
what is scope in python
16741
11:01:23,596 --> 11:01:25,276
so scope in python is the area of the
16742
11:01:25,276 --> 11:01:27,436
region of a program where you can access
16743
11:01:27,436 --> 11:01:30,400
variables functions and objects declared
16744
11:01:30,400 --> 11:01:32,160
the scope defines the accessibility of
16745
11:01:32,160 --> 11:01:34,720
the python object so the objects cannot
16746
11:01:34,720 --> 11:01:37,116
be accessed from anywhere in the program
16747
11:01:37,116 --> 11:01:39,360
variables are not visible to the entire
16748
11:01:39,360 --> 11:01:41,360
code so their visibility can be
16749
11:01:41,360 --> 11:01:42,880
restricted
16750
11:01:42,880 --> 11:01:45,840
now scope verifies which variable to be
16751
11:01:45,840 --> 11:01:47,040
accessed
16752
11:01:47,040 --> 11:01:48,880
a variable declared inside a function
16753
11:01:48,880 --> 11:01:50,880
belongs to the local scope of that
16754
11:01:50,880 --> 11:01:52,640
function and can only be used inside
16755
11:01:52,640 --> 11:01:54,080
that function
16756
11:01:54,080 --> 11:01:56,160
now whenever you use a name such as a
16757
11:01:56,160 --> 11:01:58,320
variable name or a function name python
16758
11:01:58,320 --> 11:01:59,840
will search through different scope
16759
11:01:59,840 --> 11:02:01,840
levels or namespaces to determine
16760
11:02:01,840 --> 11:02:04,240
whether the name exists or not
16761
11:02:04,240 --> 11:02:07,116
so python resolves names using the legb
16762
11:02:07,116 --> 11:02:10,560
rule the legb stands for local
16763
11:02:10,560 --> 11:02:14,800
enclosed global and built-in scope
16764
11:02:14,800 --> 11:02:17,116
now talking about local scope
16765
11:02:17,116 --> 11:02:18,560
the variables which are defined in the
16766
11:02:18,560 --> 11:02:20,240
function are called local scope of the
16767
11:02:20,240 --> 11:02:22,160
variable so these variables are defined
16768
11:02:22,160 --> 11:02:24,560
in the function's body
16769
11:02:24,560 --> 11:02:25,916
now talking about
16770
11:02:25,916 --> 11:02:27,520
enclosing scope
16771
11:02:27,520 --> 11:02:29,840
now this scope only exists for nested
16772
11:02:29,840 --> 11:02:32,080
functions if the local scope is an inner
16773
11:02:32,080 --> 11:02:33,916
or nested function then the enclosing
16774
11:02:33,916 --> 11:02:35,916
scope is the scope of the outer or
16775
11:02:35,916 --> 11:02:38,000
enclosing function
16776
11:02:38,000 --> 11:02:39,276
coming to
16777
11:02:39,276 --> 11:02:41,116
global scope
16778
11:02:41,116 --> 11:02:43,116
the variable which can be accessed from
16779
11:02:43,116 --> 11:02:45,276
anywhere in the program is known as the
16780
11:02:45,276 --> 11:02:46,640
global scope
16781
11:02:46,640 --> 11:02:48,400
such variables can be read inside or
16782
11:02:48,400 --> 11:02:49,916
outside the function
16783
11:02:49,916 --> 11:02:51,276
now the global scope is often
16784
11:02:51,276 --> 11:02:52,880
implemented when we want to use the same
16785
11:02:52,880 --> 11:02:56,320
variable name in the rest of the program
16786
11:02:56,320 --> 11:02:58,080
coming to the
16787
11:02:58,080 --> 11:03:00,560
final rule which is b that is built in
16788
11:03:00,560 --> 11:03:03,520
scope so if a variable is not defined in
16789
11:03:03,520 --> 11:03:05,756
local enclosed or global scope then
16790
11:03:05,756 --> 11:03:07,276
python looks for it in the built-in
16791
11:03:07,276 --> 11:03:09,916
scope now this scope contains names such
16792
11:03:09,916 --> 11:03:12,480
as keywords functions exceptions and
16793
11:03:12,480 --> 11:03:14,000
other attributes that are built into
16794
11:03:14,000 --> 11:03:15,200
python
16795
11:03:15,200 --> 11:03:17,116
now if you define the same variable name
16796
11:03:17,116 --> 11:03:19,360
inside and outside of a function python
16797
11:03:19,360 --> 11:03:21,916
will treat them as separate variables so
16798
11:03:21,916 --> 11:03:24,000
two separate variables one available in
16799
11:03:24,000 --> 11:03:25,360
the global scope that is outside the
16800
11:03:25,360 --> 11:03:27,116
function and the other available in the
16801
11:03:27,116 --> 11:03:29,680
local slope that is inside the function
16802
11:03:29,680 --> 11:03:31,276
now let's head over to our jupyter
16803
11:03:31,276 --> 11:03:33,520
notebook and we'll try
16804
11:03:33,520 --> 11:03:34,960
scopes in python with hands-on
16805
11:03:34,960 --> 11:03:37,040
demonstrations
16806
11:03:37,040 --> 11:03:38,960
all right so i'm on my jupiter notebook
16807
11:03:38,960 --> 11:03:40,480
let's start with our
16808
11:03:40,480 --> 11:03:44,400
examples first we'll look at
16809
11:03:44,400 --> 11:03:47,360
local scope in python
16810
11:03:47,360 --> 11:03:49,276
for that i'm going to create a function
16811
11:03:49,276 --> 11:03:50,480
called
16812
11:03:50,480 --> 11:03:52,480
fun
16813
11:03:52,480 --> 11:03:53,596
and
16814
11:03:53,596 --> 11:03:54,880
inside the function i'll create a
16815
11:03:54,880 --> 11:03:57,040
variable called
16816
11:03:57,040 --> 11:03:58,480
s which
16817
11:03:58,480 --> 11:04:00,080
holds let's say
16818
11:04:00,080 --> 11:04:02,000
an object called
16819
11:04:02,000 --> 11:04:03,436
simply loan
16820
11:04:03,436 --> 11:04:04,960
and then
16821
11:04:04,960 --> 11:04:06,800
am going to
16822
11:04:06,800 --> 11:04:08,160
print
16823
11:04:08,160 --> 11:04:10,160
the variable that is s
16824
11:04:10,160 --> 11:04:12,320
now in this case i have defined my
16825
11:04:12,320 --> 11:04:14,080
variable
16826
11:04:14,080 --> 11:04:16,720
inside the function so this becomes
16827
11:04:16,720 --> 11:04:19,040
accessible only
16828
11:04:19,040 --> 11:04:20,560
inside the function
16829
11:04:20,560 --> 11:04:22,400
now if i call the function using the
16830
11:04:22,400 --> 11:04:25,520
function name that is fun
16831
11:04:25,520 --> 11:04:29,116
and if we run it you can see we have the
16832
11:04:29,116 --> 11:04:31,680
message printed that is simply loan
16833
11:04:31,680 --> 11:04:33,756
now
16834
11:04:33,756 --> 11:04:36,240
we'll see another example
16835
11:04:36,240 --> 11:04:38,880
so suppose
16836
11:04:39,200 --> 11:04:41,116
we'll try to access it outside the
16837
11:04:41,116 --> 11:04:43,276
function it will throw an error so i'll
16838
11:04:43,276 --> 11:04:46,160
just give a comment try
16839
11:04:46,160 --> 11:04:49,400
to access
16840
11:04:49,680 --> 11:04:51,276
it
16841
11:04:51,276 --> 11:04:54,240
outside the function
16842
11:04:54,480 --> 11:04:55,596
all right
16843
11:04:55,596 --> 11:04:57,916
so again i'll create
16844
11:04:57,916 --> 11:05:00,080
a function called let's say
16845
11:05:00,080 --> 11:05:02,480
fun 1
16846
11:05:02,480 --> 11:05:05,436
i'll give a colon and here i'll say
16847
11:05:05,436 --> 11:05:06,720
let's say
16848
11:05:06,720 --> 11:05:09,520
s1 equal to
16849
11:05:09,520 --> 11:05:12,240
simply loan
16850
11:05:12,320 --> 11:05:14,320
and now
16851
11:05:14,320 --> 11:05:18,400
i'll give a comment here print s1
16852
11:05:18,400 --> 11:05:22,240
then i'll call my function fu n1
16853
11:05:22,240 --> 11:05:23,916
and
16854
11:05:23,916 --> 11:05:25,916
i'll try to
16855
11:05:25,916 --> 11:05:27,360
print
16856
11:05:27,360 --> 11:05:29,276
the variable s1
16857
11:05:29,276 --> 11:05:30,720
outside the function
16858
11:05:30,720 --> 11:05:32,480
so here s1
16859
11:05:32,480 --> 11:05:34,480
has a local scope since it is defined
16860
11:05:34,480 --> 11:05:36,880
inside the function but i am trying to
16861
11:05:36,880 --> 11:05:38,800
access the variable outside the function
16862
11:05:38,800 --> 11:05:40,000
let's run it
16863
11:05:40,000 --> 11:05:42,720
you can see the error here name s1 not
16864
11:05:42,720 --> 11:05:45,436
defined now
16865
11:05:45,436 --> 11:05:47,276
if i give a
16866
11:05:47,276 --> 11:05:48,400
comment
16867
11:05:48,400 --> 11:05:49,840
and run this
16868
11:05:49,840 --> 11:05:51,756
you see there is no message and let me
16869
11:05:51,756 --> 11:05:53,596
just remove this comment and print it
16870
11:05:53,596 --> 11:05:56,080
you can see we have the
16871
11:05:56,080 --> 11:05:58,240
s1 variable message printed that is
16872
11:05:58,240 --> 11:06:01,436
simply known so this is how the
16873
11:06:01,436 --> 11:06:05,360
local scope of a function works
16874
11:06:05,756 --> 11:06:08,240
now let's see an example of
16875
11:06:08,240 --> 11:06:11,596
enclosed scope which is
16876
11:06:11,596 --> 11:06:12,400
function
16877
11:06:12,400 --> 11:06:15,596
inside another function
16878
11:06:19,360 --> 11:06:22,720
so i'll create a function
16879
11:06:22,720 --> 11:06:26,596
let's say func
16880
11:06:26,800 --> 11:06:28,480
and
16881
11:06:28,480 --> 11:06:31,276
i'll use the variable this time s2 equal
16882
11:06:31,276 --> 11:06:32,720
to
16883
11:06:32,720 --> 11:06:35,360
simply loan
16884
11:06:36,240 --> 11:06:37,520
and then
16885
11:06:37,520 --> 11:06:40,000
inside the func function i will define
16886
11:06:40,000 --> 11:06:42,640
another function let's say
16887
11:06:42,640 --> 11:06:45,276
i n f u nc which basically stands for
16888
11:06:45,276 --> 11:06:47,116
inside function
16889
11:06:47,116 --> 11:06:51,436
and then here i'll say print s
16890
11:06:51,840 --> 11:06:53,840
s2
16891
11:06:53,840 --> 11:06:55,276
and
16892
11:06:55,276 --> 11:06:56,560
we'll call the
16893
11:06:56,560 --> 11:06:58,880
inner function that is in
16894
11:06:58,880 --> 11:07:00,400
fu and c
16895
11:07:00,400 --> 11:07:04,000
and then i'll call the outer function
16896
11:07:04,000 --> 11:07:05,680
let's run it
16897
11:07:05,680 --> 11:07:06,840
and see the
16898
11:07:06,840 --> 11:07:08,640
result
16899
11:07:08,640 --> 11:07:10,480
you can see here we have the
16900
11:07:10,480 --> 11:07:12,000
message printed
16901
11:07:12,000 --> 11:07:14,400
simply learn i'll just
16902
11:07:14,400 --> 11:07:17,116
edit this message let's say
16903
11:07:17,116 --> 11:07:19,040
welcome to simply loan
16904
11:07:19,040 --> 11:07:20,560
now if i run this
16905
11:07:20,560 --> 11:07:22,400
you can see we have updated the message
16906
11:07:22,400 --> 11:07:24,080
here
16907
11:07:24,080 --> 11:07:24,960
now
16908
11:07:24,960 --> 11:07:26,800
for the enclosed scope you need to
16909
11:07:26,800 --> 11:07:28,560
define an outer function including the
16910
11:07:28,560 --> 11:07:30,160
inner function like we
16911
11:07:30,160 --> 11:07:31,200
did here
16912
11:07:31,200 --> 11:07:33,596
and you can also comment out the local
16913
11:07:33,596 --> 11:07:35,520
variable of the inner function and refer
16914
11:07:35,520 --> 11:07:37,200
to the variable using the
16915
11:07:37,200 --> 11:07:39,680
non-local keyword let me show you an
16916
11:07:39,680 --> 11:07:41,680
example around this
16917
11:07:41,680 --> 11:07:44,480
suppose i have a
16918
11:07:44,480 --> 11:07:47,276
global variable called msg
16919
11:07:47,276 --> 11:07:50,960
i'll say this is a
16920
11:07:50,960 --> 11:07:52,560
global
16921
11:07:52,560 --> 11:07:54,160
scope
16922
11:07:54,160 --> 11:07:55,916
message
16923
11:07:55,916 --> 11:07:57,520
after that
16924
11:07:57,520 --> 11:08:00,240
i'll create a function so i'll define a
16925
11:08:00,240 --> 11:08:01,840
function called
16926
11:08:01,840 --> 11:08:03,520
outer
16927
11:08:03,520 --> 11:08:04,560
and
16928
11:08:04,560 --> 11:08:06,720
i'll use the same variable name that is
16929
11:08:06,720 --> 11:08:08,480
msg
16930
11:08:08,480 --> 11:08:10,720
and my message would be this is
16931
11:08:10,720 --> 11:08:12,480
an
16932
11:08:12,480 --> 11:08:15,720
outer variable
16933
11:08:21,040 --> 11:08:23,520
after that
16934
11:08:23,520 --> 11:08:26,400
i am going to define another function
16935
11:08:26,400 --> 11:08:27,840
called
16936
11:08:27,840 --> 11:08:30,240
inner
16937
11:08:30,240 --> 11:08:32,160
and
16938
11:08:32,160 --> 11:08:34,880
this will have
16939
11:08:35,756 --> 11:08:38,240
the same variable that is msg and my
16940
11:08:38,240 --> 11:08:40,480
message would be
16941
11:08:40,480 --> 11:08:42,800
inner
16942
11:08:42,800 --> 11:08:44,240
message
16943
11:08:44,240 --> 11:08:47,240
variable
16944
11:08:47,360 --> 11:08:50,080
now as i mentioned before i need to
16945
11:08:50,080 --> 11:08:51,360
comment out the
16946
11:08:51,360 --> 11:08:53,200
local variable of the inner function so
16947
11:08:53,200 --> 11:08:55,200
i'll just
16948
11:08:55,200 --> 11:08:57,200
comment this out
16949
11:08:57,200 --> 11:09:00,240
and then
16950
11:09:00,240 --> 11:09:02,480
i'm going to say
16951
11:09:02,480 --> 11:09:06,160
non-local which is the keyboard
16952
11:09:06,160 --> 11:09:08,320
so i'm referring to the variable using
16953
11:09:08,320 --> 11:09:10,000
the non-local keyword
16954
11:09:10,000 --> 11:09:11,680
i'll say non-local
16955
11:09:11,680 --> 11:09:14,640
variable name that is msg
16956
11:09:14,640 --> 11:09:16,960
and now
16957
11:09:16,960 --> 11:09:18,000
i'll
16958
11:09:18,000 --> 11:09:21,116
print msg
16959
11:09:23,276 --> 11:09:25,200
will come out of the function and we'll
16960
11:09:25,200 --> 11:09:26,960
call it
16961
11:09:26,960 --> 11:09:29,680
inner
16962
11:09:29,680 --> 11:09:32,560
after that we'll call the
16963
11:09:32,560 --> 11:09:33,916
outer function
16964
11:09:33,916 --> 11:09:36,080
and then i'm going to print i'll just
16965
11:09:36,080 --> 11:09:37,916
scroll this down
16966
11:09:37,916 --> 11:09:40,400
and we'll say msg
16967
11:09:40,400 --> 11:09:42,800
so here what we are doing is so this is
16968
11:09:42,800 --> 11:09:45,840
an example of an enclosed scope so where
16969
11:09:45,840 --> 11:09:47,680
we have
16970
11:09:47,680 --> 11:09:50,640
defined the outer function enclosing the
16971
11:09:50,640 --> 11:09:53,200
inner function and we have commented out
16972
11:09:53,200 --> 11:09:55,200
the local variable of the inner function
16973
11:09:55,200 --> 11:09:56,560
and we are referring to the variable
16974
11:09:56,560 --> 11:09:59,596
using the non-local keyword now let's
16975
11:09:59,596 --> 11:10:03,596
run it you see here i have my
16976
11:10:03,596 --> 11:10:04,560
two
16977
11:10:04,560 --> 11:10:06,400
print statements working here the first
16978
11:10:06,400 --> 11:10:08,640
one is this is an outer variable you can
16979
11:10:08,640 --> 11:10:11,840
see here and then i have print msg so
16980
11:10:11,840 --> 11:10:16,640
this is a global scope message cool
16981
11:10:16,880 --> 11:10:18,160
now
16982
11:10:18,160 --> 11:10:20,560
we'll see an example of how the global
16983
11:10:20,560 --> 11:10:22,880
scope works though we have already seen
16984
11:10:22,880 --> 11:10:25,276
an example above so this was an example
16985
11:10:25,276 --> 11:10:26,880
of a global
16986
11:10:26,880 --> 11:10:28,560
scope
16987
11:10:28,560 --> 11:10:30,720
now we'll see a few more examples around
16988
11:10:30,720 --> 11:10:32,800
global scope again
16989
11:10:32,800 --> 11:10:35,916
suppose i have a variable called s3
16990
11:10:35,916 --> 11:10:38,720
equal to i'll say
16991
11:10:38,720 --> 11:10:40,560
simply loan
16992
11:10:40,560 --> 11:10:43,436
and then i'll declare a function
16993
11:10:43,436 --> 11:10:44,720
f u n c
16994
11:10:44,720 --> 11:10:46,960
one
16995
11:10:46,960 --> 11:10:49,680
i give a colon and then say
16996
11:10:49,680 --> 11:10:50,960
print
16997
11:10:50,960 --> 11:10:53,520
s s3
16998
11:10:54,000 --> 11:10:57,276
now i'll call the function fu and c1
16999
11:10:57,276 --> 11:10:59,756
and we'll print it
17000
11:10:59,756 --> 11:11:01,680
s3
17001
11:11:01,680 --> 11:11:04,640
if i run it you can see here i have
17002
11:11:04,640 --> 11:11:07,116
simply learned printed twice so i have
17003
11:11:07,116 --> 11:11:09,520
used the same variable name s3
17004
11:11:09,520 --> 11:11:12,000
inside the function so
17005
11:11:12,000 --> 11:11:14,480
this simply learn is for the
17006
11:11:14,480 --> 11:11:17,520
local scope s3 that is this one and
17007
11:11:17,520 --> 11:11:18,960
outside the
17008
11:11:18,960 --> 11:11:21,596
function we have printed s3 again so
17009
11:11:21,596 --> 11:11:24,480
simply learn twice
17010
11:11:24,480 --> 11:11:26,080
all right
17011
11:11:26,080 --> 11:11:28,960
now let me show you another example
17012
11:11:28,960 --> 11:11:31,040
about using the
17013
11:11:31,040 --> 11:11:33,040
global scope
17014
11:11:33,040 --> 11:11:36,720
create a variable msg1
17015
11:11:36,720 --> 11:11:39,360
let's say my message is
17016
11:11:39,360 --> 11:11:41,436
welcome to
17017
11:11:41,436 --> 11:11:43,756
simply loan
17018
11:11:43,756 --> 11:11:45,360
python
17019
11:11:45,360 --> 11:11:48,360
programming
17020
11:11:48,880 --> 11:11:52,240
i'll define the function f unc 2 i'll
17021
11:11:52,240 --> 11:11:53,756
give a colon
17022
11:11:53,756 --> 11:11:55,916
and i'll use the same variable that i
17023
11:11:55,916 --> 11:11:57,040
used
17024
11:11:57,040 --> 11:11:59,756
or declared above that is msg1
17025
11:11:59,756 --> 11:12:02,640
i'll say this is the
17026
11:12:02,640 --> 11:12:06,480
inner msg1 variable
17027
11:12:07,756 --> 11:12:11,916
then i am going to print msg1
17028
11:12:11,916 --> 11:12:15,840
after that i will call my function func2
17029
11:12:15,840 --> 11:12:18,560
and i'll print
17030
11:12:18,560 --> 11:12:20,960
msg1
17031
11:12:20,960 --> 11:12:22,400
if i run this
17032
11:12:22,400 --> 11:12:24,560
you can see i have the two messages
17033
11:12:24,560 --> 11:12:27,360
printed first is this is the inner msg1
17034
11:12:27,360 --> 11:12:30,160
variable and the second print statement
17035
11:12:30,160 --> 11:12:32,240
will print the
17036
11:12:32,240 --> 11:12:34,560
global variable
17037
11:12:34,560 --> 11:12:36,160
message that is welcome to simply known
17038
11:12:36,160 --> 11:12:37,596
python programming
17039
11:12:37,596 --> 11:12:39,840
cool
17040
11:12:40,240 --> 11:12:43,116
now you can also
17041
11:12:43,116 --> 11:12:45,916
use the global keyword inside the
17042
11:12:45,916 --> 11:12:49,040
function to convert a local scope into
17043
11:12:49,040 --> 11:12:50,880
global scope
17044
11:12:50,880 --> 11:12:51,680
now
17045
11:12:51,680 --> 11:12:55,360
i'll just give a comment using
17046
11:12:55,916 --> 11:12:59,400
global keyword
17047
11:13:04,000 --> 11:13:06,800
so i'll define my function func let's
17048
11:13:06,800 --> 11:13:08,720
say 3
17049
11:13:08,720 --> 11:13:10,480
i'll give a colon
17050
11:13:10,480 --> 11:13:13,200
and using the global keyword
17051
11:13:13,200 --> 11:13:14,320
i'll say
17052
11:13:14,320 --> 11:13:16,320
s
17053
11:13:16,320 --> 11:13:19,756
and i'll say s equal to
17054
11:13:20,480 --> 11:13:23,116
simply loan
17055
11:13:24,560 --> 11:13:26,640
now let me
17056
11:13:26,640 --> 11:13:30,080
call fu and c3
17057
11:13:30,320 --> 11:13:33,756
and will print s outside the function
17058
11:13:33,756 --> 11:13:35,756
now one thing to remember here
17059
11:13:35,756 --> 11:13:37,596
my variable has been declared inside the
17060
11:13:37,596 --> 11:13:38,560
function
17061
11:13:38,560 --> 11:13:41,596
but since before declaring the variable
17062
11:13:41,596 --> 11:13:45,040
i have assigned the global keyword to s
17063
11:13:45,040 --> 11:13:48,000
so this now becomes a
17064
11:13:48,000 --> 11:13:50,400
global scope variable so you can access
17065
11:13:50,400 --> 11:13:51,916
it outside the function as well now if i
17066
11:13:51,916 --> 11:13:53,840
run it you can see it here i have my
17067
11:13:53,840 --> 11:13:55,436
message printed
17068
11:13:55,436 --> 11:13:57,520
simply learn if i say hello you can see
17069
11:13:57,520 --> 11:14:00,320
it hello printed
17070
11:14:00,320 --> 11:14:02,560
cool
17071
11:14:05,040 --> 11:14:06,400
now
17072
11:14:06,400 --> 11:14:08,080
we'll see another example of using the
17073
11:14:08,080 --> 11:14:09,436
global scope
17074
11:14:09,436 --> 11:14:12,640
let's say i have a variable called e1
17075
11:14:12,640 --> 11:14:15,040
which is
17076
11:14:15,040 --> 11:14:16,480
india
17077
11:14:16,480 --> 11:14:18,880
and then
17078
11:14:20,240 --> 11:14:23,680
i'll create a function called my country
17079
11:14:23,680 --> 11:14:25,360
and i'll use
17080
11:14:25,360 --> 11:14:27,116
the
17081
11:14:27,116 --> 11:14:31,596
global keyword and say a1
17082
11:14:33,116 --> 11:14:35,040
and after that
17083
11:14:35,040 --> 11:14:37,680
i'm going to
17084
11:14:37,680 --> 11:14:40,800
say a1 equal to
17085
11:14:40,800 --> 11:14:42,800
i'll change the country to let's say
17086
11:14:42,800 --> 11:14:45,200
france
17087
11:14:45,200 --> 11:14:47,520
now let me
17088
11:14:47,520 --> 11:14:50,400
call the function my country and will
17089
11:14:50,400 --> 11:14:52,400
print
17090
11:14:52,400 --> 11:14:53,596
a1
17091
11:14:53,596 --> 11:14:54,960
and you can
17092
11:14:54,960 --> 11:14:57,040
notice the difference in the result
17093
11:14:57,040 --> 11:14:59,596
so here initially i had defined india as
17094
11:14:59,596 --> 11:15:02,080
a global variable and after that i am
17095
11:15:02,080 --> 11:15:03,436
assigning the
17096
11:15:03,436 --> 11:15:06,160
same variable name with a global keyword
17097
11:15:06,160 --> 11:15:08,400
and hence it will take
17098
11:15:08,400 --> 11:15:11,756
the updated value for e1 and you can see
17099
11:15:11,756 --> 11:15:14,560
here the result is france
17100
11:15:14,560 --> 11:15:17,520
now finally let's look at
17101
11:15:17,520 --> 11:15:21,116
built-in scope in python
17102
11:15:21,116 --> 11:15:23,596
this is the final example in the demo
17103
11:15:23,596 --> 11:15:25,840
session about scopes and arguments in
17104
11:15:25,840 --> 11:15:28,080
python
17105
11:15:28,080 --> 11:15:30,160
cool
17106
11:15:30,160 --> 11:15:31,756
so
17107
11:15:31,756 --> 11:15:34,720
this example is a bit different from the
17108
11:15:34,720 --> 11:15:36,880
previous few examples that we saw on
17109
11:15:36,880 --> 11:15:38,240
local scope
17110
11:15:38,240 --> 11:15:40,720
enclosed scope and global scope
17111
11:15:40,720 --> 11:15:41,756
so here
17112
11:15:41,756 --> 11:15:43,596
i'm going to
17113
11:15:43,596 --> 11:15:45,360
import
17114
11:15:45,360 --> 11:15:48,640
pi from the math
17115
11:15:48,640 --> 11:15:51,520
module so i will say from math
17116
11:15:51,520 --> 11:15:53,276
import
17117
11:15:53,276 --> 11:15:55,520
pi
17118
11:15:56,640 --> 11:15:58,720
so this example includes
17119
11:15:58,720 --> 11:16:01,680
global scope enclosed scope as well as
17120
11:16:01,680 --> 11:16:03,916
local scope and of course we have the
17121
11:16:03,916 --> 11:16:05,840
built-in scope as well
17122
11:16:05,840 --> 11:16:07,436
now what i'll do is
17123
11:16:07,436 --> 11:16:09,520
first i'll
17124
11:16:09,520 --> 11:16:12,960
give a comment and
17125
11:16:12,960 --> 11:16:15,200
create a variable called
17126
11:16:15,200 --> 11:16:16,880
pi
17127
11:16:16,880 --> 11:16:18,320
equal to
17128
11:16:18,320 --> 11:16:20,880
i'll say
17129
11:16:21,756 --> 11:16:22,880
global
17130
11:16:22,880 --> 11:16:25,040
pi
17131
11:16:25,840 --> 11:16:28,840
variable
17132
11:16:29,116 --> 11:16:31,680
i'll say this is a global
17133
11:16:31,680 --> 11:16:33,200
pi variable
17134
11:16:33,200 --> 11:16:35,596
and then
17135
11:16:36,480 --> 11:16:39,276
what i'll do is i'll say
17136
11:16:39,276 --> 11:16:40,320
df
17137
11:16:40,320 --> 11:16:41,916
let's do one thing first of all we'll
17138
11:16:41,916 --> 11:16:43,276
remove
17139
11:16:43,276 --> 11:16:44,880
the comment
17140
11:16:44,880 --> 11:16:48,160
and now i'll create a
17141
11:16:48,480 --> 11:16:50,800
outer function
17142
11:16:50,800 --> 11:16:54,560
into the colon and say
17143
11:16:55,276 --> 11:16:58,560
the variable pi
17144
11:16:59,200 --> 11:17:01,436
i'll say
17145
11:17:01,436 --> 11:17:03,520
outer
17146
11:17:03,520 --> 11:17:05,680
variable
17147
11:17:05,680 --> 11:17:07,916
pi
17148
11:17:09,520 --> 11:17:11,840
and then
17149
11:17:11,840 --> 11:17:14,480
i'll create
17150
11:17:15,360 --> 11:17:16,960
a nested function
17151
11:17:16,960 --> 11:17:19,040
with the name inner
17152
11:17:19,040 --> 11:17:20,720
and
17153
11:17:20,720 --> 11:17:22,400
i'll say pi
17154
11:17:22,400 --> 11:17:25,720
equal to
17155
11:17:26,080 --> 11:17:28,480
inner
17156
11:17:29,680 --> 11:17:31,520
pi
17157
11:17:31,520 --> 11:17:34,520
variable
17158
11:17:40,240 --> 11:17:41,916
now
17159
11:17:41,916 --> 11:17:43,360
i am going to say
17160
11:17:43,360 --> 11:17:44,960
print
17161
11:17:44,960 --> 11:17:46,400
pi
17162
11:17:46,400 --> 11:17:47,276
and
17163
11:17:47,276 --> 11:17:48,880
after that i will
17164
11:17:48,880 --> 11:17:50,640
call the
17165
11:17:50,640 --> 11:17:53,040
inner function
17166
11:17:53,040 --> 11:17:54,480
and
17167
11:17:54,480 --> 11:17:55,756
next
17168
11:17:55,756 --> 11:17:57,840
i'll also call the
17169
11:17:57,840 --> 11:18:00,560
outer function
17170
11:18:01,520 --> 11:18:03,200
now if i
17171
11:18:03,200 --> 11:18:04,720
run this
17172
11:18:04,720 --> 11:18:06,560
you can see the result
17173
11:18:06,560 --> 11:18:07,840
inner
17174
11:18:07,840 --> 11:18:10,960
pi variable being printed which is this
17175
11:18:10,960 --> 11:18:12,640
print statement
17176
11:18:12,640 --> 11:18:14,160
now
17177
11:18:14,160 --> 11:18:16,080
what i'll do is
17178
11:18:16,080 --> 11:18:18,800
i'm going to comment out the
17179
11:18:18,800 --> 11:18:21,116
global scope variable
17180
11:18:21,116 --> 11:18:23,596
and i'll also comment out
17181
11:18:23,596 --> 11:18:25,916
the first
17182
11:18:25,916 --> 11:18:28,400
outer function variable that i defined
17183
11:18:28,400 --> 11:18:30,320
i'll comment this out
17184
11:18:30,320 --> 11:18:33,520
and i'll also comment the inner
17185
11:18:33,520 --> 11:18:34,560
function
17186
11:18:34,560 --> 11:18:36,960
pi variable
17187
11:18:36,960 --> 11:18:37,756
now
17188
11:18:37,756 --> 11:18:39,360
if i run this
17189
11:18:39,360 --> 11:18:41,520
you see the result here
17190
11:18:41,520 --> 11:18:43,840
it prints the value of pi that was
17191
11:18:43,840 --> 11:18:46,400
imported directly from the math module
17192
11:18:46,400 --> 11:18:48,400
or the math library
17193
11:18:48,400 --> 11:18:51,200
so here pi is not defined in either
17194
11:18:51,200 --> 11:18:52,400
local
17195
11:18:52,400 --> 11:18:54,080
enclosed or
17196
11:18:54,080 --> 11:18:56,800
global scope so the built-in scope is
17197
11:18:56,800 --> 11:18:59,276
looked up that is the pi value imported
17198
11:18:59,276 --> 11:19:01,596
from the math module so this is an
17199
11:19:01,596 --> 11:19:04,400
example of built-in scope in python that
17200
11:19:04,400 --> 11:19:05,360
uses
17201
11:19:05,360 --> 11:19:06,880
inbuilt
17202
11:19:06,880 --> 11:19:09,276
functions modules and objects that are
17203
11:19:09,276 --> 11:19:11,200
available in python
17204
11:19:11,200 --> 11:19:12,720
all right
17205
11:19:12,720 --> 11:19:14,840
let's glance at the
17206
11:19:14,840 --> 11:19:17,360
topics object-oriented programming in
17207
11:19:17,360 --> 11:19:19,756
python some major principles of
17208
11:19:19,756 --> 11:19:22,000
object-oriented programming and then we
17209
11:19:22,000 --> 11:19:24,080
will touch upon topics like class in
17210
11:19:24,080 --> 11:19:26,160
python objects
17211
11:19:26,160 --> 11:19:27,276
methods
17212
11:19:27,276 --> 11:19:28,800
inheritance
17213
11:19:28,800 --> 11:19:30,720
the concepts of inheritance the
17214
11:19:30,720 --> 11:19:32,720
different type of inheritance then the
17215
11:19:32,720 --> 11:19:35,520
definition and examples of polymorphism
17216
11:19:35,520 --> 11:19:37,756
encapsulation and abstraction
17217
11:19:37,756 --> 11:19:40,320
so all these topics will be
17218
11:19:40,320 --> 11:19:42,480
supported with examples
17219
11:19:42,480 --> 11:19:44,960
and the relevant terminologies and
17220
11:19:44,960 --> 11:19:47,596
definitions
17221
11:19:48,960 --> 11:19:52,160
now what is object-oriented programming
17222
11:19:52,160 --> 11:19:54,400
python is an object-oriented language
17223
11:19:54,400 --> 11:19:56,560
like any other programming language like
17224
11:19:56,560 --> 11:19:58,000
java c
17225
11:19:58,000 --> 11:20:00,160
and the object oriented programming
17226
11:20:00,160 --> 11:20:02,640
language concept is not new
17227
11:20:02,640 --> 11:20:04,880
and each and every mature programming
17228
11:20:04,880 --> 11:20:06,720
language
17229
11:20:06,720 --> 11:20:09,520
has these concepts imbibed in it
17230
11:20:09,520 --> 11:20:11,276
now the concept of object oriented
17231
11:20:11,276 --> 11:20:14,640
programming in python focuses primarily
17232
11:20:14,640 --> 11:20:17,520
on creating project objects and making
17233
11:20:17,520 --> 11:20:19,916
reusable codes so whenever we start
17234
11:20:19,916 --> 11:20:22,640
programming at a large scale or for a
17235
11:20:22,640 --> 11:20:24,800
big project it is very important to
17236
11:20:24,800 --> 11:20:26,960
identify the objects within that
17237
11:20:26,960 --> 11:20:28,880
particular project which you want to
17238
11:20:28,880 --> 11:20:30,400
define
17239
11:20:30,400 --> 11:20:32,640
now for example in this
17240
11:20:32,640 --> 11:20:33,596
in this
17241
11:20:33,596 --> 11:20:35,916
presentation what you are seeing is that
17242
11:20:35,916 --> 11:20:38,320
we are covered with full of objects like
17243
11:20:38,320 --> 11:20:39,200
car
17244
11:20:39,200 --> 11:20:43,200
dog tiger tree or a balloon so these are
17245
11:20:43,200 --> 11:20:46,000
typical examples of objects which you
17246
11:20:46,000 --> 11:20:49,276
encounter in day-to-day life
17247
11:20:49,916 --> 11:20:51,520
now what are the major principles of
17248
11:20:51,520 --> 11:20:53,756
object-oriented programming so these are
17249
11:20:53,756 --> 11:20:56,160
the different kind of you know
17250
11:20:56,160 --> 11:20:57,596
terminologies
17251
11:20:57,596 --> 11:20:59,680
classes are there there are objects
17252
11:20:59,680 --> 11:21:01,360
there are methods
17253
11:21:01,360 --> 11:21:04,160
then on these classes and objects you
17254
11:21:04,160 --> 11:21:06,800
apply inheritance polymorphism
17255
11:21:06,800 --> 11:21:10,720
encapsulation and abstraction
17256
11:21:11,840 --> 11:21:13,596
now what is a class
17257
11:21:13,596 --> 11:21:15,596
class is nothing but a collection of
17258
11:21:15,596 --> 11:21:18,400
objects or we can say a user defined
17259
11:21:18,400 --> 11:21:20,640
data type right so basically it's a
17260
11:21:20,640 --> 11:21:22,960
complex data type which has a collection
17261
11:21:22,960 --> 11:21:25,436
of objects and within the objects you
17262
11:21:25,436 --> 11:21:30,080
have primitive data types like in string
17263
11:21:30,080 --> 11:21:32,080
float etc
17264
11:21:32,080 --> 11:21:35,680
so a class also holds the methods
17265
11:21:35,680 --> 11:21:37,116
the functions
17266
11:21:37,116 --> 11:21:39,840
which basically perform operations on
17267
11:21:39,840 --> 11:21:42,080
these objects
17268
11:21:42,080 --> 11:21:44,960
right and also it also holds on to the
17269
11:21:44,960 --> 11:21:46,960
attributes of the objects so let's see
17270
11:21:46,960 --> 11:21:48,880
with an example right
17271
11:21:48,880 --> 11:21:51,596
so if you have an employee class
17272
11:21:51,596 --> 11:21:53,756
then it would contain attributes and
17273
11:21:53,756 --> 11:21:57,436
methods such as email id name age salary
17274
11:21:57,436 --> 11:21:59,756
these are the attributes and on the
17275
11:21:59,756 --> 11:22:02,080
methods like you know perform tasks give
17276
11:22:02,080 --> 11:22:03,520
presentation
17277
11:22:03,520 --> 11:22:05,116
increase salary
17278
11:22:05,116 --> 11:22:09,756
etc etc these are will be the methods
17279
11:22:12,480 --> 11:22:14,800
now let's talk about what are objects in
17280
11:22:14,800 --> 11:22:16,240
python
17281
11:22:16,240 --> 11:22:19,680
now object is known as instantiation of
17282
11:22:19,680 --> 11:22:22,880
a class right an object is a group of
17283
11:22:22,880 --> 11:22:26,160
interrelated variables and methods
17284
11:22:26,160 --> 11:22:28,560
so when a class is defined only the
17285
11:22:28,560 --> 11:22:31,040
description for the object is defined
17286
11:22:31,040 --> 11:22:33,436
therefore no memory or storage is
17287
11:22:33,436 --> 11:22:35,520
allocated at that point
17288
11:22:35,520 --> 11:22:37,916
so for example if you see in this
17289
11:22:37,916 --> 11:22:40,080
motorbike is a class
17290
11:22:40,080 --> 11:22:40,800
but
17291
11:22:40,800 --> 11:22:42,960
the different objects of the class are
17292
11:22:42,960 --> 11:22:44,960
different kind of bikes by different
17293
11:22:44,960 --> 11:22:48,240
brands like bmw suzuki royal enfield so
17294
11:22:48,240 --> 11:22:52,320
they all have inherited the attributes
17295
11:22:52,320 --> 11:22:55,116
of the class motorbike and then on top
17296
11:22:55,116 --> 11:22:57,916
of it defines certain variables
17297
11:22:57,916 --> 11:23:01,116
additional variables and methods
17298
11:23:01,116 --> 11:23:03,520
on top of it to enhance the motorbike
17299
11:23:03,520 --> 11:23:08,320
and make it look different or function
17300
11:23:12,840 --> 11:23:15,436
differently now what are methods in
17301
11:23:15,436 --> 11:23:17,276
python methods
17302
11:23:17,276 --> 11:23:20,480
are functions defined inside the body of
17303
11:23:20,480 --> 11:23:22,080
a class right
17304
11:23:22,080 --> 11:23:24,320
so basically methods are something which
17305
11:23:24,320 --> 11:23:25,040
like
17306
11:23:25,040 --> 11:23:28,160
which you which you perform on a
17307
11:23:28,160 --> 11:23:30,320
attributes of a class right method is a
17308
11:23:30,320 --> 11:23:32,560
function that is that is associated with
17309
11:23:32,560 --> 11:23:33,756
an object
17310
11:23:33,756 --> 11:23:35,916
so you create an object of a class and
17311
11:23:35,916 --> 11:23:37,840
on that object then you can execute
17312
11:23:37,840 --> 11:23:39,680
those functions
17313
11:23:39,680 --> 11:23:42,160
in python a method is not unique to
17314
11:23:42,160 --> 11:23:44,000
class instances
17315
11:23:44,000 --> 11:23:48,240
any object type can have methods
17316
11:23:49,200 --> 11:23:52,160
and the methods define the behavior of
17317
11:23:52,160 --> 11:23:55,360
an object so that's why a bmw motorbike
17318
11:23:55,360 --> 11:23:57,436
will be different from royal enfield
17319
11:23:57,436 --> 11:23:59,756
because the behavior is different the
17320
11:23:59,756 --> 11:24:02,640
methods are different
17321
11:24:02,640 --> 11:24:04,560
so if you see the example over here you
17322
11:24:04,560 --> 11:24:06,240
have a class bike
17323
11:24:06,240 --> 11:24:08,320
and in that there is a init function
17324
11:24:08,320 --> 11:24:10,320
which will get invoked when you
17325
11:24:10,320 --> 11:24:13,436
initialize the object of class bike and
17326
11:24:13,436 --> 11:24:15,596
then you can set the brand name and the
17327
11:24:15,596 --> 11:24:17,596
year of manufacturing for that
17328
11:24:17,596 --> 11:24:21,160
particular object
17329
11:24:24,480 --> 11:24:26,400
now inheritance
17330
11:24:26,400 --> 11:24:28,240
what is inheritance inheritance is
17331
11:24:28,240 --> 11:24:30,400
something which you derive
17332
11:24:30,400 --> 11:24:31,360
from a
17333
11:24:31,360 --> 11:24:34,240
parent so inheritance is like creating a
17334
11:24:34,240 --> 11:24:37,276
new class that uses all the properties
17335
11:24:37,276 --> 11:24:38,560
and behavior
17336
11:24:38,560 --> 11:24:40,560
of another class
17337
11:24:40,560 --> 11:24:42,880
now the new class is called the derived
17338
11:24:42,880 --> 11:24:45,116
class or the child class and the
17339
11:24:45,116 --> 11:24:47,360
existing class whose properties are
17340
11:24:47,360 --> 11:24:50,240
inherited is known as a base class or
17341
11:24:50,240 --> 11:24:51,680
parent class
17342
11:24:51,680 --> 11:24:53,840
it provides and the basic view
17343
11:24:53,840 --> 11:24:55,680
functionality the usage of inheritance
17344
11:24:55,680 --> 11:24:57,756
is that reusability of code
17345
11:24:57,756 --> 11:25:00,000
so if anything is defined in the parent
17346
11:25:00,000 --> 11:25:02,000
class then you don't need to rewrite
17347
11:25:02,000 --> 11:25:04,480
that particular code in the child you
17348
11:25:04,480 --> 11:25:07,840
can just inherit it
17349
11:25:09,840 --> 11:25:12,400
so as i mentioned let's take a look at
17350
11:25:12,400 --> 11:25:14,240
the different types of inheritance which
17351
11:25:14,240 --> 11:25:15,436
are available
17352
11:25:15,436 --> 11:25:18,640
you have single level in which only one
17353
11:25:18,640 --> 11:25:20,480
child is inheriting
17354
11:25:20,480 --> 11:25:22,480
the properties and the attributes from
17355
11:25:22,480 --> 11:25:23,756
the parent
17356
11:25:23,756 --> 11:25:27,436
then you have multi-level in which
17357
11:25:27,436 --> 11:25:30,080
first child has inherited from the class
17358
11:25:30,080 --> 11:25:32,320
a class b has inherited and then you
17359
11:25:32,320 --> 11:25:34,720
have another class c which is inheriting
17360
11:25:34,720 --> 11:25:36,960
from class b so this is an example of
17361
11:25:36,960 --> 11:25:39,520
multi-level and we will be supporting
17362
11:25:39,520 --> 11:25:41,360
all these concepts with an example in
17363
11:25:41,360 --> 11:25:42,560
our python
17364
11:25:42,560 --> 11:25:44,160
uh notebook
17365
11:25:44,160 --> 11:25:45,596
then there is hierarchical then there
17366
11:25:45,596 --> 11:25:48,720
are two different classes which are
17367
11:25:48,720 --> 11:25:51,040
inheriting from a single class
17368
11:25:51,040 --> 11:25:52,880
and then multiple inheritance that
17369
11:25:52,880 --> 11:25:55,116
single class is inheriting from two
17370
11:25:55,116 --> 11:25:56,720
different parents
17371
11:25:56,720 --> 11:25:58,960
so these are different set varieties of
17372
11:25:58,960 --> 11:26:01,756
inheritance basis what is the demand
17373
11:26:01,756 --> 11:26:03,756
what is the business requirement you can
17374
11:26:03,756 --> 11:26:06,160
define
17375
11:26:07,360 --> 11:26:10,000
now what is polymorphism polymorphism in
17376
11:26:10,000 --> 11:26:11,360
python
17377
11:26:11,360 --> 11:26:13,040
what does it means
17378
11:26:13,040 --> 11:26:15,916
polymorphism consists of two words poly
17379
11:26:15,916 --> 11:26:17,116
and morphs
17380
11:26:17,116 --> 11:26:20,560
poly means many and morph means shape
17381
11:26:20,560 --> 11:26:22,640
so basically it means
17382
11:26:22,640 --> 11:26:24,160
many shapes
17383
11:26:24,160 --> 11:26:26,720
so it is it means using a familiar
17384
11:26:26,720 --> 11:26:29,596
interface for multiple forms right so
17385
11:26:29,596 --> 11:26:30,960
you can use
17386
11:26:30,960 --> 11:26:33,680
a single name of the method and which
17387
11:26:33,680 --> 11:26:36,960
can perform different behavior
17388
11:26:36,960 --> 11:26:39,596
based on the object in which it is
17389
11:26:39,596 --> 11:26:42,400
invoked so in simple terms we understand
17390
11:26:42,400 --> 11:26:45,116
that a task can be performed in various
17391
11:26:45,116 --> 11:26:46,400
ways
17392
11:26:46,400 --> 11:26:48,400
let's support it with an example
17393
11:26:48,400 --> 11:26:50,880
like you have a plus operator now the
17394
11:26:50,880 --> 11:26:53,116
plus operator can be used for integer
17395
11:26:53,116 --> 11:26:55,520
data types to perform arithmetic
17396
11:26:55,520 --> 11:26:56,960
operations
17397
11:26:56,960 --> 11:27:00,080
whereas when you use plus operator to in
17398
11:27:00,080 --> 11:27:02,080
string data types it can be used for
17399
11:27:02,080 --> 11:27:03,520
concatenation
17400
11:27:03,520 --> 11:27:06,480
so it's a very typical example very good
17401
11:27:06,480 --> 11:27:08,160
example in terms of
17402
11:27:08,160 --> 11:27:09,680
how an uh
17403
11:27:09,680 --> 11:27:11,040
same operator
17404
11:27:11,040 --> 11:27:13,680
can behave differently bases what data
17405
11:27:13,680 --> 11:27:16,720
types are being used
17406
11:27:20,000 --> 11:27:22,960
now let's talk about encapsulation
17407
11:27:22,960 --> 11:27:25,596
using oops in python we can restrict
17408
11:27:25,596 --> 11:27:27,840
access to methods and variable
17409
11:27:27,840 --> 11:27:29,360
so basically in
17410
11:27:29,360 --> 11:27:31,756
in python you can you can create
17411
11:27:31,756 --> 11:27:33,840
something like as private
17412
11:27:33,840 --> 11:27:36,480
variables but of course you can't use a
17413
11:27:36,480 --> 11:27:40,240
private keyword unlike java so in in
17414
11:27:40,240 --> 11:27:42,320
python we use underscore or double
17415
11:27:42,320 --> 11:27:44,080
underscore in order to define private
17416
11:27:44,080 --> 11:27:45,840
attributes so basically
17417
11:27:45,840 --> 11:27:48,640
these attributes can be only used within
17418
11:27:48,640 --> 11:27:52,240
the class and cannot be exposed outside
17419
11:27:52,240 --> 11:27:55,520
a particular class
17420
11:27:55,520 --> 11:28:00,160
so we will see this with an example
17421
11:28:02,800 --> 11:28:05,360
now what is abstraction data abstraction
17422
11:28:05,360 --> 11:28:07,520
and capsule encapsulation are often used
17423
11:28:07,520 --> 11:28:10,320
as synonyms abstraction is used to hide
17424
11:28:10,320 --> 11:28:12,640
internal details and show only
17425
11:28:12,640 --> 11:28:14,000
functionalities
17426
11:28:14,000 --> 11:28:15,276
for example
17427
11:28:15,276 --> 11:28:18,000
you know how to turn on or offer light
17428
11:28:18,000 --> 11:28:20,000
using a switch but you don't know what
17429
11:28:20,000 --> 11:28:22,960
is happening behind socket similarly an
17430
11:28:22,960 --> 11:28:26,640
abstract class can not be instantiated
17431
11:28:26,640 --> 11:28:29,040
which means you cannot create objects
17432
11:28:29,040 --> 11:28:31,276
for this class you can only
17433
11:28:31,276 --> 11:28:32,400
define
17434
11:28:32,400 --> 11:28:33,840
the methods
17435
11:28:33,840 --> 11:28:36,240
which have to be implemented by the
17436
11:28:36,240 --> 11:28:38,480
classes which are inheriting the
17437
11:28:38,480 --> 11:28:40,080
abstracting
17438
11:28:40,080 --> 11:28:42,560
so basically abstract class provide only
17439
11:28:42,560 --> 11:28:44,320
the method signatures
17440
11:28:44,320 --> 11:28:47,276
and rest the other functionalities and
17441
11:28:47,276 --> 11:28:49,116
the implementation has to be done by the
17442
11:28:49,116 --> 11:28:51,840
child classes
17443
11:28:53,200 --> 11:28:56,720
so let's now move to the
17444
11:28:56,880 --> 11:28:57,756
our
17445
11:28:57,756 --> 11:29:00,880
jupiter notebook and we will
17446
11:29:00,880 --> 11:29:05,400
start performing certain examples
17447
11:29:05,436 --> 11:29:06,960
now as we are saying
17448
11:29:06,960 --> 11:29:10,640
like python has objects so let's create
17449
11:29:10,640 --> 11:29:12,400
object
17450
11:29:12,400 --> 11:29:14,160
we will create a class
17451
11:29:14,160 --> 11:29:16,640
bikes over here
17452
11:29:16,640 --> 11:29:20,240
and within the class
17453
11:29:28,560 --> 11:29:33,320
we will define the init method
17454
11:29:33,596 --> 11:29:34,640
and
17455
11:29:34,640 --> 11:29:37,360
in the init
17456
11:29:37,436 --> 11:29:39,276
we will
17457
11:29:39,276 --> 11:29:43,916
instantiate two variables brand name and
17458
11:29:43,916 --> 11:29:46,916
here
17459
11:30:01,916 --> 11:30:04,160
now
17460
11:30:05,276 --> 11:30:08,560
we will create an object
17461
11:30:08,560 --> 11:30:09,520
of
17462
11:30:09,520 --> 11:30:11,916
bike
17463
11:30:12,560 --> 11:30:15,276
which we we are calling as bmw and
17464
11:30:15,276 --> 11:30:17,040
another we will create an object called
17465
11:30:17,040 --> 11:30:19,840
ducati so what we are doing in this
17466
11:30:19,840 --> 11:30:21,596
to scenarios is
17467
11:30:21,596 --> 11:30:22,960
that we are
17468
11:30:22,960 --> 11:30:25,360
just instantiating the byte of bike
17469
11:30:25,360 --> 11:30:26,840
object with
17470
11:30:26,840 --> 11:30:30,160
variable bmw and ducati we are
17471
11:30:30,160 --> 11:30:32,960
instantiating
17472
11:30:34,000 --> 11:30:34,880
now
17473
11:30:34,880 --> 11:30:36,400
let's
17474
11:30:36,400 --> 11:30:38,400
print
17475
11:30:38,400 --> 11:30:39,596
the
17476
11:30:39,596 --> 11:30:42,320
year and manufacturing here the brand
17477
11:30:42,320 --> 11:30:44,080
name and the ear
17478
11:30:44,080 --> 11:30:48,720
for bmw and for ducati and let's see
17479
11:30:48,720 --> 11:30:51,116
right so what we have done is we have
17480
11:30:51,116 --> 11:30:53,916
created an object for bikes one we are
17481
11:30:53,916 --> 11:30:55,840
calling bmw and one we are calling
17482
11:30:55,840 --> 11:30:58,000
ducati and how we are differentiating
17483
11:30:58,000 --> 11:30:59,840
between these two bytes is basis the
17484
11:30:59,840 --> 11:31:04,160
brand name and the year of manufacturing
17485
11:31:04,400 --> 11:31:05,436
now
17486
11:31:05,436 --> 11:31:08,800
let's take a look at the example of
17487
11:31:08,800 --> 11:31:11,756
methods
17488
11:31:11,756 --> 11:31:13,040
i'm gonna
17489
11:31:13,040 --> 11:31:16,640
rewrite my init method
17490
11:31:16,720 --> 11:31:20,000
and now i am
17491
11:31:20,000 --> 11:31:22,480
creating an
17492
11:31:22,480 --> 11:31:23,520
function
17493
11:31:23,520 --> 11:31:24,400
a method
17494
11:31:24,400 --> 11:31:26,720
of top speed
17495
11:31:26,720 --> 11:31:29,116
and
17496
11:31:37,680 --> 11:31:41,840
a function of type
17497
11:31:45,116 --> 11:31:46,320
now
17498
11:31:46,320 --> 11:31:47,680
i'm gonna
17499
11:31:47,680 --> 11:31:52,116
create an object of bike
17500
11:31:52,960 --> 11:31:55,520
now this
17501
11:31:55,840 --> 11:31:58,240
method this object bike
17502
11:31:58,240 --> 11:32:01,276
can invoke methods top speed and type
17503
11:32:01,276 --> 11:32:03,436
because they belong to the same class
17504
11:32:03,436 --> 11:32:05,040
bytes
17505
11:32:05,040 --> 11:32:07,360
so initially when i created my object
17506
11:32:07,360 --> 11:32:10,080
bytes i'm instantiating it with bmw and
17507
11:32:10,080 --> 11:32:11,436
the ear
17508
11:32:11,436 --> 11:32:14,000
and then
17509
11:32:14,640 --> 11:32:15,840
i am
17510
11:32:15,840 --> 11:32:17,680
making a call to the
17511
11:32:17,680 --> 11:32:21,200
method top speed and then i'm making a
17512
11:32:21,200 --> 11:32:24,480
call to the method type
17513
11:32:24,480 --> 11:32:25,756
and printing
17514
11:32:25,756 --> 11:32:28,800
so basically i'm printing the output of
17515
11:32:28,800 --> 11:32:31,116
the function top speed and output of the
17516
11:32:31,116 --> 11:32:34,520
function bmw
17517
11:32:35,116 --> 11:32:37,680
form the object bmw which is of class
17518
11:32:37,680 --> 11:32:40,080
bikes
17519
11:32:45,040 --> 11:32:46,640
now let's take a look
17520
11:32:46,640 --> 11:32:48,800
at an example of single level
17521
11:32:48,800 --> 11:32:51,520
inheritance
17522
11:32:52,320 --> 11:32:53,200
let's
17523
11:32:53,200 --> 11:32:56,240
create a parent class called animal
17524
11:32:56,240 --> 11:32:57,520
and
17525
11:32:57,520 --> 11:32:59,520
create a
17526
11:32:59,520 --> 11:33:03,800
method called sound
17527
11:33:07,360 --> 11:33:11,436
and let's create a child class
17528
11:33:11,756 --> 11:33:14,840
cat which inherits
17529
11:33:14,840 --> 11:33:18,160
animal and let me just put a method over
17530
11:33:18,160 --> 11:33:21,160
here
17531
11:33:31,040 --> 11:33:34,480
is the indentation
17532
11:33:34,640 --> 11:33:35,756
okay
17533
11:33:35,756 --> 11:33:37,680
so now i have
17534
11:33:37,680 --> 11:33:38,800
object
17535
11:33:38,800 --> 11:33:41,200
parent class animal child class cat
17536
11:33:41,200 --> 11:33:43,520
which has inherited animal
17537
11:33:43,520 --> 11:33:46,000
and the parent class method is sound and
17538
11:33:46,000 --> 11:33:48,800
the child class method is meow so now
17539
11:33:48,800 --> 11:33:52,240
i'll create an object called cat
17540
11:33:52,240 --> 11:33:53,916
and with this object
17541
11:33:53,916 --> 11:33:56,840
i'll be able to invoke both child class
17542
11:33:56,840 --> 11:34:01,360
method and the parent class method
17543
11:34:02,320 --> 11:34:04,080
and as you can see
17544
11:34:04,080 --> 11:34:05,916
the child class
17545
11:34:05,916 --> 11:34:08,480
has inherited both
17546
11:34:08,480 --> 11:34:10,840
parent and child
17547
11:34:10,840 --> 11:34:15,200
class functions methods
17548
11:34:15,200 --> 11:34:18,240
now let's take a look at
17549
11:34:18,240 --> 11:34:21,040
multi-level inheritance example
17550
11:34:21,040 --> 11:34:22,640
i'll start off
17551
11:34:22,640 --> 11:34:25,200
with creating exactly the same
17552
11:34:25,200 --> 11:34:28,080
parent class called animal
17553
11:34:28,080 --> 11:34:28,960
then
17554
11:34:28,960 --> 11:34:31,520
i am i am already created another class
17555
11:34:31,520 --> 11:34:32,880
i'll create
17556
11:34:32,880 --> 11:34:36,000
same child class cat
17557
11:34:36,000 --> 11:34:36,960
and
17558
11:34:36,960 --> 11:34:40,480
now i'll and create another
17559
11:34:40,480 --> 11:34:43,520
class which will inherit cat class
17560
11:34:43,520 --> 11:34:45,756
so cat has inherited animal and cat
17561
11:34:45,756 --> 11:34:48,960
child has inherited cat
17562
11:34:49,040 --> 11:34:52,080
let's validate its syntax
17563
11:34:52,080 --> 11:34:55,200
and similarly now i'll create an object
17564
11:34:55,200 --> 11:34:57,596
for cat child
17565
11:34:57,596 --> 11:35:01,116
and this cat child now
17566
11:35:01,116 --> 11:35:02,400
can
17567
11:35:02,400 --> 11:35:05,520
invoke the same methods
17568
11:35:05,520 --> 11:35:07,360
which are in child
17569
11:35:07,360 --> 11:35:09,040
parent and the
17570
11:35:09,040 --> 11:35:11,840
super parent
17571
11:35:21,116 --> 11:35:23,360
so this is an example of multi-level
17572
11:35:23,360 --> 11:35:25,840
this is an example of
17573
11:35:25,840 --> 11:35:29,480
single level inheritance
17574
11:35:44,160 --> 11:35:45,680
now
17575
11:35:45,680 --> 11:35:50,360
let's take an example of polymorphism
17576
11:35:51,756 --> 11:35:53,756
we are going to click create a class
17577
11:35:53,756 --> 11:35:56,160
parrot
17578
11:35:56,320 --> 11:36:00,560
and define the function fly
17579
11:36:00,560 --> 11:36:03,560
and
17580
11:36:10,480 --> 11:36:14,800
now we are gonna create another class
17581
11:36:14,800 --> 11:36:16,640
mind you there is no inheritance here we
17582
11:36:16,640 --> 11:36:18,480
are just creating an independent class
17583
11:36:18,480 --> 11:36:20,640
called penguin
17584
11:36:20,640 --> 11:36:24,240
and same methods now the same name
17585
11:36:24,240 --> 11:36:26,640
fly
17586
11:36:28,080 --> 11:36:30,400
and
17587
11:36:31,040 --> 11:36:33,840
swim
17588
11:36:43,520 --> 11:36:46,520
okay
17589
11:36:50,080 --> 11:36:53,116
now let's make a
17590
11:36:54,480 --> 11:36:57,480
function
17591
11:37:00,800 --> 11:37:03,680
in which we will pass
17592
11:37:03,680 --> 11:37:06,320
an object
17593
11:37:06,320 --> 11:37:09,116
and basis
17594
11:37:09,276 --> 11:37:11,596
that what is the instance of the
17595
11:37:11,596 --> 11:37:14,320
particular object bird it the
17596
11:37:14,320 --> 11:37:18,480
corresponding method will get invoked
17597
11:37:18,640 --> 11:37:20,080
so let's see
17598
11:37:20,080 --> 11:37:23,116
in the method flying test
17599
11:37:23,116 --> 11:37:25,680
whatever instance of the bird object
17600
11:37:25,680 --> 11:37:28,640
will be the corresponding of method will
17601
11:37:28,640 --> 11:37:29,840
be called
17602
11:37:29,840 --> 11:37:32,000
so now let's create a
17603
11:37:32,000 --> 11:37:35,116
object of parrot class and then
17604
11:37:35,116 --> 11:37:39,840
let's create a object of penguin class
17605
11:37:41,596 --> 11:37:43,916
now in my flying test
17606
11:37:43,916 --> 11:37:45,040
method
17607
11:37:45,040 --> 11:37:48,240
first i'll send a object of instance
17608
11:37:48,240 --> 11:37:49,520
parrot
17609
11:37:49,520 --> 11:37:51,436
and then
17610
11:37:51,436 --> 11:37:53,680
i'll
17611
11:37:53,680 --> 11:37:57,040
send the object of instance penguin
17612
11:37:57,040 --> 11:37:59,756
so now base is the instance type
17613
11:37:59,756 --> 11:38:02,080
when this bird dot fly
17614
11:38:02,080 --> 11:38:04,560
method will be called it will be
17615
11:38:04,560 --> 11:38:06,960
called basis the instance so in the
17616
11:38:06,960 --> 11:38:10,400
first case the parrot it will call this
17617
11:38:10,400 --> 11:38:11,436
method
17618
11:38:11,436 --> 11:38:13,360
and in case of penguin it will call this
17619
11:38:13,360 --> 11:38:16,160
method fly
17620
11:38:17,916 --> 11:38:20,000
so please note what is the output the
17621
11:38:20,000 --> 11:38:23,680
output is a parrot can fly but a penguin
17622
11:38:23,680 --> 11:38:26,400
cannot fly
17623
11:38:26,400 --> 11:38:28,560
and it has been decided
17624
11:38:28,560 --> 11:38:31,116
by the python
17625
11:38:31,116 --> 11:38:33,360
at the run time that which particular
17626
11:38:33,360 --> 11:38:38,040
method to be invoked bases the instance
17627
11:38:38,640 --> 11:38:41,840
so it's a very important concept
17628
11:38:41,840 --> 11:38:43,840
now let's take an example of
17629
11:38:43,840 --> 11:38:46,800
encapsulation
17630
11:38:49,276 --> 11:38:53,436
i'll make a class cars
17631
11:38:53,436 --> 11:38:57,200
define its init method
17632
11:38:57,200 --> 11:39:00,000
now within that init method i have
17633
11:39:00,000 --> 11:39:01,360
created a
17634
11:39:01,360 --> 11:39:03,200
private variable with underscore
17635
11:39:03,200 --> 11:39:05,840
underscore max price and given a value
17636
11:39:05,840 --> 11:39:07,680
of
17637
11:39:07,680 --> 11:39:10,240
9 lakh
17638
11:39:12,320 --> 11:39:16,680
now i am creating another
17639
11:39:21,040 --> 11:39:23,116
function called sell
17640
11:39:23,116 --> 11:39:25,916
and in that i am printing the
17641
11:39:25,916 --> 11:39:29,320
max price
17642
11:39:34,880 --> 11:39:36,960
and then
17643
11:39:36,960 --> 11:39:39,436
i am creating another function in which
17644
11:39:39,436 --> 11:39:42,240
i am modifying the private variable set
17645
11:39:42,240 --> 11:39:43,520
max price
17646
11:39:43,520 --> 11:39:46,080
so i will pass any value in the set max
17647
11:39:46,080 --> 11:39:48,880
price and it will replace the max price
17648
11:39:48,880 --> 11:39:50,480
which i have set during the
17649
11:39:50,480 --> 11:39:54,320
initialization at this point
17650
11:39:56,720 --> 11:40:02,320
now let's create an object of class cars
17651
11:40:02,320 --> 11:40:03,840
and
17652
11:40:03,840 --> 11:40:06,080
see if i sell this then the selling
17653
11:40:06,080 --> 11:40:08,720
price is
17654
11:40:09,040 --> 11:40:11,916
9 lakh what i had in the set at the time
17655
11:40:11,916 --> 11:40:14,880
of initialization
17656
11:40:15,116 --> 11:40:17,276
now what i'm trying to do is i'm trying
17657
11:40:17,276 --> 11:40:19,680
to modify a private variable
17658
11:40:19,680 --> 11:40:20,960
directly
17659
11:40:20,960 --> 11:40:23,756
c underscore underscore max price
17660
11:40:23,756 --> 11:40:26,320
and i i want to check
17661
11:40:26,320 --> 11:40:28,800
if i'm trying to create a modify a
17662
11:40:28,800 --> 11:40:30,400
private variable
17663
11:40:30,400 --> 11:40:33,200
will it still show me 9 lakh or 10 lakh
17664
11:40:33,200 --> 11:40:36,000
value and it's still showing me the 9
17665
11:40:36,000 --> 11:40:39,200
lakh value it means that i can't
17666
11:40:39,200 --> 11:40:40,960
directly
17667
11:40:40,960 --> 11:40:43,520
approach or access the
17668
11:40:43,520 --> 11:40:45,116
private variable
17669
11:40:45,116 --> 11:40:47,680
i can only modify it
17670
11:40:47,680 --> 11:40:49,916
if i access it through a
17671
11:40:49,916 --> 11:40:53,040
method which is set max price in this
17672
11:40:53,040 --> 11:40:55,040
now i am setting the max price to
17673
11:40:55,040 --> 11:40:56,240
thousand
17674
11:40:56,240 --> 11:40:59,200
and now if i print what is my max price
17675
11:40:59,200 --> 11:41:01,520
it will print as
17676
11:41:01,520 --> 11:41:03,680
thousand
17677
11:41:03,680 --> 11:41:06,080
so this is the concept of encapsulation
17678
11:41:06,080 --> 11:41:08,320
you have a private variable which is
17679
11:41:08,320 --> 11:41:11,596
accessible within only the methods
17680
11:41:11,596 --> 11:41:12,800
of the
17681
11:41:12,800 --> 11:41:17,200
class you can't access it directly
17682
11:41:24,960 --> 11:41:27,436
now let's take a look at archaeal
17683
11:41:27,436 --> 11:41:30,080
inheritance
17684
11:41:31,916 --> 11:41:36,080
i'll create a class employee
17685
11:41:36,080 --> 11:41:39,080
and
17686
11:41:41,840 --> 11:41:45,040
create init method
17687
11:41:47,840 --> 11:41:49,840
within that you have
17688
11:41:49,840 --> 11:41:53,200
three variables name agent salary so an
17689
11:41:53,200 --> 11:41:58,160
employee with has name agent salary
17690
11:41:58,160 --> 11:41:59,276
now
17691
11:41:59,276 --> 11:42:01,916
i'm going to create a class
17692
11:42:01,916 --> 11:42:04,240
child class called child employee 1
17693
11:42:04,240 --> 11:42:07,360
which is inherited from employee
17694
11:42:07,360 --> 11:42:10,240
and child employee 2
17695
11:42:10,240 --> 11:42:13,840
which is also inherited from
17696
11:42:14,560 --> 11:42:17,560
employee
17697
11:42:24,400 --> 11:42:27,596
so what i'm gonna do is i'm gonna create
17698
11:42:27,596 --> 11:42:28,480
a
17699
11:42:28,480 --> 11:42:29,596
object
17700
11:42:29,596 --> 11:42:33,320
per child class
17701
11:42:38,800 --> 11:42:39,680
and
17702
11:42:39,680 --> 11:42:42,080
print
17703
11:42:49,436 --> 11:42:52,240
so this is a typical example in which
17704
11:42:52,240 --> 11:42:54,400
you have two child
17705
11:42:54,400 --> 11:42:57,596
classes which have inherited from the
17706
11:42:57,596 --> 11:42:59,436
single parent
17707
11:42:59,436 --> 11:43:03,320
ark in narratives
17708
11:43:07,680 --> 11:43:10,080
now let's take a look at
17709
11:43:10,080 --> 11:43:15,000
our last concept which is abstraction
17710
11:43:26,080 --> 11:43:30,880
so now i'll create a class car
17711
11:43:31,360 --> 11:43:34,480
and define a method
17712
11:43:34,480 --> 11:43:38,560
mileage but i am not
17713
11:43:38,560 --> 11:43:40,720
giving any definition to it i'm just
17714
11:43:40,720 --> 11:43:43,680
giving a signature mileage
17715
11:43:43,680 --> 11:43:44,800
the child
17716
11:43:44,800 --> 11:43:46,720
classes which will inhibit abstract
17717
11:43:46,720 --> 11:43:47,680
class
17718
11:43:47,680 --> 11:43:50,000
will be giving the definition
17719
11:43:50,000 --> 11:43:51,596
so i am giving
17720
11:43:51,596 --> 11:43:54,720
creating a class bmw
17721
11:43:54,720 --> 11:43:57,680
which will again
17722
11:43:57,756 --> 11:44:02,160
which has inherited the car abc car but
17723
11:44:02,160 --> 11:44:06,276
given the definition now
17724
11:44:17,436 --> 11:44:18,720
similarly
17725
11:44:18,720 --> 11:44:22,480
i am creating another class
17726
11:44:22,480 --> 11:44:26,320
jaguar which has inherited the class car
17727
11:44:26,320 --> 11:44:28,160
but now it is giving a different
17728
11:44:28,160 --> 11:44:29,520
definition
17729
11:44:29,520 --> 11:44:31,916
it's printing mileage 25 kilometer per
17730
11:44:31,916 --> 11:44:33,200
hour
17731
11:44:33,200 --> 11:44:35,436
and
17732
11:44:35,840 --> 11:44:38,720
another class
17733
11:44:40,720 --> 11:44:43,360
range rover
17734
11:44:43,360 --> 11:44:46,240
and another class
17735
11:44:46,240 --> 11:44:47,840
renault
17736
11:44:47,840 --> 11:44:48,640
okay
17737
11:44:48,640 --> 11:44:51,200
so four classes
17738
11:44:51,200 --> 11:44:53,436
which have implemented the abstract
17739
11:44:53,436 --> 11:44:57,840
class method mileage in a different way
17740
11:45:00,080 --> 11:45:03,916
now i'll create an object of first
17741
11:45:03,916 --> 11:45:06,640
bmw
17742
11:45:08,640 --> 11:45:11,360
and
17743
11:45:11,360 --> 11:45:12,880
invoke method
17744
11:45:12,880 --> 11:45:15,436
mileage
17745
11:45:17,200 --> 11:45:19,116
so it has method invoke the method
17746
11:45:19,116 --> 11:45:22,000
mileage and bmw car and printed mileage
17747
11:45:22,000 --> 11:45:25,360
is 30 kilometer similarly
17748
11:45:25,360 --> 11:45:28,480
i'll do for renault
17749
11:45:32,880 --> 11:45:35,436
jaguar
17750
11:45:35,520 --> 11:45:38,320
and
17751
11:45:38,320 --> 11:45:40,960
range rover
17752
11:45:41,596 --> 11:45:42,880
so if you see
17753
11:45:42,880 --> 11:45:43,680
the
17754
11:45:43,680 --> 11:45:44,960
parent class
17755
11:45:44,960 --> 11:45:47,116
abstract parent class has not
17756
11:45:47,116 --> 11:45:48,960
implemented the method but just given
17757
11:45:48,960 --> 11:45:51,520
the signature of the method and all
17758
11:45:51,520 --> 11:45:53,436
other classes have implemented it and
17759
11:45:53,436 --> 11:45:56,080
that's the concept of abstraction
17760
11:45:56,080 --> 11:45:58,000
so what's in it for us today
17761
11:45:58,000 --> 11:45:59,840
what we will understand what is operator
17762
11:45:59,840 --> 11:46:00,960
overloading
17763
11:46:00,960 --> 11:46:03,040
and how to overload the operators in
17764
11:46:03,040 --> 11:46:05,520
python we will perform these activities
17765
11:46:05,520 --> 11:46:08,240
in the jupyter notebook
17766
11:46:08,240 --> 11:46:10,480
now what is operator overloading
17767
11:46:10,480 --> 11:46:12,720
python operators work for built-in
17768
11:46:12,720 --> 11:46:16,080
classes in a predefined operational way
17769
11:46:16,080 --> 11:46:18,080
but the same operator behaves
17770
11:46:18,080 --> 11:46:20,400
differently for objects of different
17771
11:46:20,400 --> 11:46:21,436
classes
17772
11:46:21,436 --> 11:46:23,756
so you can make the operator work
17773
11:46:23,756 --> 11:46:24,960
differently
17774
11:46:24,960 --> 11:46:27,360
bases the requirement you have and the
17775
11:46:27,360 --> 11:46:29,040
objects you have defined
17776
11:46:29,040 --> 11:46:31,116
so for example the plus operator which
17777
11:46:31,116 --> 11:46:33,520
is the most commonly used operator will
17778
11:46:33,520 --> 11:46:36,640
perform arithmetic addition on two
17779
11:46:36,640 --> 11:46:39,596
numbers or it can merge to less
17780
11:46:39,596 --> 11:46:41,756
or if performed on two strings it
17781
11:46:41,756 --> 11:46:45,360
concatenates the two strings so it means
17782
11:46:45,360 --> 11:46:47,756
the programming language automatically
17783
11:46:47,756 --> 11:46:50,640
understands that in which context you
17784
11:46:50,640 --> 11:46:52,960
are using the plus operator
17785
11:46:52,960 --> 11:46:55,916
this feature in python that allows the
17786
11:46:55,916 --> 11:46:59,116
same operator to have different meanings
17787
11:46:59,116 --> 11:47:01,040
according to the context according to
17788
11:47:01,040 --> 11:47:03,276
the objects which are calling it or
17789
11:47:03,276 --> 11:47:04,960
according to the data types which are
17790
11:47:04,960 --> 11:47:07,436
being used in that operator it is called
17791
11:47:07,436 --> 11:47:10,160
as operator overloading
17792
11:47:10,160 --> 11:47:13,840
so if you see in this example 8 and 7
17793
11:47:13,840 --> 11:47:16,560
are the operands and the plus sign is
17794
11:47:16,560 --> 11:47:19,276
the operator sign
17795
11:47:19,276 --> 11:47:21,116
so that is the whole goal of this whole
17796
11:47:21,116 --> 11:47:23,680
exercise that we will show you
17797
11:47:23,680 --> 11:47:25,436
how you can change
17798
11:47:25,436 --> 11:47:28,160
the meaning of the plus operator
17799
11:47:28,160 --> 11:47:31,116
depending on the kind of objects or kind
17800
11:47:31,116 --> 11:47:35,360
of operands you're using along with it
17801
11:47:36,080 --> 11:47:38,320
now how to overload the operators in
17802
11:47:38,320 --> 11:47:39,596
python
17803
11:47:39,596 --> 11:47:42,880
consider we have two objects b1 and b2
17804
11:47:42,880 --> 11:47:44,960
of a class batsman
17805
11:47:44,960 --> 11:47:47,200
now we must add these objects with a
17806
11:47:47,200 --> 11:47:48,960
binary plus operator
17807
11:47:48,960 --> 11:47:52,480
so now the task in hand is that we use
17808
11:47:52,480 --> 11:47:54,720
the plus operator to add objects which
17809
11:47:54,720 --> 11:47:57,040
is not possible until and unless we
17810
11:47:57,040 --> 11:47:59,200
overload this operator
17811
11:47:59,200 --> 11:48:02,320
otherwise it will throw an error because
17812
11:48:02,320 --> 11:48:04,720
the compiler does not know how to add to
17813
11:48:04,720 --> 11:48:07,520
objects it only knows how to add
17814
11:48:07,520 --> 11:48:11,360
primitive data types like in strings etc
17815
11:48:11,360 --> 11:48:14,240
so basically we will define a method
17816
11:48:14,240 --> 11:48:16,880
for an operator that process and that
17817
11:48:16,880 --> 11:48:19,680
process is called operator overloading
17818
11:48:19,680 --> 11:48:21,756
so in the example which we will be
17819
11:48:21,756 --> 11:48:23,840
showing you we will be taking two
17820
11:48:23,840 --> 11:48:26,800
objects of batsman 1 and batsman 2 and
17821
11:48:26,800 --> 11:48:29,840
we will be using two scores per patchman
17822
11:48:29,840 --> 11:48:31,756
and then we will add them so we will
17823
11:48:31,756 --> 11:48:34,320
discuss that in the examples we will
17824
11:48:34,320 --> 11:48:36,080
show you
17825
11:48:36,080 --> 11:48:37,360
now
17826
11:48:37,360 --> 11:48:39,360
we can overload all the defined
17827
11:48:39,360 --> 11:48:41,596
operators so there's no limitation
17828
11:48:41,596 --> 11:48:43,596
addition subtraction multiplication
17829
11:48:43,596 --> 11:48:44,880
division
17830
11:48:44,880 --> 11:48:47,040
greater than less than so there are
17831
11:48:47,040 --> 11:48:49,840
certain magic methods or we can call it
17832
11:48:49,840 --> 11:48:51,436
as the primitive methods or special
17833
11:48:51,436 --> 11:48:52,480
functions
17834
11:48:52,480 --> 11:48:55,520
that are automatically invoked when an
17835
11:48:55,520 --> 11:48:57,040
associated
17836
11:48:57,040 --> 11:48:59,520
operator is being called
17837
11:48:59,520 --> 11:49:01,916
so basically if you have to do the
17838
11:49:01,916 --> 11:49:03,596
operator overloading
17839
11:49:03,596 --> 11:49:06,160
you basically have to override these
17840
11:49:06,160 --> 11:49:08,400
methods in your python program
17841
11:49:08,400 --> 11:49:11,360
and that's how you will be able to
17842
11:49:11,360 --> 11:49:14,320
override or overload the primitive
17843
11:49:14,320 --> 11:49:15,840
implementations
17844
11:49:15,840 --> 11:49:17,596
of these functions written in the
17845
11:49:17,596 --> 11:49:20,640
compiler of python
17846
11:49:20,640 --> 11:49:21,436
so
17847
11:49:21,436 --> 11:49:23,840
let's see here in our example right for
17848
11:49:23,840 --> 11:49:26,320
example when we use the plus operator
17849
11:49:26,320 --> 11:49:28,880
the magic method underscore underscore
17850
11:49:28,880 --> 11:49:30,640
add underscore underscore is
17851
11:49:30,640 --> 11:49:32,560
automatically invoked
17852
11:49:32,560 --> 11:49:34,720
in which the operation for the plus
17853
11:49:34,720 --> 11:49:36,640
operator is defined
17854
11:49:36,640 --> 11:49:39,116
now we are overloading it and in the
17855
11:49:39,116 --> 11:49:40,960
example you can see
17856
11:49:40,960 --> 11:49:42,640
we are adding
17857
11:49:42,640 --> 11:49:43,596
the
17858
11:49:43,596 --> 11:49:47,520
first score of each batchman right so we
17859
11:49:47,520 --> 11:49:51,040
have customized the addition function
17860
11:49:51,040 --> 11:49:53,756
we are adding the first score of the
17861
11:49:53,756 --> 11:49:55,916
first baseman with the first score of
17862
11:49:55,916 --> 11:49:57,756
the second batchman and then we are
17863
11:49:57,756 --> 11:50:00,640
printing the total so in our case we are
17864
11:50:00,640 --> 11:50:01,596
adding
17865
11:50:01,596 --> 11:50:05,840
and printing the first one 66 plus 23
17866
11:50:05,840 --> 11:50:09,116
and if you print b3 dot s2 you will see
17867
11:50:09,116 --> 11:50:11,840
the sum total of 78 plus 59
17868
11:50:11,840 --> 11:50:14,000
so the thing to be noted is this
17869
11:50:14,000 --> 11:50:16,640
function add function where you are
17870
11:50:16,640 --> 11:50:20,000
adding self dot s1 plus second dot s1
17871
11:50:20,000 --> 11:50:22,000
and you are adding s self dot s2 plus
17872
11:50:22,000 --> 11:50:24,480
second dot s2 and that is the whole
17873
11:50:24,480 --> 11:50:26,640
customization what you have done in
17874
11:50:26,640 --> 11:50:29,200
order to add two objects of patch
17875
11:50:29,200 --> 11:50:31,436
now lets take a look at some of the
17876
11:50:31,436 --> 11:50:33,596
examples of operator overloading we have
17877
11:50:33,596 --> 11:50:35,040
just discussed
17878
11:50:35,040 --> 11:50:37,520
so we will be executing some examples in
17879
11:50:37,520 --> 11:50:40,320
our jupiter notebook
17880
11:50:40,320 --> 11:50:41,436
now
17881
11:50:41,436 --> 11:50:43,040
what i am going to show you
17882
11:50:43,040 --> 11:50:44,000
is about
17883
11:50:44,000 --> 11:50:47,116
class batsman which we discussed in the
17884
11:50:47,116 --> 11:50:48,800
presentation
17885
11:50:48,800 --> 11:50:51,040
and i'm going to define the init
17886
11:50:51,040 --> 11:50:53,436
function
17887
11:50:54,400 --> 11:50:55,360
and
17888
11:50:55,360 --> 11:50:56,880
initialize
17889
11:50:56,880 --> 11:51:01,840
score 1 and score 2 of the attachment
17890
11:51:02,880 --> 11:51:03,840
now
17891
11:51:03,840 --> 11:51:05,596
i am going to override
17892
11:51:05,596 --> 11:51:09,436
the magic function add
17893
11:51:09,436 --> 11:51:10,640
and
17894
11:51:10,640 --> 11:51:13,916
add the score 1 and score 2 of the
17895
11:51:13,916 --> 11:51:17,200
batsman separately
17896
11:51:18,240 --> 11:51:20,240
now with this over
17897
11:51:20,240 --> 11:51:22,160
operator overloading
17898
11:51:22,160 --> 11:51:23,756
when i will pass
17899
11:51:23,756 --> 11:51:25,840
object for addition
17900
11:51:25,840 --> 11:51:29,436
then only now my object will understand
17901
11:51:29,436 --> 11:51:31,916
that it has to add
17902
11:51:31,916 --> 11:51:33,680
score one
17903
11:51:33,680 --> 11:51:35,916
of first batsman with the score one of
17904
11:51:35,916 --> 11:51:37,596
second batsman
17905
11:51:37,596 --> 11:51:39,840
and score two of first batsmen with the
17906
11:51:39,840 --> 11:51:41,596
source code two of the second batsman so
17907
11:51:41,596 --> 11:51:45,000
we will see how
17908
11:51:56,960 --> 11:51:58,560
so what we are doing is
17909
11:51:58,560 --> 11:52:00,560
one in the add
17910
11:52:00,560 --> 11:52:01,916
operator
17911
11:52:01,916 --> 11:52:04,400
we are adding these codes
17912
11:52:04,400 --> 11:52:06,800
encapsulating in a new object batchman
17913
11:52:06,800 --> 11:52:08,240
and then returning the
17914
11:52:08,240 --> 11:52:12,080
object b3 now let's create
17915
11:52:12,080 --> 11:52:14,960
object b1
17916
11:52:15,916 --> 11:52:20,400
batchman1 and batchman2
17917
11:52:22,960 --> 11:52:24,720
now i am
17918
11:52:24,720 --> 11:52:26,800
adding two objects batsman one and
17919
11:52:26,800 --> 11:52:29,596
batchman two
17920
11:52:31,200 --> 11:52:33,200
now without operator overloading if
17921
11:52:33,200 --> 11:52:35,200
would i would not have done operator
17922
11:52:35,200 --> 11:52:37,436
overloading of add then this would have
17923
11:52:37,436 --> 11:52:39,756
failed because plus operator does not
17924
11:52:39,756 --> 11:52:42,560
understand the object's batsman but with
17925
11:52:42,560 --> 11:52:45,200
overloading done
17926
11:52:45,200 --> 11:52:49,116
now i can independently print the score
17927
11:52:49,116 --> 11:52:50,080
of
17928
11:52:50,080 --> 11:52:52,880
the new batsman the batsman's the b3
17929
11:52:52,880 --> 11:52:54,080
object
17930
11:52:54,080 --> 11:52:57,276
some of score one of batsman one and
17931
11:52:57,276 --> 11:52:59,840
uh score one of batsman two which is 66
17932
11:52:59,840 --> 11:53:03,000
plus 23.
17933
11:53:06,160 --> 11:53:07,116
okay
17934
11:53:07,116 --> 11:53:10,000
so now let's take an example of a simple
17935
11:53:10,000 --> 11:53:11,756
operator function
17936
11:53:11,756 --> 11:53:14,720
like for example i'm gonna
17937
11:53:14,720 --> 11:53:19,200
simply create a variable a with value 44
17938
11:53:19,200 --> 11:53:22,640
and variable b with 7
17939
11:53:22,640 --> 11:53:23,596
and
17940
11:53:23,596 --> 11:53:26,560
print a plus b and if i run this
17941
11:53:26,560 --> 11:53:28,800
this is the expected output
17942
11:53:28,800 --> 11:53:32,000
python plus operator knows that a and b
17943
11:53:32,000 --> 11:53:32,880
are
17944
11:53:32,880 --> 11:53:35,200
primitive int variables so it's able to
17945
11:53:35,200 --> 11:53:37,680
add that
17946
11:53:37,916 --> 11:53:40,960
now behind the scenes when we printed a
17947
11:53:40,960 --> 11:53:42,320
plus b
17948
11:53:42,320 --> 11:53:44,720
actually this is what happened
17949
11:53:44,720 --> 11:53:45,840
in
17950
11:53:45,840 --> 11:53:49,116
primitive in variables add operator
17951
11:53:49,116 --> 11:53:51,360
overloaded method got called
17952
11:53:51,360 --> 11:53:54,560
and a and b got passed and 51 was
17953
11:53:54,560 --> 11:53:55,840
calculated
17954
11:53:55,840 --> 11:53:58,000
so it's automatically done because this
17955
11:53:58,000 --> 11:53:59,916
function is implicitly written in the
17956
11:53:59,916 --> 11:54:03,320
python language
17957
11:54:05,436 --> 11:54:07,436
similarly
17958
11:54:07,436 --> 11:54:10,320
i can also do this thing i can create a
17959
11:54:10,320 --> 11:54:11,840
int variable
17960
11:54:11,840 --> 11:54:14,080
and a float variable
17961
11:54:14,080 --> 11:54:17,840
and if i try to print it
17962
11:54:19,840 --> 11:54:21,840
i will get the expected output which is
17963
11:54:21,840 --> 11:54:25,436
50.5 because the plus operator accepts
17964
11:54:25,436 --> 11:54:28,000
the end and a float variable it's
17965
11:54:28,000 --> 11:54:30,000
implicitly written so that is also
17966
11:54:30,000 --> 11:54:31,276
allowed
17967
11:54:31,276 --> 11:54:34,320
now let's take another example of
17968
11:54:34,320 --> 11:54:36,320
string operators
17969
11:54:36,320 --> 11:54:38,960
string variables
17970
11:54:38,960 --> 11:54:41,436
addition
17971
11:54:42,320 --> 11:54:44,640
now irrespective of my
17972
11:54:44,640 --> 11:54:46,160
i'm giving numerical values in the
17973
11:54:46,160 --> 11:54:49,040
string print a plus b
17974
11:54:49,040 --> 11:54:50,240
in string
17975
11:54:50,240 --> 11:54:53,116
is is meant to do concatenation
17976
11:54:53,116 --> 11:54:53,880
so
17977
11:54:53,880 --> 11:54:57,200
446.5 it's a simple string concatenation
17978
11:54:57,200 --> 11:54:59,916
what python has
1088257
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.