In [1]:
from fractions import Fraction
In [2]:
print(Fraction(1, 3))
In [3]:
print(Fraction(2, 6))
In [4]:
print(Fraction(3))
In [5]:
print(Fraction(0.25))
In [6]:
print(Fraction(0.33))
In [7]:
print(Fraction('2/5'))
In [8]:
print(Fraction('16/48'))
In [9]:
a = Fraction(1, 3)
print(a)
In [10]:
print(a.numerator)
print(type(a.numerator))
In [11]:
print(a.denominator)
print(type(a.denominator))
In [12]:
# a.numerator = 7
# AttributeError: can't set attribute
In [13]:
result = Fraction(1, 6) ** 2 + Fraction(1, 3) / Fraction(1, 2)
print(result)
print(type(result))
In [14]:
print(Fraction(7, 13) > Fraction(8, 15))
In [15]:
a_f = float(a)
print(a_f)
print(type(a_f))
In [16]:
b = a + 0.1
print(b)
print(type(b))
In [17]:
a_s = str(a)
print(a_s)
print(type(a_s))
In [18]:
pi = Fraction(3.14159265359)
print(pi)
In [19]:
print(pi.limit_denominator(10))
print(pi.limit_denominator(100))
print(pi.limit_denominator(1000))
In [20]:
e = Fraction(2.71828182846)
print(e)
In [21]:
print(e.limit_denominator(10))
print(e.limit_denominator(100))
print(e.limit_denominator(1000))
In [22]:
a = Fraction(0.565656565656)
print(a)
In [23]:
print(a.limit_denominator())
In [24]:
a = Fraction(0.3333)
print(a)
In [25]:
print(a.limit_denominator())
print(a.limit_denominator(100))