Python Turtle Rainbow Drawing with Source Code: A Fun Graphics Project for Beginners
Python is widely known for its simplicity and versatility. One of its most enjoyable features for beginners is the Turtle graphics module. With just a few lines of code, you can create colorful shapes, patterns, and artistic designs. Among the many creative projects possible with Turtle graphics, a rainbow drawing is one of the most visually appealing and beginner-friendly examples.
A Python Turtle rainbow drawing project helps learners understand loops, colors, coordinates, and basic graphics programming while producing a beautiful result on the screen. Whether you are a student, a hobbyist, or someone exploring Python for the first time, creating a rainbow using Turtle graphics can be both educational and entertaining.
What Is Python Turtle?
The Turtle module is a built-in graphics library in Python. It allows users to control a virtual pen, called a turtle, which moves around a drawing window. As the turtle moves, it leaves a trail behind, making it easy to create lines, curves, and shapes.
The module was inspired by the Logo programming language, which was developed to teach programming concepts in a simple and visual way. Today, Turtle remains one of the best tools for introducing beginners to coding.
Some advantages of Turtle graphics include:
- Easy to learn
- No additional installation required
- Visual feedback while coding
- Great for understanding programming logic
- Encourages creativity
Why Create a Rainbow Drawing?
A rainbow drawing project is an excellent exercise because it combines multiple programming concepts:
Understanding Loops
Instead of drawing each rainbow arc manually, you can use loops to automate repetitive tasks.
Learning About Colors
The project introduces the use of different color values and how they affect graphics.
Working with Curves
Rainbows are made of curved arcs, helping students learn how circles and curves are drawn in Turtle graphics.
Improving Creativity
You can customize colors, backgrounds, sizes, and even add clouds or scenery around the rainbow.
How the Rainbow Drawing Works
A rainbow consists of several colored arcs arranged one inside another. In Turtle graphics, these arcs can be created using portions of circles.
The program draws the largest arc first and then gradually draws smaller arcs inside it using different colors.
The traditional rainbow colors are:
- Red
- Orange
- Yellow
- Green
- Blue
- Indigo
- Violet
Each arc uses a slightly smaller radius than the previous one, creating the layered rainbow effect.
Python Turtle Rainbow Drawing Source Code
import turtle
# Screen setup
screen = turtle.Screen()
screen.title("Rainbow Drawing using Python Turtle")
screen.bgcolor("skyblue")
# Create turtle
rainbow = turtle.Turtle()
rainbow.speed(10)
rainbow.width(10)
# Rainbow colors
colors = ["red", "orange", "yellow",
"green", "blue", "indigo", "violet"]
# Starting radius
radius = 200
# Move turtle to start position
rainbow.penup()
rainbow.goto(0, -200)
rainbow.setheading(180)
rainbow.pendown()
# Draw rainbow arcs
for color in colors:
rainbow.color(color)
rainbow.circle(radius, 180)
radius -= 15
# Hide turtle
rainbow.hideturtle()
turtle.done()
Explanation of the Code
Let us break the program into smaller sections for better understanding.
Importing the Turtle Module
import turtle
This statement loads the Turtle graphics library so that we can use its drawing functions.
Creating the Drawing Window
screen = turtle.Screen()
screen.title("Rainbow Drawing using Python Turtle")
screen.bgcolor("skyblue")
Here we create a graphics window and set a title. The background color is changed to sky blue to make the rainbow look more natural.
Creating the Turtle Object
rainbow = turtle.Turtle()
This creates the drawing turtle.
rainbow.speed(10)
rainbow.width(10)
The speed controls how fast the turtle moves, while the width sets the thickness of the rainbow arcs.
Defining Rainbow Colors
colors = ["red", "orange", "yellow",
"green", "blue", "indigo", "violet"]
A list stores the seven colors of a traditional rainbow.
Setting the Radius
radius = 200
This value determines the size of the largest rainbow arc.
Positioning the Turtle
rainbow.penup()
rainbow.goto(0, -200)
rainbow.setheading(180)
rainbow.pendown()
The turtle is moved to the correct starting point without drawing. Once positioned, drawing begins.
Drawing the Rainbow
for color in colors:
This loop runs once for each rainbow color.
rainbow.color(color)
Changes the pen color.
rainbow.circle(radius, 180)
Draws a semicircle using the current radius.
radius -= 15
Reduces the radius for the next arc, creating the layered rainbow effect.
Finishing the Program
rainbow.hideturtle()
turtle.done()
The turtle cursor is hidden, and the window remains open until the user closes it.
Enhancing the Rainbow Drawing
Once you understand the basic version, you can add extra features.
Add Clouds
You can draw circles near the ends of the rainbow to represent clouds.
rainbow.color("white")
rainbow.begin_fill()
for _ in range(6):
rainbow.circle(20)
rainbow.end_fill()
Add a Sun
Draw a yellow circle in one corner.
rainbow.penup()
rainbow.goto(250, 150)
rainbow.pendown()
rainbow.color("yellow")
rainbow.begin_fill()
rainbow.circle(40)
rainbow.end_fill()
Use Custom Colors
Experiment with different color combinations to create fantasy-style rainbows.
Animate the Drawing
Reduce the speed value and watch the rainbow appear gradually on the screen.
Educational Benefits
Creating a rainbow drawing with Python Turtle teaches several important programming concepts:
- Variables
- Lists
- Loops
- Functions
- Graphics programming
- Color management
- Coordinate systems
Because the output is visual, learners immediately see the result of their code, making the learning process more engaging.
Common Mistakes to Avoid
Forgetting to Import Turtle
Without importing the Turtle module, the program will generate an error.
Incorrect Radius Values
If the radius becomes too small too quickly, the rainbow may not look balanced.
Wrong Starting Position
Improper positioning can cause the rainbow to appear partially outside the window.
Missing turtle.done()
Without this statement, the drawing window may close immediately after the program finishes.
Conclusion
Python Turtle graphics provide an excellent introduction to programming through visual creativity. A rainbow drawing project is simple enough for beginners yet colorful enough to make learning enjoyable. By drawing multiple semicircular arcs with different colors, you can create an attractive rainbow while practicing fundamental Python concepts such as loops, variables, lists, and graphics functions.
As you gain confidence, you can expand the project by adding clouds, a sun, animated effects, or even an entire landscape. Small projects like this demonstrate that programming is not just about solving problems—it can also be a powerful tool for creating digital art and interactive experiences. The Python Turtle rainbow drawing is a perfect example of how coding and creativity can come together in a fun and educational way.