Python's turtle module is a fun and interactive way to get started with programming. It provides a virtual canvas on which a turtle can be controlled to draw pictures and shapes. The turtle can be moved using commands like .forward() and .right(). The turtle's characteristics, such as its size, colour, and speed, can be customised, and its environment can be altered by changing the screen colour, title, and size.
The turtle library is ideal for introducing children to programming, but it's also a great way for adults to get to grips with Python. It can be used to draw shapes, create designs, and make images, as well as to develop mini-games and animations.
What You'll Learn
Turtle commands and methods
The Python turtle library comes with a range of commands and methods that can be used to create drawings and animations. Here are some of the most commonly used ones:
- Forward()/fd(): This command moves the turtle forward by a specified number of units in the direction it is facing.
- Backward()/bk(): This command moves the turtle backward by a specified number of units in the direction it is facing.
- Right()/rt(): This command turns the turtle to its right by a specified number of degrees.
- Left()/lt(): This command turns the turtle to its left by a specified number of degrees.
- Up(): This command lifts the turtle's pen, which means that it will not draw anything when it moves.
- Down(): This command puts the turtle's pen down, which means that it will draw a line when it moves.
- Goto()/setpos()/setposition(): This command moves the turtle to an absolute position on the screen, specified by the coordinates (x, y).
- Home(): This command sends the turtle back to its starting position, which is usually at the centre of the screen.
- Circle(): This command draws a circle with a specified radius.
- Dot(): This command draws a filled-in circle, which is essentially a dot.
- Reset(): This command clears the screen and restores the turtle's settings to their default values.
- Clear(): This command erases all drawings from the screen without moving the turtle.
- Speed(): This command sets the speed of the turtle's movement.
- Color(): This command changes the colour of the turtle's pen.
- Pencolor(): This command changes the colour of the outline of the turtle's shape.
- Fillcolor(): This command changes the colour that will be used to fill in a shape.
- Begin_fill(): This command is used before drawing a closed shape that needs to be filled in.
- End_fill(): This command is used after drawing a closed shape to fill it in with the current fill colour.
- Stamp(): This command leaves an imprint of the turtle's shape at its current location.
- Clone(): This command creates a clone of the turtle with the same position, heading, and characteristics.
These are just a few examples of the commands and methods available in the Python turtle library. There are many more that can be used to create complex drawings and animations.
Building a Lego Turtle: Step-by-Step Guide
You may want to see also
Turtle movements
Turtle is a Python feature like a drawing board, which lets us command a turtle to draw all over it. We can use functions like turtle.forward(…) and turtle.right(…) to move the turtle around.
The turtle always starts at (0, 0) in the x-y plane. After an import turtle, we can give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. We can give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.
We can use the following commands to move the turtle:
- Forward(distance) - moves the turtle forward by the specified amount
- Backward(distance) - moves the turtle backward by the specified amount
- Right(angle) - turns the turtle clockwise by the specified amount
- Left(angle) - turns the turtle counterclockwise by the specified amount
We can also use the following commands to pick up and put down the turtle's pen:
- Penup() - picks up the turtle's pen
- Pendown() - puts down the turtle's pen
We can also change the colour of the turtle's pen using the following command:
Color(colour) - changes the colour of the turtle's pen
We can also use the following commands to move the turtle to a specific position:
- Setx(xpos) - changes the x-coordinate of the turtle to xpos
- Sety(ypos) - changes the y-coordinate of the turtle to ypos
- Goto(x, y) - moves the turtle from the current position to the location x, y along the shortest linear path between the two locations
Crafting a Bowl: Turtle Shell Transformation
You may want to see also
Turtle shapes
The turtle module in Python is a fun and interactive way to create simple drawings and animations using a "turtle" that moves around the screen. It is a great way to introduce programming concepts to beginners.
The turtle module provides turtle graphics primitives in both object-oriented and procedure-oriented ways. It uses Tkinter for the underlying graphics, so a version of Python installed with Tk support is required.
Python
From turtle import *
Create a new turtle object
Skk = Turtle()
Move the turtle forward by 100 pixels
Skk.forward(100)
Turn the turtle counterclockwise by 90 degrees
Skk.left(90)
Change the color of the turtle's pen to blue
Skk.pencolor("blue")
You can also set the shape of the turtle cursor using the `shape() function. The predefined shapes include "turtle", "arrow", "circle", "square", and "triangle". For example:
Python
Change the shape of the turtle cursor to a circle
Skk.shape("circle")
In addition to the predefined shapes, you can also create custom shapes using the `register_shape() function. This function takes two parameters: the name of the shape and a tuple of tuples containing the coordinates for the custom shape. Here is an example of how to create and register a diamond shape:
Python
Create a new turtle object
Diamond_turtle = Turtle()
Define the coordinates of the diamond shape
Shape = ((0, 0), (10, 10), (20, 0), (10, -10))
Register the new shape with the name "diamond"
Turtle.register_shape("diamond", shape)
Change the shape of the turtle cursor to the custom diamond shape
Diamond_turtle.shape("diamond")
With these basics, you can start creating more complex drawings and animations using Turtle in Python.
The Egg-laying Habits of Yellow Belly Turtles Demystified
You may want to see also
Turtle colours
The turtle module in Python is a fun and interactive way to create simple drawings and animations using a "turtle" that moves around the screen. The turtle can be controlled using a range of functions and methods, including those that change its colour.
The turtle's colour can be changed using the `color`() method. This method takes in either a string of the colour name, such as "red" or "green", or an RGB tuple, such as `(41, 41, 253)`.
Python
From turtle import *
Create a new turtle
Skk = Turtle()
Change the colour of the turtle to red
Skk.color("red")
Move the turtle forward 100 pixels
Forward(100)
Turn the turtle 90 degrees to the right
Right(90)
Move the turtle forward another 100 pixels
Forward(100)
Turn the turtle 90 degrees to the right again
Right(90)
Move the turtle forward for the third side of the square
Forward(100)
Turn the turtle 90 degrees to the right again to complete the square
Right(90)
Move the turtle forward for the final side of the square
Forward(100)
The turtle's pen colour can also be changed using the `pencolor`() method, which works in the same way as the `color()` method.
Additionally, the background colour can be changed using the `bgcolor`() method in the `Screen class.
Why Do Baby Turtles Ride on Their Mother's Back?
You may want to see also
Turtle screen
The Python Turtle library comes with a similar interactive feature that gives new programmers a taste of what it’s like to work with Python.
In this tutorial, you will:
Understand what the Python turtle library is
Learn how to set turtle up on your computer
Program with the Python turtle library
Grasp some important Python concepts and turtle commands
Develop a short but entertaining game using what you’ve learned
The Python turtle library is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called the turtle and this is what gives the library its name. In short, the Python turtle library helps new programmers get a feel for what programming with Python is like in a fun and interactive way.
Turtle is mainly used to introduce children to the world of computers. It’s a straightforward yet versatile way to understand the concepts of Python. This makes it a great avenue for kids to take their first steps in Python programming. That being said, the Python turtle library is not restricted to little ones alone! It’s also proved extremely useful for adults who are trying their hands at Python, which makes it great for Python beginners.
With the Python turtle library, you can draw and create various types of shapes and images. Here’s a sample of the kinds of drawings you can make with turtle:
The screen is divided into four quadrants. The point where the turtle is initially positioned at the beginning of your program is (0,0). This is called Home. To move the turtle to any other area on the screen, you use .goto() and enter the coordinates like this:
Your output will look like this:
You’ve drawn a line from your current position to the point (100,100) on the screen.
To bring the turtle back to its home position, you type the following:
This is like a shortcut command that sends the turtle back to the point (0,0). It’s quicker than typing t.goto(0,0).
Now, to open the turtle screen, you initialize a variable for it in the following way:
>>> s = turtle.getscreen()
You should see a separate window open up:
This window is called the screen. It’s where you can view the output of your code. The little black triangular shape in the middle of the screen is called the turtle.
Note: Keep in mind that when you name a variable, you need to pick a name that can be easily understood by anyone who’s looking at your program. However, you must also choose a name that’s convenient for you to use, especially because you’ll be calling it very often throughout your program!
For example, choosing a name like my_turtle_screen_name would be tedious to keep typing, while a name like Joe or a would appear to be very random. Using a single alphabet character, like s in this case, would be much more suitable. That’s because it’s short and sweet, and it’s clear to remember that the letter s refers to the screen.
Next, you initialize the variable t, which you’ll then use throughout the program to refer to the turtle:
>>> t = turtle.Turtle()
Just like for the screen, you can also give this variable another name like a or Jane or even my_turtle, but in this case, you’ll stick with t.
You now have your screen and your turtle. The screen acts as a canvas, while the turtle acts like a pen. You can program the turtle to move around the screen. The turtle has certain changeable characteristics, like size, color, and speed. It always points in a specific direction, and will move in that direction unless you tell it otherwise:
When it’s up, it means that no line will be drawn when it moves.
When it’s down, it means that a line will be drawn when it moves.
In the next section, you’ll explore the different ways of programming with the Python turtle library.
The first thing you’ll learn when it comes to programming with the Python turtle library is how to make the turtle move in the direction you want it to go. Next, you’ll learn how to customize your turtle and its environment. Finally, you’ll learn a couple of extra commands with which you can perform some special tasks.
There are four directions that a turtle can move in:
The turtle moves .forward() or .backward() in the direction that it’s facing. You can change this direction by turning it .left() or .right() by a certain degree. You can try each of these commands like so:
>>> t.right(90) >>> t.forward(100) >>> t.left(90) >>> t.backward(100)
When you run these commands, the turtle will turn right by ninety degrees, go forward by a hundred units, turn left by ninety degrees, and move backward by a hundred units. You can see how this looks in the image below:
You can use the shortened versions of these commands as well:
T.rt() instead of t.right()
T.fd() instead of t.forward()
T.lt() instead of t.left()
T.bk() instead of t.backward()
You can also draw a line from your current position to any other arbitrary position on the screen. This is done with the help of coordinates:
The screen is divided into four quadrants. The point where the turtle is initially positioned at the beginning of your program is (0,0). This is called Home. To move the turtle to any other area on the screen, you use .goto() and enter the coordinates like this:
Your output will look like this:
You’ve drawn a line from your current position to the point (100,100) on the screen.
To bring the turtle back to its home position, you type the following:
This is like a shortcut command that sends the turtle back to the point (0,0). It’s quicker than typing t.goto(0,0).
Now that you know the movements of the turtle, you can move on to making actual shapes. You can start by drawing polygons since they all consist of straight lines connected at certain angles. Here’s an example that you can try:
>>> t.fd(100) >>> t.rt(90) >>> t.fd(100) >>> t.rt(90) >>> t.fd(100) >>> t.rt(90) >>> t.fd(100)
Your output will look like this:
Well done! You’ve just drawn a square. In this way, the turtle can be programmed to create different shapes and images.
Now, try drawing a rectangle, using this code as a template. Remember, in a rectangle, all four sides are not equal. You’ll need to change the code accordingly. Once you do that, you can even try creating other polygons by increasing the number of sides and changing the angles.
Drawing Preset Figures
Suppose you want to draw a circle. If you attempt to draw it in the same way as you drew the square, then it would be extremely tedious, and you’d have to spend a lot of time just for that one shape. Thankfully, the Python turtle library provides a solution for this. You can use a single command to draw a circle:
You’ll get an output like this:
The number within the brackets is the radius of the circle. You can increase or decrease the size of the circle by changing the value of its radius.
In the same way, you can also draw a dot, which is nothing but a filled-in circle. Type in this command:
You’ll get a filled-in circle like this:
The number within the brackets is the diameter of the dot. Just like with the circle, you can increase or decrease the size of the dot by changing the value of its diameter.
Great job so far! You’ve learned how to move the turtle around and create different shapes with it. In the next few sections, you’ll see how you can customize your turtle and its environment, based on your requirements.
Changing the Screen Color
By default, turtle always opens up a screen with a white background. However, you can change the color of the screen at any time using the following command:
>>> turtle.bgcolor("blue")
You can replace "blue" with any other color. Try "green" or "red". You’ll get a result like this:
You can use a variety of colors for your screen just by typing in their hex code number. To learn more about using different colors, check out the Python turtle library documentation.
Changing the Screen Title
Sometimes, you may want to change the title of your screen. You can make it more personal, like "My Turtle Program", or more suitable to what you’re working on, like "Drawing Shapes With Turtle". You can change the title of your screen with the help of this command:
>>> turtle.title("My Turtle Program")
Your title bar will now display this:
In this way, you can change the heading of your screen according to your preference.
Changing the Turtle Size
You can increase or decrease the size of the onscreen turtle to make it bigger or smaller. This changes only the size of the shape without affecting the output of the pen as it draws on the screen. Try typing in the following commands:
>>> t.shapesize(1,5,10) >>> t.shapesize(10,5,1) >>> t.shapesize(1,10,5) >>> t.shapesize(10,1,5)
Your outputs will look like this:
The numbers given are the parameters for the size of the turtle:
You can change these according to your preference. In the example given above, you can see a visible difference in the appearance of the turtle. For more information on how you can change the size of the turtle, check out the Python turtle library documentation.
Changing the Pen Size
The previous command changed the size of the turtle’s shape only. However, sometimes, you may need to increase or decrease the thickness of your pen. You can do this using the following command:
>>> t.pensize(5) >>> t.forward(100)
This results in an outcome like this:
As you can see, the size of your pen is now five times the original size (which was one). Try drawing some more lines of various sizes, and compare the difference in thickness between them.
Changing the Turtle and Pen Color
When you first open a new screen, the turtle starts out as a black figure and draws with black ink. Based on your requirements, you can do two things:
Change the color of the turtle: This changes the fill color.
Change the color of the pen: This changes the outline or the ink color.
You can even choose both of these if you wish. Before you change the colors, increase the size of your turtle to help you see the color difference more clearly. Type in this code:
>>> t.shapesize(3,3,3)
Now, to change the color of the turtle (or the fill), you type the following:
>>> t.fillcolor("red")
Your turtle will look like this:
To change the color of the pen (or the outline), you type the following:
>>> t.pencolor("green")
Your turtle will look like this:
To change the color of both, you type the following:
>>> t.color("green", "red")
Your turtle will look like this:
Here, the first color is for the pen, and the second is for the fill. Note that changing the color of the pen and the fill also changes the color of the onscreen turtle accordingly.
Coloring in an image usually makes it look better, doesn’t it? The Python turtle library gives you the option to add color to your drawings. Try typing in the following code and see what happens:
>>> t.begin_fill() >>> t.fd(100) >>> t.lt(120) >>> t.fd(100) >>> t.lt(120) >>> t.fd(100) >>> t.end_fill()
When you execute this code, you’ll get a triangle that’s filled in with a solid color, like this:
When you use .begin_fill(), you’re telling your program that you’re going to be drawing a closed shape which will need to be filled in. Then, you use .end_fill() to indicate that you’re done creating your shape and it can now be filled in.
Changing the Turtle Shape
The initial shape of the turtle isn’t really a turtle, but a triangular figure. However, you can change the way the turtle looks, and you do have a couple of options when it comes to doing so. You can have a look at some of them by typing in the following commands:
>>> t.shape("turtle") >>> t.shape("arrow") >>> t.shape("circle")
The shape of the turtle will change accordingly, like this:
You have a couple of other options that you can try as well:
The classic shape is the original shape. Check out the Python turtle library documentation to learn more about the types of shapes that you can use.
Changing the Pen Speed
The turtle generally moves at a moderate pace. If you want to decrease or increase the speed to make your turtle move slower or faster, then you can do so by typing the following:
>>> t.speed(1) >>> t.forward(100) >>> t.speed(10) >>> t.forward(100)
This code will first decrease the speed and move the turtle forward, then increase the speed and move the turtle forward again, like this:
The speed can be any number ranging from 0 (the slowest speed) to 10 (the highest speed). You can play around with your code to see how fast or slow the turtle will go.
Customizing in One Line
Suppose you want to set your turtle’s characteristics to the following:
From what you’ve just learned, the code should look something like this:
>>> t.pencolor("purple") >>> t.fillcolor("orange") >>> t.pensize(10) >>> t.speed(9) >>> t.begin_fill() >>> t.circle(90) >>> t.end_fill()
It’s pretty long, but not that bad, right?
Now, just imagine if you had ten different turtles. Changing all of their characteristics would be extremely tiresome for you to do! The good news is that you can reduce your workload by altering the parameters in just a single line of code, like this:
>>> t.pen(pencolor="purple", fillcolor="orange", pensize=10, speed=9) >>> t.begin_fill() >>> t.circle(90) >>> t.end_fill()
This will give you a result like this:
This single line of code changed the entire pen, without you having to change each characteristic individually. To learn more about this command, check out the Python turtle library documentation.
Great job! Now that you’ve learned to customize your turtle and the screen, take a look at some other important commands that are required while drawing with the Python turtle library.
Picking the Pen Up and Down
Sometimes, you may want to move your turtle to another point on the screen without drawing anything on the screen itself. To do this, you use .penup(). Then, when you want to start drawing again, you use .pendown(). Give it a shot using the code that you used previously to draw a square. Try typing the following code:
>>> t.fd(100) >>> t.rt(90) >>> t.penup() >>> t.fd(100) >>> t.rt(90) >>> t.pendown() >>> t.fd(100) >>> t.rt(90) >>> t.penup() >>> t.fd(100) >>> t.pendown()
When you run this code, your output will look like this:
Here, you’ve obtained two parallel lines instead of a square by adding some extra commands in between the original program.
No matter how careful you are, there’s always a possibility of making a mistake. Don’t worry, though! The Python turtle library gives you the option to undo what you’ve done. If you want to undo the very last thing you did, then type in the following:
This undoes the last command that you ran. If you want to undo your last three commands, then you would type t.undo() three times.
Right now, you probably have a lot on your screen since you’ve started this tutorial. To make room for more, just type in the following command:
This will clean up your screen so that you can continue drawing. Note here that your variables will not change, and the turtle will remain in the same position. If you have other turtles on your screen other than the original turtle, then their drawings will not be cleared out unless you specifically call them out in your code.
Resetting the Environment
You also have the option to start on a clean slate with a reset command. The screen will get cleared up, and the turtle’s settings will all be restored to their default parameters. All you need to to do is type in the following command:
This clears the screen and takes the turtle back to its home position. Your default settings, like the turtle’s size, shape, color, and other features, will also be restored.
Now that you’ve learned the fundamentals of programming with the Python turtle library, you’ll check out some bonus features that you may want to use while programming.
You have the option of leaving a stamp of your turtle on the screen, which
Understanding How Turtles Contract Tapeworm Infections
You may want to see also
Frequently asked questions
Python Turtle is a pre-installed library that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called the turtle and this is what gives the library its name. In short, the Python Turtle library helps new programmers get a feel for what programming with Python is like in a fun and interactive way.
To get started, make sure you have Python 3 installed on your computer. Then, open your Python environment, such as IDLE or Jupyter Notebook, and import the library:
```python
import turtle
```
The turtle has certain changeable characteristics, like size, color, and speed. It always points in a specific direction, and will move in that direction unless you tell it otherwise. You can use the following commands to move the turtle:
- `t.forward(100)` or t.fd(100) to move the turtle forward by 100 units
- `t.backward(100)` or t.bk(100) to move the turtle backward by 100 units
- `t.right(90)` or t.rt(90) to turn the turtle right by 90 degrees
- `t.left(90)` or t.lt(90) to turn the turtle left by 90 degrees
You can also use the `goto()` function to move the turtle to an absolute position on the screen. For example, `t.goto(100, 100)` will move the turtle to the coordinates (100, 100).
You can customize the appearance of the turtle and its environment using various functions provided by the Python Turtle library. Here are some examples:
- `t.shapesize(1, 5, 10)` to change the size of the turtle
- `t.pencolor("green")` to change the color of the pen
- `t.fillcolor("red")` to change the fill color of the turtle
- `t.speed(10)` to change the speed of the turtle
- `t.penup()` to lift the pen up so that the turtle moves without drawing
- `t.pendown()` to put the pen down so that the turtle draws as it moves
- `t.undo()` to undo the last command
- `t.reset()` to reset the turtle to its original position and settings
You can draw various shapes using the movement commands and some additional functions provided by the Python Turtle library. Here are some examples:
- To draw a square, use the following commands: `t.forward(100)`, `t.right(90)`, `t.forward(100)`, `t.right(90)`, `t.forward(100)`, `t.right(90)`, `t.forward(100)`, `t.right(90)`
- To draw a circle, use the `t.circle(radius) function, where `radius` is the radius of the circle
- To draw a dot, use the `t.dot(diameter) function, where `diameter` is the diameter of the dot
- To draw a polygon, use the `t.begin_fill()` function, followed by the movement commands to create the polygon, and finally, the `t.end_fill()` function to close the polygon and fill it with the current fill color