In this article, we will explore different ways to calculate the Area and Perimeter of a Triangle using Python, with detailed explanations and examples.
Table of contents
What is a Triangle?
A triangle is a three-sided polygon (2D shape) with three edges and three vertices. It is one of the basic shapes in geometry and has several properties that define its angles, sides, and area.
A triangle has a base (b) and a height (h), which are used to calculate the area.
Examples of Triangles in Real Life are Pizza slices, Pyramid structures, Roofs of houses, etc.

Understand Area of a Triangle
The area of a triangle is the amount of space enclosed within its three sides. It is measured in square units such as square centimeters (cm²), square meters (m²), or square inches (in²).
Formula to Calculate the Area of a Triangle:
Area = 1⁄2 × Base × Height
- Base (b) = Any side of the triangle used as a reference.
- Height (h) = The perpendicular distance from the base to the opposite vertex.
- Area (A): The total surface covered by the triangle, measured in square units (e.g., cm², m², inches²).
For Example, If a triangle has: Base = 10 cm, Height = 5 cm
Then, the Area is: Area = 1⁄2 × 10 × 5 = 50⁄2 = 25 cm²
1. How to Calculate Area of a Triangle
Below are the steps to calculate the area of a triangle in Python:
- Define the Base and Height
Assign values to base and height.
For example, base = 10, height = 5 - Apply the Area Formula
Use the formula:
Area = 0.5 * base * heightarea = 0.5 * 10 * 5 = 25 - Mention the Correct Unit
If the measurements are in meters, the area will be in square meters (m²).
If the measurements are in centimetres, the area will be in square centimetres (cm²).
If the measurements are in inches, the area will be in square inches (in²) - Display the result
Display the area and perimeter using the
print()function
Code Example
Output:
Area of the triangle: 25.0 square units
2. Calculate Area of Triangle Using User Inputs
If you want to calculate the area using the values provided by user then use the input() function. In this approach, we take the user’s input for the triangle’s base and height.
Note: As user inputs are by default in string format, we must convert them into floats for the calculations.
Code Example
3. Calculate Area of Triangle Using a Function
There are multiple advantages of using a function to calculate the area of a triangle as follows:
- A function allows us to write the calculation once and use it multiple times without repeating code.
- A function makes the code cleaner and more readable, improving maintainability.
- Avoid errors: Using function we can keep formula in one place. It helps prevent mistakes
- Easier Debugging & Updating: If the formula needs to be updated or changed, a function makes it easy to modify in one place.
In the example below, we calculated the area of two triangles by calling the same function.
Code Example:
4. Calculate Area of Triangle Using Class (OOP)
Object-Oriented Programming (OOP) is a structured way to write code by creating classes and objects. A class is a blueprint for creating objects, while an object is an instance of a class.
This approach provides better structure, reusability, and maintainability in your code. This approach is highly useful, especially for larger applications where multiple triangles need to be handled efficiently.
The following are the advantages of using OOP to calculate the area of a triangle:
- Reusability: If you need to calculate the area of multiple triangles, you can create multiple objects.
- Extensibility: With OOP, we can easily extend the class to calculate perimeter without affecting the existing structure.
Code Example
Explanation
- First Define a Class
- Define a class named
Triangle. - The class will store the base and height of the triangle.
- Define a class named
- Now, Create a Constructor using the
__init__method- The constructor (
__init__) initializes the base and height when an object is created. - The
selfkeyword refers to the current instance of the class.
- The constructor (
- Now, Create a Method to Calculate Area
- Define a method
calculate_area()inside the class. - This method returns the area using the formula:
Area = 0.5 * Base * Height
- Define a method
- Now, Create an Object of the Class
- Use
triangle1 = Triangle(10, 5)to create a triangle with:base = 10, height = 5 - Use
triangle2 =to create a triangle with:Triangle(20, 7)base = 20, height = 7
- Use
- In the end, call the mkethod to get the area and print it
- Use
calculate_area()to get the area. - Store the result in a variable and display it using
print().
- Use
5. Calculate Area of Triangle Using Lambda
A lambda function in Python is a small, anonymous function that can have multiple arguments but only one expression, which is evaluated and returned. For a more compact implementation, you can use a lambda function.
A lambda function syntax: lambda arguments: expression
lambda→ Keyword to define the function.arguments→ Input parameters (like a normal function).expression→ A single operation that returns a result.
Code Example
Explanation
baseandheightare the input parameters.0.5 * base * heightis the calculation expression.triangle_areais the name to call the anonymous lambda function.
Summary
Calculating the area of a triangle in Python is a straightforward task with multiple flexible approaches. Whether you’re using basic arithmetic, user input, functions, object-oriented programming, or lambda expressions, each method offers a clear way to apply the logic effectively.
These approaches reinforce core programming skills and make your code modular, reusable, and scalable. Choosing the right method depends on your specific use case, whether it’s a quick calculation or part of a larger application.

Leave a Reply