0:01
Hey friends, welcome back to another
0:02
Python beginner series video. This I
0:05
Raja Lakmi Singum and today we are
0:07
solving a really interesting problem
0:09
that every beginner should know which is
0:11
how to find the largest and smallest
0:13
number in a list without using Python's
0:16
built-in max and min functions. We have
0:20
done this exercise before if you follow
0:22
this entire course step by step. But
0:25
today we are going to use for and while
0:30
Yes, we are going to do it in the manual
0:32
way. And trust me, you'll love this
0:34
because it really helps you understand
0:36
how Python works behind the scenes.
0:40
So the goal is simple. You enter a list
0:42
of numbers and our program will tell you
0:45
which one is the biggest and which one
0:47
is the smallest. And the fun part we'll
0:49
do it using both for loop and while
0:52
loop. So let's go step by step.
0:55
First let's grab some numbers from the
1:01
grab input from user.
1:09
equal to I'm going to get input
1:20
separated with commas or by commas.
1:27
Okay. And we are going to split them
1:30
separated with commas. But here if you
1:35
observe the previous exercises we didn't
1:38
need the input to convert it into
1:40
integer or any other data type because
1:43
there we didn't perform any mathematical
1:45
operation but coming to finding the
1:47
maximum and minimum numbers it is a kind
1:49
of mathematical operations right so for
1:51
that we have to convert it to integer so
1:56
what I'm going to do is I'm going to
2:01
to store the numbers like members is
2:03
nothing but it is going to store the
2:05
input in the form of list. So
2:10
I'm going to do list and inside that I'm
2:14
going to use a map method and inside
2:16
that I'm going to give the data type to
2:18
int and then integer.
2:23
So what's happening here? We are asking
2:25
the user to enter the numbers. It could
2:32
will break the string into a list. And
2:36
what this map will do, it will convert
2:39
each piece into an integer. Okay. So it
2:44
map is nothing but your mapping. It
2:46
means like you're going to that
2:47
particular element and checking the data
2:49
type. Obviously it will be string the
2:52
default data type. So it will convert
2:54
each and every element like for example
3:06
So initially these will be in obviously
3:12
So each and everything is in string.
3:15
What this map will do first it goes to
3:17
10. It will check the data type. It is
3:19
string. So we have to convert it to
3:21
integer because we gave integer here. So
3:24
that is why it is converting it into
3:27
So finally the list will create a proper
3:33
That's it. Actually we can do each and
3:36
everything step by step but we are doing
3:38
everything in a single line where we are
3:40
getting the input and separating it by
3:42
comma and converting the each and every
3:45
inputs data type to integer using the
3:47
map and then we are using list. That's
3:54
Now let's assume the first number in the
3:57
list is both the largest and the
3:59
smallest number and we'll update them as
4:02
we go. So let's create
4:07
How do we access the first number in the
4:10
list? Obviously by using index. So
4:13
numbers of zero. And the smallest also
4:16
will be numbers of zero.
4:27
Okay. Now the loop. So for num in
4:37
if num the number greater than the
4:43
then largest equal to num.
4:56
then smallest will be n.
5:02
That's it. It's as simple as that. So
5:04
what we are doing here? We check each
5:06
number one by one. If it's bigger than
5:09
the largest number, then we update the
5:11
largest. If it is smaller than our
5:14
current smallest, then we update the
5:16
smallest. And finally we print the
5:47
smallest. And that's it.
5:52
And now you know how to find the biggest
5:54
and smallest numbers manually.
5:57
So this is how we do. Okay. Let's trace
6:00
this logic. This is the most interesting
6:02
part. This is where we learn everything.
6:05
So let's give the input
6:14
Let's say this line converted this 10
6:17
257 from string of set of input to a
6:21
list of integer values. Okay. So this is
6:25
the entire purpose of this single line.
6:28
And now we created two variables. One is
6:31
largest and the other is smallest. And
6:35
number 10 which is the first element the
6:38
numbers of zero is nothing but the first
6:40
element in the list is nothing but 10.
6:43
We are imagining assuming saying that
6:48
largest is 10 and smallest is also 10.
6:51
Okay. Now what this for loop will do it.
6:54
So we instantly created a variable
6:56
called num. It will store the value
7:00
every time. it will change the value for
7:03
each and every loop. So for n in numbers
7:08
num value is 10 because that's the first
7:11
element. So the pointer starts from
7:14
here. So this is the num.
7:22
If 10 greater than largest
7:25
is 10 greater than 10? No. So false. And
7:31
now it will check is 10 less than 10.
7:34
Again it is false. So now the program
7:37
jumps to second element. Num jumps to
7:40
second element. What is the second
7:47
Is 20 greater than 10? Yes. So this is
7:53
true. So now the program will change the
7:56
largest value from 10 to 20 in this
8:04
uh num less than smallest. So is 20 less
8:08
than 10 is false. So again now the
8:12
program jumps to the next value. So what
8:15
is this next value is five. And what is
8:18
the largest value now? It changed from
8:24
Largest value changed from 10 to 20. Now
8:27
the third value is five. Is 5 greater
8:30
than 20? False. Is five less than what
8:34
is the smallest value? 10. Is five less
8:37
than 10? True. So now the program
8:42
changes the smallest value to five.
8:47
Okay. So now the value changed to five.
8:53
And now the program goes here. And now
8:56
the num value changes from 5 to 7.
9:01
Num 7 greater than 20? The largest value
9:06
is 20. 7 greater than 20?
9:10
False. Okay. Is 7 less than five?
9:15
False. So now again it jumps to the next
9:19
item. So now it comes out of the loop
9:23
and now the largest value is 20 and the
9:26
smallest value is five.
9:46
number 20 and smallest number five. So
9:48
this is what happens. Okay,
9:52
I hope I'm clear in this exercise. If
9:54
you still have any doubts, please let me
9:56
know. I'm going to do one more video in
9:59
detail if you want. But this is not the
10:02
end. We also have to work on this
10:04
exercise by dealing with the while loop.
10:07
So let's try the same thing but this
10:09
time with the while loop. So as usual,
10:12
we are getting numbers and we are doing
10:14
the smallest, the largest and the
10:18
Okay. And always when it comes to while
10:21
loop, you have to initialize the pointer
10:26
and depends on the condition situation.
10:33
I less than len of numbers.
10:41
I already explained why we are dealing
10:43
with a numbers whenever we are running
10:46
the loop especially when we are working
10:48
with the list. If you want please check
10:49
the previous exercise. I insist you to
10:52
go through each and every exercise to
10:54
understand every minute concept.
10:58
So the same logic I'm just going to
11:01
remove the for loop and keeping the
11:03
while loop. But here we are going to do
11:07
numbers of I. Again here numbers of I
11:15
and this will be the numbers of I
11:22
numbers of I and that's it.
11:29
But the only difference is I = 0. it
11:34
starts with zero. In for loop, it
11:36
usually starts with zero. But while we
11:39
have to tell it to start with zero and
11:40
that's why the i value is zero and alen
11:44
of numbers is nothing but the quantity
11:46
of elements present in the list which is
11:48
1 2 3 and four. So the alien of numbers
11:51
is four. So initially I value is zero
12:02
four in this case. Okay.
12:06
And I value is zero.
12:09
Okay. If numbers of I so numbers of zero
12:14
which is 10 same is greater than
12:19
so the initially the largest value
12:22
number is 10 and the smallest is also 10
12:26
same thing so now the largest is 10
12:31
greater than 10 false is 10 less than 10
12:35
false so now the i value jumps from 0 to
12:39
1 so we have to give an operation called
12:42
I + = 1 which is same as
12:49
I = I + 1. If we don't do then we are
12:53
going to be in that infinite loop. So we
12:56
have to break that condition by
12:58
incrementing that means by increasing
13:00
the value of I by one for every time. So
13:03
now the I value becomes one. Okay. Aln
13:07
of numbers is four. Is one less than
13:09
four? Yes. Is numbers of one? What is
13:13
numbers of one? 20. Is 20 greater than
13:17
10? True. So, the largest will be
13:19
updated to 20. And is 20 less than 10?
13:25
No. False. Now, the i value increments
13:28
by two. So, this will be two.
13:32
Is two less than four? True. Is numbers
13:36
of two? What is numbers of two? Five. Is
13:42
greater than 20? No. Is five less than
13:45
10? Yes. So the smallest value will be
13:49
updated to five. And again now I value
13:55
Is three less than four? True. Is
13:58
numbers of three? What is numbers of
14:00
three? 0 1 2 3 7. Is 7 greater than 20?
14:05
No. Is 7 less than five? No. So now
14:10
again I value becomes four. Is four less
14:13
than four? No. Then it will come out of
14:16
the loop and then it will print the
14:18
largest and the smallest numbers.
14:41
So the largest number is 20 and the
14:43
smallest number is five.
14:47
So instead of looping automatically like
14:50
for we are manually moving through list
14:52
by using this index I. So each time we
14:56
check we update and move I by one so
15:01
that it can move one step forward. Same
15:04
result but different way of thinking. So
15:07
there you go my friends, we actually
15:11
learned two ways to find the largest and
15:14
smallest number without using shortcuts.
15:17
And here is the thing. Programming is
15:19
not about memorizing the fancy
15:21
functions. It's about understanding how
15:24
things actually work step by step.
15:28
So we have done we have worked with for
15:34
and if you found this video helpful do
15:36
me a small favor hit that subscribe
15:38
button because here we are learning
15:40
Python the simple and beginner friendly
15:42
way one program at a time and trust me
15:45
the more we practice together the
15:47
stronger your coding skills will become.
15:49
All right I'll see you in the next
15:50
program. Until then, keep practicing and