- [Instructor] In this video,
we're going to consider a couple of Python loops.
We have a for loop and a while loop.
First, let's consider the for, F-O-R loop.
With a for loop, we can do something
for as long as we have a value in a list variable.
Remember the list variable?
We had those straight brackets
with values inside separated by commas,
we saw that in a previous video.
But let's say for example, we had a variable of inventory
that equals in straight brackets,
and these are strings or use quotes.
We have a catalyst 2960 close quote
comma separator, in quotes I've got a catalyst
3750 close quote, comma,
I've also got, we'll see a 2911 router,
close quote and a comma
and let's say we've got a Cisco 7965GIP phone,
close quote comma,
let's do one more.
Let's say that we've got
a Cisco Unified Communications Manager server,
close quote and let's close our brackets.
We've now populated this value of inventory.
And we can see the items one at a time using a for loop.
Because we're going to do a for loop
for as long as we have a value in that list variable.
The first time through the loop,
we're going to be looking at the first value,
which is a catalyst 2960.
The next time we go through the loop,
we'll be looking at a catalyst 3750.
Let me show you.
I can say for, item
by the way item is just a variable name that I'm using.
I'm saying for item in inventory.
And we use a colon like we did
with the if statement.
And this says we going to be going through
this list variable named inventory.
And the first time through we going to assign
the first value of catalyst 2960.
We're going to assign that to the variable called item
and then we're going to print it,
we'll say print, and we'll say item.
Let's press Enter a couple of times.
And we looped through inventory
for each of those values.
And we printed them each time.
And once we ran out of values,
we exited the loop.
In fact, while we're looping through that,
we might want to combine that logic
with the if logic we saw in a previous video,
here's what I mean.
I can say, for item in inventory,
like we did before, Colin,
instead of just printing out whatever that is,
I'm going to say, if it contains catalysts, then print it out.
If I just want to see what's in catalyst,
I'll say if and in quotes,
catalyst, close quote,
if that string is in whatever the item value is now,
we'll do a colon,
then I want to print it, I'll say print
parentheses item.
And it prints just those items,
or those values in our list variable
containing the word catalyst.
In fact, we could use this to populate another variable,
we could populate a list variable
that just contained our catalyst switches.
For example, I can say I've got a new variable
called catalyst switch,
and it equals right now it's going to be a list variable.
It's empty right now.
So I'm just going to give an open and close brackets,
and we'll press Enter.
And then we'll go through this for loop again.
I'll say, for item in inventory,
just like we did before.
And I'm going to say if the current item contains
the word catalyst, just like we did before,
if that's contained in the current value of item,
instead of just printing it,
here's what I want to do.
I want to append to the catalyst switch variable
that I created that list,
I'll say catalyst, switch dot append,
whatever that item value is,
and we'll press Enter a couple of times.
And let's see what we now have in this list
named the catalyst switch,
I'll just say, catalyst switch.
And notice we have in brackets separated by commas,
the strings of catalyst 2960, and 3750.
We were able to populate a list variable by saying,
if the current item in inventory has catalyst in it,
let's put that as a value in this other list.
That's a look at the for loop.
And again, that does something
for as long as we have a value in a list variable.
Next, let's take a look at a while loop,
which is going to loop while a condition is true.
And let's do this with a program.
I've already got a program started over here
in the left hand pane and I've called it Loops.PY,
and I'm going to say that X equals something
that the user is going to give me,
I'm going to ask the user to give me a number,
an integer to which they wish to count.
And I'm going to say, input,
we saw that in a previous video,
and in quotes, I'm going to say,
enter the number two, which you wish to count,
colon, space, close quote, close parentheses.
Now, here's the challenge,
and we saw this in a previous video,
when we ask the user for input like this,
if they type in a number,
even though it's a number,
and it's an integer,
it's going to be seen as a string,
we need to convert it to an actual integer for our purposes.
Remember how we convert data types.
If I want to convert something into an integer,
I'm going to use the INT function.
So I'm going to say that my value of X equals
the INT with X in parentheses
which means whatever that string was of X originally,
I'm going to make that value an integer.
And now we going to start counting beginning at one,
and let's create a counting variable.
Let's use Y, and we going to start at one.
So I'll say Y equals one.
And remember the while loop is going to loop
while a condition is true.
So let's specify that condition.
I'm going to say while Y is less than or equal to X,
and we have to end this with a colon like
the if statement we saw earlier.
While that's true, then let's print the value of Y,
but if we just left it at this, what would happen?
It would always be true,
Y would just equal one forever
and we would just keep printing one, one, one, one, one.
So what we want to do is every time we loop through this,
we want to increment this counting variable
of Y we want to increment it by one.
How do I make Y one more than it currently is?
I'm going to say that
Y equals Y plus one.
And let's save that.
I want to run that,
It asks for a number to which I wish to count,
I'll say seven and notice it counts all the way to seven,
and it stops.
Let's follow the logic.
When I entered seven, that number seven was a string,
I converted it to an integer
by saying X equals INT with X and parentheses,
so now X equals the integer of seven.
And I started counting with
a value of one, we had Y equals one,
and I said while Y is less than or equal to X,
then I want to print the value of Y.
Well, initially Y equals one,
we just set it to one,
and I'm going to print one,
but then I say Y equals Y plus one,
well, Y is currently one,
so Y equals one plus one.
Now Y equals two.
And since that condition was true before,
we go through the while loop again,
and I'm going to say while this time
two is less than or equal to X,
then I'll print the current value of Y.
And X is what we typed in.
That was a seven in our case.
So yeah, two is less than or equal to seven.
After we get all the way up to seven,
and we say Y equals Y plus one,
then suddenly Y equals eight.
And when we evaluate that while statement the next time,
is eight less than or equal to seven,
no, it's not.
So we don't execute what's in the while loop.
Let's take a look at a variant of this.
Let me show you how we can break out of a loop
if we want to.
We'll still leave the first few lines as they are,
but let's set a while condition
that's always going to be true.
In fact, I'll use the Boolean value type of true,
which is always true.
And if this condition is true,
and it's always going to be true,
then I'm going to print the value of Y
I'll continue to increment the value of y
by one each time, but I can do my evaluation here.
I can say if the value of Y is now greater than X,
then I want to stop counting.
And I'll do a colon because this is an if statement.
And my response to this condition of Y being greater than X,
is to break out of this loop using the break command.
So you see what's happening here,
the first time through Y equals one,
I'll enter 10, for example,
as our integer value, and Y is going to be one,
then we're going to increment it,
It's going to be two and we keep evaluating each time
is Y greater than X?
Well, Y is not going to be greater than X
until we say that Y is 10,
and we print 10,
then when we say Y equals Y plus one, it becomes 11.
When we run that evaluation that if statement
when Y equals 11, we going to break out
so it's not going to print 11,
it will break out before we loop back and print it out.
So let's save and run this.
Let's do a save and let's run it,
and also we want to count to 10,
counts all the way to 10 and it stops.
That's a way that we can break out of a while loop
that is always true.
And in this video,
just to sum up, we've taken a look at a couple of ways
that we could loop
through some logic that might be printing something out,
we might be (murmurs) something to a variable,
lots of things we can do while we're looping.
We saw how to count how to increment,
a variable by one each time.
And we had two types of loops
that we talked about in this video.
We had a for loop,
which would loop for as long
as there is a value in a list variable,
and we took a look at a while loop,
which would loop while a particular condition was true.