turtle.home() function in Python

The turtle.home() function in Python moves the turtle to the origin coordinates (0, 0) at the center of the screen. Also resets its heading to its standard start position (east, 0 degrees).

Syntax: turtle.home()
Parameters: None.

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

Demo8.py

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

import turtle

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

t.forward(100)
t.left(45)
t.forward(50)
t.home()        # Return to origin (0,0) facing east

window.exitonclick()

The following is the output:

turtle.home() in Python Turtle

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library for drawing.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() makes the turtle (the pen) you’ll control.
  4. Move forward: t.forward(100) draws a 100-unit line in the current direction.
  5. Turn left: t.left(45) rotates the turtle 45 degrees counterclockwise without moving.
  6. Move forward again: t.forward(50) draws a 50-unit line along the new 45° heading.
  7. Return to origin: t.home() moves the turtle back to the coordinate origin (0, 0) and resets its heading to east; it draws a line on the way if the pen is down (default).
  8. 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:

turtle.seth() function in Python
turtle.dot() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment