Type Converaion, Type Casting and eval()


Type Conversion and Type Casting In Python

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

Before learning Type Conversion in Python, you should have knowledge about Python Data Types :-

Type Conversion

The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. 

Python has two types of type conversion :-

  • Implicit Type Conversion
  • Explicit Type Conversion(Type Casting)

Implicit Type Conversion

In Implicit type conversion, Python automatically converts one data type to another data type. This process does not need any help of the developers, this is pre-defined in the Python Programming Language.

Let's see  where Python convert the conversion of the lower data type (integer) to the higher data type (float) in the program.

Converting integer to float

int = 500
float = 5.55

val = int * float

print("number data type(integer) :",type(int))
print("number data type(float) :",type(float))

print("After conversion :",type(val))

Above program explanation in given below :-

  • We will multiply two variables int and float, storing the value in val.
  • We will look at the data type of all three objects respectively.
  • In the output, we can see the data type of int is an integer while the data type of float is a float data type.
  • Also, we can see the val has a float data type because Python always converts smaller data types to larger data types.

But some time we use strings with them .

Now, let's try add a string and an integer, and we see what heppened in Python :-

int = 093321
str = "45665421"

print("int data type :",type(int))
print("str data type:",type(str))

print(int+str)

Output is shown with a error :-

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

Above program explanation in given below :-

We add two variables int and str.
As we can see from the output, we got TypeError. Python is not able to use Implicit Conversion in such conditions.

However, Python has a solution for these types of situations which is known as Explicit Conversion.

Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion.

This type of conversion is also called type casting because the user casts (changes) the data type of the objects.

Syntax :

(expression)

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

int = 093321
str = "45665421"

print("int data type :",type(int))
print("str data type:",type(str))

v_str = int(str)
print("after Type Casting:",type(v_str))

sum = int + str

print("Sum :",sum)
print("type of the sum:",type(sum))

Above program explanation in given below :-

  • We add str and int variable together.
  • We converted str from string to integer type using int() function to perform the addition.
  • After converting str to an integer value, Python is able to add these two variables.
  • We got the sum value and data type to be an integer.

Points to remember before use type conversion

  • Type Conversion is the conversion of object from one data type to another data type.
  • Implicit Type Conversion is automatically performed by the Python.
  • Explicit Type Conversion is also called Type Casting, the data types of objects are converted.



<-- All the details are available in the video.......Plz Subscribe the channel and click on the bell icon .... -->

 If you liked this post, then please distribute it among your friend circle or on the social media platforms .......


Thank you Guys and Good Bye...

Keep learning,

Tech Prog

                                Related Links 

>>> Data Types (with Python Project)          >>> CGI in Python


>>> Multi-threading In Python                      
>>> Convert Speech To Text 

 >>> Socket Programming in Python     >>> Data Types In Python                        
>>> Indentation, Quotes and Reserved Words In Python

>>> Linear Search & Binary Search                                            


>>> Introduction of Python               >>> Installation of Python

>>> schemas used in pandas dataframes 

Reactions

Post a Comment

0 Comments