0
0
PythonProgramBeginner · 2 min read

Python Program to Print Half Diamond Pattern

You can print a half diamond pattern in Python using nested loops like for i in range(1, n+1): print('* ' * i) for the top half and for i in range(n-1, 0, -1): print('* ' * i) for the bottom half.
📋

Examples

Input3
Output* * * * * * * * *
Input5
Output* * * * * * * * * * * * * * * * * * * * * * * * *
Input1
Output*
🧠

How to Think About It

To print a half diamond pattern, first print increasing rows of stars from 1 up to the given number, then print decreasing rows of stars from one less than the given number down to 1. Each row contains stars equal to the current row number.
📐

Algorithm

1
Get input number n for the size of the half diamond.
2
Loop from 1 to n and print stars equal to the loop counter.
3
Loop from n-1 down to 1 and print stars equal to the loop counter.
4
End the program.
💻

Code

python
n = int(input())
for i in range(1, n + 1):
    print('* ' * i)
for i in range(n - 1, 0, -1):
    print('* ' * i)
Output
* * * * * * * * *
🔍

Dry Run

Let's trace input n=3 through the code

1

Input

n = 3

2

Top half loop

i=1: print('* ') i=2: print('* * ') i=3: print('* * * ')

3

Bottom half loop

i=2: print('* * ') i=1: print('* ')

LoopiOutput
Top half1*
Top half2* *
Top half3* * *
Bottom half2* *
Bottom half1*
💡

Why This Works

Step 1: Print increasing stars

The first loop prints stars from 1 up to n, creating the top half of the diamond.

Step 2: Print decreasing stars

The second loop prints stars from n-1 down to 1, creating the bottom half of the diamond.

Step 3: Combine both halves

Together, these loops form a half diamond shape with stars.

🔄

Alternative Approaches

Using a single loop with conditional printing
python
n = int(input())
for i in range(1, 2 * n):
    if i <= n:
        print('* ' * i)
    else:
        print('* ' * (2 * n - i))
This method uses one loop and a condition to print the top and bottom halves, making the code shorter but slightly less intuitive.
Using while loops
python
n = int(input())
i = 1
while i <= n:
    print('* ' * i)
    i += 1
j = n - 1
while j > 0:
    print('* ' * j)
    j -= 1
This approach uses while loops instead of for loops, which some beginners find easier to understand.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses two loops that together print roughly n^2 stars, so the time complexity is O(n^2).

Space Complexity

The program uses constant extra space, only variables for counters, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; the single loop method is shorter but not faster.

ApproachTimeSpaceBest For
Two for loopsO(n^2)O(1)Clear and easy to understand
Single loop with conditionO(n^2)O(1)Shorter code, slightly less clear
While loopsO(n^2)O(1)Beginners who prefer while loops
💡
Use nested loops carefully to control the number of stars printed on each line.
⚠️
A common mistake is printing the bottom half starting from n instead of n-1, causing the middle line to repeat.