Solid square in python assignment expert

ou are given a side of a square as input. Write a program to find the perimeter and area of the square.

Input

The input is an integer representing the length of the side of the square.

Output

The first line of the output should contain area of the square, and the second line of the output should contain the perimeter of the square as per the format shown in the sample output.

Explanation

Given the length of the side is 2.

As the area of square is side*side and the perimeter of the square is 4*side.

Then output should be

4

8

Sample Input 1

3

Sample Output 1

9

12

Sample Input 2

4

Sample Output 2

16

16

Solid Rectangle - 2

This Program name is Solid Rectangle - 2. Write a Python program to Solid Rectangle - 2, it has two test cases

The below link contains Solid Rectangle - 2 question, explanation and test cases

//drive.google.com/file/d/1wbpSRbuN1zsktqN5LIQS5vwRV2kwkrU1/view?usp=sharing

We need exact output when the code was run

rows = int[input["Please Enter the Total Number of Rows  : "]]
columns = int[input["Please Enter the Total Number of Columns  : "]]


print["Rectangle Star Pattern"] 
for i in range[rows]:
    for j in range[columns]:
        print['+', end = '  ']
    print[]

Learn more about our help with Assignments: Python

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at Thank you

We all know that Python offers a variety of libraries for game Development, GUI Development, Data Visualization, and many more. These libraries not only make our work easy but also make the code efficient and deployable. Keeping this thing in mind, today, in this article, we will discuss and learn about Graphics Programming by using a library named Turtle. Here, we will cover the basics of Python Turtle Shapes, with an in-depth explanation of code and also share the output for your reference. Have we ever wondered how can Python help us in drawing shapes? Read and learn to find your answer.

Basically, we will cover all the different shapes that can be drawn using the Turtle library. We will also learn about adding colors to those shapes. This tutorial will emphasize on basics of the turtle library. If you are intermediate, we recommend visiting our Tutorials on Turtle Projects. Before moving ahead, let’s take a basic overview of what this library is used for and then kick-start our article Python Turtle Shapes.

Basic Overview

A pre-installed Python module called turtle gives users a virtual canvas on which to draw shapes and images. The library gets its name from the on-screen pen we will use to sketch, known as the turtle. In brief, the Python turtle library gives beginning programmers an engaging introduction to learning basic graphics programming. Now let’s start with the coding part of our article.

Drawing Different Python Turtle Shapes

1. Drawing a Square

import turtle

t = turtle.Turtle[]
s = turtle.Screen[]

def draw_square[length]:
    for i in range[0,4]:
        t.forward[length]
        t.right[90]

draw_square[100]

Explanation:
Line 1: We imported the turtle library
Line 3: Here we simply stored the constructor method named Turtle[] in a variable named “t”. So we will simply use “t” instead of a turtle.Turtle[]
Line 4: turtle.Screen[] is used to display a canvas wherein we can draw our graphics.
Line 6: Now we have simply defined a function that will be responsible for drawing a square.
Line 7 to 11: Used a for loop. Inside that, we used the forward[] and right[] functions of the turtle library. In the end, we called our function by passing a length i.e 100.

Output:

Other Variations of Square

1. A square with green pen color

import turtle

t = turtle.Turtle[]
s = turtle.Screen[]

t.pencolor["green"]

def draw_square[length]:
    for i in range[0,4]:
        t.forward[length]
        t.right[90]

draw_square[100]

s.exit title[]

2. A square with a green pencolor and filled with a light-blue color

import turtle

t = turtle.Turtle[]
s = turtle.Screen[]

t.fillcolor["light blue"]

t.pencolor["green"]

t.begin_fill[]

def draw_square[length]:
    for i in range[0,4]:
        t.forward[length]
        t.right[90]

draw_square[100]

t.end_fill[]

s.exit title[]

2. Drawing a Circle

Here’s a very easy way to draw a circle in the turtle among all other shapes in Python Turtle Shapes. It would be quite time-consuming and tiresome to try to draw it using the same technique that you used for the square, so don’t even try it. Thankfully, there is a fix for this in the Python turtle library. To create a circle, just provide the following command:

import turtle

t = turtle.Turtle[]
s = turtle.Screen[]

t.circle[60]

s.exit title[]

Explanation:
This circle[] function from the turtle library allows a user to draw the circle of any radius. The circle’s radius is indicated by the number in brackets. By altering the circle’s radius value, you can make it larger or smaller.

  • Syntax: t.circle[radius, extent=None, steps=None]
  • radius: The circle’s circumference.
  • extent: The arc-shaped portion of the circle in degrees.
  • steps: Separate the form into the specified number of equal-sized steps.

Output:

Other Variations of Circle

1. Tangent circles

import turtle
    
t = turtle.Turtle[]
s = turtle.Screen[]
  
# radius for smallest circle
r = 10
  
# number of circles
n = 10
  
# loop for printing tangent circles
for i in range[1, n + 1, 1]:
    t.circle[r * i]

s.exit title[]

2. Spiral circles

import turtle
    
t = turtle.Turtle[]
s = turtle.Screen[]
  
# taking radius of initial radius
r = 10
  
# Loop for printing spiral circle
for i in range[100]:
    t.circle[r + i, 45]

s.exit title[]

3. Drawing a Dot

import turtle
 
t = turtle.Turtle[]
t.dot[40,"red"]

turtle.done[]

Explanation:
This is another simplest shape that one can easily draw using the turtle library. To draw a dot, you don’t need any other logic. All you need to use is simply a dot[] function.

  • Syntax: t.dot[size=None, *color]
  • size: an integer that is greater than or equal to 1
  • color: name of the color or color tuple

Output:

Other Variations of Dot

Multiple colored dots of different size

import turtle

turtle.delay[500]

turtle.ht[]

# some dots with diameter and color
turtle.dot[200, color="red"]
turtle.dot[180, color="orange"]
turtle.dot[160, color="yellow"]
turtle.dot[140, color="green"]
turtle.dot[120, color="blue"]
turtle.dot[100, color="indigo"]
turtle.dot[80, color="violet"]
turtle.dot[60, color="white"]

# write text
turtle.write["CopyAssignment", align="center",
			font=['Verdana', 12, 'bold']]

4. Drawing a Rectangle

Now, moving on to drawing another shape in this article on Python Turtle Shapes. Here we will be drawing rectangles.

import turtle
    
t = turtle.Turtle[]
s = turtle.Screen[]

def draw_rectangle[length, height]:
    for i in range[0,4]:
        if i % 2 == 0: 
            t.forward[length]
            t.right[90]
        else: 
            t.forward[height]
            t.right[90]

draw_rectangle[200, 100]

Explanation:
Line 1 to 4: Basic import and adding screen [Refer here for detailed explanation]

Line 6 to 15: Here we defined a function that will take the length and height of the rectangle. In this function, we used a for loop in the range of 0 to 4. Now with the use of a conditional statement, we checked if i%2 is equal to 0 [basically checking whether “i” is even or not]. If it is even then the turtle will move forward, with the units equal to the one we passed in length, and then will turn 90 degrees towards the right. If “i” is odd then the flow will enter in the else block.

Line 16: Until the mouse is clicked, this function is used to enter the main loop. There’s no need for debate about that.

Output:

5. Drawing an Ellipse/Oval

import turtle
    
t = turtle.Turtle[]
s = turtle.Screen[]
 
