Placement Prep

Half Pyramid Pattern in Python: 5 Variants with Code

Learn five half pyramid patterns in Python with asterisks, same numbers, incrementing, decrementing, and alphabets. Each variant includes code and verified output.

By FACE Prep Team 5 min read
python patterns nested-loops placement-prep coding-practice

Half pyramid patterns test nested loop control and appear in coding rounds at companies screening for Python fundamentals. Five variants show up most often in placement assessments: asterisks, same-number rows, incrementing numbers, decrementing numbers, and alphabets.

What Is a Half Pyramid Pattern?

A half pyramid is a triangular arrangement where row i contains exactly i elements, starting from one element at the top and reaching n elements at the base. For n = 4, the shape has four rows with 1, 2, 3, and 4 elements respectively.

Two nested loops drive every half pyramid variant:

  • The outer loop iterates from row 1 to n, controlling the total number of rows.
  • The inner loop iterates from 1 to the current row number (inclusive), controlling how many elements print per row.

Only the print statement inside the inner loop changes across the five variants covered here. Get that structure right once, and all five become trivial adaptations.

These patterns appear in Python practice programs used in pre-placement coding assessments because they test range handling and nested loop control in one compact problem. Off-by-one errors in range() arguments are the most common source of wrong output in timed tests.

Asterisk Half Pyramid

The most common variant: print * characters, increasing by one per row.

For n = 4, the output is:

* 
* * 
* * * 
* * * * 

Code:

n = int(input("Enter number of rows: "))
for i in range(1, n + 1):
    for j in range(i):
        print("*", end=" ")
    print()

How it works:

  • for i in range(1, n + 1) runs the outer loop for rows 1 through n.
  • for j in range(i) runs the inner loop i times per row.
  • print("*", end=" ") places each star followed by a space.
  • print() after the inner loop ends the current row and moves to the next line.

Python’s range() function accepts start, stop, and step. range(i) is shorthand for range(0, i, 1), which generates i values starting from 0.

Row-by-row trace for n = 4:

  • i = 1: inner loop runs once → *
  • i = 2: inner loop runs twice → * *
  • i = 3: inner loop runs three times → * * *
  • i = 4: inner loop runs four times → * * * *

Number Half Pyramids

Three number-based patterns are common in placement assessments. All three share the outer loop structure from the asterisk variant.

Same Number in Each Row

Row r repeats the value r exactly r times.

For n = 4:

1 
2 2 
3 3 3 
4 4 4 4 

Code:

n = int(input("Enter number of rows: "))
for row in range(1, n + 1):
    for col in range(row):
        print(row, end=" ")
    print()

Row-by-row trace for n = 4:

  • row = 1: range(1) runs once, prints 1 → 1
  • row = 2: range(2) runs twice, prints 2 twice → 2 2
  • row = 3: range(3) runs three times, prints 3 three times → 3 3 3
  • row = 4: range(4) runs four times, prints 4 four times → 4 4 4 4

Incrementing Numbers

Each row prints integers from 1 up to the current row number.

For n = 4:

1 
1 2 
1 2 3 
1 2 3 4 

Code:

n = int(input("Enter number of rows: "))
for row in range(1, n + 1):
    for col in range(1, row + 1):
        print(col, end=" ")
    print()

Row-by-row trace for n = 4:

  • row = 1: range(1, 2) → col = 1 → 1
  • row = 2: range(1, 3) → col = 1, 2 → 1 2
  • row = 3: range(1, 4) → col = 1, 2, 3 → 1 2 3
  • row = 4: range(1, 5) → col = 1, 2, 3, 4 → 1 2 3 4

Decrementing Numbers

Each row starts at the current row number and counts down to 1.

For n = 4:

1 
2 1 
3 2 1 
4 3 2 1 

Code:

n = int(input("Enter number of rows: "))
for row in range(1, n + 1):
    for col in range(row, 0, -1):
        print(col, end=" ")
    print()

The negative step -1 in range(row, 0, -1) counts from row down to 1, stopping before 0.

