Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Find Area of Triangle (Different Ways With Examples)
Triangles, with their simplicity and elegance, have been crucial part of the lives of mathematicians, artists, and architects for centuries. They are the building blocks of many geometric structures and hold a prominent place in various fields, from mathematics and physics to computer graphics and engineering.
The area of a triangle, a fundamental property, unveils the spatial significance within its three sides and angles. Understanding how to calculate area of triangle in Python not only strengthens our grasp of geometric principles but also equips us with a versatile tool to solve real-world problems.
Here, we will explore the Python program to find area of triangle with different techniques and examples. You will not only get an understanding of the concept of calculating the area of a triangle in Python but also possess a powerful program that can handle any triangle you throw at it.
So, let’s get started!
Calculate Area of Triangle in Python (Program)
Code
# Python program to calculate the area of a triangle with base and height
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("The area of the triangle is:", area)
Output
Enter the base length of the triangle: 23
Enter the height of the triangle: 10
The area of the triangle is: 115.0
Explanation
-
We take input from the user for the base length and height of the triangle. We use the float() function to convert the user input into floating-point numbers to handle decimal values.
-
Then, we calculate the area of the triangle using the formula: area = 0.5 * base * height. Here, we multiply the base length by the height and divide the result by 2.
-
Finally, we display the calculated area of the triangle using the print() function.
-
By running this program, you can input the base length and height of a triangle, and it will calculate and show you the area.
Python Program to Find Area of Triangle Using class
Code
# Write a program to find area of triangle with class
class Triangle:
def __init__(self, base, height):
self.base = base
self.height = height
def calculate_area(self):
return 0.5 * self.base * self.height
# Taking input from the user
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Creating an instance of the Triangle class
triangle = Triangle(base, height)
# Calculating and printing the area of the triangle
area = triangle.calculate_area()
print("The area of the triangle is:", area)
Output
Enter the base length of the triangle: 5
Enter the height of the triangle: 9
The area of the triangle is: 22.5
Explanation
-
Here, we define a class called Triangle. The __init__ method is used as the constructor, which initializes the base and height attributes of the triangle object.
-
We also define a method called calculate_area within the class, which calculates the area of the triangle using the formula: area = 0.5 * base * height. It accesses the base and height attributes of the object using the self keyword.
-
We then take input from the user for the base length and height of the triangle.
-
Next, we create an instance of the Triangle class, passing in the user-provided base and height values as arguments to the constructor.
-
Finally, we call the calculate_area method on the triangle object to calculate the area, and we print the result.
By running this program, you can take user input for the base length and height, and it will use the class to calculate and show the area of the triangle in Python.
Using a class provides a structured and object-oriented approach to solve the problem, encapsulating the properties and behavior of a triangle within a single entity.
Area of Triangle Program in Python Using inheritance
Code
# Find Area of Triangle With Inheritance
class Shape:
def __init__(self):
pass
def calculate_area(self):
pass
class Triangle(Shape):
def __init__(self, base, height):
super().__init__()
self.base = base
self.height = height
def calculate_area(self):
return 0.5 * self.base * self.height
# Taking input from the user
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Creating an instance of the Triangle class
triangle = Triangle(base, height)
# Calculating and printing the area of the triangle
area = triangle.calculate_area()
print("The area of the triangle is:", area)
Output
Enter the base length of the triangle: 6
Enter the height of the triangle: 4
The area of the triangle is: 12.0
Explanation
-
We define a base class called Shape that has an __init__ method and a calculate_area method. This method is defined as an abstract method, which means it has no implementation in the base class. The purpose of the Shape class is to serve as a blueprint for other shape classes to inherit from.
-
Next, we define a derived class called Triangle that inherits from the Shape class using the super() function. The Triangle class has its own constructor (__init__ method) that takes the base and height of the triangle as parameters and initializes the corresponding attributes.
-
The Triangle class also overrides the calculate_area method from the Shape class with its own implementation. It calculates the area of the triangle using the Python formula: area = 0.5 * base * height.
-
We then take input from the user for the base length and height of the triangle.
-
Next, we create an instance of the Triangle class, passing in the user-provided base and height values to the constructor.
-
Finally, we call the calculate_area method on the triangle object, and we print the result.
Using inheritance allows us to create specialized classes (such as Triangle) that inherit common attributes and behaviors from a base class (Shape). This promotes code reusability and enhances the overall structure and organization of the program.
Area of Triangle Using Lambda Function in Python
Code
# Find Area of Triangle With Python Lambda
calculate_area = lambda base, height: 0.5 * base * height
# Taking input from the user
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculating the area of the triangle using the lambda function
area = calculate_area(base, height)
print("The area of the triangle is:", area)
Output
Enter the base length of the triangle: 10
Enter the height of the triangle: 13
The area of the triangle is: 65.0
Explanation
-
We define a lambda function called calculate_area that takes the base and height of the triangle as input parameters. The lambda function calculates the area of the triangle using the formula: area = 0.5 * base * height.
-
We then take input from the user for the base length and height of the triangle.
-
Next, we invoke the lambda function calculate_area and pass in the user-provided base and height values as arguments.
-
Finally, we print the calculated area of the triangle.
Lambda functions provide a concise and compact way to define small, anonymous functions. In this case, it allows us to define the area calculation as a one-liner without explicitly defining a separate function.
Calculate Area of Equilateral Triangle
Code
# Python program to find area of an equilateral triangle
import math
side = float(input("Enter the side length of the equilateral triangle: "))
# Calculate the area of the equilateral triangle
area = (math.sqrt(3) / 4) * side ** 2
print("The area of the equilateral triangle is:", area)
Output
Enter the side length of the equilateral triangle: 7
The area of the equilateral triangle is: 21.217622392718745
Explanation
-
We take input from the user for the side length of the equilateral triangle.
-
Next, we calculate the area of the equilateral triangle using the formula: area = (sqrt(3) / 4) * side^2. Here, we import the math module to access the sqrt function for calculating the square root.
-
Finally, we print the calculated area of the equilateral triangle.
Calculate Area of Right Angle Triangle
Code
# Find area of right angle triangle in Python
base = float(input("Enter the base length of the right-angled triangle: "))
height = float(input("Enter the height of the right-angled triangle: "))
# Calculate the area of the right-angled triangle
area = 0.5 * base * height
print("The area of the right-angled triangle is:", area)
Output
Enter the base length of the right-angled triangle: 5
Enter the height of the right-angled triangle: 4
The area of the right-angled triangle is: 10.0
Explanation
-
We take input from the user for the base length and height of the right-angled triangle.
-
Next, we calculate the area of the right-angled triangle using the formula: area = 0.5 * base * height.
-
Finally, we print the calculated area of the right-angled triangle.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program