Type Conversion In Python


In this chapter, we will learn what is type conversion in python

Python is a dynamically typed language, which means that variables are not bound to a particular data type during declaration. This allows for great flexibility in coding, but it also means that variables may need to be converted from one type to another for operations or assignments.

This process is known as type conversion or typecasting.

In this chapter, we will explore the various ways to perform type conversion in Python.

 

Implicit Type Conversion


Python automatically converts data types if an expression involves different data types. For example, if we add an integer and a floating-point number, Python will implicitly convert the integer to a float before performing the operation.
 

x = 5    # integer
y = 3.14 # float
z = x + y # addition of integer and float

print(z) # Output: 8.14

In the above example, Python automatically converted the integer value of x to a float before performing the addition with the float value of y.

 

Explicit Type Conversion


Sometimes, we may need to convert a variable to a specific data type. This can be done using explicit type conversion or typecasting. Python provides several built-in functions for typecasting, which we will discuss below.

A. int()

The int() function is used to convert a variable to an integer data type.
Example :

x = 3.14   # float
y = "10"   # string
z = True   # boolean

a = int(x) # float to integer conversion
b = int(y) # string to integer conversion
c = int(z) # boolean to integer conversion

print(a) # Output: 3
print(b) # Output: 10
print(c) # Output: 1

In the above example, we convert a float value to an integer using int(x). We also convert a string value to an integer using int(y). The string value of "10" is converted to the integer value of 10. We also convert a boolean value to an integer using int(z). The boolean value of True is converted to the integer value of 1.

 

B. float()

The float() function is used to convert a variable to a float data type.

Example:

x = 5      # integer
y = "3.14" # string
z = True   # boolean

a = float(x) # integer to float conversion
b = float(y) # string to float conversion
c = float(z) # boolean to float conversion

print(a) # Output: 5.0
print(b) # Output: 3.14
print(c) # Output: 1.0

In the above example, we convert an integer value to a float using float(x). We also convert a string value to a float using float(y). The string value of "3.14" is converted to the float value of 3.14. We also convert a boolean value to a float using float(z). The boolean value of True is converted to the float value of 1.0.

 

C. str()

The str() function is used to convert a variable to a string data type.
Example:

 

x = 10    # integer
y = 3.14  # float
z = False # boolean

a = str(x) # integer to string conversion
b = str(y) # float to string conversion
c = str(z) # boolean to string conversion

print(a) # Output: "10"
print(b) # Output: "3.14"
print(c) # Output: "False"


In the above example, we convert an integer value to a string using `str(x)`. We also convert a float value to a string using `str(y)`. The float value of `3.14` is converted to the string value of `"3.14"`. We also convert a boolean value to a string using `str(z)`. The boolean value of `False` is converted to the string value of `"False"`.

 

d. bool()

The `bool()` function is used to convert a variable to a boolean data type.
Example:

 

x = 10    # integer
y = 0     # integer
z = "True" # string

a = bool(x) # integer to boolean conversion
b = bool(y) # integer to boolean conversion
c = bool(z) # string to boolean conversion

print(a) # Output: True
print(b) # Output: False
print(c) # Output: True

In the above example, we convert an integer value to a boolean using bool(x). The integer value of 10 is converted to the boolean value of True. We also convert an integer value of 0 to a boolean using bool(y). The integer value of 0 is considered False in a boolean context. We also convert a string value to a boolean using bool(z). The string value of "True" is converted to the boolean value of True.

 

Type Conversion Errors


It's important to note that type conversion may not always be possible, especially when converting between incompatible data types. For example, trying to convert a string that contains letters to an integer will result in a ValueError:

x = "abc"
y = int(x) # Raises a ValueError: invalid literal for int() with base 10: 'abc'


Similarly, trying to convert a string that represents a floating-point number to an integer will result in a ValueError:

x = "3.14"
y = int(x) # Raises a ValueError: invalid literal for int() with base 10: '3.14'


Summary


Type conversion is an essential part of programming in Python, and it's important to understand the different ways to convert variables between data types. Python provides several built-in functions for typecasting, including int(), float(), str(), and bool(). However, it's important to note that type conversion may not always be possible, and attempting to convert incompatible data types can result in errors. By understanding how to perform type conversion in Python, we can write more flexible and robust code.

 


 

© 2022-2023 All rights reserved.