Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - TYPE CASTING


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'.


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)) 


Output :



  The added value is : 11
  The data type of x is : <class 'int'>
  The data type of y is : <class 'int'>
  The data type of z is : <class 'int'>

So, if we see the above output, the Data Type of 'x' and 'y' is 'int' because both have an integer value in it.


x = 5
y = 6

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.


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)) 


Output :



  The added value is : 11.5
  The data type of x is : <class 'int'>
  The data type of y is : <class 'float'>
  The data type of z is : <class 'float'>

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 :


For variable 'x', it is 'int'.
For variable 'y', it is 'float'.

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.


Converting a float Data Type to an int Data Type


Example :


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)) 

Output :



  The added value is : 11
  The data type of x is : <class 'int'>
  The data type of y is : <class 'float'>
  The data type of z is : <class 'int'>

Now, if we see the output, we can see the added value of '5' and '6.5' is '11' and not '11.5'.


The added value is : 11

And the Data Type of the variable that holds '11' is 'int'.


The data type of z is : <class '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()'.


z = int(x + y)

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'.


Converting an int Data Type to an float Data Type


Example :


x = float(5)
print(x)
print(type(x))


Output :



  5.0
   <class 'float'>

As we know '5' is of Data Type 'int'. And we have used the type converter 'float()',


x = float(5)

To convert the value '5' to 'float'.


And if you see the output, it's '5.0' and the Data Type is 'float'.


Converting an str Data Type to an int or float Data Type


Say, there is a scenario, where we have assigned a number in String format.


Let us clear with the below example.


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))


Output :



  Traceback (most recent call last):
  File "add.py", line 3, in <module>
  z = x + y
  TypeError: unsupported operand type(s) for +: 'int' and 'str'

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'),


y = "6"

And a number,


x = 5

And we just ended up with the error.


TypeError: unsupported operand type(s) for +: 'int' and 'str'

Now, let us fix the above example with Type Casting.


Example :


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))


Output :



  The added value is : 11
  The data type of x is : <class 'int'>
  The data type of y is : <class 'int'>
  The data type of z is : <class 'int'>

So, we have used the Type Convertor 'int()' to convert the String to int.


y = int("6")

And '6' is treated as a number and we were able to add them.