Python Turtle Graphics

In this Python Turtle tutorial, we will learn How to make turtle graphics in Python turtle and we will also cover different examples related to turtle graphics. And, we will cover these topics.

  • Python turtle graphics tutorial
  • Python turtle graphics code
  • Python turtle graphics example
  • Python turtle graphics bar graph
  • Python turtle graphics online compiler
  • Python turtle graphics project

Python turtle graphics tutorial

In this section, we will learn about how to make turtle graphics in Python turtle.

Before moving forward we should have a piece of knowledge about graphics. Graphics are used to give a presentation or we can say that graphics is an image, drawing, design, or representing an object. Graphics are in conflict with text. The use of graphics looks our presentation attractive.

Code:

In the following code, we import turtle module import turtle for making turtle graphics which is shown on the screen after completion.

  • The turtle() method is used to make objects.
  • tur.penup() is used to pick up the turtle pen.
  • tur.pensize(10) is used to give the size to the pen.
  • tur.pencolor(“cyan”) is used to give the color to the pen.
  • tur.forward(100) is used to move the turtle in the forwarding direction.
  • tur.backward(100) is used to move the turtle in the backward direction.
  • tur.right(90) is used to move the turtle in the right direction.
  • tur.left(90) is used to move the turtle in the left direction.
import turtle
tur=turtle.Turtle()
tur.penup()
tur.setpos(-20,40)
tur.pendown()
tur.pensize(10)
tur.pencolor("cyan")
tur.forward(100)
tur.backward(100)
tur.right(90)
tur.forward(100)
tur.left(90)
tur.forward(100)
tur.backward(100)
tur.right(90)
tur.forward(100)
tur.left(90)
tur.forward(100)
turtle.done()

Output:

After running the above code we get the following output in which we can see the letter E is drawn with the use of graphics.

Python turtle graphic example
Python turtle graphic example

Read: Python Turtle Art – How to draw

Python turtle graphics code

In this section, we will learn about the turtle graphics code in the turtle python.

Graphics are used to give an attractive look to our application where users feel Interested to work on the console and with help of graphics we animate the text and images to our console.

Code:

In the following code, we import turtle library import turtle. The turtle() method is used to make objects.

We are using the right() and forward() functions to give graphic shapes and these functions help to define an attractive look.

import turtle
tur = turtle.Turtle()
 
tur.right(70)
tur.forward(90)
 
for i in range(4):
    tur.right(140)
    tur.forward(90)
     
turtle.done()

Output:

In the following output, we can see the graphic shape where with the help of forward() and right() used with help of loop to give this shape.

Python turtle graphic code
Python turtle graphic code Output

Read: Python Turtle Write Function

Python turtle graphics example

In this section, we will learn about graphics examples in Python turtle.

As we know graphics is used to give the attractive look to our application. We use graphics to give design to our text, shapes, images which gives a beautiful look to our window.

Code:

In the following code, we import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • turt.forward(50) is used to move the turtle in the forward direction.
  • turt.right(90) is used to move the turtle in the right direction.
from turtle import *

import turtle
turt = turtle.Turtle()
 
for x in range(4):
    turt.forward(50)
    turt.right(90)
     
turtle.done()

Output:

In the following output, we have designed a symbol notation using the forward() and right() functions.

Python turtle graphic example
Python turtle graphic example Output

Read: Python Turtle Speed With Examples

Python turtle graphics bar graph

In this section, we will learn about turtle graphics bar graph in python turtle.

A bar graph is a graph that represents the data is drawn in the form of rectangular bars. Graphics bar graph looks attractive which attracts the people and represents its data in an attractive manner.

Code:

In the following code, we import the turtle module from turtle import *, import turtle for making a graphics bar graph.

  • tur.begin_fill() is used for starting filling colors.
  • tur.left(90) is used to move the turtle in the left direction.
  • tur.forward(heig) is used to move the turtle in the forward direction.
  • tur.end_fill() is used to stop filling the shape.
  • turtle.Screen() is used to make a screen for graphics.
from turtle import *

import turtle
  

def draw_bar(tur, heig, clr):
    
    tur.fillcolor(clr)
    tur.begin_fill()              
    tur.left(90)
    tur.forward(heig)
    tur.write(str(heig))
    tur.right(90)
    tur.forward(40)
    tur.right(90)
    tur.forward(heig)
    tur.left(90)
       

    tur.end_fill()                 
  

  
axis = [50, 110, 130, 90, 145, 210, 100]
colors = ["pink", "green", "blue", "brown",
        "cyan", "yellow", "red"]
  
maxheig = max(axis)
numberbars = len(axis)
bordr = 10
   

ws = turtle.Screen()             
ws.setworldcoordinates(0 - bordr, 0 - bordr, 
                       40 * numberbars + bordr,
                       maxheig + bordr)
   

tur = turtle.Turtle()           
tur.pensize(3)
   
for x in range(len(axis)):
      
    draw_bar (tur, axis[x],
             colors[x])
  
ws.exitonclick()

Output:

After running the above code, we get the following output in which we can see the graphics bar graph is shown on the screen.

Python turtle graphics bar graph
Python turtle graphics bar graph Output

Also, check: Python Turtle Colors + Examples

Python turtle graphics online compiler

In this section, we will learn about graphics online compiler in Python turtle.

We use an online compiler for graphics we use a replit online compiler for graphics and give attractive to our application.

In replit, we can host our app and run out code on a server that we can access from anywhere.

In this, Link, we can see how our code is working on the Replit Platform and we can access that to see the output.

Code:

In the following code, we will learn about the turtle module import turtle.

  • tur.penup() is used to pick up the pen.
  • tur.fillcolor(clr) is used to fill the color.
  • tur.circle(Size) is used to draw the circle shape.
import turtle

def drawcircle(tur, clr, Size, i, j):
    tur.penup()
    tur.color(clr)
    tur.fillcolor(clr)
    tur.goto(i,j)
    tur.begin_fill()
    tur.circle(Size)
    tur.end_fill()
    tur.pendown()

ballon = turtle.Turtle()
ballon.shape("turtle")
ballon.speed(500)

drawcircle(ballon, "pink", 50, 25, 0)
drawcircle(ballon, "red", 50, 0, 0)
drawcircle(ballon, "cyan", 50, -25, 0)
turtle.done()

Output:

After running the above code we get the following output in which we can see the graphics are run on an online compiler.

Python turtle graphics online compiler
Python turtle graphics online compiler Output

Read: Python Turtle Font

Python turtle graphics project

In this section, we will learn about the turtle graphics project in Python turtle.

Before moving forward we should have a piece of knowledge about a graphics project. Graphics are used for giving the look to our application. In this project, we draw rainbow benzene with the help of a turtle from which our screen gives an attractive look to the user.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • turtle.bgcolor(‘black’) is used to give the background color to screen.
  • tur.pencolor(colrs[i%6]) is used to give the pencolor to pen.
  • tur.width(i//100 + 1) is used to give the width to pen.
  • tur.forward(i) is used to move the turtle in the forward direction.
  • tur.left(59) is used to move the turtle in left direction.
from turtle import *
# Python program to draw
# Rainbow Benzene
# using Turtle Programming
import turtle
colrs = ['yellow', 'blue', 'orange', 'cyan', 'pink', 'red']
tur = turtle.Turtle()
turtle.bgcolor('black')
for i in range(360):
    tur.pencolor(colrs[i%6])
    tur.width(i//100 + 1)
    tur.forward(i)
    tur.left(59)

Output:

After running the above code, we get the following output in which we can see the rainbow benzene is drawn on the screen.

Python turtle graphics project
Python turtle graphics project Output

You may also like to read the following tutorials.

So, in this tutorial, we will discuss Python turtle graphics and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle graphics tutorial
  • Python turtle graphics code
  • Python turtle graphics example
  • Python turtle graphics bar graph
  • Python turtle graphics online compiler
  • Python turtle graphics project