# Given the “legs” of a right triangle, return its hypotenuse in Python

PythonNumpyServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To get the hypotenuse, use the numpy.hypot() method in Python Numpy. The method returns the hypotenuse of the triangle(s). This is a scalar if both x1 and x2 are scalars. This method is equivalent to sqrt(x1**2 + x2**2), element-wise. If x1 or x2 is scalar_like, it is broadcast for use with each element of the other argument. The parameters are the leg of the triangle(s). If x1.shape != x2.shape, they must be broadcastable to a common shape.

## Steps

At first, import the required library −

import numpy as np

Creating an array with integer elements −

arr = np.ones((3, 3), dtype=int)

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)


Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

Get the hypotenuse −

print("\nHypotenuse..\n",np.hypot((3*arr), (4*arr)))

## Example

import numpy as np

# To get the hypotenuse, use the numpy.hypot() method in Python Numpy.
# The method returns the hypotenuse of the triangle(s). This is a scalar if both x1 and x2 are scalars.
# This method is equivalent to sqrt(x1**2 + x2**2), element-wise. If x1 or x2 is scalar_like, it is broadcast for use with each element of the other argument.
# The parameters are the leg of the triangle(s). If x1.shape != x2.shape, they must be broadcastable to a common shape.

# Creating an array with integer elements
arr = np.ones((3, 3), dtype=int)

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# Get the hypotenuse
print("\nHypotenuse..\n",np.hypot((3*arr), (4*arr)))

## Output

Array...
[[1 1 1]
[1 1 1]
[1 1 1]]

Our Array type...
int64

Our Array Dimensions...
2

Number of elements...
9

Hypotenuse..
[[5. 5. 5.]
[5. 5. 5.]
[5. 5. 5.]]