What happens once a for loop has finished executing.
Usually you will want to summarize a block of output or move on to other work that your program must
accomplish.
Any lines of code after the for loop that are not intended are executed once without repetition.
So let's write a thank you to a group of group of car makers as a whole and thanking them for putting
on an excellent cars.
So to display this group of message after all of the individual messages have been printed, we place
them.
Thank you message after the for loop without the indentation here.
So now we will write print and thank you every car makers.
These cars are special and great.
And here now let's run this and you will see all this message is just executed once.
So the first two cars to print are repeated once for each car makers in the list, as you saw earlier.
However, because the last line is not intended, it's printed only once.
If we add indentation to it, you will see, as you can see here, Thank you.
Every car makers, thank you, every car makers, thank you, every car makers and so on.
So we will delete this indentation and we will get this thank you message only once because it's not
included in this loop because of the indentation we didn't put before the print here.
So when you're processing data using a for loop, you will find that this is a good way to summarize
an operation that was performed on an entire dataset.
For example, you might want to use a for loop to initialize a game by running through the list of characters
and displaying each character on the screen.
You might then write some additional code after this loop that displays a play button.
After all the characters have been drawn to the screen.
Python uses indentation to determine how a line or group of lines is related to the rest of the program.
In the previous in this examples the lines that printed messages to individual car makers were part
of the for loop because they were indented.
So python's use of indentation makes code very easy to read.
Basically, it uses whitespace to force you to write neatly formatted code with a clear visual structure.
In longer Python programs, you will notice block of code indented at a few different levels.
These indentations levels help you gain a general sense of the overall program's organization.
So as you begin to write code that relies on proper indentation, you will need to watch for a few common
indentation errors.
For example, people sometimes indent lines of code that don't need to be indented or forget the indented
lines that need to be indented.
So seeing examples of these errors now will help you avoid them in the future and correct them when
they do appear in your own programs.
So now let's examine some some of the more common indentation errors so you can also forget I here indent
so always indent a line after the fourth statement in a loop.
If you forget, Python will not remind you here.
And as you can see here, expected and indented.
Block.
So here.
If we write, for example car makers and just let the segmentation Eleanor also so the let's actually
delete this and I will explain all of this.
So the call to print should be indented.
So but it's not here.
So when Python expects an identity indented block and doesn't find one, it lets you know which line
it had problem with.
And if we run this code again and let's run it here, and as you can see, we have a indentation error
at line three.
So you can usually resolve these kinds of annotation errors by indenting the lines or lines immediately
after the for statement.
You can also forget things or forget to indent additional lines.
So sometimes your loop will run without any errors but won't produce the expected result.
So this can happen when you are trying to do several tasks in a loop and you forget to indent some of
its lines.
For example, this is what happens when we forget to indent the second line in the loop.
And here.
Do here.
We will not get any error here.
And now we'll if we run this and as you can see here, at List Object has no attribute title.
As a result, the first print.
And if if makers here this code will end as you can see.
Let's.
I'll make ups.
I'll make.
This code will run without a problem if you just edit it.
Edit this and if we change this to car make.
And we will get this as as well.
So here.
The.
As the result here, the first printf call is not indented, is indented and.
So the first that's why first print call is executed once for each name in the list because it's indented.
But here the second print call is not indented, so it's executed only once after the loop has finished
running because the final value associated with with Volkswagen here and Volkswagen is the only one
who receives the looking who receives the.
Thank you, Eric.
Carmakers.
These cars are special and correct.
And Volkswagen, only one who gets the miles per gallon is high on Volkswagen.
And here this is a logical error.
This syntax is valid python code, but the code does not produce the desired result because a problem
occurs in its logic.
So if you expect to see certain action repeated once for each item in place and it's executed only once,
determine whether you need to simply indent a line or group of lines.
So here that's.
Now we are getting this error here because in for loop, if you have a for loop here for now, let's
print message will execute it once.
But your program firstly execute your for loop again, again and again.
And then after completing the for loop it will go to next lines here.