Python Program to Generate Christmas Tree Pattern

In this python example, we first read number of row from user using built-in function input(). Since function input() returns string value, we need to convert given number to number type using int(). And then we generate Christmas tree pattern using stars.

Note: Here row number indicates number of row in each section in pattern. If we give input row number 7 then total row will be 21 :)

Python Program to Generate Christmas Tree Pattern Using Stars


# Python Program to Generate Christmas Tree Pattern

# Generating Triangle Shape
def triangleShape(n):
    for i in range(n):
        for j in range(n-i):
            print(' ', end=' ')
        for k in range(2*i+1):
            print('*',end=' ')
        print()

# Generating Pole Shape
def poleShape(n):
    for i in range(n):
        for j in range(n-1):
            print(' ', end=' ')
        print('* * *')

# Input and Function Call
row = int(input('Enter number of rows: '))

triangleShape(row)
triangleShape(row)
poleShape(row)

In this program print() only is used to bring control to new lines.

Output

Enter number of rows: 7

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