带有Python示例的方法

时间:2025-02-11 12:37:32

Python ()方法 (Python () method)

() method is a library method of math module, it is used to find GCD (Greatest Common Divisor) of given numbers, it accepts two integer numbers and returns their greatest common divisor (the largest positive integer that divides both of the numbers).

()方法数学模块的库方法,用于查找给定数字的GCD(最大公约数),它接受两个整数并返回其最大公约数(将两个整数相除的最大正整数)。数字)。

Note:

注意:

  • () is available in newer version Python 3.5

    ()在更高版本的Python 3.5中可用

  • If both numbers are 0 – it returns 0.

    如果两个数字均为0 –则返回0。

  • If one of the numbers is 0 – it returns another non-zero number.

    如果其中一个数字为0,则返回另一个非零数字。

  • If anything else like float or string is provided, the method returns a "TypeError",

    如果提供了其他类似float或string的方法,则该方法将返回“ TypeError”

    In the case of float parameter, it returns

    对于float参数,它返回

    "TypeError: 'float' object cannot be interpreted as an integer"

    “ TypeError:'float'对象无法解释为整数”

    In the case of a string parameter, it returns

    如果是字符串参数,则返回

    "TypeError: 'str' object cannot be interpreted as an integer"

    “ TypeError:'str'对象无法解释为整数”

Syntax of () method:

()方法的语法:

    (a, b)

Parameter(s): a, b – two integer numbers whose greatest common divisor has to be calculated.

参数: a,b –两个整数,必须计算其最大公约数。

Return value: int – it returns an integer value, which is the GCD (Greatest Common Divisor) . the largest integer number that divided both numbers.

返回值: int –它返回一个整数值,即GCD(最大公约数),即将两个数字相除的最大整数。

Example:

例:

    Input:
    a = 10
    b = 15

    # function call
    print((a, b))

    Output:
    15

Python代码演示()方法的示例 (Python code to demonstrate example of () method)

# python code to demonstrate example of 
# () method

# importing math module
import math

# numbers
a = 10
b = 15
print("gcd is = ", (a,b))

a = 10
b = 0
print("gcd is = ", (a,b))

a = 0
b = 0
print("gcd is = ", (a,b))

Output

输出量

gcd is =  5
gcd is =  10
gcd is =  0


翻译自: /python/