Python Practice › Basic Programs › Program 9
Pythagoras theorem
Write a Python program to find the hypotenuse of a right-angled triangle using the Pythagoras theorem: a² + b² = c².
Input
a = 3, b = 4
Output
Hypotenuse c = 5.0 Check: True
Approach
- Compute a² + b².
- Take the square root to get c.
Solution
import math a = 3b = 4c = math.sqrt(a ** 2 + b ** 2)print("Hypotenuse c =", c)print("Check:", a ** 2 + b ** 2 == c ** 2)Output
Hypotenuse c = 5.0 Check: True
Line by line
Line 1
math provides sqrt().
Line 5
The hypotenuse is the square root of the sum of the squares of the other two sides.
Line 7
Squaring c again confirms the theorem holds.
Tip
math.hypot(a, b) computes the same value in one call.
Learn the concept
New to this topic? Read the lesson, then come back.