Understanding Rational Numbers in Python
A Beginner's Guide to Rational Numbers in Python Programming

Rational numbers in Python refer to the representation and manipulation of numbers that can be expressed as a fraction (a quotient of two integers) using various programming techniques and functions in the Python language.
What Are Rational Numbers?
In the context of Python programming, a rational number refers to any numerical value that can be represented as the quotient or ratio of two integers, with the condition that the denominator is not equal to zero. The category of rational numbers encompasses integers, fractions, and decimals that either terminate after a finite number of decimal places or exhibit a repeating pattern in their decimal expansion.
For instance, the fraction 1/2, the negative integer -3, and the decimal 0.75 are all examples of rational numbers. In Python, rational numbers can be effectively represented and manipulated using various programming techniques, functions, and libraries specifically designed for handling such numerical values. These tools enable developers to perform arithmetic operations, comparisons, and conversions involving rational numbers with ease and precision, thereby enhancing the overall efficiency and accuracy of their numerical computations.
Rational numbers can be represented with a finite number of decimal places, not all real numbers with finite decimal representations are rational.A rational number is defined as a number that can be expressed as the ratio of two integers, where the denominator is not zero. Rational numbers include fractions like 1/2, 3/4, and integers like 5. These numbers can indeed be represented with a finite number of digits after the decimal point. For example:
1/2 can be represented as 0.5
3/4 can be represented as 0.75
5 can be represented as 5.0
However, there are real numbers that cannot be expressed as the ratio of two integers, and their decimal representations are non-repeating and non-terminating. These numbers are called irrational numbers. One famous example is π (pi), which is approximately 3.14159265359... The decimal expansion of π goes on forever without repeating, and it cannot be expressed as a simple fraction of two integers.
Fraction Class
Rational numbers can be represented in Python using the Fraction class in fraction module.
>>> from fractions import Fraction
>>> x = Fraction(3,4)
>>> print(x)
3/4
>>> y = Fraction(22,7)
>>> print(y)
22/7
>>> z = Fraction(6,10)
>>> print(z)
3/5
>>> a = Fraction(1,-4)
>>> print(a)
-1/4
In the code above, we can observe that fractions are automatically simplified. For example, Fraction(6,10) is reduced to Fraction(3,5). Additionally, a negative sign, if present, is always attached to the numerator.
Fraction Constructor
In Python, you can create fractions using the Fraction constructor provided by the fractions module. The Fraction constructor allows you to create fractions by specifying the numerator and denominator as arguments. Here's the basic syntax:
>>> from fractions import Fraction
>>> # Create a fraction with a numerator and denominator
>>> frac = Fraction(numerator, denominator)
>>> frac = Fraction(22,7)
>>> # getting numerator and denominator from fraction
>>> frac.numerator
>>> 22
>>> frac.denominator
>>> 7
numerator: This is the integer value that represents the numerator of the fraction, which is the top part.denominator: This is the integer value that represents the denominator of the fraction, which is the bottom part. The denominator cannot be zero, as division by zero is undefined in mathematics.
Here are some examples of how to create a fraction using the Fraction constructor:
1. Fraction(numerator=0,denominator=1)
2. Fraction(other_fraction) # we can create fraction from another fraction
3. Fraction(float) # we can create fraction from float
4. Fraction(deciaml) # we can create fraction from decimal
5. Fraction(String) # we can create fraction from string
We can create fractions in several ways:
>>> frac1 = Fraction(3, 4)
>>> print(frac1)
3/4
>>> frac2 = Fraction("3/4") #using String
>>> print(frac2)
3/4
>>> frac3 = Fraction(0.25) #using float
>>> print(frac3)
1/4
>>> frac4 = Fraction(frac3) #using another fraction
>>> print(frac4)
1/4
>>> Frac5 = Fraction(numerator=1,denominator=2) # using constructor
>>> print(Frac5)
1/2
>>> frac6 = Fraction(1.25) #using decimal
>>> print(frac6)
5/4
Basic Fraction Operations
Once you have created fractions, you can perform various operations on them, such as addition, subtraction, multiplication, and division:
>>> frac1 = Fraction(1, 4)
>>> frac2 = Fraction(1, 2)
>>> # Addition
>>> result_add = frac1 + frac2 # Result: 3/4
>>> # Subtraction
>>> result_sub = frac1 - frac2 # Result: -1/4
>>> # Multiplication
>>> result_mul = frac1 * frac2 # Result: 1/8
>>> # Division
>>> result_div = frac1 / frac2 # Result: 1/2
Converting to Floats and Integers
You can easily convert fractions to floating-point numbers or integers:
>>> frac = Fraction(3, 4)
>>> # Convert to a float
>>> float_val = float(frac) # Result: 0.75
>>> # Convert to an integer (truncates towards zero)
>>> int_val = int(frac) # Result: 0
Float as Fractions
Float objects have finite precision. Any float object can be written as fractions
>>> Fraction(0.75)
Fraction(3, 4)
>>> Fraction(1.375)
Fraction(11, 8)
>>> import math
>>> x = Fraction(math.pi)
>>> print(x)
884279719003555/281474976710656
>>> y = Fraction(math.sqrt(2))
>>> print(y)
6369051672525773/4503599627370496
Even though pi and the square root of 2 are both irrational, they are internally represented as floats with finite precision real numbers. Because of this, they can be expressed as rational numbers. But it is an approximation.
Constraining the denominator
Given a Fraction object, we can find an approximate equivalent fraction with a constrained denominator by using the limit_denominator(max_denominator=100000) instance method. This means we can find the closest rational number that can be considered precisely equal.
>>> x = Fraction(math.pi)
>>> x
Fraction(884279719003555, 281474976710656)
>>> x.limit_denominator(10)
Fraction(22, 7)
>>> x.limit_denominator(100)
Fraction(311, 99)
>>> x.limit_denominator(200)
Fraction(355, 113)
Handling Errors
Python's fractions module also handles cases like division by zero gracefully by raising a ZeroDivisionError:
>>> frac1 = Fraction(1, 0) # Raises a ZeroDivisionError
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
frac1 = Fraction(1, 0)
File "C:\Users\LOGA\AppData\Local\Programs\Python\Python39\lib\fractions.py", line 156, in __new__
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
ZeroDivisionError: Fraction(1, 0)
The fractions module in Python is a highly useful tool for executing accurate arithmetic operations with fractions. This module enables the creation, manipulation, and simplification of fractions, guaranteeing precise and dependable calculations. It is an essential resource for tackling mathematical problems, financial computations, or any application necessitating exact fraction management. However, be cautious of ZeroDivisionError, as demonstrated in the provided traceback, when creating fractions with a denominator of zero.
In Python, rational numbers refer to numerical values that can be represented as a quotient of two integers. These numbers can be effectively represented and manipulated using various programming techniques, functions, and libraries. The Fraction class in the fractions module allows developers to perform arithmetic operations, comparisons, and conversions involving rational numbers with ease and precision. This module enables the creation, manipulation, and simplification of fractions, guaranteeing precise and dependable calculations, making it an essential resource for tackling mathematical problems, financial computations, or any application necessitating exact fraction management.


