Python Tutorial-For Loop

A For Loop is used when you want to run through a code until a condition is met. For example, print Hello 10 times. For Loops are often used in conjunction with Lists which we have not discussed yet. For this tutorial I will just explain how a For Loop works with a couple of simple examples.

Here is a simple example of a For Loop:

You should have gotten this:

For Loop Example

Let’s talk about what happened here. First of all we set the variable myLoop to a range of 1 to 10. Then we told Python to print myLoop which resulted in Python outputting 1, 2, 3, 4, 5, 6, 7, 8, 9. But where’s the 10?

When we set the range we are telling Python that the loop runs UP TO 10. So the loop does not include 10. Go ahead, try it with a different range of numbers.

That’s it on For Loops for now but we will revisit them in a future tutorial.