Page Nav

HIDE

Grid

GRID_STYLE

Classic Header

{fbt_classic_header}

Header Ads

program

latest

Rainbow Benzene program

                       Turtle Programming in Python Import the turtle module Create a turtle to control. Draw around using the turtle method...

                      Turtle Programming in Python

  1. Import the turtle module
  2. Create a turtle to control.
  3. Draw around using the turtle methods.
  4. Run turtle.done().

So as stated above, before we can use turtle, we need to import it. We import it as : 
 

from turtle import *
# or
import turtle


After importing the turtle library and making all the turtle functionalities available to us, we need to create a new drawing board(window) and a turtle. Let’s call the window as wn and the turtle as skk. So we code as: 
 

wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()


Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the direction skk is facing, we code: 
 

skk.forward(100)


We have moved skk 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done! 
 

turtle.done()


So, we have created a program that draws a line 100 pixels long. We can draw various shapes and fill different colors using turtle methods. There’s plethora of functions and programs to be coded using the turtle library in python. Let’s learn to draw some of the basic shapes. 


***************************RainbowBenzeneprogram************************

# Python program to draw
# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
t.pencolor(colors[x%6])
t.width(x/100 + 1)
t.forward(x)
t.left(59)




No comments