PYTHON TURTLE🐢 WITH PRACTICAL

 PYTHON TURTLE  LIBRARY 


    




 In Python, you use variables to store information that you’ll use later on in your program. You initialize a variable when you assign a starting value to it. Since the value of the variable isn’t constant, it can change several times during the execution of your program.

Now, to open the turtle screen, you initialize a variable for it in the following way:

You should see a separate window open up:

Python Turtle Initial Screen New

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.

Next, you initialize the variable t, which you’ll then use throughout the program to refer to the 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:

In the next section, you’ll explore the different ways of programming with the Python turtle library.

Programming With turtle

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.

Moving the Turtle

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:

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:

Python Turtle Moving Updated

You can use the shortened versions of these commands as well:

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:

Python Turtle Coordinates New

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:

Python Turtle GOTO NEWER

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).

Drawing a Shape

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:

Your output will look like this:

Python Turtle Square Edit Newer

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:

Python Turtle Circle Updated

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:

Python Turtle Dot Update

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:

You can replace "blue" with any other color. Try "green" or "red". You’ll get a result like this:

Python Turtle Background Color

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:

Your title bar will now display this:

Python Turtle Screen Title Updated

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:

Your outputs will look like this:

Python Turtle Shape Size Updated

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:

This results in an outcome like this:

Python Turtle Pen Size More NEW

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:

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:

Now, to change the color of the turtle (or the fill), you type the following:

Your turtle will look like this:

Python Turtle Fill Color Red

To change the color of the pen (or the outline), you type the following:

Your turtle will look like this:

Python Turtle Pen Color Updated Green

To change the color of both, you type the following:

Your turtle will look like this:

Python Turtle Color Single Line Updated

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.

Filling in an Image

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:

When you execute this code, you’ll get a triangle that’s filled in with a solid color, like this:

Python Turtle Begin Fill End Fill New

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:

The shape of the turtle will change accordingly, like this:

Python Turtle Shapes

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:

This code will first decrease the speed and move the turtle forward, then increase the speed and move the turtle forward again, like this:

Python Turtle Speed Updated

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:

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:

This will give you a result like this:

Python Turtle Single Line Pen Newer

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:

When you run this code, your output will look like this:

Python Turtle Pen Up Pen Down Edit

Here, you’ve obtained two parallel lines instead of a square by adding some extra commands in between the original program.

Undoing Changes

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.

Clearing the Screen

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.

Leaving a Stamp

You have the option of leaving a stamp of your turtle on the screen, which is nothing but an imprint of the turtle. Try typing in this code to see how it works:

Your output will look like this:

Python Turtle Stamps Edit

The numbers that appear are the turtle’s location or stamp ID. Now, if you want to remove a particular stamp, then just use the following:

This will clear the one with the stamp ID of 8.

Cloning Your Turtle

Sometimes, you may need to have more than one turtle on your screen. You’ll see an example of this later on in the final project. For now, you can get another turtle by cloning your current turtle into your environment. Try running this code to create a clone turtle, c, and then move both the turtles on the screen:

The output will look like this:

Python Turtle Clone NEWER

Comments

Popular posts from this blog

HANDWRITTEN NOTES (COMPUTER NETWORK ) USEFULL IN EXAMS (IMP TOPICS)

SESSION LAYER IN COMPUTER NETWORK

DATA STRUCTURE (IMP QUESTIONS )FOR (BE STUDENTS ,DIPLOMA STUNDENTS,UNIVERSITY) (CHEPTAR WISE )