turtle.setpos() and turtle.goto() functions in Python
Last Updated :
21 Mar, 2025
turtle module in Python enables graphics using a virtual pen (turtle) and supports both object-oriented and procedural approaches. It relies on Tkinter for rendering, requiring a Python version with Tk support. The methods setpos(), setposition() and goto() move the turtle to a specified position and function identically, making them interchangeable.
Example: Moving the turtle to different positions
Python
# import package
import turtle
# forward turtle by 100
turtle.forward(100)
# stamp the turtle shape
turtle.stamp()
# set the position by using setpos()
turtle.up()
turtle.setpos(-50,50)
turtle.down()
# forward turtle by 100
turtle.forward(100)
# stamp the turtle shape
turtle.stamp()
# set the position by using goto()
turtle.up()
turtle.goto(-50,-50)
turtle.down()
# forward turtle by 100
turtle.forward(100)
Output

Explanation: Turtle moves forward 100 pixels, stamps its shape, then uses setpos(-50,50) to continue drawing. It moves to (-50,-50) with goto() and advances again. penup() lifts the pen to prevent drawing, while pendown() resumes it.
Syntax
import turtle
turtle.setpos(x, y=None)
# or
turtle.setposition(x, y=None)
# or
turtle.goto(x, y=None)
Parameters:
- x: The x-coordinate of the new position.
- y: The y-coordinate of the new position.
- If y is not provided, x must be a 2D vector (Vec2D object or tuple of (x, y)).
Functionality: These methods reposition the turtle to an absolute coordinate on the screen. The movement is direct and does not leave a trail unless penup() is used before calling these methods.
Examples
Example 1: Drawing a Circular Pattern with setpos()
Python
import turtle
def draw(radius):
# Draw a circle with the given radius
turtle.circle(radius)
# Move the turtle to a new position below the circle
turtle.penup()
turtle.setpos(0, -radius)
turtle.pendown()
# Create a pattern with multiple concentric circles
for i in range(5):
draw(20 + 20 * i)
turtle.done()
Output

Explanation: draw() function creates a circle of a specified radius. After drawing, the turtle moves to a new position below the circle. A loop generates five concentric circles, each with an increasing radius.
Example 2: Creating a Star with goto()
Python
import turtle
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
for i in range(5):
turtle.forward(200)
turtle.right(144) # Turns the turtle to create a star
turtle.done()
Output
star logoExplanation: Turtle moves to the starting position, then a loop draws a five-pointed star by moving forward and turning 144 degrees after each step.
Key Differences between goto() and setpos()
Although goto(x, y) and setpos(x, y) perform the same task, knowing their differences improves readability, ensures OOP consistency and enhances code maintainability based on coding style and context.
Feature | goto(x,y) | setpos(x,y) |
---|
Functionality | Moves the turtle to a specific (x, y) coordinate. | Moves the turtle to a specific (x, y) coordinate. |
---|
Usage | More commonly used due to its shorter name. | Less commonly used but works the same way. |
---|
Readability | Easier to read and type, making it preferable in most cases. | Slightly longer but still understandable.
|
---|
Object-Oriented Approach | Can be used in both procedural and OOP styles. | Sometimes preferred in OOP to maintain consistency with vector-based methods. |
---|
Code Style Preference | Used more frequently in general coding practices. | Preferred when working with structured OOP-based approaches. |
---|
Similar Reads
turtle.showturtle() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.showturtle() This method is used to makes the turtle visible. It doesn't req
1 min read
turtle.Screen().turtles() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.Screen().turtles() This function is used to return the list of turtles on th
1 min read
turtle.get_shapepoly() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.get_shapepoly() This method is used to return the current shape polygon as a
2 min read
turtle.settiltangle() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.settiltangle() This function is used to rotate the turtleshape to point in t
2 min read
turtle.turtlesize() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.turtlesize() This function is used to return or set the pen's attributes x o
2 min read
turtle.resetscreen() function in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.resetscreen() This function is used to reset all Turtles on the Screen to th
1 min read