Row-by-row trace for n = 4:

  • row = 1: range(1, 0, -1) → col = 1 → 1
  • row = 2: range(2, 0, -1) → col = 2, 1 → 2 1
  • row = 3: range(3, 0, -1) → col = 3, 2, 1 → 3 2 1
  • row = 4: range(4, 0, -1) → col = 4, 3, 2, 1 → 4 3 2 1

Alphabet Half Pyramid

The alphabet variant uses Python’s chr() built-in to convert integer ASCII values to characters. chr(65) returns 'A', chr(66) returns 'B', and so on. For the inner loop column starting at 1, the expression chr(64 + col) maps col = 1 to 'A', col = 2 to 'B', col = 3 to 'C', and col = 4 to 'D'.

For n = 4:

A 
A B 
A B C 
A B C D 

Code:

n = int(input("Enter number of rows: "))
for row in range(1, n + 1):
    for col in range(1, row + 1):
        print(chr(64 + col), end=" ")
    print()

Row-by-row trace for n = 4:

  • row = 1: col = 1 → chr(65) = 'A'A
  • row = 2: col = 1, 2 → 'A', chr(66) = 'B'A B
  • row = 3: col = 1, 2, 3 → 'A', 'B', chr(67) = 'C'A B C
  • row = 4: col = 1, 2, 3, 4 → 'A', 'B', 'C', chr(68) = 'D'A B C D

The Common Structure Across All Five Variants

Every pattern above follows the same outer loop. The only difference is what the inner loop prints.

VariantInner loopPrint expressionOutput row 3 (n=4)
Asteriskrange(i)"*"* * *
Same numberrange(row)row3 3 3
Incrementingrange(1, row + 1)col1 2 3
Decrementingrange(row, 0, -1)col3 2 1
Alphabetrange(1, row + 1)chr(64 + col)A B C

Time complexity for all five: O(n²). Total iterations = 1 + 2 + 3 + … + n = n(n+1)/2. For n = 10, that is 55 total print calls inside the inner loop.

This nested loop pattern generalises to other programs too. The Armstrong number check uses a similar digit-by-digit inner loop, and the same control flow appears across dozens of standard placement coding problems.

Where assessors add difficulty: they ask for inverted half pyramids (counting rows backwards), right-aligned versions (adding leading spaces), or combined patterns (top half + bottom half forming a full diamond). All of those are extensions of the five-variant structure above, so understanding this base set gives you the tools to adapt without memorising new code for each shape.

Every pattern variant above reduces to one insight: the outer loop sets the scope; the inner loop does the work. That nested loop control is exactly what you use when parsing API response rows, formatting table output, or processing character sequences in real Python programs. TinkerLLM is the ₹299 step that takes that loop logic from printed triangles to actual LLM-powered tools, where the inner loop isn’t printing stars but processing tokens, formatting prompts, or iterating over model responses.

Primary sources

Frequently asked questions

What is a half pyramid pattern in Python?

A half pyramid is a triangular arrangement where row i contains exactly i elements. The outer loop runs from 1 to n and the inner loop runs from 1 to the current row number. All five standard variants share this two-loop structure.

How many loops does a half pyramid program need?

Two nested loops: the outer loop controls the row count (1 to n) and the inner loop controls the column count (1 to row). Only the print statement inside the inner loop differs across the five variants.

What is the time complexity of printing a half pyramid?

O(n²). Total iterations = 1 + 2 + 3 + ... + n = n(n+1)/2. For n = 5, that is 15 total print calls. As n grows, the iteration count scales quadratically.

How do I print an alphabet half pyramid in Python?

Use Python's chr() function. chr(64 + col) returns A when col = 1, B when col = 2, and so on. In the inner loop, iterate col from 1 to row (inclusive). For 4 rows the output is: A / A B / A B C / A B C D.

How do I print a decrementing number half pyramid in Python?

Use range(row, 0, -1) in the inner loop. This counts from the current row number down to 1, stopping before 0. For row = 3, range(3, 0, -1) yields 3, 2, 1, printing 3 2 1 on that row.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF