>> Hello everyone and welcome back to
intro to Python here on Cybrary OnDemand.
I'm your instructor Joe Perry.
This is the unfortunately pun based lesson,
Lesson 4, for loops.
Those of you who just came from
Lesson 3 will be aware that I had
the depressing realization that I had unintentionally
written a pun into my lessons and that's okay,
but that's what happens and
I guess we just have to live with that now.
In lesson 4, we're going to
understand the concept of looping,
what it actually is, what it's for,
what it does, how we use it?
Then we're going to learn and we're going to implement
a specific type of loop called a for loop.
Loops are a fundamental part of flow control.
They're one of the things you
implement based on Boolean logic,
just like you do with if statements.
Now all of programming,
as I've said before and all of computer science
and a huge chunk of math is in some way
derived or derivable from
Boolean logic but loops are
a very specific implementation of it.
What they do is they take a piece of code,
they take some sequence of actions,
what we would often call an algorithm or a set of rules,
and they repeat that sequence of actions
until some condition is met.
They may apply logic to every member of a set,
if you have a list of items,
they might apply logic to all of that.
They might count to a specified index.
They might just count up by one number each time
or by multiple numbers depending
on how you structure it,
or they may cycle through some series of options.
Loops can be used to implement all sorts of things.
In the next lesson, we'll talk about while loops,
you'll see how programs that are always
on or programs that last
forever are executed and
a lot of those will make use of while
loops to perform what we call an infinite loop,
or basically a loop that doesn't terminate.
You can see the comic right above me,
"Bob is our infinite loop specialists."
Generally speaking, when you're creating a loop,
you're going to give some initial value
to help set the condition.
We'll talk about that a little bit
more in the next lesson as well.
For loops in Python,
you actually can get away with not using
initial values pretty often.
Just because of the way Python is structured,
it will often fill in your special variable for
you or fill in your conditional for you. For loops.
An example here for all of the items in this list,
remember I said we could apply
logic to every member of a set,
for all of the items in this list, the Number 1,
the letter b,
and the Number 150, print that item.
Below that you see pretty
close to what you would see in Python.
You wouldn't say for i in list,
you would use whatever the lists variable name is.
You couldn't use lists as the variable name
for reasons that we'll discuss later.
But this is roughly Pythonic.
For i in list, print i.
We're not going to talk deep about functions.
All the print statement here is doing is
taking whatever value is given to
it and it's writing it to the screen.
For i in list, print i.
Well, let's have a look.
Is there an item in the list?
That is the conditional
upon which this is actually being evaluated.
The for i in list,
this for loop is an implementation saying,
is there an item in the list?
If yes, retrieve that item.
That's where if comes in,
that's where the Boolean logic evaluation,
the execution decision based
upon internal logic comes from.
Is there an item in the list? Of course there is.
There are three items at present. Print that item.
In this case, the first item is going to be the Number 1.
Generally speaking, lists are
evaluated from left to right as they are written.
Largely, it's just a programming standard,
it's the way it generally implements.
However, lists are not inherently sorted so
sometimes you'll want to
control the way it interprets your list.
But generally speaking, it's going to be
left to right as it is written.
Is there another item in the list?
We've printed our first item,
we've retrieved it, we've used it, we're done with it.
Now we're going to go to the next item in the list,
we're going to increment what we call the index.
We started at item 0,
now we're going to go to the item that is
indexed at one,
in the same way that you would use,
for example, an index and a card catalog system.
You'll see indexes referenced often.
All an index is a number that
identifies the location in the set,
in the list in the array and whatever.
Is there another item in the list?
We increment our index and we see
that yes, of course, there is.
In this case, it is the letter b.
Then for a third item, we say,
is there another item in the list?
Sure enough, there is, it's the number
150, so we print that.
Now we reach a point that's a little bit trickier
because we've run out of items in this list.
There's nothing left in the list to print,
there's nothing left to retrieve.
Is there another item in the list evaluates to
false and your for loop will terminate execution.
The condition here is actually an inverted condition.
We talked about logical inversion
in our earlier lesson about Boolean logic.
What's happening here is a logical inversion.
It's saying so long as there
are items in the list, keep printing.
When it becomes false,
when there are no more items left to print,
then you'll continue the program
and end the execution of this loop.
That's just important to understand, again,
all that's happening here is a piece
of Boolean logic being evaluated every time
this loop is run until such time as it
evaluates to false and then the loop terminates.
Here's another example for i,
where i is each number between zero and 100, print i.
This is also similar to Python,
though not exactly the same for reasons we'll
discuss when we talk about range in a later video.
But for i and the range 100,
so basically the numbers between zero
and 100, you will print i.
We're going to start out Python.
We don't have to set this value.
Python is going to assign that value for us.
So i is going to be set to
zero at the beginning of this loop.
The logic will be evaluated.
This is our case of Boolean logic,
is i less than or equal to 100?
Of course, in this case, it is because zero
is substantially smaller than 100.
It evaluates to true and we
perform the logic underneath it.
We print i and then we increment i.
Now the way this for loop is designed,
the way it's constructed, it will do
all of the incrementation for us.
We don't have to pay any attention to that,
the for-loop is covering.
We see that i is one,
which of course is less than 100,
we continue our evaluation.
This is going to happen for another 100 loops until
finally we reach i equals 101.
Now it's important to know that with
the list that we were evaluating in the last slide,
it didn't actually magically know,
that was all of the items I've done.
It had to first get to a condition where
the evaluation turned out to be false.
In this case, we see that 101 is still greater than 100.
Therefore, it is going to evaluate to false,
and therefore, we're going to
end the execution of our for loop.
Here's a knowledge check. I'm going to
let you try and do this one at home.
For i in range 10, again,
evaluate that the same way we did with range 100.
If you already know Python,
you'll know why that's a little bit off.
Don't worry about it.
Also, if you already know Python,
maybe starting the Python lessons.
Anyway, [NOISE] for i in range 10,
print i times 2.
Go ahead and pause this video and take
a couple of seconds to give it a shot.
You're either back because you figured it
out or your back because you gave up or
you just watched me sit very still for a
second as my dog snored in the background.
Either way, we're now going to look at what happens.
I is going to start out equaling 0,
of course, and therefore we're going to
perform our operation against the Number 0.
Print i times 2,
of course 0 times 2 is 0.
Then we're going to move on to one.
Now 1 times 2 obviously is 2,
so we're going to print that.
Here we're combining the two concepts we talked about
earlier of performing a piece of logic to
every item in a set and incrementing a number.
As we increment this number,
we're going to multiply it by two
each time until finally,
we get to the last number,
which of course is going to be 10 in this case.
We print 10 times 2,
we say, "We're done."
Then it's going to evaluate under the hood.
This is all happening in Python.
You're not seeing any of it.
Under the hood is going to value it and say,
the next Number 11 is not actually a valid case for this,
the conditional is false and we're done executing.
That's all there is to for loops.
That's the lesson. In this lesson again,
we talked about the concept of looping.
We talked about what looping is used for,
and we examined a few different implementations
of a for loop.
In our next lesson when we come back,
we're going to talk about another type of loop
called a while loop.
I mentioned that one a few times in this lesson,
but when you come back, we're going to spend
a little bit of time really digging into it.
I want to thank you all for being a part of this course.
I want to thank you for watching,
and of course I look forward to
having you back for our next lesson.
As always, I'm your instructor Joe Perry,
and thank you for watching this on Cybrary OnDemand.