Graphics with Turtle
Playing with Turtle helps learner many different powerful features of programming language through fun.
Turtle is part of default Python library. It is now inside already. You just need to import it.
import turtle
Turtle is initialized in your screen left to right horzontally. Which means if you call forward()
it will move horizontally left to right.
turtle.forward(100)
import turtle
turtle.forward(100)
turtle.right(90) #will take 90 degree right turn
turtle.forward(100)
turtle.done() #by calling this the popup window will stay
Square
To draw a square you can use the below code
import turtle
turtle.forward(100)
turtle.right(90) #will take 90 degree right turn
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
#by calling this the popup window will stay
turtle.done()
Because we are rpeating the same set four times, we can use loop instead.
turtle.forward(100)
turtle.right(90)
Using loop for drawing square,
import turtle
for i in range(4):
turtle.forward(100)
turtle.right(90)
turtle.done()
Rectangle
import turtle
for i in range(2):
turtle.forward(200)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.done()
Circle
import turtle
turtle.circle(120)
turtle.done()
Color
You can change the color of the line and fill them inside,
import turtle
turtle.color("red", "yellow")
turtle.begin_fill()
turtle.circle(120)
turtle.end_fill()
turtle.done()
The output would look like