Nearby lessons

58 of 159

Python - Advanced Pattern Programs

📌 What You Will Learn
  • Invert hollow patterns to build the decreasing family
  • Print rectangles and multi-section star shapes
  • Use conditions such as i % 2 to alternate values
  • Understand while True and break in number diamonds
  • Handle odd and even star counts with range steps

Introduction

Part 10 covers the final pattern programs from Pattern-91 to Pattern-100.

In this part, we will learn:

Pattern-91  → Inverted Hollow Decreasing Number Pattern
Pattern-92  → Inverted Hollow Decreasing Alphabet Pattern
Pattern-93  → Inverted Hollow Increasing Alphabet Pattern
Pattern-94  → Right-Shifted Star Rectangle
Pattern-95  → Double Increasing Star Triangle
Pattern-96  → Alternating 1 and 0 Triangle
Pattern-97  → Multi-Section Star Pattern
Pattern-98  → Even-Star Pyramid
Pattern-99  → Repeated Even-Length Star Rows
Pattern-100 → Large Multi-Section Star Pattern

The important concepts used are:

  • Nested loops
  • Increasing and decreasing spaces
  • Hollow patterns
  • Number patterns
  • Alphabet patterns using chr()
  • Binary patterns
  • Odd and even checking
  • Multiple pattern sections
  • String multiplication
  • Complex star structures

Pattern-91 — Inverted Hollow Decreasing Number Pattern

Pattern-91 continues the inverted hollow pattern family from Pattern-90.

Pattern-90 prints increasing numbers, whereas Pattern-91 prints decreasing numbers.

Output for num = 5

5       5
 4     4
  3   3
   2 2
    1

The numbers decrease:

5 → 4 → 3 → 2 → 1

At the same time:

  • Leading spaces increase.
  • Inner spaces decrease.
  • The two boundaries move toward each other.

Pattern-91 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2for i in range(1,num+1):
3 print(" "*(i-1),end="")
4 for j in range(i,i+1):
5 print(num+1-i,end=" ")
6 if i<num:
7 print(" "*(2*num-2*i-2),end="")
8 for k in range(i,i+1):
9 print(num+1-i,end=" ")
10 print()
Output
No output captured.

Pattern-91 — Step-by-Step Explanation

Step 1 — Read Number

num=int(input("Enter a number:"))

Step 2 — Control Rows

for i in range(1,num+1):

For num = 5:

i = 1, 2, 3, 4, 5

Step 3 — Leading Spaces

" " * (i-1)

The spaces increase:

0, 1, 2, 3, 4

Step 4 — Calculate Number

num + 1 - i

For num = 5:

i = 1 → 5
i = 2 → 4
i = 3 → 3
i = 4 → 2
i = 5 → 1

Step 5 — Inner Spaces

2*num - 2*i - 2

The inner spaces gradually decrease.

Step 6 — Final Row

if i < num:

The second number is not printed on the final row.

Pattern-91 — Execution Flow

Read num
   │
   ▼
Start Row Loop
   │
   ▼
Print i-1 Spaces
   │
   ▼
Calculate num+1-i
   │
   ▼
Print Left Number
   │
   ▼
Is i < num?
   │
 ┌─┴────────────┐
 │ Yes          │ No
 ▼              ▼
Print Inner    Skip Second
Spaces         Number
 │
 ▼
Print Right Number
 │
 └──────┬───────┘
        ▼
     Next Row

Pattern-92 — Inverted Hollow Decreasing Alphabet Pattern

Pattern-92 converts Pattern-91 from numbers into alphabets.

Output for num = 5

E       E
 D     D
  C   C
   B B
    A

The alphabet sequence is:

E → D → C → B → A

Pattern-92 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2for i in range(1,num+1):
3 print(" "*(i-1),end="")
4 for j in range(i,i+1):
5 print(chr(64+num+1-i),end=" ")
6 if i<num:
7 print(" "*(2*num-2*i-2),end="")
8 for k in range(i,i+1):
9 print(chr(64+num+1-i),end=" ")
10 print()
Output
No output captured.

Pattern-92 — Alphabet Formula

The important expression is:

chr(64 + num + 1 - i)

For num = 5:

i = 1 → chr(69) → E
i = 2 → chr(68) → D
i = 3 → chr(67) → C
i = 4 → chr(66) → B
i = 5 → chr(65) → A

Therefore, Pattern-92 is the alphabet version of Pattern-91.

Pattern-92 — Execution Flow

Read num
   │
   ▼
Start Row Loop
   │
   ▼
Print Leading Spaces
   │
   ▼
Calculate Alphabet
chr(64+num+1-i)
   │
   ▼
Print Left Alphabet
   │
   ▼
Print Inner Spaces
   │
   ▼
Print Right Alphabet
   │
   ▼
Next Row

Pattern-93 — Inverted Hollow Increasing Alphabet Pattern

Pattern-93 has the same inverted hollow structure, but alphabets increase from A onward.

Output for num = 5

A       A
 B     B
  C   C
   D D
    E

The sequence is:

A → B → C → D → E

Pattern-93 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2for i in range(1,num+1):
3 print(" "*(i-1),end="")
4 for j in range(i,i+1):
5 print(chr(64+i),end=" ")
6 if i < num:
7 print(" "*(2*num-2*i-2),end="")
8 for k in range(i,i+1):
9 print(chr(64+i),end=" ")
10 print()
Output
No output captured.

Pattern-93 — Step-by-Step Explanation

The alphabet is generated using:

chr(64+i)

Therefore:

i = 1 → A
i = 2 → B
i = 3 → C
i = 4 → D
i = 5 → E

The document uses:

if i <= 4:

For the demonstrated value num = 5, this prevents a second E from appearing on the last row.

The inner-space formula is:

2*num - 2*i - 2

Pattern-91 to Pattern-93 — Comparison

Pattern Value Direction Expression
91 Number 5 → 1 num+1-i
92 Alphabet E → A chr(64+num+1-i)
93 Alphabet A → E chr(64+i)

Pattern-94 — Right-Shifted Star Rectangle

Pattern-94 prints the same number of stars in every row.

However, every new row gets one additional leading space.

Output for num = 5

* * * * *
 * * * * *
  * * * * *
   * * * * *
    * * * * *

This creates a slanted or right-shifted rectangle.

Pattern-94 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2for i in range(1,num+1):
3 print(" "*(i-1),end="")
4 for j in range(1,num+1):
5 print("*",end=" ")
6 print()
Output
No output captured.

Pattern-94 — Program Explanation

The outer loop controls rows:

for i in range(1,num+1):

The leading spaces are:

i - 1

The inner loop always executes num times:

for j in range(1,num+1):

Therefore every row contains the same number of stars.

For num = 5

Row 1 → 0 leading spaces + 5 stars
Row 2 → 1 leading space  + 5 stars
Row 3 → 2 leading spaces + 5 stars
Row 4 → 3 leading spaces + 5 stars
Row 5 → 4 leading spaces + 5 stars

Pattern-94 — Execution Flow

Read num
   │
   ▼
Start Outer Loop
   │
   ▼
Print i-1 Spaces
   │
   ▼
Run Inner Loop num Times
   │
   ▼
Print *
   │
   ▼
Move to New Line
   │
   ▼
Next Row

Pattern-95 — Double Increasing Star Triangle

Pattern-95 prints two increasing star groups in every row.

Output for num = 5

    *     *
   * *    * *
  * * *   * * *
 * * * *  * * * *
* * * * * * * * * *

Each row contains:

Left Star Group + Spaces + Right Star Group

Both star groups increase together.

Pattern-95 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2for i in range(1,num+1):
3 print(" "*(num-i),end="")
4 for j in range(1,i+1):
5 print("*",end=" ")
6 print(" "*(num-i),end="")
7 for k in range(1,i+1):
8 print("*",end=" ")
9 print()
Output
No output captured.

Pattern-95 — Step-by-Step Explanation

First, leading spaces are printed:

" " * (num-i)

Then the first star group is created:

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

