Language / Turtle
Turtle
Python turtle module allows you to create images with turtle graphics.
# Drawing in Python
#
# Create a window with small arrow ...
# that represents the turtle.
#
# To move the turtle forward use fd() method.
# The argument of fd is in pixels, for lt() and rt() in degrees.
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(100) # pixels
bob.lt(90) # degrees
bob.fd(100)
turtle.mainloop()
Modify the program and draw a square.
# Square draw
#
# Draw a square using turtle module
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
turtle.mainloop()
Loop
The syntax of a for statement is similar to a function definition.
# Square draw - using loop
#
# The syntax of a for statement is similar to a function definition.
# The flow of execution runs through body and then loops back to the top.
import turtle
bob = turtle.Turtle()
print(bob)
for i in range(4):
bob.fd(100) # pixels
bob.lt(90) # degrees
turtle.mainloop()
Last update: 61 days ago