Difference between Type Casting and Type Conversion
The basic difference between type casting and type conversion is that type conversion is made automatically made by the interpreter or compiler whereas type casting is to be explicitly done by the developer.
We need type casting and type conversion when we need to convert the type of one variable to another type. When we want to perform addition or subtraction,the datatype of both variables .Type casting can be defined as, casting of one data type to another data type, by the programmer, at the time of program design.
Automatic Type Conversion(Implicit Type Conversion)
The interpreter only converts lower datatypes into higher datatypes.It cannot automatically convert higher datatype to lower datatype . It is also called Implicit Type Conversion
We can understand it by taking a real world example.Suppose that there is a person in a company who is working at a higher post, he cannot be automatically demoted by company, instead he has to be forced for doing that. Likewise, we may have other person in the company who is working at lowest position. If we want to promote him then it will happen very smoothly and the person is not required to be forced.
eg.
var_1=80 var_2=100.98 print("Type of var_1 is ",type(var_1)) print("Type of var_2 is ",type(var_2)) var_3=var_1+var_2 print("Type of var_3 is ",type(var_2)) print("Value of var_3 is ",var_3)
Output:
Type of var_1 is <class 'int'>
Type of var_2 is <class 'float'>
Type of var_3 is <class 'float'>
Value of var_3 is 180.98000000000002
In this example we see that int(lower data type) is automatically converted to float(higher data type).
eg.
var_1="80" var_2=100 print("Type of var_1 is ",type(var_1)) print("Type of var_2 is ",type(var_2)) var_3=var_1+var_2 print("Type of var_3 is ",type(var_2)) print("Value of var_3 is ",var_3)
Output:
Type of var_1 is <class 'str'>
Type of var_2 is <class 'int'>
Traceback (most recent call last):
File "C:/Users/your name/PycharmProjects/firstproj/newfile.py"., line 5, in <module>
var_3=var_1+var_2
TypeError: can only concatenate str (not "int") to str
In the above example we are getting typeerror as interpreter cannot perform implicit type conversion
Explicit Type Conversion
If we convert some data type to other datatype forcibly(i.e. manually) then it is called explicit type conversion.
Type conversion can be done by assigning the required data type function to the expression.
eg.
var_1="80" var_2=100 print("Type of var_1 is ",type(var_1)) print("Type of var_2 is ",type(var_2)) var_1=int(var_1) # Converting string to integer datatype var_3=var_1+var_2 print("Type of var_1 after conversion is is ",type(var_1)) print("Type of var_3 is ",type(var_2)) print("Value of var_3 is ",var_3)
Output:
Type of var_1 is <class 'str'>
Type of var_2 is <class 'int'>
Type of var_1 after conversion is is <class 'int'>
Type of var_3 is <class 'int'>
Value of var_3 is 180
In the above example we are converting string to integer datatype using int() function
eg.
var_1="80" var_2=100 print("Type of var_1 is ",type(var_1)) print("Type of var_2 is ",type(var_2)) var_2=str(var_2) # Converting integer to string datatype var_3=var_1+var_2 print("Type of var_2 after conversion is ",type(var_2)) print("Type of var_3 is ",type(var_3)) print("Value of var_3 is ",var_3)
Output:
Type of var_1 is <class 'str'>
Type of var_2 is <class 'int'>
Type of var_2 after conversion is <class 'str'>
Type of var_3 is <class 'str'>
Value of var_3 is 80100
In the above example we are converting integer to string datatype using str() function
eg.
var_1=100.58 var_2=100 print("Type of var_1 is ",type(var_1)) print("Type of var_2 is ",type(var_2)) var_1=int(var_1) # Converting string to integer datatype var_3=var_1+var_2 print("Type of var_1 after conversion is is ",type(var_1)) print("Type of var_3 is ",type(var_2)) print("Value of var_3 is ",var_3)
Output:
Type of var_1 is <class 'float'>
Type of var_2 is <class 'int'>
Type of var_1 after conversion is is <class 'int'>
Type of var_3 is <class 'int'>
Value of var_3 is 200
In the above example we are converting float to integer datatype using int() function.
Similarly we can convert a datatype to float using float() function.Syntax is same.
Converting user input to int()
eg.
a=int(input("Enter first value")) # Converting input to integer b=float(input("Enter second value"))# Converting input to float c=int(input("Enter third value"))# Converting input to integer print("Type of a is",type(a)) print("Type of b is",type(b)) print("Type of c is",type(c)) print("Addition of numbers is",a+b+c)
Output:
Enter first value23
Enter second value34.56
Enter third value12
Type of a is <class 'int'>
Type of b is <class 'float'>
Type of c is <class 'int'>
Addition of numbers is 69.56
In the above example we are converting user input to integer and float. By default the input() takes values as string. To know more Click here
Comments
Post a Comment
Please do not enter any spam link in the comment box