Draw Rainbow using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides:
- Drawing using a screen (cardboard).
- Turtle (pen).
To draw something on the screen, we need to move the turtle (pen), and to move the turtle, there are some functions like the forward(), backward(), etc.
Prerequisite: Turtle Programming Basics
Draw Rainbow Using Turtle Graphics
In this section, we will discuss how to draw a Rainbow using two different ways using Turtle Graphics.
Approach:
- Import Turtle.
- Set screen
- Make Turtle Object
- Define colors used for drawing
- Loop to draw semi-circles oriented by 180-degree position.
Example 1:
python3
# Import turtle packageimport turtle# Creating a turtle screen objectsc = turtle.Screen()# Creating a turtle object(pen)pen = turtle.Turtle()# Defining a method to form a semicircle# with a dynamic radius and colordef semi_circle(col, rad, val): # Set the fill color of the semicircle pen.color(col) # Draw a circle pen.circle(rad, -180) # Move the turtle to air pen.up() # Move the turtle to a given position pen.setpos(val, 0) # Move the turtle to the ground pen.down() pen.right(180)# Set the colors for drawingcol = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']# Setup the screen featuressc.setup(600, 600)# Set the screen color to blacksc.bgcolor('black')# Setup the turtle featurespen.right(90)pen.width(10)pen.speed(7)# Loop to draw 7 semicirclesfor i in range(7): semi_circle(col[i], 10*( i + 8), -10*(i + 1))# Hide the turtlepen.hideturtle() |
Output:

Example 2:
Python3
import turtlemypen= turtle.Turtle()mypen.shape('turtle')mypen.speed(10)window= turtle.Screen()window.bgcolor('white')rainbow= ['red','orange','yellow','green','blue','indigo','violet']size= 180mypen.penup()mypen.goto(0,-180)for color in rainbow: mypen.color(color) mypen.fillcolor(color) mypen.begin_fill() mypen.circle(size) mypen.end_fill() size-=20turtle.done() |
Output:



Please Login to comment...