Nearby lessons
53 of 159Python - Pattern Programs
- Understand how pattern programs build loop logic using nested for loops
- Learn the row and column logic behind the basic star, number, and alphabet patterns
- Use range(), print(), and the end parameter correctly
- Explain a pattern output from its program
- Understand string multiplication such as '* ' * 3 and chr() for alphabets
Introduction
Pattern programs are one of the best ways to understand loops in Python.
In pattern programs, we normally use:
forloops- Nested
forloops range()print()endparameter- Row and column logic
In this part, we will learn the first three patterns:
Pattern-1 → Square Star Pattern Pattern-2 → Same Row Number Pattern Pattern-3 → Increasing Column Number Pattern
Understanding these basic patterns is important because many advanced patterns are created using the same row and column concepts.
Pattern-1 — Square Star Pattern
In Pattern-1, we print a square made of * symbols.
If the user enters:
10
then the program prints:
- 10 rows
- 10 stars in every row
Rows = n Columns = n
Therefore, for n = 10, we get a 10 × 10 star pattern.
Pattern-1 — Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern-1 — Program
Pattern-1 — Program Explanation
Let's understand the program line by line.
Line 1
n=int(input("Enter the number of rows: "))
input() accepts the number of rows from the user.
By default, input() returns a string. Therefore, int() converts that value into an integer.
For example:
Enter the number of rows: 10 n = 10
Line 2
for i in range(1,n+1):
This loop controls the number of rows.
If:
n = 10
then:
range(1,n+1) becomes range(1,11)
The values of i are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Therefore, the loop executes 10 times.
Line 3
print("* "*n)
The string:
"* "
contains one star followed by one space.
Multiplying this string by n repeats it n times.
For example:
"* " * 5
produces:
* * * * *
For n = 10:
"* " * 10
produces 10 stars.
Because the print() statement executes once for every iteration of the outer loop, the same row is printed 10 times.
Pattern-1 — Understanding String Multiplication
Python allows us to multiply a string by an integer.
Example:
print("* " * 3)
Output:
* * *
Similarly:
print("* " * 5)
Output:
* * * * *
Therefore:
"* " * n
means:
Print "* " n times
Pattern-1 — Row Logic
The loop controls how many rows are printed.
for i in range(1,n+1):
For n = 10:
| Iteration | i | Output |
|---|---|---|
| 1 | 1 | 10 Stars |
| 2 | 2 | 10 Stars |
| 3 | 3 | 10 Stars |
| 4 | 4 | 10 Stars |
| 5 | 5 | 10 Stars |
| 6 | 6 | 10 Stars |
| 7 | 7 | 10 Stars |
| 8 | 8 | 10 Stars |
| 9 | 9 | 10 Stars |
| 10 | 10 | 10 Stars |
Notice that i is not used inside print().
Its purpose is only to repeat the statement n times.
Pattern-1 — Dry Run with n = 3
Suppose:
n = 3
The loop becomes:
for i in range(1,4):
Execution:
| i | Expression | Printed Row |
|---|---|---|
| 1 | "* " * 3 |
* * * |
| 2 | "* " * 3 |
* * * |
| 3 | "* " * 3 |
* * * |
Final output:
* * * * * * * * *
Pattern-1 — Execution Flow
START │ ▼ Read n │ ▼ i = 1 │ ▼ Is i <= n? │ ├── No ─────────────► END │ Yes │ ▼ Print "* " n times │ ▼ Move to next i │ └──────────────► Check i <= n again
Pattern-1 — Important Points
nrepresents the number of rows.- The
forloop executesntimes. "* " * ncreatesnstars.- Every row contains the same number of stars.
- The result is a square star pattern.
- This pattern does not require a nested loop because Python string multiplication is used.
Pattern-2 — Same Row Number Pattern
Pattern-2 prints numbers in a square format.
The important rule is:
Row 1 → print 1 Row 2 → print 2 Row 3 → print 3 ... Row n → print n
Every row contains n values.
Therefore:
Row Number = Printed Number
This pattern introduces the concept of nested loops.
Pattern-2 — Output
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10
Pattern-2 — Program
Pattern-2 — Nested Loop Concept
This program contains two loops:
Outer Loop → Controls Rows Inner Loop → Controls Columns
The structure is:
for i in range(1,n+1): ← Rows
for j in range(1,n+1): ← Columns
print(i,end=" ")
Therefore:
i → Row Number j → Column Number
Pattern-2 — Outer Loop Explanation
The outer loop is:
for i in range(1,n+1):
It controls the rows.
For n = 10, i takes the values:
1 2 3 4 5 6 7 8 9 10
Each value represents one row.
i = 1 → First row i = 2 → Second row i = 3 → Third row ... i = 10 → Tenth row
Pattern-2 — Inner Loop Explanation
The inner loop is:
for j in range(1,n+1):
It controls the number of columns.
For every row, the inner loop executes n times.
If n = 10:
j = 1,2,3,4,5,6,7,8,9,10
Therefore, each row contains 10 values.
Pattern-2 — Why print(i)?
The important statement is:
print(i,end=" ")
We print i, not j.
Since i represents the current row number, the same number is printed throughout that row.
For example:
i = 1
The inner loop executes 10 times:
print(1) print(1) print(1) ...
Therefore:
1 1 1 1 1 1 1 1 1 1
When:
i = 2
we get:
2 2 2 2 2 2 2 2 2 2
Pattern-2 — Understanding end=' '
Normally:
print(i)
moves the cursor to the next line after printing.
But this pattern needs multiple values on the same line.
Therefore, we use:
print(i,end=" ")
The end=" " tells Python:
After printing the value, do not move to the next line. Add one space instead.
This allows all column values to remain on the same row.
Pattern-2 — Why print() is Required
After the inner loop finishes, we use:
print()
This moves the cursor to the next line.
The flow is:
Print all columns of Row 1
│
▼
print()
│
▼
Move to Row 2
Without this final print(), all numbers would continue on the same line.
Pattern-2 — Dry Run with n = 3
Suppose:
n = 3
The program becomes logically:
i = 1
j = 1 → print 1
j = 2 → print 1
j = 3 → print 1
i = 2
j = 1 → print 2
j = 2 → print 2
j = 3 → print 2
i = 3
j = 1 → print 3
j = 2 → print 3
j = 3 → print 3
Output:
1 1 1 2 2 2 3 3 3
Pattern-2 — Iteration Table
| i | j Values | Printed Value | Row Output |
|---|---|---|---|
| 1 | 1, 2, 3 | i = 1 | 1 1 1 |
| 2 | 1, 2, 3 | i = 2 | 2 2 2 |
| 3 | 1, 2, 3 | i = 3 | 3 3 3 |
Pattern-2 — Execution Flow
START │ ▼ Read n │ ▼ Start Outer Loop i = 1 to n │ ▼ Start Inner Loop j = 1 to n │ ▼ Print i │ ▼ More columns? │ ├── Yes ──► Next j ──► Print i │ No │ ▼ print() │ ▼ Move to Next Row │ ▼ More rows? │ ├── Yes ──► Next i │ No │ ▼ END
Pattern-2 — Important Points
- The outer loop controls rows.
- The inner loop controls columns.
- Both loops execute
ntimes. irepresents the row number.jrepresents the column iteration.- The program prints
i, so the same number appears throughout each row. end=" "keeps numbers on the same line.print()moves execution to the next output line after one row is completed.
Pattern-3 — Increasing Column Number Pattern
Pattern-3 also creates a square number pattern.
However, instead of repeating the row number, every row prints:
1 2 3 4 ... n
For n = 10, every row contains:
1 2 3 4 5 6 7 8 9 10
The important rule is:
Printed Number = Column Number
Pattern-3 — Output
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
Pattern-3 — Program
Pattern-3 — Program Explanation
This program is very similar to Pattern-2.
The main difference is:
Pattern-2: print(i,end=" ") Pattern-3: print(j,end=" ")
This small change produces a completely different pattern.
In Pattern-3:
i → controls rows j → controls columns and is also printed
Pattern-3 — Outer Loop
The outer loop is:
for i in range(1,n+1):
Its job is to control the number of rows.
For n = 10:
i = 1 to 10
Therefore, 10 rows are generated.
Notice that i is not printed in this pattern.
Pattern-3 — Inner Loop
The inner loop is:
for j in range(1,n+1):
For every row, j takes values from 1 to n.
For n = 10:
j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10
Because the program prints j:
print(j,end=" ")
each row becomes:
1 2 3 4 5 6 7 8 9 10
Pattern-3 — Why Every Row is the Same
The inner loop always starts again from:
j = 1
whenever a new outer-loop iteration starts.
Consider:
i = 1 j → 1 2 3 4 5
After the first row finishes, the outer loop moves to:
i = 2
The inner loop starts again:
j → 1 2 3 4 5
Therefore, every row contains the same increasing number sequence.
Pattern-3 — Dry Run with n = 3
Suppose:
n = 3
Outer Loop — i = 1
j = 1 → print 1 j = 2 → print 2 j = 3 → print 3
First row:
1 2 3
Outer Loop — i = 2
The inner loop starts again:
j = 1 → print 1 j = 2 → print 2 j = 3 → print 3
Second row:
1 2 3
Outer Loop — i = 3
j = 1 → print 1 j = 2 → print 2 j = 3 → print 3
Third row:
1 2 3
Final output:
1 2 3 1 2 3 1 2 3
Pattern-3 — Iteration Table
| Row (i) | j Values | Values Printed |
|---|---|---|
| 1 | 1, 2, 3 | 1 2 3 |
| 2 | 1, 2, 3 | 1 2 3 |
| 3 | 1, 2, 3 | 1 2 3 |
Pattern-3 — Execution Flow
START │ ▼ Read n │ ▼ Start Outer Loop i = 1 to n │ ▼ Start Inner Loop j = 1 to n │ ▼ Print j │ ▼ Next j │ ▼ Is inner loop complete? │ ├── No ─────► Print next j │ Yes │ ▼ print() │ ▼ Move to next row │ ▼ Is outer loop complete? │ ├── No ─────► Start j from 1 again │ Yes │ ▼ END
Pattern-2 vs Pattern-3
Pattern-2 and Pattern-3 use almost the same nested-loop structure.
The important difference is the variable being printed.
| Feature | Pattern-2 | Pattern-3 |
|---|---|---|
| Outer Loop | i |
i |
| Inner Loop | j |
j |
| Printed Variable | i |
j |
| Row 1 | 1 1 1 | 1 2 3 |
| Row 2 | 2 2 2 | 1 2 3 |
| Row 3 | 3 3 3 | 1 2 3 |
Main Rule
print(i) → Pattern depends on ROW print(j) → Pattern depends on COLUMN
This is one of the most important concepts to understand before moving to more advanced pattern programs.
Pattern-3 — Important Points
- The outer loop controls rows.
- The inner loop controls columns.
jstarts from 1 for every new row.- The program prints
j, so values increase from left to right. - Every row contains the same number sequence.
end=" "keeps the values on the same row.- The empty
print()starts the next row.
Part 1A — Complete Comparison
| Pattern | Type | Main Logic |
|---|---|---|
| Pattern-1 | Square Star Pattern | "* " * n |
| Pattern-2 | Same Row Number | print(i,end=" ") |
| Pattern-3 | Increasing Column Numbers | print(j,end=" ") |
Part 1A — Core Pattern Programming Concept
The first three patterns introduce an important idea:
PATTERN PROGRAM
│
┌─────────┴─────────┐
│ │
▼ ▼
Rows Columns
│ │
▼ ▼
Outer Loop Inner Loop
│ │
▼ ▼
i j
In most pattern programs:
i = Row j = Column
Then we decide what should be printed by creating an expression using:
i j n
For example:
print(i) → Row-based pattern print(j) → Column-based pattern
Understanding this concept will make the upcoming number, alphabet, triangle, pyramid and reverse patterns much easier.
Part 1A — Summary
In Part 1A, we completed Pattern-1, Pattern-2 and Pattern-3.
Pattern-1
* * * * * * * * *
Uses string multiplication:
"* " * n
Pattern-2
1 1 1 2 2 2 3 3 3
Prints the outer-loop variable:
print(i,end=" ")
Pattern-3
1 2 3 1 2 3 1 2 3
Prints the inner-loop variable:
print(j,end=" ")
Golden Rule
Outer Loop → Rows Inner Loop → Columns i → Row j → Column
Introduction
In Part 1B, we continue with square patterns.
We will learn:
Pattern-4 → Reverse Number Square Pattern-5 → Same Row Alphabet Square Pattern-6 → Increasing Alphabet Square
The main concepts are:
- Reverse
range() - Nested loops
chr()- ASCII values
- Row-based alphabet patterns
- Column-based alphabet patterns
Pattern-4 — Reverse Number Square Pattern
This pattern prints numbers from n to 1 in every row.
For n = 10:
10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1
The important logic is:
Rows → 1 to n Columns → n to 1
Pattern-4 — Complete Program
Pattern-4 — Program Explanation
Outer Loop
for i in range(1,n+1):
The outer loop controls the rows.
Inner Loop
for j in range(n,0,-1):
This is a reverse loop.
For n = 10:
j = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
The third argument:
-1
means decrease the value by one after every iteration.
Printing
print(j,end=" ")
Since j is printed, numbers appear in reverse order.
Pattern-4 — Understanding range(n,0,-1)
The syntax of range() is:
range(start, stop, step)
Here:
range(n, 0, -1) start = n stop = 0 step = -1
The stop value is excluded.
Therefore, for n = 5:
range(5,0,-1) 5, 4, 3, 2, 1
Pattern-4 — Dry Run with n = 3
i = 1 j = 3 → print 3 j = 2 → print 2 j = 1 → print 1 i = 2 j = 3 → print 3 j = 2 → print 2 j = 1 → print 1 i = 3 j = 3 → print 3 j = 2 → print 2 j = 1 → print 1
Output:
3 2 1 3 2 1 3 2 1
Pattern-4 — Execution Flow
Read n │ ▼ Outer Loop: i = 1 to n │ ▼ Inner Loop: j = n to 1 │ ▼ Print j │ ▼ j = j - 1 │ ▼ Inner Loop Complete? │ ├── No → Continue │ Yes ▼ print() │ ▼ Next Row
Pattern-5 — Same Row Alphabet Square
Pattern-5 prints one alphabet repeatedly in every row.
A A A A A A A A A A B B B B B B B B B B C C C C C C C C C C D D D D D D D D D D E E E E E E E E E E F F F F F F F F F F G G G G G G G G G G H H H H H H H H H H I I I I I I I I I I J J J J J J J J J J
The rule is:
Row 1 → A Row 2 → B Row 3 → C ... Row 10 → J
Pattern-5 — Complete Program
Pattern-5 — Understanding chr()
Python's chr() converts an integer Unicode code point into its corresponding character.
For uppercase English letters:
| Expression | Value | Character |
|---|---|---|
chr(65) | 65 | A |
chr(66) | 66 | B |
chr(67) | 67 | C |
chr(68) | 68 | D |
The program uses:
chr(64+i)
Therefore:
i = 1 → chr(65) → A i = 2 → chr(66) → B i = 3 → chr(67) → C
Pattern-5 — Dry Run with n = 3
i = 1 chr(64+1) = chr(65) = A → A A A i = 2 chr(64+2) = chr(66) = B → B B B i = 3 chr(64+3) = chr(67) = C → C C C
Output:
A A A B B B C C C
Pattern-5 — Main Formula
Character = chr(64 + i)
Since the formula uses i, the alphabet depends on the row number.
i → Row
│
▼
64 + i
│
▼
chr()
│
▼
Alphabet
Pattern-6 — Increasing Alphabet Square
Pattern-6 prints increasing alphabets from left to right.
Every row follows:
A B C D E F G H I J
The same sequence is repeated for every row.
Pattern-6 — Complete Program
Pattern-6 — Program Explanation
The main expression is:
chr(64+j)
Here the alphabet depends on j.
j = 1 → A j = 2 → B j = 3 → C ... j = 10 → J
Since the inner loop restarts for every row, each row again starts from A.
Pattern-6 — Dry Run with n = 3
i = 1: j = 1 → A j = 2 → B j = 3 → C i = 2: j = 1 → A j = 2 → B j = 3 → C i = 3: j = 1 → A j = 2 → B j = 3 → C
Output:
A B C A B C A B C
Pattern-5 vs Pattern-6
| Pattern | Expression | Depends On |
|---|---|---|
| Pattern-5 | chr(64+i) | Row |
| Pattern-6 | chr(64+j) | Column |
chr(64+i) → Row-based alphabet chr(64+j) → Column-based alphabet
Part 1B — Summary
| Pattern | Main Logic |
|---|---|
| 4 | range(n,0,-1) |
| 5 | chr(64+i) |
| 6 | chr(64+j) |
Part 1B introduces two especially important concepts: reverse ranges and converting numbers into alphabet characters using chr().
- Patterns combine an outer loop (rows) and an inner loop (columns)
- print() with end=' ' keeps output on the same line; a plain print() moves to the next row
- '* ' * n prints a star followed by a space n times
- chr(65) converts an ASCII value into a character
- range(n, 0, -1) counts down from n to 1