Type Casting nothing but converting one data type to other data type.
Int: Converting float, Bool, Str type to Int type.
- Convert Float type to Int type: int(10.5) => 10
- Convert Bool type to Int type: int(True) => 1
- Convert Bool type to Int type: int(False) => 0
- Convert Str type to Int type: int(“10”) => 10
- Convert Str type to Int type: int(“10.5”) => Value Error will get, if we want convert Str type to Int, string should be decimal.
- Convert complex type to Int type: int(10+20j) => Type Error will get
Float: Converting int, Bool, Str type to Float type.
- Converting Int type to Float type: float(10) => 10.0
- Converting Bool (True) type to Float type: float(True) => 1.0
- Converting Bool (False) type to Float type: float(False) => 0.0
- Converting Str type to Float type: float(“10”) => 10.0
- Converting Str type to Float type: float(“10.5”) => 10.5
- Converting Complex type to Float type: float(10 + 20j) => cant convert complex to float.
Complex: we can convert other type to complex type.its 2 form’s are there.
form1: complex(x) => x + 0j
form2: complex(x,y) => x + yj
form1:
- Converting Int type to Complex type: complex(10) => 10 + 0j
- Converting Float type to Complex type: complex(10.5 ) => 10.5 + 0j
- Converting Str type to Complex type: complex(“10” ) => 10 + 0j
- Converting Str type to Complex type: complex(“10.5” ) => 10.5 + 0j
form2:
- Converting Int type to Complex type: complex(10,20) => 10 + 20j
- Converting Bool type to Complex type: complex(True, False) => 1+ 0j
- Converting Str type to Complex type: complex(“10”, “20”) => complex() cant take arg if first is a string.
Bool: we can convert one data type to bool type.
- Converting Int type to Bool type: bool(0) => False
- Converting Int type to Bool type: bool(1) => True
- Converting Int type to Bool type: bool(-100) => True (Except 0 Remaining all True)
- Converting Float type to Bool type: bool(0.0) => False
- Converting Float type to Bool type: bool(0.1) => True
- Converting Complex type to Bool type: bool(10 + 20j) => True
- Converting Complex type to Bool type: bool(0 + 0j) => False
- Converting Str type to Bool type: bool(“) => False
- Converting Str type to Bool type: bool(“stackorigin) => True
- Converting Str type to Bool type: bool(” “) => True
Str: we can perform any type
- Converting Int type to Str type: str(100) => “100”
- Converting Bool type to Str type: str(True) => “True”
- Converting Complex type to Str type: str(10+20j) => “(10+20j)”