


#CHRISTMAS TREE DRAWING CODE#
In case you’re unfamiliar with recursion, it means that a function calls itself causing a loop-like code structure. Last but not least, let’s use a recursive approach for printing the Christmas tree. When you call the draw_tree() function and pass it a height, it will print a Christmas tree with that height using asterisks (*).įor example, if you call draw_tree(15), it will print a Christmas tree with a height of 15: 3. Once again, the outer loop iterates through each row of the tree, and the inner loop prints the asterisks on each row. This piece of code defines a draw_tree() function that takes a height as an argument and uses two nested while loops to print the asterisks in the shape of a Christmas tree. # Loop while the row number is less than or equal to the height of the tree # Set the initial values for the while loop Here’s what it looks like in the code: # Function to draw a Christmas tree with a given height To draw a Christmas tree in Python, let’s use a while loop to print the asterisks in the shape of a tree. More importantly, if you’re new to loops, you should definitely try reproducing the tree in the previous example using a while loop. The for-loop approach is the simplest one to generate a Christmas tree in Python.īut it doesn’t mean you couldn’t do it with a while loop. When you call the draw_tree() function and pass it a height, it will print a Christmas tree with the given height.įor instance, if you call draw_tree(5), it prints a Christmas tree like follows: 2. The number of spaces before the asterisks and the number of asterisks on each row is calculated based on the height of the tree. The inner for loop prints the asterisks on each row.The outer for loop iterates through each row of the tree.It takes a height as an argument and uses two nested for loops to print the asterisks in the shape of a Christmas tree. This piece of code defines a draw_tree() function. # Call the function to draw a tree with a height of 5 # Print the spaces before the asterisks on each row
#CHRISTMAS TREE DRAWING HOW TO#
Here is a code example of how to do this: # Function to draw a Christmas tree with a given height To draw a Christmas tree using asterisks (*) in Python, you can use a for loop to print the asterisks in the shape of a tree. In each example, the code has comments you can read to follow along. Let’s take a closer look at how each of these approaches works. There are three main ways to draw Christmas trees in Python: Let’s jump into it! Drawing Christmas Trees in Python This guide teaches you how to draw Christmas trees in Python in three different ways that incorporate loops and recursion. You will also find a few more drafts that will make your drawing as convenient as possible, even if you are experiencing slight difficulties.Drawing a Christmas tree with asterisks (*) in Python isn’t probably the most impressive Christmas present, but it offers a nice challenge that tests your understanding of loops.

First, you will find a compact version of this post that you can use even if the Internet is not available. In this PDF, you will find all the information you need to complete this guide.
#CHRISTMAS TREE DRAWING PDF#
I have prepared a PDF that contains a short version of this drawing tutorial. This is a very important step.Ĭhoose any color you like for your Christmas decorations. Balls are a great choice for a Christmas tree.Ĭheck all the lines and proportions of all the previous steps.

You can paint any decorations you like.Īdd some decorations of your choice. It should be a very small rectangle at the bottom of our Christmas tree. To do this, draw some jagged lines that match the lines from the previous step.ĭepict a small star on top of your tree. It looks like a large triangle in the center of the paper.ĭivide the triangle into several parts using smooth horizontal lines.
