Turtle- python

keshav
Nerd For Tech
Published in
3 min readSep 15, 2021

--

source

Introduction

Turtle in python is like a drawing board, which lets us draw on the drawing board with the help of the turtle. We can control the movement of the turtle with the help of certain functions.

It can be used to introduce programming for kids, and It was a part of the original Logo programming language.

Importing the package

To make use of the turtle methods and functionalities, we need to import turtle.” turtle” comes with the standard Python package and need not be installed externally.

from turtle import *

or

import turtle

Let’s look at some functions:

bgcolor

turtle.bgcolor(*args)

args: A color string(“red”, “green”, etc.) or (r,g,b) color codes.

It sets the background color to the turtle screen.

turtle.bgcolor("black")
turtle.bgcolor("blue")

pencolor

turtle.pencolor(*args)

args: Color String or tuple of r,g,b, or RGB colors.

import turtleturtle.bgcolor("black")
turtle.pencolor("Cyan")
turtle.forward(300)
import turtleturtle.bgcolor("black")
turtle.pencolor("#709A41")
turtle.forward(300)

forward

turtle.forward(*args)

turtle.fd(*args)

args: An integer -distance by which the turtle has to move forward. This function helps in moving the turtle by the specified distance in the direction in which it is headed.

import turtle
turtle.forward(300)
import turtle
turtle.fd(300)

backward

turtle.back(*args)

turtle.bk(*args)

turtle.backward(*args)

args: An integer -distance by which the turtle has to move. It moves the turtle backward, opposite to the direction in which it is moving.

import turtle
turtle.fd(300)
turtle.backward(200)

Right

turtle.right(angle)

turtle.rt(angle)

angle: Integer it floats.

This function turns the turtle by the specified angle towards the right.

import turtle
turtle.fd(300)
turtle.right(50)
turtle.fd(30)

Left

turtle.left(angle)

turtle.lt(angle)

This function turns the turtle by the specified angle towards the left.

import turtle
turtle.fd(300)
turtle.left(50)
turtle.fd(30)

Now, it must be easy to make a square.

import turtlefor _ in range(4):
turtle.fd(100)
turtle.rt(90)

--

--