In this Python turtle tutorial, we will learn about How to Draw the Line in Python turtle and we will also cover different examples related to Turtle Draw. And we will cover these topics.
- Python turtle draw line
- Python turtle draw line between two points
- Python turtle draw a dashed line
- Python turtle draw dotted line
Python turtle draw line
In this section, we will learn about how to draw the line in python turtle.
Before moving forward, we should have a piece of knowledge about a line. A line is defined as a long, narrow mark, arrow, wire, rope which perfectly describes the line. We can make different shapes with the help of a line. Here we can draw a line with the help of a turtle.
Code:
In the following code, we will import turtle libraries from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.fillcolor(‘cyan’) is used to fill the color inside the shape.
- tur.forward(150) is used to move the turtle in the forward direction.
from turtle import *
import turtle
tur = turtle.Turtle()
tur.fillcolor('cyan')
tur.forward(150)
turtle.done()
Output:
After running the above code, we get the following output in which we can see the line is drawn on the screen.
Read: Python turtle onclick with examples
Python turtle draw line between two points
In this section, we will learn how to draw lines between two points in python turtle.
A line is described by the two-point which connected the line from the end. And it covers a shorter distance between these two points and also determines a segment, ray between two points x and y.
Code:
- In the following code, we will import turtle libraries from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.penup() is used to stop the drawing.
- tur.goto(x) is used to move the turtle at its accurate position.
- tur.pendown() is used to start the drawing.
- tur.hideturtle() is used to remove the turtle from the screen.
- tur.exitonclick() is used to exit the screen only on clicking on them.
from turtle import *
import turtle as tur
x = (10, 140)
y = (100, 240)
tur.penup()
tur.goto(x)
tur.pendown()
tur.goto(y)
tur.hideturtle()
tur.exitonclick()
Output:
After running the above code, we get the following output in which we can see the line is drawn between the x and y points.
Read: Python Turtle Screen Size
Python turtle draw a dashed line
In this section, we will learn about how to draw a dashed line in a python turtle.
Before moving forward, we should have a piece of knowledge about a dashed line. Dashed is defined as a blank space to fill something inside this space or we can say that for completing the sentence we can use a dashed line in the middle, end, or starting of the paragraph or sentence.
Code:
In the following code, we will import turtle libraries from turtle import *, import turtle. The turtle() method is used to make objects.
- turtle.Screen() is used to create the screen.
- tur.color(“black”) is used to give the color to object.
- tur.penup() is used to start drawing.
- tur.forward(180) is used to move the turtle in the forward direction.
- turtle.listen() is used to listen the event.
- turtle.onkeypress(motion(),”w”) is used on presses a key the even is occur.
from turtle import *
import turtle
ws = turtle.Screen()
tur = turtle.Turtle()
tur.color("black")
tur.penup()
def motion():
tur.forward(180)
turtle.listen()
turtle.onkeypress(motion(),"w")
turtle.done()
Output:
After running the above code we get the following output in which we can see the turtle move in the form of the dashed line.
Read: Python Clear Turtle with examples
Python turtle draw a dotted line
In this section, we will learn about how to draw a dotted line in a python turtle.
A dotted line is made up of a series of dots.Dot() function is used to make a dotted line. We can draw a dotted line with the help of a turtle.
Code:
In the following code, we will import turtle libraries from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.dot() is used to draw dots on the screen.
- tur.forward(space) is used to move the turtle in the forward direction.
- tur.backward(space*x) 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.
- tur.penup() is used to stop the drawing.
from turtle import *
import turtle
tur = turtle.Turtle()
def drawdot(space,x):
for i in range(x):
for j in range(x):
tur.dot()
tur.forward(space)
tur.backward(space*x)
tur.right(90)
tur.forward(space)
tur.left(90)
tur.penup()
drawdot(10,8)
tur.hideturtle()
Output:
After running the above code, we get the following output in which we can see the dotted line is drawn on the screen.
You may also like to read the following articles on Python Turtle.
- Python Turtle Get Position
- Python Turtle Nested Loop
- Python Turtle Mouse
- Python Turtle Grid
- Python turtle draw letters
- Python Turtle Pen + Examples
- Python Turtle Triangle + Examples
- Python Turtle Size – Detailed Guide
- Python Turtle Art – How to draw
So, in this tutorial, we discussed Python Turtle Draw Line and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle draw line
- Python turtle draw line between two points
- Python turtle draw a dashed line
- Python turtle draw a dotted line
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.