Python hypot

Python 的 hypot 函数返回指定参数平方和的平方根。在本节中,我们将通过一个示例讨论如何使用 hypot 函数。此编程语言中 math hypot 的语法如下所示。

math.hypot(Value1, Value2, ...ValueN);
  • 如果参数值为正整数和负整数,它会返回输出。
  • 如果它不是数字,它会返回 TypeError。

Python hypot 函数示例

hypot 函数找到给定数字平方和的平方根。在此示例中,我们将找到不同数据类型的平方根的平方和,并显示输出。

首先,我们将其直接应用于正整数和负整数。以下 Python 语句可找到相应值的平方和的平方根。

接下来,我们将 hypot 函数用于 Tuple 和 List 项。从下面的图片可以看出,它在这两类数据上都能完美运行。接下来,我们将 math 函数应用于多个值。

在最后一个语句中,我们尝试将其应用于字符串值,但返回了 TypeError。

import math

Tup = (1, 2, 3, -4 , 5)
Lis = [-1, 2, -3.5, -4 , 7.5]

print('HYPOT value of Positive Number = %.2f' %math.hypot(2, 3))
print('HYPOT value of Negative Number = %.2f' %math.hypot(2, -4))

print('HYPOT value of Tuple Item = %.2f' %math.hypot(Tup[3], Tup[2]))
print('HYPOT value of List Item = %.2f' %math.hypot(Lis[2], Lis[4]))

print('HYPOT value of Multiple Number = %.2f' %math.hypot(2 + 7 - 4, 9 - 5))

print('HYPOT value of String Number = %.2f', math.hypot('Hello', 'Python'))
math hypot Function