A6. Python Type Conversion
In this section, we will explore how to convert between data types in Python with examples.
In Python programming, type conversion
refers to the process of changing data from one data type to another. For example, converting an integer to a string or vice versa.
Python supports two types of type conversion:
Implicit conversion :
Python automatically converts data types as needed. This occurs in the background (done by the compiler )without the programmer explicitly requesting it.
Explicit conversion :
The programmer explicitly specifies the type to convert the data to, also known as typecasting. The desired data type is passed in the form of a function to wrap the data in a conversion method.
Below Examples show both implicit and explicit conversion on how type conversion
works in Python and when each method is appropriate to use.
Python Implicit Type Conversion
In Python, implicit type conversion is the automatic conversion of one data type to another data type without the programmer having to explicitly code the conversion. This is also known as type coercion
.
Python supports various types of data types such as integers, floating-point numbers, strings, and booleans. When performing operations on these data types, Python automatically converts the data types as necessary to complete the operation.
For example, if we try to add an integer and a floating-point number, Python will automatically convert the integer to a floating-point number before performing the addition operation. Similarly, if we try to concatenate a string and an integer, Python will convert the integer to a string before concatenating the two.
Belo are some examples of implicit type conversion in Python:
# Implicit conversion from int to float
a = 5
b = 2.5
c = a + b
print(c) # Output: 7.5
# Implicit conversion from int to string
x = 10
y = "The value of x is: " + str(x)
print(y) # Output: The value of x is: 10
# Implicit conversion from string to int
s = "50"
t = s + 100
print(t) # Output: 150
In the above example, Python will first convert the string “50” to an integer before adding it to the integer 100. This is an example of implicit type conversion from string to int.
However, it’s important to note that not all types can be implicitly converted to each other, and some conversions may result in unexpected behavior.
Python Explicit Type Conversion
In Python, explicit type conversion is the conversion of data from one type to another type using built-in functions. Unlike implicit type conversion, the programmer has to explicitly code the conversion.
Python provides built-in functions such as int(), float(), str(), bool(), and list() to perform explicit type conversion. These functions take a value of one data type and return a value of another data type.
Here are some examples of explicit type conversion in Python:
# Explicit conversion from float to int
a = 5.6
b = int(a)
print(b) # Output: 5
# Explicit conversion from int to string
x = 10
y = "The value of x is: " + str(x)
print(y) # Output: The value of x is: 10
# Explicit conversion from string to int
s = "50"
t = int(s) + 100
print(t) # Output: 150
In the above example we use int() function to convert the string “50” to an integer before adding it to the integer 100. This is an example of explicit type conversion from string to int.
Explicit type conversion is useful when we want to ensure that the data type of a variable is a specific type before performing operations on it. It can also help avoid unexpected results that can occur when Python performs implicit type conversion.
Important Tips to remember while using type conversion in Python
Here is a rewritten version of the key points:
1. Type conversion involves changing an object from one data type to another.
2. Implicit type conversion is performed automatically by Python as needed.
3. Python attempts to avoid loss of data with implicit conversion.
4. Explicit type conversion, also known as type casting, is performed by the programmer using conversion functions. With explicit conversion, data loss may occur as the object is forced into a specific data type.
In both cases, conversion methods allow you to convert between string, integer, floating point, and other data types as required for your application.
Type Conversions in Python
Conversion methods are functions used to explicitly convert data from one type to another in Python. Some common conversion methods include:
int() - Converts value to an integer
float() - Converts value to a float (decimal number)
str() - Converts value to a string
chr() - Converts an integer to a character
ord() - Converts a single character string to its integer value
complex() - Converts value to a complex number
For example:
int('10') # Converts '10' to integer 10
float(10) # Converts 10 to float 10.0
str(10) # Converts 10 to string '10'
chr(65) # Converts integer 65 to character 'A'
ord('A') # Converts character 'A' to integer 65
complex(1, 2) # Converts real and imaginary parts to a complex number (1 + 2j)
These methods allow you to be explicit about converting between types and can handle exceptions or edge cases more gracefully than implicit conversion in some scenarios.