Python Program to Print Christmas Tree Star Pattern
Last Updated :
28 Aug, 2023
The “Python program to print Tree pattern” is a piece of code that creates a tree pattern out of asterisks (*) using Python. The indentation and amount of asterisks per row are controlled via loops and string manipulation to produce the tree pattern, which is presented on the console.
Print Christmas Tree Star Pattern using Loops
This program uses nested loops to print out the tree pattern. The outer loop controls the rows of the pattern, and the inner loops control the columns.
The program consists of four sections, each printing a different triangular pattern.
In the first section, a variable num is defined with a value of 3. The program then uses a for loop to iterate from 1 to num+1, which is 1 to 4. Inside the loop, another loop is used to print spaces before the stars, creating a right-aligned triangular pattern. The number of spaces before the stars are calculated using the formula 2*num-i+3, where num is the number of rows and i is the current iteration value of the outer loop. After printing the spaces, the inner loop prints i a number of stars, separated by spaces.Â
The second section is similar to the first section but prints num + 2 number of rows instead of num rows. The formula for calculating the number of spaces before the stars are 2*num-i+1.
The third section also prints a triangular pattern, but the number of rows is increased to num + 5. The formula for calculating the number of spaces before the stars are 2*num-i.
The fourth section prints a fixed pattern of three stars in each row, with the stars aligned to the right. The number of spaces before the stars are fixed at 2*num spaces.
Python3
num = 3
for i in range ( 1 , num + 1 ):
print ( " " * ( 2 * num - i + 3 ), end = "")
for j in range ( 1 , i + 1 ):
print ( "*" , end = " " )
print ()
for i in range ( 1 , num + 3 ):
print ( " " * ( 2 * num - i + 1 ), end = "")
for j in range ( - 1 , i + 1 ):
print ( "*" , end = " " )
print ()
for i in range ( 1 , num + 5 ):
print ( " " * ( 2 * num - i), end = "")
for j in range ( - 2 , i + 1 ):
print ( "*" , end = " " )
print ()
for i in range ( 1 , num + 3 ):
print ( " " * (( 2 * num)), end = "")
print ( "* " * 3 )
|
Output:
Terminal Output
Print Christmas Tree Star Pattern using List Comprehension
The code uses list comprehension to generate the string of asterisks for each row of the tree pattern and then joins them with spaces using the Python join() method. The print() function is used to print each row with the appropriate indentation.
Example
The first loop generates the top part of the tree, with increasing indentation and the number of asterisks per row corresponding to the loop variable i.Â
The second loop generates the middle part of the tree, with decreasing indentation and the number of asterisks per row also corresponding to the loop variable i. However, there is an issue with this loop where the range for j is from -1 to i+1, which results in negative indices and incorrect asterisk placement.Â
The third loop generates the bottom part of the tree, with increasing indentation and the number of asterisks per row starting from -2 and incrementing by 1.Â
The fourth loop generates the trunk of the tree, with constant indentation and always printing 3 asterisks per row.
Python3
num = 3
for i in range ( 1 , num + 1 ):
print ( " " * ( 2 * num - i + 3 ) + " " .join( "*" for j in range ( 1 , i + 1 )))
for i in range ( 1 , num + 3 ):
print ( " " * ( 2 * num - i + 1 ) + " " .join( "*" for j in range ( - 1 , i + 1 )))
for i in range ( 1 , num + 5 ):
print ( " " * ( 2 * num - i) + " " .join( "*" for j in range ( - 2 , i + 1 )))
for i in range ( 1 , num + 3 ):
print ( " " * (( 2 * num)) + " " .join( "*" for j in range ( 3 )))
|
Output :
Terminal Output
Time complexity: The time complexity of the given code is O(n^2), where n is the value of the variable ‘num’. This is because the code contains nested loops, each iterating from 1 to ‘num’ or a constant number of iterations (in the case of the last loop). As such, the time complexity is quadratic and increases with the square of the input size.
Space complexity: The space complexity of the given code is O(1) as it does not use any additional data structures that consume memory.
Python Program to Print Christmas Tree Star Pattern using recursion
To print a Christmas tree star pattern using recursion in Python, we can define a recursive function that handles each row of the tree and then call that function recursively to build the entire tree. The star pattern of the Christmas tree will be made up of ‘*’ characters. Here’s a Python program to achieve this:
Python
def print_spaces(count):
if count > 0 :
print ( ' ' , end = '')
print_spaces(count - 1 )
def print_stars(count):
if count > 0 :
print ( '*' , end = '')
print_stars(count - 1 )
def print_tree_row(spaces, stars):
if spaces > 0 :
print_spaces(spaces)
if stars > 0 :
print_stars(stars)
print ()
def print_tree(rows, current_row = 1 ):
if current_row > rows:
return
spaces = rows - current_row
stars = 2 * current_row - 1
print_tree_row(spaces, stars)
print_tree(rows, current_row + 1 )
def print_multiple_trees(num_trees, tree_height):
if num_trees = = 0 :
return
print_tree(tree_height)
print ()
print_multiple_trees(num_trees - 1 , tree_height)
if __name__ = = "__main__" :
num_trees = 3
tree_height = 5
print_multiple_trees(num_trees, tree_height)
|
Output:
*
***
*****
*******
*********
*
***
*****
*******
*********
*
***
*****
*******
*********
Share your thoughts in the comments
Please Login to comment...