 In this video, we are briefly looking at best practices when coding in Python. So let's first create a new file and let's rename it into bestpractices.ipython notebook. And let's revisit our very first example of a Python program. So example and example was finding the average of all even numbers in a list. And let's use the same numbers list we used previously. So either the numbers 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, and 4. And our very first solution approach goes as follows. We write a for loop by saying for number in numbers. And then for every individual number in the list, we check if it is even by saying if number mod already divided by 2 has no rest. And then we want to do something for every even number. And here we need two helper variables, so a variable called total and another one called count. And we set both of them to zero. And then we say for every even number we encounter while looping over the numbers list. We say our new total is going to be our old total plus the current number because it is an even number. And also our new count is going to be our old count plus 1 because every time we encounter an even number we just add 1 to our count of even numbers. And then to find the average we will simply go ahead and take our total of even numbers and divide it by the count of even numbers. And that will give 7.0. So let's first look at this example, a code here and try to see if we think this is a good approach or why this may not be a good approach. So first of all, it is of course a good approach because it works. The code, yeah, it works, it is correct. And also it generalizes. So if I wanted to calculate the average of different even numbers, all I have to do is I have to change the numbers up here and the code will still continue to work. So that is a very good first solution. However, when we write programs one way of analyzing code is always to look at how readable it is. And by readability I mean if another programmer or if we ourselves look at this code maybe in a month from now. How likely is it that we will immediately understand what the code does? And here it is probably not so obvious. So even an experienced programmer would probably need half a minute or so, maybe a minute to read through all of this and figure out what the code does. So the code itself does not communicate what it does in a clear way. And also there are a couple of other things that are not that good, but I will talk about them as we correct the code and improve on it. So let's do the following. Instead of looping over all the numbers and then inside the for loop have some if condition and do some calculations, we could break down or we could view the problem in a different way. So a different solution approach could be as follows. So let's write alternative. And let's say first create a new list with only the even numbers. And then second, thumb up the new list, third count the number of elements in the new list and then calculate the average. So in the current solution, what we do is we do basically all of them are all of that together in one big giant step. So this for loop does two things, A goes over all the numbers and then it shows us them out. And then while filtering, we are also doing the calculation. So we're doing all of the steps together in one big step. And that is not so good. So let's try to see how we can use our alternative way of viewing the problem and rewrite it. So let's first go ahead and let's simply evaluate the numbers list here. So we have it right here. Now let's say create the first step here says create a new list with only the even numbers. There are a couple of ways of how to do that, but I will now introduce a new concept and the new concept goes by the name list comprehension. Okay. So this is a compact syntax to derive new list objects out of this thing once. So this is what a list comprehension means. And now let's see an example of how we write that. So in order to create a new list out of an existing list, we will write brackets first because we want to create a list object. And then inside this list object, we will go up here and we will simply copy paste the four line of the code cell above without the colon. Simply copy paste that in here. And now let's go ahead. And on the left hand side, before the word for, let's write out the number singular and let's execute this. And we see that this syntax derives a new list object out of the existing list object called numbers. And every element that is kept in the new list object is just the number as it comes from the old numbers list. Okay. So now the first thing we will do to make this a little bit more readable, instead of using number singular, we will now use simply the letter N. Okay. So normally my recommendation is to spell out variable names. But when you use a comprehension to make something compact, to make something look compact, then you could also use shorter variable names, for example, simply N. Okay. So on the right side to numbers, we will simply go ahead and we will copy paste the if logic without the colon colon. And because the variable number was renamed into N, let's also simply replace number with simply N and let's execute this. And this expression, as we see creates a new list object with only the even numbers in it. Okay. You can read this just like a mathematical rule. Okay. So this basically says create a new list object and take out of the list numbers all the ends one by one and only keep the ones that are even. This is basically what it says. And now what we will do is we will go ahead and we will assign this new list object to a variable and we will give it a good name, a name that we can easily understand and such a name in this case would be simply events. Okay. So without looking at the entire or any details of the list comprehension on the right hand side, what we see here is that we're deriving something out of numbers and the result is going to be called events. So as an experience program, if you read this, we will probably understand this right away. We'll say, okay, we are, you know, filtering out the odd numbers. So to say, and we will just keep the events. Okay. So let's go ahead. The next step would be sum up the list. So summing something up, summing the elements of a list up is something that already sounds very trivial to do. So shouldn't there be something built into Python that does that for us? Because I think it's not a good idea if we have to write a follow every time we want to sum up a list of numbers. And indeed, there is, of course, a built-in function. So let's go to Python docs, the library reference to the built-in functions. And indeed, there is a function called sum. And the sum function takes input, several inputs. And the first one goes by the name iterable. What that means is something that we will learn in a future video. For now, let's just define that an interval is anything we can loop over such as a list. And then it says sums the items in the interval. And I think that's already enough to understand what the function does. Okay. So let's go ahead and use the sum function. And let's call it and give it maybe the numbers list, the original numbers list. And we get the number 78. So that is the sum of all the numbers in the original list. And if we replace numbers with events, then we get what is going wrong here. Well, I have to rerun the cell because in the last time when I run it, I didn't have the events set to here. So now it works. And now the sum of all the events is 42. And this kind of makes sense. We already had this number in the other video where we developed the solution that we show above here. So that is now an easy way of how we can calculate the sum of a list. And now for step three, we want to go ahead and count the number of elements in the list. And for that, there is also a built-in function. So let's go back up here to the list of all the built-in functions. And here is a function called len for length. And len takes one argument here or one input. And we say it returns the length, the number of items of an object. Okay. So let's go back and simply try it out. So len of events. And this is just six because there are, as we see, let's maybe put, let's read the events list right here. So here we have six elements inside the events list. So len with events is six. Okay. And now if you want to calculate the average, we could do it as easy as this. We could go ahead and simply write sum of events and divide that by the length of events. And if we execute that, we also get 7.0. Okay. So let's maybe write that in one cell. Let's copy-paste this here, the list comprehension. And then now let's also copy-paste the calculation of the average. And let's also give it a name. So let's simply call it average equals. And then maybe with a space in between, let's simply read the variable average. And this will also, of course, give us 7.0. Okay, so and if we look at this cell, as compared to this cell, the cell below is a lot easier to read, right? So first we take the numbers list. We take the events out of them, we give it the variable a nice name. And in the next step, we calculate the average. And for those of us, the math nerds who want to understand what an average is, well, it's the sum of the events divided by the number of the events. And this is basically using built-in functions. And built-in functions will become second nature. So you will be able to read these kind of expressions very easily in the future. So by looking at this cell, this is very easy to read. There's a second advantage. So for built-in functions, we know they are correct, okay? So these functions have been part of Python for a very long time. And they have been tested by many, many people. And they are in use by many, many people around the world. And if there were an error in the sum or the length function, we would know. Secondly, also, they are usually a lot faster in execution. So in the beginners course, this is not a very important topic, but in the long run, you should know that whenever you have to write a for loop on your own, for loops tend to, for technical reasons, they are a bit slower. If you use a built-in function, that does also a loop in the background. So we can think of summing up the events. This here will also do a for loop in the background somehow. But these functions are highly optimized at a very technical level. So these built-in functions also have a way better runtime, okay? So these are a couple of advantages. So the most important advantage being that this code cell is easier to read and maintain in the future if we have to make changes. But also, with the higher probability, it's correct. And of course, the code is running faster, okay? So this is the kind of code that we are aiming at. So in future videos, whenever there is a trade-off between different ways of solving a problem, I will always give you the pros and cons of different solutions. And this way, you will learn about best practices. An important best practice that you should already know, maybe search that on the internet, is the so-called PEP 8 standard. So PEP 8, in the Python world, and it's a link to the official documentation. This is a document that is a so-called Python enhancement proposal, PEP for short. And these are official documents that people can write and submit to Python, to the Python Software Foundation, and then there is a peer review process. So for some time, many programmers and people from the Python community will look at this and in these PEPs, usually these are specifications of things, they have suggestions of how to make Python better and explain certain concepts that Python is built on. And PEP 8 is a very old PEP, it goes back to the year 2001, as you see. And PEP 8 gives you many, many rules of how you should format code, so that is with a high chance maintainable and better. And one example that I will take out of this document, if we go to Intendation, here it says use four spaces per level of Intendation, okay? So this is why we use four spaces and not one or two and some other languages use eight spaces per level of Intendation, okay? So the Python community at some point decided let's all use four spaces so that we all write the same code, basically. And of course there are many, many other things on this list as well. I would recommend to glimpse over this for now. You won't understand so much here in the beginning, but from time to time you should go back and review this understanding and applying PEP 8 is the first step to writing a good Python code. And so we will also get back to this PEP here a couple of times throughout this lecture, okay? But this concludes our brief discussion on finding maybe a better solution to an already solved problem in this course.