Basics Of Python Turtle

In this step-by-step tutorial, you'll learn the basics of Python programming with the help of a simple and interactive Python library called turtle.

Basics Of Python Turtle

Turtle

Turtle is a python feature like a drawing board, which lets us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around.

Installation:

pip install turtle

Creating turtle:

import turtle
t=turtle.Turtle()
turtle.mainloop()

Moving turtle :

import turtle
t=turtle.Turtle()
t.forward(150)
t.left(90)
t.forward(150)
t.left(90)
t.forward(150)
turtle.mainloop()

Output:

Explanation:

1. The code starts by creating a turtle object.
2. The turtle is then told to move forward 150 units and left 90 degrees, which it does.
3. It then moves forward another 150 units and left another 90 degrees.
4. Finally, the code tells the turtle to stop moving by telling it to go back in its original position of 0,0.
5. The code will move the turtle 150 units in the direction it is facing, then 90 degrees left.
6.  It will then move 150 units in the other direction and 90 degrees left again.

Changing Background Color:

import turtle
t=turtle.Turtle()
turtle.bgcolor("yellow")
#The bgcolor() method is used to set the background color of the turtle to yellow.
t.circle(30)
#A circle is drawn with a radius of 30 pixels on the screen.
#You can also try this
#t.dot(10)
#it draws 10 dots in random locations on the screen using dot().
turtle.mainloop()

Output:

Filling the shape with color:

import turtle
t=turtle.Turtle()
t.pen(pencolor="red",fillcolor="yellow")
t.begin_fill()
t.circle(90)
t.end_fill()
turtle.mainloop()
#The code creates a red pen, yellow fill color.
#The turtle then draws a circle of 90 degrees.

Output:

Some Other Methods:

import turtle
#Change Title bar
turtle.title("CyberRide")
#Change Turtle size
turtle.shapesize(10, 5, 1)
#Change Pensize
turtle.pensize(5)
# Change stamp
turtle.stamp()
#Picking Pen Up and Down
turtle.penup()
turtle.pendown()