TYPE CASTING ============ ==> Also called as "Type Conversion". ==> use to convert one form of the data to another form. ==> type casting is possible in two ways: 1) internal type casting/coercion/automatic type casting 2) external type casting External Type Casting ====================== ==> inbuilt methods are: int() float() complex() bool() str() int() ==== ==> used to convert any data into integer (decimal format). Syntax: int(value/Value reference) Rules for Integer conversion: ============================= 1) any form of the integer data can allow to convert into decimal Ex: binary ==> integer; octal ==> decimal; hexadecimal ==> decimal 2) any float data can convert into integer (decimal) Syntax: int(float value) 3) no complex data can convert into an integer (decimal) data a + bj ==> a ==> real and bj ==> imaginary int() ==> digits and not an alphabets ==> int(complex) ==> Type Error 4) Boolean data to integer data conversion is possible True ==> 1 False ==> 0 5) Not all the string formats can convert into integer ==> if string with only alphabets ==> not possible ==> if string with special characters ==> not possible ==> if string with digits (only with decimal) ==> possible Note: if a string with binary/octal/hexa-decimal/float ==> not possible to convert into integer. a = 100 # decimal b = 0b1100011 # binary c = 0o1122 # octal d = 0XAf12 # hexadecimal e = 123.456 f = 0.1122e-7 # float # g = 1122-2233j g = True h = False i = 'abcd' # type error while converting into an integer j = '@123' # type error k = '123' # possible to convert into integer l = '123.123' # type error m = '0b11001' # Value Error n = '0o1122' # value error print("The data in decimal form = ") print(int(a),a) print(int(b),b) print(int(c),c) print(int(d),d) print(int(e)) print(int(f)) # print(int(g)) print(int(g)) print(int(h)) print(int(k)) print(int(n)) =========================================== float() ====== ==> use to convert any data into a float Syntax: float(any data/data reference) Rules for Float conversion ========================== 1) any integer can convert into a float 2) no complex data can convert into float 3) any Boolean value can convert into float 4) Not all the string formats can convert into float ==> if string with only alphabets ==> not possible ==> if string with special characters ==> not possible ==> if string with digits (only with decimal) ==> possible ==> if string with floats ==> possible Note: if a string with binary/octal/hexa-decimal/complex ==> not possible to convert into float. a = 100 # decimal b = 0b1101 # binary c = 0o1122 # octal d = 0x123 # hexadecimal e = 12.23 - 23.45j # Type Error f = True g = False h = 'abcd';i = '123';j = '0b11011';k = '0x1122';l = '1234.2345';m = '12-23j' print("Any in float format is = ") print(float(a));print(float(b));print(float(c));print(float(d)) # print(float(e)) print(float(f));print(float(g)) # print(float(h)) Value error print(float(i)) # print(float(j)) value error print(float(l)) # print(float(m)) # value error ============================================================== complex() ========= 1) to convert any data into complex data Syntax: complex(any data/data reference) Rules for complex data conversion ================================= 1) any integer can convert complex 2) any float can convert into complex 3) any Boolean can convert into complex 4) string with decimals, floats and complex can convert into complex data. print(complex(100)) print(complex(0b110001)) print(complex(0O1122)) print(complex(0X1af)) print(complex(123.123)) print(complex(123e-7)) print(complex(True)) print(complex(False)) print(complex('123')) print(complex('123.234')) print(complex('12-23j')) # print(complex('0b110001')) 2) complex() can use to define the complex data by accepting two values Syntax: complex(value1,value2) here: value1 ==> real part value2 ==> imaginary part ex: value1 + value2j a = complex(10,20) b = complex(-10,-20) c = complex(0b1100011,0x12233) d = complex(123.345,123e-9) e = complex(True,False) # f = complex('123','123.456') print(a,b,c,d,e) =================================================== bool() ====== ==> can use to convert any data which includes any string literal into Boolean Note: any non-zero value can decode by the PVM as 'True' ==> 1 Syntax: bool(any data/data reference) print(bool(100),bool(0)) print(bool(0b0011),bool(0b00)) print(bool(0o1122),bool(0o00)) print(bool(0x1122),bool(0x00)) print(bool(123.234),bool(0.0000)) print(bool(123-234j),bool(0-0j)) print(bool('a'),bool('')) ==================================================== str() ==== ==> use to convert any data into string. Syntax: str(any value/data reference) print(str(100),str(0b110011),str(0o1122),type(str(0x1122))) print(str(123.234)) print(str(123-234j)) print(str(True)) ==================================================== Internal Type casting ===================== can perform by the system automatically according to the operation. print(123 + True) print(True * 3) print(True + True) # 2 a = 100 b = 13 d = 15.0 c = a/b e = a/d print(a) print(b) print(c) print(e)