Python math function | hypot ()

Python Methods and Functions

Syntax :

 hypot (x, y) 

Parameters :

 x and y are numerical values ​​

Returns :

 Returns a float value having Euclidean norm,  sqrt (x * x + y * y).  

Error:

 When more then two arguments are passed, it returns a  TypeError . 

Note. Before using the hypot () function, you must import the math module.

Below is a demo of the hypot () function :

Code # 1:

# Python3 program for hypot () function

 
# Importing a math module

import math

  
# Using the hypothesis function

print ( "hypot (3, 4):" , math.hypot ( 3 , 4 ) )

 
# Ignore the negative sign

print ( "hypot (-3, 4):" , math.hypot ( - 3 , 4 ))

 

print ( "hypot (6, 6 ): " , math.hypot ( 6 , 6 ))

Output:

 hypot (3, 4): 5.0 hypot (-3, 4): 5.0 hypot (6, 6): 8.48528137423857 

Code # 2:

# Python3 program for hypot () error

 
# import math module

import math

 
# Using the hypot () function

print ( "hypot (3, 4, 6):" , math.hypot ( 3 , 4 , 6 ) )

Output:

 Traceback (most recent call last): File "/home/d8c8612ee97dd2c763e2836de644fac1.py", line 7, in print ("hypot (3, 4, 6):", math.hypot (3, 4, 6 )) TypeError: hypot expected 2 arguments, got 3 

Practical application:
Find the hypotenuse for the given perpendicular and base of the right triangle.

Using the Pythagorean theorem, which states that the square of the hypotenuse (the side opposite to the right angle) is equal to the sum of the squares of the other two sides.

Therefore,

  Hypotenuse  = sqrt (p ^ 2 + b ^ 2) 

Code # 3:

# Python3 program for finding hypotenuse
# in the hypot () function

 
# import math module

from math import hypot

 
# Perpendicular and base

p = 3

b = 4

 
# Calculates hypotenuse

print ( "Hypotenuse is:" , hypot (p, b ))

Output:

 Hypotenuse is: 5.0