The middle spacing also uses:

" " * (num-i)

Finally, another star group is created:

for k in range(1,i+1):
    print("*",end=" ")

Star Counts

Row 1 → 1 + 1 = 2 stars
Row 2 → 2 + 2 = 4 stars
Row 3 → 3 + 3 = 6 stars
Row 4 → 4 + 4 = 8 stars
Row 5 → 5 + 5 = 10 stars

Pattern-95 — Execution Flow

Read num
   │
   ▼
Start Row
   │
   ▼
Print Leading Spaces
   │
   ▼
Print i Stars
   │
   ▼
Print Middle Spaces
   │
   ▼
Print i Stars Again
   │
   ▼
New Line

Pattern-96 — Alternating 1 and 0 Triangle

Pattern-96 is a binary triangle containing alternating 1 and 0.

Output

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
0 1 0 1 0 1
1 0 1 0 1 0 1

Every row contains exactly as many values as its row number.

Pattern-96 — Complete Program

🐍Code Cell
1n=int(input("Enter a number:"))
2for i in range(1,n+1):
3 for j in range(1,i+1):
4 if (i%2!=0 and j%2!=0) or (i%2==0 and j%2==0):
5 print("1",end=" ")
6 else:
7 print("0",end=" ")
8 print()
Output
No output captured.

Pattern-96 — Understanding the Condition

The most important condition is:

(i%2!=0 and j%2!=0) or (i%2==0 and j%2==0)

A 1 is printed when:

i is odd  AND j is odd

OR

i is even AND j is even

In simple words:

Same parity → 1
Different parity → 0

Pattern-96 — Condition Table

i j Condition Output
Odd Odd True 1
Odd Even False 0
Even Odd False 0
Even Even True 1

Pattern-96 — Row-by-Row Explanation

Row 1

i = 1

j = 1
Odd + Odd → 1

Output:
1

Row 2

i = 2

j = 1 → Even + Odd  → 0
j = 2 → Even + Even → 1

Output:
0 1

Row 3

i = 3

j = 1 → Odd + Odd  → 1
j = 2 → Odd + Even → 0
j = 3 → Odd + Odd  → 1

Output:
1 0 1

Pattern-96 — Execution Flow

Read n
   │
   ▼
Start Row Loop i
   │
   ▼
Start Column Loop j
   │
   ▼
Check Parity of i and j
   │
 ┌─┴────────────────┐
 │ Same             │ Different
 ▼                  ▼
Print 1           Print 0
 │                  │
 └────────┬─────────┘
          ▼
      Next Column
          │
          ▼
       New Line

Pattern-97 — Multi-Section Star Pattern

Pattern-97 is a special pattern created using multiple independent loop sections.

Unlike a normal triangle, this pattern combines several growing star sections followed by a vertical section.

The document builds this pattern using four loop blocks.

Pattern-97 — Complete Program

🐍Code Cell
1num=int(input("Enter a number:"))
2 
3for i in range(1,num+1):
4 print(" "*(2*num-i+3),end="")
5 for j in range(1,i+1):
6 print("*",end=" ")
7 print()
8 
9for i in range(1,num+3):
10 print(" "*(2*num-i+1),end="")
11 for j in range(-1,i+1):
12 print("*",end=" ")
13 print()
14 
15for i in range(1,num+5):
16 print(" "*(2*num-i),end="")
17 for j in range(-2,i+1):
18 print("*",end=" ")
19 print()
20 
21for i in range(1,num+3):
22 print(" "*(2*num),end="")
23 print("* "*3)
Output
No output captured.

Pattern-97 — First Section

The first section is:

for i in range(1,num+1):
    print(" "*(2*num-i+3),end="")
    for j in range(1,i+1):
        print("*",end=" ")

The number of stars increases normally:

1, 2, 3, 4, 5 ...

The leading-space expression is:

2*num - i + 3

Pattern-97 — Second Section

The second block uses:

for i in range(1,num+3):

Its inner loop is:

for j in range(-1,i+1):

Because the range starts from -1, more iterations occur than in a standard range(1,i+1).

This creates a wider star section.

Pattern-97 — Third and Fourth Sections

The third section increases the width further using:

for j in range(-2,i+1):

The final section is:

for i in range(1,num+3):
    print(" "*(2*num),end="")
    print("* "*3)

This repeatedly prints:

* * *

at a fixed horizontal position, creating the final vertical portion of the design.

Pattern-97 — Execution Flow

Read num
   │
   ▼
Section 1
Normal Growing Triangle
   │
   ▼
Section 2
Wider Growing Triangle
   │
   ▼
Section 3
Still Wider Triangle
   │
   ▼
Section 4
Fixed 3-Star Rows
   │
   ▼
Final Combined Pattern

Pattern-98 — Even-Star Pyramid

Pattern-98 prints an increasing pyramid where every row contains an even number of stars.

Output for n = 5

        * *
      * * * *
    * * * * * *
  * * * * * * * *
* * * * * * * * * *

The star counts are:

2
4
6
8
10

Pattern-98 — Complete Program

🐍Code Cell
1n=int(input("Enter a number:"))
2for i in range(1,n+1):
3 print(" "*(2*(n-i)),end="")
4 print("* "*(2*i))
Output
No output captured.

Pattern-98 — Step-by-Step Explanation

Leading Spaces

2 * (n-i)

For n = 5:

i = 1 → 8 spaces
i = 2 → 6 spaces
i = 3 → 4 spaces
i = 4 → 2 spaces
i = 5 → 0 spaces

Stars

"* " * (2*i)

The number of stars is:

i = 1 → 2
i = 2 → 4
i = 3 → 6
i = 4 → 8
i = 5 → 10

Therefore, the number of stars always remains even.

Pattern-98 — Execution Flow

Read n
   │
   ▼
Start Row Loop
   │
   ▼
Calculate 2*(n-i)
   │
   ▼
Print Leading Spaces
   │
   ▼
Calculate 2*i
   │
   ▼
Print Stars
   │
   ▼
Next Row

Pattern-99 — Repeated Even-Length Star Rows

Pattern-99 prints star rows in pairs.

Output for n = 5

**
**
****
****
******
******
********
********
**********
**********

Each even star count appears twice:

2 stars  → 2 times
4 stars  → 2 times
6 stars  → 2 times
8 stars  → 2 times
10 stars → 2 times

Pattern-99 — Complete Program

🐍Code Cell
1n=int(input("Enter a number"))
2for i in range(1,2*n+1):
3 if i%2==0:
4 print("*"*i,end=" ")
5 else:
6 print("*"*(i+1),end=" ")
7 print()
Output
No output captured.

Pattern-99 — Understanding the Logic

The outer loop executes:

2 * n

times.

For n = 5:

i = 1 to 10

The program checks:

i % 2 == 0

If i is Even

print("*"*i)

If i is Odd

print("*"*(i+1))

Therefore:

i = 1 → i+1 = 2 → **
i = 2 → i   = 2 → **

i = 3 → i+1 = 4 → ****
i = 4 → i   = 4 → ****

i = 5 → i+1 = 6 → ******
i = 6 → i   = 6 → ******

This is why each even-sized row appears twice.

Pattern-99 — Execution Flow

Read n
   │
   ▼
Loop i from 1 to 2*n
   │
   ▼
Is i Even?
   │
 ┌─┴─────────────┐
 │ Yes           │ No
 ▼               ▼
Print i Stars   Print i+1 Stars
 │               │
 └───────┬───────┘
         ▼
      New Line
         │
         ▼
      Next i

Pattern-100 — Large Multi-Section Star Pattern

Pattern-100 is the final pattern in the document.

It is a large composite star pattern created by repeatedly generating growing star sections and finally adding a fixed-width vertical section.

The program uses:

  • An outer loop with a step value of 2
  • A nested row loop
  • Dynamic leading spaces
  • Dynamic star counts
  • A separate final loop

This makes Pattern-100 one of the more complex patterns in the chapter.

Pattern-100 — Complete Program

🐍Code Cell
1n=int(input("Enter a number:"))
2 
3for a in range(1,n+1,2):
4 for i in range(1,n+1):
5 print(" "*(2*n-i-a),end="")
6 for j in range(1,i+a):
7 print("*",end=" ")
8 print()
9 
10for b in range(1,n+1):
11 print(" "*(n-2),end="")
12 print("* "*3)
Output
No output captured.

Pattern-100 — Outer Loop

The first important loop is:

for a in range(1,n+1,2):

The third argument of range() is:

2

Therefore a increases by 2.

For example, if:

n = 5

then:

a = 1, 3, 5

This means the program creates multiple star sections with increasing starting widths.

Pattern-100 — Row Loop

Inside every value of a, another loop runs:

for i in range(1,n+1):

Therefore each major section contains n rows.

The leading spaces are calculated using:

2*n - i - a

As i increases, the leading spaces decrease.

As a increases, the complete section also shifts.

Pattern-100 — Star Loop

The stars are generated using:

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

The number of iterations is approximately controlled by:

i + a - 1

Therefore both variables affect the width:

i → controls growth inside a section
a → controls starting width of each new section

Example

If:

a = 1

then the star counts grow approximately as:

1, 2, 3, 4, 5

If:

a = 3

the section begins wider:

3, 4, 5, 6, 7

Pattern-100 — Final Section

After all growing sections are completed, the document uses:

for b in range(1,n+1):
    print(" "*(n-2),end="")
    print("* "*3)

This loop executes n times.

Every iteration prints:

* * *

The number of leading spaces remains constant:

n - 2

Therefore this section creates a vertical block or trunk at the bottom of the complete pattern.

Pattern-100 — Execution Flow

                    START
                      │
                      ▼
                    Read n
                      │
                      ▼
          a = 1, 3, 5, ... n
                      │
                      ▼
             Run i from 1 to n
                      │
                      ▼
        Calculate Leading Spaces
              2*n-i-a
                      │
                      ▼
             Print Leading Spaces
                      │
                      ▼
          Run j from 1 to i+a
                      │
                      ▼
                 Print Stars
                      │
                      ▼
                 Next Row
                      │
                      ▼
               Next a Section
                      │
                      ▼
             Complete Sections
                      │
                      ▼
            Run Final b Loop
                      │
                      ▼
             Print Fixed Spaces
                      │
                      ▼
                Print * * *
                      │
                      ▼
                     END

Understanding the Hollow Pattern Family

Patterns 84 to 93 form an important hollow-pattern family.

The general idea is:

Left Boundary + Inner Spaces + Right Boundary

Increasing Hollow Shape

    *
   * *
  *   *
 *     *
*       *

Used in Patterns 84–88.

Decreasing Hollow Shape

*       *
 *     *
  *   *
   * *
    *

Used in Patterns 89–93.

Increasing vs Decreasing Hollow Space Formulas

Pattern Type Leading Spaces Inner Spaces
Increasing Hollow num-i 2*i-4
Decreasing Hollow i-1 2*num-2*i-2

For an increasing hollow pattern:

Leading spaces ↓
Inner spaces   ↑

For a decreasing hollow pattern:

Leading spaces ↑
Inner spaces   ↓

Pattern-90 to Pattern-93 — Value Comparison

Pattern Output Type Sequence
90 Numbers 1 → 5
91 Numbers 5 → 1
92 Alphabets E → A
93 Alphabets A → E

Understanding Odd and Even Checking

Patterns 96 and 99 make important use of the modulus operator.

Even Number

number % 2 == 0

Odd Number

number % 2 != 0

Pattern-96 compares the parity of the row and column positions.

Pattern-99 checks whether the current row counter is odd or even.

Important Formulas in Part 10

Increasing Leading Spaces

i - 1

Decreasing Hollow Inner Spaces

2*num - 2*i - 2

Decreasing Number

num + 1 - i

Decreasing Alphabet

chr(64 + num + 1 - i)

Increasing Alphabet

chr(64+i)

Pattern-95 Spaces

num - i

Pattern-98 Spaces

2*(n-i)

