# 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.

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.

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'>``````