turtle.pos() function in Python

The turtle.pos() function returns the turtle’s current location (x, y) as a Vec2D vector. This is equivalent to (turtle.xcor(), turtle.ycor()).

Syntax: turtle.pos() or turtle.position()
Parameters: None.

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

Demo48.py

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

import turtle

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

t.forward(100)
t.left(45)
t.forward(50)

position = t.pos()  # Get current (x, y) position
print("Current position:", position)

window.exitonclick()

The following is the output:

turtle.pos() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Move forward: t.forward(100) draws a 100-unit line (pen down by default).
  • Turn left: t.left(45) rotates the turtle 45° counterclockwise.
  • Move forward again: t.forward(50) draws another line in the new heading.
  • Get position: position = t.pos() retrieves the current (x, y) coordinates as a tuple of floats.
  • Print position: print(“Current position:”, position) outputs the coordinates to the console.
  • Close on click: window.exitonclick() waits for a mouse click, 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:

turtle.resizemode() function in Python
turtle.reset() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment