Javatpoint Logo
Javatpoint Logo

Type Conversion in Python

Type conversion is the transformation of a Py type of data into another type of data. Implicit type translation and explicit type converter are the two basic categories of type conversion procedures in Python.

Python has type conversion routines that allow for the direct translation of one data type to another. This is helpful for both routine programming and competitive programming. This page aims to enlighten readers about specific conversion functions.

In Python, there are two kinds of type conversion:

  • Explicit Type Conversion-The programmer must perform this task manually.
  • Implicit Type Conversion-By the Python program automatically.

Implicit Type Conversion

Implicit character data conversion is the term used in Python when a data type conversion occurs whether during compilation or even during runtime. We do not need to manually change the file format into some other type of data because Python performs the implicit character data conversion. Throughout the tutorial, we have seen numerous examples of this type of data type conversion.

Without user input, the Programming language automatically changes one data type to another in implicit shift of data types.

Output:

x type: <class 'int'>
y type: <class 'float' >20.6
a type: <class 'float'>

As we can see, while one variable, x, is only of integer type and the other, y, is of float type, the data type of "z" was automatically transformed to the "float" type. Due to type promotion, which enables operations by transforming raw data it in to a wider-sized type of data without any information loss, the float value is instead not turned into an integer. This is a straightforward instance of Python's Implicit type conversion.

The result variable was changed into to the float dataset rather than the int data type since doing so would have required the compiler to eliminate the fractional element, which would have resulting in data loss. To prevent data loss, Python always translates smaller data types into larger data types.

We may lose the decimal portion of the report if the variable percentage's data type is integer. Python automatically converts percentage data to float type, which can store decimal values, to prevent this data loss.

Explicit Type Conversion:

Let us say we want to change a number from a higher data type to a lower data type. Implicit type conversion is ineffective in this situation, as we observed in the previous section. Explicit type conversion, commonly referred to as type casting, now steps in to save the day. Using built-in Language functions like str() to convert to string form and int() to convert to integer type, a programmer can explicitly change the data form of an object by changing it manually.

The user can explicitly alter the data type in Python's Explicit Type Conversion according to their needs. Since we are compelled to change an expression into a certain data type when doing express type conversion, there is chance of data loss. Here are several examples of explicit type conversion.

  • The function int(a, base) transforms any data type to an integer. If the type of data is a string, "Base" defines the base wherein string is.
  • float(): You can turn any data type into a floating-point number with this function.

Output:

following the conversion to integer base 2: 18
After converting to float : 1010.0

3. The ord() method turns a character into an integer.

4. The hex() method turns an integer into a hexadecimal string.

5. The oct() method turns an integer into an octal string.

Output:

After converting the character into integer : 52
After converting the 56 to hexadecimal string : 0x38
After converting the 56 into octal string : 0o70

6. The tuple() method is used to transform data into a tuple.

7. The set() function, which converts a type to a set, returns the set.

8. The list() function transforms any data type into a list type.

Output:

After converting the string to tuple : ('j', 'a', 'v', 'a')
After converting the string to set : {'j', 'a', 'v', 'a'}
After converting the string the to list : ['j', 'a', 'v', 'a']






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA