• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Pythagorean Theorem in Python – Calculating Length of Triangle Sides

Pythagorean Theorem in Python – Calculating Length of Triangle Sides

February 10, 2022 Leave a Comment

In Python, we can calculate the lengths of the sides of a triangle easily using the Pythagorean Theorem.

def pythagoreanTheorem(toSolve,side1,side2):
    if toSolve == "Hypot":
        length = (side1 ** 2 + side2 ** 2) ** (1/2)        
    else: 
        if side2 < side1:
            temp = side2
            side2 = side1
            side1 = temp
        length = (side2 ** 2 - side1 ** 2) ** (1/2)
    return length

print(pythagoreanTheorem("Hypot",3,4))
print(pythagoreanTheorem("Hypot",2.5,9.1))
print(pythagoreanTheorem("Side",4,5))

#Output:
5.0
9.43716058992322
3.0

One of the most famous and well-known math equations is the Pythagorean Theorem. The Pythagorean Theorem states that:

In a right-angled triangle, the square of the hypotenuse side is equal to the sum of squares of the other two sides.

In equation form, we have for a right-angled triangle, that the hypotenuse length is equal to the square of the length of side one and the square of the length of side two.

Using Python, we can easily implement the Pythagorean Theorem.

To create a function which will apply the Pythagorean Theorem in Python, we just need to know which side to solve for.

If we are solving for the length of the hypotenuse, then the formula in Python is the square root of the sum of squared side lengths:

hypotenuse_length = (side1_length ** 2 + side2_length ** 2) ** (1/2)

If we are solving for the length of one of the sides of the triangle, then the formula in Python is:

side1_length = (hypotenuse_length ** 2 - side2_length ** 2) ** (1/2)

Below is a function which will has three arguments which will allow us to use the Pythagorean Theorem in Python. The three arguments are the side we want to solve for, and two lengths.

def pythagoreanTheorem(toSolve,side1,side2):
    if toSolve == "Hypot":
        length = (side1 ** 2 + side2 ** 2) ** (1/2)        
    else: 
        if side2 < side1:  #order matters here, so we can make the hypotenuse the bigger length
            temp = side2
            side2 = side1
            side1 = temp
        length = (side2 ** 2 - side1 ** 2) ** (1/2)
    return length

print(pythagoreanTheorem("Hypot",3,4))
print(pythagoreanTheorem("Hypot",2.5,9.1))
print(pythagoreanTheorem("Side",4,5))

#Output:
5.0
9.43716058992322
3.0

Hopefully this article has been helpful for you to learn how to use the Pythagorean Theorem in Python to get the lengths of the sides of a triangle.

Other Articles You'll Also Like:

  • 1.  Using Lambda Expression with min() in Python
  • 2.  Remove Empty Lists from List in Python
  • 3.  Subtract Seconds from Datetime Variable Using Python timedelta() Function
  • 4.  Using Python to Get Day of Week
  • 5.  Using Python to Sort Two Lists Together
  • 6.  Reverse a List in Python Without Reverse Function
  • 7.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 8.  How to Check if a Letter is in a String Using Python
  • 9.  Count Primes Python – How to Count Number of Primes in a List
  • 10.  How to Multiply Two Numbers in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy