How To Draw Quadratic Equation In Python Turtle
Python Turtle Tutorial and Animations
Delight use Google Chrome or Mozilla FireFox to see the animations properly.
Python Turtle is something that evolved from Logo programming language, invented in 1966 by Wally Feurzig. With the aid of Object Oriented Programming approach, we tin can create an impressive prepare of animations easily.
The following animation was created by Python Turtle; the code is at the lesser of this tutorial.
The Requirements
Earlier using Python Turtle for animations, please accept the post-obit steps to install the surroundings:
- Download the latest version of Python from here.
- Load Python IDLE - Integrated Evolution and Learning Environment - from Windows.
- Open a new file and save information technology with .py extension.
- Write down import turtle at the tiptop of the file in social club to import the module - classes and methods.
- Write downwards the lawmaking and make impressive animations.
The all-time way to learn the Python turtle is running prepare of codes, from the simplest to the more advanced gradually, rather than making an effort to sympathize the simulator fully at first. This is the approach adopted in this tutorial.
The documentation of Python turtle is here.
Here are some of the turtle methods; they direct the turtle what to practice:
| Instructions | Meaning |
|---|---|
| turtle.forward(number) | Motion forrard |
| turtle.back(number) | Motion backward |
| turtle.right(angle) | Plough clockwise |
| turtle.left(angle) | Plow anti-clockwise |
| turtle.pencolor(colour string) | Drawing colour |
| turtle.pensize(number) | Choosing size of pen nib |
| turtle.circle(radius) | Drawing a circle |
| turtle.speed(number) | Choosing speed - ane to ten |
| turtle.write(message,font) | Writing on the screen |
| turtle.ht() | Hiding the turtle |
| turtle.setpos(x,y) | Changing the position of turtle |
Drawing a right-bending
The post-obit animation shows the turtle at work in producing a correct-angle on the screen:
This is the code for the above animation:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle form for drawing
screen=turtle.Screen() #making a canvas for drawing
screen.setup(400,300) #choosing the screen size
screen.bgcolor('black') #making canvas black
trtl.pencolor('cherry-red') #making colour of the pen red
trtl.pensize(5) #choosing the size of pen nib
trtl.speed(one) #choosing the speed of cartoon
trtl.shape('turtle') #choosing the shape of pen beak
trtl.frontward(150) #drawing a line of 200 pixels
trtl.right(90) #request turtle to plow xc degrees
trtl.forward(150) #drawing a line of 200 pixels
trtl.penup() # preparing for moving pen without drawing
trtl.setpos(-140,-120) # making the new position of the turtle
trtl.pendown() # bringing the pen down for drawing again
trtl.pencolor('green') # choosin the pen colour as green
trtl.write('Vivax Solutions', font=("Arial", xx, "bold")) # chosing the font
trtl.penup()
trtl.ht() # hiding the turtle from the screen
# sign indicates the comments in Python scripts.
Cartoon a Foursquare
The following animation shows how the turtle draws a foursquare on the screen:
This is the lawmaking for the to a higher place blitheness:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle course for drawing
screen=turtle.Screen() #making a sail for cartoon
screen.setup(400,300) #choosing the screen size
screen.bgcolor('black') #making canvas black
trtl.pencolor('cerise') #making colour of the pen red
trtl.pensize(v) #choosing the size of pen nib
trtl.speed(1) #choosing the speed of drawing
trtl.shape('turtle') #choosing the shape of pen nib
trtl.forward(100) #top line
trtl.right(90)
trtl.forward(100) # correct vertical line
trtl.right(ninety)
trtl.forward(100) # bottom line
trtl.correct(90)
trtl.forward(100) # left vertical line
# information press
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('light-green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "assuming"))
trtl.penup()
trtl.ht()
Although, the above code produces a square, it is not good programming exercise due to repetition of the code, which could have been tackled by a simple loop. Therefore, the code tin be revised to produce the same shape with efficiency every bit follows:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle class for cartoon
screen=turtle.Screen() #making a canvass for drawing
screen.setup(400,300) #choosing the screen size
screen.bgcolor('black') #making sheet black
trtl.pencolor('red') #making colour of the pen red
trtl.pensize(five) #choosing the size of pen nib
trtl.speed(1) #choosing the speed of cartoon
trtl.shape('turtle') #choosing the shape of pen nib
for i in range(4): # for loop to minimize the aforementioned lines of codes being written
trtl.forwards(100) # for lines
trtl.right(90) # for turning
# information press
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()
The for loop makes the drawing of the square much easier. It function is actually important when we create other polygons.
Recommended Books
If you are learning calculator science at GCSE, hither is a set of books for you lot: they are revision guides, nevertheless they encompass every unmarried topic, while giving ample data to grasp the concepts in an innovative way; the books show a clear path to follow, something that the bulky text books fail to exercise; they are good even for a 6th onetime.
Cartoon Polygons
In social club to modify the in a higher place lawmaking to describe polygons, we need to take into business relationship some concepts in geometry. As you can come across, with each plough, the turtle moves through a certain angle, specified past right(angle). This is the exterior angle of the polygon, which is the same as 360/n, where northward is the number of sides.
With for and while loops, the following code produces a set of polygons on screen - from a triangle to a decagon.
The lawmaking for the in a higher place blitheness is as follows:
import turtle # importing the module
import fourth dimension #importing the time module trtl = turtle.Turtle() #making a turtle object of Turtle form for drawing
screen=turtle.Screen() #making a sheet for drawing
screen.setup(620,470) #choosing the screen size
screen.bgpic('bg.gif') #making sheet black
trtl.pencolor('red') #making colour of the pen ruby-red
trtl.pensize(5) #choosing the size of pen beak
trtl.speed(ane) #choosing the speed of drawing
trtl.shape('turtle') #choosing the shape of pen pecker
time.slumber(12)
northward=3 starting for a triangle
shapes=['Triangle','Square','Pentagon','Hexagon','Heptagon','Octagon','Nonagon','Decagon']
while n<11: # limiting to a decagon
for i in range(n): # for loop to minimize the aforementioned lines of codes being written
trtl.pencolor('reddish')
trtl.frontwards(100) #acme line
trtl.right(360/n) #determining the exterior angle of the polygon
trtl.penup()
trtl.setpos(-80,180) #moving the turtle to make the animation more axial
trtl.pendown()
trtl.pencolor('blue')
trtl.write(' This is '+shapes[north-three], font=("Arial", 16, "bold")) #press the name of the polygon
north=n+1
fourth dimension.sleep(1)
#making turtle slumber for ane second trtl.clear()
trtl.penup()
trtl.setpos(-n*viii,n*fourteen)
trtl.pendown()
If you want to do it interactively, here is the lawmaking:
Drawing Letter E
Delight note how the image is centred on the screen, with turtle.setpos() method.
Drawing Circles
Hither is the code for calculation 2 numbers and drawing a circle:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle class for cartoon
screen=turtle.Screen() #making a canvas for drawing
screen.setup(420,320) #choosing the screen size
screen.bgpic('bg.gif') #making canvas black
trtl.pencolor('cherry') #making colour of the pen ruddy
trtl.pensize(iv) #choosing the size of pen neb
trtl.speed(1) #choosing the speed of drawing
trtl.shape('turtle') #choosing the shape of pen nib
trtl.circle(sixty) cartoon circle with radius 60 pixels
The post-obit animation shows how the two numbers are taken in as two inputs and later on answer is given out:
Every bit you tin can see, the starting point of the circle is the centre of the screen by default, which is not the centre of the circumvolve. In order to get round this problem, nosotros take to set the position by lawmaking as follows:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle grade for drawing
screen=turtle.Screen() #making a canvas for drawing
screen.setup(420,320) #choosing the screen size
screen.bgpic('bg.gif') #making canvas black
trtl.pencolor('scarlet') #making color of the pen red
trtl.pensize(4) #choosing the size of pen nib
trtl.shape('turtle') #choosing the shape of pen nib
trtl.penup() #moving the pen upward
trtl.setpos(0,-60) #setting new position
trtl.pendown() #moving the pen down
trtl.circle(lx) #drawing circle with radius 60 pixels
Advertising: No more sore pharynx suffering with this:
Drawing Concentric Circles
Hither is the lawmaking for drawing concentric circles with the assistance of loops:
import turtle # importing the module
trtl = turtle.Turtle() #making a turtle object of Turtle course for drawing
screen=turtle.Screen() #making a canvas for drawing
screen.setup(420,320) #choosing the screen size
screen.bgpic('bg.gif') #making canvas black
trtl.pencolor('red') #making colour of the pen red
trtl.pensize(4) #choosing the size of pen nib
trtl.shape('turtle') #choosing the shape of pen neb
n=0
while n<vii: #loop for 7 circles
n=n+1
trtl.penup()
trtl.setpos(0,-n*twenty)
trtl.pendown()
trtl.circumvolve(20*north)
The following animation shows how the animation works:
Changing Colours
The colour of the pen can exist changed in many different ways; hither are two ways:
- turtle.pencolor('red')
- turtle.pencolor(red, dark-green, blue)
If you use the 2nd method, red, light-green and blue can be any integer betwixt ane - 255. However, before that, turtle.colormode(one) or turtle.colormode(255) must be declared in the lawmaking.
Hither is the lawmaking:
import turtle # importing the module
import random # importing random module for generating random numbers
trtl = turtle.Turtle() #making a turtle object of Turtle course for drawing
screen=turtle.Screen() #making a sail for drawing
screen.setup(420,320) #choosing the screen size
screen.bgpic('bg.gif') #making canvas black
trtl.pensize(4) #choosing the size of pen nib
trtl.speed(i) #choosing the speed of cartoon
trtl.shape('turtle') #choosing the shape of pen nib
n=0
while n<seven:
r=random.randint(i,120) #random numbers for red
g=random.randint(81,200) #random numbers for green
b=random.randint(61,255) #random numbers for blue
turtle.colormode(255) #declaring the colour mode
trtl.pencolor(r,g,b) #pen color
n=north+1
trtl.penup()
trtl.setpos(0,-n*20)
trtl.pendown()
trtl.circle(20*n)
The following blitheness shows the output - with random colours, of course.
Advanced Animations - turtle in its habitat!
The following code creates a turtle that moves effectually on a beach - leaving behind a sure regular pattern!
import turtle
import random
trtl = turtle.Turtle()
screen=turtle.Screen()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.pensize(4)
trtl.speed(ane)
trtl.shape('turtle')
turtle.colormode(255)
trtl.pencolor(242,242,242)
trtl.penup()
trtl.setpos(-160,-100)
trtl.pendown()
for i in range(4):
trtl.forrard(40)
trtl.left(90)
trtl.forward(30)
trtl.right(90)
trtl.forward(40)
The following animation shows the iteration that leads to a countdown, based on the user input:
Advanced Animations - diverging turtles
In this blitheness, turtles go out their human foot print on the beach: this is achieved by turtle.postage() method along with turtle.penup(). This is the code:
import turtle
import random
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.shape('turtle')
trtl.colour('darkgoldenrod','blackness')
south=10
trtl.penup()
trtl.setpos(30,30)
for i in range(28):
s=s+ii
trtl.postage stamp()
trtl.forrard(s)
trtl.right(25)
fourth dimension.sleep(0.25) #activated with a break of a 1/4th of a second
The blitheness is as follows:
Avant-garde Animations - colourful star
With the assistance of uncomplicated geometry, a star can be drawn past Python Turtle. This is the code:
import turtle
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgcolor('black')
clr=['red','dark-green','blue','yellow','majestic']
trtl.pensize(4)
trtl.penup()
trtl.setpos(-90,thirty)
trtl.pendown()
for i in range(5):
trtl.pencolor(clr[i])
trtl.forrard(200)
trtl.right(144)
trtl.penup()
trtl.setpos(80,-140)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()
This is the blitheness that produces the star:
Avant-garde Animations - a clock dial
In the following animation, Python Turtle is used to construct a clock dial - with numbers, 1 to 12, surrounded by a circle. This is the lawmaking:
import turtle
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(620,620)
screen.bgcolor('blackness')
clr=['red','greenish','blue','yellowish','royal']
trtl.pensize(4)
trtl.shape('turtle')
trtl.penup()
trtl.pencolor('cerise')
m=0
for i in range(12):
m=m+1
trtl.penup()
trtl.setheading(-30*i+60)
trtl.forward(150)
trtl.pendown()
trtl.forward(25)
trtl.penup()
trtl.forrard(20)
trtl.write(str(m),align="eye",font=("Arial", 12, "normal"))
if m==12:
m=0
trtl.abode()
trtl.habitation()
trtl.setpos(0,-250)
trtl.pendown()
trtl.pensize(ten)
trtl.pencolor('blue')
trtl.circle(250)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()
This is the animation at work:
Advanced Animations - multi-coloured bloom
The following code produces a flower in different colours. The colours are provided with the aid of a list.The code is equally follows:
trtl=turtle.Turtle()
screen=turtle.Screen()
screen.setup(620,620)
screen.bgcolor('blackness')
trtl.pensize(3)
trtl.speed(10)
northward=-i
for angle in range(0,360,15):
north=n+1
if n==5:
northward=-i
trtl.color(colors[n])
trtl.seth(angle)
trtl.circle(100)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()
This is the animation:
As you lot tin can encounter, the effect is produced by turtle.seth() function - in turning the direction of turtle.
Cartoon Multiple Squares
In this blitheness, predetermined number of squares are drawn with the assistance of a role. The function has two parameters - length of the square and the colour. Then, using a for loop, we can draw the number of squares nosotros need. You lot can change the length and colour in order to practice interactively.
Creating a Graph Paper
With the lawmaking given beneath, y'all can create a graph newspaper easily with Python Turtle:
import turtle
trtl=turtle.Turtle()
trtl.speed(10)
for i in range(0,400,twenty):
trtl.pencolor('lightgrey')
trtl.penup()
trtl.setpos(-200+i,-200)
if i==0:
trtl.left(90)
trtl.pendown()
trtl.forward(400)
trtl.backward(400)
for i in range(0,400,20):
trtl.pencolor('lightgrey')
trtl.penup()
trtl.setpos(-200,-200+i)
if i==0:
trtl.right(xc)
trtl.pendown()
trtl.frontward(400)
trtl.backward(400)
trtl.penup()
trtl.home()
trtl.pendown()
trtl.pencolor('black')
trtl.astern(200)
trtl.frontwards(400)
trtl.astern(200)
trtl.left(90)
trtl.forward(200)
trtl.backward(400)
trtl.penup()
trtl.setpos(5,5)
trtl.pendown()
trtl.write(0)
trtl.penup()
trtl.setpos(190,5)
trtl.pendown()
trtl.write("x")
trtl.penup()
trtl.setpos(v,190)
trtl.pendown()
trtl.write("y")
trtl.penup()
trtl.setpos(eighty,-180)
trtl.pendown()
trtl.write("Vivax Solutions")
trtl.ht()
With the in a higher place code, the following tin can exist produced:
You can practise information technology interactively here:
The Code for Fifty Shades of Grey Animation
In order to produce the animation at the elevation of this tutorial, delight follow this code:
import turtle
turtle.penup()
turtle.setpos(-100,250)
turtle.pendown()
turtle.pencolor('olive')
turtle.write('50 Shades of Grey',font=("Arial", 18, "assuming"))
turtle.penup()
turtle.setpos(0,0)
turtle.pendown()
turtle.color("blackness", "white")
turtle.colormode(1.0)
SQUARES = fifty
SIDE = 150
shade = one.0
for count in range(SQUARES):
turtle.fillcolor(shade, shade, shade)
turtle.begin_fill()
turtle.left(360 // SQUARES)
for side in range(4):
turtle.forward(SIDE)
turtle.left(90)
turtle.end_fill()
shade -= turtle.colormode() / float(SQUARES)
turtle.penup()
turtle.setpos(150,-270)
turtle.pendown()
turtle.pencolor('olive')
turtle.write('Vivax Solutions',font=("Arial", 12, "normal"))
turtle.done()
Now that you have read this tutorial, y'all will find the post-obit tutorials very helpful besides:
- Python: basic to intermediate - interactive
- Visual Basic - interactive
- Trivial Human Estimator - LMC Tutorial
- Object Oriented Programming - OOP
- HTML Tutorial
- JavaScript Tutorial
Source: https://www.vivaxsolutions.com/web/python-turtle.aspx
Posted by: felixforridgen.blogspot.com

0 Response to "How To Draw Quadratic Equation In Python Turtle"
Post a Comment