def draw[rad]:
     
  for i in range[2]:
    turtle.circle[rad,90]
    turtle.circle[rad//2,90]
 
turtle.seth[-45]
draw[100]

s.exit title[]

Explanation:
Line 6: draw[] function definition
Line 8 to 10: Used a for loop that will run in between the range of 0 to 2. We then used a turtle.circle[] function. This function takes multiple arguments [Refer here for a detailed explanation]. Here in our case, we passed “rad” and “90”. rad is equivalent to 100 and another argument 90 is the arc-shaped portion of the circle in degrees.
Now, we again used the circle function but this time we divided the rad by 2 and turned the turtle by 90 degrees.
Line 11: turtle.seth[]

  • Syntax 1: turtle.seth[to_what_angle]
  • Syntax 2: turtle.setheading[to_what_angle]
  • to_what_angle: a number [can be integer or float]
    Set the turtle’s orientation to angle. Here are a few typical directions [standard – mode] in degrees:
    East: 0, North:90, West: 180, South: 270

Output:

6. Drawing a Triangle

Moving on to our next shape. In this article on Python Turtle Shapes, we have covered many shapes till now. And drawing a triangle is much easier than the previous one. Let’s see how.

import turtle
 
t = turtle.Turtle[]

t.forward[200] 
 
t.left[120]
t.forward[200]

t.left[120]
t.forward[200]

Explanation:
Drawing triangle among other Python Turtle Shapes is very easy. All we have used are basic turtle functions like forward[], right[], etc. In order to draw an equivalent triangle, we used a 120-degree angle and so we passed 120 on left[].

Output:

7. Drawing a Star

import turtle
 
board = turtle.Turtle[]
 
# first triangle for star
board.forward[100] 
 
board.left[120]
board.forward[100]
 
board.left[120]
board.forward[100]
 
board.penup[]
board.right[150]
board.forward[50]
 
# second triangle for star
board.pendown[]
board.right[90]
board.forward[100]
 
board.right[120]
board.forward[100]
 
board.right[120]
board.forward[100]
 
turtle.done[]

Explanation:
Whenever there’s a need to draw a star, at first we will simply make a simple triangle. Once that’s done, we need to draw an inverted triangle on our triangle. For that purpose, we need to lift the pen up, in order to take it to another place and then start drawing our inverted triangle.
For this purpose, we have used penup[] on line 14. This will basically pick up the turtle once the normal triangle is drawn. Now, once we move our cursor to a different coordinate by using the right[] and forward[], we will use pendown[] so that turtle can start drawing again. This is the only difference between drawing a triangle and a star.

Output:

8. Drawing a Hexagon

import turtle

s = turtle.Screen[]
t = turtle.Turtle[]
 
for i in range[6]:
    t.forward[90]
    t.left[300]

s.exit title[]

Explanation:
The very last shape in the series of Python Turtle Shapes is Hexagon. This is almost similar to drawing a triangle. But the only difference is that here we will run the for loop for 6 times as the hexagon has 6 sides. Also, we will turn the turtle using the left[] by 300. As the angle between each of the sides of the hexagon is the same, this loop will go on and display the hexagon once the code flow comes out of the for a loop.

Output:

Some Other Functions

Below are some of the functions that you can use while drawing the above shapes.

Function NameDescription
t.pencolor[] This function allows a user to change the color of the pen. It offers a wide range of standard colors like red, green, blue, and cyan, as well as options like light green, turquoise, sky blue, etc
t.fillcolor[] The shapes that turtles draw can also be filled with a certain color. The line and fill colors are identical by default.
t.shape[] One can also change the shape of the turtle. The six options for a turtle’s form provided by the Python library are “arrow,” “circle,” “classic,” “square,” “triangle,” and “turtle.” Turtles start out with the ‘traditional’ shape by default.

Reference Links

  • Tutorials on Various Logos and Shapes using Turtle: Click Here
  • Official Turtle Docs: Click Here

Conclusion

All in all, in this article we tried to cover as many shapes as we can along with their detailed explanation. We hope this article on Python Turtle Shapes was of great help to you and a good learning experience. These shapes are the basics of a turtle. We encourage all our readers to try their level best and come up with new shapes. As mentioned earlier, we have a dedicated series on Python Turtle Shapes and Logos. We have included logos of many companies as well as very unique beginners to advanced-level turtle projects. Do explore and keep building. We will be back with amazing articles like Python Turtle Shapes very soon. Thank you for visiting our website

Thank you for reading this article.

Also Read:

Chủ Đề