Skip to content

Python nested for | Example code

  • by

The for loop is used to iterate over a sequence such as a list, string, tuple, or other iterable objects such as range. Using a for loop inside a for a loop is called a Python nested for.

for x in sequence:

  for y in sequence:

The outer loop can contain more than one inner loop without limitation on the chaining of loops.

Python nested for example

Simple example code for Nested loop to print multiplication table in Python

for i in range(0, 5):

    for j in range(0, 5):
        print(i * j, end=' ')


Python nested for

Another example

Nested for Loop to Print Pattern.

rows = 5
# outer loop
for i in range(1, rows + 1):
    # inner loop
    for j in range(1, i + 1):
        print("*", end=" ")


* * 
* * * 
* * * * 
* * * * * 

Do comment if you have any doubts or suggestions on this Python nested topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *