 # Calculating the area and perimeter of shapes using python Share : I’ve argued for a long time that Computer Science and Mathematics teaching could and should be much more integrated. For a long time, excellent mathematics educators have made use of graphics calculators (including programmable ones, such as the Texas Instruments TI-84 with its TI-BASIC and now built-in Python!) to turbo-charge maths learning. Now with the popularity and ease of access of Python programming, there is no good reason I can see for not learning significant parts of Mathematics with the help of the power of Python, and for applying what is learned in Maths to basic Python programming for most, if not all, students. It is clear that looking at each of these subjects through the perspective of the other deepens and broadens our understanding of both.

In this article we will explore how to use Python to calculate to areas and perimeters of various geometric shapes such as:

• Rectangles
• Triangles
• General Parallelograms
• Circles

I have provided functions for these calculations, as using functions is often a very good way to organise code and make it reusable for different values. You can of course simply use the code contained in the functions on its own, either by running a file containing it, or in Python interpreter mode, where you can evaluate expressions directly, without actually running a file.

## TRIANGLE AREA AND PERIMETER WITH PYTHON

To calculate the area of a triangle, we can use the following formula:

``area = (base * height) / 2``

Where `base` is the length of one of the sides of the triangle, and `height` is the perpendicular distance from the opposite vertex to the base.

We can define a function that calculates the area of a triangle given the lengths of its sides as follows:

``````def triangle_area(base, height):
return (base * height) / 2``````

The way you would use this function is to call it with the required lengths as arguments, like so:

``````base = 10
height = 5
print(triangle_area(base, height))``````

Output:

``25.0``

This means the area is 25 square units – whatever units were intended when we defined `base` and `height`.

To calculate the perimeter of a triangle, we can simply add up the lengths of all its sides. We can define a function that calculates the perimeter of a triangle given the lengths of its sides, like so:

``````def triangle_perimeter(side1, side2, side3):
return side1 + side2 + side3``````

## RECTANGLE AREA AND PERIMETER WITH PYTHON

To calculate the area of a rectangle, we can use the following formula:

``area = base * height``

Sometimes `length` and `width` are used, but I prefer to fix the orientation by refering to `base` and `height`.

We can define a function that calculates the area of a rectangle given the lengths of its sides as follows:

``````def rectangle_area(base, height):
return base * height``````

To calculate the perimeter of a rectangle, we can add up the lengths of all its sides. Since a rectangle has two pairs of equal length sides, we can add the base length to the height and double the result. We can define a function that calculates the perimeter of a rectangle given the lengths of its sides as follows:

``````def rectangle_perimeter(base, height):
return 2 * (base + height)```

```

## PARALLELOGRAM

A rectangle is a special case of a Parallelogram. For a general parallelogram, we need the vertical height.

To calculate the area of a parallelogram, we can use the following formula:

``area = base * height``

Where `base` is the length of one of the sides of the parallelogram, and `height` is the perpendicular distance from the opposite side to the base.

We can define a function that calculates the area of a parallelogram given the lengths of its sides as follows:

``````def parallelogram_area(base, height):
return base * height``````

To calculate the perimeter of a parallelogram, we can add up the lengths of all its sides. Since a parallelogram has two pairs of sides of equal length, we can simply add up the lengths of the two pairs and double the result to get the perimeter. We can define a function that calculates the perimeter of a parallelogram given the lengths of its sides just like we did for a rectangle, as follows:

``````def parallelogram_perimeter(side1, side2):
return 2 * (side1 + side2)``````

## AREA AND CIRCUMFERENCE OF A CIRCLE WITH PYTHON

The circumference of a circle is the distance around the outside. The formula for calculating the circumference of a circle is `2 * pi * radius`, where `pi` is a mathematical constant approximately equal to `3.14159` and `radius` is the distance from the centre of the circle to the edge. Since the radius is half the diameter, we can also use `pi * diameter`.

To find the circumference and radius of a circle in Python, we can use the `math` module, which provides mathematical functions and constants such as `pi`. Here is how we can use it to find the area and circumference of circles:

``````import math

# Find the circumference of a circle with radius 5
circumference = 2 * math.pi * radius
print(circumference)  # 31.41592653589793

# Find the area of a circle with radius 5
area = math.pi * radius ** 2
print(area)  # 78.53981633974483``````

In this example, we import the `math` module and use its `pi` constant to calculate the circumference and area of a circle with a given radius.

Using the `math` module is not the only way to calculate the circumference and radius of a circle in Python. For example, you could define your own constant for `pi`. However, using the `math` module is a simple and straightforward approach that is suitable for most cases.

## TURTLE GRAPHICS CIRCLES CHALLENGE:

Check out this fun coding challenge using Turtle Graphics to draw concentric circles:

Drawing Circles With Python Turtle Graphics

This article has shown how to use Python to calculate the area and permiter of various geometric shapes with Python. I hope you found it useful.

Happy computing!