Welcome to your first graphics lesson! Today, we will explore Python's turtle
module, which lets you draw on the screen using code.
turtle
is a built-in Python library that provides a way to draw using code. It opens a window and shows a turtle icon that you can control to draw shapes and patterns.
Itβs especially useful for beginners to understand loops, movement, and even angles while creating visual feedback instantly.
To use turtle, you don't need to install anything if you're using standard Python. Just use import turtle
in your code. Make sure you're running your script in a Python environment that supports graphical output (like a local IDE, not most online editors).
import turtle
# Create screen and turtle object
window = turtle.Screen()
my_turtle = turtle.Turtle()
# Draw an L-shape
my_turtle.forward(100)
my_turtle.left(90)
my_turtle.forward(100)
# Keep the window open
window.mainloop()
This code opens a window and makes the turtle draw an L-shape.
for
loop.my_turtle.pencolor("blue")
.my_turtle.speed(1)
sets how fast the turtle moves.my_turtle.penup()
and pendown()
allow you to move the turtle without drawing.window.bgcolor("lightblue")
changes the background color.Write a Python program that uses the turtle module to draw a simple house: