Python Numbers in Hindi – Integer, Float, Complex, Real Numbers aur Type Conversion | Complete Guide
Table of Contents
1. Introduction to Numbers in Python in Hindi – Numbers Kya Hote Hain?
2. Types of Numbers in Python in Hindi – Numeric Types के प्रकार
6. Real Numbers in Hindi – Python में Real Numbers कैसे Represent होते हैं?
Introduction to Numbers in Python in Hindi – Numbers Kya Hote Hain?
Python में जब भी हम किसी गिनती, माप या calculation से जुड़ा data store करते हैं, तो उसे Numbers कहा जाता है। यह programming की सबसे basic और सबसे ज्यादा इस्तेमाल होने वाली data type है।
मान लीजिए आप एक shop का बिल बना रहे हैं। सामान की quantity एक साधारण गिनती है, उसकी price में paise भी शामिल हैं, और अगर आप किसी scientific calculation में हों तो कुछ values imaginary भी हो सकती हैं। Python इन सभी situations को अलग-अलग numeric types के जरिए handle करता है।
दूसरी languages की तरह Python में हमें variable बनाते समय उसका type खुद बताने की जरूरत नहीं होती। Python यह automatically समझ लेता है कि value किस type की है, इसे dynamic typing कहा जाता है।
Python में मुख्य रूप से तीन built-in numeric types होते हैं – int, float और complex। इन्हीं तीनों के आधार पर Real numbers की पूरी concept भी समझी जाती है, जिसे हम आगे विस्तार से देखेंगे।
Types of Numbers in Python in Hindi – Numeric Types के प्रकार
ऊपर दिया गया diagram दिखा रहा है कि Python में Numbers मुख्य रूप से Integer, Float, Complex और Boolean type में divide होते हैं, और Real Numbers की concept Integer और Float के मिलने से बनती है।
Python में हर number का एक type होता है, और हम
type()function की मदद से किसी भी value का type check कर सकते हैं।यह जानना बहुत जरूरी है क्योंकि exam में अक्सर पूछा जाता है कि कौन सी value किस category में आती है, और calculation करते समय अलग-अलग types के बीच क्या फर्क आता है।
a = 10
b = 3.5
c = 2 + 3j
print(type(a))
print(type(b))
print(type(c))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
Explanation: यहाँ a एक पूरा number है इसलिए उसका type int आया, b में decimal point है इसलिए वह float बना, और c में real और imaginary दोनों part हैं इसलिए उसका type complex दिखाया गया।
Integer (int) Numbers in Hindi
Integer वह numbers होते हैं जिनमें कोई decimal point नहीं होता, यानी यह पूरे (whole) numbers होते हैं। इसमें positive, negative और zero तीनों तरह की values आ सकती हैं।
Python की खास बात यह है कि यहाँ integer की size पर कोई fixed limit नहीं होती, यानी आप चाहें तो 5 digit का number लिखें या 500 digit का, Python उसे बिना किसी error के handle कर लेगा, जबकि C या Java जैसी languages में integer की एक fixed range होती है।
Integer का उपयोग ज्यादातर counting, indexing, loops चलाने और किसी भी whole quantity को represent करने के लिए किया जाता है।
x = 45
y = -120
z = 999999999999999999999
print(x, type(x))
print(y, type(y))
print(z, type(z))
Output:
45 <class 'int'>
-120 <class 'int'>
999999999999999999999 <class 'int'>
Explanation: तीनों values integer हैं इसलिए तीनों का type int ही आया। ध्यान दें कि z जितना बड़ा number है, उसे भी Python बिना किसी overflow error के आसानी से store कर लेता है।
Float Numbers in Hindi
Float वह numbers होते हैं जिनमें एक decimal point मौजूद होता है, यानी यह fractional या दशमलव values को represent करते हैं। इसे scientific रूप में भी लिखा जा सकता है, जैसे
2.5e3।Float का उपयोग वहाँ किया जाता है जहाँ precise measurement की जरूरत होती है, जैसे price calculation, percentage, average निकालना, या किसी scientific value को store करना।
एक जरूरी बात यह भी है कि float values में कभी-कभी थोड़ी सी precision की गलती आ सकती है, क्योंकि computer इन्हें binary format में store करता है, इसलिए बहुत sensitive calculations में इसका ध्यान रखना पड़ता है।
price = 199.99
temperature = -12.5
sci_value = 2.5e3
print(price, type(price))
print(temperature, type(temperature))
print(sci_value, type(sci_value))
Output:
199.99 <class 'float'>
-12.5 <class 'float'>
2500.0 <class 'float'>
Explanation: यहाँ sci_value को scientific notation में लिखा गया था, जिसे Python ने खुद decimal form में convert करके 2500.0 दिखाया, और तीनों values का type float आया क्योंकि उनमें decimal point मौजूद है।
Complex Numbers in Hindi
Complex numbers वे numbers होते हैं जिनमें एक real part और एक imaginary part दोनों होते हैं। Python में इसे
a + bjके format में लिखा जाता है, जहाँareal part है औरbimaginary part, तथाjको√-1के बराबर माना जाता है।Complex numbers का उपयोग ज्यादातर engineering, signal processing, physics और advanced mathematical calculations में होता है, जहाँ सिर्फ real values से काम पूरा नहीं होता।
Python में
.realऔर.imagattributes की मदद से किसी भी complex number के दोनों हिस्सों को अलग-अलग निकाला जा सकता है।
num = 4 + 5j
print(num)
print(type(num))
print("Real part:", num.real)
print("Imaginary part:", num.imag)
Output:
(4+5j)
<class 'complex'>
Real part: 4.0
Imaginary part: 5.0
Explanation: num.real से हमें केवल real part 4.0 मिला और num.imag से imaginary part 5.0 मिला। ध्यान दें कि दोनों values float के रूप में return होती हैं, चाहे शुरुआत में हमने integer भी लिखा हो।
Real Numbers in Hindi – Python में Real Numbers कैसे Represent होते हैं?
Mathematics में Real Numbers की category में वो सभी numbers आते हैं जिन्हें number line पर represent किया जा सकता है, यानी integers, fractions और decimal values सब real numbers ही होते हैं।
Python में अलग से कोई
realनाम की data type नहीं होती, बल्कि Real Numbers कोintऔरfloattypes की मदद से ही represent किया जाता है। मतलब जो भी number complex नहीं है, वह Python के लिए real number की category में आता है।इसीलिए जब भी कोई value integer या float type की हो, तो उसे Real Number माना जाता है, और यह तभी complex बनता है जब उसमें imaginary part भी जुड़ जाए।
a = 25
b = 12.75
c = 6 + 0j
print(isinstance(a, (int, float)))
print(isinstance(b, (int, float)))
print(isinstance(c, (int, float)))
Output:
True
True
False
Explanation: a और b दोनों real numbers हैं इसलिए isinstance() ने True दिया, लेकिन c एक complex number है, इसलिए भले ही उसका imaginary part 0 हो, फिर भी Python उसे real numbers की category में नहीं मानता।
Type Conversion Between Numeric Types in Hindi
यह diagram दिखा रहा है कि किसी int value को float और फिर complex में कैसे convert किया जाता है, और यह conversion Implicit या Explicit दोनों तरीकों से हो सकता है।
Type Conversion का मतलब है किसी एक numeric type की value को दूसरे numeric type में बदलना, जैसे
intकोfloatमें बदलना याfloatकोintमें बदलना।यह जरूरत तब पड़ती है जब हमें दो अलग-अलग types की values के बीच calculation करनी हो, या जब किसी function को किसी specific type का input चाहिए हो।
Python में इसके लिए तीन मुख्य built-in functions दिए गए हैं –
int(),float()औरcomplex(), जिनकी मदद से किसी भी numeric value को इच्छानुसार type में बदला जा सकता है।
a = 12 # int
b = float(a) # int se float
c = complex(a) # int se complex
print(a, type(a))
print(b, type(b))
print(c, type(c))
Output:
12 <class 'int'>
12.0 <class 'float'>
(12+0j) <class 'complex'>
Explanation: float(a) ने integer 12 को 12.0 में बदल दिया, और complex(a) ने उसी value को complex number (12+0j) में convert कर दिया, जिसमें imaginary part खुद-ब-खुद 0 सेट हो गया।
Float से Int में Conversion
जब किसी float value को int में convert किया जाता है, तो
int()function decimal part को हटा देता है, यानी वह round नहीं करता बल्कि सीधे truncate (काट) कर देता है।
d = 9.87
e = int(d)
print(d, type(d))
print(e, type(e))
Output:
9.87 <class 'float'>
9 <class 'int'>
Explanation: यहाँ 9.87 का decimal part .87 हटा दिया गया और सिर्फ पूरा हिस्सा 9 बचा, यह rounding नहीं है बल्कि truncation है, इसलिए exam में इस फर्क को याद रखना जरूरी है।
Implicit vs Explicit Type Conversion in Hindi
-
1. Implicit Type Conversion:
इसमें Python खुद automatically एक type को दूसरे type में बदल देता है, बिना programmer के किसी instruction के। यह ज्यादातर तब होता है जब दो अलग-अलग numeric types के बीच कोई operation किया जाता है। -
2. Explicit Type Conversion:
इसे Type Casting भी कहा जाता है, जिसमें programmer खुदint(),float()याcomplex()जैसे functions का उपयोग करके किसी value का type manually बदलता है।
x = 7 # int
y = 2.5 # float
result = x + y # implicit conversion
print(result, type(result))
z = int(input("Koi number likhiye: ")) # explicit conversion
print(z, type(z))
Output:
9.5 <class 'float'>
Koi number likhiye: 15
15 <class 'int'>
Explanation: जब int और float को आपस में जोड़ा गया, तो Python ने खुद ही x को float में बदलकर calculation की, यह Implicit Conversion है। जबकि int(input()) में हमने खुद int() function लगाकर string को integer में convert किया, यह Explicit Conversion है।
Type Conversion में होने वाली Common Errors
अगर किसी string value में letters मौजूद हों और उसे directly
int()याfloat()में convert करने की कोशिश की जाए, तो PythonValueErrorदे देता है।Complex numbers को कभी सीधे
int()याfloat()में convert नहीं किया जा सकता, क्योंकि उनमें imaginary part भी होता है, इसे convert करने की कोशिश करने परTypeErrorआता है।Conversion करते समय हमेशा ध्यान रखना चाहिए कि value वास्तव में उस format में convert होने लायक है या नहीं, वरना program बीच में ही crash हो सकता है।
value = "12abc"
number = int(value)
Output:
ValueError: invalid literal for int() with base 10: '12abc'
Explanation: "12abc" एक valid number नहीं है क्योंकि इसमें letters भी शामिल हैं, इसलिए Python इसे integer में convert नहीं कर पाया और ValueError throw कर दिया। ऐसी situations में try-except block का उपयोग करना एक अच्छी practice मानी जाती है।
Numeric Types समझने के फायदे
Numeric types की सही समझ होने से आप calculation में होने वाली गलतियों को पहले ही पहचान सकते हैं, जैसे float division में आने वाला extra decimal।
Type Conversion सीखने से आप user input, file data या किसी API से आई हुई values को सही format में बदलकर आसानी से process कर सकते हैं।
Complex numbers की जानकारी होने से scientific और engineering programs लिखना आसान हो जाता है, जहाँ real और imaginary दोनों values की जरूरत पड़ती है।
Exam की दृष्टि से भी यह topic बहुत महत्वपूर्ण है, क्योंकि
type(),int(),float()औरcomplex()से जुड़े output-based questions अक्सर पूछे जाते हैं।
Numbers और Type Conversion की सीमाएं
Float numbers में precision की छोटी-छोटी गलतियां आ सकती हैं, जिसकी वजह से बहुत sensitive calculations (जैसे banking systems) में extra सावधानी बरतनी पड़ती है।
Implicit Conversion कभी-कभी beginners के लिए confusing हो जाता है, क्योंकि उन्हें यह समझ नहीं आता कि Python ने खुद type क्यों बदल दिया।
Complex numbers को directly int या float में convert नहीं किया जा सकता, इसलिए इनके साथ काम करते समय अलग approach अपनानी पड़ती है।
Invalid string values को number में convert करने की कोशिश करने पर program error देकर रुक जाता है, इसलिए हर बार proper validation या exception handling जरूरी हो जाती है।
Frequently Asked Questions (FAQ) – Python Numbers in Hindi
Python में मुख्य रूप से तीन built-in numeric types होते हैं – Integer (int), Float (float) और Complex (complex)।
Python में अलग से कोई "real" data type नहीं होती, बल्कि Real Numbers को int और float types के जरिए ही represent किया जाता है।
Implicit Conversion में Python खुद type बदलता है, जबकि Explicit Conversion में programmer खुद int(), float() या complex() function का उपयोग करके type बदलता है।
Complex number में real और imaginary दोनों parts होते हैं, इसलिए उसे directly int या float में convert करने पर Python TypeError दे देता है।
float() किसी integer value के अंत में .0 जोड़ देता है, जैसे 12 को float() से convert करने पर वह 12.0 बन जाता है।