Let's now discover what are loops and how to use
them with Python. So let's first understand why
you need loops. I have created a very simple
program here that prints Hello 10 times. Okay,
let's run that program. And actually, yes, it
prints Hello 10 times. But that's quite not
convenient. And now what if I asked you to print
Hello 100 times, or 1000 times? Well, you're not
going to write 1000 lines of print Hello, no. So
whenever you need to repeat some instructions,
let's say more than two or three times, this is
where you are going to use loops. And we're going
to start with the for loop. So I'm going to remove
all that, and instead, I'm going to do for i in
range 10. So let's just follow along, and then I'm
going to explain to you, print hello. If I execute
this, I still have Hello printed 10 times, okay.
So what happened here. You can use the for loop,
so you start with for, and then you're going to
use an index, okay, so the index, which is going
to go through a range or through a list, and the
convention for that index is to name it, i okay.
So for i, and then in, what you can do is use
range. So basically, if you use range, a range is
a list, which goes from zero to this value minus
one. So basically, this is going to go between
zero and nine. Okay, so you have 10 elements
between zero and nine. And then you have a colon
here. So what is going to happen is that this is
going to execute the next block of code which is
indented here, as you can see, is going to execute
for each index, okay, that you have for the range,
okay? So it's going to execute it for i is equal
to 0,1,2,3,4,5,6,7,8, and 9. So basically, with that,
you just execute that block of code 10 times, and
the indentation here is super, super important.
I'm going to print test here, okay. So Hello, and
then test 10 times, I click, play to run. And you
can see Hello, test, Hello, test, 10 times. Now I'm
going to remove the indentation here and see what
happens. We have Hello 10 times and then test is
just one time, okay, because with for loop
you're going to execute this for 10 times. Only the
instructions that are indented in that block of
code. And then you continue the execution back to
the normal indentation and you continue the
execution of your program. Okay, so the
indentation is super important. And one thing I'm
going to show you is we can so we can actually
print that i, so that i will be here an integer.
So I can do Hello, and then plus, let's see str
i. Okay, I cast the number to a string. And
let's run that. And you can see now I have Hello,
0, Hello 1, etc. Until Hello 9. So you can
actually see what's happening here. So the i is
going to take all the values between zero and 10
minus one and then we can use that inside the
loop. Now if I want to print 100 time, I just put
100 here, and I run, and you can see we print Hello
with 100 time with a different number every
time. Okay, so that's very powerful. Here we've
just two lines of code, we can print this 100 times.