Bu yazıyı Türkçe okuyun · Leer en español
What does this project do?
In the first episode of the Python for Kids series, we use the turtle library to make a turtle draw a square and a filled star. We learn forward, left, the for loop, penup, goto and colors. For ages 10 and up who are new to Python.
- Age group: 10 and up
- Level: Beginner
- Time: About 30 minutes
- What you will learn: the turtle library, forward and left, the for loop, colors and fill
What you need
| Item | Notes |
|---|---|
| Computer | Windows, macOS or Linux |
| Python 3 | Free from python.org; IDLE comes with it |
Code
In IDLE, open a new window with File → New File, type the code, save it as shapes.py and press F5 to run it.
# Python Turtle: Square and Star - ozandamci.com
import turtle
screen = turtle.Screen()
screen.bgcolor("lightyellow")
pen = turtle.Turtle()
pen.shape("turtle")
pen.pensize(5)
pen.speed(3)
# 1) Square: 4 sides, 90 degrees at each corner
pen.penup()
pen.goto(-250, -75)
pen.pendown()
pen.color("green")
for i in range(4):
pen.forward(150)
pen.left(90)
# 2) Star: 5 points, 144 degrees at each point
pen.penup()
pen.goto(50, 50)
pen.pendown()
pen.color("orange", "gold")
pen.begin_fill()
for i in range(5):
pen.forward(200)
pen.right(144)
pen.end_fill()
pen.hideturtle()
turtle.done()
Step by step
- The library and the screen:
import turtleloads Python’s drawing library.Screen()opens the drawing window andbgcolor()picks the background color. - Get the pen ready:
Turtle()puts a turtle on the screen; we named itpen.shapesets its shape,pensizethe line width andspeedhow fast it moves. - Lift the pen and move it:
penup()lifts the pen so the turtle moves without drawing.goto(-250, -75)takes it to a point on the screen.pendown()puts the pen down again. - A square with a for loop:
range(4)repeats four times. Each time the turtle moves 150 steps forward and turns 90 degrees to the left. The lines inside the loop must start with four spaces. - The star and the fill:
color("orange", "gold")makes the line orange and the inside gold. Whatever is drawn betweenbegin_fill()andend_fill()gets filled. We turn 144 degrees for the sharp points of the star. - Finishing:
hideturtle()hides the turtle.turtle.done()keeps the window from closing right away.
Try it yourself
- Draw a hexagon: change 4 and 90 in the square to 6 and 60
- A bigger star: write 300 instead of 200
- Your own colors: try colors such as pink, skyblue or purple





“Python for Kids: Draw a Square and a Star with Turtle (Code and Guide)” için 2 yorum