In this tutorial, I am going to discuss about type conversion in python language. When we evaluate any expression it contains different variables and they might be of different datatypes, so we are going to discuss how python handles such situation.
Type Conversion
Type conversion is the process of converting value of one data type (like string, integer, float) to another data type. Python has two types of type conversion:
- Implicit Conversion
- Explicit Conversion
We will learn about each of them one by one.
Implicit Type Conversion
Implicit type conversion means that python automatically converts one data type to another. This doesn't require any user involvement.
Let's see an example:
var_int = 24
var_flt = 24.5
new_var = var_int + var_flt
print("data type of var_int : ", type(var_int))
print("data type of var_flt : ", type(var_flt))
print("value of new_var : ", new_var)
print("data type of new_var : ", type(new_var))
data type of var_int : < class 'int' >
data type of var_flt : < class 'float' >
value of new_var : 48.5
data type of new_var : < class 'float '>
In the above program,we add var_int and var_flt variables and store the output in new_var.
In above code,we can observe the following:
- var_int has data type int, var_flt has data type float.
- new_var variable has data type float as python converts smaller data types to larger data types for avoiding data loss.
Let's see another example:
var_str = '35'
var_int = 41
print("data type of var_str : ", type(var_str))
print("data type of var_int : ", type(var_int))
var_sum = var_str + var_int
print("value of var_sum : ", var_sum)
print("data type of var_sum : ", type(var_sum))
data type of var_str : < class 'str' >
data type of var_int : < class 'int' >
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-4-4338627e2965 > in < module >
5 print("data type of var_int : ", type(var_int))
6
----> 7 var_sum = var_str + var_int
8 print("value of var_sum : ", var_sum)
9 print("data type of var_sum : ", type(var_sum))
TypeError: can only concatenate str (not "int") to str
In the above program,we add var_str and var_int variables and store the output in var_sum.
The following can be observed from the above program :
- We got TypeError in the output. Python is not able to use implicit conversion in such conditions.
- For this python provides a way for conversion of data types explicitly also known as Explicit Type conversion.
Explicit Type Conversion
In python, users can explicitly convert one data type to another with the help of some predefined functions or methods. This is known as Explicit Type Conversion in python. Predefined functions like int()
, float()
, str()
etc. are used for this purpose.
Explicit type conversion is also known as type casting because the user explicitly casts(changes) the data types of the object.
Type casting can be done by assigning the required data type function to the expression.
Let's look at some examples of explicit type conversion in python:
var_str = '35'
var_int = 41
print("data type of var_str : ", type(var_str))
print("data type of var_int : ", type(var_int))
var_str = int(var_str) #type casting str to int
print("data type of var_str : ", type(var_str))
var_sum = var_str + var_int
print("value of var_sum : ", var_sum)
print("data type of var_sum : ", type(var_sum))
data type of var_str : < class 'str' >
data type of var_int : < class 'int' >
data type of var_str : < class 'int' >
value of var_sum : 76
data type of var_sum : < class 'int' >
As you can see the same code which was giving Type Error is now working as expected because of explicit type conversion.
In the above program, we add var_str and var_int variables. Observe the following points:
- We converted var_str to integer data type using predefined
int()
method. - After converting we are able to add var_str and var)int variables the result of which is stored in var_sum.
- We got a value in var_sum and its data type is an integer.
Thus, python handles any discrepancies among the datatypes of variables in an expression using any of these two type conversions, which also help in evaluation of an expression with utmost precision.
Conclusion
In above tutorial, we discussed implicit and explicit type conversion in python. We have seen use and importance of type conversion while evaluating expressions in python.
If you have any questions please comment below also share your views and suggestions in the comment box.