Fraction carries out all the fraction operations including addition, subtraction, multiplication, division, reciprocation
Project description
fraction 
fraction is a Python module that is designed for fractional values numerator/denominator
Installation
Easy to install
$ python -m pip install Fraction
Usage
Import
>>> from fraction import Fraction
Initialization
Creates a fraction equal to 0/1
>>> f1 = Fraction()
Creates a fraction equal to (1/2) / (1/4)
>>> print(Fraction('1/2', '1/4'))
2/1
Create fraction objects from strings and decimals
>>> print(Fraction('1.2'))
6/5
>>> print(Fraction('1.22/6'))
61/300
Create fraction objects from recurring decimals
>>> f = Fraction.fromdecimal(1.3, rec='3')
>>> print(f)
4/3
>>> f = Fraction.fromdecimal(24.5067, rec='067')
>>> print(f)
122411/4995
Creates a fraction equal to 3/10
>>> a = Fraction('3/10')
>>> b = Fraction('1/2')
# Get numerator/denominator
>>> a.numerator, a.denominator
(3, 10)
Arithmetic Operations
Computes reciprocal of fraction as Fraction object
>>> recip_a = Fraction.reciprocal(a)
>>> recip_a.numerator, recip_a.denominator
(10, 3)
Perform addition of Fraction objects (returns a Fraction object)
>>> sum_ab = a + b
>>> sum_ab.numerator, sum_ab.denominator
(4, 5)
Perform subtraction of Fraction objects (returns a Fraction object)
>>> diff_ab = a - b
>>> diff_ab.numerator, diff_ab.denominator
(-1, 5)
Perform multiplication of Fraction objects (returns a Fraction object)
>>> mul_ab = a * b
>>> mul_ab.numerator, mul_ab.denominator
(3, 20)
Perform div of Fraction objects (returns a Fraction object)
>>> div_ab = a / b
>>> div_ab.numerator, div_ab.denominator
(3, 5)
Comparison/Relational operations
Supports comparison operations [<, <=, >, >=, ==, !=] (returns boolean)
>>> a < b
True
>>> a == Fraction(7, 10)
False
For debugging
Supports str() and repr() built in objects methods
>>> a = Fraction()
>>> print a
0/1
>>> a
Fraction: 0/1
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Fraction-2.1.1.tar.gz
(4.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Fraction-2.1.1.tar.gz.
File metadata
- Download URL: Fraction-2.1.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75a27c61c68f357dbb8ea395eab932b3a16a3aa2b6aa44a052bcef319724f882
|
|
| MD5 |
0adee0a9143f24de7580d48dbe525033
|
|
| BLAKE2b-256 |
a68b4e1c65fc15f4dcadb7131bad57d0e99733645a59dff0c3f59ac31764f865
|
File details
Details for the file Fraction-2.1.1-py3-none-any.whl.
File metadata
- Download URL: Fraction-2.1.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2d4b00ac96051d3777e1feb5294e4b77b2980175ca6e452694e4464152bbc88
|
|
| MD5 |
e0388d4620d00855dc21bdc9d3f2213a
|
|
| BLAKE2b-256 |
f48b3c1e69825f3e29e3ec970f1cb928ca0b33301bb0f246584262779989fea6
|