PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Calculate Perimeter of Triangle

Python Programs to Calculate Perimeter of Triangle

Updated on: April 17, 2025 | Leave a Comment

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.

Perimeter of Triangle

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:

  1. Identify the side lengths

    Triangle has three sides.
    For example, a = 5 cm, b = 10 cm, c = 12 cm

  2. Use the Perimeter Formula

    The formula to calculate the Perimeter of a triangle is:
    perimeter = a + b + c
    perimeter = 5 + 10 + 12 = 27 cm

  3. 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)

  4. Display the result

    Display the output using print() function

Code Example

# Get all sides of triangle
side1 = 5
side2 = 6
side3 = 7

# Perimeter Formula
perimeter = side1 + side2 + side3
print("Perimeter of the triangle:", perimeter)Code language: Python (python)

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

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

perimeter = a + b + c
print("Perimeter of the triangle:", perimeter)Code language: Python (python)

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

# Function to calculate perimeter
def triangle_perimeter(a, b, c):
    return a + b + c

# Example usage
p1 = triangle_perimeter(5, 6, 7)
print("Perimeter of the triangle:", p1)

p2 = triangle_perimeter(10, 12, 14)
print("Perimeter of the triangle:", p2)


# Output:
# Perimeter of the triangle: 18
# Perimeter of the triangle: 36Code language: Python (python)

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

# Define a class named Triangle
class Triangle:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    # Method to calculate perimeter
    def calculate_perimeter(self):
        return self.a + self.b + self.c

# Create an object of the Triangle class
triangle1 = Triangle(5, 6, 7)
triangle2 = Triangle(10, 12, 14)

# Call the method to calculate perimeter
p1 = triangle1.calculate_perimeter()
p2 = triangle2.calculate_perimeter()

# Display the perimeter
print("Perimeter of the triangle1:", p1)
print("Perimeter of the triangle2:", p2)


# Output:
# Perimeter of the triangle1: 18
# Perimeter of the triangle2: 36Code language: Python (python)

Explanation:

  • Define a Class
    • Define a class named Triangle.
    • The class will store all three sides of the triangle.
  • Create a Constructor (__init__ method)
    • The constructor (__init__) initializes the three sides when an object is created.
    • The self keyword refers to the current instance of the class.
  • 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
  • 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
  • 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().

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

triangle_perimeter = lambda a, b, c: a + b + c
print("Perimeter of the triangle:", triangle_perimeter(5, 6, 7))

# Output:
# Perimeter of the triangle: 18Code language: Python (python)

Explanation

  • a, b, and c are the input parameters.
  • a + b + c is the calculation expression.
  • triangle_perimeter is 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.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com