How do you write pythagorean theorem in python?

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

Python Math: Exercise-68 with Solution

Write a Python program to create a Pythagorean theorem calculator.

Note : In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem, is a fundamental relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.

Sample Solution:-

Python Code:

from math import sqrt

print('Pythagorean theorem calculator! Calculate your triangle sides.')
print('Assume the sides are a, b, c and c is the hypotenuse (the side opposite the right angle')
formula = input('Which side (a, b, c) do you wish to calculate? side> ')

if formula == 'c':
	side_a = int(input('Input the length of side a: '))
	side_b = int(input('Input the length of side b: '))

	side_c = sqrt(side_a * side_a + side_b * side_b)
	
	print('The length of side c is: ' )
	print(side_c)

elif formula == 'a':
    side_b = int(input('Input the length of side b: '))
    side_c = int(input('Input the length of side c: '))
    
    side_a = sqrt((side_c * side_c) - (side_b * side_b))
    
    print('The length of side a is' )
    print(side_a)

elif formula == 'b':
    side_a = int(input('Input the length of side a: '))
    side_b = int(input('Input the length of side c: '))
        
    side_c = sqrt(side_c * side_c - side_a * side_a)
    
    print('The length of side b is')
    print(side_c)

else:
	print('Please select a side between a, b, c')
	

Sample Output:

Pythagorean theorem calculator! Calculate your triangle sides.                                                
Assume the sides are a, b, c and c is the hypotenuse (the side opposite the right angle                       
Which side (a, b, c) do you wish to calculate? side>a                                                         
Input the length of side b:10                                                                                 
Input the length of side c:15                                                                                 
The length of side a is                                                                                       
11.180339887498949 

Flowchart:

How do you write pythagorean theorem 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 create a dot string.
Next: Write a Python function to round up a number to specified digits.

Python: Tips of the Day

Combining Lists Using Zip:

  • Takes multiple collections and returns a new collection.
  • The new collection contains items where each item contains one element from each input collection.
  • It allows us to transverse multiple collections at the same time.
name = 'abcdef'
suffix = [1,2,3,4,5,6]
zip(name, suffix)
--> returns (a,1),(b,2),(c,3),(d,4),(e,5),(f,6)

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    hypot() function is an inbuilt math function in Python that return the Euclidean norm,

    How do you write pythagorean theorem in python?
    .

    Syntax :

    hypot(x, y) 

    Parameters :

    x and y are numerical values 

    Returns :

    Returns a float value having Euclidean norm, sqrt(x*x + y*y). 

    Error :

    When more then two arguments are 
    passed, it returns a TypeError.

    Note : One has to import math module before using hypot() function.
     
    Below is the demonstration of hypot() function :

    Code #1 :

    import math

    print("hypot(3, 4) : ", math.hypot(3, 4))

    print("hypot(-3, 4) : ", math.hypot(-3, 4))

    print("hypot(6, 6) : ", math.hypot(6, 6))

    Output :

    hypot(3, 4) :  5.0
    hypot(-3, 4) :  5.0
    hypot(6, 6) :  8.48528137423857
    

     
    Code #2 :

    import math

    print("hypot(3, 4, 6) : ",  math.hypot(3, 4, 6))

    Output :

    Traceback (most recent call last):
      File "/home/d8c8612ee97dd2c763e2836de644fac1.py", line 7, in 
        print("hypot(3, 4, 6) : ",  math.hypot(3, 4, 6))
    TypeError: hypot expected 2 arguments, got 3
    

     
    Practical Application :
    Given perpendicular and base of a right angle triangle find the hypotenuse.

    Using Pythagorean theorem which states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.

    Hence,

     Hypotenuse = sqrt(p^2 + b^2) 

    Code #3 :

    from math import hypot

    p = 3

    b = 4

    print("Hypotenuse is:", hypot(p, b))

    Output :

    Hypotenuse is: 5.0
    

    How do you find the hypotenuse in Python?

    hypot() method returns the Euclidean norm. The Euclidian norm is the distance from the origin to the coordinates given. Prior Python 3.8, this method was used only to find the hypotenuse of a right-angled triangle: sqrt(x*x + y*y). From Python 3.8, this method is used to calculate the Euclidean norm as well.

    How do you print Pythagorean triples in Python?

    Python Program to Find All Pythagorean Triplets in the Range.
    Take in the upper limit and store it in a variable..
    Using a while loop and for loop, compute the Pythagorean triplets using the formula..
    If the value of the c is greater than the upper limit or if any of the numbers is equal to 0, break from the loop..

    How do you find the right triangle in Python?

    Python: Check whether three given lengths of three sides form a right triangle.
    Input: ... .
    Pictorial Presentation:.
    Sample Solution:.
    Python Code: print("Input three integers(sides of a triangle)") int_num = list(map(int,input().split())) x,y,z = sorted(int_num) if x**2+y**2==z**2: print('Yes') else: print('No') ... .
    Flowchart:.