Python Type Conversion in Hindi – उदाहरणों के साथ Explicit और Implicit Type Conversion

Python Type Conversion in Hindi को आसान भाषा और प्रैक्टिकल उदाहरणों के साथ सीखें। Explicit Type Conversion (Type Casting), Implicit Type Conversion (Coercion), int(), float(), str(), bool(), chr(), ord() जैसे built-in conversion functions तथा Type Conversion से जुड़ी सामान्य errors को विस्तार से समझें।

Python में Type Conversion (टाइप कन्वर्जन)

  • Type Conversion का अर्थ है — डेटा के एक टाइप (Data Type) को किसी दूसरे डेटा टाइप में बदलना (जैसे: Integer को String में बदलना)।
  • पायथन में Type Conversion मुख्य रूप से दो तरीकों से होता है:
    1. Explicit Conversion (स्पष्ट कन्वर्जन / टाइप कास्टिंग)
    2. Implicit Conversion (अंतर्निहित कन्वर्जन / ऑटोमैटिक)

Explicit Conversion (Type Casting)

  • Explicit Conversion को Type Casting भी कहा जाता है।
  • इसमें प्रोग्रामर स्वयं किसी डेटा का डेटा टाइप अपनी आवश्यकता के अनुसार बदलता है।
  • Explicit Type Conversion करने के लिए Python के predefined functions जैसे int(), float(), str(), chr() आदि का उपयोग किया जाता है।

📌 सिंटैक्स (Syntax)

required_datatype(expression)

Example: Roll Number और Student Name को जोड़ना

मान लेते हैं कि हमारे पास एक छात्र का रोल नंबर (Integer) और उसका नाम (String) है। अगर हम इन्हें सीधे जोड़ने की कोशिश करेंगे, तो पायथन एरर (TypeError) देगा। इसलिए, हमें रोल नंबर को पहले String में बदलना होगा।

कोड उदाहरण (Code Example):

roll_no = 25
student_name = “Riya”

# कन्वर्जन से पहले दोनों का डेटा टाइप चेक करना
print(“roll_no:”, type(roll_no))
print(“student_name:”, type(student_name))

# Explicit Conversion: Integer को String में बदलना
roll_no = str(roll_no)
print(“roll_no after conversion:”, type(roll_no))

# अब दोनों Strings को आसानी से जोड़ (Concatenate) सकते हैं
student_info = roll_no + ” – ” + student_name
print(“Student Info:”, student_info)
print(“Student_info final type:”, type(student_info))

📊 आउटपुट (Output)

roll_no: <class ‘int’>
student_name: <class ‘str’>
roll_no after conversion: <class ‘str’>
Student Info: 25 – Riya
student_info: <class ‘str’>
Student_info final type: <class ‘str’>

Python में Explicit Type Conversion Functions

Explicit Type Conversion के लिए Python कई built-in functions प्रदान करता है। इन functions की सहायता से हम किसी value को अपनी आवश्यकता के अनुसार दूसरे डेटा टाइप में बदल सकते हैं।

FunctionDescription (विवरण)
int(value)Value को Integer (int) में बदलता है।
float(value)Value को Floating-point Number (float) में बदलता है।
str(value)Value को String (str) में बदलता है।
chr(value)दिए गए Unicode (ASCII) integer value को उसके Character में बदलता है।
ord(value)दिए गए Character का Unicode (ASCII) integer value लौटाता है।

Note: unichr() function केवल Python 2 में उपलब्ध था। Python 3 में इसका उपयोग नहीं किया जाता। Python 3 में chr() function ही Unicode character return करता है।

Example: String को Integer में Convert करना (Explicit Type Conversion)

input() function हमेशा string (str) के रूप में input लेता है।
यदि हम किसी string में सीधे कोई number जोड़ने का प्रयास करते हैं, तो Python TypeError देता है।
ऐसी स्थिति में int() function का उपयोग करके string input को integer में बदला जाता है, जिससे उस पर गणितीय (arithmetic) operations किए जा सकते हैं।

x = int(input(“Enter a number: “))
print(x + 2)

Output:

Enter a number: 5
7

कुछ और उदाहरण (Examples of Explicit Type Conversion)

Example CodeSample OutputExplanation (व्याख्या)
 price = 50 item = ” Pens” print(str(price) + item)50 Pensstr(price) integer value को string में बदल देता है। इसके बाद इसे + operator की सहायता से दूसरी string के साथ जोड़ा जा सकता है।
marks = 89.7 bonus = 5 print(int(marks) + bonus)94int(marks) float value को integer में बदल देता है। Conversion के दौरान दशमलव (decimal) भाग हटा दिया जाता है, इसलिए 89.7 → 89 बन जाता है। इसके बाद 89 + 5 = 94 प्राप्त होता है।
count = 3 status = False print(bool(count) + status)1bool(count) integer value को Boolean में बदल देता है। क्योंकि count की value 0 नहीं है, इसलिए यह True बन जाता है।  में True का मान 1 और False का मान 0 होता है, इसलिए True + False = 1 होता है।

Note: bool() function भी Explicit Type Conversion के लिए उपयोग किया जा सकता है। यह किसी value को True या False में बदलता है।

Implicit Conversion (स्वचालित टाइप कन्वर्ज़न)

  • Implicit Conversion को Coercion भी कहा जाता है।
  • Implicit Type Conversion का अर्थ है कि Python बिना किसी विशेष function का उपयोग किए, एक डेटा टाइप को दूसरे डेटा टाइप में स्वतः (automatically) बदल देता है।
  • इस प्रकार का conversion सामान्यतः छोटे (narrower) डेटा टाइप से बड़े (wider) डेटा टाइप में होता है, जिससे डेटा का कोई नुकसान (loss of information) नहीं होता।
  • यह आमतौर पर तब होता है जब अलग-अलग डेटा टाइप्स के बीच कोई operation किया जाता है।

Example: Simple Interest की गणना

# Code to calculate Simple Interest
principal = 3500
rate_of_interest = 6.2
time_period = 5
simple_interest = (principal * rate_of_interest * time_period) / 100
print(“datatype of principal amount :”, type(principal))
print(“datatype of rate of interest :”, type(rate_of_interest))
print(“value of simple interest :”, simple_interest)
print(“datatype of simple interest :”, type(simple_interest))

Output

datatype of principal amount : <class ‘int’>
datatype of rate of interest : <class ‘float’>
value of simple interest : 1085.0
datatype of simple interest : <class ‘float’>

Example: Integer और Float पर Operation

num1 = 20
num2 = 4.5
result = num1 – num2
print(result)
print(type(result))

Output

15.5
<class ‘float’>

कुछ और उदाहरण (Implicit Type Conversion एवं Type Behavior)

Example CodeSample OutputExplanation (व्याख्या)
 a = 15 b = “World” print(a + b)TypeError:unsupported operand type(s) for +: ‘int’ and ‘str’यहाँ a एक integer है और b एक string।  इन दोनों को + operator से सीधे नहीं जोड़ सकता, इसलिए TypeError उत्पन्न होता है। इसे सही करने के लिए पहले integer को str() की सहायता से string में बदलना होगा।
 c = “Hi” n = 4 print(c * n)HiHiHiHiजब * operator का उपयोग string और integer के साथ किया जाता है, तो string उतनी ही बार दोहराई जाती है जितनी integer की value होती है। यहाँ “Hi” चार बार print होता है।
 x = False y = 8 print(x + 10)10 में False का मान 0 माना जाता है। इसलिए False + 10 का अर्थ 0 + 10 होता है, जिसका परिणाम 10 है।
 m = True n = 12 print(n – m)11 में True का मान 1 माना जाता है। इसलिए 12 – True का अर्थ 12 – 1 होता है, जिसका परिणाम 11 है।

Note: bool डेटा टाइप, int का एक विशेष रूप (subclass) है। इसलिए Python में True का मान 1 और False का मान 0 माना जाता है, जिससे वे arithmetic operations में भाग ले सकते हैं।

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *