Nearby lessons

55 of 159

Python - Pyramid Patterns

📌 What You Will Learn
  • Understand the 2*i-1 formula that controls pyramid width
  • Build star, number, and alphabet pyramids
  • Create palindrome pyramids using two inner loops
  • Reverse a pyramid to produce the inverted family
  • Compare increasing and inverted pyramid logic

Introduction to Pyramid Patterns

Pattern-34 starts a new category: Pyramid Patterns.

A normal pyramid follows two important formulas:

Leading Spaces = n - i

Values = 2 * i - 1

For n = 5:

i Spaces 2*i-1
141
233
325
417
509

Therefore the number of values follows the odd-number sequence:

1, 3, 5, 7, 9, ...

Pattern-34 — Star Pyramid

Pattern-34 creates a pyramid using stars.

Output

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

The source program uses:

"* " * (2*i-1)

So every row contains an odd number of stars.

Pattern-34 — Complete Program

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

Pattern-34 — Program Explanation

The expression:

" " * (n-i)

controls the leading spaces.

The expression:

"* " * (2*i-1)

controls the number of stars.

For each row:

i = 1 → 2(1)-1 = 1
i = 2 → 2(2)-1 = 3
i = 3 → 2(3)-1 = 5
i = 4 → 2(4)-1 = 7
i = 5 → 2(5)-1 = 9

Pattern-34 — Dry Run

Row Spaces Stars
141
233
325
417
509

Pattern-35 — Repeated Number Pyramid

Output

    1
   2 2 2
  3 3 3 3 3
 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5

The row number is repeated an odd number of times.

Pattern-35 — Complete Program

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

Pattern-35 — Step-by-Step Explanation

The current number is:

i

It is converted into a string:

str(i)

A space is added:

str(i) + " "

Then it is repeated:

2*i - 1

Example for i = 4:

2*4 - 1
= 7

"4 " * 7

4 4 4 4 4 4 4

Pattern-36 — Repeated Alphabet Pyramid

Output

    A
   B B B
  C C C C C
 D D D D D D D
E E E E E E E E E

The alphabet changes according to the row.

The repetition count follows:

1, 3, 5, 7, 9

Pattern-36 — Complete Program

🐍Code Cell
1n=int(input("Enter the number of rows: "))
2for i in range(1,n+1):
3 print(" "*(n-i) + (str(chr(64+i)+" "))*(2*i-1))
Output
No output captured.

Pattern-36 — Program Explanation

The alphabet formula is:

chr(64+i)

Therefore:

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

The repetition count is:

2*i - 1

For row 3:

Character = C

Repetitions =
2*3 - 1
= 5

C C C C C

Pattern-37 — Odd Alphabet Pyramid

Output

    A
   C C C
  E E E E E
 G G G G G G G
I I I I I I I I I

This pattern skips one alphabet after every row.

A → C → E → G → I

Pattern-37 — Complete Program

🐍Code Cell
1n=int(input("Enter the number of rows: "))
2for i in range(1,n+1):
3 print(" "*(n-i) + (str(chr(63+2*i)+" "))*(2*i-1))
Output
No output captured.

Pattern-37 — Character Formula Explanation

The important formula is:

chr(63 + 2*i)

For each row:

i = 1

63 + 2
= 65

chr(65)
= A
i = 2

63 + 4
= 67

chr(67)
= C
i = 3

63 + 6
= 69

chr(69)
= E

Therefore:

A, C, E, G, I

Again, the repetition count is:

2*i - 1

Pattern-38 — Palindrome Alphabet Pyramid

Pattern-38 is more advanced because every row contains an increasing and decreasing alphabet sequence.

Output

    A
   A B A
  A B C B A
 A B C D C B A
A B C D E D C B A

This is an alphabet palindrome pattern.

Each row reads the same from left to right and right to left.

Pattern-38 — Complete Program

🐍Code Cell
1n=int(input("Enter the number of rows: "))
2for i in range(1,n+1):
3 print(" "*(n-i),end="")
4 for j in range(65,65+i):
5 print(chr(j),end=" ")
6 for k in range(63+i,64,-1):
7 print(chr(k),end=" ")
8 print()
Output
No output captured.

Pattern-38 — First Inner Loop

The first inner loop is:

for j in range(65,65+i):
    print(chr(j),end=" ")

It prints alphabets in increasing order.

For i = 5:

65 → A
66 → B
67 → C
68 → D
69 → E

A B C D E

Pattern-38 — Second Inner Loop

The second inner loop is:

for k in range(63+i,64,-1):
    print(chr(k),end=" ")

It prints the remaining alphabets in reverse order.

For i = 5:

k starts at:

63 + 5
= 68

chr(68) = D

Then:

68 → D
67 → C
66 → B
65 → A

So the complete row becomes:

A B C D E + D C B A

A B C D E D C B A

Pattern-38 — Row-by-Row Logic

i Increasing Part Decreasing Part Complete Row
1 A - A
2 A B A A B A
3 A B C B A A B C B A
4 A B C D C B A A B C D C B A
5 A B C D E D C B A A B C D E D C B A

Pattern-39 — Reverse Odd Number Pyramid

Output

    1
   3 2 1
  5 4 3 2 1
 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1

Each row starts with an odd number and decreases until 1.

Pattern-39 — Complete Program

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

Pattern-39 — Step-by-Step Explanation

The starting number is:

2*i - 1

Therefore:

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

The loop:

range(2*i-1,0,-1)

counts backward until 1.

For i = 4:

2*4 - 1
= 7

range(7,0,-1)

7 6 5 4 3 2 1

Pattern-39 — Dry Run

i Starting Value Output
111
233 2 1
355 4 3 2 1
477 6 5 4 3 2 1
599 8 7 6 5 4 3 2 1

Pattern-40 — Increasing Alphabet Pyramid

Output

    A
   A B C
  A B C D E
 A B C D E F G
A B C D E F G H I

Every row starts from A.

The number of alphabets follows:

1, 3, 5, 7, 9

Pattern-40 — Complete Program

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

Pattern-40 — Step-by-Step Explanation

The alphabet loop is:

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

The loop always starts at ASCII:

65 → A

The number of iterations is:

2*i - 1

For row 3:

2*3 - 1
= 5

Therefore five alphabets are printed:

A B C D E

For row 5:

2*5 - 1
= 9

A B C D E F G H I

Pattern-34 to Pattern-40 — Pyramid Comparison

Pattern Pyramid Content Main Formula
34 Stars 2*i-1 stars
35 Repeated row number i repeated 2*i-1 times
36 Repeated row alphabet chr(64+i)
37 Odd-position alphabet chr(63+2*i)
38 Alphabet palindrome Increasing + decreasing alphabets
39 Reverse numbers 2*i-1 down to 1
40 Increasing alphabets 2*i-1 alphabets

Understanding the 2*i-1 Formula

The expression:

2*i - 1

generates consecutive odd numbers.

i Calculation Result
12 × 1 - 11
22 × 2 - 13
32 × 3 - 15
42 × 4 - 17
52 × 5 - 19

This formula is useful because a pyramid normally grows symmetrically:

        1
      3 values
    5 values
  7 values
9 values

Part 4 — Complete Formula Table

Pattern Leading Spaces Main Logic
31 i-1 j = 1 to n+1-i
32 i-1 chr(65+n-i) repeated n+1-i
33 i-1 A to decreasing endpoint
34 n-i 2*i-1 stars
35 n-i i repeated 2*i-1
36 n-i chr(64+i) repeated 2*i-1
37 n-i chr(63+2*i) repeated 2*i-1
38 n-i Increasing + decreasing alphabet
39 n-i 2*i-1 down to 1
40 n-i Print 2*i-1 alphabets from A

Part 4 — Execution Flow

                         START
                           │
                           ▼
                        Read n
                           │
                           ▼
                    Outer Loop (i)
                           │
                           ▼
                  Identify Pattern
                           │
              ┌────────────┴────────────┐
              │                         │
              ▼                         ▼
        Pattern 31-33              Pattern 34-40
              │                         │
              ▼                         ▼
       Decreasing Shape               Pyramid
              │                         │
              ▼                         ▼
     Spaces = i - 1             Spaces = n - i
              │                         │
              ▼                         ▼
 Values = n + 1 - i          Values usually = 2*i-1
              │                         │
              └────────────┬────────────┘
                           │
                           ▼
                    Print Spaces
                           │
                           ▼
                   Generate Values
                           │
              ┌────────────┼────────────┐
              ▼            ▼            ▼
           Stars        Numbers      Alphabets
              │            │            │
              └────────────┴────────────┘
                           │
                           ▼
                      Print Row
                           │
                           ▼
                       Next Row
                           │
                           ▼
                          END

Part 4 — Important Notes

  • Patterns 31–33 use increasing leading spaces and decreasing values.
  • The common space formula for Patterns 31–33 is i-1.
  • Pattern-31 always starts its number sequence from 1.
  • Pattern-32 uses chr(65+n-i) to generate decreasing alphabets.
  • Pattern-33 always starts its alphabet sequence from A.
  • Pattern-34 begins the pyramid-pattern section.
  • The most important pyramid formula is 2*i-1.
  • 2*i-1 generates the sequence 1, 3, 5, 7, 9, ....
  • The common leading-space formula for these increasing pyramids is n-i.
  • Pattern-37 uses chr(63+2*i) to generate A, C, E, G, I....
  • Pattern-38 requires two inner loops because it first increases alphabets and then decreases them.
  • Pattern-39 uses a negative step -1 to print numbers in reverse order.
  • Pattern-40 combines ASCII values with the 2*i-1 pyramid formula.

Part 4 — Summary

Pattern Output Type Key Concept
31 Decreasing numbers Increasing spaces
32 Repeated decreasing alphabet ASCII + repetition
33 Decreasing alphabet sequence ASCII range
34 Star pyramid 2*i-1 stars
35 Number pyramid Repeated row number
36 Alphabet pyramid Repeated row alphabet
37 Odd alphabet pyramid A, C, E, G, I
38 Palindrome alphabet pyramid Forward + backward loops
39 Reverse number pyramid Odd number to 1
40 Alphabet pyramid 2*i-1 alphabets

Part 4 — Quick Revision

                    PATTERN 31-40
                          │
          ┌───────────────┴───────────────┐
          │                               │
          ▼                               ▼
     Pattern 31-33                   Pattern 34-40
          │                               │
          ▼                               ▼
 Decreasing Right-Shifted               Pyramid
          │                               │
          ▼                               ▼
 Spaces = i - 1                   Spaces = n - i
          │                               │
          ▼                               ▼
Values = n + 1 - i              Values ≈ 2*i - 1
          │                               │
     ┌────┴────┐             ┌────────────┼────────────┐
     ▼         ▼             ▼            ▼            ▼
  Numbers   Alphabets      Stars       Numbers      Alphabets
                                                    │
                                      ┌─────────────┼─────────────┐
                                      ▼             ▼             ▼
                                   Repeated     Palindrome     Sequence

Introduction

Part 5 covers Pattern-41 to Pattern-50.

In this part, we will learn:

Pattern-41 → Reverse Alphabet Pyramid
Pattern-42 → Reverse-to-Forward Number Pyramid
Pattern-43 → Reverse-to-Forward Alphabet Pyramid
Pattern-44 → Palindrome Number Pyramid
Pattern-45 → Repeated Increasing Alphabet Pyramid
Pattern-46 → Descending Number Triangle
Pattern-47 → Inverted Star Pyramid
Pattern-48 → Inverted Repeated Number Pyramid
Pattern-49 → Inverted Odd Number Pyramid
Pattern-50 → Inverted Number Sequence Pattern

Important concepts used in this part are:

  • Nested loops
  • Increasing and decreasing ranges
  • Negative step in range()
  • ASCII values with chr()
  • Leading spaces
  • Odd-number formulas
  • Two inner loops in the same row
  • Increasing and inverted pyramids

Pattern-41 — Reverse Alphabet Pyramid

Pattern-41 prints alphabets in reverse order.

The starting alphabet increases by two positions in every row.

Output

    A
   C B A
  E D C B A
 G F E D C B A
I H G F E D C B A

Observe the first alphabet of every row:

A
C
E
G
I

These are alternate alphabets.

After selecting the starting alphabet, the program prints backward until A.

Pattern-41 — Complete Program

🐍Code Cell
1n=int(input("Enter the number of rows: "))
2for i in range(1,n+1):
3 print(" "*(n-i),end="")
4 for j in range(65+2*i-2,64,-1):
5 print(chr(j),end=" ")
6 print()
Output
No output captured.

Pattern-41 — Step-by-Step Explanation

Step 1 — Read Number of Rows

n=int(input("Enter the number of rows: "))

The variable n stores the number of rows.

Step 2 — Outer Loop

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

The outer loop controls the rows.

Step 3 — Print Leading Spaces

print(" "*(n-i),end="")

The number of spaces decreases in every row.

n = 5

i = 1 → 4 spaces
i = 2 → 3 spaces
i = 3 → 2 spaces
i = 4 → 1 space
i = 5 → 0 spaces

Step 4 — Calculate Starting Alphabet

65 + 2*i - 2

For different rows:

i = 1 → 65 → A
i = 2 → 67 → C
i = 3 → 69 → E
i = 4 → 71 → G
i = 5 → 73 → I

Step 5 — Print in Reverse Order

range(65+2*i-2,64,-1)

The value decreases by 1 until ASCII value 65.

For row 4:

Starting ASCII = 71

71 → G
70 → F
69 → E
68 → D
67 → C
66 → B
65 → A

Therefore:

G F E D C B A

Pattern-41 — Dry Run

i Start ASCII Start Character Output
165AA
267CC B A
369EE D C B A
471GG F E D C B A
573II H G F E D C B A

Pattern-42 — Reverse-to-Forward Number Pyramid

Pattern-42 creates a number pyramid using two parts.

The first part prints numbers in decreasing order and the second part prints numbers in increasing order.

Output

    0
   1 0 1
  2 1 0 1 2
 3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4

The center value of every row is:

0

The values are symmetrical around 0.

Pattern-42 — Complete Program

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

Pattern-42 — First Inner Loop

The first inner loop is:

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

It prints decreasing values before 0.

For i = 5:

j = 1 → 5-1 = 4
j = 2 → 5-2 = 3
j = 3 → 5-3 = 2
j = 4 → 5-4 = 1

Output:

4 3 2 1

Pattern-42 — Second Inner Loop

The second inner loop is:

for k in range(0,i):
    print(k,end=" ")

It prints:

0 1 2 ... i-1

For i = 5:

0 1 2 3 4

Combining both loops:

4 3 2 1 + 0 1 2 3 4

4 3 2 1 0 1 2 3 4

Pattern-42 — Row-by-Row Logic

Row Left Part Right Part Complete Output
1-00
210 11 0 1
32 10 1 22 1 0 1 2
43 2 10 1 2 33 2 1 0 1 2 3
54 3 2 10 1 2 3 44 3 2 1 0 1 2 3 4

Pattern-43 — Reverse-to-Forward Alphabet Pyramid

Pattern-43 is the alphabet version of Pattern-42.

Output

    A
   B A B
  C B A B C
 D C B A B C D
E D C B A B C D E

The pattern decreases toward A and then increases again.

Pattern-43 — Complete Program

🐍Code Cell
1n=int(input("Enter the number of rows: "))
2for i in range(1,n+1):
3 print(" "*(n-i),end="")
4 for j in range(1,i):
5 print(chr(i-j+65),end=" ")
6 for k in range(0,i):
7 print(chr(k+65),end=" ")
8 print()
Output
No output captured.

Pattern-43 — Step-by-Step Explanation

First Part

for j in range(1,i):
    print(chr(i-j+65),end=" ")

This prints alphabets in decreasing order.

For i = 5:

j = 1 → chr(69) → E
j = 2 → chr(68) → D
j = 3 → chr(67) → C
j = 4 → chr(66) → B

First part:

E D C B

Second Part

for k in range(0,i):
    print(chr(k+65),end=" ")

For i = 5:

k = 0 → A
k = 1 → B
k = 2 → C
k = 3 → D
k = 4 → E

Second part:

A B C D E

Complete row:

E D C B A B C D E

Pattern-42 vs Pattern-43

Feature Pattern-42 Pattern-43
Data TypeNumbersAlphabets
Center0A
Left SideDecreasing numbersDecreasing alphabets
Right SideIncreasing numbersIncreasing alphabets
Character ConversionNot requiredchr()

Pattern-44 — Palindrome Number Pyramid

Output

    1
   1 2 1
  1 2 3 2 1
 1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Every row first increases from 1 to the row number and then decreases back to 1.

Therefore every row forms a numeric palindrome.

Pattern-44 — Complete Program

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

Pattern-44 — Increasing Part

The first loop is:

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

For row 5:

1 2 3 4 5

Pattern-44 — Decreasing Part

The second loop is:

for k in range(i-1,0,-1):
    print(k,end=" ")

The loop starts from:

i - 1

This prevents the highest value from being printed twice.

For row 5:

4 3 2 1

Complete row:

1 2 3 4 5 4 3 2 1

Pattern-44 — Dry Run

i Increasing Part Decreasing Part Output
11-1
21 211 2 1
31 2 32 11 2 3 2 1
41 2 3 43 2 11 2 3 4 3 2 1
51 2 3 4 54 3 2 11 2 3 4 5 4 3 2 1

Pattern-45 — Repeated Increasing Alphabet Pyramid

Output

    A
   A B A
  A B C A B
 A B C D A B C
A B C D E A B C D

Each row contains two alphabet sequences.

The first sequence prints from A to the current row alphabet.

The second sequence again starts from A, but stops one alphabet earlier.

Pattern-45 — Complete Program

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

Pattern-45 — First Alphabet Sequence

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

For row 5:

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

Result:

A B C D E

Pattern-45 — Second Alphabet Sequence

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

For row 5:

A B C D

Therefore the complete row is:

A B C D E A B C D

The second loop uses:

range(1,i)

instead of:

range(1,i+1)

Therefore it prints one fewer alphabet.

Pattern-46 — Descending Number Triangle

Pattern-46 prints numbers starting from n and decreases toward smaller values.

The number of values increases row by row.

Program Logic Output for n = 5

    5
   5 4
  5 4 3
 5 4 3 2
5 4 3 2 1

Pattern-46 — Complete Program

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

Pattern-46 — Step-by-Step Explanation

The value printed is calculated using:

n + 1 - j

If n = 5:

j = 1 → 5+1-1 = 5
j = 2 → 5+1-2 = 4
j = 3 → 5+1-3 = 3
j = 4 → 5+1-4 = 2
j = 5 → 5+1-5 = 1

But the inner loop executes only i times:

range(1,i+1)

Therefore:

Row 1 → 5
Row 2 → 5 4
Row 3 → 5 4 3
Row 4 → 5 4 3 2
Row 5 → 5 4 3 2 1

Pattern-46 — Formula

Leading Spaces = n - i

Number of Values = i

Printed Value = n + 1 - j

Pattern-47 — Inverted Star Pyramid

Pattern-47 begins the inverted/decreasing pyramid section.

Output

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

The number of stars follows:

9
7
5
3
1

At the same time, leading spaces increase.

Pattern-47 — 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+2-i):
5 print("*",end=" ")
6 for k in range(1,num+1-i):
7 print("*",end=" ")
8 print()
Output
No output captured.

Pattern-47 — First Star Loop

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

The first loop prints:

num + 1 - i

stars.

For num = 5:

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

Pattern-47 — Second Star Loop

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

The second loop prints:

num - i

additional stars.

Therefore the total is:

(num + 1 - i) + (num - i)

= 2*num + 1 - 2*i

For num = 5:

i = 1 → 9 stars
i = 2 → 7 stars
i = 3 → 5 stars
i = 4 → 3 stars
i = 5 → 1 star

Pattern-47 — Dry Run

i Spaces First Loop Second Loop Total Stars
10549
21437
32325
43213
54101

Pattern-48 — Inverted Repeated Number Pyramid

Output

5 5 5 5 5 5 5 5 5
 4 4 4 4 4 4 4
  3 3 3 3 3
   2 2 2
    1

The row value decreases:

5 → 4 → 3 → 2 → 1

The number of repetitions also decreases:

9 → 7 → 5 → 3 → 1

Pattern-48 — 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(0,num+1-i):
5 print(num+1-i,end=" ")
6 for k in range(1,num+1-i):
7 print(num+1-i,end=" ")
8 print()
Output
No output captured.

Pattern-48 — Printed Number

The number printed in each row is:

num + 1 - i

For num = 5:

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

Pattern-48 — Repetition Logic

Two loops print the same number.

First Loop

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

Second Loop

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

Together they generate:

2*num + 1 - 2*i

values.

Therefore:

Row 1 → 9 copies of 5
Row 2 → 7 copies of 4
Row 3 → 5 copies of 3
Row 4 → 3 copies of 2
Row 5 → 1 copy of 1

Pattern-49 — Inverted Odd Number Pyramid

Output

9 9 9 9 9 9 9 9 9
 7 7 7 7 7 7 7
  5 5 5 5 5
   3 3 3
    1

This pattern prints decreasing odd numbers.

9
7
5
3
1

The number itself is also repeated the same number of times.

Pattern-49 — 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(0,num+1-i):
5 print(2*num+1-2*i,end=" ")
6 for k in range(1,num+1-i):
7 print(2*num+1-2*i,end=" ")
8 print()
Output
No output captured.

Pattern-49 — Odd Number Formula

The most important expression is:

2*num + 1 - 2*i

For num = 5:

i = 1

2*5 + 1 - 2*1
= 10 + 1 - 2
= 9
i = 2

11 - 4
= 7
i = 3

11 - 6
= 5
i = 4

11 - 8
= 3
i = 5

11 - 10
= 1

Therefore the values are:

9, 7, 5, 3, 1

Pattern-49 — Complete Row Logic

i Spaces Value Repetitions
1099
2177
3255
4333
5411

Pattern-50 — Inverted Number Sequence Pattern

Pattern-50 prints an inverted sequence of numbers.

Output

1 2 3 4 5 6 7
 1 2 3 4 5
  1 2 3
   1

The number of printed values decreases by two after every row.

7 → 5 → 3 → 1

This pattern uses two inner loops to construct every row.

Pattern-50 — 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+2-i):
5 print(j,end=" ")
6 for k in range(2,num+2-i):
7 print(num+k-i,end=" ")
8 print()
Output
No output captured.

Pattern-50 — First Inner Loop

The first inner loop is:

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

It prints an increasing sequence starting from 1.

For example, when num = 4:

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

Pattern-50 — Second Inner Loop

The second inner loop is:

for k in range(2,num+2-i):
    print(num+k-i,end=" ")

It prints the remaining increasing numbers required to complete the row.

For num = 4 and i = 1:

First loop:
1 2 3 4

Second loop:

k = 2 → 4 + 2 - 1 = 5
k = 3 → 4 + 3 - 1 = 6
k = 4 → 4 + 4 - 1 = 7

Therefore:

1 2 3 4 5 6 7

For row 2:

First loop:
1 2 3

Second loop:
4 5

Complete:
1 2 3 4 5

For row 3:

1 2 3

For row 4:

1

Pattern-50 — Dry Run

For num = 4:

i Spaces First Loop Second Loop Complete Row
1 0 1 2 3 4 5 6 7 1 2 3 4 5 6 7
2 1 1 2 3 4 5 1 2 3 4 5
3 2 1 2 3 1 2 3
4 3 1 - 1

Pattern-41 to Pattern-46 — Increasing Pyramid Comparison

Pattern Type Main Logic
41 Reverse Alphabet Odd-position alphabet down to A
42 Number Symmetry Decrease to 0, then increase
43 Alphabet Symmetry Decrease to A, then increase
44 Number Palindrome Increase and then decrease
45 Repeated Alphabet Sequence Two increasing alphabet loops
46 Descending Numbers Start from n and decrease

Pattern-47 to Pattern-50 — Inverted Pattern Comparison

Pattern Type Row Size
47 Stars 9, 7, 5, 3, 1
48 Repeated Numbers 9, 7, 5, 3, 1
49 Repeated Odd Numbers 9, 7, 5, 3, 1
50 Increasing Number Sequence 7, 5, 3, 1 for num = 4

Important Formulas in Part 5

Increasing Pyramid Spaces

n - i

Used in Patterns 41–46.

Inverted Pyramid Spaces

i - 1

Used in Patterns 47–50.

Pattern-41 Starting Alphabet

65 + 2*i - 2

Pattern-46 Printed Number

n + 1 - j

Inverted Odd Row Size

2*num + 1 - 2*i

For num = 5, this generates:

9, 7, 5, 3, 1

Pattern-49 Printed Value

