So most of you probably know some programming language already.
And in every programming, language loops are very important and also they are often combined with if
statements so loops and if statements in Python are very similar to the cases in other programming languages.
So let me show you here two examples for loops.
The while loop and the for loop.
And then we will also combine one of the loops with an if statement.
So let me get started with a mile loop.
So the syntax for a while loop is, of course, we start with while and then we have to define some
condition.
For example, we could say we have an index called I add this I should be smaller or equal to the number
of 10, and then we add this to dots and this will start to loop.
Now, when we press, enter the editor or the text editor here.
Just you put a notebook in the browser already makes a tab here.
And this is quite common for Python.
So you do not write your commands here, but you make a tab so that it works fine.
So here we have to define what will happen, actually.
And since our condition will be that the number I must be smaller, equal to 10, we should do something
with AI.
First of all, we should define the number AI and we should do this outside of the loop.
So we should say the starting role, if I could, for example, be one.
And then in the loop, we increase the value of AI by one every time it loops.
We increase the value by one.
So I run this now.
Nothing happens.
But also there is no error because we start from April one and then we loop make it two, three, four
five and so on.
And then once we are at 11, the wire condition is no longer true.
So the loop stops and the cell finishes the calculation.
So what we can do instead is we can add another calculation here or another command.
And I want to add it actually before I increase the index and I want to say it, just print the number
I.
So if I run this now, you see it loops 10 times, increase the value of AI and prints the value of
AI.
And then once I is 11, it stops.
So this is a way to print the value.
Of course, we could also do something like this.
So then it gives us twice the value of ESD output.
So what else can we do with while loops?
Well, of course, we can add some if statements, and I want to show you here that I want to basically
copy this.
And what I want to do is I want to print the value of AI only when it's an even number and when it's
an odd number.
Instead, I want to print the word order.
So how can we do this?
Well, the beginning is quite good.
We start from one, we increase the value and we stop ones is larger than ten.
So the only thing that we have to change is this one here.
And I do the following I write an if statement, which begins with if so, then here we have to write
our condition, which I will do in a second.
Then we will write this again.
And you see it adds another tab.
And here we write our command that we want to do or execute when the condition is true.
So this would be print out and then we go one tap a back so that we are just at the level of if I will
then write ours and if it's not true, then we we will print the words out.
And then since this is once again at the same level as if and else this is outside of the if statement
and this will be done every time.
So the only thing that we have to now specify is the condition and the condition will be that AI is
an even number.
So how can we do this?
Well, we can calculate the modulus, which I have explained to you at the very beginning.
So we write like this.
So this means the modulus of AI and two.
So basically, the remainder of the divisions, I divide it by two and then you just take the remainder.
If this is equal to zero, then it means it's an even number.
For example, if I is eight, then eight divided by two is four with no remainder.
So this is true.
So it will print the value eight.
If it's seven, then seven divided by two is three with a remainder of one.
So this is false.
So it goes to Alt's and prints OT.
So if I run this, you see for every odd number.
Just Prince, T-Bird, odd and even number it prints to value itself.
So pretty easy, I think, and pretty much the same as in every other programming language.
So the for loop is also, of course, pretty similar.
So here's the only thing or the nice thing where it is useful could be to work with lists so you could
write for and you have some value, some variables, some running index, for example.
So we could say for AI in and then we can add here a list.
So just some random list numbers.
So this means that we have a running index AI, which takes every well, you off this list and then
the first loop, it takes this value and the second loop it takes this value and so on.
And then the fourth one, this one and then sends the list ends.
Also, the loop ends.
So this means now we can write what should happen with AI, given the fact that it changes the value
every time it runs through the loop.
And of course, the easy thing would be to just print the value of AI.
So this would give us these four values, of course.
And what I found pretty cool is that the same thing happens or works not only with lists, but also
with strings.
For example, I write for AI in the world, one in the string test print.
I think you see the result will be all the letters.
So basically, it treats the string like a list of characters.
And so in the first loop, the AI will have to value T and so on and so on.
So that's pretty cool.
Now what I want to do next is I want to restore or redo this example of the while loop with the if statement,
also with the for loop.
So since it will be pretty similar, let me just copy this and now we have to modify it.
And of course, we have to modify this one.
I think the rest can be, yeah, can be as it is.
So what we do is we ride for AI in and now we must make a list basically from one to and so on until
10.
And this is, of course, a bit tedious.
So maybe let me start it.
And for now, I mean, it's OK as only 10 numbers, so I can show you what happens
for this one.
So like this?
So now we have the same output as before.
So you see, it works.
But a trick that I want to show you is, how can you optimize this long list here?
Because if you would say, I want to do this for the first 1000 numbers, then it would be very tedious
to write all of these numbers down in a nice way.
To construct such a list is to range commands.
So the range command works like this.
You basically give a starting value and you give an end value and then it takes all the numbers and
creates a list out of it.
So if I run this, it gives me almost the correct result.
It starts with one and ends with nine.
So this is once again the same problem as we have encountered before.
We have to take the ending index one number higher.
So like this, by the way, you could also leave out the first number.
But then the I would start at zero, which is not what we want.
So we just tried one 11 for the range arguments.
So I think that's pretty good.
So we have now discussed the while loop and we have also discussed the for loop.
And the only thing that is left that I want to show you is I want to show you how you can like very
easily easily store these outputs in a list.
And for this, you just write something like store storage or store list and make it an empty list,
which would be this one.
So just to square brackets which indicate that this one is a list which has no entries yet, and then,
for example, you can just write for I in range of the numbers one, two six and what do you want to
do is you want to now append the number to the list.
So you write store list, dot append and then you write here what you want to append.
And for example, I want to attend the square of these numbers.
Are on this, and you see, now we have stored these numbers in the store list variable.
So here you have square numbers one, four, nine, 16 and 25.
And in the following, we will work further with lists and with loops.
And I want to show you how you can store this information in files and how you can load the information
back into your notebook.