How do you find the triangle in python?

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()

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic: Exercise-30 with Solution

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.

  • 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.

How do you find the triangle in python?

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:

How do you find the triangle in python?

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


How do you program a triangle in Python?

Programs to print triangles using *, numbers and characters In the first loop, we iterate from i = 0 to i = rows . The second loop runs from j = 0 to i + 1. In each iteration of this loop, we print i + 1 number of * without a new line. Here, the row number gives the number of * required to be printed on that row.

How do you find the type of triangle in Python?

Python Source Code: Triangle Type # Validity of Triangle given sides # Function definition to check validity def is_valid_triangle(a,b,c): if a+b>=c and b+c>=a and c+a>=b: return True else: return False # Function definition for type def type_of_triangle(a,b,c): if a==b and b==c: print('Triangle is Equilateral.

Is triangle a function in Python?

Python | sympy Triangle() method The function Triangle() takes the given points as vertices of a triangle and computes the area of triangle with the help of area. Syntax: Triangle(x, y, z). area Parameters: where x, y, z are coordinates. Return: Area of triangle.

How do you make a triangle in code?

triangle(x1, y1, x2, y2, x3, y3);