Typecasting in Python

Typecasting in Python

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion.

एक डेटा टाइप (integer, string, float, आदि) के मूल्य को दूसरे डेटा प्रकार में परिवर्तित करने की प्रक्रिया को टाइप कनवर्जन कहा जाता है। पायथन में दो प्रकार के रूपांतरण होते हैं।

  1. Implicit Type Conversion – ईम्प्लिक्ट टाईप कनवर्जन
  2. Explicit Type Conversion – एक्सप्लिक्ट टाईप कनवर्जन

Implicit Type Conversion
ईम्प्लिक्ट टाईप कनवर्जन 

In Implicit type conversion, Python automatically converts one data type to another data type: This process doesn’t need any user involvement. Let’s see an example where Python promotes conversion of lower data type (integer) to higher data type (float) to avoid data loss.

ईम्प्लिक्ट टाईप के कनवर्जन में, पायथन ऑटोमेटिक रूप से एक डेटा टाईप को दूसरे डेटा टाईप में परिवर्तित करता है। इस प्रक्रिया को किसी भी उपयोगकर्ता की भागीदारी की आवश्यकता नहीं है। एक उदाहरण देखें जहां पायथन डेटा के नुकसान से बचने के लिए निम्न डेटा टाइप (इंटीजर) को उच्च डेटा टाईप (फ्लोट) में बदलने को बढ़ावा देता है।

Example 1: Converting integer to float – इंटीजर को फ्लोट में परिवर्तित करना

a = 143 
b = 1.43 
c = a + b 
print ("Type of a :", type (a))
print ("Type of b :", type (b)) 
print ("value of c :", c) 
print ("Type of c:", type (c))

Output:
Type of a: <class ‘int’>
Type of b: <class ‘float’>
Value of c: 144.43
Type of c: <class ‘float’>

In the above program step by step:

उपरोक्त कार्यक्रम में स्टेप बाई स्टेप

  • We add two variables ‘a’ and ‘b’, storing the value in ‘c’.

हम दो वैरियेबल ‘a’ और ‘b’ जोड़ते हैं, ‘c’ में वैल्यू को संग्रहित करते हैं।

  • We will look at the data type of all three objects respectively.

हम क्रमशः तीनों ऑब्जेक्ट्स के डेटा टाईप को देखेंगे।

  • In the output we can see the data type of ‘a’ is an integer, data type of ‘b’ is a float.

आउटपुट में हम देख सकते हैं कि ‘a’ का डेटाटाइप एक इंटीजर है, ‘b’ का डेटाटाइप एक फ्लोट है।

  • Also, we can see the ‘c’ has float data type because Python always converts smaller data type to larger data type to avoid the loss of data.

इसके अलावा, हम देख सकते हैं कि ‘c’ में फ्लोट डेटा प्रकार है क्योंकि पायथन हमेशा डेटा के नुकसान से बचने

के लिए छोटे डेटा प्रकार को बड़े डेटा प्रकार में परिवर्तित करता है।

Now, let’s try adding a string and an integer, and see how Python treats it.

अब, एक स्ट्रिंग और एक इंटीजर जोड़ने का प्रयास करें, और देखें कि पायथन कैसे व्यवहार करता है।

Example 2: Addition of string (higher) data type and integer (lower) data type 

स्ट्रिंग का जोड़ (हायर) डेटा प्रकार और इंटीजर (लोवर) डेटाटाईप

a = 123 
b = "321"
print ("Data type of a:", type (a)) 
print ("Data type of b :", type (b)) 
print (a+b)

Output:
Data type of a: <class ‘int’>
Data type of b: <class ‘str’>
TypeError: unsupported operand type (s) for +: ‘int’ and ‘str’

In the above program, added two variable ‘a’ and ‘b’. But we got type error. Python is not able use Implicit Conversion in such condition. However Python has the solution for this type of situation which is known as Explicit Conversion.

