< Previous Page | Home Page | Next Page >
In the followig section, we'll have a quick look at type conversion and type casting.
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion.
In Implicit type conversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement. Let's see an example where Python promotes conversion of lower datatype (integer) to higher data type (float) to avoid data loss.
Example : Converting integer to float
num_int = 123
num_flo = 1.23
num_new = num_int + num_flo
print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))
print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
In the above code,
num_int
and num_flo
, storing the value in num_new
.num_int
is an integer
, datatype of num_flo
is a float
.num_new
has float
data type because Python always converts smaller data type to larger data type to avoid the loss of data.Now, let's try adding a string and an integer, and see how Python treats it.
Example: Addition of string(higher) data type and integer(lower) datatype
num_int = 123
num_str = "456"
print("Data type of num_int:",type(num_int))
print("Data type of num_str:",type(num_str))
print(num_int+num_str)
In the above program,
num_int
and num_str
.typeerror
. Python is not able use Implicit Conversion in such condition.In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like int()
, float()
, str()
, etc to perform explicit type conversion.
This type conversion is also called typecasting because the user casts (change) the data type of the objects.
Syntax :
(required\_datatype)(expression)
Typecasting can be done by assigning the required data type function to the expression.
Example: Addition of string and integer using explicit conversion
num_int = 123
num_str = "456"
print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
In above program,
num_str
and num_int
variable.num_str
from string(higher) to integer(lower) type using int()
function to perform the addition.num_str
to a integer value Python is able to add these two variable.num_sum
value and data type to be integer.When programming we often want to make sure that a variable is only of a certain type. Hence, we make use of two built-in functions (type()
and isinstance()
) to check the data type of a variable.
#Find out what data type a certain variable is
a = "Python"
b = 1.2
c = {1,-2,3}
print(type(a)) # result -> str
print(type(b)) # result -> float
print(type(c)) # result -> set
#Check if a variable is of a certain data type
isinstance(a, str) #Is variable a of type string? Result -> True
isinstance(b, int) #Result -> False
#The function isinstance() allows us to check for multiple data types, i.e. we can check if a variable is either int or float.
# The code looks as follows:
myVar = 4.4 # this is a float
isinstance(myVar, (int, float))
myVar = 4 # this is an integer
isinstance(myVar, (int, float))
< Previous Page | Home Page | Next Page >