Python

Python Advanced Loops

Python Advanced Loops

This page covers Python advanced loops and continues the previous tutorial of Python intermediate loops.

For Python intermediate loops here.

It continues deeper aspects of loops.

  • “else” statement
  • nested “while” loops

“Else” statement

The “else” statement executes code, once the loop finishes.

This means that the statement operates after all iterations of the loop.

Please note that the “else” statement does not produce anything if the loop stops in advance (e.g. “break” statement).

The following is an example of a “for” loop.

someList = ["apple", "red", "car"]

for item in someList:
    print(item)
else:
    print("The list is finished.")
# Output:
apple
red
car
The list is finished.

Below is an example of a “while” loop.

someTuple = (1, 2, 3, 4)
num = 0

while num != 4:
    for n in someTuple:
        print(n)
        num += 1
else:
    print("The loop finished.")
# Output:
1
2
3
4
The loop finished.

If the loop stops in the middle of execution, the “else” statement does not produce anything.

The following implements a “break” statement inside the “for” loop.

someList = ["apple", "red", "car"]

for item in someList:
    print(item)
    if item == "red":
        break
else:
    print("The list is finished.")
# Output:
apple
red

The loop breaks at the word “red”, and therefore the “else” statement does not execute.

Nested “while” loops

In Python, nested “while” loops refer to the same idea as nested “for” loops.

And as such, a loop executes inside another loop.

Below is an example of a nested while loop.

num1 = 1
while num1 <= 2:
    print(num1)
    num2 = 0
    while num2 <= 2:
        print(num1 * num2)
        num2 += 1
    num1 += 1
# Output:
1
0
1
2
2
0
2
4

The operation behind the nested “while” loops is as follows:

  1. First (outer) loop iterates once
  2. Then second (inner) loop iterates all possibilities
  3. Once second loop reaches its condition
  4. First loop iterates once more
  5. The whole cycle repeats until the outer loop reaches its condition

Let’s add a simple “print” message statement. This will create a better understanding of the process.

num1 = 1
while num1 <= 2:
    print(num1)
    num2 = 0
    while num2 <= 2:
        print(num1 * num2, "inner loop")
        num2 += 1
    num1 += 1
# Output:
1
0 inner loop
1 inner loop
2 inner loop
2
0 inner loop
2 inner loop
4 inner loop

Again, outer loop iterates once, second loop executes all possible iterations. And the cycle repeats.

The above workings:

  1. Num1 is 1 and outer loop prints the output 1.
  2. Inner loop starts with num2 as 0 and prints num1 (1) times num2 (0), which equals to 0.
  3. Num2 increments by 1 and now equals 1.
  4. Inner loop iterates again until num2 is more than 2 (as condition is num2 <= 2).
  5. Num1 (1) * num2 (1) outputs a 1.
  6. Num2 increments by 1 and becomes 2.
  7. Condition is still True.
  8. Inner loop iterates again. Num1 (1) * num2 (2) equals to 2 now.
  9. With the next iteration, num2 becomes 3, and condition becomes False.
  10. Therefore, the second loop stops and the first continues.
  11. First (outer) loop continues with the incremental of num1 by 1.
  12. Num1 becomes 2. Outer loop prints the results as 2.
  13. But it also defines (resets) num2 to 0.
  14. Outer loop still meets the condition of True (as num1 <= 2)
  15. Inner loop starts again, with num2 equaling 0.
  16. Inner loop iterates and prints num1 (2) * num2 (0), outputting 0.
  17. Num2 increments to 1. Inner loop iterates again.
  18. Inner loop results the num1 (2) * num2 (1) as an outcome of 2.
  19. Inner loop iterates again, incrementing num2 to 2.
  20. Inner loop prints num1 (2) times num2 (2) as 4.
  21. Next iteration of inner loop switches the condition to False.
  22. Therefore, back to first (outer) loop.
  23. Outer loop increments num1 to 3, which switches num1’s condition to False.
  24. And so the outer loop stops executing.

Next: Python Random

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.