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:
- First (outer) loop iterates once
- Then second (inner) loop iterates all possibilities
- Once second loop reaches its condition
- First loop iterates once more
- 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:
- Num1 is 1 and outer loop prints the output 1.
- Inner loop starts with num2 as 0 and prints num1 (1) times num2 (0), which equals to 0.
- Num2 increments by 1 and now equals 1.
- Inner loop iterates again until num2 is more than 2 (as condition is num2 <= 2).
- Num1 (1) * num2 (1) outputs a 1.
- Num2 increments by 1 and becomes 2.
- Condition is still True.
- Inner loop iterates again. Num1 (1) * num2 (2) equals to 2 now.
- With the next iteration, num2 becomes 3, and condition becomes False.
- Therefore, the second loop stops and the first continues.
- First (outer) loop continues with the incremental of num1 by 1.
- Num1 becomes 2. Outer loop prints the results as 2.
- But it also defines (resets) num2 to 0.
- Outer loop still meets the condition of True (as num1 <= 2)
- Inner loop starts again, with num2 equaling 0.
- Inner loop iterates and prints num1 (2) * num2 (0), outputting 0.
- Num2 increments to 1. Inner loop iterates again.
- Inner loop results the num1 (2) * num2 (1) as an outcome of 2.
- Inner loop iterates again, incrementing num2 to 2.
- Inner loop prints num1 (2) times num2 (2) as 4.
- Next iteration of inner loop switches the condition to False.
- Therefore, back to first (outer) loop.
- Outer loop increments num1 to 3, which switches num1’s condition to False.
- And so the outer loop stops executing.
Next: Python Random