Javatpoint Logo
Javatpoint Logo

Type Conversion in Python

Introduction:

In this article, we are discussing type conversion in Python. It converts the Py-type data into another form of data. It is a conversion technique. Implicit type translation and explicit type converter are Python's two basic categories of type conversion procedures.

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

In Python, there are two kinds of type conversion, these are -

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

Implicit Type Conversion

Implicit character data conversion is used in Python when a data type conversion occurs, whether during compilation or runtime. We do not need to manually change the file format into some other data type 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 an implicit shift of data types.

Program code 1:

Here we give an example of implicit type conversion in Python. In the below code, we initialize some values and find their data type in Python. So, the code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Data type of a: <class 'int'>
Data type of b: <class 'float'>
The value of c: 22.6
Data type of c: <class 'float'>
Data type of d: <class 'str'>

Explanation:

As we can see, while one variable, a, is only of integer type and the other, b, is of float type, the data type of "c" was automatically transformed to the "float" type. Due to type promotion, which enables operations by transforming raw data into 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 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 resulted in data loss. Python always translates smaller data types into larger data types to prevent data loss.

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

Program code 2:

Here we give another example of implicit type conversion in Python. In the below code, we take user inputs and find their data type in Python. So, the code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Priya
Data type of x: <class 'str'>
23
Data type of y: <class 'int'>
0.5
Data type of z: <class 'float'>

Explanation:

In the above code, we take user input and find their data type. Here we take the value of x as a string, the value of y as an integer, and the value of z as a float. After taking the user input, they check their data type and print them.

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.

  1. 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.
  2. float(): You can turn any data type into a floating-point number with this function.

Program code:

Here we give another example of explicit type conversion in Python. In the below code, we initialize the values and find their data type in Python. So, the code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

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

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

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

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

Program code:

Here we gave an example of finding the integer, hexadecimal, and octal values of given values. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

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

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

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

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

Program code:

Here we give an example of how to convert a given string into a list(), tuple(), and set(). The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

After converting the string to a tuple: ('j,' 'a', 'v,' 'a', 'T,' 'p,' 'o', 'i', 'n,' 't')
After converting the string to a set: {'n,' 'o', 'p,' 'j,' 'v,' 'T,' 'i', 'a', 't'}
After converting the string to the list: ['j,' 'a', 'v,' 'a', 'T,' 'p,' 'o', 'i', 'n,' 't']

Explanation:

In this above code, we convert a given string, "javaTpoint" into the tuple(), set(), and list(). The set is not ordered, so the elements are not order-wise in the output.







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