Christmas is coming soon , I wish you a merry Christmas in advance !:christmas_tree::christmas_tree::christmas_tree:
Someone has to say , Christmas is the day when Jesus was born , I don't believe in Christianity , What's there to celebrate . You don't know ,Python The birth of is also related to Christmas ：1989
year , It was winter , The first snow of that year came earlier than ever , There is a programmer , Amsterdam feels very free during Christmas , Decided to develop a new scripting language . He developed and painted the play , So the name of the new language also comes from his favorite Douban 9 Distraction drama 《Monty
Python’s Flying Circus（ Python's Flying Circus ）》.
This person is the later Python Father of · A benevolent dictator · Guido van Rossum .
therefore , Christmas is approaching , Then send us some Christmas trees .
1, Minimalist Edition
This is the simplest Christmas tree . Similar to the printed triangle in our exercise , Three or four lines of code （ Students who are just beginning to learn programming can try to write it without looking at the code ）：
height = 11 for i in range(height): print((' ' * (height - i)) + ('*' * ((2 *
i) + 1))) print((' ' * height) + '|')
2, Color version
3, Turtle version
Python There is a drawing tool called turtle , The programming classroom was introduced before 《【 Programming classroom 】 Turtle mapping 》, It can also be used to draw Christmas trees . See the address at the end of the text for the complete code , Core drawing code ：
# Create Sketchpad import turtle screen = turtle.Screen() screen.setup(800,600) # Draw a red circle
circle = turtle.Turtle() circle.shape('circle') circle.color('red') circle.up()
circle.goto(0,100) circle.stamp() # Draw a green square square = turtle.Turtle()
square.shape('square') square.color('green') square.up() square.goto(0,200)
square.stamp() # suspend , Click to exit turtle.exitonclick()
4, Turtle fractal Edition
fractal （Fractal）： A rough or fragmentary geometry , It can be divided into several parts , And every part （ At least approximately ） Is the overall reduced shape . For example, the Christmas tree above , Each branch is a small Christmas tree . This is very similar to the idea of recursion in programming ：
n = 50 from turtle import * speed("fastest") left(90) forward(3*n) # Top Pentagram skipped
color("dark green") backward(n*4.8) def tree(d, s): if d <= 0: return
forward(s) tree(d-1, s*.8) right(120) tree(d-3, s*.5) right(120) tree(d-3,
s*.5) right(120) backward(s) tree(15, n) backward(n/2) exitonclick()
5, Colorful version
It's usually hung on the Christmas tree blingbling Small colored lights . adopt cls / clear Command clear console , Loop multiple draw , You can achieve something similar GIF
Dynamic effects of pictures . Screen clearing part code ：
import platform import os os_name = platform.uname() IS_WIN = os_name ==
'Windows' os.system('cls' if IS_WIN else 'clear')
The complete codes of the above Christmas trees have been uploaded .