Open In App

turtle.setpos() and turtle.goto() functions in Python

Last Updated : 21 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

turtle-example

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

turlte-setpos

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

turtle-star-logo

star logo

Explanation: 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.



Next Article
Article Tags :
Practice Tags :

Similar Reads