Skip to content
Permalink
874e7d5550
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

For Loops

{{ todo("Write intro specifically to for-loops") }}

Iterating iterables

For loops always start with the keyword 'for' and then an iterator followed by some condition. Here are a few examples:

{{ code_from_file("for-loops/example-1.py", flavor = "python") }}

In this first case, we define a sting of text we poignantly called string. Then we create the 'for' loop by stating that for each index (i) inside the string, we want to print out the value of 'i'. It is common practice in coding to use 'i' as the variable that iterates through something in a loop or for an index location for something iterable. The output of this code is that each letter is looked at by the 'i' in the for loop and then printed out to a new line in the console.

{{ code_from_file("for-loops/example-2.py", flavor = "python", execute = True) }}

In this example, we have a more pythonic idea. The word 'letter' works the same way as the 'i' in the previous example, it's just more descriptive which helps with clarity when rereading later. See if you can work out what this program does without running it.
Other than strings you can also iterate through any type of iterable including: dictionaries, lists, tuples and sets. Here are some examples:

{{ code_from_file("for-loops/example-3.py", 3, 6, execute = True) }}

{{ code_from_file("for-loops/example-3.py", 8, 11, execute = True) }}

{{ code_from_file("for-loops/example-3.py", start = 13, execute = True) }}

Iterating Ranges

Using the range function, we can also create 'for' loops that iterate a number of times. Consider these examples:

{{ code_from_file("for-loops/example-4.py", 3, 5, execute = True) }} {{ code_from_file("for-loops/example-4.py", 7, 9) }} {{ code_from_file("for-loops/example-4.py", 11, 13) }}

In these examples, you can see how the range function is used to set a maximum limit to the number of times the 'for' loop iterates. Notice also that if you run the first example, count never makes it to 10, the numbers 0 all the way up to 9 are printed instead.

{{ code_from_file("for-loops/example-4.py", 15, 20) }} {{ code_from_file("for-loops/example-4.py", 22, 24) }}

In these two examples, it's slightly more complicated to work out the integer after the calculation, but we know it will be a finite number. See if you can work out what these programs will print out without running them.

Nested Loops

All loops can also be nested inside each other consider this example:

{{ code_from_file("for-loops/example-5.py", 1, 8) }}

In this example we iterate over two separate lists and join them together in the print statement. In the outside 'for' loop, x runs through each of the items in the variable 'adjective', then the inner 'for' loop is called and y iterates through the 'fruits' variable. When the outer loop gets to the first index, it runs the inner loop until it completes all it's iterations and then x moves onto the next index. In our example this means that 'x' first has the value 'red', then 'y' iterates through each of the 'fruits' list printing out 'red apple', 'red banana' and 'red cherry' first... Run the code for yourself and see.
To see how many times an inner loop will run, you can multiply it by all the possible different options of the outer loops and the inner loop to find out. Consider this example:

{{ code_from_file("for-loops/example-5.py", 11) }}

If you run this code, you can see that the code runs 36 times and that that's exactly the same number of times as each of list lengths multiplied together. There are some other niche cases you may see in loops, one of which is the 'else' statement.

{{ code_from_file("for-loops/example-6.py", 1, 19) }}

When the loop finishes, anything in the 'else' statement is executed. If the loop is broken out of or fails to execute, the 'else' statement doesn't execute. Try this for yourself:

{{ code_from_file("for-loops/example-6.py", 22, 39) }}

Test Your Knowledge: Countdown

See if you can create a loop that can count down from 10, and that once it gets to zero it prints the the word 'Liftoff!'. See if you can also make it print that the rocket was successfully launched.