Turtle Graphics
In Python, turtle graphics provides a representation of a physical “turtle” (a little robot with a pen) that draws on a sheet of paper on the floor.
It’s an effective and well-proven way for learners to encounter programming concepts and interaction with software, as it provides instant, visible feedback. It also provides convenient access to graphical output in general.
Turtle drawing was originally created as an educational tool, to be used by teachers in the classroom. For the programmer who needs to produce some graphical output it can be a way to do that without the overhead of introducing more complex or external libraries into their work.
Pre-requisite : Click Here to install python if you don’t have it on your machine
Let’s Get Started
Without wasting much time, start creating .py files to design common shapes and patterns
- Square
# Python program to draw square
# using Turtle Programming
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle. Done()
- Star
# Python program to draw star
# using Turtle Programming
import turtle
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle. Done()
- Hexagon
# Python program to draw hexagon
# using Turtle Programming
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle. Done()
- Parallelogram
import turtle
# Initialize the turtle
t = turtle.Turtle()
# Set the turtle's speed
t.speed(1)
# Draw the parallelogram
for i in range(2):
t.forward(100)
t.left(60)
t.forward(50)
t.left(120)
- Circle with color fill
import turtle
# Set up the turtle screen and set the background color to white
screen = turtle.Screen()
screen.bgcolor("white")
# Create a new turtle and set its speed to the fastest possible
pen = turtle.Turtle()
pen.speed(0)
# Set the fill color to red
pen.fillcolor("red")
pen.begin_fill()
# Draw the circle with a radius of 100 pixels
pen.circle(100)
# End the fill and stop drawing
pen.end_fill()
pen.hideturtle()
# Keep the turtle window open until it is manually closed
turtle. Done()