Python Program to Print Christmas Tree Pattern

0

Python Program to Print Christmas Tree Pattern

  • 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

Source Code:

# 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)
 

Output:


 

 

 


Post a Comment

0Comments
Post a Comment (0)