0:00
Hey friends, welcome back to another
0:02
Python beginner series video. Well, this
0:04
Sai Raja Lakmi Singum and today's
0:07
program looks super simple, but I
0:09
promise you it's going to teach you a
0:11
very powerful concept. We are going to
0:14
take a list of numbers and if the first
0:16
number is negative, we'll replace it
0:18
with zero. But here is the cool part.
0:21
I'll first show you how to do it
0:23
manually and then we'll understand the
0:26
importance of something called map in
0:29
Python. And yes, we'll solve this with
0:32
both for loop and while loop so you get
0:38
So step one is to take the numbers from
0:40
the user and if the first number is
0:42
negative replace it with zero and step
0:45
three you will print the modified list.
0:47
Sounds easy right? But the way we write
0:50
it matters a lot. Let's first do it
0:53
without map. Okay. First let's take the
1:21
and you'll do dotsplit.
1:28
And if I enter -5 10 20 as the input. So
1:33
that gives me -5 10 20 in string format.
1:38
Let's check by just printing my list
1:52
10 20. So these are not numbers. These
1:56
are actually strings. So we need to
1:59
convert each element to numbers. So what
2:04
I do for that we use for loop. Okay.
2:14
len of my list. Guys we have done it
2:17
multiple times. I think now you know why
2:19
are we using this range method and why
2:21
we need the alien to get the actual size
2:24
of the list. And in that according to
2:26
that we are going to change and my list
2:44
let's run it and check again. But before
2:47
that let's print my list.
3:00
See now it changed to numbers. Okay. Now
3:05
what we have to do? We have to check
3:07
whether the first element in the list is
3:10
negative or not. If it's negative then
3:13
we have to replace it with zero. Okay.
3:15
So again for that we are going to do for
3:32
So if the first element i =0 and
3:50
then you will make it
3:59
then you will just come out of the loop
4:04
okay so what's happening here for i in
4:07
range of ln of my list
4:11
okay what is ln of my list for exam
4:14
Example here you gave
4:18
- y 10 20 as input. Okay. So because of
4:24
this it converted to integers. Let's say
4:26
let's assume that these are numbers not
4:29
the strings. So for i in range of ln of
4:33
my list. What is this ln of my list? The
4:36
number of items present in the list. So
4:39
the number of items present in the list
4:41
is three. So ln of my list is three. So
4:46
I in range of three. That means I starts
4:50
from zero and runs until I becomes
4:55
So initially what is the value of i?
5:01
So if I =0 and my list of i less than
5:06
zero that means the first position is
5:11
you're checking whether is that a first
5:13
position and at the same time that first
5:16
position is less than zero or not. So i
5:19
equal to0 and my list of zero is nothing
5:22
but we are accessing the first element
5:24
in the list. So which is this one and we
5:27
are checking whether it is less than
5:29
zero because we have to check whether
5:31
the number is negative or not. So how
5:34
you check whether a number is negative
5:36
or not when it is less than zero
5:38
obviously we will say that number is a
5:40
negative. So my list of i less than
5:43
zero. So if these two conditions are
5:46
true then only this statement will get
5:50
executed. So then the first element will
5:57
and then you will immediately come out
5:59
of the loop because you don't have to do
6:01
any other modifications in the list. You
6:03
don't have to change any other elements.
6:05
You just need the first element and you
6:07
just have to access it and you check
6:09
whether it is less than zero or not and
6:11
then you will print according to the
6:13
result by doing it to zero. So this is
6:20
So you will just print that
6:34
is something like this. And when you run
6:52
the modified list is 0 10 20. So this is
6:57
But notice how we had to manually
7:00
convert each and every element. This
7:03
thing that's a bit long, right?
7:07
So here is where Python makes life
7:10
easier. Instead of converting one by
7:12
one, we can just say my list of
7:28
And we need this for list. So just keep
7:37
So list of map of int comma input. You
7:40
don't have to write this logic.
7:43
You can do it just like this. You're
7:46
saying that hey python take every item
7:48
in the list and turn it into an integer
7:50
for me. And it does that in one line.
7:53
One single line. You don't have to write
7:55
for loop. You don't have to like you
7:57
don't have to write multiple lines of
7:59
code. Now it looks so clean, short and
8:02
professional hooking code,
8:19
See no change at all. So that is the
8:22
importance of map. The reason I didn't
8:25
say is because first you guys must
8:28
understand the syntax of map. So that is
8:31
why in previous video I didn't explain
8:34
but this is all the actual importance of
8:36
map. So it will actually convert each
8:38
and every element in the list to
8:41
integer. So that is why we use map.
8:46
Okay. Now it's time to work with the
8:48
while loop. As usual, we'll take the
8:51
numbers and we'll use map here. And then
8:55
next, we check the first element in a
8:57
loop. Always remember in while loop, you
9:00
have to initialize it to zero. So I'm
9:03
initializing it to zero. And you will do
9:10
I less than len of my list everything is
9:16
same. Then you will come out of the
9:17
loop. Here I'm not incrementing I by one
9:21
because I just need the first value. So
9:25
it will just come out of the loop. So
9:26
this is what happening. So let's run it.
9:41
That's it. Everything is same. Same
9:44
result just a different way of looping
9:45
through the list. This helps you
9:47
understand how both for and while loops
9:50
can solve the same problem. And here is
9:52
something super important to remember is
9:55
in our earlier exercises I didn't use
9:58
map. Why? Because I wanted you to first
10:01
understand the basics of lists and
10:03
loops. And now that you are comfortable
10:06
and I wanted you to see the beauty of
10:08
map so it makes our code more shorter
10:11
and cleaner and even easier to read. So
10:15
there you go my friends. We learned how
10:17
to replace the first element of list if
10:20
it's negative using both for and while
10:23
loops. Also remember programming is like
10:26
learning to ride a bicycle. At first you
10:28
take the longer path step by step and
10:32
then one day you just learn the
10:34
shortcuts that make the ride smoother.
10:37
If this video made coding feel simple
10:39
and clear, do me a tiny favor. Hit that
10:42
subscribe button because here we are not
10:44
just writing Python programs. We are
10:46
building your confidence one small step
10:51
Keep practicing, keep coding, and I'll
10:53
see you in the next lesson.