![]() |
python type casting |
PYTHON | Type Casting
Python Type casting specify the data type of variable .
Type casting is when you assign a value of one primitive data type to another type .
There are two type of type casting
1) Implicit type casting
In which, python converts the data type into another data type automatically. In this type of casting user can't involve
Example
a = 7
b = 7.0
c = '7'
print(Type(a)) # python Automatically convert 'a' to Integer
print(Type(b)) # python Automatically convert 'a' to Float
print(Type(c)) # python Automatically convert 'a' to String
In Which , User has involvement and change the variable data type into another data type
int() : int () is convert float or string to Integer data type
float() : float() is convert int or string to Float data type
str() : str() is convert int or float to string data type
Example - 1
a = 5
b = float(a) # Return 5.0 , means a is convert Int to Float
Example - 2
a = 5.9
b = int(a) # Return 5 , means a is convert Float to Int
Example - 3
a = 5.9
b = str(a) # Return '5.9' , means a is convert Float to String