Javatpoint Logo
Javatpoint Logo

Draw a Rainbow using Turtle Graphics in Python

Python comes with a module for Turtle. It includes a cardboard sketching screen and a turtle (pen). To create anything on the screen, move the turtle (pen). Other operations, such as forward() and backward(), can move the turtle.

On this, we can draw utilising different drawing techniques just like we would on a drawing board. The fundamental commands manage the actual movement of the sketching pen. Starting with a very simple programme, we progress to design the Rainbow with Python Turtle.

Rainbow in turtle - Python :

This section will cover two distinct methods for drawing a rainbow using Turtle Graphics.

A straightforward application called Rainbow displays a vibrant rainbow moving back and forth across the screen. The programme takes advantage of the fact that new lines always follow existing lines in a pattern. For programmers learning turtle, this software is useful.

Approach :

  • Import the Turtle.
  • Set the screen
  • Create a turtle object.
  • Define the colours that will be used to draw loops that are 180 degrees apart.

Walking through the code :

We will start by importing the turtle python library to our code.

Then, we will create a screen for drawing using Screen() method and will then define a turtle instance.

Then we defined a method to draw a semicircle with a dynamic radius and colour named semi_circle().

Next we set the colour to be filled using color() method and also set the radius for the circle using circle() method.

Then we moved the turtle from one location to other using up() and down() methods along with the setpos() method to give the coordinates of the new location.

Then we provided a list of all the colours that are to be used to draw the Rainbow, using the list name clr.

We used the setup() method to set the size of the screen for drawing and the bgcolor() method to set the background colour for the screen.

Next, we set the width of the turtle using the width() method and the speed of drawing the Rainbow using the speed() method.

Lastly, we used a for loop to draw the 7 arcs of the rainbow with different colours. The for loop was executed 7 times and called the semi_circle() method every time on execution to draw each arc. The colour of the arc was decided by the clr list which was given earlier.

The hideturtle() method was also used in order to hide the turtle after the completion of the drawing.

Complete code :

Output:

Draw a Rainbow using Turtle Graphics in Python

Conclusion :

This post taught us how to create a rainbow in Python using turtle graphics. Aside from that, printing in colour is attractive, entertaining, and has a variety of advantageous uses as well.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA