Typecasting is the process of converting a variable’s datatype to another datatype. It can be done implicitly in python.
How to know
the datatype of a variable in python???
Here, is
the clue…
A keyword ‘type()’ is used to find the data type.
Examples
are given below.
a = 4
b = 5.7
c = ‘s’
d = “String”
print(“The
data type of the variable a is:”,type(a))
print(“The
data type of the variable b is:”,type(b))
print(“The
data type of the variable c is:”,type(c))
print(“The
data type of the variable d is:”,type(d))
For eg, if you are using a variable in ‘int’ datatype, but the output needs in float.
a = 12
b = 5
c = a / b
print(“The value is”,c)
Next, explicit type conversion in python is given below…..
This can be
done by using keywords.
- · int()
- · float()
- · str()
Converting
int to float :
It is the
most used conversion. It mainly used in mathematical expressions.
Eg:
a = 12
b = 23.6
c = float(a) + b
print(“The sum is:”,c)
Converting
float to int:
This can be
done by int() method.
Eg:
a = 3.45
b = int(a)
print(“The
converted int value is:”,b)
Converting int, float numbers to string is not that much applicable.