Python Program to Print Christmas Tree Star Pattern
Last Updated :
24 Dec, 2024
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. Here's the algorithmic breakdown:
Step 1: Setting the Tree Size
- num = 3: Defines the base height of each leaf level. Increasing num will make the tree taller and wider.
Step 2: First Level of Leaves
- Loop (for i in range(1, num+1)): Iterates through each row of the first leaf level. Printing Spaces (" "*(2*num - i + 3)) to centers the stars by printing a specific number of spaces before the stars.
- Formula: 2*num - i + 3 ensures proper alignment based on the current row.
- Printing Stars (for j in range(1, i+1)): Prints stars (*) separated by spaces, with the number of stars increasing by one each row.
Step 3: Second Level of Leaves
- Loop (for i in range(1, num+3)): Adds additional rows to create a fuller second leaf level. Printing Spaces (" "*(2*num - i + 1)) to Adjusts spacing to center the stars for the second level.
- Formula: 2*num - i + 1 decreases the number of spaces as the row number increases.
Step 4: Third Level of Leaves
- Loop (for i in range(1, num+5)): Adds more rows for an additional triangular layer, making the tree larger. Printing Spaces (" "*(2*num - i)) which centers the stars by reducing the number of spaces as rows increase.
- Formula: 2*num - i further decreases spaces for a wider base.
Step 5: Trunk of the Tree
- Loop (for i in range(1, num+3)): Determines the height of the trunk based on the tree's size.
- Printing Spaces (" "*(2*num)): Centers the trunk beneath the leaves by printing a fixed number of spaces.
Example:
Python
# Number of levels for the Christmas tree
num = 3
# First Level of Leaves
for i in range(1, num + 1):
# Print spaces to center the stars
print(" " * (2 * num - i + 3), end="")
# Print stars with a space in between
for j in range(1, i + 1):
print("*", end=" ")
# Move to the next line after printing stars
print()
# Second Level of Leaves
for i in range(1, num + 3):
print(" " * (2 * num - i + 1), end="")
for j in range(-1, i + 1):
print("*", end=" ")
print()
# Third Level of Leaves (Optional for larger trees)
for i in range(1, num + 5):
print(" " * (2 * num - i), end="")
for j in range(-2, i + 1):
print("*", end=" ")
print()
# Trunk of the Tree
for i in range(1, num + 3):
# Print spaces to center the trunk
print(" " * (2 * num), end="")
# Print the trunk (3 stars)
print("* " * 3)
Output:
TreePrint 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.
Explanation:
- Initialization and First Level of Leaves: The variable num is set to 3, determining the number of main levels for the Christmas tree. The first for loop runs from 1 to num (1 to 3), printing the initial tier of the tree. For each iteration, it calculates the necessary spaces to center the stars and prints an increasing number of asterisks (*) separated by spaces, forming a triangular shape.
- Second Level of Leaves: The second for loop extends the tree by running from 1 to num + 2 (1 to 5). This loop adds a wider tier by adjusting the spacing to maintain symmetry and printing more asterisks on each line. Specifically, it starts generating stars from j = -1 to j = i + 1, resulting in i + 2 stars per line, which makes the second level broader than the first.
- Third Level of Leaves: The third for loop further enhances the tree's fullness by iterating from 1 to num + 4 (1 to 7). It continues to decrease the leading spaces to keep the tree centered and prints an even larger number of asterisks on each line. By generating stars from j = -2 to j = i + 1, each line in this tier has i + 3 stars, making the third level the widest part of the tree.
- Trunk of the Tree: The final for loop constructs the trunk by running from 1 to num + 2 (1 to 5). It centers the trunk beneath the foliage by adding consistent spaces and prints three asterisks (* * *) on each line to represent the trunk. Repeating this multiple times gives the trunk an appropriate height, providing a stable base to complete the Christmas tree's appearance.
Example:
Python
num = 3 # Define the number of levels for the Christmas tree
# First Level of Leaves
for i in range(1, num + 1):
# Print spaces to center the stars and then print stars separated by spaces
print(" " * (2 * num - i + 3) + " ".join("*" for j in range(1, i + 1)))
# Second Level of Leaves
for i in range(1, num + 3):
print(" " * (2 * num - i + 1) + " ".join("*" for j in range(-1, i + 1)))
# Third Level of Leaves
for i in range(1, num + 5):
print(" " * (2 * num - i) + " ".join("*" for j in range(-2, i + 1)))
# Trunk of the Tree
for i in range(1, num + 3):
print(" " * (2 * num) + " ".join("*" for j in range(3)))
Output :
TreeTime 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.
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 leaves and trunks 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 leaves(l, maxl, offset, start, end):
if l > maxl:
return
print(" " * (offset - level) + " ".join("*" for _ in range(start, level + end)))
leaves(l + 1, maxl, offset, start, end)
def trunk(h, offset, w=3):
if h == 0:
return
print(" " * offset + " ".join("*" for _ in range(w)))
trunk(h - 1, offset, w)
n = 3 # Define the number of levels for the Christmas tree
# Print First Level of Leaves
leaves(1, n, 2 * n + 3, 1, 1)
# Print Second Level of Leaves
leaves(1, n + 3, 2 * n + 1, -1, 1)
# Print Third Level of Leaves
leaves(1, n + 5, 2 * n, -2, 1)
# Print Trunk of the Tree
trunk(n + 3, 2 * n)
Output:
Tree
Explanation:
- leaves is a recursive function that prints each level of the tree's leaves.
- l: current level of leaves being printed.
- maxl: maximum level of leaves to be printed.
- offset: base offset to center the leaves.
- start: starting value for the range in the join function.
- end: ending value for the range in the join function.
- trunk is a recursive function that prints the trunk of the tree.
- h: height of the trunk.
- offset: base offset to center the trunk.
- w: number of stars in the trunk (default is 3).
Similar Reads
Python Program to print the pattern 'G'
In this article, we will learn how to print the pattern G using stars and white-spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7Output : *** * * * *** * * * * *** Input : 9Output : ***** * * * * *** * * * * * * ***** In this program, w
2 min read
Python Program to print digit pattern
The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples: Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *'s that are equivalent to each digit in the integer. He
3 min read
Python Program to Print Alphabetic Triangle Pattern
In this article, we examine a recursive method for printing character patterns in Python. Nesting loops are used in the conventional iterative approach to regulating character spacing and printing. We have provided the answer using both methods. This method prints an alphabet pattern in the form of
4 min read
Python program to print the Inverted heart pattern
Let us see how to print an inverted heart pattern in Python. Example: Input: 11 Output: * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* ********* ******** ******* ****** ***** **** Input: 15 Output: * *** ***** *****
2 min read
Python 3 | Program to print double sided stair-case pattern
Below mentioned is the Python 3 program to print the double-sided staircase pattern. Examples: Input: 10Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Note: This code only works for even values of n. Example1 The given
3 min read
Program to print the Fish Pattern
Given an integer N, the task is to print a pattern of fish over 2N+1 rows. Example: Input: N=3Output: * *** * ***** ************ ***** ** *** * * Input: N=5Output: * *** * ***** ** ******* *** ********* ******************** ********* **** ******* *** ***** ** *** * * Approach: The fish consists of t
6 min read
Program to print the pattern 'G'
In this article, we will learn how to print the pattern G using stars and white-spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program,
6 min read
Program to print the pattern âDâ
In this article, we will learn how to print the pattern D using stars and white-spaces. Given a number n, we will write a program to print the pattern D over n lines or rows.Examples : Input : n = 9 Output : ****** * * * * * * * * * * * * * * ****** Input : n = 12 Output : ********* * * * * * * * *
6 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half st
4 min read
Python | Print an Inverted Star Pattern
An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python Examples: 1) Below is the inverted star pattern of size n=5 (Because there are 5 horizontal
2 min read