Python Type Conversion

Introduction

Type conversion in Python refers to the process of converting a value from one data type to another. This is a common task when writing code. It allows for more flexibility in how data is processed and ensures that operations between different types of data are error-free. Python performs some type conversions automatically, and also provides several built-in functions to convert between different data types.

Implicit Type Conversion

Implicit type conversion happens automatically when Python converts one data type to another without the programmer’s intervention. Python applies implicit type conversion for some closely related types.

Example:

num_int = 10   # Integer
num_float = 0.5   # Float

num_new = num_int + num_float

print("Value of num_new:", num_new)
print("Data type of num_new:", type(num_new))

Output:

Value of num_new: 10.5
Data type of num_new: <class 'float'>

In the example above, Python automatically converts num_int (an int) to a float and then adds it to num_float, resulting in a float type.

Example:

print(True + 5)
print(False + 5)

Output:

6
5

In the example above, Python converts the Boolean value True to the int value of 1 before adding it to another int type (line 1), and False to a 0 (line 2).

Explicit Type Conversion

Explicit type conversion requires the use of built-in functions to convert one data type to another. These functions are named after the data type being converted to and include int(), float(), str(), list(), tuple(), set(), and dict().

Example:

temp_c = input("Enter temperature in Celsius: ")
temp_f = float(temp_c) * 9/5 + 32
print("The temperature in Fahrenheit is:", temp_f)

Output:

Enter temperature in Celsius: 20
The temperature in Fahrenheit is: 68.0

The example above converts a temperature from Celsius to Fahrenheit. First it obtains the input from the user using the input() function (line 1), which is returns a str type. The float() function is used to explicitly convert the input string to a float before performing the arithmetic operation to convert Celsius to Fahrenheit (line 2). Not converting to a float in this case would result in an error as Python tries to perform arithmetic operations on a string.

Conversion Methods

Behind the scenes, to perform the conversion, Python’s type conversion functions call dunder methods of the objects being converted.

A dunder method is a function a that belong to an object, and whose name begins and ends with double underscores __. Dunder functions are considered special methods meant to be hidden and discouraged from direct use.

Example:

num = 10
print(num.__float__())

In the example above, the integer num is converted to a float using its __float__() method. This is equivalent to writing float(num), which is the preferred way.

Summary & Reference for Python Type Conversion

Type conversion in Python refers to the process of converting a value from one data type to another. It can be implicit or explicit.


Implicit type conversion happens automatically when Python converts one data type to another without the programmer’s intervention.

num_int = 10
num_float = 0.5
num_new = num_int + num_float  # Converts num_int from int to a float type

Python converts True and False to 1 and 0, respectively, when used in arithmetic operations.

print(True + 5)  # --> 6
print(False + 5)  # --> 5

Explicit type conversion requires using built-in functions such as int(), float(), str(), list(), tuple(), set(), or dict().

temp_c = input("Enter temperature in Celsius: ")
temp_f = float(temp_c) * 9/5 + 32

Dunder methods, named with double underscores __ on each side, are used internally for conversions. They are called by the built-in conversion functions.

Example:

num = 10
print(num.__float__())  # --> 10.0