2*num + 1 - 2*i

Understanding Increasing vs Inverted Pyramid

Increasing Pyramid

Leading spaces decrease while values increase.

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

Typical space formula:

n - i

Inverted Pyramid

Leading spaces increase while values decrease.

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

Typical space formula:

i - 1

This relationship is very important when solving pattern-programming questions.

Part 5 — Execution Flow

                         START
                           │
                           ▼
                      Read n / num
                           │
                           ▼
                       Outer Loop
                           │
                           ▼
                 Calculate Row Number
                           │
                           ▼
                    Print Spaces
                           │
             ┌─────────────┴─────────────┐
             │                           │
             ▼                           ▼
      Increasing Pyramid          Inverted Pyramid
       Pattern 41-46              Pattern 47-50
             │                           │
             ▼                           ▼
      Spaces Decrease              Spaces Increase
         n - i                       i - 1
             │                           │
             ▼                           ▼
      Values Increase              Values Decrease
             │                           │
             └─────────────┬─────────────┘
                           │
                           ▼
                  Execute Inner Loop
                           │
                    ┌──────┴──────┐
                    ▼             ▼
               First Part     Second Part
                    │             │
                    └──────┬──────┘
                           ▼
                       Print Row
                           │
                           ▼
                       Next Row
                           │
                           ▼
                          END

Part 5 — Important Notes

  • Pattern-41 uses ASCII values to generate alternate starting alphabets A, C, E, G, I.
  • A negative step such as -1 is used when values must move backward.
  • Pattern-42 combines decreasing and increasing number sequences.
  • Pattern-43 applies the same idea using alphabets.
  • Pattern-44 is a proper number palindrome because the second half is the reverse of the first half without repeating the center value.
  • Pattern-45 uses two increasing alphabet loops.
  • Pattern-46 always begins its sequence from n.
  • Pattern-47 starts the inverted pyramid patterns in this section.
  • In an inverted pyramid, spaces generally increase while the number of values decreases.
  • Patterns 47–49 use two loops to create odd-sized rows.
  • The sequence 9, 7, 5, 3, 1 can be generated using 2*num+1-2*i when num = 5.
  • Pattern-48 repeats the value num+1-i.
  • Pattern-49 uses the same odd-number formula both for the printed value and the effective row width.
  • Pattern-50 creates decreasing odd-sized rows of consecutive numbers.
  • end="" is important when spaces and pattern values must continue on the same line.
  • print() after the inner loops moves the cursor to the next row.

Part 5 — Summary

Pattern Pattern Type Key Concept
41 Reverse Alphabet Pyramid ASCII + reverse range
42 Number Symmetry Pyramid Decrease → 0 → Increase
43 Alphabet Symmetry Pyramid Decrease → A → Increase
44 Number Palindrome Forward + backward loops
45 Repeated Alphabet Sequence Two forward loops
46 Descending Number Triangle n+1-j
47 Inverted Star Pyramid Odd decreasing width
48 Inverted Number Pyramid Repeated decreasing value
49 Inverted Odd Number Pyramid 2*num+1-2*i
50 Inverted Number Sequence Two increasing number loops

Part 5 — Quick Revision

                     PATTERN 41-50
                           │
          ┌────────────────┴────────────────┐
          │                                 │
          ▼                                 ▼
     Pattern 41-46                    Pattern 47-50
   Advanced Pyramids                Inverted Patterns
          │                                 │
     Spaces = n-i                      Spaces = i-1
          │                                 │
     ┌────┼────────────┐              ┌─────┼───────────┐
     ▼    ▼            ▼              ▼     ▼           ▼
 Alphabet Number   Palindrome        Star  Number   Sequence
     │    │            │              │     │           │
     │    │            │              │     │           │
    41   42,46         44             47    48,49        50
     │
     ├── Pattern 43 → Alphabet Symmetry
     │
     └── Pattern 45 → Repeated Alphabet Sequence
📝 Key Takeaways
  • Row i of a pyramid prints 2*i-1 characters
  • Palindrome pyramids use one loop to grow and one loop to shrink
  • Inverted pyramids reverse the row-count logic
  • chr() and ASCII formulas drive alphabet pyramids

🧠 Test Your Knowledge

21 Questions
Progress: 0 / 21