• Articles
  • Tutorials
  • Interview Questions

Type Conversion in Python - From Basic to Advanced

Tutorial Playlist

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: 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 runtime, 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. 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 (sum)
print (type (sum)) #type() is used to display the datatype of a variable


Output:
10.5
<class ‘float’>

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 could see that the data type of the sum variable had 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, which would have resulted in data loss. So, Python always converts smaller data types into larger data types to prevent the loss of data.

Get 100% Hike!

Master Most in Demand Skills Now!

Explicit Type Conversion in Python

Explicit type conversion is also known as typecasting. 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. The following table contains some of the in-built functions for type conversion, along with their descriptions.

Function Description
int(y [base]) It converts y to an integer, and Base specifies the number base. For example, if you want to convert the string into decimal numbers then you’ll use 10 as the base.
float(y) It converts y to a floating-point number.
complex(real [imag]) It creates a complex number.
str(y) It converts y to a string.
tuple(y) It converts y to a tuple.
list(y) It converts y to a list.
set(y) It converts y to a set.
dict(y) It creates a dictionary and y should be a sequence of (key, value) tuples.
ord(y) It converts a character into an integer.
hex(y) It converts an integer to a hexadecimal string.
oct(y) 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:

(required_data type)(expression)

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
print(result2)


Output:
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
300

In the above example, 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 an int type and then added variables 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.

Certification in Full Stack Web Development

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 a 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 typecasting.
  • Python, when performing implicit type casting, avoids the loss of data.

We have now completed this module of the 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 Data Science tutorial.
Now look into the  Python Certification Training for upgrading your career to new heights. Also, check out our Python Interview Questions For Freshers.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.