turtle.up() or turtle.penup() function in Python

The turtle.up() or turtle.penup() function lifts the pen up. When the pen is up, the turtle can move without drawing a line.

Syntax: turtle.penup() or turtle.up()
Parameters: None.

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

Demo18.py

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

import turtle

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

t.forward(50)   # Draws a line
t.penup()       # Lift the pen up (stop drawing)
t.forward(50)   # Moves without drawing
t.pendown()     # Put the pen down (start drawing again)
t.forward(50)   # Draws a line again

window.exitonclick()

The following is the output:

turtle.penup() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
  4. Draw first segment: t.forward(50) moves 50 units forward, drawing a line because the pen is down by default.
  5. Lift the pen: t.penup() raises the pen so movements won’t draw.
  6. Move without drawing: t.forward(50) advances 50 units with no line since the pen is up.
  7. Lower the pen: t.pendown() puts the pen back down to resume drawing.
  8. Draw second segment: t.forward(50) draws another 50-unit line.
  9. Close on click: window.exitonclick() keeps the window open until you click, 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.ycor() function in Python
turtle.down() or turtle.pendown() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment