Practice CornerNested Loops

Nested Loops

Nested loops are loops inside loops. They allow us to perform operations on multi-dimensional data structures or execute repeated actions within an iterative process. Practice these problems to strengthen your understanding of nested loops.

Practice Problems

Problems Index

  1. Print a square pattern of * with n rows and columns.
  2. Print a right-angled triangle pattern of *.
  3. Generate a multiplication table up to n.
  4. Create a pyramid pattern of numbers.
  5. Print the following pattern for n:
    1
    12
    123
    1234
  6. Input a list of lists and calculate the sum of all elements.
  7. Print all pairs of elements from two lists.
  8. Find the transpose of a 2D matrix.
  9. Print a pattern where each row contains numbers from 1 to the row number.
  10. Generate a checkerboard pattern with alternating 0 and 1.

Try It Yourself

Now that you’ve reviewed the problems, practice them below using the code runner!

Pyground

Play with Python!

Output:

Solutions

Solutions

  1. Print a square pattern of * with n rows and columns.

    Show Code
    n = int(input("Enter the size of the square: "))
    for i in range(n):
        for j in range(n):
            print("*", end=" ")
        print()
  2. Print a right-angled triangle pattern of *.

    Show Code
    n = int(input("Enter the height of the triangle: "))
    for i in range(1, n + 1):
        print("*" * i)
  3. Generate a multiplication table up to n.

    Show Code
    n = int(input("Enter the table size: "))
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            print(f"{i * j:4}", end=" ")
        print()
  4. Create a pyramid pattern of numbers.

    Show Code
    n = int(input("Enter the height of the pyramid: "))
    for i in range(1, n + 1):
        print(" " * (n - i) + " ".join(str(x) for x in range(1, i + 1)))
  5. Print the following pattern for n:

    1
    12
    123
    1234
    Show Code
    n = int(input("Enter the number of rows: "))
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(j, end="")
        print()
  6. Input a list of lists and calculate the sum of all elements.

    Show Code
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    total = 0
    for row in matrix:
        for num in row:
            total += num
    print("Total sum:", total)
  7. Print all pairs of elements from two lists.

    Show Code
    list1 = [1, 2, 3]
    list2 = ['a', 'b', 'c']
    for item1 in list1:
        for item2 in list2:
            print(f"({item1}, {item2})")
  8. Find the transpose of a 2D matrix.

    Show Code
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    transpose = []
    for i in range(len(matrix[0])):
        row = []
        for j in range(len(matrix)):
            row.append(matrix[j][i])
        transpose.append(row)
    print("Transpose of the matrix:", transpose)
  9. Print a pattern where each row contains numbers from 1 to the row number.

    Show Code
    n = int(input("Enter the number of rows: "))
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(j, end=" ")
        print()
  10. Generate a checkerboard pattern with alternating 0 and 1.

    Show Code
    n = int(input("Enter the size of the checkerboard: "))
    for i in range(n):
        for j in range(n):
            print((i + j) % 2, end=" ")
        print()