Python Program To Calculate The Area Of A Triangle

Python Program To Calculate The Area Of A Triangle

In this tutorial, you will learn about a python program to calculate the area of a triangle.

Calculating the area of a triangle is a fundamental task in mathematics and programming.

Python, being a versatile programming language, offers several approaches to accomplish this task.

In this blog post, we will explore various Python programs that we can use to calculate the area of a triangle.

Each program will be employing different techniques and formulas.

So, let’s dive in and discover the possibilities!

Method 1

Python Program To Calculate The Area Of A Triangle Using Base And Height

We can calculate the area of a triangle by multiplying the base and height of the triangle and dividing the result by 2.

Here’s a Python program that uses this formula:

Python Program To Calculate The Area Of A Triangle

def calculate_area_base_height(base, height):
    # Calculate the area of a triangle using base and height
    area = (base * height) / 2
    return area

# Example usage
base = 10 
height = 6  
area = calculate_area_base_height(base, height)  # Call the function 'calculate_area_base_height' with 'base' and 'height' as arguments, and assign the result to the variable 'area'
print("Area of the triangle:", area)  # Display the calculated area of the triangle

You can run this code on our free Online Python Compiler.

Output

Area of the triangle: 30.0

In the calculate_area_base_height function, we define a function that takes two parameters: base and height.

Inside the function, we calculate the area of the triangle by multiplying the base and height, and then dividing the result by 2.

This calculated area is stored in the variable area, which is then returned by the function.

In the example usage section, we assign specific values to the variables base and height.

These values represent the base length and the height of the triangle, respectively.

Next, we call the calculate_area_base_height function with the provided base and height as arguments,

And assign the returned area value to the variable area.

Finally, we print the result, which displays the calculated area of the triangle on the console.

Method 2

Program using Lengths of All Three Sides (Heron’s Formula)

We can use Heron’s formula to calculate the area of a triangle when we know the lengths of all sides.

Here’s a Python program that employs Heron’s formula:

import math  # Importing the math module to use mathematical functions

def calculate_area_heron(side1, side2, side3):
    # Calculate the area of a triangle using Heron's formula
    s = (side1 + side2 + side3) / 2  # Calculate the semi-perimeter of the triangle
    area = math.sqrt(s * (s - side1) * (s - side2) * (s - side3))  # Calculate the area using Heron's formula
    return area

# Example usage
side1 = 5 
side2 = 7
side3 = 8
area = calculate_area_heron(side1, side2, side3)  # Call the function 'calculate_area_heron' with 'side1', 'side2', and 'side3' as arguments, and assign the result to the variable 'area'
print("Area of the triangle:", area)  # Display the calculated area of the triangle

Output

Area of the triangle: 17.32050807568877

In this code snippet, we first import the math module to access the sqrt function,

which we will be using for calculating the square root.

The calculate_area_heron function takes three parameters: side1, side2, and side3, representing the lengths of the triangle’s sides.

Inside the function, we calculate the semi-perimeter of the triangle s by summing up all three sides and dividing the result by 2.

Then, using Heron’s formula, we calculate the area by taking the square root of the product of s and the differences between s and each side length.

The calculated area is stored in the variable area, which is then returned by the function.

In the example usage section, we assign specific values to the variables side1, side2, and side3, representing the lengths of the sides of the triangle.

Then, we call the calculate_area_heron function with the provided side lengths as arguments and assign the returned area value to the variable area.

Finally, we print the result, which displays the calculated area of the triangle on the console.

Method 3

Program using Coordinates of Triangle Vertices (Shoelace Formula)

The Shoelace formula, also known as Gauss’s area formula, allows us to calculate the area of a triangle.

We can use this method when the we have the values for coordinates of its three vertices.

Here’s a Python program that implements the Shoelace formula:

def calculate_area_shoelace(vertices):
    n = len(vertices)
    area = 0

    for i in range(n):
        j = (i + 1) % n
        area += vertices[i][0] * vertices[j][1]
        area -= vertices[i][1] * vertices[j][0]

    area = abs(area) / 2
    return area

# Example usage
vertices = [(0, 0), (4, 0), (0, 3)]
area = calculate_area_shoelace(vertices)
print("Area of the triangle:", area)

You can run this code on our free Online Python Compiler.

Output

Area of the triangle: 6.0

Wrapping Up

Conclusions: Python Program To Calculate The Area Of A Triangle

In this blog post, we explored several Python programs to calculate the area of a triangle using different techniques.

Depending on the given information you can choose the appropriate program to obtain accurate results.

Python’s flexibility allows you to tackle various scenarios, making it a powerful language for mathematical computations.

By understanding these programs, you can enhance your problem-solving skills and apply them to real-life applications.

Love this read? Check out the best Online Python Compiler In the world.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading