# Drawing a Square and a Rectangle in Turtle - Python

## Turtle in Python :

Along with Python, comes a module called turtle. It offers drawing with a cardboard screen and a turtle (pen). Move the turtle to sketch anything on the screen (pen).

In other words, python has a feature called "Turtle" that functions like a whiteboard and allows us to instruct a turtle to draw all over it. There are other functions, such as forward() and reverse(), to move the turtle. The turtle may be advanced from a position to other using the functions like turtle.forward() and turtle.right().

The library is called the turtle, and the title of the on-screen pen we use to draw with it is the turtle. In conclusion, learning Python programming with the help of the Python turtle library is entertaining and interesting for novice programmers. Children are typically introduced to computers through Turtle.

## Using Turtle for plotting :

We must import turtle library, in order to access its various features and methods. The python Language package has in-built library "turtle", thus it is not a requirement to install it separately. The four steps that make up the road map for carrying out a turtle programme are as followed :

1. Add the turtle module.
2. Creating a turtle you can manage.
3. Make use of the turtle drawing techniques.
4. Using done() method.

As was already mentioned, we must import turtle before using it. It is imported as :

We should first establish a new drawing board (window) and turtle after importing the turtle library and enabling all of its features for us. We assigned the turtle a name ttl?and the window a name wndw. As a result, we used it in the code as :

The turtle needs to be moved now that the window and the turtle have been made. We code further to advance ttl by 200 pixels in the direction ttl is facing.

We've advanced ttl by 200 pixels.?With the help of the done() function, we can now call the programme to an end.

## Drawing the shapes :

Forward() and Left() are two functions that we can utilise to draw squares and rectangles. The fundamental characteristics of each shape must be understood before we can draw it.

## Square :

Let's begin by using a square. An equal number of sides make up a square. And there is a 90° angle between the two adjacent sides. Parallel sides are situated next to one another.

### Explaining the code :

We now know the square's fundamental characteristics i.e. all sides are equal. Python Turtle needs a square drawn now. Assume that a square's side is 200 units long.

We imported?the turtle module here?at this time. After then, a new drawing board was made and given to an object with the name?ttl.

The turtle has advanced by 150 units in the forward direction as a square's side is 150 units long. As the angle between neighbouring sides is 90°, we then turned the turtle 90°. The square's one side is now complete.

Here we repeated the last step three times to construct the remaining three sides of the square in a similar way as we did for the first side. To draw the remaining three sides, the same statements were reiterated three more times.

Output :

### Creating a square in Turtle using loops :

As we can see, we repeated the same statements (forward(150) and left(90)) four times in the code above. So, rather than writing them repeatedly, we may use a loop that runs four times.

### Complete Code :

Output :

The output of the aforementioned code will be same as that of the previous code.

### Explanation :

In this program, instead of using the forward(150) and left(90) function four times, we used it only once but executed it four times by the help of a for loop to get the same desired output as that in the previous program.

### Rectangle :

We are very well aware of the fact that the rectangle's diagonals are equal. Moreover, the opposite sides of a rectangle are of equal lengths. A rectangle's neighbouring sides meet at a 90° angle. We shall draw the rectangle bearing in mind these characteristics. Let's say the rectangle has a length of 140 units and a width of 70 units. Using the code below we will get a rectangle in turtle.

### Explaining the code :

We imported?the turtle module here?at this time. After then, a new drawing board was made and given to an object with the name?ttl.

The turtle has advanced 140 units in our direction because a rectangle's length is 140 units. As the angle between neighbouring sides is 90°, we then turned the turtle 90°. The rectangle's one side is now complete. The turtle was then turned 90 degrees and advanced 70 units. The rectangle's second side is now complete.

To draw the final two sides, the same arguments are repeated once again in both directions. Ultimately, completing the code to create a rectangle in Turtle Python.

Output :

### Drawing a rectangle in Turtle using loops :

Using a for loop for drawing is pretty much similar to how we used it for drawing a square. In a for loop, we'll put forward(140), left(90), forward(70), and left(90) and execute it twice.

Output :

### Explanation :

In this program, instead of using the forward(140), left(90), forward(70) and left(90) ?function two times, we used it only once but executed it two times by the help of a for loop to get the same desired output as that in the previous program.

## Drawing a square and a rectangle together :

As the heading suggests, we will be drawing a square and a rectangle in one single program altogether with the help of various functions of the turtle python library. The code for the following is given below :

Output :

### Explanation :

In the aforementioned programme, first of all, we imported the turtle library to our program. Then we used a for loop to draw a square first. The loop consisted of the forward(60) and left(90) methods and executed four times to complete a square. Then we used the up() method to lift up the turtle pen and moved the pen to the new coordinates using the method goto(80,0). Then we used the down() method to start using the turtle pen again. Next, we drew a rectangle, using the methods forward(120) and left(90) to draw the first side of rectangle and the methods forward(80) and left(90) to draw the second side of the rectangle. We then repeated the last two steps again in order to draw the remaining two sides of the rectangle.

Finally, as a result we got an output where we got both, a square and a rectangle drawn.?

## Conclusion :

In this article, we used?Python's Turtle library?to draw a square and a?rectangle in various possible ways. We believe that this article?will clarify the idea behind the turtle library and one of its applications, which is to create various shapes.

For Videos Join Our Youtube Channel: Join Now