Python Nested Loops
Python programming language allows using one loop inside another loop. The following section shows a few examples to illustrate the concept.
Syntax of Python Nested for Loop
The syntax for a nested for loop statement in Python programming language is as follows:
for iterator_var in sequence: for iterator_var in sequence: statements(s) statements(s)
Syntax of Python Nested while Loop
The syntax for a nested while loop statement in Python programming language is as follows:
while expression: while expression: statement(s) statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.
Python3
from __future__ import print_function for i in range ( 1 , 5 ): for j in range (i): print (i, end = ' ' ) print () |
Output:
1 2 2 3 3 3 4 4 4 4
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements.