Theory
वह Input Trap जो Calculations तोड़ता है
कल्पना कीजिए एक samosa expense माँगने के लिए अपने PocketMoney tracker के लिए एक त्वरित script लिखना। आप prompt में 30 type करते हैं। variable इसे perfectly capture करता है। पर जिस पल आपकी script balance - spend calculate करती है, Python एक चमकदार TypeError के साथ execution रोक देता है। आपके Semester 1 BCA104 C labs में, compiler variable declarations लागू करके इसे रोकता। Python runtime पर string characters को numbers से टकराने क्यों देता है, और हम इसे कैसे ठीक करते हैं?
Theory
Airport Currency Counter
अलग data types को अलग global currencies की तरह सोचिए। अगर आप Delhi में एक दुकान में 500 Rupees और एक 10 Dollar bill के साथ जाते हैं, आप बस यह नहीं कह सकते कि आपके पास 510 units currency है। दुकानदार उन्हें सीधे जोड़ने से मना करेगा। आपको पहले एक currency exchange counter पर जाना होगा ताकि किसी भी addition से पहले उस 10 Dollar bill को इसके Rupee समकक्ष में स्पष्ट रूप से बदलें।
Theory
Implicit Conversion बनाम Explicit Casting
Python data translation को दो mechanisms से manage करता है। Implicit type conversion अपने-आप होती है जब Python data loss रोकने के लिए एक निचले data type को एक ऊँचे data type में बढ़ावा देता है, जैसे एक integer को एक floating point number में जोड़ना। इसके उलट, explicit type conversion या type casting के लिए आपको int(), float(), या str() जैसे built-in constructors इस्तेमाल करके एक value को एक श्रेणी से दूसरी में ख़ुद convert करना होता है।
At a glance
Table 1: automatic implicit conversions और manual casting techniques की तुलना।
| Conversion Type | यह कैसे होता है | Behavior उदाहरण | Data Loss जोखिम |
|---|---|---|---|
| Implicit | interpreter द्वारा अपने-आप | 5 + 2.0 का नतीजा 7.0 (float) | कोई जोखिम नहीं (safe widening) |
| Explicit (int) | int() function से हाथ से | int(5.9) का नतीजा 5 | उच्च जोखिम (decimals काटता है) |
| Explicit (float) | float() function से हाथ से | float("30") का नतीजा 30.0 | कोई जोखिम नहीं |
| Explicit (str) | str() function से हाथ से | str(450) का नतीजा "450" | कोई जोखिम नहीं |
Theory
एक Worked Example: Expense Flow ठीक करना
आइए देखें कि user expenses को ठीक से कैसे process करें। Python में, input() function हमेशा data को text strings के रूप में capture करता है। हमें अपना बचा balance calculate करने से पहले उस string value को एक number में स्पष्ट रूप से cast करना होगा।
Practical
PocketMoney Input Casting Solution
initial_balance = 500
# Simulating user typing an expense amount
input_spend = "35.50"
# Explicitly cast text string into a float number
converted_spend = float(input_spend)
# Implicit conversion happens here: int minus float becomes float
final_balance = initial_balance - converted_spend
print(final_balance)
print(type(final_balance))This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Computational Types की भविष्यवाणी
ऊपर के Python script का मन में विश्लेषण कीजिए। final_balance के लिए कौन सी बिल्कुल value और data type class print होगी? tap करने से पहले conversion ट्रेस कीजिए।
Show the answer
script output करेगी:
464.5
<class 'float'>
क्यों? explicit float("35.50") 35.5 देता है। जब integer 500 से 35.5 घटाते हैं, Python decimal part सँभालने के लिए expression को अपने-आप एक float में upgrade करता है, 464.5 का नतीजा देते हुए।
Quiz
x = int(14.8) + int("5") execute करने के बाद variable x में कौन सी अंतिम value store होगी?
- 19.8
- 20
- 19
- SyntaxError: invalid conversion
Show the answer
19
int() function floating point numbers को round करने के बजाय शून्य की ओर truncate करता है। इसलिए, int(14.8) 14 बन जाता है। इसे converted integer 5 में जोड़ने पर बिल्कुल 14 + 5 = 19 मिलता है।
Quiz
क्या होता है अगर आप expression: amount = int("45.5") execute करते हैं?
- यह सफलतापूर्वक integer value 45 में convert होता है।
- यह एक runtime ValueError फेंकता है।
- यह integer value 46 तक round up करता है।
- यह अपने-आप इसके बजाय एक float type बनाता है।
Show the answer
यह एक runtime ValueError फेंकता है।
int() constructor एक string को तभी convert कर सकता है अगर वह string एक साफ़, whole base 10 integer दर्शाता है। एक decimal point वाली string को सीधे int() में देना तुरंत एक ValueError पैदा करता है। आपको पहले float() इस्तेमाल करना होगा!
Watch out
Classic जाल: Double Jump String
university programming viva exams में सबसे बार-बार marks-गँवाने वाली ग़लती "98.6" जैसे string decimals को सीधे int() में देना है। students मानते हैं कि Python fraction छोड़ देगा और 98 देगा। यह नहीं देता। एक textual input string का decimal part सुरक्षित रूप से छोड़ने के लिए, आपको एक double jump करना होगा: पहले string को एक float में convert करें, फिर उस float को int(float("98.6")) की तरह एक integer में cast करें।
Theory
भविष्य के Semesters से जोड़ना
अभी explicit type conversion पर महारत Semester 3 के लिए अहम है। आपके data analytics labs (BCA302 और BCA303) में, जब आप pandas इस्तेमाल करके external Excel या CSV sheets से data पढ़ते हैं, numeric columns अक्सर generic string text के रूप में import होते हैं। आप statistical equations चलाने से पहले अपने datasets साफ़ करने के लिए .astype('float') जैसे casting methods पर निर्भर करेंगे।
Summary
Key takeaways
- Implicit conversion mathematical precision की रक्षा के लिए Python द्वारा अपने-आप की जाती है।
- Explicit type casting data structures को ज़बरदस्ती shift करने के लिए function constructors इस्तेमाल करता है।
- input() function हमेशा inputs को string characters के रूप में प्राप्त करता है।
- floating point values को integers में cast करना बिना round किए decimal fractions पूरी तरह छोड़ देता है।
- decimal numbers रखती strings अगर सीधे int() constructor में दी जाएँ तो errors फेंकती हैं।
- Memory Hook: Automatic बढ़ावा देती है, पर manual truncate करती है!