As it says in the header, FOR LOOP is used when we want to execute block of code statements again and again until a condition is met, we can use For loops.
To be very specific, to iterate any collections, like a list, tuples, etc. we can use For loops.
Example: Let’s understand FOR Loop with an example. Let’s try to create a list and try to print all the values in the list using FOR loop.

In the above code, colors have the list of values i.e., red, blue and green.
In the second line, for loop is used which will take each value from the colors list and feed into x variable.
In the last line, we are printing the value of x variable which contain the value of colors list and this gets repeated until the values in the colors list are exhausted i.e., this code will run for 3 times as there are three values in the colors list.
Output:

The code ran for 3 times which is equal to the number of values in the colors list.
For Loop with Condition:
Now, let’s assume that we need to print only when a specific condition is met i.e., print only when the color is blue.
Example:

I have added an if condition which will check if the x variable contains the value we need, i.e., blue.
Print statement will be executed only if the above condition is met.
Output:

In the output, it only printed when the color is blue. Hence, print statement doesn’t gets executed if the condition is not met.
Likewise, we can use for loop with other keywords, like if, while, etc. based on our requirement.
I hope this would have given a basic idea about FOR Loop.
Thanks for subscribing my blog. Meet you soon with my next post. π Stay Safe. π