Python Examples

Python Program to Calculate the Area of a Triangle

Python programming language is versatile and easy to use. It has many libraries with various predefined functions ready to use just by importing the library to the code. Tasks like finding area, volume, density can be easily done using these libraries. For example, Let’s take the python triangle program that calculates the area of a triangle.

We can calculate the area of a triangle using Heron’s formula.

python triangle

           Source : Wikipedia

where ‘s’ is the semi perimeter of the triangle which is calculated by the given formula:

s= 1/2*(a+b+c)

and a, b and c are the lengths of three sides of the triangle.

 Source Code

                    

#area of triangle  

p = float(input('Enter the length of first side: '))  
q = float(input('Enter the length of second side: '))  
r = float(input('Enter the length of final side: '))  

# calculate the semi-perimeter  
s = (p + q + r)/2

# calculate the area  
area_tri = (s*(s-p)(s-q)(s-r)) ** 0.5  
print('The area of the triangle is %0.2f' %area_tri)

 

Output:

                    

Enter the length of the first side: 7
Enter the length of the second side: 5
Enter the length of the final side: 8
The area of the triangle is 17.32

Frequently Asked Questions

How do you make a triangle in python?

Python provides a simple pen and a virtual canvas to draw shapes patterns drawings. All the functions of this pen and virtual canvas are contained in the turtle module of python.

We can draw a triangle using the turtle module. For which we have to import the turtle module by using the import turtle command

Then we can use the required functions from the turtle module to draw a triangle as we wish.

For example:
                    

import turtle

canvas = turtle.Turtle()

canvas.forward(150) # draw base

canvas.left(120)

canvas.forward(150)

canvas.left(120)

canvas.forward(150)

turtle.done()

Output:
python triangle

                                               Source: Self

How do you find the Area of the Triangle in Python?

We can calculate the area of a triangle using Heron’s formula.

where s is the semi perimeter of the triangle that is calculated by the given formula:

s= (a+b+c)/2

and a, b and c are the lengths of three sides of the triangle.

For example:
                    

# area of a triangle using python

a = 5

b = 6

c = 7

# calculate the semi-perimeter

s = (a+b+c) /2

# calculate the area

area_tri = (s*(s-a)(s-b)(s-c)) ** 0.5

print('The area of triangle is : %0.2f' %area_tri)

Output:

The area of the triangle is 14.70

What does end =’do Python?

The end parameter in the print function is used to add any string. At the end of the output of the print statement in python.

By default, the print function ends with a newline.

Passing the whitespace to the end parameter (end=‘ ‘) indicates that the end character has to be identified by whitespace and not a newline.

For example:

print(“Toppr”, end=’ ‘)

print(“is awesome”)

Output:

Toppr is awesome

For example:

print(“toppr”, end=’ says ‘)

print(“you are awesome”)

Output:

toppr says you are awesome

What is the Pascal Triangle in Python?

Pascal’s Triangle is a number pattern in the shape of a triangle.

python triangle

Each number in the triangle is the sum of the two numbers above it. There are many ways to implement the pascals triangle in python. One of which is by using the nCr formula.

n!=n!/(n-r)!r!

python triangle

For example:
                    

# Python program to print pascal's triangle

from math import factorial

# input value of n

n = 5

#uncomment this line to take input from user

#n=int(input("Enter the value of n: "))

for i in range(n):

    for j in range(n-i+1):

        # for spacing

        print(end=" ")

    for j in range(i+1):

        # nCr formula      

print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

    # for new line

print()

Output:

      

                    

      1
 
     1 1

   1 2 1

  1 3 3 1

 1 4 6 4 1

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.