Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Make an Indian Flag in Python
Next article icon

Turtle is an inbuilt module in Python. It provides: 

  1. Drawing using a screen (cardboard).
  2. 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: 

  1. Import Turtle.
  2. Set screen
  3. Make Turtle Object
  4. Define colors used for drawing
  5. Loop to draw semi-circles oriented by 180-degree position.

Example 1:

python3
# Import turtle packageimportturtle# 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 colordefsemi_circle(col,rad,val):# Set the fill color of the semicirclepen.color(col)# Draw a circlepen.circle(rad,-180)# Move the turtle to airpen.up()# Move the turtle to a given positionpen.setpos(val,0)# Move the turtle to the groundpen.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 semicirclesforiinrange(7):semi_circle(col[i],10*(i+8),-10*(i+1))# Hide the turtlepen.hideturtle()

Output:

Rainbow using Turtle Graphics

Example 2:

Python3
importturtlemypen=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)forcolorinrainbow:mypen.color(color)mypen.fillcolor(color)mypen.begin_fill()mypen.circle(size)mypen.end_fill()size-=20turtle.done()

Output:


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp