Fraction Type in Python 3.0 में Fraction नाम का एक और Data Type Add किया गया था, जो कि Rational Number Object को Implement करता है। इस Data Type में एक Numerator और एक Denominator दोनों होते हैं इसलिए ये Data Type, Floating Point Numbers की कुछ Limitations व Inaccuracies को Avoid करने में सहायक साबित होता है।
Decimals की तरह Fraction Type, हमारे Computer System के Underlying Hardware के साथ ज्यादा Closely Map नहीं होता। जिसकी वजह से इनकी Performance उतनी अच्छी नहीं होती लेकिन ये हमें एक Extra Utility Provide करता है, जिसका उपयोग हम एक Standard Python Tool के रूप में कर सकते हैं।
Fraction, काफी हद तक Decimal Type की तरह Fixed Precision Floating Point Value के रूप में ही काम करता है और दोनों का प्रयोग Floating-Point Type के Numerical Inaccuracies को Handle करने के लिए ही किया जाता है। साथ ही Decimal की तरह ही Fraction भी fractions नाम के Module में Exist है जिसे Use करने से पहले हमें हमारे Python Program में Import करना जरूरी होता है।
Fraction Object को Represent करने के लिए हमें Fraction Constructor Use करना पड़ता है और इस Constructor में हमें दो Numerator व Denominator के रूप में Numerical Parameters Pass करने पड़ते हैं। Fraction Object Create करने का दूसरा तरीका ये होता है कि हम Decimal() Constructor की तरह ही Fraction Constructor में भी String के रूप में Floating Point Number Pass करें। इन दोनों तरीकों को अगले Example द्वारा आसानी से समझा जा सकता है-
[code] FileName: FractionConstructor.py from fractions import Fraction x = Fraction(12, 22) y = Fraction(32, 3) print("Fraction x:", x) print("Fraction y:", y) print() # Using Fraction in Mathematical Expressions print("Addition of Fraction x + y:", x + y) print("Subtraction of Fraction x - y:", x - y) print() # Creating Fraction using Floating-Point Values fractionValue = Fraction('.4') + Fraction('3.98') print("fractionValue:", fractionValue) Output Fraction x: 6/11 Fraction y: 32/3 Addition of Fraction x + y: 370/33 Subtraction of Fraction x - y: -334/33 fractionValue: 219/50 [/code]
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।
Python in Hindi | Page: 602 | Format: PDF