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
printwithend=""controls output on the same line
The Pattern Decoder — 4 Questions
Before writing any code, ask:
- How many rows? → outer loop count
- How many items in row i? → inner loop count (often equals row number)
- What to print? →
*, a number, or a character - 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:
- Outer loop picks the row (
i) - Inner loop prints something
itimes 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) runs | Output |
|---|---|---|
| 1 | j = 1 | `*` |
| 2 | j = 1, 2 | `**` |
| 3 | j = 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 userange(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 bareprint()to go to the next line - Test with
n = 3first — 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 rowOuter loop (i) controls rows. Inner loop (j) prints i stars. After each row, print newline.
Related Page
🔗Related Content
- 💻
Phase 2 - Practice Problems
Practice Phase 2 concepts with hands-on coding problems
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with assessment questions
- ➡️
Phase 3 - Get Started
Continue to Phase 3 after mastering Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum