turtle.backward() function in Python

The turtle.backward() function in Python moves the turtle backward by the specified distance, in the opposite direction of its current heading. The turtle’s orientation remains unchanged.

Syntax: turtle.backward(distance)
Parameters: distance: a number (integer or float) representing the number of pixels to move.

Let us see an example to implement the turtle.backward() function in Python:

Demo2.py

# turtle.backward() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.forward(100)  # Move forward 100 units
t.backward(50)  # Move backward 50 units

window.exitonclick()

The following is the output:

turtle.backward() in Python Turtle

In the above code, we followed the below steps:

  1. Import the module: Brings in Python’s built-in turtle graphics library so you can draw with a virtual “pen” (the turtle).
  2. Create the screen: window = turtle.Screen() opens a drawing window where the turtle will move and draw.
  3. Create the turtle: t = turtle.Turtle() creates a turtle object (the pen) you control with movement commands.
  4. Move forward:forward(100) moves the turtle 100 units in its current direction, drawing a line if the pen is down (it is by default).
  5. Move backward:backward(50) moves the turtle 50 units in the opposite direction along the same heading, drawing another line.
  6. Wait for click to close:exitonclick() keeps the window open until you click inside it, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Create and Run First Turtle Program
turtle.left() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment