For-loop example

This example shows you how a for loop works.

A common technique to do something a certain number of times is to use the range() function. The simplest way to use range() is to pass it a single number, and then it returns a list of numbers starting at zero and ending one less than the number you passed. For example:


            >>> range(5)
            [0, 1, 2, 3, 4]
        

There are five numbers in the returned list, so if that's used to power a for loop, it'll loop through five times.

Use the Next button to step through the code one line at a time. The current line of code is highlighted so you can see where you are in the process, and the box to the right of the code shows you the current values of the two variables (a value will briefly turn to red when it's changed).

If the box under the Next button is checked then all occurrences of i and j in the code will also be replaced with their respective values in order to make the comparisons more explicit.

Variables
i
j

Replace the variables n and j in the code with their respective values

Output: