So far we have seen, how to add two numbers that are of the same type.
i.e. We have added two numbers that are of Data Type 'int'.
x = 5 y = 6 z = x + y print("The added value is : ",z) print("The data type of x is : ",type(x)) print("The data type of y is : ",type(y)) print("The data type of z is : ",type(z))
So, if we see the above output, the Data Type of 'x' and 'y' is 'int' because both have an integer value in it.
Similarly, if we see the Data Type of the variable 'z', it is also 'int'. Because 'z' holds the added value of the variables 'x' and 'y'. Which is also an integer(i.e. 11).
Now, there might also be a scenario where you want to add a floating point number and a whole number.
i.e. We want to add the whole number '5' and a floating point number '6.5'. So, what would be the output and most importantly, what would be the Data Type of the variable that would be holding the added value.
Let us see in the below example.
x = 5 y = 6.5 z = x + y print("The added value is : ",z) print("The data type of x is : ",type(x)) print("The data type of y is : ",type(y)) print("The data type of z is : ",type(z))
Well ! If we see the output, the added value of '5' and '6.5' is '11.5'.
Now, if we see the Data Types of all the variables :
And for the variable 'z' it is 'float' as well. Because the added value is '11.5' and as usual, Python made an intelligent guess that the Data Type of 'z' should be 'float'.
So, the result of a 'int' and 'float' is a 'float', which is decided by Python.
But, let's say, we don't want the added value of '5' and '6.5' to be '11.5'. Rather we want it to be just '11'.
This is where 'Type Casting' comes into picture.
Let us rewrite the above program with Type Casting.
x = 5 y = 6.5 z = int(x + y) print("The added value is : ",z) print("The data type of x is : ",type(x)) print("The data type of y is : ",type(y)) print("The data type of z is : ",type(z))
Now, if we see the output, we can see the added value of '5' and '6.5' is '11' and not '11.5'.
And the Data Type of the variable that holds '11' is 'int'.
And how did we achieve this ?
Well ! By Type Casting.
At the time of adding the numbers, we tell Python that the added value should be of Data Type 'int' and not 'float'.
And we have used the Type Converter 'int()'.
The Type Converter operator 'int()' checks the value inside its brackets('x + y' in this case). And converts it into Data Type 'int'.
Similarly, we have Type Convertors like 'float()' and 'str()' that are used to covert other Data Types to 'float' and 'str'.
x = float(5) print(x) print(type(x))
As we know '5' is of Data Type 'int'. And we have used the type converter 'float()',
To convert the value '5' to 'float'.
And if you see the output, it's '5.0' and the Data Type is 'float'.
Say, there is a scenario, where we have assigned a number in String format.
Let us clear with the below example.
x = 5 y = "6" z = x + y print("The added value is : ",z) print("The data type of x is : ",type(x)) print("The data type of y is : ",type(y)) print("The data type of z is : ",type(z))
So, the output tells us a lot! But the moral of the story is, we have ended up with an error.
And that isbecause we have tried adding a String(Note the double quotes '""' around '6'),
And a number,
And we just ended up with the error.
Now, let us fix the above example with Type Casting.
x = 5 y = int("6") z = x + y print("The added value is : ",z) print("The data type of x is : ",type(x)) print("The data type of y is : ",type(y)) print("The data type of z is : ",type(z))
So, we have used the Type Convertor 'int()' to convert the String to int.
And '6' is treated as a number and we were able to add them.