Teach you to draw a Christmas tree in Python

simon brigerk
3 min readDec 22, 2020

Share with you an article, teach you how to draw a Christmas tree in Python, come and learn.

How to draw a Christmas tree in Python?

How to draw a Christmas tree in Python?

the easiest:

height = 5

stars = 1
for i in range(height):
print((‘ ‘ * (height — i)) + (‘*’ * stars))
stars += 2
print((‘ ‘ * height) + ‘|’)

effect:

Hahahaha, there is always a feeling of deceiving everyone.

In fact, this article wants to introduce Turtle library to draw Christmas trees.

import turtle

screen = turtle.Screen()
screen.setup(375, 700)


circle = turtle.Turtle()
circle.shape(‘circle’)
circle.color(‘red’)
circle.speed(‘fastest’)
circle.up()

square = turtle.Turtle()
square.shape(‘square’)
square.color(‘green’)
square.speed(‘fastest’)
square.up()

circle.goto(0, 280)
circle.stamp()

k = 0
for i in range(1, 13):
y = 30 * i
for j in range(i — k):
x = 30 * j
square.goto(x, -y + 280)
square.stamp()
square.goto(-x, -y + 280)
square.stamp()

if i % 4 == 0:
x = 30 * (j + 1)
circle.color(‘red’)
circle.goto(-x, -y + 280)
circle.stamp()
circle.goto(x, -y + 280)
circle.stamp()
k += 3

if i % 4 == 3:
x = 30 * (j + 1)…

--

--