Type Casting
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Typecast
- Implicit Type casting
- Explicit Type casting
Implicit Type Conversion
a = 10 # a is an integer
b = 10.1 # b is a float
print(a)
print(b)10
10.1
print(type(a))
print(type(b))<class 'int'>
<class 'float'>
# Lets add the two numbers variable a and variable b
# and save the output as a new variable c
c = a+b
print(c)20.1
print(type(c))<class 'float'>
As you have seen, the addition of 10 and 10.1 is a decimal (float) 20.1
Of course, it should be 20.1, a float value, right. Actually, its slightly more complicated than one might think. 10 is an integer and 10.1 is a float data type.
To be honest, we have different data types for a reason. By default we can not add one type of data type to another one. But because addding up integers and floats outputs a decimal, python first does a type conversion of integer to float.
So, python first converts the data type of variable a (integer 10) to float and then the two float values (a+b) are added, so the result comes as a data type of float. This process when the type conversion is done automatically by python is called implicit type casting.
Some little details that we should we aware of, right. Not convinced yet? Let's look at more challenging example.
# This is supposed to throw an error as we will attempt
# to add a string to a float
# a = "10"
# b = 10.1
# The below statement will throw an error
# c = a+b
# print(c)The value of string "10" looks like its a number, so intuitively we might think that we can add this string "10" to a float 10.1 but like you noticed, these two variables had different data type. A string data type can not be added to a float (or integer) data type.
But because we know that the value of the string is "10", we can convert this string of value "10" to an integer first and this sort of type conversion is called explicit type conversion
print(a)10
print(type(a))<class 'str'>
Here we'll use the int() inbuilt python function to explicitly convert the data type of variable a from string to integer.
d = int(a)
print(d)
print(type(d))10
<class 'int'>
Now, we are ready to add the integer variable d and float variable b.
print(d + b)20.1
Again, one implicit type conversion happened, which first converted data type of variable d to float before adding variable d to variable b.
Note: Type casting should be used with caution as not all strings could be converted into integers or floats.
e = "2 apples"
print(e)
print(type(e))2 apples
<class 'str'>
Because our variable e contains characters now, the whole string can not be converted to integer.
# The following code will give an error
# int(e)Type conversion of booleans
f = True
g = FalseRemember, True boolean values are stored as 1 in the backend of python or any programming language.
print(int(f))1
False boolean values are stored as 0 in the backend of python.
print(int(g))0
Because booleans and numerics are so related to each other, we can use type conversion between booleans and numeric values.
bool(1)Truebool(0)FalseActually, if we convert any numeric value other than 0 to boolean, python will be able to convert (Type cast) it into True boolean.
If the number is zero, then only python converts it into False.
bool(1.1)Truebool(0.0)Falsebool(0.1)TrueEven the negative numbers are True boolean
bool(-10)Truebool(-10.33)True-0 and 0 means the same thing, so these are two of the only 3 cases where False comes as output
bool(-0)FalseNone data type means a null value, a value that does not exist. So, if there's nothing, upon type conversion python converts that into False .
bool(None)FalseFinished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →