(Ambient Music)
- There are two forms of looping statements
that you can write with Python.
And these are useful for executing
the same tasks repeatedly.
They're known as while loops and for loops.
Let's take a look at each one.
The while loop is used
when you want to execute a block of code,
as long as the condition remains true.
I'm going to start by creating a variable called A,
and I'm going to set it the initial value to one.
Then we'll start the while loop and say that
while A is less than 10 and this sets up the condition.
So as long as the value of a is less than 10,
then the following lines will execute.
We move into the loop block
with a colon and then indent, the next lines, two spaces.
So as long as A is less than 10
I want to print the current value of A
to the messages window.
Then I want to increase the value of A by one.
We can do that on the next side
by typing in A is equal to A plus one.
And that's all there is to the while loop.
Python doesn't use the same begin and end statements
that you would use when writing a while loop
in transact sequel.
And the entire block is defined strictly
by the indenting of these two lines.
So make sure that you insert the same number
of spaces for each one.
It doesn't necessarily have to be two spaces I have here
at the beginning of each of these lines.
It could be three or four.
It doesn't really matter.
As long as these lines are indented the same.
If I make one of these lines indented four spaces
and leave the other one set to two spaces, then
if I try and execute this, you're going to see what happens.
I'll get this error message.
And in fact, if you read through the output message
you're going to see specifically
that it's an indentation error.
So I'm going to go ahead
and fix the indentation and run it again.
This time the code is successfully run
and I get the output numbers one through nine.
Once the value of A is equal to 10
the while condition is no longer true.
A is not less than 10.
So the loop exits and the print
and increment lines are no longer executed.
So that's a while loop.
The other loop that I want to take a look
at is called a for loop.
This method is used when you want a block
of code to execute a fixed number of times.
To do this, you'll use an iterating variable
which you can name whatever you want.
I'm just going to say for any variable, X,
this variable will automatically start
at a value of zero and increment each time the loop runs.
Then as long as this variable is in range of zero
and let's say three, they loop will execute.
Just like with the while loop,
you enter the block with a colon
and then indent all of the following lines in the block,
the same amount.
For this I'm simply going to print the text, hello world.
I'll press the execute button
and take a look at the results.
And it looks like I forgot the closing parentheses here
and I print command, execute it again.
And this time it runs and I get the text
Hello world, repeated three times.
So by this, you can tell
that the range specified up above is not inclusive.
The for loop repeats, as long
as the iterating variables value is equal
to the first number, but less than the second.
We can see it this clearer by printing the current value
of X instead of the text, hello world
I'll change it to say print X and then execute again.
This shows that the first time through the loop
the value of X was zero.
Then it was one.
Then two, then X is no longer in the specified range,
so the loop stops.
Another thing that we can do
with a for loop is to iterate over the number
of items in a group rather than within a numeric range.
So instead of the range, zero to three,
I could do something a little different.
Instead I'll specify a range of in the text, hello world.
Now X represents each letter in the text string.
And when I execute the statement and returns each letter
in sequence.
Or I can provide a list of Python items.
Python uses square brackets to denote a list.
Let's go ahead and get rid of this text, hello world, here.
And I'll type in a list of colors.
We'll do that by opening up a square bracket
and then each color name wrapped in double quotation marks.
I'll separate each list item with a comma.
At the end of the list, I'll close the square bracket.
So now I have your list
with four colors, red, blue, green, and purple.
I'm going to iterate over this list
and print out each list item in sequence.
I'll press the execute button
and we'll get the results down below.
So those are the basics
of using loops in your Python scripts.
The while loop is useful when you want to
test a condition each time and repeat the loop.
As long as the condition remains true.
And the for loop for a repeated
block of code a specific number of times.
(ambient music)