python


Python Type Conversion and Type Casting


Type Conversion

Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. Python has two types of type conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion in Python

In Python, when the data type conversion takes place during compilation or during the run time, then it’s called an implicit data type conversion. Python handles the implicit data type conversion, so we don’t have to explicitly convert the data type into another data type. Let’s add two Python variables of two different data types and store the result in a variable and see if the Python compiler is able to convert the data type of the resultant variable or not.

a = 2
c = 27.2
sum = a + c
print (sum)
print (type (sum)) #type() is used to display the datatype of a variable


Output:
29.2
<class 'float'>

In the above example, we have taken two variables of integer and float data types and added them. Further, we have declared another variable named ‘sum’ and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion. The reason that the sum variable was converted into the float data type and not the integer data type is that if the compiler had converted it into the integer data type, then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts smaller data type into larger data type to prevent the loss of data. In some cases, Python cannot use implicit type conversion and that’s where explicit type conversion comes into play. Moving ahead, let’s learn more about it.


Explicit Type Conversion

Explicit type conversion is also known as type casting. Explicit type conversion takes place when the programmer clearly and explicitly defines the same in the program. For explicit type conversion, there are some in-built Python functions. Following table contains some of the in-built functions for type conversion, along with their descriptions.

Function Description
int(y [base]) It converts y to an integer, and Base specifies the number base. For example, if you want to convert the string in decimal number then you’ll use 10 as base.
float(y) It converts y to a floating-point number.
complex(real [imag]) It creates a complex number.
str(y) It converts y to a string.
tuple(y) It converts y to a tuple.
list(y) It converts y to a list.
set(y) It converts y to a set.
dict(y) It creates a dictionary and y should be a sequence of (key,value) tuples.
ord(y) It converts a character into an integer.
hex(y) It converts an integer to a hexadecimal string.
oct(y) It converts an integer to an octal string

Now that we know the in-built functions provided by Python that are used for explicit type conversion, let’s see the syntax for explicit type conversion:

(required_data type)(expression)

Let’s go over the following examples for explicit type conversion in Python.

# adding string and integer data types using explicit type conversion
a = 100
b = '200'
result1 = a + b
b = int(b)
result2 = a + b
print(result2)


Output:
Traceback (most recent call last):
File '', line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'
300

In the above example, the variable a is of the number data type and variable b is of the string data type. When we try to add these two integers and store the value in a variable named result1, a TypeError occurs as shown in the output. So, in order to perform this operation, we have to use explicit type casting. As we can see in the above code block, we have converted the variable b into int type and then added variable a and b. The sum is stored in the variable named result2, and when printed it displays 300 as output, as we can see in the output block.

Key Points to Remember While Performing Type Conversion in Python