Hello everybody and welcome back to this tutorial. And right now what we are going
to do is we're going to take a look at the for loop. So we already covered the
if statement, the if-else statement, the while loop, the while true loops, how to
break out of them, how to continue in them. We coded some of the normally, I
mean, some of the useful programs, such as a password checker. Really nothing special
yet, but, hey, that is something as well. So, let us go with the for loop right now. In
order to do that we can just open up our IDLE, and the for loop, usually, but
doesn't have to be, but usually comes with the statement, or the function, range.
So what that means is that it allows us for some counter in range, for example
from one to a hundred, we want to perform a certain task. Of course in that counter,
of course in that for loop, we also want to increase that counter every time we
perform that specific task, because otherwise that would be an infinite for
loop and that is not really that useful for us. So let us see how we can actually
implement the for loop. So, first of all, while you are actually using the counter
in the for loop, it is good to notice that you don't
really need to define it before it. You can actually use it, or the for loop
defines it itself. So for i in range, for example, (1, 100), as I said, so
like this, add the two dots, and right here for i in range(1,100) you
want to perform some specific actual function, or some specific task right
here. So we can just print('hello'). We're not really doing anything specific, but
I'm just showing you how you can do that. And then of course
you need to increase the counter right after it before that certain task.
Otherwise, this would not make any sense. So you want to increase it i = i
+ 10, press here enter once again, and we get printed out the actual, pardon me, this
is a for-loop. Yeah, I forgot, you don't really need to actually increase the
counter in the loop, the for loop increases it itself, which is another
good thing. So, for example, let me just do it once again since we actually added a
line that we don't need. So for i in range(1,10), so let us use
1-10, print('HELLO ' + str(i)). So this will be this will be printing
out HELLO 1, HELLO 2, HELLO 3, HELLO 4, and so on and so on until HELLO 10.
So just type here right here enter. It finishes our for loop. You can see
that right here we didn't really increase the counter. Not really sure why I told
you that you need to increase it. I maybe mistook it with the actual while loop
and if statement. In the for statement you don't really need to do that, the for
does it for you. The for statement does it for you. As we can see right here, for
a certain counter i in range from one to ten, we are going to print hello, and it
printed out hello 10 times. Starts counting from one and goes through nine.
The reason why it doesn't go through 10 is because the 10 is not
considered as in the frame range itself. So there is also another thing
that is most used in the for statement as well. This is a function
that we already encountered before which is the length function. So if you just specify a
string, for example, now I will use it with a string, but it is most likely
used always with the list itself, counting the elements. But, right now,
we're going to count the characters from a string. Now if you have no idea what
I'm talking about, don't worry, right now I'll show you. So let's say we have a
string that says let me just find it that says 'hello there'.
Simple as that, string that says 'hello there'. Then you can use a for loop that
will actually go to the range of the length of that certain string.
Which isn't really that useful for us right here as a string, but it would be
useful if we had a list. We would go to the range of the length of that list, and
the length of that list would be retrieving the actual number of elements
in that certain list. So let me just show you what I mean. Let us first of all
count how many characters are in the string. So, 1, 2, 3, 4, 5 in hello, one empty
space and five more in there. So 11 characters in this string. Then we can
use for i in range, and then open brackets, len, open another bracket, from
the string, close two brackets. And then let me just go right here, two dots, print
this is len of string. Doesn't even matter what you print, this is just so it
prints something. You press here enter and it will print this is len of a
string the same amount of times that there are
characters in the actual string itself. So this right here is the actual, first
of all, it performs the inner function which is the length, the length of string
is 11, and what this does is for i in range 11, so 1 through 11, this will get
printed out. And that's why it gets printed out 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
times. So, that is basically the simple usage of a for loop.
Now, also, there are a few other things that you can do with it, which we are
going to cover. Such as, for example, this range right here doesn't have to be an
actual specification of one to ten. You can actually specify it to count one through
ten, but every second step. So, for example, it will print out every second number.
This will get shortened from ten times printed out to five times printed
out, and you can specify that with another comma and then the number that
you want to skip. So for i in range 1 to 10, and then comma 2. What this means is
it will go for i in range one through ten, but each time it will not increase i by
1, it will rather increase i by 2. So that would mean that, for example, if i is 0
at the beginning, pardon me, 1 at the beginning, it will be 3 in the next turn
of the for loop. So I'll show you. Right now it will print it only 5 times. So
print('This will get printed 5 times'), close this, and you can see this will get printed
5 times. Now if you specified any other number here such as, for example, for i in
range 1 through 10, and then the actual 5, so that means that this will get printed
out five times. Print hello, pardon me 2 times, not 5 times. This will get
printed out 2 times, close the brackets, and you can see this gets printed out
two times. Now also one more important thing, well not so important but one more
thing that you should know, is that you can actually count backwards with a
for loop. So you don't need to type here for i in range 1 through 10, you can simply type
here for i in range 10 to 1. But right here you need to also specify how
to adjust the i variable. So we need to actually subtract it every time by 1.
This will also print the actual statement 10 times but it will go
backwards, in case you want i to go from 10 to 1 instead of 1 to 10, for any
reason you want. If you want, for example, to print a list like this, so let me
just show you. For i in range 1 through 10, and you simply just print i, you
can see it gets printed out 1, 2, 3, 4, 5, 6, 7, 8, 9. But if you just type here for i in
range 10 to 1, and then comma -1, and then you specify print i once again,
you can see it gets printed out as 9, 8, 7, 6, 5, 4, 3, 2. So
it goes backwards. Now in some cases that might be useful, for us it really isn't
at the moment. I just wanted to show you that there is that option as well. And,
basically, that is all for the actual for statement. Now what we are going to
do in the next lecture is we are going to cover some of the modules, library importation, and
how you can use different libraries that are already pre-installed with Python 3. We'll take a
look at some of them. Now we will not of course use many of those functions, yet.
We're only going to see how you can import them and how you can use them. So
hope you enjoyed this tutorial, and I hope I see you in the next lecture. Bye!