<jupyter_start><jupyter_text>Creating Fraction Instance<jupyter_code>import fractions
for n, d in [(1,2),(2,4),(3,6)]:
    f = fractions.Fraction(n,d)
    print('{}/{}={}'.format(n,d,f))
import fractions
for s in ['1/2', '2/4', '3/6']:
    f = fractions.Fraction(s)
    print('{}={}'.format(s, f))
import fractions
for s in ['0.5', '1.5', '2.0', '5e-1']:
    f = fractions.Fraction(s)
    print('{0:>4}={1}'.format(s, f))
import fractions
for v in [0.1, 0.5, 1.5, 2.0]:
    print('{}={}'.format(v, fractions.Fraction(v)))
import decimal
import fractions
values= [
    decimal.Decimal('0.1'),
    decimal.Decimal('0.5'),
    decimal.Decimal('1.5'),
    decimal.Decimal('2.0'),
]
for v in values:
    print('{}={}'.format(v, fractions.Fraction(v)))<jupyter_output>0.1=1/10
0.5=1/2
1.5=3/2
2.0=2<jupyter_text>Arithmetic<jupyter_code>import fractions
f1 = fractions.Fraction(1,2)
f2 = fractions.Fraction(3,4)
print('{} + {} = {}'.format(f1, f2, f1 + f2))
print('{} - {} = {}'.format(f1, f2, f1 - f2))
print('{} * {} = {}'.format(f1, f2, f1 * f2))
print('{} / {} = {}'.format(f1, f2, f1 / f2))<jupyter_output>1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3<jupyter_text>Approximating Values<jupyter_code>import fractions
import math
print('PI     =', math.pi)
f_pi = fractions.Fraction(str(math.pi))
print('No limit=', f_pi)
for i in [1,6,11,60,70,90,100]:
    limited = f_pi.limit_denominator(i)
    print('{0:8}={1}'.format(i, limited))<jupyter_output>1=3
       6=19/6
      11=22/7
      60=179/57
      70=201/64
      90=267/85
     100=311/99
{'fraction_non_alphanumeric': 0.23894939141575913, 'fraction_numerical': 0.1005765534913517, 'mean_word_length': 3.7333333333333334, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '102997', 'license': 'MIT', 'n_tokens_mistral': 768, 'n_tokens_neox': 714, 'n_words': 127, 'path': 'Python-Standard-Library/mathematic/fractions.ipynb', 'repo_name': 'gaufung/CodeBase', 'size': '1561'}