Python math function | hypot()

hypot() function is an inbuilt math function in Python that return the Euclidean norm,  \sqrt{(x*x + y*y)} .

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 : One has to import math module before using hypot() function.
 
Below is the demonstration of hypot() function :

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for hypot() function 
  
# Import the math module
import math
  
# Use of hypot fucntion
print("hypot(3, 4) : ", math.hypot(3, 4))
  
# Neglects the negative sign
print("hypot(-3, 4) : ", math.hypot(-3, 4))
  
print("hypot(6, 6) : ", math.hypot(6, 6))

chevron_right


Output :

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

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for error in hypot() function 
  
# import the math module
import math
  
# Use of hypot() function
print("hypot(3, 4, 6) : ",  math.hypot(3, 4, 6))

chevron_right


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 :
Given perpendicular and base of a right angle triangle find the hypotenuse.

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

Hence,

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

Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for finding Hypotenuse
# in hypot() function 
  
# import the math module
from math import hypot
  
# Perpendicular and base
p = 3
b = 4
  
# Calculates the hypotenuse
print("Hypotenuse is:", hypot(p, b))

chevron_right


Output :

Hypotenuse is: 5.0


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.