Control Flow
Nested Loops
Loops Inside Loops
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 = 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()
size ← 5
1size→ 5 = 5 #@size=3, 1023# Print header4print(" ", end="")5for i in range(1, size + 1):outputfor i in range(1, size + 1):
pass 1 of 54print(" ", end="")5for i1 in range(1, size5 + 1):6 print(f"{i1:4d}", end="")7print()output 1All 5 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 print(" " + "-" * (size * 4))
6 print(f"{i:4d}", end="")7print()8print(" " + "-" * (size5 * 4))output --------------------for row in range(1, size + 1): #?outer_loop
pass 1 of 510# Print table11for row1 in range(1, size5 + 1): #?outer_loop12 print(f"{row1:2d} |", end="")13 for col in range(1, size + 1): #?inner_loopoutput 1 |All 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 for col in range(1, size + 1): #?inner_loop
pass 1 of 2512print(f"{row:2d} |", end="")13for col1 in range(1, size5 + 1): #?inner_loop14 print(f"{row1 * col1:4d}", end="")15print()output 125 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 1 2 7 2 2 8 3 2 9 4 2 ⋯ 14 more passes ⋯ 24 4 5 25 5 5 print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()
size ← 3
1size→ 3 = 323# Print header4print(" ", end="")5for i in range(1, size + 1):outputfor i in range(1, size + 1):
pass 1 of 34print(" ", end="")5for i1 in range(1, size3 + 1):6 print(f"{i1:4d}", end="")7print()output 1All 3 passes — pass 1 is the card above pass i1 1 2 2 3 3 print(" " + "-" * (size * 4))
6 print(f"{i:4d}", end="")7print()8print(" " + "-" * (size3 * 4))output ------------for row in range(1, size + 1):
pass 1 of 310# 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 pass row1 1 2 2 3 3 for col in range(1, size + 1):
pass 1 of 912print(f"{row:2d} |", end="")13for col1 in range(1, size3 + 1):14 print(f"{row1 * col1:4d}", end="")15print()output 1All 9 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 1 2 5 2 2 6 3 2 7 1 3 8 2 3 9 3 3 print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()
size ← 10
1size→ 10 = 1023# Print header4print(" ", end="")5for i in range(1, size + 1):outputfor i in range(1, size + 1):
pass 1 of 104print(" ", end="")5for i1 in range(1, size10 + 1):6 print(f"{i1:4d}", end="")7print()output 1All 10 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 print(" " + "-" * (size * 4))
6 print(f"{i:4d}", end="")7print()8print(" " + "-" * (size10 * 4))output ----------------------------------------for row in range(1, size + 1):
pass 1 of 1010# 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 pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 for col in range(1, size + 1):
pass 1 of 10012print(f"{row:2d} |", end="")13for col1 in range(1, size10 + 1):14 print(f"{row1 * col1:4d}", end="")15print()output 1100 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 ⋯ 89 more passes ⋯ 99 9 10 100 10 10 print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()print()
14 print(f"{row * col:4d}", end="")15print()
Outer loop: each row (1-10). Inner loop: each column (multiply by 1-10).
Print rectangle of stars
Draw a rectangle pattern with asterisks.
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)
width ← 5, height ← 3
1width→ 5 = 5 #@width=3, 82height→ 3 = 3 #@height=2, 534print(f"Rectangle {width5}x{height3}:")outputRectangle 5x3:for row in range(height):
pass 1 of 36for row0 in range(height3):7 for col in range(width):8 print("* ", end="")All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 156for row in range(height):7 for col0 in range(width5):8 print("* ", end="")9 print() # New line after each rowoutput*15 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 4 more passes ⋯ 14 3 15 4 print() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint(" Hollow rectangle:")
11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):output Hollow rectangle:for row in range(height):
pass 1 of 312print("\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 pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 1513for 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 pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 4 more passes ⋯ 14 3 15 4 if row == 0 or row == height - 1 or col == 0 or col == width - 1:
pass 1 of 1214for 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 pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 4 8 2 0 9 2 1 10 2 2 11 2 3 12 2 4 print()
18 print(" ", end="")19print()else:
pass 1 of 315 if row == 0 or row == height - 1 or col == 0 or col == width - 1:16 print("* ", end="")17 else:18 print(" ", end="")19print()outputprint()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print(" Using string multiplication:")
21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):output Using string multiplication:for row in range(height):
pass 1 of 322print("\nUsing string multiplication:")23for row0 in range(height3):24 print("* " * width5)output* * * * *All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2
width ← 3, height ← 3
1width→ 3 = 32height→ 3 = 334print(f"Rectangle {width3}x{height3}:")outputRectangle 3x3:for row in range(height):
pass 1 of 36for row0 in range(height3):7 for col in range(width):8 print("* ", end="")All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 96for row in range(height):7 for col0 in range(width3):8 print("* ", end="")9 print() # New line after each rowoutput*All 9 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 print() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint(" Hollow rectangle:")
11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):output Hollow rectangle:for row in range(height):
pass 1 of 312print("\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 pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 913for 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 pass col1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 if row == 0 or row == height - 1 or col == 0 or col == width - 1:
pass 1 of 814for 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 pass rowcol1 0 0 2 0 1 3 0 2 4 1 0 5 1 2 6 2 0 7 2 1 8 2 2 print()
18 print(" ", end="")19print()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()outputprint()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print(" Using string multiplication:")
21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):output Using string multiplication:for row in range(height):
pass 1 of 322print("\nUsing string multiplication:")23for row0 in range(height3):24 print("* " * width3)output* * *All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2
width ← 8, height ← 3
1width→ 8 = 82height→ 3 = 334print(f"Rectangle {width8}x{height3}:")outputRectangle 8x3:for row in range(height):
pass 1 of 36for row0 in range(height3):7 for col in range(width):8 print("* ", end="")All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 246for row in range(height):7 for col0 in range(width8):8 print("* ", end="")9 print() # New line after each rowoutput*24 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 0 ⋯ 13 more passes ⋯ 23 6 24 7 print() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint(" Hollow rectangle:")
11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):output Hollow rectangle:for row in range(height):
pass 1 of 312print("\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 pass row1 0 2 1 3 2 for col in range(width):
pass 1 of 2413for 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 pass col1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 0 ⋯ 13 more passes ⋯ 23 6 24 7 if row == 0 or row == height - 1 or col == 0 or col == width - 1:
pass 1 of 1814for 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 pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 0 5 7 0 6 8 0 7 9 1 0 ⋯ 7 more passes ⋯ 17 2 6 18 2 7 print()
18 print(" ", end="")19print()else:
pass 1 of 615 if row == 0 or row == height - 1 or col == 0 or col == width - 1:16 print("* ", end="")17 else:18 print(" ", end="")19print()outputprint()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print(" Using string multiplication:")
21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):output Using string multiplication:for row in range(height):
pass 1 of 322print("\nUsing string multiplication:")23for row0 in range(height3):24 print("* " * width8)output* * * * * * * *All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2
width ← 5, height ← 2
1width→ 5 = 52height→ 2 = 234print(f"Rectangle {width5}x{height2}:")outputRectangle 5x2:for row in range(height):
pass 1 of 26for row0 in range(height2):7 for col in range(width):8 print("* ", end="")for col in range(width):
pass 1 of 106for row in range(height):7 for col0 in range(width5):8 print("* ", end="")9 print() # New line after each rowoutput*All 10 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 10 4 print() # New line after each row
8 print("* ", end="")9print() # New line after each rowfor row in range(height):
pass 2 of 26for row1 in range(height2):7 for col in range(width):8 print("* ", end="")print() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint(" Hollow rectangle:")
11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):output Hollow rectangle:for row in range(height):
pass 1 of 212print("\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:for col in range(width):
pass 1 of 1013for 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 pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 10 4 if row == 0 or row == height - 1 or col == 0 or col == width - 1:
pass 1 of 1014for 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 pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 1 8 1 2 9 1 3 10 1 4 print()
18 print(" ", end="")19print()for row in range(height):
pass 2 of 212print("\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:print()
18 print(" ", end="")19print()print(" Using string multiplication:")
21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):output Using string multiplication:for row in range(height):
pass 1 of 222print("\nUsing string multiplication:")23for row0 in range(height2):24 print("* " * width5)output* * * * *for row in range(height):
pass 2 of 222print("\nUsing string multiplication:")23for row1 in range(height2):24 print("* " * width5)output* * * * *
width ← 5, height ← 5
1width→ 5 = 52height→ 5 = 534print(f"Rectangle {width5}x{height5}:")outputRectangle 5x5:for row in range(height):
pass 1 of 56for row0 in range(height5):7 for col in range(width):8 print("* ", end="")All 5 passes — pass 1 is the card above pass row1 0 2 1 3 2 4 3 5 4 for col in range(width):
pass 1 of 256for row in range(height):7 for col0 in range(width5):8 print("* ", end="")9 print() # New line after each rowoutput*25 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 print() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint() # New line after each row
8 print("* ", end="")9print() # New line after each rowprint(" Hollow rectangle:")
11# Hollow rectangle12print("\nHollow rectangle:")13for row in range(height):output Hollow rectangle:for row in range(height):
pass 1 of 512print("\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 pass row1 0 2 1 3 2 4 3 5 4 for col in range(width):
pass 1 of 2513for 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 pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 if row == 0 or row == height - 1 or col == 0 or col == width - 1:
pass 1 of 1614for 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 pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 4 8 2 0 9 2 4 ⋯ 5 more passes ⋯ 15 4 3 16 4 4 print()
18 print(" ", end="")19print()else:
pass 1 of 915 if row == 0 or row == height - 1 or col == 0 or col == width - 1:16 print("* ", end="")17 else:18 print(" ", end="")19print()outputprint()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print()
18 print(" ", end="")19print()print(" Using string multiplication:")
21# Pythonic: string multiplication22print("\nUsing string multiplication:")23for row in range(height):output Using string multiplication:for row in range(height):
pass 1 of 522print("\nUsing string multiplication:")23for row0 in range(height5):24 print("* " * width5)output* * * * *All 5 passes — pass 1 is the card above pass row1 0 2 1 3 2 4 3 5 4
Outer loop: rows. Inner loop: columns within each row.
Print triangle pattern
Create a right triangle by varying inner loop iterations.
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)
height ← 5
1height→ 5 = 5 #@height=3, 723# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):outputRight triangle:for row in range(1, height + 1):
pass 1 of 54print("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 pass row1 1 2 2 3 3 4 4 5 5 for col in range(row): #?varying_limit
pass 1 of 155for 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 pass colrow1 0 1 2 0 2 3 1 2 4 0 3 5 1 3 6 2 3 7 0 4 8 1 4 9 2 4 ⋯ 4 more passes ⋯ 14 3 5 15 4 5 print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print(" Inverted triangle:")
10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):output Inverted triangle:for row in range(height, 0, -1):
pass 1 of 511print("\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 pass row1 5 2 4 3 3 4 2 5 1 for col in range(row):
pass 1 of 1512for 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 pass colrow1 0 5 2 1 5 3 2 5 4 3 5 5 4 5 6 0 4 7 1 4 8 2 4 9 3 4 ⋯ 4 more passes ⋯ 14 1 2 15 0 1 print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print(" Right-aligned triangle:")
17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):output Right-aligned triangle:for row in range(1, height + 1):
pass 1 of 518print("\nRight-aligned triangle:")19for row1 in range(1, height5 + 1):20 print(" " * (height5 - row1), end="") # spaces21 print("* " * row1) # starsoutput *All 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 print(" Pythonic right triangle:")
23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):output Pythonic right triangle:for row in range(1, height + 1):
pass 1 of 524print("\nPythonic right triangle:")25for row1 in range(1, height5 + 1):26 print("* " * row1)output*All 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5
height ← 3
1height→ 3 = 323# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):outputRight triangle:for row in range(1, height + 1):
pass 1 of 34print("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 pass row1 1 2 2 3 3 for col in range(row):
pass 1 of 65for 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 pass colrow1 0 1 2 0 2 3 1 2 4 0 3 5 1 3 6 2 3 print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print(" Inverted triangle:")
10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):output Inverted triangle:for row in range(height, 0, -1):
pass 1 of 311print("\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 pass row1 3 2 2 3 1 for col in range(row):
pass 1 of 612for 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 pass colrow1 0 3 2 1 3 3 2 3 4 0 2 5 1 2 6 0 1 print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print(" Right-aligned triangle:")
17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):output Right-aligned triangle:for row in range(1, height + 1):
pass 1 of 318print("\nRight-aligned triangle:")19for row1 in range(1, height3 + 1):20 print(" " * (height3 - row1), end="") # spaces21 print("* " * row1) # starsoutput *All 3 passes — pass 1 is the card above pass row1 1 2 2 3 3 print(" Pythonic right triangle:")
23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):output Pythonic right triangle:for row in range(1, height + 1):
pass 1 of 324print("\nPythonic right triangle:")25for row1 in range(1, height3 + 1):26 print("* " * row1)output*All 3 passes — pass 1 is the card above pass row1 1 2 2 3 3
height ← 7
1height→ 7 = 723# Right triangle (left-aligned)4print("Right triangle:")5for row in range(1, height + 1):outputRight triangle:for row in range(1, height + 1):
pass 1 of 74print("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 pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 for col in range(row):
pass 1 of 285for 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 pass colrow1 0 1 2 0 2 3 1 2 4 0 3 5 1 3 6 2 3 7 0 4 8 1 4 9 2 4 ⋯ 17 more passes ⋯ 27 5 7 28 6 7 print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print()
7 print("* ", end="")8print()print(" Inverted triangle:")
10# Inverted triangle11print("\nInverted triangle:")12for row in range(height, 0, -1):output Inverted triangle:for row in range(height, 0, -1):
pass 1 of 711print("\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 pass row1 7 2 6 3 5 4 4 5 3 6 2 7 1 for col in range(row):
pass 1 of 2812for 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 pass colrow1 0 7 2 1 7 3 2 7 4 3 7 5 4 7 6 5 7 7 6 7 8 0 6 9 1 6 ⋯ 17 more passes ⋯ 27 1 2 28 0 1 print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print()
14 print("* ", end="")15print()print(" Right-aligned triangle:")
17# Right-aligned triangle18print("\nRight-aligned triangle:")19for row in range(1, height + 1):output Right-aligned triangle:for row in range(1, height + 1):
pass 1 of 718print("\nRight-aligned triangle:")19for row1 in range(1, height7 + 1):20 print(" " * (height7 - row1), end="") # spaces21 print("* " * row1) # starsoutput *All 7 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 print(" Pythonic right triangle:")
23# Pythonic versions24print("\nPythonic right triangle:")25for row in range(1, height + 1):output Pythonic right triangle:for row in range(1, height + 1):
pass 1 of 724print("\nPythonic right triangle:")25for row1 in range(1, height7 + 1):26 print("* " * row1)output*All 7 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7
The inner loop's limit depends on the outer loop variable.
Search in 2D grid
Find a value in a 2D list (matrix).
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}")
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:for row in grid:
pass 1 of 39print("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 pass row1 [1, 2, 3, 4] 2 [5, 6, 7, 8] 3 [9, 10, 11, 12] for cell in row:
pass 1 of 1210for row in grid:11 for cell1 in row[1, 2, 3, 4]:12 print(f"{cell1:3d}", end="")13 print()output 1All 12 passes — pass 1 is the card above pass cellrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] 10 10 [9, 10, 11, 12] 11 11 [9, 10, 11, 12] 12 12 [9, 10, 11, 12] print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()found_pos ← None
15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):for row_idx, row in enumerate(grid):
pass 1 of 216found_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:for col_idx, cell in enumerate(row):
pass 1 of 717for 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 pass col_idxcellrowrow_idxgridtargetfound_pos[0]found_pos[1]found_pos1 0 1 [1, 2, 3, 4] — — — — — — 2 1 2 [1, 2, 3, 4] — — — — — — 3 2 3 [1, 2, 3, 4] — — — — — — 4 3 4 [1, 2, 3, 4] 1 [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] — — — — 5 0 5 [5, 6, 7, 8] — — — — — — 6 1 6 [5, 6, 7, 8] — — — — — — 7 2 7 [5, 6, 7, 8] 1 — 7 1 2 (1, 2) for row_idx, row in enumerate(grid):
pass 2 of 216found_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: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:if found_pos:
21 break22if found_pos(1, 2):23 breakif 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]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)
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:for row in grid:
pass 1 of 39print("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 pass row1 [1, 2, 3, 4] 2 [5, 6, 7, 8] 3 [9, 10, 11, 12] for cell in row:
pass 1 of 1210for row in grid:11 for cell1 in row[1, 2, 3, 4]:12 print(f"{cell1:3d}", end="")13 print()output 1All 12 passes — pass 1 is the card above pass cellrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] 10 10 [9, 10, 11, 12] 11 11 [9, 10, 11, 12] 12 12 [9, 10, 11, 12] print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()found_pos ← None
15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):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: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)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:if found_pos:
21 break22if found_pos(0, 0):23 breakif 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]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)
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:for row in grid:
pass 1 of 39print("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 pass row1 [1, 2, 3, 4] 2 [5, 6, 7, 8] 3 [9, 10, 11, 12] for cell in row:
pass 1 of 1210for row in grid:11 for cell1 in row[1, 2, 3, 4]:12 print(f"{cell1:3d}", end="")13 print()output 1All 12 passes — pass 1 is the card above pass cellrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] 10 10 [9, 10, 11, 12] 11 11 [9, 10, 11, 12] 12 12 [9, 10, 11, 12] print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()found_pos ← None
15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):for row_idx, row in enumerate(grid):
pass 1 of 316found_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 pass row_idxrowcelltargetcol_idxfound_pos[0]found_pos[1]found_pos1 0 [1, 2, 3, 4] — — — — — — 2 1 [5, 6, 7, 8] — — — — — — 3 2 [9, 10, 11, 12] 12 12 3 2 3 (2, 3) for col_idx, cell in enumerate(row):
pass 1 of 1217for 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 pass col_idxcellrowtargetrow_idxfound_pos[0]found_pos[1]found_pos1 0 1 [1, 2, 3, 4] — — — — — 2 1 2 [1, 2, 3, 4] — — — — — 3 2 3 [1, 2, 3, 4] — — — — — 4 3 4 [1, 2, 3, 4] — — — — — 5 0 5 [5, 6, 7, 8] — — — — — 6 1 6 [5, 6, 7, 8] — — — — — 7 2 7 [5, 6, 7, 8] — — — — — 8 3 8 [5, 6, 7, 8] — — — — — 9 0 9 [9, 10, 11, 12] — — — — — 10 1 10 [9, 10, 11, 12] — — — — — 11 2 11 [9, 10, 11, 12] — — — — — 12 3 12 [9, 10, 11, 12] 12 2 2 3 (2, 3) 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:if found_pos:
21 break22if found_pos(2, 3):23 breakif 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]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)
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:for row in grid:
pass 1 of 39print("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 pass row1 [1, 2, 3, 4] 2 [5, 6, 7, 8] 3 [9, 10, 11, 12] for cell in row:
pass 1 of 1210for row in grid:11 for cell1 in row[1, 2, 3, 4]:12 print(f"{cell1:3d}", end="")13 print()output 1All 12 passes — pass 1 is the card above pass cellrow1 1 [1, 2, 3, 4] 2 2 [1, 2, 3, 4] 3 3 [1, 2, 3, 4] 4 4 [1, 2, 3, 4] 5 5 [5, 6, 7, 8] 6 6 [5, 6, 7, 8] 7 7 [5, 6, 7, 8] 8 8 [5, 6, 7, 8] 9 9 [9, 10, 11, 12] 10 10 [9, 10, 11, 12] 11 11 [9, 10, 11, 12] 12 12 [9, 10, 11, 12] print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()print()
12 print(f"{cell:3d}", end="")13print()found_pos ← None
15# Search for target (using break with flag)16found_pos→ None = None17for row_idx, row in enumerate(grid):for row_idx, row in enumerate(grid):
pass 1 of 316found_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 pass row_idxrowtarget1 0 [1, 2, 3, 4] — 2 1 [5, 6, 7, 8] — 3 2 [9, 10, 11, 12] 99 for col_idx, cell in enumerate(row):
pass 1 of 1217for 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 pass col_idxcellrowtarget1 0 1 [1, 2, 3, 4] — 2 1 2 [1, 2, 3, 4] — 3 2 3 [1, 2, 3, 4] — 4 3 4 [1, 2, 3, 4] — 5 0 5 [5, 6, 7, 8] — 6 1 6 [5, 6, 7, 8] — 7 2 7 [5, 6, 7, 8] — 8 3 8 [5, 6, 7, 8] — 9 0 9 [9, 10, 11, 12] — 10 1 10 [9, 10, 11, 12] — 11 2 11 [9, 10, 11, 12] — 12 3 12 [9, 10, 11, 12] 99 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 gridresult ← 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.
Compare all pairs
Find all pairs that satisfy a condition.
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}")
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:for i in range(len(numbers)):
pass 1 of 47print(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 pass inumbers[i]numbers[j]target_sum1 0 10 40 50 2 1 20 30 50 3 2 — — — 4 3 — — — for j in range(i + 1, len(numbers)): #?avoid_duplicates
pass 1 of 68for 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 pass jinumbers[i]numbers[j]target_sum1 1 0 — — — 2 2 0 — — — 3 3 0 10 40 50 4 2 1 20 30 50 5 3 1 — — — 6 3 2 — — — if numbers[i] + numbers[j] == target_sum:
pass 1 of 29for 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 = 50if numbers[i] + numbers[j] == target_sum:
pass 2 of 29for 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 = 50print(" 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):for i in range(len(numbers)):
pass 1 of 414print("\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 pass i1 0 2 1 3 2 4 3 for j in range(len(numbers)):
pass 1 of 1615for 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 pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if i != j:
pass 1 of 1216for 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 pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30 print(f" Using combinations:")
23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):output Using combinations:for a, b in combinations(numbers, 2):
pass 1 of 623print(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 pass abtarget_sum1 10 20 — 2 10 30 — 3 10 40 50 4 20 30 50 5 20 40 — 6 30 40 — if a + b == target_sum:
pass 1 of 224for a, b in combinations(numbers, 2):25 if a10 + b40 == target_sum50:26 print(f" {a10} + {b40} = {target_sum50}")output 10 + 40 = 50if a + b == target_sum:
pass 2 of 224for a, b in combinations(numbers, 2):25 if a20 + b30 == target_sum50:26 print(f" {a20} + {b30} = {target_sum50}")output 20 + 30 = 50print(" Using permutations:")
28print("\nUsing permutations:")29for pair in permutations(numbers, 2):output Using permutations:for pair in permutations(numbers, 2):
pass 1 of 1228print("\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 pass pair1 (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)
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:for i in range(len(numbers)):
pass 1 of 57print(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 pass i1 0 2 1 3 2 4 3 5 4 for j in range(i + 1, len(numbers)):
pass 1 of 108for 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 pass ji1 1 0 2 2 0 3 3 0 4 4 0 5 2 1 6 3 1 7 4 1 8 3 2 9 4 2 10 4 3 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):for i in range(len(numbers)):
pass 1 of 514print("\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 pass i1 0 2 1 3 2 4 3 5 4 for j in range(len(numbers)):
pass 1 of 2515for 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 pass j1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 if i != j:
pass 1 of 2016for 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 pass ijnumbers[i]numbers[j]1 0 1 5 10 2 0 2 5 15 3 0 3 5 20 4 0 4 5 25 5 1 0 10 5 6 1 2 10 15 7 1 3 10 20 8 1 4 10 25 9 2 0 15 5 ⋯ 9 more passes ⋯ 19 4 2 25 15 20 4 3 25 20 print(f" Using combinations:")
23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):output Using combinations:for a, b in combinations(numbers, 2):
pass 1 of 1023print(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 pass ab1 5 10 2 5 15 3 5 20 4 5 25 5 10 15 6 10 20 7 10 25 8 15 20 9 15 25 10 20 25 print(" Using permutations:")
28print("\nUsing permutations:")29for pair in permutations(numbers, 2):output Using permutations:for pair in permutations(numbers, 2):
pass 1 of 2028print("\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 pass pair1 (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)
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:for i in range(len(numbers)):
pass 1 of 47print(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 pass inumbers[i]numbers[j]target_sum1 0 10 20 30 2 1 — — — 3 2 — — — 4 3 — — — for j in range(i + 1, len(numbers)):
pass 1 of 68for 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 pass jinumbers[i]numbers[j]target_sum1 1 0 10 20 30 2 2 0 — — — 3 3 0 — — — 4 2 1 — — — 5 3 1 — — — 6 3 2 — — — 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 = 30print(" 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):for i in range(len(numbers)):
pass 1 of 414print("\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 pass i1 0 2 1 3 2 4 3 for j in range(len(numbers)):
pass 1 of 1615for 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 pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if i != j:
pass 1 of 1216for 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 pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30 print(f" Using combinations:")
23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):output Using combinations:for a, b in combinations(numbers, 2):
pass 1 of 623print(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 pass abtarget_sum1 10 20 30 2 10 30 — 3 10 40 — 4 20 30 — 5 20 40 — 6 30 40 — 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 = 30print(" Using permutations:")
28print("\nUsing permutations:")29for pair in permutations(numbers, 2):output Using permutations:for pair in permutations(numbers, 2):
pass 1 of 1228print("\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 pass pair1 (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)
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:for i in range(len(numbers)):
pass 1 of 47print(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 pass i1 0 2 1 3 2 4 3 for j in range(i + 1, len(numbers)):
pass 1 of 68for 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 pass ji1 1 0 2 2 0 3 3 0 4 2 1 5 3 1 6 3 2 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):for i in range(len(numbers)):
pass 1 of 414print("\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 pass i1 0 2 1 3 2 4 3 for j in range(len(numbers)):
pass 1 of 1615for 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 pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if i != j:
pass 1 of 1216for 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 pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30 print(f" Using combinations:")
23print(f"\nUsing combinations:")24for a, b in combinations(numbers, 2):output Using combinations:for a, b in combinations(numbers, 2):
pass 1 of 623print(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 pass ab1 10 20 2 10 30 3 10 40 4 20 30 5 20 40 6 30 40 print(" Using permutations:")
28print("\nUsing permutations:")29for pair in permutations(numbers, 2):output Using permutations:for pair in permutations(numbers, 2):
pass 1 of 1228print("\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 pass pair1 (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.
Exercise: diamond.py
Create a diamond shape pattern with careful loop control