Introduction to Python Type Conversion

In this article, you will learn about Python Type conversion and its uses.

Python Type Conversion – Before moving ahead, in Type Conversion, make sure you should have knowledge about Data Types.

Introduction – Python is open-source and free programming language. It is easy to use and learn as compared to other languages like C ++, Java, C# and JavaScript. It is highly recommended and allows itself to get integrated with other languages like C ++, C#, etc., One of the most important key factors of Python is it is Object-Oriented Language and had been used by many giant companies for so many purposes such as handling file data, web app, Desktop application, Machine learning and so on.

Python Type conversion – Each programming language has its own rules on how types can be converted. In Python, Type conversion is defined as changing data type (int, float, string) to another data type.

Python includes two types of type conversion –

1. Implicit Type Conversion

2. Explicit Type Conversion

Let’s begin first with implicit Type Conversion!

Implicit Type Conversion – It defined as an automated process. Python automatically converts one data type to another data type. Python doesn’t need any involvement to change it.

Example – Here we’re going to convert one data type i.e., int to another data type float.

x_int = 4
y_float = 5.0
new_number = x_int+y_float
 
print("Datatype of x_int:", type(x_int))
print("Datatype of y_float:", type(y_float))
print("Value of new_number:", new_number)
print("Datatype of new_number:", type(new_number)) 

Python will give us an output –

Datatype of x_int: <class 'int'>
Datatype of y_float: <class 'float'>
Value of new_number: 9.0
Datatype of new_number: <class 'float'> 
python-type-conversion

As it is shown clearly that, we first defined two variables having different data types and after that we add them. Later we saw all three data types respectively. In output we can clearly see that x_int has been converted into a float data type because Python always converts smaller data types to larger data types.

Let’s proceed ahead with trying adding a string and an integer.

Example – Adding string data type with an integer data type.

x_str = "110"
y_int = 5
z = x_str + y_int

print("Datatype of x_str:", type(x_str))
print("Datatype of y_int:", type(y_int))
print("New number:", z) 

Python will give us an output –

Datatype of x_str: <class str>
Datatype of y_int: <class int>

Traceback (most recent call last):
File "python", line 3, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str' 
python-type-conversion

As it is shown clearly that, we add string and int data type, Python successfully executes first and second print statement but for third print statement Python gives us an error. This error in Python known as TypeError. By this example we can know that Python is unable to use Implicit Conversion. But also Python has a solution for these types of problem which is known as Explicit Conversion.

Explicit Type Conversion – It defined as conversion of one data type to another data type. For using this, Python has some predefined functions such as int(), float(), str(), etc to perform explicit type conversion.

In programming lauguage, this type of conversion known as typecasting because Python casts (changes) the data type of the objects.

Syntax :
<Datatype>(expression)

Here, Typecasting can be done by assigning the required data type function to the expression.

Example – By using explicit conversion, adding a string in an integer.

x_int = 5
y_str = "10"

print("Data type of x_int:", type(x_int))
print("Data type of y_str before Type Casting:", type(y_str))
print("Sum of both x_int and y_str:", x_int + y_str) 

Python will give us an output –

Data type of x_int: <class 'int'>
Data type of y_str before Type Casting: <class 'str'>
Traceback (most recent call last):
TypeError: unsupported operand type(s) for +: 'int' and 'str'  
python-type-conversion

After converting y_str in an int data type.

new_y_data_type = int(y_str)
print("Data type of y_str after Type Casting:", type(new_y_data_type))
z_sum = x_int + new_y_data_type
print("z_sum:", z_sum)
print("Data type of z_sum:", type(z_sum)) 

Python will give us an output –

Data type of x_int: <class 'int'>
Data type of y_str before Type Casting: <class 'str'>
Data type of y_str after Type Casting: <class 'int'>
z_sum: 15
Data type of the z_sum: <class 'int'> 
python-type-conversion

As it is shown clearly that, we first defined an int data type and string data type. But Python is unable to add them because one of them is string data type. So, for adding both data type, first we convert string data type (y_str) in an int data type. After converting it in int data type, now Python can add both data type or variable. At the end we got sum of both variables (z_sum) and data type.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Follow Us

Leave a Comment

Stay in the loop

codingstreets