Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

What is Nested Loops in Python?

Nested Loops in Python

Condition for Nested Loops in Python

  • Description: Nested loops in Python refer to a loop inside another loop. The inner loop is executed completely for each iteration of the outer loop. This structure is used to handle complex iterations, such as working with matrices or multi-dimensional data.
Step-by-Step Process
  • Nested loop use loop inside another loop
  • Outer Loop: The outer loop begins and executes its body.
  • Inner Loop Executes: For every single iteration of the outer loop, the inner loop runs through all its iterations.
  • Repeat: The process continues until all iterations of the outer loop are completed.
Sample Code
  • #Nested for loop
    rows = 5
    print("Nested for loop output:")
    for i in range(1, rows + 1):
    for j in range(1, i + 1):
    print(j, end=" ")
    print()
    #Nested While loop
    i = 2
    print("Nested while loop output:")
    while i <= 2:
    j = 1
    while j <= 10:
    print(f"{i} x {j} = {i * j}")
    j += 1
    i += 1
Screenshots
  • Nested Loops in Python