The process of converting a datatype into another datatype is known as Type conversion. There are mainly two types of type conversions in Python, namely, implicit type conversion and explicit type conversion. In this module, we will go through the following topics, in case you want to jump to a specific one.
So, without any further delay let’s get started.
Learn Python in 16 hrs from experts
In Python, when the datatype conversion takes place during compilation or during run time, then it’s called implicit Datatype conversion. Python handles the implicit datatype conversion, so we don’t have to explicitly convert the datatype into another datatype. We have seen various examples of this kind of Datatype conversion throughout the tutorial.
Don’t worry if you can’t recall it, let’s see another example through the following code block.
Let’s add two variables of two different datatypes and store the result in a variable and see if the python compiler is able to convert the datatype of the resultant variable or not.
In the above example, we have taken two variables of datatype integer and float and added them. Further we have declared another variable named sum and stored the result of the addition in it. When we checked the datatype of the sum variable, we can see that the datatype of sum variable has been automatically converted into float datatype by the Python compiler. This is called Implicit Type conversion.
The reason that the sum variable was converted into float datatype and not integer datatype is that if the compiler had converted it into integer datatype then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts the smaller datatype into larger datatype to prevent the loss of data.
In some cases, Python cannot use implicit type conversion and that’s where Explicit type conversion comes into play. Moving ahead, let’s learn more about it.
Explicit type conversion is also known as type casting. Explicit type conversion takes place when the programmer clearly ad explicitly defines the same in the program. For explicit type conversion, Python provides some inbuilt functions. Following table contains some of the inbuilt function for type conversion along with their descriptions.
|int(y [base])||Converts y to an integer. Base specifies the base if y is a string.|
|float(y)||Converts y to a floating-point number.|
|complex(real [imag])||Creates a complex number.|
|str(y)||Converts y to a string.|
|tuple(y)||Converts y to a tuple.|
|list(y)||Converts y to a list.|
|set(y)||Converts y to a set.|
|dict(y)||Creates a dictionary. y should be a sequence of (key,value) tuples.|
|ord(y)||Converts a character into an integer|
|hex(y)||Converts integer to hexadecimal string|
|oct(y)||Converts integer to octal string|
Now that we know the in-built functions provided by Python, that are used for type explicit type conversion.
The syntax for explicit type conversion is as follows:
Let’s go over the following examples for explicit type conversion in Python.
In the above example, the variable a is of number datatype and variable b is of string datatype. When we try to add these two integers and store the value in variable named result1, a TypeError occurs as shown in the output. So, in order to perform this operation, we have to use explicit type casting.
As we can see in the above code block, we have converted the variable b into int type and then added variable a and b. The sum is stored in variable named result2 and when printed it display 300 as output, as we can see in the output block.
With this, we come to the end of this module. The next module highlights the if-else statements in Python. See you there!Previous Next
Download Interview Questions asked by top MNCs in 2019?