How to Calculate Pythagorean Theorem in Python

Pythagorean Theorem in Python

To calculate hypothenuse of a right triangle with Pythagorean theorem in Python:

  1. Sum up the squares of the two sides a and b according to the Pythagorean theorem.
  2. Take the square root of the sum to get the length of the hypotenuse c.
Pythagorean Theorem in Python
The Pythagorean theorem says the hypotenuse squared equals the sum of the sides squared.

For example:

import math

a = 3
b = 4

c = math.sqrt(a ** 2 + b ** 2)

Output:

5.0

How to Build a Pythagorean Theorem Calculator in Python

Let’s build a simple calculator that asks users for side lengths a and b in a right triangle. The program then calculates the length of the hypotenuse c.

Pythagorean Theorem in Python

To implement this calculator, you need to:

  • Ask the user for side lengths a and b.
  • Calculate the hypotenuse c using the Pythagorean theorem.
  • Print the result into the console.

Here is how it looks in the code:

import math

a = float(input("Give side a: "))
b = float(input("Give side b: "))

c = math.sqrt(a ** 2 + b ** 2)

print(f"The length of the hypotenuse c is {c}")

Example run:

Give side a: 3
Give side b: 4

The length of the hypotenuse c is 5.0

Advanced Pythagorean Theorem Calculator

The previous program assumes you always want to know the length of the hypotenuse c.

What if you want to find the length of one of the sides given the hypotenuse and another side?

In this case, you need to extend the implementation of the program.

Before you do that, you need to know how to write the Pythagorean theorem given the hypotenuse and one of the sides.

Here is an illustration:

Pythagorean theorem - find out the lengths of the sides.

To make the program work, you need to:

  • Ask user for which side to solve.
  • Solve the Pythagorean theorem for the given side.

Here is how it looks in code:

import math

side = input("Give the side you want to solve for (a, b, or c): ")

if side == "a":
    b = float(input("Give side b: "))
    c = float(input("Give hypotenuse c: "))
    a = math.sqrt(c ** 2 - b ** 2)
    print(f"The length of the side a is {a}")
    
elif side == "b":
    a = float(input("Give side a: "))
    c = float(input("Give hypotenuse c: "))
    b = math.sqrt(c ** 2 - a ** 2)
    print(f"The length of the side b is {b}")

elif side == "c":
    a = float(input("Give side a: "))
    b = float(input("Give side b: "))
    c = math.sqrt(a ** 2 + b ** 2)
    print(f"The length of the hypotenuse c is {c}")
else:
    print("Invalid input")

And here is the result of an example run:

Give the side you want to solve for (a, b, or c): b
Give side a: 4
Give hypotenuse c: 5
The length of the side b is 3.0

Conclusion

Today you learned how to create a Pythagorean theorem calculator in Python.

  • A simple version calculates the length of the hypotenuse c given sides a and b.
  • The advanced version can let user decide what side to calculate.

Thanks for reading.

Happy coding!

Further Reading

Python Advanced Features

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.