Python Program to Print (Generate) Christmas Tree Pattern
This python program prints Christmas tree pattern made up of stars up to n lines.
In this python example, we first read number of row in Christmas tree pattern 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 python's for
loop.
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 Source Code: Christmas Tree Pattern
# 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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *