in ,

Python Type Conversion and Type Casting

Python Type Conversion and Type Casting

In this article, you will learn about the Type conversion and uses of type conversion.

Before learning Type Conversion in Python, you ought to know about Python Data Types.


In this article, you will learn-

Type Conversion

The process converting the value of one data type (whole number, string, skim, and so on.) to another data type is called type conversion. Python has two kinds of types of conversion.

1. Implicit Type Conversion

2. Explicit Type Conversion


Implicit Type Conversion

In Implicit type conversion, Python automatically changes over one data type to another information type. This procedure needn’t bother with any user involvement.

We should see a model where Python advances the change of the lower information type (whole number) to the higher information type (float) to avoid data loss.

Example 1: Converting integer to float

num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

When we run the above program, the output will be:

datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>

Value of num_new: 124.23
datatype of num_new: <class 'float'>

In the above program,

We include two variables num_int and num_flo, storing the value in  num_new.

We will look at the data type of all three objects respectively.

In the output, we can see the data sort of num_int is an integer while the data kind of num_flo is a float.

Also, we can see the num_new has a float data type since Python consistently changes over smaller data types to larger data types to avoid the loss of data.

Now, let’s try adding a string and an integer, and see how Python deals with it.


Example 2: Addition of string(higher) data type and integer(lower) datatype

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))

print(num_int+num_str)

When we run the above program, the output will be:

Data type of num_int: <class 'int'> 
Data type of num_str: <class 'str'> 

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

In the above program,

We include two factors num_int and num_str.

As we can see from the output, we got TypeError. Python can’t use Implicit Conversion in such conditions.

In any case, Python has an answer for these kinds of circumstances which is known as Explicit Conversion.


Explicit Type Conversion

In Explicit Type Conversion, clients convert the information sort of an object to the required information type. We utilize the predefined capacities like int(), float(), str(), and so on to perform express sort transformation.

This sort of transformation is likewise called pigeonholing in light of the fact that the client throws (changes) the information kind of the articles.

Syntax :

<required_datatype>(expression)

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


Example 3: Addition of string and integer using explicit conversion

num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))

num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))

num_sum = num_int + num_str

print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))

When we run the above program, the output will be:

Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>

Data type of num_str after Type Casting: <class 'int'>

Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>

In the above program,

We include the num_str and num_int variable.

We converted num_str from the string(higher) to integer(lower) type using int() function to perform the addition.

In the wake of changing over num_str to a number worth, Python can include these two factors.

We got the num_sum value and data type to be an integer.


Key Points to Remember

Type Conversion is the conversion of the object starting with one information type then onto the next data type.

Implicit Type Conversion is consequently performed by the Python interpreter.

Python avoids the loss of data in Implicit Type Conversion.

Explicit Type Conversion is also called Type Casting, the data sorts of articles are changed over using predefined function by the client.

In Type Casting, loss of data may occur as we enforce the object to a specific data type.


Please feel free to give your comment if you face any difficulty here.

For More Latest Articles Click on Below Link

salman khan

Written by worldofitech

Leave a Reply

Python Data Types

Python Data Types

Python Input, Output and Import

Python Input, Output, and Import