 In this video, we are going to look at a first example of a program written in Python. So let's create a new file and rename it to a first example. Okay, so let me give you an example. The example goes as follows. Given a list of numbers, find the average of all the even numbers in the list. Okay, so find the average of all the even numbers in a list. That is the example. So in order to do that, I have to give you a list of numbers first. So let's start with that. Let's create a variable called numbers and assign to it a list. And to do that, we use the bracket notation, so open and closing brackets. And in between the brackets, we are now going to list all the numbers that are going to be in the list, separated by commas. So let's go ahead. 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1 and 4. Let's execute the cell. It works. And now let's confirm that Python understood what we want. So let's simply ask Python, hey, what is numbers? And numbers is a list of numbers. The same numbers we gave it. Okay, so let's summarize what we have learned syntactically speaking so far. So at first, the single equals sign. This is what we call the so-called assignment statement. And the important thing is here, the right-hand side is evaluated first. And only if the right-hand side works, then the result of that will be assigned to the variable on the left-hand side. Then the bracket notation. This is what we refer to as the so-called literal notation to create lists. And there will be other ways to create list objects in Python. But the notation using the brackets, the so-called literal notation is one of the easiest ways to do so. Yeah, and now we have a list of numbers. Okay, so now that we have a list of many numbers, we want to do something with each individual number inside the list. So therefore, we have to find a way how we can access the individual numbers inside the list. There are two ways. And let's start with a first one. We can, so to say, grab inside the list and pull out an individual element. And we do that by specifying the index of the element we want to take out. So there is a syntax for that. And it goes like this. First, you write the name of the variable numbers. And then without a space, we write brackets again. And inside brackets, we have to specify the index of the element we want to extract or we want to look at. So let's say I want to look at the first element, the seven. So maybe I can do so by specifying the index one for the first element in the list. Let's execute this. And we get back 11, which is the second element. And that is not a mistake. This is just a convention that we see at play here. So by convention, in some programming languages, and Python is among them, we start to count at the number zero. So the index zero is referring to the first element in this case in the list. So to obtain the first element, we use the index zero. This gives me back the number seven. So today, if I want to obtain the 12 from the list, let's count how many elements there are before it. So first element, second, third, fourth, fifth, and the sixth element. And the index that goes with the sixth element would be the index five. So let's go ahead and write numbers. The brackets operator and index five. And this will obtain or pull out the number 12. Okay, so how do we call the brackets here in this context? So in this context, what we do is we call the brackets, the so-called indexing operator. So technically speaking, the brackets up here and the brackets down here have a totally different meaning. And that is something that happens often in programming that a symbol may have one meaning in one context and another meaning in a different context. Okay, so this is indexing, grabbing inside. And just to illustrate this, if we look at numbers, all the numbers are still inside the list. So by indexing, we are just looking at an individual element in the list, but we are not pulling it out. Okay, we are only pulling it out for, let's say, read-only purposes. Okay, we are not really pulling it out. It remains in the list. So that is one way to look at individual elements in the list. But this is rather tedious because we would have to index 12 times into the list, and we have to manually add the index number and thereby obtain the individual numbers. That would basically mean we could just go ahead and calculate the average of the even numbers by hand because that is basically the same amount of time we would need. So we need a better way to go over all the individual elements in the list. And to do so, we go ahead with the so-called for loop. So we write for, and now we have to go ahead and specify a variable. And here you will soon see why I call this variable number singular. And then we will write in numbers plural. And the numbers plural refers to the variable up here, of course. And then we end the line by saying colon. And now what we do is what this line does is this is a so-called for statement or for loop. This is going to execute a given code several times. And in particular, once for every time there is an element inside the list. And now what we do is I hit the Enter key. And this will, of course, take me to a new line. However, the new line is indented by four spaces. So that is a Python's way of knowing that whatever I'm going to write here belongs inside the for loop. And whatever code I write here is going to be executed several times. Okay, so I'm going ahead. And for now, let's simply go ahead and write print, parentheses, and let's say number singular. And what is print? Well, we've seen print briefly before. Print is a built-in function that simply takes input and print something out to the screen. So if ever you are in doubt if a function is a built-in function or not, what you could do is you could go ahead and go to the Python docs. And then you will click here on the library reference and scroll down a bit. And here you see there are the built-in functions. Let's click here. And here you have a list of all the built-in functions. And here we have indeed the print function. I click on here. And this takes me down to the part where the print function is being explained. And the function takes a couple of arguments. And one of the exercises actually walks you through this. And you have to figure out what these inputs mean. And then it basically explains here what the function does. But the print function is rather simple. It simply takes stuff and prints it out. In particular, it prints out the input on an individual line by default. So if I go ahead and run the cell, what that does is it prints out the numbers one by one on a line on their own. So let me repeat that. The for loop causes the code that is in the indented block here to be executed several times, namely once for each element in the list. So the first time the print number line is executed, number singular is going to be set to 7. The second time the print number line is going to be executed, number singular will be set to 11 and so on. So inside this block we are basically doing everything right here is done to an individual number. That is why I chose number singular here as a name. Of course, inside the print function here, what we could do is we could also write plus one. And this would simply add one to the number first and then print it out. So we can have some more fancy code here. But for now let's keep things simple here. So some people may be confused and ask, how does Python know singular and plural? Well, the answer is it doesn't. So it doesn't matter what name I specify here. So I could go ahead and instead of number singular, I simply say for example X and this would not change the output. So everything stays as before. However, my recommendation is that you always try to use names that are kind of meaningful. So for example, if you have a list of numbers of many numbers, then calling it numbers plural is kind of intuitive. It makes sense. Calling a variable that is set to an individual number, just number, is also kind of a good idea. So by choosing names in this way, we are keeping our code readable. So that is the idea of a for loop here. So let's summarize quickly the for dot dot dot in dot dot dot and colon. We call this a for statement or a for loop. And what this does is executes code repeatedly. And now the question is, how can we generalize the syntax here? And the answer is you will also quite soon see another example of such a line that ends with a colon and these lines we call header lines because header lines are all the lines in Python that are followed by an intended block of code. And just to illustrate another point, we could go ahead and create another variable. Let's call it result and 32, let's say number plus three. And then we could say print result here. So we could also have more than one line of code in the block. However, note how both lines have the same level of indentation, namely four spaces, four space characters. So this is how Python knows that those two lines basically belong together. They are one block of code that is executed repeatedly for each element in the list. Okay, so let's undo the changes and go back to the print at the simple print number version. One more thing syntactically speaking that we want to understand is the meaning of the parentheses here. So the parentheses here are what we call the so-called call operator. And what this does is it executes a function. Okay, so if I go ahead and I simply write print without the parentheses and I execute this, I get back a reference to the function, but the function is not being executed. Okay, so this is just referring to the function without executing it. But by putting the parentheses here next to it, that basically means not only look up the function but also execute it. Okay, and maybe execute it with the number one in it and print out the number one. Okay, so that is a little bit more of syntactical terms that we should learn. So now let's go back to the problem. Now we have to come up with a solution approach. So now we know how we model the numbers in the list. And we also know how we can loop over all the individual numbers inside the list. Now we have to come up with some logic, so-called business logic that solves our problem. And the problem is that we want to find the average of all the even numbers in the list. So we have to break down this big problem into many small steps and then solve them one by one. And the next step that we want to solve is we want to go ahead and make our for loop such that whatever is in the block here is only executed for even numbers. So in other words, I want to put in a filter that basically skips all the odd numbers. Okay, that's what we want to do. So, okay, let's think of how can we characterize even numbers versus odd numbers that we want to skip. So without going into too much math, I think we all remember from high school that any number, any whole number divided by two that has no rest is even. So in other words, if we divide that by two and we get a rest, we have an odd number. If we get no rest, we have an even number. So that means we now have to figure out how can we do division in Python. Okay, so let's learn that. So let's go ahead and say let's take the number seven and let's divide it by the number two. And the easiest way would be to use the single forward slash seven forward slash two. And this gives me back the result of 3.5. This makes sense. Okay, now I will show you two more ways of how we can divide a number by another. So the first variant of that is to use the double forward slash and double forward slash is what we call the integer division. So the integer division takes two numbers, two whole numbers, and it will give me back another whole number. So in other words, it will not look at the decimals after the decimal point. So seven divided by two is three and some rest. And therefore, seven divided by two gives me just three here with this using this division here. And then we have a similar variant, the third variant of division. This is the variant that simply gives me back the rest. Okay, and this has a name. It is called model or division. So let's go ahead and do that in Python. And the symbol for doing that is the percentage sign. So seven percentage sign two will be read as seven model or divided by two. And this gives me back one because seven divided by two is three and the rest of one. And this is simply what we get back. Okay, so let's do the same three examples here using an even number. Let's take eight, for example. So eight divided by two gives me back 4.0. This should already get you a little bit suspicious. I'm not getting back four here. I'm getting back 4.0. So for a computer, this seems to make a difference. If I divide eight with a double forward slash by two, I get back four as an integer. And this makes sense because it's just an even number. So the division works perfectly. And then if we go ahead and model or divide eight by two, we get back zero because eight divided by two has a rest of zero. Okay, so this is division. And now we're still not done with this filtering part. So the next thing we want to do is we have after we divide a number by using the model or division by two, we have to compare the result of the division to either zero or one in order to figure out if the number is even or odd. So the next we have to learn is we have to figure out how one number or a variable holding a number can be compared to another number. Okay, and there is a comparison operator in Python. And the comparison operator is very simple. It goes like this is simply a double equal sign. So if I compare number one to the number one, then I get back true. If, on the other hand, I compare the number one to the number two, I get back false. Okay, and true and false are just Python's special values for saying yes and no. Okay, so this is the so-called comparison operator here. Okay, so let's maybe write that here. Write that down. The double equal sign is the comparison operator. And then the result that we see, the true and the false. This is just these are special values indicating yes and no. This is how Python says yes and no. Okay, so now we have learned how we can divide a number using the rest division and compare the result to zero in this case. And if we then get back true, then we know the number that was divided is even. So let's go back to the follow. And inside here in the first line of the code block, we now have to write down some logic of how we compare each individual number to or the rest by dividing by two to zero. So how do we do that? We go ahead and we write if. This is the so-called if statement. And then we go ahead and we write if number singular, modulo divided by two. So the rest division, double equals zero. So if number divided by two has no rest. This is how this read colon then do something. And what do we want to do? Well, for now, let's just print out the number. And what we are going to do here is we are going to indent the print number line by one more level. So now print numbers indented by eight spaces. Okay, let's execute this code cell. And as we see, we only see the even numbers. Okay, so we are now skipping the odd numbers. Okay, so now let's learn another concept here. So let's say I want to make this line here easy to understand for a human reader here. So what I will do is I will comment this line. So I will write a hashtag here and I will say if number is even. Okay, so that is what this line here means. Okay, so this is an example of a comment. And a comment basically means this is text in a code inside a code cell. And Python basically does not execute it. Okay, it just ignores that. Okay, so let's summarize the if dot dot dot colon. This is the so called if statement or also sometimes called the conditional statement. And here the code block is only executed if a condition is true. And then also the hashtag symbol here, this is basically a comment. And what that means is Python ignores whatever comes after it until the end of the line. Okay, and maybe I introduce another nice abbreviation end of line or programmers is often abbreviated with EOL. So you will often see EOL, you will also see that in a couple of error messages in Python. EOL just means the end of the line. Okay, and if I execute this code, we see that we get the same output. Okay, so now we have written code that does something for every odd for every even number inside the list. Okay, so now let's go back to our big problem. And now let's think of how could we proceed. And the answer to this is if we only have all the even numbers, then what we do or what we should do in order to figure out the average as well, we should simply add up all the even numbers and then divide by the number of even numbers. Okay, this will give us the average. So how do we do that here? So this is an example now of where we introduce a temporary variable or two temporary variables to help us solving a problem, but the variables itself, we are not really interested. So let's first create a variable. Let's call it total. And let's set it to zero at the beginning of the code cell. And the variable total will hold the sum of all the even numbers. So what we do now here, instead of where it says instead of print number, we are now going ahead and we go ahead and we'll say the new total is will be set to the old total plus whatever the current even number is. So the follow this looping over the even numbers for an every iteration of the loop number singular is an even number. So we take all the individual all the even numbers one by one, and we add them on the total and the total starts at zero and then at the end, at the end of the cell, if we ask Python hey what is the total. Python will give me back 42. So the sum of all the even numbers in the list is 42. Okay, so for some of you this may look confusing because this looks like a mathematical equation, but this is not an equation. So as I said above, when we see an assignment statement, the single equality sign, then what we do is we evaluate the right hand side first. So we add total plus number and then the result of that will be assigned to the total. Okay, so that is what I mean by the old total on the right hand side and the new total on the left hand side. Sometimes we refer to this as updating, updating and variable. Similarly now, let's go ahead and introduce a second temporary variable and call it count. And let's set it equal to zero as well. This is called initializing a variable. Let's go down here. And let's also update the count variable by saying the new count is the old count plus one. So if for every even number, we always add one to the count, then at the end of the day, the count will be the number of even numbers in the list. And then what is the average? Well, the average is just the sum of the even numbers called total divided by the count of the even numbers count here. Okay, and if I execute this, I get back as the result 7.0. Okay, and this makes sense if we look at the numbers, we are starting with all the whole numbers from one to 12. And if I average all the even numbers in this list, then 7.0 seems to be a intuitive result or sum that looks correct. Okay, so that is our first example of a program that involves a little task. And what you should learn from that is, of course, all of the syntax that we talked about here. But also you should get used to the way we break down this in, so to say, big problem here into many small steps. And we write code that solves the problem in a programmatic way. And now what we could do is we could change the list here. We could have a different list with different numbers and execute this code cell here. And then we would calculate the average of all the even numbers in another list. Okay, so this is how the problem here is solved. Okay, so I will leave it at that and I see you soon.