18 Sep turtle.undo() function in Python
The turtle.undo() function in Python undoes (reverses) the last turtle action. This can be repeated to undo multiple steps, depending on the undo buffer size.
Syntax: turtle.undo()
Parameters: None.
Let us see an example to implement the turtle.undo() function in Python:
Demo11.py
# turtle.undo() function in Python # Code by Studyopedia import turtle window = turtle.Screen() t = turtle.Turtle() t.forward(100) t.left(90) t.forward(50) t.undo() # Undo the last action (forward 50) t.undo() # Undo the previous action (left 90) window.exitonclick()
The following is the output:

In the above code, we followed the below steps:
- Import the module: import turtle loads Python’s turtle graphics library.
- Create the screen: window = turtle.Screen() opens the drawing window.
- Create the turtle: t = turtle.Turtle() creates the turtle (the pen) you control.
- Draw first segment: t.forward(100) moves 100 units forward, drawing a line.
- Turn left: t.left(90) rotates the turtle 90° counterclockwise without moving.
- Draw second segment: t.forward(50) draws a 50-unit line in the new direction.
- Undo last action: t.undo() reverses the most recent action, removing the 50-unit forward movement and restoring the prior position.
- Undo previous action: t.undo() reverses the earlier left turn, restoring the previous heading.
- Close on click: window.exitonclick() keeps the window open until you click inside it, then closes.
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:
- OpenCV Tutorial
- Python Tutorial
- NumPy Tutorial
- Pandas Tutorial
- Matplotlib Tutorial
- Generative AI Tutorial
- LangChain Tutorial
- RAG Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments