.hypot()
Anonymous contributor
Published Nov 16, 2024
Contribute to Docs
In Numpy, the .hypot()
function returns the hypotenuse of a right triangle, given the legs.
Syntax
numpy.hypot(x1, x2, out=None, where=True)
x1
,x2
: These are the legs of the triangle(s). If the shapes ofx1
andx2
are not identical, they must be broadcastable to a common shape.out
(Optional): This parameter specifies an array where the result will be stored. The shape ofout
must match the shape of the output. If not provided, a new array will be allocated for the result.where
(Optional): An optional condition that can be applied element-wise on the input arrays. The result will only be computed where the condition isTrue
. If the condition isFalse
for an element, that element’s result will be ignored.
Example 1
The below example calculates the hypotenuse of a right triangle:
import numpy as npprint("If the first leg is 3 and the second is 4, the hypotenuse is ", np.hypot(3, 4))
The code above generates the following output:
If the first leg is 3 and the second is 4, the hypotenuse is 5.0
Example 2
The following example calculates the hypotenuses of three right triangles:
import numpy as np# Creating the sides of three trianglestriangle_leg1 = [3,5,6] # First leg of each triangletriangle_leg2 = [4,12,8] # Second leg of each triangle# Computing the hypotenuseshypotenuses = np.hypot(array1, array2)print("Calculated hypotenuses:", hypotenuses)print("\nThe hypotenuses are {}, {} and {}, respectively.".format(hypotenuses[0], hypotenuses[1], hypotenuses[2]))
The code above generates the following output:
Calculated hypotenuses: [ 5. 13. 10.]The hypotenuses are 5.0, 13.0 and 10.0, respectively.
Codebyte Example
In this codebyte, .hypot()
computes the hypotenuses for two triangles. The first triangle has legs of 2 and 3, and the second triangle has legs of 8 and 15.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours