16.python_while_loop
16.python_while_loop
With the while loop we can execute a set of statements as long as a condition is
true
Example
Print i as long as i is less than 6
In [1]: i = 1
while i < 6:
print(i)
i += 1 # as same as i = i + 1
1
2
3
4
5
The while loop requires relevant variables to be ready, in this example we need to
define an indexing variable, i, which we set to 1
1
2
3
1
2
4
5
6
1
2
3
4
5
i is no longer less than 6
outer_count += 1
Outer count: 1, Inner count: 1
Outer count: 1, Inner count: 2
Outer count: 1, Inner count: 3
Outer count: 2, Inner count: 1
Outer count: 2, Inner count: 2
Outer count: 2, Inner count: 3
Outer count: 3, Inner count: 1
Outer count: 3, Inner count: 2
Outer count: 3, Inner count: 3