Python already solved this#PythonTips #DontReinventTheWheel #CleanCode
Show More Show Less View Video Transcript
0:00
Now, if you observe this code, we have a
0:01
list of values and we are storing in the
0:05
nums, right?
0:06
So, all these values are stored in nums.
0:09
And here, for example, if we want to get
0:12
the index value, as usual in list, index
0:15
starts from zero. So, we humans know
0:18
that the index of eight is nothing but
0:20
it starts with zero, one,
0:23
two,
0:24
three, and four. So, the index of value
0:27
eight is four, right? But, in order to
0:30
tell the computer to get the index value
0:32
of the preferred number, we have to
0:34
write the logic. And so, this is the
0:37
logic. So, for I in range of the alien
0:40
of nums, as usual, and if nums of I is
0:43
equal to eight,
0:45
so, then we are going to get the index
0:47
number. And the reason we gave it minus
0:50
one is because index number usually
0:52
starts with zero.
0:54
So, we have to do minus one for that.
0:57
And once you got the number, you're just
0:58
breaking it.
1:00
And when you run it, the position of
1:03
eight is four.
1:06
And the position of two is one.
1:10
Right?
1:11
So, it's working so good.
1:13
But, don't you think that we are writing
1:15
loops for everything?
1:17
Because sometimes Python must have
1:19
already solved this problem, right?
1:23
Like, this is not wrong, but it's too
1:25
long.
1:26
So, instead of this, we can just do
1:28
something like
1:32
position equal to
1:35
nums. dot
1:37
index of
1:45
It's going to give the same value as
1:46
before.
1:48
This logic is a bit long and even it's
1:51
going to take more iterations to run.
1:55
So,
1:56
this dot index directly gives you the
1:58
position of the value. So, Python
2:01
built-in functions exist for a reason.
2:04
And if value not found, then it raises
2:07
error. So, use it carefully.
2:10
So,
2:12
beginners, follow this first. You have
2:15
to understand the flow of the program.
2:17
So, trust me, follow this first. Once
2:19
you're good with this, then you can use
2:21
this.
2:22
So, before writing 10 lines of code,
2:24
always ask yourself, "Does Python
2:27
already have this?"
2:30
If there's already a solution for this,
2:32
why you waste your time on that?
2:34
Instead, you can learn something new,
2:36
and
2:37
you can implement it faster, right? So,
2:38
that's how you grow faster.
2:41
So,
2:42
always keep these things in mind. If you
2:45
didn't understand at any point, just
2:47
comment them down below. I'm always
2:50
there to explain it a bit more. I'm
2:52
going to elaborate it for you.
2:55
See you in the next one.
#Science

