0:00
Hey friends, welcome back to our Python
0:02
for beginners series. Well, this s Raja
0:05
Lakmi Singum and today's program is a
0:07
short one, but it's packed with an
0:10
important lesson and we are going to
0:13
learn how to replace the first element
0:15
of a list, but only if it's negative. It
0:20
sounds easy, right? But here is the cool
0:22
part. This tiny idea will teach you how
0:25
to check conditions and update lists at
0:29
the same time. So just imagine the user
0:32
will enter a list of numbers and we'll
0:35
check if the list is not empty and then
0:38
if the first element is negative, we'll
0:40
replace it with a zero and finally we
0:43
will print the modified list.
0:46
Okay, let's start coding together. So
1:25
dot split and you'll use comma to split.
1:49
So if my list so if it's not empty if
1:57
here is the step three.
2:05
Check if the first element is negative
2:15
So if to access the first element we
2:19
have index concept we have already
2:21
learned how to access the elements in
2:22
this list using the index concept. So
2:26
I'm doing the same thing. So if in if if
2:36
of zero is nothing but you're accessing
2:38
the first element and it's less than
2:45
but if you observe I didn't use any
2:47
integers here right I just use the input
2:51
that means the input that I'm getting
2:53
from the user will be in strings so
2:57
it'll be something like like this.
3:14
something like this but I want it to be
3:17
converted to integer so I will add int
3:22
So you can use int anywhere. So I'm
3:24
using it here to convert the first
3:26
element to this integer from string. So
3:31
then what you will do if the number is
3:33
less than zero that means if the number
3:35
is negative then you will just simply
3:43
And that's it. It's as simple as that.
3:46
But always remember that you that the
3:49
list is actually a string list. So I
3:51
just want you to replace it with a
3:53
string to zero. That's it. And then you
3:55
don't have to write else. I know every
3:58
if part should have else. I even I told
4:01
it in the previous videos. But if you
4:04
really don't need else part, then you
4:09
So then you can just print
4:26
And yeah, let's check. So like for
4:28
example, when we run this, enter the
4:32
numbers separated by commas. So I'm
4:39
So it is actually changing the minus one
4:46
So what's happening here? First we ask
4:49
the user to enter the number separated
4:51
by commas. So like for example if they
4:54
give any - 5, 10 or 20 then after
4:58
splitting it to like this something like
5:11
Next we check if the list is empty or
5:13
not. For that you just do simply my
5:15
list. If there is something then it will
5:17
be true. If the list is empty then it
5:19
will be false. So if it is true then it
5:22
will go here and then it will check
5:24
whether the first element is negative or
5:27
not. So we look at the first element
5:29
which is my list of zero we consider it
5:31
as first element and we'll check how we
5:34
check by doing it less than zero. So if
5:38
it is less than zero then we consider it
5:40
as negative. So but in order to check we
5:43
also have to convert this string to an
5:45
integer. So that's why I'm using int
5:47
here. So now the first it will take
5:50
minus5 which is in string and then it'll
5:52
be converted to integer minus 5 and then
5:54
it will check whether it is less than
5:56
zero or not. If it is less than zero
5:58
then it will simply change the first
6:03
So finally we print the modified list.
6:10
Now let's try again with the list that
6:12
doesn't start with negative
6:25
30. The modified list is 15 25 30.
6:30
Nothing changed because the first number
6:32
was already positive. So it will come
6:35
here. it will check whether the list is
6:38
there or not. If the list is there then
6:40
it will check whether the number is less
6:42
than zero or not. So if the number is if
6:46
this condition is false that means if
6:48
the number is greater than zero then
6:50
simply it will skip this else part. It
6:52
will come out of it and then it will
6:58
And there you go. You just learned how
7:00
to check the first element of a list and
7:02
update it if needed. This may look
7:05
small, but trust me, you will use this
7:07
kind of logic again and again in bigger
7:08
projects. All right, before we wrap up,
7:11
let me ask you something. If this video
7:13
helped you learn even a little bit,
7:16
isn't that worth a tiny thank you? And
7:19
the best way you can thank me is by
7:21
hitting the subscribe button. Think of
7:23
it like adding yourself to the Python
7:25
learners list, except this time there's
7:28
no negative numbers, only positive
7:30
vibes. So go ahead, subscribe, ring the
7:32
bell icon, and let's keep learning
7:34
Python together. Until next time, keep
7:37
coding and keep smiling. See you soon.