Python type conversion

In python we can convert variables from one type to another. The type conversion can be implicit or explicit. Implicit type conversion occurs automatically by python, while explicit type conversion requires conversion functions.

Implicit type conversion.

To understand this conversion lets take an example of adding two numbers, one of integer and another of float. The result will be float.

x = 5    # integer value
y = 8.5  # float value
z = x+y  # added will be also float

print("z=",z)
print("type of z=",type(z))

# another example
a = 5.5  # float value 
b = 4+7j # complex value
c = a + b # result will be complex
print("c=",c)
print("type of c=",type(c))
z= 13.5
type of z= <class 'float'>
c= (9.5+7j)
type of c= <class 'complex'>

As you can see python language automatically converted integer to float when adding and float to complex. This type of conversion is termed as explicit.

However if we try to add other non numeric value with a numeric value(say a string to an integer), the interpreter will through error message. The way to achieve this solution is via explicit type conversion.

Explicit type conversion

In this type of conversion we use various conversion function. Following is a list of some functions.

int()converts to integer value, e.g int(4.5) or int(“30”)
float()converts to float value. e.g float(5) or float(“30.33”)
complex()converts to complex number, e.g. complex(5)
str()converts to string , e.g str(50.5)
bool()converts a value to boolean value, e.g. bool(3) , note: if the value is empty it return False else it returns True.

 Example:

a = 4 # integer 
b = "33" # string 
result = a + int(b)
print("result = ",result," type = ",type(result))

c = "Score is "
d = 3
r = c + str(3)
print("r = ",r," type = ",type(r))
result =  37  type =  <class 'int'>
r =  Score is 3  type =  <class 'str'>

Note: while we can convert string of 30 to int we can not directly convert string of 30.33 to int we first have to convert to float and then to int.For example. 

>>> int("33.33") # will throw error
Traceback (most recent call last):
  File "d:\PythonSparkContent\typeconversion.py", line 17, in <module>
    int("33.33") # will throw error
ValueError: invalid literal for int() with base 10: '33.33'

To make this work we first have to convert string to float value and then to integer value like this

>>> int(float("33.33"))

Other type conversion

Apart from numbers and strings we can also convert sequence types of similar items. For example converting a list to sets, list to tuple and tuple to sets and vice versa. 

list()converts to list. e.g. list({1,2,3}) or list((2,3,4))
tuple()converts to tuple e.g. tuple([3,4,5])
set()converts to set e.g. set((2,3,4))

For example:

l = [1,2,3]  # list datatype
t  = (5,6,7) # tuple datatype
s = {8,9,10}  # set datatype

tuple_to_list = list(t) 
print("tuple_to_list=",tuple_to_list, "type =",type(tuple_to_list))

set_to_list = list(s)
print("set_to_list=",set_to_list, "type =",type(set_to_list))

list_to_tuple = tuple(l) 
print("tuple_to_list=",tuple_to_list, "type =",type(list_to_tuple))

set_to_tuple = tuple(s)
print("set_to_tuple=",set_to_tuple, "type =",type(set_to_tuple))

list_to_set = set(l) 
print("list_to_set=",list_to_set, "type =",type(list_to_set))

tuple_to_set = set(t)
print("tuple_to_set=",tuple_to_set, "type =",type(tuple_to_set))
tuple_to_list= [5, 6, 7] type = <class 'list'>
set_to_list= [8, 9, 10] type = <class 'list'>
tuple_to_list= [5, 6, 7] type = <class 'tuple'>
set_to_tuple= (8, 9, 10) type = <class 'tuple'>
list_to_set= {1, 2, 3} type = <class 'set'>
tuple_to_set= {5, 6, 7} type = <class 'set'>

Leave a Reply

Your email address will not be published. Required fields are marked *