Write a for loop that prints a rose emoji (🌹) for each number from 5 to 10.
Write a for loop that prints a rose emoji (🌹) for each number from 5 to 10.
Open the code editor and write your code.
Python Documentation: You can use a for loop with range(start, stop) to repeat an action.
Example:
for i in range(3):
print('Hello!')
This prints 'Hello!' three times.
Run your code. You should see the rose emoji printed 6 times (once for each number from 5 to 10).
This document provides a reference for common commands and concepts used in Python's turtle
graphics library.
To use the turtle
library, it must be imported.
import turtle
The screen is the window where the turtle draws. You need to get a reference to it.
turtle.Screen()
: Creates or returns the singleton screen object.screen = turtle.Screen()
screen.setup(width, height)
: Sets the size of the screen window in pixels.screen.setup(width=600, height=400)
screen.bgcolor(color_name_or_code)
: Sets the background color of the screen.screen.bgcolor("lightgray")
screen.exitonclick()
: Keeps the window open until the user clicks on it.turtle.done()
: A common way to keep the window open until manually closed.screen.mainloop()
: Starts the event loop, also keeping the window open. Used in more complex programs.# At the end of your script:
screen.exitonclick()
# OR
# turtle.done()
The turtle is the object that moves and draws on the screen.
turtle.Turtle()
: Creates a new turtle object.my_turtle = turtle.Turtle()
These commands move the turtle forward or backward in its current direction, drawing a line if the pen is down.
my_turtle.forward(distance)
: Moves the turtle forward by distance
pixels.
my_turtle.fd(distance)
my_turtle.backward(distance)
: Moves the turtle backward by distance
pixels.
my_turtle.bk(distance)
my_turtle.forward(100) # Draws a line 100 pixels long
my_turtle.backward(50) # Moves back 50 pixels
These commands change the turtle's direction without moving it. Angles are measured in degrees.
my_turtle.left(angle)
: Turns the turtle left by angle
degrees.
my_turtle.lt(angle)
my_turtle.right(angle)
: Turns the turtle right by angle
degrees.
my_turtle.rt(angle)
my_turtle.left(90) # Turns 90 degrees counter-clockwise
my_turtle.right(45) # Turns 45 degrees clockwise
Control whether the turtle draws when it moves. By default, the pen is down.
my_turtle.penup()
: Lifts the pen. The turtle will move without drawing.
my_turtle.pu()
, my_turtle.up()
my_turtle.pendown()
: Puts the pen down. The turtle will draw when it moves.
my_turtle.pd()
, my_turtle.down()
my_turtle.penup() # Stop drawing
my_turtle.forward(50) # Move without drawing
my_turtle.pendown() # Start drawing again
my_turtle.forward(50) # Draw a line
Change the look of the turtle's drawing or the turtle itself.
my_turtle.pencolor(color_name_or_code)
: Sets the drawing color. Can use color names (e.g., "red"
, "blue"
) or hex color codes (e.g., "#FF0000"
).my_turtle.pencolor("green")
my_turtle.forward(100) # Draws a green line
my_turtle.speed(speed_value)
: Sets the turtle's animation speed.
0
: Fastest (no animation)1
: Slowest10
: Fastmy_turtle.speed(1) # Slow speed for observation
my_turtle.speed(0) # Fastest speed
my_turtle.hideturtle()
: Makes the turtle icon invisible.
my_turtle.ht()
my_turtle.showturtle()
: Makes the turtle icon visible.
my_turtle.st()
my_turtle.hideturtle() # Hide the turtle after drawing
Move the turtle directly to a specific point on the screen using coordinates. The center of the screen is (0, 0).
my_turtle.goto(x, y)
: Moves the turtle to the point with coordinates (x, y)
.
my_turtle.setpos(x, y)
, my_turtle.setposition(x, y)
my_turtle.penup() # Lift pen before moving
my_turtle.goto(50, 50) # Move to coordinate (50, 50)
my_turtle.pendown() # Put pen down to draw from here
my_turtle.forward(50) # Draw a line from (50, 50)
Shapes are drawn by combining movement and turning commands.
side_length = 100
for _ in range(4):
my_turtle.forward(side_length)
my_turtle.left(90) # Or right(90)
side_length = 100
for _ in range(3):
my_turtle.forward(side_length)
my_turtle.left(120) # Or right(120)
width = 100
height = 50
for _ in range(2):
my_turtle.forward(width)
my_turtle.left(90)
my_turtle.forward(height)
my_turtle.left(90)
Circles:
my_turtle.circle(radius)
: Draws a circle with the given radius
. The turtle starts drawing from the bottom of the circle.
my_turtle.circle(50) # Draws a circle with radius 50
Using fundamental programming concepts makes turtle drawings more efficient and dynamic.
line_color = "purple"
line_length = 75
turn_angle = 90
my_turtle.pencolor(line_color)
my_turtle.forward(line_length)
my_turtle.left(turn_angle)
for
loop): Execute a block of code multiple times. Useful for drawing shapes with repeating patterns (like squares or triangles) or drawing multiple similar objects.# Repeat an action 5 times
for i in range(5):
# Code inside the loop runs 5 times
my_turtle.forward(50)
my_turtle.right(60)
# Iterate over items in a list
colors = ["red", "orange", "yellow", "green", "blue"]
for color in colors:
my_turtle.pencolor(color)
my_turtle.forward(20)
def draw_square(t, size):
"""Draws a square with given size using turtle t."""
for _ in range(4):
t.forward(size)
t.left(90)
# Use the function
artist = turtle.Turtle()
draw_square(artist, 80) # Draw a square of size 80
After your script finishes drawing, the Turtle window will close immediately unless you tell it to wait.
screen.exitonclick()
: Waits for you to click on the screen before closing.turtle.done()
: Similar to exitonclick()
, often used at the end of a script.screen.mainloop()
: Also keeps the window open; used in event-driven Turtle programs.# At the end of your script:
screen = turtle.Screen() # Get the screen object if not already in scope
screen.exitonclick()
# OR
# turtle.done()