Type Conversion in Python


 Type Conversion

          The process of converting the value of one data type into another data type is called type conversion.

           There are mainly two types of type conversion methods in Python :

1. Implicit type conversion :

       When the data type conversion takes place during compilation or during the run time, then is called an implicit type conversion. Python handles the implicit type conversion, so we do not have to explicitly convert the data type into another data type.
        In implicit type conversion Python automatically converts any data type to another data type. This process does not need any user involvement.
          
Python promotes conversion to lower data type to higher data type.

Example 

a=5

b=5.5

sum=a+b

print(type(a))

print(type(b))

print(sum)

print(type(sum))

Output

<class 'int'>

<class 'float'>

10.5

<class 'float'>

In the given example, we have taken two variables of integer and float data types and added them.Further, we have declared another variable named 'sum' and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion.

2. Explicit type conversion :

    Explicit type conversion is also known as type casting. Explicit type conversion takes place when the programmer clearly and explicitly defines the variables in the program.
Python provides type conversion functions to directly convert one data type to another data type. Using Python we can easily convert data into different data types. There are various functions for type conversion.

Converting string to numeric types

    To convert from string type objects to numeric objects, there are different methods like int(), float() etc. Using the int() method we can convert any number as string to integer value (base 10). It takes the string type arguments default base is 10. We can also specify the base to convert from a string of that base to decimal number.
Similarly using the float() method one string which contains value in decimal form, can be converted as float.

Example

# Initialising string

s="10010"

# printing string converting to int base 2

c=int(s,2)

print(c)

# printing string converting to float

e=float(s)

print(e)

Output

18

10010.0


Note : In Python there is complex number class .So using this we can convert two integers (real and imaginary part) to complex numbers.

Example 

my_complex=complex(10,5)

# convert to complex number

print(my_complex)

Output

(10+5j)

Converting Containers

      In Python there are different container types objects like list, tuple, set etc. We can change one type of containers to another type of containers using list(), tuple(), set() etc.

Example

list=[10,20,30,40,50]

set={10,10,20,30,20,50,20}

# converting list to tuple

print(tuple(list))

# converting list to set

print(set(list))

# converting set to list

print(list(set))

Output

(10,20,30,40,50)

{40,10,50,20,30}             # order of elements may be changed

[10,20,50,30]

Converting tuple to dictionary

     Tuple is one of the most important container in Python. In Python, we can convert tuple with objects with two values to dictionary objects. The dict() method can do the conversion.

Example

tuple=(("technical",1),("gupta",2),("ji",3),("tutorial",4))

my_dict=dict(tuple)

print(my_dict)

Output

{'technical': 1, 'gupta': 2, 'ji': 3, 'tutorial': 4}







No comments:

Post a Comment