You're printing a 10×10 multiplication table. For each row (1-10), you need to print 10 products (row × 1, row × 2, ... row × 10). The outer loop picks the row; the inner loop fills each column.

Multiplication table

Generate a multiplication table using nested loops.

size
multiplication.py
Replay: real traced execution (multi-file project)
size = 5

# Print header
print("   ", end="")
for i in range(1, size + 1):
    print(f"{i:4d}", end="")
print()
print("   " + "-" * (size * 4))

# Print table
for row in range(1, size + 1):
    print(f"{row:2d} |", end="")
    for col in range(1, size + 1):
        print(f"{row * col:4d}", end="")
    print()


size = 3

# Print header
print("   ", end="")
for i in range(1, size + 1):
    print(f"{i:4d}", end="")
print()
print("   " + "-" * (size * 4))

# Print table
for row in range(1, size + 1):
    print(f"{row:2d} |", end="")
    for col in range(1, size + 1):
        print(f"{row * col:4d}", end="")
    print()


size = 10

# Print header
print("   ", end="")
for i in range(1, size + 1):
    print(f"{i:4d}", end="")
print()
print("   " + "-" * (size * 4))

# Print table
for row in range(1, size + 1):
    print(f"{row:2d} |", end="")
    for col in range(1, size + 1):
        print(f"{row * col:4d}", end="")
    print()


  1. size ← 5

    1size→ 5 = 5  #@size=3, 1023# Print header4print("   ", end="")5for i in range(1, size + 1):
    output   
  2. for i in range(1, size + 1):

    pass 1 of 5
    4print("   ", end="")5for i1 in range(1, size5 + 1):6    print(f"{i1:4d}", end="")7print()
    output   1
    All 5 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
  3. print(" " + "-" * (size * 4))

    6    print(f"{i:4d}", end="")7print()8print("   " + "-" * (size5 * 4))
    output   --------------------
  4. for row in range(1, size + 1): #?outer_loop

    pass 1 of 5
    10# Print table11for row1 in range(1, size5 + 1):  #?outer_loop12    print(f"{row1:2d} |", end="")13    for col in range(1, size + 1):  #?inner_loop
    output 1 |
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  5. for col in range(1, size + 1): #?inner_loop

    pass 1 of 25
    12print(f"{row:2d} |", end="")13for col1 in range(1, size5 + 1):  #?inner_loop14    print(f"{row1 * col1:4d}", end="")15print()
    output   1
    25 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    441
    551
    612
    722
    832
    942
    ⋯ 14 more passes ⋯
    2445
    2555
  6. print()

    14    print(f"{row * col:4d}", end="")15print()
  7. print()

    14    print(f"{row * col:4d}", end="")15print()
  8. print()

    14    print(f"{row * col:4d}", end="")15print()
  9. print()

    14    print(f"{row * col:4d}", end="")15print()
  10. print()

    14    print(f"{row * col:4d}", end="")15print()
  1. size ← 3

    1size→ 3 = 323# Print header4print("   ", end="")5for i in range(1, size + 1):
    output   
  2. for i in range(1, size + 1):

    pass 1 of 3
    4print("   ", end="")5for i1 in range(1, size3 + 1):6    print(f"{i1:4d}", end="")7print()
    output   1
    All 3 passes — pass 1 is the card above
    passi
    11
    22
    33
  3. print(" " + "-" * (size * 4))

    6    print(f"{i:4d}", end="")7print()8print("   " + "-" * (size3 * 4))
    output   ------------
  4. for row in range(1, size + 1):

    pass 1 of 3
    10# Print table11for row1 in range(1, size3 + 1):12    print(f"{row1:2d} |", end="")13    for col in range(1, size + 1):
    output 1 |
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  5. for col in range(1, size + 1):

    pass 1 of 9
    12print(f"{row:2d} |", end="")13for col1 in range(1, size3 + 1):14    print(f"{row1 * col1:4d}", end="")15print()
    output   1
    All 9 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    412
    522
    632
    713
    823
    933
  6. print()

    14    print(f"{row * col:4d}", end="")15print()
  7. print()

    14    print(f"{row * col:4d}", end="")15print()
  8. print()

    14    print(f"{row * col:4d}", end="")15print()
  1. size ← 10

    1size→ 10 = 1023# Print header4print("   ", end="")5for i in range(1, size + 1):
    output   
  2. for i in range(1, size + 1):

    pass 1 of 10
    4print("   ", end="")5for i1 in range(1, size10 + 1):6    print(f"{i1:4d}", end="")7print()
    output   1
    All 10 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
    66
    77
    88
    99
    1010
  3. print(" " + "-" * (size * 4))

    6    print(f"{i:4d}", end="")7print()8print("   " + "-" * (size10 * 4))
    output   ----------------------------------------
  4. for row in range(1, size + 1):

    pass 1 of 10
    10# Print table11for row1 in range(1, size10 + 1):12    print(f"{row1:2d} |", end="")13    for col in range(1, size + 1):
    output 1 |
    All 10 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
    88
    99
    1010
  5. for col in range(1, size + 1):

    pass 1 of 100
    12print(f"{row:2d} |", end="")13for col1 in range(1, size10 + 1):14    print(f"{row1 * col1:4d}", end="")15print()
    output   1
    100 passes — pass 1 is the card above
    passcolrow
    111
    221
    331
    441
    551
    661
    771
    881
    991
    ⋯ 89 more passes ⋯
    99910
    1001010
  6. print()

    14    print(f"{row * col:4d}", end="")15print()
  7. print()

    14    print(f"{row * col:4d}", end="")15print()
  8. print()

    14    print(f"{row * col:4d}", end="")15print()
  9. print()

    14    print(f"{row * col:4d}", end="")15print()
  10. print()

    14    print(f"{row * col:4d}", end="")15print()
  11. print()

    14    print(f"{row * col:4d}", end="")15print()
  12. print()

    14    print(f"{row * col:4d}", end="")15print()
  13. print()

    14    print(f"{row * col:4d}", end="")15print()
  14. print()

    14    print(f"{row * col:4d}", end="")15print()
  15. print()

    14    print(f"{row * col:4d}", end="")15print()

Outer loop: each row (1-10). Inner loop: each column (multiply by 1-10).

nested loop A loop inside another loop. Inner completes fully for each outer iteration.

Print rectangle of stars

Draw a rectangle pattern with asterisks.

example
rectangle.py
Replay: real traced execution (multi-file project)
width = 5
height = 3

print(f"Rectangle {width}x{height}:")

for row in range(height):
    for col in range(width):
        print("* ", end="")
    print()  # New line after each row

# Hollow rectangle
print("\nHollow rectangle:")
for row in range(height):
    for col in range(width):
        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

# Pythonic: string multiplication
print("\nUsing string multiplication:")
for row in range(height):
    print("* " * width)
width = 3
height = 3

print(f"Rectangle {width}x{height}:")

for row in range(height):
    for col in range(width):
        print("* ", end="")
    print()  # New line after each row

# Hollow rectangle
print("\nHollow rectangle:")
for row in range(height):
    for col in range(width):
        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

# Pythonic: string multiplication
print("\nUsing string multiplication:")
for row in range(height):
    print("* " * width)
width = 8
height = 3

print(f"Rectangle {width}x{height}:")

for row in range(height):
    for col in range(width):
        print("* ", end="")
    print()  # New line after each row

# Hollow rectangle
print("\nHollow rectangle:")
for row in range(height):
    for col in range(width):
        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

# Pythonic: string multiplication
print("\nUsing string multiplication:")
for row in range(height):
    print("* " * width)
width = 5
height = 2

print(f"Rectangle {width}x{height}:")

for row in range(height):
    for col in range(width):
        print("* ", end="")
    print()  # New line after each row

# Hollow rectangle
print("\nHollow rectangle:")
for row in range(height):
    for col in range(width):
        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

# Pythonic: string multiplication
print("\nUsing string multiplication:")
for row in range(height):
    print("* " * width)
width = 5
height = 5

print(f"Rectangle {width}x{height}:")

for row in range(height):
    for col in range(width):
        print("* ", end="")
    print()  # New line after each row

# Hollow rectangle
print("\nHollow rectangle:")
for row in range(height):
    for col in range(width):
        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

# Pythonic: string multiplication
print("\nUsing string multiplication:")
for row in range(height):
    print("* " * width)
  1. width ← 5, height ← 3

    1width→ 5 = 5   #@width=3, 82height→ 3 = 3  #@height=2, 534print(f"Rectangle {width5}x{height3}:")
    outputRectangle 5x3:
  2. for row in range(height):

    pass 1 of 3
    6for row0 in range(height3):7    for col in range(width):8        print("* ", end="")
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for col in range(width):

    pass 1 of 15
    6for row in range(height):7    for col0 in range(width5):8        print("* ", end="")9    print()  # New line after each row
    output* 
    15 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 4 more passes ⋯
    143
    154
  4. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  5. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  6. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  7. print(" Hollow rectangle:")

    11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):
    output
    Hollow rectangle:
  8. for row in range(height):

    pass 1 of 3
    12print("\nHollow rectangle:")13for row0 in range(height3):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for col in range(width):

    pass 1 of 15
    13for row in range(height):14    for col0 in range(width5):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:16            print("* ", end="")
    15 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 4 more passes ⋯
    143
    154
  10. if row == 0 or row == height - 1 or col == 0 or col == width - 1:

    pass 1 of 12
    14for col in range(width):15    if row0 == 0 or row == height3 - 1 or col0 == 0 or col == width5 - 1:16        print("* ", end="")17    else:
    output* 
    All 12 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    714
    820
    921
    1022
    1123
    1224
  11. print()

    18        print("  ", end="")19print()
  12. else:

    pass 1 of 3
    15    if row == 0 or row == height - 1 or col == 0 or col == width - 1:16        print("* ", end="")17    else:18        print("  ", end="")19print()
    output  
  13. print()

    18        print("  ", end="")19print()
  14. print()

    18        print("  ", end="")19print()
  15. print(" Using string multiplication:")

    21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):
    output
    Using string multiplication:
  16. for row in range(height):

    pass 1 of 3
    22print("\nUsing string multiplication:")23for row0 in range(height3):24    print("* " * width5)
    output* * * * * 
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  1. width ← 3, height ← 3

    1width→ 3 = 32height→ 3 = 334print(f"Rectangle {width3}x{height3}:")
    outputRectangle 3x3:
  2. for row in range(height):

    pass 1 of 3
    6for row0 in range(height3):7    for col in range(width):8        print("* ", end="")
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for col in range(width):

    pass 1 of 9
    6for row in range(height):7    for col0 in range(width3):8        print("* ", end="")9    print()  # New line after each row
    output* 
    All 9 passes — pass 1 is the card above
    passcol
    10
    21
    32
    40
    51
    62
    70
    81
    92
  4. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  5. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  6. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  7. print(" Hollow rectangle:")

    11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):
    output
    Hollow rectangle:
  8. for row in range(height):

    pass 1 of 3
    12print("\nHollow rectangle:")13for row0 in range(height3):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for col in range(width):

    pass 1 of 9
    13for row in range(height):14    for col0 in range(width3):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:16            print("* ", end="")
    All 9 passes — pass 1 is the card above
    passcol
    10
    21
    32
    40
    51
    62
    70
    81
    92
  10. if row == 0 or row == height - 1 or col == 0 or col == width - 1:

    pass 1 of 8
    14for col in range(width):15    if row0 == 0 or row == height3 - 1 or col0 == 0 or col == width3 - 1:16        print("* ", end="")17    else:
    output* 
    All 8 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    410
    512
    620
    721
    822
  11. print()

    18        print("  ", end="")19print()
  12. else:

    15    if row == 0 or row == height - 1 or col == 0 or col == width - 1:16        print("* ", end="")17    else:18        print("  ", end="")19print()
    output  
  13. print()

    18        print("  ", end="")19print()
  14. print()

    18        print("  ", end="")19print()
  15. print(" Using string multiplication:")

    21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):
    output
    Using string multiplication:
  16. for row in range(height):

    pass 1 of 3
    22print("\nUsing string multiplication:")23for row0 in range(height3):24    print("* " * width3)
    output* * * 
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  1. width ← 8, height ← 3

    1width→ 8 = 82height→ 3 = 334print(f"Rectangle {width8}x{height3}:")
    outputRectangle 8x3:
  2. for row in range(height):

    pass 1 of 3
    6for row0 in range(height3):7    for col in range(width):8        print("* ", end="")
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  3. for col in range(width):

    pass 1 of 24
    6for row in range(height):7    for col0 in range(width8):8        print("* ", end="")9    print()  # New line after each row
    output* 
    24 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    65
    76
    87
    90
    ⋯ 13 more passes ⋯
    236
    247
  4. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  5. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  6. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  7. print(" Hollow rectangle:")

    11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):
    output
    Hollow rectangle:
  8. for row in range(height):

    pass 1 of 3
    12print("\nHollow rectangle:")13for row0 in range(height3):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  9. for col in range(width):

    pass 1 of 24
    13for row in range(height):14    for col0 in range(width8):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:16            print("* ", end="")
    24 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    65
    76
    87
    90
    ⋯ 13 more passes ⋯
    236
    247
  10. if row == 0 or row == height - 1 or col == 0 or col == width - 1:

    pass 1 of 18
    14for col in range(width):15    if row0 == 0 or row == height3 - 1 or col0 == 0 or col == width8 - 1:16        print("* ", end="")17    else:
    output* 
    18 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    605
    706
    807
    910
    ⋯ 7 more passes ⋯
    1726
    1827
  11. print()

    18        print("  ", end="")19print()
  12. else:

    pass 1 of 6
    15    if row == 0 or row == height - 1 or col == 0 or col == width - 1:16        print("* ", end="")17    else:18        print("  ", end="")19print()
    output  
  13. print()

    18        print("  ", end="")19print()
  14. print()

    18        print("  ", end="")19print()
  15. print(" Using string multiplication:")

    21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):
    output
    Using string multiplication:
  16. for row in range(height):

    pass 1 of 3
    22print("\nUsing string multiplication:")23for row0 in range(height3):24    print("* " * width8)
    output* * * * * * * * 
    All 3 passes — pass 1 is the card above
    passrow
    10
    21
    32
  1. width ← 5, height ← 2

    1width→ 5 = 52height→ 2 = 234print(f"Rectangle {width5}x{height2}:")
    outputRectangle 5x2:
  2. for row in range(height):

    pass 1 of 2
    6for row0 in range(height2):7    for col in range(width):8        print("* ", end="")
  3. for col in range(width):

    pass 1 of 10
    6for row in range(height):7    for col0 in range(width5):8        print("* ", end="")9    print()  # New line after each row
    output* 
    All 10 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    104
  4. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  5. for row in range(height):

    pass 2 of 2
    6for row1 in range(height2):7    for col in range(width):8        print("* ", end="")
  6. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  7. print(" Hollow rectangle:")

    11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):
    output
    Hollow rectangle:
  8. for row in range(height):

    pass 1 of 2
    12print("\nHollow rectangle:")13for row0 in range(height2):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
  9. for col in range(width):

    pass 1 of 10
    13for row in range(height):14    for col0 in range(width5):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:16            print("* ", end="")
    All 10 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    104
  10. if row == 0 or row == height - 1 or col == 0 or col == width - 1:

    pass 1 of 10
    14for col in range(width):15    if row0 == 0 or row == height2 - 1 or col0 == 0 or col == width5 - 1:16        print("* ", end="")17    else:
    output* 
    All 10 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    711
    812
    913
    1014
  11. print()

    18        print("  ", end="")19print()
  12. for row in range(height):

    pass 2 of 2
    12print("\nHollow rectangle:")13for row1 in range(height2):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
  13. print()

    18        print("  ", end="")19print()
  14. print(" Using string multiplication:")

    21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):
    output
    Using string multiplication:
  15. for row in range(height):

    pass 1 of 2
    22print("\nUsing string multiplication:")23for row0 in range(height2):24    print("* " * width5)
    output* * * * * 
  16. for row in range(height):

    pass 2 of 2
    22print("\nUsing string multiplication:")23for row1 in range(height2):24    print("* " * width5)
    output* * * * * 
  1. width ← 5, height ← 5

    1width→ 5 = 52height→ 5 = 534print(f"Rectangle {width5}x{height5}:")
    outputRectangle 5x5:
  2. for row in range(height):

    pass 1 of 5
    6for row0 in range(height5):7    for col in range(width):8        print("* ", end="")
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  3. for col in range(width):

    pass 1 of 25
    6for row in range(height):7    for col0 in range(width5):8        print("* ", end="")9    print()  # New line after each row
    output* 
    25 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  4. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  5. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  6. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  7. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  8. print() # New line after each row

    8    print("* ", end="")9print()  # New line after each row
  9. print(" Hollow rectangle:")

    11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):
    output
    Hollow rectangle:
  10. for row in range(height):

    pass 1 of 5
    12print("\nHollow rectangle:")13for row0 in range(height5):14    for col in range(width):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54
  11. for col in range(width):

    pass 1 of 25
    13for row in range(height):14    for col0 in range(width5):15        if row == 0 or row == height - 1 or col == 0 or col == width - 1:16            print("* ", end="")
    25 passes — pass 1 is the card above
    passcol
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  12. if row == 0 or row == height - 1 or col == 0 or col == width - 1:

    pass 1 of 16
    14for col in range(width):15    if row0 == 0 or row == height5 - 1 or col0 == 0 or col == width5 - 1:16        print("* ", end="")17    else:
    output* 
    16 passes — pass 1 is the card above
    passrowcol
    100
    201
    302
    403
    504
    610
    714
    820
    924
    ⋯ 5 more passes ⋯
    1543
    1644
  13. print()

    18        print("  ", end="")19print()
  14. else:

    pass 1 of 9
    15    if row == 0 or row == height - 1 or col == 0 or col == width - 1:16        print("* ", end="")17    else:18        print("  ", end="")19print()
    output  
  15. print()

    18        print("  ", end="")19print()
  16. print()

    18        print("  ", end="")19print()
  17. print()

    18        print("  ", end="")19print()
  18. print()

    18        print("  ", end="")19print()
  19. print(" Using string multiplication:")

    21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):
    output
    Using string multiplication:
  20. for row in range(height):

    pass 1 of 5
    22print("\nUsing string multiplication:")23for row0 in range(height5):24    print("* " * width5)
    output* * * * * 
    All 5 passes — pass 1 is the card above
    passrow
    10
    21
    32
    43
    54

Outer loop: rows. Inner loop: columns within each row.

Print triangle pattern

Create a right triangle by varying inner loop iterations.

height
triangle.py
Replay: real traced execution (multi-file project)
height = 5

# Right triangle (left-aligned)
print("Right triangle:")
for row in range(1, height + 1):
    for col in range(row):
        print("* ", end="")
    print()

# Inverted triangle
print("\nInverted triangle:")
for row in range(height, 0, -1):
    for col in range(row):
        print("* ", end="")
    print()

# Right-aligned triangle
print("\nRight-aligned triangle:")
for row in range(1, height + 1):
    print("  " * (height - row), end="")  # spaces
    print("* " * row)                      # stars

# Pythonic versions
print("\nPythonic right triangle:")
for row in range(1, height + 1):
    print("* " * row)

height = 3

# Right triangle (left-aligned)
print("Right triangle:")
for row in range(1, height + 1):
    for col in range(row):
        print("* ", end="")
    print()

# Inverted triangle
print("\nInverted triangle:")
for row in range(height, 0, -1):
    for col in range(row):
        print("* ", end="")
    print()

# Right-aligned triangle
print("\nRight-aligned triangle:")
for row in range(1, height + 1):
    print("  " * (height - row), end="")  # spaces
    print("* " * row)                      # stars

# Pythonic versions
print("\nPythonic right triangle:")
for row in range(1, height + 1):
    print("* " * row)

height = 7

# Right triangle (left-aligned)
print("Right triangle:")
for row in range(1, height + 1):
    for col in range(row):
        print("* ", end="")
    print()

# Inverted triangle
print("\nInverted triangle:")
for row in range(height, 0, -1):
    for col in range(row):
        print("* ", end="")
    print()

# Right-aligned triangle
print("\nRight-aligned triangle:")
for row in range(1, height + 1):
    print("  " * (height - row), end="")  # spaces
    print("* " * row)                      # stars

# Pythonic versions
print("\nPythonic right triangle:")
for row in range(1, height + 1):
    print("* " * row)

  1. height ← 5

    1height→ 5 = 5  #@height=3, 723# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):
    outputRight triangle:
  2. for row in range(1, height + 1):

    pass 1 of 5
    4print("Right triangle:")5for row1 in range(1, height5 + 1):6    for col in range(row):  #?varying_limit7        print("* ", end="")
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  3. for col in range(row): #?varying_limit

    pass 1 of 15
    5for row in range(1, height + 1):6    for col0 in range(row1):  #?varying_limit7        print("* ", end="")8    print()
    output* 
    15 passes — pass 1 is the card above
    passcolrow
    101
    202
    312
    403
    513
    623
    704
    814
    924
    ⋯ 4 more passes ⋯
    1435
    1545
  4. print()

    7    print("* ", end="")8print()
  5. print()

    7    print("* ", end="")8print()
  6. print()

    7    print("* ", end="")8print()
  7. print()

    7    print("* ", end="")8print()
  8. print()

    7    print("* ", end="")8print()
  9. print(" Inverted triangle:")

    10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):
    output
    Inverted triangle:
  10. for row in range(height, 0, -1):

    pass 1 of 5
    11print("\nInverted triangle:")12for row5 in range(height5, 0, -1):13    for col in range(row):14        print("* ", end="")
    All 5 passes — pass 1 is the card above
    passrow
    15
    24
    33
    42
    51
  11. for col in range(row):

    pass 1 of 15
    12for row in range(height, 0, -1):13    for col0 in range(row5):14        print("* ", end="")15    print()
    output* 
    15 passes — pass 1 is the card above
    passcolrow
    105
    215
    325
    435
    545
    604
    714
    824
    934
    ⋯ 4 more passes ⋯
    1412
    1501
  12. print()

    14    print("* ", end="")15print()
  13. print()

    14    print("* ", end="")15print()
  14. print()

    14    print("* ", end="")15print()
  15. print()

    14    print("* ", end="")15print()
  16. print()

    14    print("* ", end="")15print()
  17. print(" Right-aligned triangle:")

    17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):
    output
    Right-aligned triangle:
  18. for row in range(1, height + 1):

    pass 1 of 5
    18print("\nRight-aligned triangle:")19for row1 in range(1, height5 + 1):20    print("  " * (height5 - row1), end="")  # spaces21    print("* " * row1)                      # stars
    output
    * 
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  19. print(" Pythonic right triangle:")

    23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):
    output
    Pythonic right triangle:
  20. for row in range(1, height + 1):

    pass 1 of 5
    24print("\nPythonic right triangle:")25for row1 in range(1, height5 + 1):26    print("* " * row1)
    output* 
    All 5 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
  1. height ← 3

    1height→ 3 = 323# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):
    outputRight triangle:
  2. for row in range(1, height + 1):

    pass 1 of 3
    4print("Right triangle:")5for row1 in range(1, height3 + 1):6    for col in range(row):7        print("* ", end="")
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  3. for col in range(row):

    pass 1 of 6
    5for row in range(1, height + 1):6    for col0 in range(row1):7        print("* ", end="")8    print()
    output* 
    All 6 passes — pass 1 is the card above
    passcolrow
    101
    202
    312
    403
    513
    623
  4. print()

    7    print("* ", end="")8print()
  5. print()

    7    print("* ", end="")8print()
  6. print()

    7    print("* ", end="")8print()
  7. print(" Inverted triangle:")

    10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):
    output
    Inverted triangle:
  8. for row in range(height, 0, -1):

    pass 1 of 3
    11print("\nInverted triangle:")12for row3 in range(height3, 0, -1):13    for col in range(row):14        print("* ", end="")
    All 3 passes — pass 1 is the card above
    passrow
    13
    22
    31
  9. for col in range(row):

    pass 1 of 6
    12for row in range(height, 0, -1):13    for col0 in range(row3):14        print("* ", end="")15    print()
    output* 
    All 6 passes — pass 1 is the card above
    passcolrow
    103
    213
    323
    402
    512
    601
  10. print()

    14    print("* ", end="")15print()
  11. print()

    14    print("* ", end="")15print()
  12. print()

    14    print("* ", end="")15print()
  13. print(" Right-aligned triangle:")

    17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):
    output
    Right-aligned triangle:
  14. for row in range(1, height + 1):

    pass 1 of 3
    18print("\nRight-aligned triangle:")19for row1 in range(1, height3 + 1):20    print("  " * (height3 - row1), end="")  # spaces21    print("* " * row1)                      # stars
    output
    * 
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  15. print(" Pythonic right triangle:")

    23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):
    output
    Pythonic right triangle:
  16. for row in range(1, height + 1):

    pass 1 of 3
    24print("\nPythonic right triangle:")25for row1 in range(1, height3 + 1):26    print("* " * row1)
    output* 
    All 3 passes — pass 1 is the card above
    passrow
    11
    22
    33
  1. height ← 7

    1height→ 7 = 723# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):
    outputRight triangle:
  2. for row in range(1, height + 1):

    pass 1 of 7
    4print("Right triangle:")5for row1 in range(1, height7 + 1):6    for col in range(row):7        print("* ", end="")
    All 7 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
  3. for col in range(row):

    pass 1 of 28
    5for row in range(1, height + 1):6    for col0 in range(row1):7        print("* ", end="")8    print()
    output* 
    28 passes — pass 1 is the card above
    passcolrow
    101
    202
    312
    403
    513
    623
    704
    814
    924
    ⋯ 17 more passes ⋯
    2757
    2867
  4. print()

    7    print("* ", end="")8print()
  5. print()

    7    print("* ", end="")8print()
  6. print()

    7    print("* ", end="")8print()
  7. print()

    7    print("* ", end="")8print()
  8. print()

    7    print("* ", end="")8print()
  9. print()

    7    print("* ", end="")8print()
  10. print()

    7    print("* ", end="")8print()
  11. print(" Inverted triangle:")

    10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):
    output
    Inverted triangle:
  12. for row in range(height, 0, -1):

    pass 1 of 7
    11print("\nInverted triangle:")12for row7 in range(height7, 0, -1):13    for col in range(row):14        print("* ", end="")
    All 7 passes — pass 1 is the card above
    passrow
    17
    26
    35
    44
    53
    62
    71
  13. for col in range(row):

    pass 1 of 28
    12for row in range(height, 0, -1):13    for col0 in range(row7):14        print("* ", end="")15    print()
    output* 
    28 passes — pass 1 is the card above
    passcolrow
    107
    217
    327
    437
    547
    657
    767
    806
    916
    ⋯ 17 more passes ⋯
    2712
    2801
  14. print()

    14    print("* ", end="")15print()
  15. print()

    14    print("* ", end="")15print()
  16. print()

    14    print("* ", end="")15print()
  17. print()

    14    print("* ", end="")15print()
  18. print()

    14    print("* ", end="")15print()
  19. print()

    14    print("* ", end="")15print()
  20. print()

    14    print("* ", end="")15print()
  21. print(" Right-aligned triangle:")

    17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):
    output
    Right-aligned triangle:
  22. for row in range(1, height + 1):

    pass 1 of 7
    18print("\nRight-aligned triangle:")19for row1 in range(1, height7 + 1):20    print("  " * (height7 - row1), end="")  # spaces21    print("* " * row1)                      # stars
    output
    * 
    All 7 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77
  23. print(" Pythonic right triangle:")

    23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):
    output
    Pythonic right triangle:
  24. for row in range(1, height + 1):

    pass 1 of 7
    24print("\nPythonic right triangle:")25for row1 in range(1, height7 + 1):26    print("* " * row1)
    output* 
    All 7 passes — pass 1 is the card above
    passrow
    11
    22
    33
    44
    55
    66
    77

The inner loop's limit depends on the outer loop variable.

Search in 2D grid

Find a value in a 2D list (matrix).

target
grid_search.py
Replay: real traced execution (multi-file project)
grid = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]
target = 7

# Print the grid
print("Grid:")
for row in grid:
    for cell in row:
        print(f"{cell:3d}", end="")
    print()

# Search for target (using break with flag)
found_pos = None
for row_idx, row in enumerate(grid):
    for col_idx, cell in enumerate(row):
        if cell == target:
            found_pos = (row_idx, col_idx)
            break
    if found_pos:
        break

if found_pos:
    print(f"\nFound {target} at [{found_pos[0]}][{found_pos[1]}]")
else:
    print(f"\n{target} not found in grid")

# Pythonic: using next() with generator
print("\nUsing generator expression:")
result = next(
    ((r, c) for r, row in enumerate(grid) for c, val in enumerate(row) if val == target),
    None
)
print(f"Result: {result}")
grid = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]
target = 1

# Print the grid
print("Grid:")
for row in grid:
    for cell in row:
        print(f"{cell:3d}", end="")
    print()

# Search for target (using break with flag)
found_pos = None
for row_idx, row in enumerate(grid):
    for col_idx, cell in enumerate(row):
        if cell == target:
            found_pos = (row_idx, col_idx)
            break
    if found_pos:
        break

if found_pos:
    print(f"\nFound {target} at [{found_pos[0]}][{found_pos[1]}]")
else:
    print(f"\n{target} not found in grid")

# Pythonic: using next() with generator
print("\nUsing generator expression:")
result = next(
    ((r, c) for r, row in enumerate(grid) for c, val in enumerate(row) if val == target),
    None
)
print(f"Result: {result}")
grid = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]
target = 12

# Print the grid
print("Grid:")
for row in grid:
    for cell in row:
        print(f"{cell:3d}", end="")
    print()

# Search for target (using break with flag)
found_pos = None
for row_idx, row in enumerate(grid):
    for col_idx, cell in enumerate(row):
        if cell == target:
            found_pos = (row_idx, col_idx)
            break
    if found_pos:
        break

if found_pos:
    print(f"\nFound {target} at [{found_pos[0]}][{found_pos[1]}]")
else:
    print(f"\n{target} not found in grid")

# Pythonic: using next() with generator
print("\nUsing generator expression:")
result = next(
    ((r, c) for r, row in enumerate(grid) for c, val in enumerate(row) if val == target),
    None
)
print(f"Result: {result}")
grid = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
]
target = 99

# Print the grid
print("Grid:")
for row in grid:
    for cell in row:
        print(f"{cell:3d}", end="")
    print()

# Search for target (using break with flag)
found_pos = None
for row_idx, row in enumerate(grid):
    for col_idx, cell in enumerate(row):
        if cell == target:
            found_pos = (row_idx, col_idx)
            break
    if found_pos:
        break

if found_pos:
    print(f"\nFound {target} at [{found_pos[0]}][{found_pos[1]}]")
else:
    print(f"\n{target} not found in grid")

# Pythonic: using next() with generator
print("\nUsing generator expression:")
result = next(
    ((r, c) for r, row in enumerate(grid) for c, val in enumerate(row) if val == target),
    None
)
print(f"Result: {result}")
  1. grid ← [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], target ← 7

    1grid→ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] = [2    [1, 2, 3, 4],3    [5, 6, 7, 8],4    [9, 10, 11, 12]5]6target→ 7 = 7  #@target=1, 12, 9978# Print the grid9print("Grid:")10for row in grid:
    outputGrid:
  2. for row in grid:

    pass 1 of 3
    9print("Grid:")10for row[1, 2, 3, 4] in grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]:11    for cell in row:12        print(f"{cell:3d}", end="")
    All 3 passes — pass 1 is the card above
    passrow
    1[1, 2, 3, 4]
    2[5, 6, 7, 8]
    3[9, 10, 11, 12]
  3. for cell in row:

    pass 1 of 12
    10for row in grid:11    for cell1 in row[1, 2, 3, 4]:12        print(f"{cell1:3d}", end="")13    print()
    output  1
    All 12 passes — pass 1 is the card above
    passcellrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    1010[9, 10, 11, 12]
    1111[9, 10, 11, 12]
    1212[9, 10, 11, 12]
  4. print()

    12    print(f"{cell:3d}", end="")13print()
  5. print()

    12    print(f"{cell:3d}", end="")13print()
  6. print()

    12    print(f"{cell:3d}", end="")13print()
  7. found_pos ← None

    15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):
  8. for row_idx, row in enumerate(grid):

    pass 1 of 2
    16found_pos = None17for row_idx0, row[1, 2, 3, 4] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):18    for col_idx, cell in enumerate(row):19        if cell == target:
  9. for col_idx, cell in enumerate(row):

    pass 1 of 7
    17for row_idx, row in enumerate(grid):18    for col_idx0, cell1 in enumerate(row[1, 2, 3, 4]):19        if cell == target:20            found_pos = (row_idx, col_idx)
    All 7 passes — pass 1 is the card above
    passcol_idxcellrowrow_idxgridtargetfound_pos[0]found_pos[1]found_pos
    101[1, 2, 3, 4]
    212[1, 2, 3, 4]
    323[1, 2, 3, 4]
    434[1, 2, 3, 4]1[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    505[5, 6, 7, 8]
    616[5, 6, 7, 8]
    727[5, 6, 7, 8]1712(1, 2)
  10. for row_idx, row in enumerate(grid):

    pass 2 of 2
    16found_pos = None17for row_idx1, row[5, 6, 7, 8] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):18    for col_idx, cell in enumerate(row):19        if cell == target:
  11. found_pos ← (1, 2)

    18for col_idx, cell in enumerate(row):19    if cell7 == target7:20        found_pos→ (1, 2) = (row_idx1, col_idx2)21        break22if found_pos:
  12. if found_pos:

    21        break22if found_pos(1, 2):23    break
  13. if found_pos:

    25if found_pos(1, 2):26    print(f"\nFound {target7} at [{found_pos[0]1}][{found_pos[1]2}]")27else:
    output
    Found 7 at [1][2]
  14. result ← (1, 2)

    30# Pythonic: using next() with generator31print("\nUsing generator expression:")32result→ (1, 2) = next(33    ((r(empty), c(empty)) for r, row[5, 6, 7, 8] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) for c, val(empty) in enumerate(row) if val == target7),34    None35)36print(f"Result: {result(1, 2)}")
    output
    Using generator expression:
    Result: (1, 2)
  1. grid ← [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], target ← 1

    1grid→ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] = [2    [1, 2, 3, 4],3    [5, 6, 7, 8],4    [9, 10, 11, 12]5]6target→ 1 = 178# Print the grid9print("Grid:")10for row in grid:
    outputGrid:
  2. for row in grid:

    pass 1 of 3
    9print("Grid:")10for row[1, 2, 3, 4] in grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]:11    for cell in row:12        print(f"{cell:3d}", end="")
    All 3 passes — pass 1 is the card above
    passrow
    1[1, 2, 3, 4]
    2[5, 6, 7, 8]
    3[9, 10, 11, 12]
  3. for cell in row:

    pass 1 of 12
    10for row in grid:11    for cell1 in row[1, 2, 3, 4]:12        print(f"{cell1:3d}", end="")13    print()
    output  1
    All 12 passes — pass 1 is the card above
    passcellrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    1010[9, 10, 11, 12]
    1111[9, 10, 11, 12]
    1212[9, 10, 11, 12]
  4. print()

    12    print(f"{cell:3d}", end="")13print()
  5. print()

    12    print(f"{cell:3d}", end="")13print()
  6. print()

    12    print(f"{cell:3d}", end="")13print()
  7. found_pos ← None

    15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):
  8. for row_idx, row in enumerate(grid):

    16found_pos = None17for row_idx0, row[1, 2, 3, 4] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):18    for col_idx, cell in enumerate(row):19        if cell == target:
  9. for col_idx, cell in enumerate(row):

    17for row_idx, row in enumerate(grid):18    for col_idx0, cell1 in enumerate(row[1, 2, 3, 4]):19        if cell == target:20            found_pos = (row_idx, col_idx)
  10. found_pos ← (0, 0)

    18for col_idx, cell in enumerate(row):19    if cell1 == target1:20        found_pos→ (0, 0) = (row_idx0, col_idx0)21        break22if found_pos:
  11. if found_pos:

    21        break22if found_pos(0, 0):23    break
  12. if found_pos:

    25if found_pos(0, 0):26    print(f"\nFound {target1} at [{found_pos[0]0}][{found_pos[1]0}]")27else:
    output
    Found 1 at [0][0]
  13. result ← (0, 0)

    30# Pythonic: using next() with generator31print("\nUsing generator expression:")32result→ (0, 0) = next(33    ((r(empty), c(empty)) for r, row[1, 2, 3, 4] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) for c, val(empty) in enumerate(row) if val == target1),34    None35)36print(f"Result: {result(0, 0)}")
    output
    Using generator expression:
    Result: (0, 0)
  1. grid ← [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], target ← 12

    1grid→ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] = [2    [1, 2, 3, 4],3    [5, 6, 7, 8],4    [9, 10, 11, 12]5]6target→ 12 = 1278# Print the grid9print("Grid:")10for row in grid:
    outputGrid:
  2. for row in grid:

    pass 1 of 3
    9print("Grid:")10for row[1, 2, 3, 4] in grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]:11    for cell in row:12        print(f"{cell:3d}", end="")
    All 3 passes — pass 1 is the card above
    passrow
    1[1, 2, 3, 4]
    2[5, 6, 7, 8]
    3[9, 10, 11, 12]
  3. for cell in row:

    pass 1 of 12
    10for row in grid:11    for cell1 in row[1, 2, 3, 4]:12        print(f"{cell1:3d}", end="")13    print()
    output  1
    All 12 passes — pass 1 is the card above
    passcellrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    1010[9, 10, 11, 12]
    1111[9, 10, 11, 12]
    1212[9, 10, 11, 12]
  4. print()

    12    print(f"{cell:3d}", end="")13print()
  5. print()

    12    print(f"{cell:3d}", end="")13print()
  6. print()

    12    print(f"{cell:3d}", end="")13print()
  7. found_pos ← None

    15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):
  8. for row_idx, row in enumerate(grid):

    pass 1 of 3
    16found_pos = None17for row_idx0, row[1, 2, 3, 4] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):18    for col_idx, cell in enumerate(row):19        if cell == target:
    All 3 passes — pass 1 is the card above
    passrow_idxrowcelltargetcol_idxfound_pos[0]found_pos[1]found_pos
    10[1, 2, 3, 4]
    21[5, 6, 7, 8]
    32[9, 10, 11, 12]1212323(2, 3)
  9. for col_idx, cell in enumerate(row):

    pass 1 of 12
    17for row_idx, row in enumerate(grid):18    for col_idx0, cell1 in enumerate(row[1, 2, 3, 4]):19        if cell == target:20            found_pos = (row_idx, col_idx)
    All 12 passes — pass 1 is the card above
    passcol_idxcellrowtargetrow_idxfound_pos[0]found_pos[1]found_pos
    101[1, 2, 3, 4]
    212[1, 2, 3, 4]
    323[1, 2, 3, 4]
    434[1, 2, 3, 4]
    505[5, 6, 7, 8]
    616[5, 6, 7, 8]
    727[5, 6, 7, 8]
    838[5, 6, 7, 8]
    909[9, 10, 11, 12]
    10110[9, 10, 11, 12]
    11211[9, 10, 11, 12]
    12312[9, 10, 11, 12]12223(2, 3)
  10. found_pos ← (2, 3)

    18for col_idx, cell in enumerate(row):19    if cell12 == target12:20        found_pos→ (2, 3) = (row_idx2, col_idx3)21        break22if found_pos:
  11. if found_pos:

    21        break22if found_pos(2, 3):23    break
  12. if found_pos:

    25if found_pos(2, 3):26    print(f"\nFound {target12} at [{found_pos[0]2}][{found_pos[1]3}]")27else:
    output
    Found 12 at [2][3]
  13. result ← (2, 3)

    30# Pythonic: using next() with generator31print("\nUsing generator expression:")32result→ (2, 3) = next(33    ((r(empty), c(empty)) for r, row[9, 10, 11, 12] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) for c, val(empty) in enumerate(row) if val == target12),34    None35)36print(f"Result: {result(2, 3)}")
    output
    Using generator expression:
    Result: (2, 3)
  1. grid ← [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], target ← 99

    1grid→ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] = [2    [1, 2, 3, 4],3    [5, 6, 7, 8],4    [9, 10, 11, 12]5]6target→ 99 = 9978# Print the grid9print("Grid:")10for row in grid:
    outputGrid:
  2. for row in grid:

    pass 1 of 3
    9print("Grid:")10for row[1, 2, 3, 4] in grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]:11    for cell in row:12        print(f"{cell:3d}", end="")
    All 3 passes — pass 1 is the card above
    passrow
    1[1, 2, 3, 4]
    2[5, 6, 7, 8]
    3[9, 10, 11, 12]
  3. for cell in row:

    pass 1 of 12
    10for row in grid:11    for cell1 in row[1, 2, 3, 4]:12        print(f"{cell1:3d}", end="")13    print()
    output  1
    All 12 passes — pass 1 is the card above
    passcellrow
    11[1, 2, 3, 4]
    22[1, 2, 3, 4]
    33[1, 2, 3, 4]
    44[1, 2, 3, 4]
    55[5, 6, 7, 8]
    66[5, 6, 7, 8]
    77[5, 6, 7, 8]
    88[5, 6, 7, 8]
    99[9, 10, 11, 12]
    1010[9, 10, 11, 12]
    1111[9, 10, 11, 12]
    1212[9, 10, 11, 12]
  4. print()

    12    print(f"{cell:3d}", end="")13print()
  5. print()

    12    print(f"{cell:3d}", end="")13print()
  6. print()

    12    print(f"{cell:3d}", end="")13print()
  7. found_pos ← None

    15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):
  8. for row_idx, row in enumerate(grid):

    pass 1 of 3
    16found_pos = None17for row_idx0, row[1, 2, 3, 4] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]):18    for col_idx, cell in enumerate(row):19        if cell == target:
    All 3 passes — pass 1 is the card above
    passrow_idxrowtarget
    10[1, 2, 3, 4]
    21[5, 6, 7, 8]
    32[9, 10, 11, 12]99
  9. for col_idx, cell in enumerate(row):

    pass 1 of 12
    17for row_idx, row in enumerate(grid):18    for col_idx0, cell1 in enumerate(row[1, 2, 3, 4]):19        if cell == target:20            found_pos = (row_idx, col_idx)
    All 12 passes — pass 1 is the card above
    passcol_idxcellrowtarget
    101[1, 2, 3, 4]
    212[1, 2, 3, 4]
    323[1, 2, 3, 4]
    434[1, 2, 3, 4]
    505[5, 6, 7, 8]
    616[5, 6, 7, 8]
    727[5, 6, 7, 8]
    838[5, 6, 7, 8]
    909[9, 10, 11, 12]
    10110[9, 10, 11, 12]
    11211[9, 10, 11, 12]
    12312[9, 10, 11, 12]99
  10. else:

    25if found_pos:26    print(f"\nFound {target} at [{found_pos[0]}][{found_pos[1]}]")27else:28    print(f"\n{target99} not found in grid")
    output
    99 not found in grid
  11. result ← None

    30# Pythonic: using next() with generator31print("\nUsing generator expression:")32result→ None = next(33    ((r(empty), c(empty)) for r, row[9, 10, 11, 12] in enumerate(grid[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) for c, val(empty) in enumerate(row) if val == target99),34    None35)36print(f"Result: {resultNone}")
    output
    Using generator expression:
    Result: None

Nested loops to traverse all [row][col] positions.

row-major Process row by row: outer loop = rows, inner loop = columns.

Compare all pairs

Find all pairs that satisfy a condition.

example
pairs.py
Replay: real traced execution (multi-file project)
numbers = [10, 20, 30, 40]
target_sum = 50

print(f"Find pairs that sum to {target_sum}:")

# All pairs (avoiding duplicates)
print(f"\nAll pairs with sum {target_sum}:")
for i in range(len(numbers)):
    for j in range(i + 1, len(numbers)):
        if numbers[i] + numbers[j] == target_sum:
            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")

# All possible pairs (cartesian product)
print("\nAll pairs (order matters):")
for i in range(len(numbers)):
    for j in range(len(numbers)):
        if i != j:
            print(f"  ({numbers[i]}, {numbers[j]})")

# Pythonic: using itertools
from itertools import combinations, permutations

print(f"\nUsing combinations:")
for a, b in combinations(numbers, 2):
    if a + b == target_sum:
        print(f"  {a} + {b} = {target_sum}")

print("\nUsing permutations:")
for pair in permutations(numbers, 2):
    print(f"  {pair}")

numbers = [5, 10, 15, 20, 25]
target_sum = 50

print(f"Find pairs that sum to {target_sum}:")

# All pairs (avoiding duplicates)
print(f"\nAll pairs with sum {target_sum}:")
for i in range(len(numbers)):
    for j in range(i + 1, len(numbers)):
        if numbers[i] + numbers[j] == target_sum:
            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")

# All possible pairs (cartesian product)
print("\nAll pairs (order matters):")
for i in range(len(numbers)):
    for j in range(len(numbers)):
        if i != j:
            print(f"  ({numbers[i]}, {numbers[j]})")

# Pythonic: using itertools
from itertools import combinations, permutations

print(f"\nUsing combinations:")
for a, b in combinations(numbers, 2):
    if a + b == target_sum:
        print(f"  {a} + {b} = {target_sum}")

print("\nUsing permutations:")
for pair in permutations(numbers, 2):
    print(f"  {pair}")

numbers = [10, 20, 30, 40]
target_sum = 30

print(f"Find pairs that sum to {target_sum}:")

# All pairs (avoiding duplicates)
print(f"\nAll pairs with sum {target_sum}:")
for i in range(len(numbers)):
    for j in range(i + 1, len(numbers)):
        if numbers[i] + numbers[j] == target_sum:
            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")

# All possible pairs (cartesian product)
print("\nAll pairs (order matters):")
for i in range(len(numbers)):
    for j in range(len(numbers)):
        if i != j:
            print(f"  ({numbers[i]}, {numbers[j]})")

# Pythonic: using itertools
from itertools import combinations, permutations

print(f"\nUsing combinations:")
for a, b in combinations(numbers, 2):
    if a + b == target_sum:
        print(f"  {a} + {b} = {target_sum}")

print("\nUsing permutations:")
for pair in permutations(numbers, 2):
    print(f"  {pair}")

numbers = [10, 20, 30, 40]
target_sum = 45

print(f"Find pairs that sum to {target_sum}:")

# All pairs (avoiding duplicates)
print(f"\nAll pairs with sum {target_sum}:")
for i in range(len(numbers)):
    for j in range(i + 1, len(numbers)):
        if numbers[i] + numbers[j] == target_sum:
            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")

# All possible pairs (cartesian product)
print("\nAll pairs (order matters):")
for i in range(len(numbers)):
    for j in range(len(numbers)):
        if i != j:
            print(f"  ({numbers[i]}, {numbers[j]})")

# Pythonic: using itertools
from itertools import combinations, permutations

print(f"\nUsing combinations:")
for a, b in combinations(numbers, 2):
    if a + b == target_sum:
        print(f"  {a} + {b} = {target_sum}")

print("\nUsing permutations:")
for pair in permutations(numbers, 2):
    print(f"  {pair}")

  1. numbers ← [10, 20, 30, 40], target_sum ← 50

    1numbers→ [10, 20, 30, 40] = [10, 20, 30, 40]  #@numbers=[5, 10, 15, 20, 25]2target_sum→ 50 = 50  #@target_sum=30, 4534print(f"Find pairs that sum to {target_sum50}:")56# All pairs (avoiding duplicates)7print(f"\nAll pairs with sum {target_sum50}:")8for i in range(len(numbers)):
    outputFind pairs that sum to 50:
    
    All pairs with sum 50:
  2. for i in range(len(numbers)):

    pass 1 of 4
    7print(f"\nAll pairs with sum {target_sum}:")8for i0 in range(len(numbers[10, 20, 30, 40])):9    for j in range(i + 1, len(numbers)):  #?avoid_duplicates10        if numbers[i] + numbers[j] == target_sum:
    All 4 passes — pass 1 is the card above
    passinumbers[i]numbers[j]target_sum
    10104050
    21203050
    32
    43
  3. for j in range(i + 1, len(numbers)): #?avoid_duplicates

    pass 1 of 6
    8for i in range(len(numbers)):9    for j1 in range(i0 + 1, len(numbers[10, 20, 30, 40])):  #?avoid_duplicates10        if numbers[i] + numbers[j] == target_sum:11            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]target_sum
    110
    220
    330104050
    421203050
    531
    632
  4. if numbers[i] + numbers[j] == target_sum:

    pass 1 of 2
    9for j in range(i + 1, len(numbers)):  #?avoid_duplicates10    if numbers[i]10 + numbers[j]40 == target_sum50:11        print(f"  {numbers[i]10} + {numbers[j]40} = {target_sum50}")
    output  10 + 40 = 50
  5. if numbers[i] + numbers[j] == target_sum:

    pass 2 of 2
    9for j in range(i + 1, len(numbers)):  #?avoid_duplicates10    if numbers[i]20 + numbers[j]30 == target_sum50:11        print(f"  {numbers[i]20} + {numbers[j]30} = {target_sum50}")
    output  20 + 30 = 50
  6. print(" All pairs (order matters):")

    13# All possible pairs (cartesian product)14print("\nAll pairs (order matters):")15for i in range(len(numbers)):
    output
    All pairs (order matters):
  7. for i in range(len(numbers)):

    pass 1 of 4
    14print("\nAll pairs (order matters):")15for i0 in range(len(numbers[10, 20, 30, 40])):16    for j in range(len(numbers)):17        if i != j:
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  8. for j in range(len(numbers)):

    pass 1 of 16
    15for i in range(len(numbers)):16    for j0 in range(len(numbers[10, 20, 30, 40])):17        if i != j:18            print(f"  ({numbers[i]}, {numbers[j]})")
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  9. if i != j:

    pass 1 of 12
    16for j in range(len(numbers)):17    if i0 != j1:18        print(f"  ({numbers[i]10}, {numbers[j]20})")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030
  10. print(f" Using combinations:")

    23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):
    output
    Using combinations:
  11. for a, b in combinations(numbers, 2):

    pass 1 of 6
    23print(f"\nUsing combinations:")24for a10, b20 in combinations(numbers[10, 20, 30, 40], 2):25    if a + b == target_sum:26        print(f"  {a} + {b} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passabtarget_sum
    11020
    21030
    3104050
    4203050
    52040
    63040
  12. if a + b == target_sum:

    pass 1 of 2
    24for a, b in combinations(numbers, 2):25    if a10 + b40 == target_sum50:26        print(f"  {a10} + {b40} = {target_sum50}")
    output  10 + 40 = 50
  13. if a + b == target_sum:

    pass 2 of 2
    24for a, b in combinations(numbers, 2):25    if a20 + b30 == target_sum50:26        print(f"  {a20} + {b30} = {target_sum50}")
    output  20 + 30 = 50
  14. print(" Using permutations:")

    28print("\nUsing permutations:")29for pair in permutations(numbers, 2):
    output
    Using permutations:
  15. for pair in permutations(numbers, 2):

    pass 1 of 12
    28print("\nUsing permutations:")29for pair(10, 20) in permutations(numbers[10, 20, 30, 40], 2):30    print(f"  {pair(10, 20)}")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passpair
    1(10, 20)
    2(10, 30)
    3(10, 40)
    4(20, 10)
    5(20, 30)
    6(20, 40)
    7(30, 10)
    8(30, 20)
    9(30, 40)
    10(40, 10)
    11(40, 20)
    12(40, 30)
  1. numbers ← [5, 10, 15, 20, 25], target_sum ← 50

    1numbers→ [5, 10, 15, 20, 25] = [5, 10, 15, 20, 25]2target_sum→ 50 = 5034print(f"Find pairs that sum to {target_sum50}:")56# All pairs (avoiding duplicates)7print(f"\nAll pairs with sum {target_sum50}:")8for i in range(len(numbers)):
    outputFind pairs that sum to 50:
    
    All pairs with sum 50:
  2. for i in range(len(numbers)):

    pass 1 of 5
    7print(f"\nAll pairs with sum {target_sum}:")8for i0 in range(len(numbers[5, 10, 15, 20, 25])):9    for j in range(i + 1, len(numbers)):10        if numbers[i] + numbers[j] == target_sum:
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  3. for j in range(i + 1, len(numbers)):

    pass 1 of 10
    8for i in range(len(numbers)):9    for j1 in range(i0 + 1, len(numbers[5, 10, 15, 20, 25])):10        if numbers[i] + numbers[j] == target_sum:11            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")
    All 10 passes — pass 1 is the card above
    passji
    110
    220
    330
    440
    521
    631
    741
    832
    942
    1043
  4. print(" All pairs (order matters):")

    13# All possible pairs (cartesian product)14print("\nAll pairs (order matters):")15for i in range(len(numbers)):
    output
    All pairs (order matters):
  5. for i in range(len(numbers)):

    pass 1 of 5
    14print("\nAll pairs (order matters):")15for i0 in range(len(numbers[5, 10, 15, 20, 25])):16    for j in range(len(numbers)):17        if i != j:
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  6. for j in range(len(numbers)):

    pass 1 of 25
    15for i in range(len(numbers)):16    for j0 in range(len(numbers[5, 10, 15, 20, 25])):17        if i != j:18            print(f"  ({numbers[i]}, {numbers[j]})")
    25 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    54
    60
    71
    82
    93
    ⋯ 14 more passes ⋯
    243
    254
  7. if i != j:

    pass 1 of 20
    16for j in range(len(numbers)):17    if i0 != j1:18        print(f"  ({numbers[i]5}, {numbers[j]10})")
    output  (5, 10)
    20 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    101510
    202515
    303520
    404525
    510105
    6121015
    7131020
    8141025
    920155
    ⋯ 9 more passes ⋯
    19422515
    20432520
  8. print(f" Using combinations:")

    23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):
    output
    Using combinations:
  9. for a, b in combinations(numbers, 2):

    pass 1 of 10
    23print(f"\nUsing combinations:")24for a5, b10 in combinations(numbers[5, 10, 15, 20, 25], 2):25    if a + b == target_sum:26        print(f"  {a} + {b} = {target_sum}")
    All 10 passes — pass 1 is the card above
    passab
    1510
    2515
    3520
    4525
    51015
    61020
    71025
    81520
    91525
    102025
  10. print(" Using permutations:")

    28print("\nUsing permutations:")29for pair in permutations(numbers, 2):
    output
    Using permutations:
  11. for pair in permutations(numbers, 2):

    pass 1 of 20
    28print("\nUsing permutations:")29for pair(5, 10) in permutations(numbers[5, 10, 15, 20, 25], 2):30    print(f"  {pair(5, 10)}")
    output  (5, 10)
    20 passes — pass 1 is the card above
    passpair
    1(5, 10)
    2(5, 15)
    3(5, 20)
    4(5, 25)
    5(10, 5)
    6(10, 15)
    7(10, 20)
    8(10, 25)
    9(15, 5)
    ⋯ 9 more passes ⋯
    19(25, 15)
    20(25, 20)
  1. numbers ← [10, 20, 30, 40], target_sum ← 30

    1numbers→ [10, 20, 30, 40] = [10, 20, 30, 40]2target_sum→ 30 = 3034print(f"Find pairs that sum to {target_sum30}:")56# All pairs (avoiding duplicates)7print(f"\nAll pairs with sum {target_sum30}:")8for i in range(len(numbers)):
    outputFind pairs that sum to 30:
    
    All pairs with sum 30:
  2. for i in range(len(numbers)):

    pass 1 of 4
    7print(f"\nAll pairs with sum {target_sum}:")8for i0 in range(len(numbers[10, 20, 30, 40])):9    for j in range(i + 1, len(numbers)):10        if numbers[i] + numbers[j] == target_sum:
    All 4 passes — pass 1 is the card above
    passinumbers[i]numbers[j]target_sum
    10102030
    21
    32
    43
  3. for j in range(i + 1, len(numbers)):

    pass 1 of 6
    8for i in range(len(numbers)):9    for j1 in range(i0 + 1, len(numbers[10, 20, 30, 40])):10        if numbers[i] + numbers[j] == target_sum:11            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passjinumbers[i]numbers[j]target_sum
    110102030
    220
    330
    421
    531
    632
  4. if numbers[i] + numbers[j] == target_sum:

    9for j in range(i + 1, len(numbers)):10    if numbers[i]10 + numbers[j]20 == target_sum30:11        print(f"  {numbers[i]10} + {numbers[j]20} = {target_sum30}")
    output  10 + 20 = 30
  5. print(" All pairs (order matters):")

    13# All possible pairs (cartesian product)14print("\nAll pairs (order matters):")15for i in range(len(numbers)):
    output
    All pairs (order matters):
  6. for i in range(len(numbers)):

    pass 1 of 4
    14print("\nAll pairs (order matters):")15for i0 in range(len(numbers[10, 20, 30, 40])):16    for j in range(len(numbers)):17        if i != j:
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  7. for j in range(len(numbers)):

    pass 1 of 16
    15for i in range(len(numbers)):16    for j0 in range(len(numbers[10, 20, 30, 40])):17        if i != j:18            print(f"  ({numbers[i]}, {numbers[j]})")
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  8. if i != j:

    pass 1 of 12
    16for j in range(len(numbers)):17    if i0 != j1:18        print(f"  ({numbers[i]10}, {numbers[j]20})")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030
  9. print(f" Using combinations:")

    23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):
    output
    Using combinations:
  10. for a, b in combinations(numbers, 2):

    pass 1 of 6
    23print(f"\nUsing combinations:")24for a10, b20 in combinations(numbers[10, 20, 30, 40], 2):25    if a + b == target_sum:26        print(f"  {a} + {b} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passabtarget_sum
    1102030
    21030
    31040
    42030
    52040
    63040
  11. if a + b == target_sum:

    24for a, b in combinations(numbers, 2):25    if a10 + b20 == target_sum30:26        print(f"  {a10} + {b20} = {target_sum30}")
    output  10 + 20 = 30
  12. print(" Using permutations:")

    28print("\nUsing permutations:")29for pair in permutations(numbers, 2):
    output
    Using permutations:
  13. for pair in permutations(numbers, 2):

    pass 1 of 12
    28print("\nUsing permutations:")29for pair(10, 20) in permutations(numbers[10, 20, 30, 40], 2):30    print(f"  {pair(10, 20)}")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passpair
    1(10, 20)
    2(10, 30)
    3(10, 40)
    4(20, 10)
    5(20, 30)
    6(20, 40)
    7(30, 10)
    8(30, 20)
    9(30, 40)
    10(40, 10)
    11(40, 20)
    12(40, 30)
  1. numbers ← [10, 20, 30, 40], target_sum ← 45

    1numbers→ [10, 20, 30, 40] = [10, 20, 30, 40]2target_sum→ 45 = 4534print(f"Find pairs that sum to {target_sum45}:")56# All pairs (avoiding duplicates)7print(f"\nAll pairs with sum {target_sum45}:")8for i in range(len(numbers)):
    outputFind pairs that sum to 45:
    
    All pairs with sum 45:
  2. for i in range(len(numbers)):

    pass 1 of 4
    7print(f"\nAll pairs with sum {target_sum}:")8for i0 in range(len(numbers[10, 20, 30, 40])):9    for j in range(i + 1, len(numbers)):10        if numbers[i] + numbers[j] == target_sum:
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  3. for j in range(i + 1, len(numbers)):

    pass 1 of 6
    8for i in range(len(numbers)):9    for j1 in range(i0 + 1, len(numbers[10, 20, 30, 40])):10        if numbers[i] + numbers[j] == target_sum:11            print(f"  {numbers[i]} + {numbers[j]} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passji
    110
    220
    330
    421
    531
    632
  4. print(" All pairs (order matters):")

    13# All possible pairs (cartesian product)14print("\nAll pairs (order matters):")15for i in range(len(numbers)):
    output
    All pairs (order matters):
  5. for i in range(len(numbers)):

    pass 1 of 4
    14print("\nAll pairs (order matters):")15for i0 in range(len(numbers[10, 20, 30, 40])):16    for j in range(len(numbers)):17        if i != j:
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  6. for j in range(len(numbers)):

    pass 1 of 16
    15for i in range(len(numbers)):16    for j0 in range(len(numbers[10, 20, 30, 40])):17        if i != j:18            print(f"  ({numbers[i]}, {numbers[j]})")
    16 passes — pass 1 is the card above
    passj
    10
    21
    32
    43
    50
    61
    72
    83
    90
    ⋯ 5 more passes ⋯
    152
    163
  7. if i != j:

    pass 1 of 12
    16for j in range(len(numbers)):17    if i0 != j1:18        print(f"  ({numbers[i]10}, {numbers[j]20})")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passijnumbers[i]numbers[j]
    1011020
    2021030
    3031040
    4102010
    5122030
    6132040
    7203010
    8213020
    9233040
    10304010
    11314020
    12324030
  8. print(f" Using combinations:")

    23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):
    output
    Using combinations:
  9. for a, b in combinations(numbers, 2):

    pass 1 of 6
    23print(f"\nUsing combinations:")24for a10, b20 in combinations(numbers[10, 20, 30, 40], 2):25    if a + b == target_sum:26        print(f"  {a} + {b} = {target_sum}")
    All 6 passes — pass 1 is the card above
    passab
    11020
    21030
    31040
    42030
    52040
    63040
  10. print(" Using permutations:")

    28print("\nUsing permutations:")29for pair in permutations(numbers, 2):
    output
    Using permutations:
  11. for pair in permutations(numbers, 2):

    pass 1 of 12
    28print("\nUsing permutations:")29for pair(10, 20) in permutations(numbers[10, 20, 30, 40], 2):30    print(f"  {pair(10, 20)}")
    output  (10, 20)
    All 12 passes — pass 1 is the card above
    passpair
    1(10, 20)
    2(10, 30)
    3(10, 40)
    4(20, 10)
    5(20, 30)
    6(20, 40)
    7(30, 10)
    8(30, 20)
    9(30, 40)
    10(40, 10)
    11(40, 20)
    12(40, 30)

Comparing every element with every other element requires O(n²) nested loops.

O(n²) Time complexity when nested loops both iterate n times.
comprehension Python's nested list comprehensions can replace some nested loops.

Exercise: diamond.py

Create a diamond shape pattern with careful loop control