Type Conversion in Python with Examples
The process of converting a Python data type into another data type is known as type conversion. There are mainly two types of type conversion methods in Python, namely, implicit type conversion and explicit type conversion.
In this module, we will go through the following topics:
So, without any further delay, let’s get started.
Implicit Type Conversion in Python
In Python, when the data type conversion takes place during compilation or during the run time, then it’s called an implicit data type conversion. Python handles the implicit data type conversion, so we don’t have to explicitly convert the data type into another data type. We have seen various examples of this kind of data type conversion throughout the tutorial.
Looking for Python for Data Science Course All-in-1 Combo Training? Enroll now!
Don’t worry if you can’t recall it. Let’s see another example.
Let’s add two Python variables of two different data types and store the result in a variable and see if the Python compiler is able to convert the data type of the resultant variable or not.
a = 5
b = 5.5
sum = a + b
print (type (sum)) #type() is used to display the datatype of a variable
In the above example, we have taken two variables of integer and float data types and added them. Further, we have declared another variable named ‘sum’ and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion.
The reason that the sum variable was converted into the float data type and not the integer data type is that if the compiler had converted it into the integer data type, then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts smaller data type into larger data type 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.
Enrol yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!
Explicit Type Conversion in Python
Explicit type conversion is also known as type casting. Explicit type conversion takes place when the programmer clearly and explicitly defines the same in the program. For explicit type conversion, there are some in-built Python functions. Following table contains some of the in-built functions for type conversion, along with their descriptions.
||It converts y to an integer, and Base specifies the number base. For example, if you want to convert the string in decimal number then you’ll use 10 as base.
||It converts y to a floating-point number.
||It creates a complex number.
||It converts y to a string.
||It converts y to a tuple.
||It converts y to a list.
||It converts y to a set.
||It creates a dictionary and y should be a sequence of (key,value) tuples.
||It converts a character into an integer.
||It converts an integer to a hexadecimal string.
||It converts an integer to an octal string
Now that we know the in-built functions provided by Python that are used for explicit type conversion, let’s see the syntax for explicit type conversion:
Let’s go over the following examples for explicit type conversion in Python.
# adding string and integer data types using explicit type conversion
a = 100
b = “200”
result1 = a + b
b = int(b)
result2 = a + b
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
In the above example, the variable a is of the number data type and variable b is of the string data type. When we try to add these two integers and store the value in a 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 the variable named result2, and when printed it displays 300 as output, as we can see in the output block.
Go through this Python Course in London to get a clear understanding of Python!
Key Points to Remember While Performing Type Conversion in Python
- Type conversion is the process of converting a data type into another data type.
- Implicit type conversion is performed by Python interpreter only.
- Explicit type conversion is performed by the user by explicitly using type conversion functions in the program code.
- Explicit type conversion is also known as type casting.
- Python, when performing implicit type casting, avoids the loss of data.
With this, we come to an end of this module in Python Tutorial. Now, if you want to know why Python is the most preferred language for Data Science, you can go through this blog on Python for Data Science
Now, head toward our Python Certification Training for upgrading career to new heights. Also, check out our Python Interview Questions.