0:00
Hey everyone, welcome back to our Python
0:02
basics series. Well, this Raja Lakmi
0:04
Singam and today's program is super
0:06
simple but very useful. We are going to
0:09
check if a specific number is present
0:11
inside a list or not. We have already
0:14
done this kind of exercise before, but
0:16
today we are going to do it using for
0:19
loop and while loop. So here's the idea.
0:21
Imagine you have a shopping list or
0:23
maybe a list of numbers and you want to
0:25
quickly check if something is there or
0:28
not. for example, is the number five in
0:33
That's exactly what we are building
0:37
All right, let's start with the first
0:39
version using a formula. Don't worry if
0:42
you're a beginner. I'll explain you
0:44
every single line. So, first, as usual,
1:25
And we are going to split that using the
1:52
Okay. So number equal to input
2:14
Now let's create a variable called
2:16
present. So this variable will say that
2:20
whether the element is present inside
2:22
the list or not. So we'll keep that
2:24
initially false. Let's imagine that the
2:27
number is not present in the list. Okay.
2:30
Now let's work on the for loop. So for
2:48
then set the present to true.
2:53
And then you can come out of the loop
2:55
because you don't have to check the
2:56
elements anymore because you got the
2:58
element that you're searching for.
3:01
And you can just print
3:28
Then you will do that if present is
3:31
true. Else you will just print
3:57
So here what we are doing first we take
4:01
the list of numbers from the user and
4:04
notice that we are using dotsplit
4:06
method. So this splits input wherever
4:09
there is a comma and then we ask the
4:13
user for a number to check. Next we
4:17
create a variable called present to set
4:20
it to false. This is uh just like a
4:23
little flag saying that I haven't found
4:28
Okay, now comes the fun part, the for
4:31
loop. It goes through each item in the
4:36
and if the item matches our number
4:40
then we set the present to true and stop
4:43
searching using break and finally we
4:47
print whether the number present or not.
4:51
Okay, let's imagine that user gave a set
5:00
121 just any random numbers and the user
5:06
want to check the number
5:10
121. Okay, initially the present was
5:14
false. So now we created for loop saying
5:19
that for item in my list. I know you
5:22
guys already have learned the for loop
5:25
syntax how the for exactly works. So you
5:29
can keep I just created item here. You
5:32
can keep either for x in my list, for
5:34
underscore in my list, for y in my list,
5:37
whatever it is. You created a variable
5:39
called item to store the element inside
5:44
the element of the list inside the item
5:47
for a while. Okay. Now for item in my
5:51
list, so the item value starts from here
5:57
So initially what is the value of item
6:01
So item value is 100. It's in the
6:07
Okay. If item equal to number is 100
6:14
No. Right. It's false. So this entire
6:18
instruction is skipped. Okay. Now item
6:22
jumps from 100 to 108. So now the value
6:28
So is 108 equal to 121?
6:32
No. These entire instructions are
6:35
skipped again. Okay. Now item jumps from
6:41
121. Listen to me. If 121 equal to 121
6:48
is this condition is true now. Yes, it
6:51
is true. So now the value of present
6:54
changes from false to true.
6:57
Okay. So now here the present value
6:59
changed to true and then it will come
7:03
out of the loop. Okay.
7:06
Now what is the value of present?
7:10
So the value of present is true. So then
7:14
which line will get executed? Either
7:16
this line or this line. Which line? The
7:21
left side one or the right side one. I'm
7:23
asking you questions because assuming
7:24
that you guys have already completed the
7:27
previous exercises with me and in all
7:29
the exercises if you observe we are
7:31
following the same pattern. We are
7:33
getting the user performing the logic
7:34
using for loop and while loop and then
7:36
we are presenting it using the print
7:39
loop in a single line using this if else
7:42
instead of writing four lines. So that
7:44
is why I'm asking. So if present is true
7:47
then obviously this line is executed. So
7:52
the number 121 is on the list. So this
7:55
is what we are expecting. Okay, let's
8:05
So I'm giving numbers 100 or 8
8:11
121 as we are assuming. Enter. Enter the
8:15
number to check if it is on the list.
8:17
121. I want to check. Enter. number 121
8:21
is on the list. So this is what we are
8:23
expecting and this is what we got. So
8:25
far so good. No errors. Okay. What if we
8:29
want to check the number which is not in
8:31
the list. What happens when you trace
8:33
something like this? So you gave the
8:35
same input but you gave the number which
8:38
is not in the list. You know we are the
8:40
human brains. We understand we can see
8:42
the things we can compare it instantly
8:44
and we can say that number 120 is not in
8:47
the list. But computer is not like us
8:49
right. We have to guide the computer
8:51
each and every step even the next
8:54
instant step. So what happens initially
8:58
the present will be false. So for item
9:01
in my list again now what is the value
9:03
of item is 100. Okay. If item equal to
9:08
number is item equal to number? No. So
9:13
then this entire thing will be skipped
9:16
and now the item value becomes 108. So
9:19
now it becomes 108 and now it will check
9:21
is 108 equal to 120. No again this will
9:26
be skipped and now the item value is
9:30
Okay. Is 121 equal to 120? No. Then
9:35
again this entire instructions are
9:37
skipped and now it will come out of the
9:40
loop because we came to an end. Now what
9:42
is the value of present?
9:45
So the value of present is false because
9:48
it didn't run this line because this
9:52
condition is not true.
9:55
So that's the reason. So if the present
9:58
is false then which side of the
10:00
instruction will get executed the left
10:05
Obviously the right side. The number 120
10:09
is not on the list. Okay, let's run it.
10:28
The number 120 is not on the list. So
10:31
this is how the program runs.
10:35
Okay. Now let's try the same thing but
10:38
with the while loop.
10:40
So always when you are using while loop
10:43
we have to initialize it by giving the
10:46
starting value. So I'm giving I equal to
10:53
And we gave the present value to false.
10:56
And now we will write while
11:00
I less than up to what extent you want
11:05
to run this loop until the entire loop
11:08
ends. So how you will check whether the
11:11
entire list when the entire list ends
11:14
when the size the number of items when
11:16
it covers each and every item then it
11:19
comes out of the loop right so how many
11:22
times it will run obviously the number
11:24
of times the element present in the list
11:27
how many times the numbers are present
11:29
in the list three times because we have
11:32
three elements so the size of the list
11:34
is three in this case it could be
11:38
If the number of elements in the list
11:40
are four then the size is four then the
11:43
ln will be four. So it depends on the
11:46
number of elements. So while I less than
11:54
So then you are going to do the same
11:57
thing. The logic is same but here we
12:00
will do my list of i.
12:08
Okay. And then everything is same. But
12:11
here we will increment I value by 1.
12:20
But instead of using like this, I will
12:23
write it in a different way which is I
12:30
which is same as I = I + 1.
12:45
So everything is same. All you did is
12:47
you change the value. You changed the
12:50
loop from 4 to while. Okay. Let's run it
12:52
again. Let's trace it. So here we have
12:56
values 100, 108, 121. And for example,
12:59
we give the value as 121. And initially
13:02
the i value is zero and the present is
13:05
false. So while i less than what is the
13:08
alien of my list is nothing but the
13:11
number of times the elements present in
13:13
the list which according to this case we
13:15
have three elements. So the length is
13:18
three. So is i less than three? True. So
13:23
now the loop jumps here. If my list of
13:26
i. What is i? It is in the zeroth
13:29
position. So what is my list of zero is
13:32
nothing but 100 0 1 2 my list of zero my
13:36
list of one my list of two
13:40
so my list of zero is 100 so is 100
13:46
false this this entire thing will be
13:50
skipped and now the i value becomes 1
13:56
initially i value is zero now the i
13:58
value becomes one Is my list of 1 equal
14:02
to 121? So what is my list of one here?
14:06
Now it's 1 8. Is 1 8 equal to 121? No.
14:12
False. Again this will be skipped. And
14:14
now I value becomes 2. Now listen
14:17
carefully. I value becomes 2. Is I less
14:21
than 2 less than three? True. If my list
14:27
of two is nothing but 121 equal to
14:31
number which is 121 are they equal?
14:35
True. Now the value of present changes
14:37
to true and then it will come out of the
14:40
loop. It will immediately come out of
14:42
the loop. It will not even run this
14:43
instruction. It will just come out of
14:46
the loop. And now what is the value of
14:48
present? It is true. So what will be the
14:51
output? Now the number 121 is on the
15:10
See the number 121 is on the list.
15:15
Okay. What if you give 120 or any other
15:18
number any random number or 11 okay
15:23
initial i value zero same thing it will
15:25
repeat the same thing I feel like I'm
15:28
bragging this so what happens now
15:32
this instructions will never run because
15:34
the 11 is not on the list so this
15:36
condition is always will be false so
15:39
once this condition is false then the
15:42
present is initially false so it will be
15:44
false forever because 11 is not there in
16:04
the number 11 is not on the list. So
16:07
this is how the while also exactly runs.
16:12
So what's happening here? Instead of for
16:14
we are using a counter I which is
16:16
starting at zero. So the condition while
16:20
I less than alen of my list keeps the
16:22
loop running until we check all the
16:27
and inside we compare the item at
16:29
position i with our number. If we find
16:32
it, we will set the present to true and
16:35
then we will break out. And also don't
16:38
forget to increment increase the value
16:40
by one for every loop. Otherwise, we
16:43
will get stuck in an infinite loop.
16:46
It'll be keep on running. And finally,
16:49
we print the result just like before we
16:52
did for loop. So there you go. We have
16:55
two simple ways to check if a number
16:58
exist inside a list or not. one with a
17:01
for loop and the other with while loop.
17:04
This is super useful for beginners
17:05
because it teaches you how to loop
17:08
through list and how to use flags like
17:11
present and how to use conditions inside
17:15
the loops. So if you want to keep
17:17
learning without getting confused, hit
17:19
that subscribe button and also tap the
17:21
like button if this helped you and don't
17:24
forget to share it with a friend who is
17:26
also learning Python. Let's grow
17:28
together one line of code at a time. See
17:32
you in the next video.