Hello and welcome to another lecture on writing clean and effective Python code in this electively will
learn of it to use a list comprehension and instead of using some complex things like using map and
food.
So let's say you have a problem statement in Python and you have to multiply each number in the list
by two.
So let's first go ahead and get a list.
So let's type in equals let's see is the name of your list.
So that's going to be one two three four five six seven and eight.
So you actually need to go ahead and give spaces are we here as well or else we will be violating the
general rule of not giving speeches.
So make sure that you have proper spaces in between these elements.
And it's actually quite a good practice to always write clean Python code because the more you write
clean code the more you remember what good actually is.
OK so once you have this list with you and your problem statement is to multiply each number in the
list by two.
So one way of doing that is by using a list comprehension and either way is by using a map and fill
it up.
So you might recall that by using map in order to calculate result what you could do is that you would
type in result which is going to be a resultant list and then you could type map and after that you
could specify a lambda function.
So you type lambda and as in this case we have to work with a will specify an equation for x x and x
multiplied by 2.
And that's actually going to give us the result for the list.
So always make sure that you also have spaces so we're here.
OK so once you do that I guess we don't need a space here.
So it seems whitespace before this.
OK.
So once you have this lambda over to you this is actually going to store your result in the result variable.
And when you print out the result.
So if you save the code and let's say you run the code as well.
So let's see what result we get.
So when you actually go ahead and run this code we actually got this kind of result.
And the reason for that is we do have to typecast this result to lose even before we bus it as an output.
So if you do that and if you save the code and run this thing you will actually get the result as a
list and each of your element is multiplied by 2.
So this is one way of doing so that is by using a map and lambdas.
But this is one of the most inefficient ways when you actually want to perform a relatively simple operation.
So I am in this case was to go ahead go through the list and multiply each number for two.
And we have used a lambda function and math for this.
And the thing with lambda and maps is that it's only efficient if we use maps and lambdas while we perform
some complex operations.
But in case of simply handling some kind of list or anything like that this is actually not a correct
way to do so.
Instead what you could simply do is that you would use a list comprehension instead of this particular
map.
So what you could have done here is that you could have replaced this thing with a result equals and
you could simply have used a list comprehension by typing let's say X multiplied by 2 for x in A.
So once you do that this code is much simpler.
And this list comprehension actually requires a lot of less resources and less amount of code and it
actually makes the job much more simpler as well as it makes the code efficient.
So if you saved this code and if you actually go ahead and run this as you could see now you'd get the
same result.
But this time you have used list comprehension as opposed to maps and lambdas.
So this is one of the things which you need to keep in mind that always use less comprehension whenever
possible but when you're working with complex data you could opt for map and lambastes.
Now one of the most interesting things one thing which you might not know about Python is that you could
always use an else statement in for a loop.
So usually we use the L statement in combination with the if statement.
But in python we could use any statement with for loop as well.
So let's take a simple example so I'm going to type in for x in range Let's see 5.
And what you could do is that you could simply print out the value of x.
And here just like we do for the if statement you could specify the else block away here.
And let's say you print something out like let's say this is the as block.
So now if you go ahead see if the code and if you run this code as it could see you get 0 1 2 3 4 as
part of this loop.
And finally no matter what the else block is going to get executed.
So this type of functionality exploited in Python because it's required in some of the cases and we
have not yet encountered any case where you actually need to use an ELSE block along with the for loop.
So in case of an IF statement what happens is that if the condition here turns out to be true then the
else block is never executed.
But when the condition turns out to be false the else block is executed.
But this is not the case while using the else block with the flu.
So when you write a for loop no matter what is there in the for loop the else block always gets executed.
So one way to stop this is by including a brief statement or a here.
So what you could do in order to stop execution of the else block.
You could use if statement by typing.
Let's see if x is equal equal to.
Let's see.
You want to stop at full.
So you need to type and break over here.
So now if we actually go ahead and if you execute this code as you could see now as soon as X and going
the value equal full the code actually breaks.
And now the else block is not executed.
So this is how you could actually use the else block with the for loop and don't get confused if you
ever encounter such code.
This simply means that the for loop is going to get executed and if the for loop is not going to have
a brief statement the else block is going to be executed.
In any case.
So that's it for this lecture.
And hopefully you guys were able to learn a few things about writing clean and efficient code in Python.
So thank you very much for watching and I'll see you guys next time.
Thank you.