Pythagorean Theorem with Python

Pythagorean Theorem in Python
The Pythagorean theorem states that given a right triangle, the hypotenuse squared equals the sum of the sides squared.

To calculate the length of a hypotenuse of a right triangle using Pythagorean theorem:

  1. Sum up the squares of the two sides a and b.
  2. Take the square root of the sum to get the length of the hypotenuse c.

Here is an example.

Given sides a = 3 and b = 4 in a right triangle, what is the length of the hypotenuse?

The solution:

A pythagorean theorem calculator on the internet

That’s it for the theory.

Next, let’s write a small program in Python to calculate the hypotenuse given sides a and b using the Pythagorean theorem.

Here is the code:

import math

a = 3
b = 4

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

Output:

5.0

Next, let’s take a look at how to implement a general Pythagorean theorem calculator in Python.

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 illustrated with a right triangle

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 the user for which side to solve.
  • Solve the Pythagorean theorem for the given side.

Here is how it looks in the 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 users decide what side to calculate.

Thanks for reading.

Happy coding!

Read Also

Python Math Module