Note:Converting boolean value into integer will give 1 if boolean is True and 0 if false. In more simpler terms True means 1 and False means 0.
Converting string value in integer:
Note:String can only be converted in integer , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123" can be converted to 123.See Below:
Characters as a String: Will not convert
In [4]:
Integer as a String: Will convert
In [5]:
float(): Converting any Data Type to float data type
Converting integer data type in float:
In [6]:
Converting boolean data type in float:
In [8]:
Converting String data type in float:
Note:String can only be converted in float , if it is possible mathematically or if the string is a number. This means that string "ConsoleFlare" can not be converted to integer data type as it is not possible, but string "123.5" can be converted to 123.5.See Below:In [14]:
Note:Can we convert a String Float (float number in string) in integer data type directly?In [15]:
This will give an error, We cannot convert String float Directly into an integer data type. But there is a WorkAround that we can follow.
Convert string float to float data type.
Convert the float value to integer.
In [16]:
str(): Converting any Data Type to string data type
Note:str() can convert data type of any variables or values to string data type with no obstacles and no workaround needed.In [17]:
bool(): Converting any Data Type to boolean data type
To Convert Data type of a particular operand in boolean , we must know what that particular value represents , does it represent 0(False) or 1(True).
While converting any value in boolean , outcome will be either non-zero value which is True or zero-value which is false.
Zero-Values: Zero-values are the values that is equal to nothing or 0. Because 0 in its total sense is nothing.
Zero-values: 0 , "" , '','''''' and more where the value is nothing.
Non Zero-Values: Non Zero-values are the values that is not equal nothing. Any value that is not zero or nothing is Non-Zero Value.
Non - Zero-values: "0",123,123.5," "(there is a space between quotes), "12321","ConsoleFlare"
Converting Zero Values to boolean gives False:
In [18]:
In [19]:
In [20]:
Converting Non Zero Values to boolean gives True:
In [21]:
In [22]:
In [23]:
In [ ]:
Note:You must have seen in examples that converting 0 (integer type) gives False, but converting "0" gives True. It's because string type 0 is not equal to 0.
Why do we use Type Casting ?
Sometimes we need to do a particular operation on values but their data type does not allow us to perform such operations, in that case we need to convert its data type to perform desired operation.