0
0
PythonProgramBeginner · 2 min read

Python Program to Print Christmas Tree Pattern

You can print a Christmas tree pattern in Python using nested loops like for i in range(1, n+1): print(' '*(n-i) + '*'*(2*i-1)) to create the tree shape.
📋

Examples

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

How to Think About It

To print a Christmas tree pattern, think of it as a triangle made of stars. For each line, you print some spaces first to center the stars, then print an increasing number of stars. The number of stars grows by two each line, starting from one star at the top.
📐

Algorithm

1
Get the number of rows (height) for the tree.
2
For each row from 1 to the number of rows:
3
Calculate the number of spaces to print before stars to center the tree.
4
Calculate the number of stars to print, which is (2 * current row - 1).
5
Print the spaces followed by the stars.
6
Optionally, print the trunk of the tree below the leaves.
💻

Code

python
n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * (2 * i - 1))
# Print the trunk
for _ in range(2):
    print(' ' * (n - 1) + '*')
Output
* *** ***** ******* ********* * *
🔍

Dry Run

Let's trace the program with n=3 to see how it prints the Christmas tree.

1

First row

Spaces: 3 - 1 = 2, Stars: 2*1 - 1 = 1, Line: ' *'

2

Second row

Spaces: 3 - 2 = 1, Stars: 2*2 - 1 = 3, Line: ' ***'

3

Third row

Spaces: 3 - 3 = 0, Stars: 2*3 - 1 = 5, Line: '*****'

RowSpacesStarsPrinted Line
121 *
213 ***
305*****
💡

Why This Works

Step 1: Calculate spaces

We print spaces first to center the stars. The number of spaces is n - i where i is the current row.

Step 2: Calculate stars

Stars increase by two each row, calculated as 2 * i - 1, making the tree wider as we go down.

Step 3: Print trunk

After printing the leaves, we print the trunk as a vertical line of stars centered under the tree.

🔄

Alternative Approaches

Using while loops
python
n = 5
i = 1
while i <= n:
    print(' ' * (n - i) + '*' * (2 * i - 1))
    i += 1
j = 0
while j < 2:
    print(' ' * (n - 1) + '*')
    j += 1
This uses while loops instead of for loops; it's more verbose but works the same.
Using string center method
python
n = 5
for i in range(1, n + 1):
    print(('*' * (2 * i - 1)).center(2 * n - 1))
for _ in range(2):
    print('*'.center(2 * n - 1))
This centers the stars automatically using string's center method, making code cleaner.

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

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner printing of stars can be up to 2n-1 characters, resulting in O(n^2) time.

Space Complexity

The program uses constant extra space for variables and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using string center method can improve readability but not performance.

ApproachTimeSpaceBest For
For loops with manual spacesO(n^2)O(1)Clear control over spacing
While loopsO(n^2)O(1)Beginners learning loops
String center methodO(n^2)O(1)Cleaner code, easier to read
💡
Use the formula 2*i - 1 for stars and n - i for spaces to keep the tree symmetrical.
⚠️
Beginners often forget to print the correct number of spaces, causing the tree to be left-aligned instead of centered.