Python Type Casting
Python type casting is used when we want to change the data type of an object into another kind of data type. The process of doing this data type change is called type casting. It is also called casting, type conversion, data type conversion, or data type casting.
In another term, if we want to define casting, we can say, “Python has several kinds of data types. By using casting, we can change one data type into another data type.”
We use casting when we have to use a particular data type. Or maybe if we are working on a project and we have to work on a fixed data type.
Use of Python Type Casting
We already learned we use type casting when we have to use a fixed data type. For example:
Code:
a = 'Python'
b = 104
print(a + b)
Output:
TypeError: can only concatenate str (not "int") to str
In this example, we have two variables, a and b
and we are trying to concatenate or add both variables. We get an error in the output and it’s indicated that only the str value is concatenated to str. Here a is a string and b is an integer; both have different data types, and we cannot simply concatenate different data types. We can only concatenate both variables when they have the same data type. Now here, we have to use casting; either we change variable a
into an integer or variable b
into a string.
So in such cases, we have to use type casting.
How Many Types of Python Type Casting?
Mainly, there are two types of casting in Python.
1. Implicit casting or automatic casting |
2. Explicit casting or manual casting or coercion type conversion |
Implicit Casting in Python
When the Python interpreter automatically changed an object’s data type into another data type. It is called implicit casting. And because we didn’t do this casting. So it is also called automatic casting. However, there are only a few examples when the interpreter does automatic casting. Because Python is a strongly typed language. In weakly typed languages, there are high chances of automatic casting.
Before going to see an example of implicit casting. First, we understand the order of data types in Python. Data types are divided into higher and lower order. Every data type consumes memory. Those who consume more memory are considered higher-order data types.
For example, an integer data type consumes 4 bytes of memory, while a float data type consumes 8 bytes of memory because of the decimal part. So according to the interpreter, the float data type is higher than the integer data type. Now here is a rule, followed by the interpreter while doing the implicit casting. A higher data type will never be automatically converted into a lower data type but a lower data type can easily be converted into a higher data type. If the interpreter converts a higher data type into a lower data type, there are the possibilities of data lost. That is why the interpreter does not convert higher data types into lower data types.
For example, if we want to convert a float (8 bytes) data type into an integer (4 bytes) data type. It is possible data may be lost because we are trying to convert 8 bytes into 4 bytes. However, 4 bytes can easily be converted into 8 bytes. That is why, when we do an arithmetic operation between float and int data types, the interpreter automatically converts the int data type into the float data type.
Code:
a = 10
b = 20.0
c = a + b
print(c)
print(type(c))
Output:
30.0
<class 'float'>
Here, a is an int data type, and b is a float data type. Whenever the interpreter does an operation between int and float, the int data type is automatically converted into the float data type. So when c = a + b
, here, the interpreter converts a = 10
into a = 10.0
So that is why we get 30.0 in the output. And also we get the float class in the output. Same if we do a multiplication operation between them,
Code:
a = 10
b = 20.0
c = a * b
print(c)
print(type(c))
Output:
200.0
<class 'float'>
Same if we do an arithmetic operation between boolean and int data types. Boolean data type converted into int data type. Because int consumes 4 bytes while boolean consumes 2 bytes. So the boolean data type has a lower order compared to the int data type.
Code:
a = True
b = 1
c = a + b
print(c)
print(type(c))
Output:
2
<class 'int'>
We get the output class ‘int’ because before doing the arithmetic operation, a is converted into the int data type.
Same if we do an arithmetic operation between boolean and float data types. Then the boolean value is converted two times, first into the int data type and then into the float data type. For example:
Code:
a = True
b = 15.0
c = a + b
print(c)
print(type(c))
Output:
16.0
<class 'float'>
Here, a = True (boolean); So first, the boolean is converted into an integer (a = 1), and then the integer is converted into a float (a = 1.0) data type. So we get the float class in the output.
So this is how implicit casting works in Python. Lower-order data types can easily be converted into higher-order data types. It is also called coercion type conversion.
Click to learn about Python data types
Explicit Casting in Python
Explicit casting is done by ourselves. Programmers can do casting according to the uses. We can also convert higher-order data types into lower-order data types in explicit casting. Python has several built-in functions that are used in explicit casting.
Built-in Functions | Uses |
int() function | Used to convert into an integer data type. |
float() function | Used to convert into a float data type. |
complex() function | Used to convert into a complex data type. |
str() function | Used to converting into a string data type. |
tuple() function | Used to convert into a tuple data type. |
list() function | Used to convert into a list data type. |
set() function | Used to convert into a set data type. |
dict() function | Used to converting into a dictionary data type. |
frozenset() function | Used to convert a frozenset data type. |
chr() function | Used to convert a Unicode number into a character. |
ord() function | Used to converting a character into a Unicode number. |
int() function in Python
The int() function is used to convert a data type into an integer data type. However, not every data type can be converted into an integer data type. The int() function is useful when we have to use the integer data type for each value. For example:
Code:
num1 = 10
num2 = "10"
print(type(num1))
print(type(num2))
print(num1 + num2)
Output:
<class 'int'>
<class 'str'>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
We get an error while using the add operator between num1
and num2
. It means we cannot add int and str data types. num1 is an int data type and num2 is a str data type. So we have to convert num2
into the int data type.
Code:
num1 = 10
num2 = int("10")
print(type(num1))
print(type(num2))
print(num1 + num2)
Output:
<class 'int'>
<class 'int'>
20
Now we get 20 in the output, without any error, because now both num1 and num2 are integer data types. So by using the int() function, we convert the string data type into the integer data type. However, we cannot convert every string value into an integer data type. For example:
Code:
num1 = 10
num2 = int('Python')
print(num1 + num2)
Output:
ValueError: invalid literal for int() with base 10: 'Python'
We have ‘Python’ as a string value, and we are trying to change this into an integer data type but it is not possible because ‘Python’ is a word, and we cannot change words into numbers. So in the upper example, when we changed the string value ’10’ into an integer. It was possible because it was a number. So if we have a numeric string value, we can change the string data type into an integer data type.
We can also use the int() function with arguments.
Syntax: int(value, base) value: First argument, here we provide a number or string that can be converted into an integer number. base: This is an optional argument. Here we provide the number system in which we want to change the value. The default base value is 10. Base 10 means decimal number system. |
For example:
Code:
num1 = int('50', 10)
Here we provide both arguments, ’50’ as the value argument and 10 as the base argument. And because 10 is the default base value. So if we write this like, num1 = int('50')
It is the same as above. Now, when we write, num1 = int('50', 10)
it means we want to change the string value (’50’) into the decimal number system. Now if we change the base value.
Code:
num1 = int('100', 2)
Here the value argument is ‘100’ and the base argument is 2. Base 2 means binary number system. Now here, we are changing the string value (‘100’) into the binary number system. See the below example:
Code:
num1 = int('100', 10)
num2 = int('100', 2)
print(num1)
print(num2)
Output:
100
4
So we get the different output because in num1
, we are changing ‘100’ into a decimal number. And in num2
we are changing ‘100’ into a binary number. Just like this, base 8 means the octal number system. Base 16 means the hexadecimal number system.
In the implicit casting, we learned how the interpreter automatically converts lower-order data types into higher-order data types. We learned how the integer data type is automatically converted into the float data type. But here in explicit casting, we can change the higher-order data type into a lower-order data type. For example:
Code:
num1 = 2.5
print(type(num1))
print(num1)
num2 = int(num1)
print(type(num2))
print(num2)
Output:
<class 'float'>
2.5
<class 'int'>
2
Here we convert the float data type into the integer data type.
End of the Python type casting (part 1)
4 thoughts on “Python Type Casting (Part 1): What is Type Casting or Type Conversion in Python?”