The Pythagorean theorem states that in any right triangle, the square of the hypotenuse equals the sum of the squares of the other two sides, written as a² + b² = c². Here a and b are the two legs that meet at the 90° angle, and c is the hypotenuse, the longest side sitting opposite that right angle. In Python the cleanest way to compute the hypotenuse from two known legs is math.hypot(a, b), which returns √(a² + b²) with better numerical stability than a hand-written sqrt. To find a missing leg when you already know the hypotenuse and one leg, you rearrange the formula to b = math.sqrt(c**2 - a**2), checking first that the hypotenuse really is longer than the known leg. Both patterns fit in a single line of code, but for everyday measuring, drafting, and homework you usually just want the answer, and that's where the Pythagorean Theorem Calculator enters — it shows the missing side along with the area, perimeter, and both acute angles the moment you type two known lengths.

how to calculate pythagorean theorem in python
Calculate Pythagorean Theorem in Python: Step-by-Step

The Formula at a Glance

The relationship a² + b² = c² is the whole theorem, and it has held for more than two thousand years — Euclid recorded it as Proposition 47 of Book I of the Elements. It only applies to right triangles, those with exactly one 90° angle, and that single condition is what makes the rule so powerful: knowing any two of the three sides fixes the third.

If you know both legs, the hypotenuse is c = √(a² + b²). If you know the hypotenuse and one leg, the other leg is b = √(c² − a²). Because the hypotenuse is always the longest side, c must be greater than either leg, and any answer that comes out shorter than a known leg means the input describes an impossible triangle.

A quick worked check uses the canonical 3-4-5 triangle. With legs a = 3 and b = 4, the formula gives c = √(3² + 4²) = √(9 + 16) = √25 = 5. The numbers come out exactly, with no rounding, and any integer multiple — 6-8-10, 9-12-15 — works the same way. That single example is enough to confirm that a calculator or a script is behaving the way it should.

Calculate the Pythagorean Theorem in Python

Python gives you three reasonable ways to evaluate a² + b² = c², and the right one depends on whether you want a single number, an array of numbers, or a hand-rolled implementation that mirrors the formula on a chalkboard.

1. math.hypot for a single triangle

The standard-library function math.hypot is purpose-built for this exact problem. It accepts one or more coordinates and returns the Euclidean distance from the origin, which for two arguments collapses to √(a² + b²):

import matha, b = 3, 4c = math.hypot(a, b)print(c) # 5.0

Under the hood math.hypot scales its inputs to avoid the intermediate overflow and underflow that a naive math.sqrt(a*a + b*b) can suffer from when a or b is very large or very small. For typical homework-sized numbers the difference is invisible, but the hypot form is the recommended idiom and the one that ships in the official Python documentation.

2. The manual formula

If you want the code to read exactly like the math, write the squares out yourself:

import matha, b = 5, 12c_squared = a**2 + b**2c = math.sqrt(c_squared)print(c) # 13.0

This mirrors the textbook line c = √(a² + b²) and is the version most people write first when they are learning. The trade-off is that very large inputs can overflow during the squaring step and very small ones can lose precision; math.hypot sidesteps both.

3. NumPy for arrays of triangles

When you have many triangles to solve at once — say, every right triangle in a coordinate grid — NumPy handles the whole batch with one expression:

import numpy as nplegs = np.array([[3, 4], [5, 12], [8, 15]])hypotenuses = np.hypot(legs[:, 0], legs[:, 1])print(hypotenuses) # [ 5. 13. 17.]

np.hypot broadcasts element-wise and keeps the same numerical-stability guarantees as the scalar math version, which is why it shows up so often in game-development and data-science code that leans on the distance formula.

Find a Missing Leg in Python

Working backwards — known hypotenuse and one leg, looking for the other — is just as straightforward. Rearrange a² + b² = c² into b = √(c² − a²), square the hypotenuse, subtract the square of the known leg, and take the square root:

import mathc, a = 13, 5b = math.sqrt(c**2 - a**2)print(b) # 12.0

Two checks belong in any production code around this pattern. First, the hypotenuse must be strictly greater than the known leg; if it is not, the radicand goes negative and math.sqrt raises ValueError instead of returning a real number. Second, a tiny negative radicand caused by floating-point round-off on a valid triangle will fail the same way, so a tolerance such as b = math.sqrt(max(c**2 - a**2, 0.0)) keeps the script from crashing on clean inputs like 5-12-13.

If you need the angles as well, the inverse trigonometric functions finish the job. The angle opposite leg a is math.degrees(math.asin(a / c)), the angle opposite leg b is the same expression with b substituted, and the two should sum to 90° within floating-point error.

Use the Pythagorean Theorem Calculator

For everyday measuring, drafting, and homework the fastest path is a dedicated tool. The Pythagorean Theorem Calculator accepts any two known sides and returns everything that can be derived from them. Three steps cover it:

  1. Choose whether you want to find the hypotenuse from two legs or a missing leg from the hypotenuse and one leg.
  2. Type the two lengths you already know into the input boxes.
  3. Read the missing side, the area, the perimeter, and both acute angles as soon as the result updates.

Numbers are rounded to a readable number of decimal places while keeping enough precision to verify exact triples, and the working is shown as a formula line such as c = √(a² + b²) so you can follow each step rather than trusting a black box. Every result updates the moment you change an input, so you can experiment with different values and watch the triangle respond in real time. That makes it a quick right-triangle solver for textbook problems, drafting, and everyday measuring alike.

Pythagorean Triples Worth Recognising

Some side triples are so clean they are worth memorising, because they confirm instantly whether a calculator is behaving and because they show up constantly in construction and textbook problems. Any integer multiple of a triple is itself a valid right triangle.

TripleVerificationWhere it shows up
3-4-53² + 4² = 9 + 16 = 25 = 5²Simplest textbook example; builder's reference for a 90° corner
5-12-135² + 12² = 25 + 144 = 169 = 13²Roof pitches and stair layouts
8-15-178² + 15² = 64 + 225 = 289 = 17²Larger framing and surveying checks
7-24-257² + 24² = 49 + 576 = 625 = 25²Surveying and navigation problems
9-40-419² + 40² = 81 + 1600 = 1681 = 41²Larger construction and verification tasks

Because all of these reduce to the square of an integer, running any of them through a calculator is a free sanity check: a rounded-but-clean integer result means the tool is doing what it should.

When a² + b² = c² Doesn't Apply

The theorem is exact only for right triangles, those with exactly one 90° angle. The instant the triangle loses that property, a² + b² = c² is no longer the right equation. For any triangle with sides a, b, c and an angle C between sides a and b, the correct relationship is the Law of Cosines: c² = a² + b² − 2ab·cos(C). When C is exactly 90°, cos(C) is 0 and the cosine term vanishes, so the Law of Cosines collapses to the Pythagorean theorem — which is why a² + b² = c² is sometimes called a special case of the more general rule.

Two practical limits follow. The hypotenuse must always be the longest side, so any input that would produce a hypotenuse shorter than a leg describes no triangle at all. The Pythagorean Theorem Calculator checks that condition and flags impossible inputs rather than silently returning a negative radicand or an imaginary number.

Coding Pitfalls When Implementing Pythagoras in Python

Even with the math reduced to one line, a few recurring traps catch beginners and seasoned developers alike. Being aware of them is the difference between a script that runs once and one you can trust across many inputs.

  • Forgetting the square root. Solving for c² and stopping is the single most common error. The hypotenuse is the square root of a² + b², not a² + b² itself.
  • Integer division in legacy code. Modern Python 3 always promotes to float on /, but code ported from Python 2 or other languages can quietly truncate results. If the inputs are integers, square them as integers and convert before taking the root.
  • Negative radicands. math.sqrt of a negative number raises ValueError rather than returning a complex result. Validate that c > a (or c > b) before computing b = √(c² − a²).
  • Mixed units. The theorem requires consistent units. A leg measured in inches and a hypotenuse measured in centimetres will produce a nonsense result.
  • Wrong angle arguments. math.sin, math.cos, and math.tan all expect radians. If you have degrees, wrap them in math.radians() before calling, or use math.degrees() on the output if you want degrees back.

Most of these never appear when you let a tool handle the arithmetic. The Pythagorean Theorem Calculator keeps everything in one place, displays the substituted formula on screen, and updates the area, perimeter, and angles every time you change an input — so you can focus on the geometry rather than the bookkeeping. Real-world uses are everywhere: builders use the theorem to square up walls and check that corners are true right angles, surveyors turn north-south and east-west distances into a straight line, and the same rule is the reason a TV or monitor "size" can be read off its diagonal from width and height.

If you prefer working in a spreadsheet instead of a script, the same formula carries over to Excel with SUMSQ and SQRT, and the steps follow the same logic shown here.

Related reading: How to Figure Out the Pythagorean Theorem.