Python Glossary Русский

all Python 3until 3.9Numbers & mathfunction

fractions.gcd()

removed

Greatest common divisor from the fractions module. Removed in Python 3.9; use math.gcd() instead.

Syntax

fractions.gcd(a, b)  # до 3.9

The function lived in fractions because it was needed to reduce fractions. Python 3.5 added math.gcd(), faster and in the right module, so fractions.gcd() was deprecated and then removed in 3.9. How to rewrite: from fractions import gcd becomes from math import gcd. One difference: fractions.gcd() took the sign of its second argument, while math.gcd() is never negative. Since 3.9 math.gcd() accepts any number of arguments, and math.lcm() gives the least common multiple.

Examples

# Python 3.9+
import fractions
try:
    fractions.gcd(12, 18)
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.9+
import math
print(math.gcd(12, 18))  # → 6
print(math.gcd(4, -6))  # → 2
print(math.gcd(12, 18, 27))  # → 3
import fractions
print(fractions.gcd(12, 18))  # → 6
print(fractions.gcd(4, -6))  # → -2

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy