And before we continue, one thing to know is to
know how to iterate on list with loops. So you
can perform a set of instructions for each element
on the list. And I'm going to show you this now,
because simply, you're going to use this multiple
times in every project you create in the future.
Okay, so I'm not going to introduce any new
concepts to you now. We are just going to use
what we saw previously. And let's get started with
an example. And I'm going simply to comment this,
okay, so we don't use it, I can remove it, I could
comment it, if you want to comment, you can go on
Code, Comment with Line Comments here, and you
have the shortcut if you want. Alright, so now the
program basically has nothing in it. I'm going to
create a number_list. Okay, let's see 3, 5, 8, 12, -3,
-7. Okay. And then I'm going to do
for number in number_list. And I'm going to print
number. This is going to go through all the
elements of the lists. And every time we enter the
for loop, the number variable here will contain
the next element on the list. And we can actually
use that to do anything we want here. As you can
see, that is quite similar with this, okay, we
have for i in range(10), range(10) is actually a
list between zero and nine. So we have the same
thing here for, we just don't use i as an index.
Okay, as a counter, we use a no meaningful name
here because we have a number_list. So if we have
a number_list, we're going to use a number, okay.
Because why do we have a number_list we have
numbers. for number in number_list, print number.
Let's run that. And you can see we print all the
elements, okay, in the list. Now you can do for
example, if number is lower, strictly lower than
zero, let's say print number is negative. You
could also add an else to say the number is
positive, let's run that. So you can see we have
3, 5, 8, 12, -3, and the number is negative,
-7, number is negative. Okay, just to show
you that you can combine conditions and loops,
okay. Also, you can use an if inside of for,
you could use another for inside that if or a
while or whatever you want. Okay, so you can
combine them between each other. And well this as
you can see, this is very convenient way to go
through an lists. Okay, and to recap, as you can
see, combining a for loop here with a list is an
extremely powerful combination. You just need one
line of code to do that here. And then you can
write a block of instructions to apply for each
element on the list. And this you're going to use
it quite often in all your future programs.