In this article, we will explore different ways to calculate the Perimeter of a Triangle using Python, with detailed explanations and examples.
Table of contents
- What is a Triangle?
- Understand the Perimeter of a Triangle
- 1. How to Calculate Perimeter of Triangle
- 2. Calculate Perimeter of Triangle Using User Inputs
- 3. Calculate Perimeter of Triangle Using a Function
- 4. Calculate Perimeter of Triangle Using Class (OOP)
- 5. Calculate Perimeter of Triangle Using Lambda
- Summary
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.
Examples of Triangles in Real Life are Pizza slices, Pyramid structures, Roofs of houses, etc.

Understand the Perimeter of a Triangle
The perimeter of a triangle is the total length around the triangle — in other words, it is the sum of the lengths of its three sides.
Perimeter = a + b + c
Where:
- a, b, and c are the lengths of the triangle’s three sides.
- Units are the same as the sides (e.g., cm, m, inches).
For Example, a triangle has sides of length: a = 5 cm, b = 6 cm, c = 7 cm
Then, perimeter = 5 + 6 + 7= 18 cm
1. How to Calculate Perimeter of Triangle
Below are the steps to find the Perimeter of a Triangle in Python:
- Identify the side lengths
Triangle has three sides.
For example, a = 5 cm, b = 10 cm, c = 12 cm - Use the Perimeter Formula
The formula to calculate the Perimeter of a triangle is:
perimeter = a + b + cperimeter = 5 + 10 + 12 = 27cm - Mention the Correct Unit
If sides are in meters (m) → Perimeter is in meters (m)
If sides are in centimeters (cm) → Perimeter is in centimeters (cm)
If sides are in inches (in) → Perimeter is in inches (in) - Display the result
Display the output using
print()function
Code Example
Output:
Perimeter of the triangle: 18
2. Calculate Perimeter of Triangle Using User Inputs
Using the input() function we can ask user to provide values for all three sides of a triangle
Note: As the user inputs are in string format, we must convert them into a float for the calculations.
Code Example
Output:
Enter side a: 5
Enter side b: 10
Enter side c: 12
Perimeter of the triangle: 27.0
3. Calculate Perimeter of Triangle Using a Function
There are multiple advantages of using a function to calculate the perimeter of a triangle as follows:
- A function allows us to write the calculation once and use it multiple times without repeating code.
- 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 perimeter of two triangles by calling the same function.
Code Example
4. Calculate Perimeter 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 perimeter of a triangle:
- Extensibility: With OOP, we can easily extend the class to calculate area without affecting the existing structure.
- Reusability: If you need to calculate the perimeter of multiple triangles, you can create multiple objects.
Code Example
Explanation:
- Define a Class
- Define a class named
Triangle. - The class will store all three sides of the triangle.
- Define a class named
- Create a Constructor (
__init__method)- The constructor (
__init__) initializes the three sides when an object is created. - The
selfkeyword refers to the current instance of the class.
- The constructor (
- Create a Method to Calculate Perimeter
- Define a method
calculate_perimeter()inside the class. - This method returns the perimeter using the formula:
perimeter = a + b + c
- Define a method
- Create an Object of the Class
- Use
triangle1 = Triangle(5, 6, 7)to create a triangle with:a = 5, b = 6, c = 7 - Use
triangle1 = Triangle(10, 12, 14)to create a triangle with:a = 10, b = 12, c = 14
- Use
- Call the Method to get the Perimeter and print it
- Use
calculate_perimeter()to get the perimeter. - Store the result in a variable and display it using
print().
- Use
5. Calculate Perimeter 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
- a, b, and c are the input parameters.
a + b + cis the calculation expression.triangle_perimeteris the name to call the anonymous lambda function.
Summary
The article offers a clear and simple way for beginners to understand and implement triangle perimeter calculations in Python, helping them strengthen basic programming and math concepts.

Leave a Reply