If a, b and c are three sides of a triangle. Then,
s = [a+b+c]/2 area = √[s[s-a]*[s-b]*[s-c]]
Source Code
# Python Program to find the area of triangle
a = 5
b = 6
c = 7
# Uncomment below to take inputs from the user
# a = float[input['Enter first side: ']]
# b = float[input['Enter second side: ']]
# c = float[input['Enter third side: ']]
# calculate the semi-perimeter
s = [a + b + c] / 2
# calculate the area
area = [s*[s-a]*[s-b]*[s-c]] ** 0.5
print['The area of the triangle is %0.2f' %area]
Output
The area of the triangle is 14.70
In this program, area of the triangle is calculated when three sides are given using Heron's formula.
If you need to calculate area of a triangle depending upon the input from the user, input[] function can be used.
Python has a simple pen drawing library called turtle. Using simple movement commands, we can draw shapes using the python turtle library. When teaching python to children, turtle is a good library to introduce to get children excited about the language and its features.
The basic actions used in the following examples are,
- Draw a line with pen - forward[] command
- Move without drawing - penup[], pendown[] commands
- Turn the pen to an angle - left[], right[] commands
The following python program draws a simple equilateral triangle,
import turtle board = turtle.Turtle[] board.forward[100] # draw base board.left[120] board.forward[100] board.left[120] board.forward[100] turtle.done[]
The following python program draws a right angled triangle,
import turtle board = turtle.Turtle[] board.forward[100] # draw base board.left[90] board.forward[100] board.left[135] board.forward[142] turtle.done[]
The following python program draws a star shape by drawing two identical isosceles triangles,
import turtle board = turtle.Turtle[] # first triangle for star board.forward[100] # draw base board.left[120] board.forward[100] board.left[120] board.forward[100] board.penup[] board.right[150] board.forward[50] # second triangle for star board.pendown[] board.right[90] board.forward[100] board.right[120] board.forward[100] board.right[120] board.forward[100] turtle.done[]
Write a Python program that will accept the base and height of a triangle and compute the area. Python: Area of a triangle A triangle is a polygon with three edges and three vertices. It is one of the basic shapes in
geometry. A triangle with vertices A, B, and C is denoted triangle ABC.Python Basic: Exercise-30 with Solution
- Vertex of a triangle: The point at which two sides of a triangle meet.
- Altitude of a triangle: The perpendicular segment from a vertex of a triangle to the line containing the opposite side.
- Base of a triangle: The side of a triangle to which an altitude is drawn.
- Height of a triangle: The length of an altitude.
Sample Solution:-
Python Code:
b = int[input["Input the base : "]]
h = int[input["Input the height : "]]
area = b*h/2
print["area = ", area]
Sample Output:
Input the base : 20 Input the height : 40 area = 400.0
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to print out a set containing all the colors from a list which are not present in
another list.
Next: Write a Python program to compute the greatest common divisor [GCD] of two positive integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Unzipping:
name = 'abcdef' suffix = [1,2,3,4,5,6] result = zip[name, suffix] --> returns [a,1],[b,2],[c,3],[d,4],[e,5],[f,6] unzipped = zip[*result]
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation