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
sc = turtle.Screen()
pen = turtle.Turtle()
def semi_circle(col, rad, val):
pen.color(col)
pen.circle(rad, - 180 )
pen.up()
pen.setpos(val, 0 )
pen.down()
pen.right( 180 )
col = [ 'violet' , 'indigo' , 'blue' ,
'green' , 'yellow' , 'orange' , 'red' ]
sc.setup( 600 , 600 )
sc.bgcolor( 'black' )
pen.right( 90 )
pen.width( 10 )
pen.speed( 7 )
for i in range ( 7 ):
semi_circle(col[i], 10 * (
i + 8 ), - 10 * (i + 1 ))
pen.hideturtle()
|
Output:

Example 2:
Python3
import turtle
mypen = turtle.Turtle()
mypen.shape( 'turtle' )
mypen.speed( 10 )
window = turtle.Screen()
window.bgcolor( 'white' )
rainbow = [ 'red' , 'orange' , 'yellow' , 'green' , 'blue' , 'indigo' , 'violet' ]
size = 180
mypen.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 - = 20
turtle.done()
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Oct, 2020
Like Article
Save Article