Pattern-98 Star Count

2*i

Pattern-100 Spaces

2*n-i-a

Pattern-100 Star Growth

range(1,i+a)

Pattern-91 to Pattern-100 — Complete Comparison

Pattern Pattern Type Main Concept
91 Hollow Number Decreasing numbers
92 Hollow Alphabet Decreasing alphabets
93 Hollow Alphabet Increasing alphabets
94 Star Rectangle Increasing leading spaces
95 Double Star Triangle Two increasing star groups
96 Binary Triangle Odd/even row-column checking
97 Special Star Pattern Multiple loop sections
98 Even-Star Pyramid 2, 4, 6, 8... stars
99 Repeated Star Rows Each even width appears twice
100 Complex Star Pattern Nested multi-section construction

Part 10 — Important Notes

  • Pattern-91 prints decreasing numbers in an inverted hollow structure.
  • Pattern-92 converts the same structure into decreasing alphabets.
  • Pattern-93 prints increasing alphabets.
  • The decreasing hollow inner-space formula is 2*num-2*i-2.
  • Pattern-94 prints a fixed number of stars while shifting each row toward the right.
  • Pattern-95 prints two star groups whose sizes increase together.
  • Pattern-96 introduces an alternating binary triangle.
  • Pattern-96 prints 1 when the row and column have matching parity.
  • Pattern-97 is built from four separate loop sections.
  • Pattern-98 always prints an even number of stars.
  • Pattern-98 uses 2*i for star count.
  • Pattern-99 prints every even-sized star row twice.
  • Pattern-99 uses i%2==0 to distinguish even and odd iterations.
  • Pattern-100 uses range(1,n+1,2), so the outer variable increases by 2.
  • Pattern-100 combines multiple growing sections with a final fixed-width section.
  • Pattern-100 is the final pattern in the uploaded document.

Part 10 — Quick Revision

                     PATTERN 91-100
                           │
        ┌──────────────────┼──────────────────┐
        │                  │                  │
        ▼                  ▼                  ▼
   Pattern 91-93      Pattern 94-95      Pattern 96-100
      HOLLOW              STAR              SPECIAL
      FAMILY            SHAPES             PATTERNS
        │                  │                  │
   ┌────┼────┐        ┌────┴────┐      ┌──────┼────────┐
   ▼    ▼    ▼        ▼         ▼      ▼      ▼        ▼
Number Alpha Alpha  Shifted   Double Binary Complex  Repeated
  ↓      ↓     ↑    Rectangle Triangle Triangle Stars  Stars

Pattern Programs Chapter — Final Summary

We have now completed the complete sequence of pattern programs from Pattern-1 to Pattern-100.

Across these 100 programs, we learned how to create:

  • Square patterns
  • Rectangular patterns
  • Increasing triangles
  • Decreasing triangles
  • Right-aligned triangles
  • Number patterns
  • Alphabet patterns
  • Increasing pyramids
  • Decreasing pyramids
  • Symmetrical pyramids
  • Diamond patterns
  • Combined upper and lower patterns
  • Hollow patterns
  • Binary patterns
  • Special multi-section star patterns

Core Pattern Programming Formula

Outer Loop
    │
    ├── Controls Rows
    │
    ▼
Spaces
    │
    ├── Control Alignment
    │
    ▼
Inner Loop
    │
    ├── Controls Columns / Values
    │
    ▼
Print *, Number or Alphabet
    │
    ▼
Move to Next Line

The most important skill in pattern programming is not memorizing the programs.

We should understand how the following values change from one row to another:

1. Number of rows
2. Number of columns
3. Number of leading spaces
4. Number of inner spaces
5. Number of printed values
6. Value printed at each position

Once these relationships are understood, complex patterns can be divided into smaller and easier sections.

📝 Key Takeaways
  • i % 2 == 0 checks whether a row index is even
  • Multi-section patterns divide the shape into several loop sections
  • range(1, n+1, 2) generates odd numbers
  • while True runs until a break condition is reached

🧠 Test Your Knowledge

18 Questions
Progress: 0 / 18