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 કરે છે!