उपरोक्त कार्यक्रम में, दो वैरियेबल ‘a’ और ‘b’ जोड़े गए। लेकिन हमें टाइप एरर मिला। ऐसी स्थिति में पायथन ईम्प्लिक्ट कनवर्जन का उपयोग करने में सक्षम नहीं है। जब भी पायथन के पास इस प्रकार की स्थिति का समाधान होता है इसे ईम्प्लिक्ट कनवर्जन के रूप में जाना जाता है।

Explicit Type Conversion
एक्सप्लिक्ट टाईप कनवर्जन

In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion. This type conversion is also called typecasting because the user casts (change) the data type of the objects.

एक्सप्लिक्ट टाईप कनवर्जन में यूजर ऑब्जेक्ट के डेटा टाईप को आवश्यक डेटा टाईप में परिवर्तित करते हैं। हम स्पष्ट प्रकार के कनवर्जन करने के लिए पूर्वनिर्धारित फंक्शन जैसे int(), float(), str(), आदि का उपयोग करते हैं। इस प्रकार के कनवर्जन को टाइपकास्टिंग भी कहा जाता है क्योंकि उपयोगकर्ता वस्तुओं के डेटा टाईप (कनवर्जन) कास्ट करता है।

Syntax:(required_datatype)(expression)

Example 3: Addition of string and integer using explicit conversion

एक्सप्लिक्ट कनवर्जन का उपयोग करके स्ट्रिंग और इंटीजर का जोड़।

a = 123
b = "321" 
print ("Data type of a :", type (a))
print("Data type of b before Type Casting:", type (b)) 
b = int (b) 
print ("Data type of b after Type Casting:", type (b)) 
total = a + b 
print ("Sum of a and b:", total) 
print ("Data type of the total:", type (total))

Output:
Data type of a : <class ‘int’>
Data type of b before Type Casting: <class ‘str’>
Data type of b after Type Casting: <class ‘int’>
Sum of a and b: 444
Data type of the total: <class ‘int’>

int(): We can use this function to convert values from other types to int type except complex type. If we want to convert string type to int type, compulsory string should be only integral value with base 10.

हम इस फंक्शन का उपयोग कॉम्प्लेक्स टाईप को छोड़कर अन्य प्रकारों से वैल्यूज को अंतर में परिवर्तित करने के लिए कर सकते हैं। यदि हम स्ट्रिंग. प्रकार को int type में बदलना चाहते हैं, तो अनिवार्य स्ट्रिंग को. केवल आधार 10 के साथ इंटीग्रल वैल्यू होना चाहिए।

For example:

>>> int (21.22) 
21 
>>> int (20.2+9j) 
TypeError: can't convert complex to int 
>>> int (True)
1
>>> int (False)
0
>>>int("32") 
32 
>>>int("33.33") 
ValueError: invalid literal for int() with base 10: '33.33' 
>>> int("two") 
ValueError: invalid literal for int() with base 10: 'two' 
>>> int ("0B10101")
 ValueError: invalid literal for int() with base 10: 'OB10101' 
>>> int (0B10101) 
21 
>>> int (0o12) 
10 
>>> int(0xFACE) 
64206

float ()

We can use float () function to convert other type to float type except complex type. If we are trying to convert string to float type compulsory string should be either integral or floating point literal and should be specified only in base 10.

हम अन्य प्रकार को फ्लोट टाईप को छोड़कर कॉम्प्लेक्स टाईप को बदलने के लिए float() फंक्शन का उपयोग कर सकते हैं। अगर हम स्ट्रिंग को फ्लोट टाईप में बदलने की कोशिश कर रहे हैं तो अनिवार्य स्ट्रिंग या तो इंटीग्रल या फ्लोटिंग पॉइंट लिटरल होना चाहिए और इसे केवल 10 के अनुसार निर्दिष्ट किया जाना चाहिए।

For example

>>> float (110) 
110.0 
>>> float (10.3) 
10.3 
>>> float ('20') 
20.0 
>>> float ( '21.7' ) 
21.7 
>>> float ('20.7+2j') 
ValueError: could not convert string to float: "20.7+2j' 
>>> float (True)
1.0 
>>> float (False) 
0.0 
>>> float ("two") 
ValueError: could not convert string to float: 'two' 
>>> float (0b111) 
7.0 
>>> float (0x12) 
18.0 
>>> float (0o22) 
18.0 
>>> float ("0b111") 
ValueError: could not convert string to float: '0b111'

Complex ()

We can use complex () function to convert other type to complex type. In Python, we have two types’ complex conversion functions.

हम अन्य टाइप को कॉम्प्लेक्स टाइप में बदलने के लिए complex() फंक्शन का उपयोग कर सकते हैं। पायथन में, हमारे पास दो प्रकार के कॉम्प्लेक्स कनवर्जन फंक्शन हैं।

1.complex(x): This function convert x into complex real part and imaginary part ‘0’

यह फंक्शन x को कॉम्प्लेक्स रीयल पार्ट और काल्पनिक भाग में परिवर्तित करता है कॉम्प्लेक्स ‘0’।

For example:

>>> complex (2) 
(2+0j) 
>>> complex (20.7) 
(20.7+0j) 
>>> complex (True) 
(1+0j) 
>>> complex (False) 
0j
>>> complex ('20') 
(20+0j) 
>>> complex ('33.33') 
(33.33+0j) 
>>> complex ("three") 
ValueError: complex () arg is a malformed string

2.complex(x,y):

This function convert x and y into complex number, here x is real part and y is imaginary part.

यह फंक्शन x और y को कॉम्प्लेक्स नम्बर में परिवर्तित करता है, यहाँ x वास्तविक भाग है और y काल्पनिक भाग है।

Rules: नियम

1.Can’t take second argument (y) if first argument (x) is a string.

यदि पहला तर्क (x) एक स्ट्रिंग है, तो दूसरा तर्क () नहीं ले सकता।

2.Second argument (y) can’t be a string at any time.

दूसरा तर्क (v) किसी भी समय एक स्ट्रिंग नहीं हो सकता है।

For example:

>>> complex (1,2) 
(1+2j) 
>>> complex (1.2,2.2) 
(1.2+2.2j) 
>>>complex ('1','2') 
TypeError: complex () can't take second arg if first is a string 
>>> complex (1,'2') 
TypeError: complex () second arg can't be a string 
>>> complex (True, True) 
(1+1j) 
>>> complex (False, False) 
0j 
>>> complex (0b111, True) 
(7+1j) 
>>> complex (0b111, 0b111) 
(7+7j) 
>>> complex (0x12,0x12) 
(18+18j) 
>>> complex (0o2,0o2) 
(2+2j)

bool ()

Using this function, we can convert other type to Boolean type. If Boolean argument value is ‘O’ then only it returns ‘False’, other than it returns ‘True’.

इस फंक्शन का उपयोग करके हम अन्य टाईप को बूलियन टाईप में परिवर्तित कर सकते हैं। यदि बूलियन तर्क वैल्यू ‘0’ है, तो केवल ‘फाल्स’ पर लौटता है, इसके अलावा अन्य पर ‘टू’ पर रिटर्न होगा।

>>>bool(0) 
False >>>
bool(1) 
True 
>>> bool (10) 
True 
>>>bool(0.01) 
True 
>>>bool(0.0) 
False 
>>>bool(-8) 
True 
>>> bool(10-2j) 
True 
>>> bool(0-0j) 
False 
>>> bool (0b1)
True 
>>> bool (0o1) 
True 
>>> bool (0x1) 
True 
>>> bool('False') 
True 
>>> bool ('True') 
True 
>>> bool('anything') 
True 
>>> bool ("") 
False

str()

Using this function, we can convert other type to string type.

इस फंक्शन का उपयोग करके हम अन्य टाईप को स्ट्रिंग टाईप में परिवर्तित कर सकते हैं।

For example:

>>> str (100) 
'100' 
>>> str (11.11) 
'11.11' 
>>> str (0b1) 
'1'
>>> str (0o1) 
'1'
>>> str (0x1) 
'1'
>>> str (0+0j) 
'0j'
>>> str (1+2j) 
' (1+2j) '
>>> str (True) 
'True' 
>>> str (False) 
'False' 
>>> str ()
''

Chapter wise Model Paper Link

About Me