MASTER YOUR LOGIC BUILDING:Phase 2 - Level 4: Pattern Printing

Mastering phase 2 - level 4: pattern printing concepts and implementation.

Phase 2 - Level 4: Pattern Printing

Why This Matters

Ever noticed how text-based game maps or ASCII art are built row by row? Pattern printing teaches you nested loops — a loop inside another loop. It's one of the best ways to truly understand how loops work together, and it's a classic skill in programming interviews and courses.

Don't worry if patterns look confusing at first. There's a simple method to decode any pattern.

What You'll Learn

  • How nested loops work (outer = rows, inner = columns)
  • How to analyze a pattern before writing code
  • How to print stars, numbers, and shapes row by row
  • How print with end="" controls output on the same line

The Pattern Decoder — 4 Questions

Before writing any code, ask:

  1. How many rows? → outer loop count
  2. How many items in row i? → inner loop count (often equals row number)
  3. What to print? → *, a number, or a character
  4. Need spaces? → for centered shapes like pyramids

Right triangle of stars

*
**
***
****

Row 1 → 1 star, Row 2 → 2 stars, Row 3 → 3 stars. The inner loop runs i times.

for i in range(1, n + 1):       # rows
    for j in range(1, i + 1):   # columns in this row
        print("*", end="")
    print()                     # new line after each row

Step-by-step:

  1. Outer loop picks the row (i)
  2. Inner loop prints something i times
  3. print() moves to the next line

Number pattern

1
12
123
1234

Same structure — but print j instead of *:

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How Nested Loops Execute

For n = 3:

Outer (i)Inner (j) runsOutput
1j = 1`*`
2j = 1, 2`**`
3j = 1, 2, 3`***`

The inner loop completes fully for each value of the outer loop.

Common Mistakes

  • Forgetting `print()` after the inner loop — everything prints on one line
  • Wrong inner range — range(1, i) gives one fewer item than you need; usually use range(1, i + 1)
  • Mixing up row and column logic — outer = rows, inner = columns. Say it out loud!
  • Trying to solve complex pyramids first — start with the right triangle, then level up

Tips

  • Draw the pattern on paper and label row numbers
  • Count stars (or numbers) in each row — that count is your inner loop range
  • Use end="" to print on the same line; use bare print() to go to the next line
  • Test with n = 3 first — small enough to trace by hand

Practice Focus

Try the examples below on your own:

  • Print a right triangle of stars using nested loops
  • Print the increasing number pattern (1, 12, 123, …)
  • Before coding, answer the 4 decoder questions for each pattern

Hands-on Examples

Print Right Triangle Pattern

# Take n as input
n = int(input("Enter number of rows: "))

# Print right triangle
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print("*", end="")
    print()  # New line after each row

Outer loop (i) controls rows. Inner loop (j) prints i stars. After each row, print newline.