What Is Type Conversion in Python
A Python data type conversion method is called a conversion of type. We can convert objects of string type to a numeric value, convert them to various container types, etc. Python primarily provides two types of conversion methods: implicit conversion type and explicit conversion type.
In Python, the conversion of data type is called an implicit conversion of data type when it takes place during compilation, or during run time. Python manages the conversion of the implicit data type, so we do not directly convert the data type to another data type.
Implicit type casting means conversion without losing the original significance of data forms. This form of typecasting is important if you want to adjust the data without changing the importance of the values stored in the variable.
Conversion of the implicit type occurs automatically if a value is copied to its compatible type. Strict guidelines for conversion of forms shall be enforced during conversion. If the operands are of two different data types, then the operand with a lower data type will be automatically converted to a higher data type.
Let us add two Python variables of two types of data, and store the result in a variable, and see if the Python compiler converts the resultant variable data type.
Output:
In the example above, we have taken and added two variables of integer and float data. In addition, we have declared and stored the result of the added variable called ‘sum’. By checking the sum variable data type, the Python compiler transforms the sum variable data type automatically into the float data type. This is referred to as conversion of implicit type.
The explanation for converting the sum variable into the floating data type is that if the compiler had converted it into the integer data type, the fractional part had to be omitted and data loss would have occurred. Python thus turns smaller data types into larger data types to avoid data loss.
In certain instances, Python cannot use implicit conversion and explicit conversion of the form enters into play.
Users convert the data type of an object to the data type required by Explicit Type Conversion. To perform explicit type conversion, we use predefined functions such as int(), float(), str().
This conversion form is also called typecasting as the user casts (changes) the object data type.
The conversion of the explicit type happens when the programmer specifies the program clearly and explicitly. There are several built-in Python functions for explicit form conversion.
Syntax:
Output:
Variable value1 is the data type number and variable value2 is the data type string. If these two entries are inserted and the value is stored in the variable called result1, a TypeError happens as seen in the output. So, we must use explicit casting to perform this operation.
We’ve turned the variable value2 to int and then inserted variable value1 and value2. The amount is stored in the result2 variable and 400 is shown in the output.
This function int (a, base) converts any data type into an integer. It is a method to convert number string a in the given base value to decimal value. ‘Base’ defines the base in which a string is transformed if the data type is a string. ‘Base’ is set to 10 by default.
Example:
Output:
A floating point number from a number or string is returned by float() method.
Must have some type of number.
Values that can be returned by float() according to the passed statement:
Example:
Output:
The ord() function in Python accepts an argument for a string in unit length and returns the Unicode equivalence of the passed argument. In other words, the ord() function returns the integer of the Unicode point of the character in the Unicode case or the byte value in the case of an 8-bit argument.
Example 1:
We find the Unicode code point values of an integer, a character, and a special character. The first 128 Unicode points are similar to those of ASCII, which means that the Unicode points are the same as the ASCII values of the passed strings of length 1.
Output:
Example 2:
An Error is raised when the length of the string is not equal to 1
The hex() function is one of Python3’s built-in functions used to convert the integer into a suitable hexadecimal form for the number of the integer.
Syntax :
The hex() function converts and returns the integer in a string form to the corresponding hexadecimal number.
The hexadecimal string returned begins with the prefix 0x which means it is hexadecimal.
Example 2:
Output:
Example 2: Passing the types other than the integer to the hex() method causes an error.
Output:
oct() is one of Python3’s built-in methods. The oct() method is an integer and returns its octal representation in a string format.
Syntax :
Example:
Output:
This function is used to convert a data type into a tuple.
A tuple is generated by placing all items (elements), separated by commas, inside the parentheses (). The parentheses are optional, but use of them is a great practice.
This function takes a single iterable parameter (optional). It’s an iterable object (list, range, etc.). The corresponding tuple is generated, if an iterable is transferred. If you don’t move the iterable, empty tuples are made.
Syntax:
This feature accepts a single iterable parameter (optional). It is an iterable object(list, range etc.). The corresponding tuple is formed when an iterable is moved. If you don’t move the iterable, you build empty tuple.
Output:
syntax:
Example:
Output:
This function converts every type of data into a list type.
The list() constructor returns the list.
Output:
This function is used to convert a tuple of order (key, value) into a dictionary.
Example:
Output:
This function is used to convert a value (integer or float) into a string.
Example:
Output:
When real and imaginary parts are given, the complex() method returns a complex number, or converts a string to a complex number.
Complex() normally takes two parameters:
If this form is the first parameter to be passed on, it will be construed as a complex number. In that case, you do not pass the second parameter.
Example:
Output:
The chr() method returns the character (string) from the integer (represents unicode code point of the character).
Only one integer is used as an argument in the chr() method. The range will vary between 0 and 1,1141,111 (0x10FFFF in base 16). The chr() method returns an integer character with a number unicode. The method returns a value error if an integer is passed beyond the range.
Example:
Output:
Conclusion:
Typecasting is also referred to as type conversion. It implies that one data form is transformed into another. It is often called type promotion as the smaller data type is converted into a bigger one.
When the compatible data type is found, implicit type conversion operates automatically.
Research & References of What Is Type Conversion in Python|A&C Accounting And Tax Services
Source
0 Comments