Type Casting in Python
Type Casting is the method to convert the Python
variable datatype into a certain data type in order to
perform the required operation by users. In this article, we
will see the various techniques for typecasting. There can
be two types of Type Casting in Python:
Python Implicit Type Conversion
Python Explicit Type Conversion
Implicit Type Conversion
Implicit type conversion occurs
when Python automatically converts one data type to
another during an operation to ensure correct and safe
evaluation, without requiring any action from the user.
# Python automatically converts 'a' to int
a = 7
print(type(a))
# Python automatically converts 'b' to float
b = 3.0
print(type(b))
# Python automatically converts 'c' to float
as it is a float addition
c = a + b
print(c)
print(type(c))
# Python automatically converts 'd' to float
as it is a float multiplication
d = a * b
print(d)
print(type(d))
Output
<class 'int'>
<class 'float'>
10.0
<class 'float'>
21.0
<class 'float'>