1.5 For Loops - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

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