Join us

Python Type Conversion Explained

Python Type Conversion Explained.png

Type conversion is the process of converting one data type into another data type. And type conversion is widely used in the field of programming. In python type conversions are of two types.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Now let us understand both type conversion in detail

Implicit Type Conversion

Implicit type conversion is an automatic type conversion which can be automatically done by the interpreter and it does not require any involvement of the user. In implicit type conversion, the interpreter converts the data type into any other data type. Implicit type conversion is a type of conversion which takes place at the time of program compilation or running.

Users are not required to perform any explicit conversion in implicit type conversion it will be automatically handled by python. Implicit type conversion converts smaller data types into bigger data types to avoid any data loss. Now let us understand implicit type conversion with an example.

Example:

Let us look at the python program for adding two numbers of different data types and then checking the data type of the variable in which the result is stored. In the below program, we have used the type() method of python for checking the data type of the variable.

# python program to demonstrate implicit type conversion

a = 9

print(type(a),"is a data type of variable a")

b = 9.9

print(type(b),"is a data type of variable b")

add = a + b

print("sum of a and b is:",add)

print(type(add),"is a data type of sum of a and b")

Output

<class 'int'> is a data type of variable a

<class 'float'> is a data type of variable b

sum of a and b is: 18.9

<class 'float'> is a data type of sum of a and b

In the above lines of code, we have taken two variables and stored integer value in one variable and in another variable we have stored float value. And after that, we declared another variable named 'add'. In which we have stored the sum of two previously declared variables. And then we check the type of add variable and print the type then we observed that the type of add variable is automatically converted into float type. This is what we call implicit type conversion.

The reason behind converting the type into float type, not in Integer is that if the type of add will be converted into integer then the fractional part of float will be lost. So python performs implicit type conversion without any loss of data. So it will convert the smaller type into a larger data type that's why float and integer value when added is converted into float type for preventing any data loss.

But in some cases, implicit type conversion is not sufficient. We are required to perform conversion according to your requirement then we will use explicit type conversion in these scenarios. There maybe your program contains recursions as well, in that case implicit conversion wont be able to do it.

Explicit Type Conversion

Manual conversion of data type by the user according to its need is referred to as explicit type conversion in python. As we are forced to convert data type in explicit type conversion so there is a chance of loss of data in this conversion.

An explanation of different types of explicit type conversion is given below:

1. int(a, base):

This method is used for converting any type into an integer data type. And here ‘a’ specifies the string or number which is to be converted into an integer data type. And ‘base’ specifies the number format of the currently specified value.

Example:

# python program for demonstrating the use of the int() method of python

# declaring a string

s = “010101”

# converting base 2 string value into an integer

a = int(s,2)

print("Converting base 2 value into integer value:",a)

# converting octal string value (base 8) into an integer value

a = int("0o16", 8)

print("Integer conversion of 0o16 is:", a)

Output:

Converting base 2 value into integer value: 21

Integer conversion of 0o16 is: 14

2. float()

This method is used for converting any type into a float data type.

Example:

# python program for demonstrating the use of the int() method of python

# declaring a string

s = “010101”

# converting string value to float type value

a = float(s)

print("float conversion of the string is:",a)

Output:

float conversion of the string is: 10101.0

3. ord()

This method is used for converting character type value into integer data type

Example:

# python program for demonstrating the use of the ord() method of python

# declaring a character

s = ‘a’

# converting character value into integer value using ord()

a = ord(s)

print("the integer value of character ‘a’ is:",a)

Output:

the integer value of character ‘a’ is: 97

4. hex()

This method is used for converting integer data type value into a hexadecimal string.

5. oct()

This method is used for converting integer data type value into an octal string.

Example

# python program for demonstrating the use of hex() and oct() method of python

# declaring an integer value

a = 56

# converting integer value into a hexadecimal string using hex()

b = hex(a)

print("hexadecimal conversion of 56 is:",b)

# converting integer value into an octal string using oct()

c = oct(a)

print("octal conversion of 56 is:",c)

Output:

hexadecimal conversion of 56 is: 0x38

octal conversion of 56 is: 0o70

6. tuple()

This method is used to make a tuple from other data types.

Example:

# python program for demonstrating the use of the tuple() method of python

# declaring a string

s = ‘welcome!’

# converting string to a tuple using the tuple() method

t = tuple(s)

print("tuple conversion of the string is:",t)

Output

tuple conversion of the string is: ('w', 'e', 'l', 'c', 'o', 'm', 'e', '!')

7. set()

This method is used to make a set.

Example:

# python program for demonstrating the use of the set() method of python

# declaring a string

s = ‘welcome!’

# converting string to set using set() method

s1 = set(s)

print("set conversion of string is:",s1)

Output

set conversion of string is: {'e', 'o', 'w', '!', 'm', 'l', 'c'}

8. list()

This method is used to make a list from other data types

Example:

# python program for demonstrating the use of the list() method of python

# declaring a string

s = ‘welcome!’

# converting string to the list using the list() method

s1 = list(s)

print("list conversion of the string is:",s1)

Output:

list conversion of the string is: ['w', 'e', 'l', 'c', 'o', 'm', 'e', '!']

9. dict()

This method is used for creating the dictionary.

Example:

# python program for demonstrating the use of the dict() method of python

# declaring a tuple

t = (('a', 1) ,('f', 2), ('g', 3))

# converting tuple to dict using dict() method

d = dict(t)

print("dictionary conversion of the tuple is:",d)

Output:

dictionary conversion of the tuple is: {'a': 1, 'f': 2, 'g': 3}

10. str()

This method is used for converting the integer type value into a string.

Example:

# python program for demonstrating the use of the str() method of python

# declaring a variable

a = 5

# converting integer to string using str() method

s = str(a)

print("string conversion of integer 5 is:",s)

Output:

string conversion of integer 5 is: 5

11. complex(real,imag)

This method is used for converting into complex numbers and here real specifies the real part of the complex number and imag specifies the imaginary part of the complex number.

Example:

# python program for demonstrating the use of complex() method of python

# creating a complex number using the complex() method

a = complex(1,2)

print("The complex number is:",a)

Output:

The complex number is: (1+2j)

12. chr(number)

This method is used for converting integer values into their respective ASCII character value.

Example:

# python program for demonstrating the use of the chr() method of python

# declaring a variable

a = 88

b = 95

# printing ASCII value of a using chr()

print("ASCII value of 88 is",chr(a))

print("ASCII value of 95 is",chr(b))

Output:

ASCII value of 88 is X

ASCII value of 95 is _

Conclusion

  • Type conversion is the process of converting one data type into another data type.
  • Implicit and Explicit are two types of type conversion.
  • Implicit type conversion is an automatic conversion which is done by python without any involvement of user.
  • Explicit type conversion is converting the data type according to the user requirement.
  • Type conversion is the process of converting one data type into another data type.
  • Implicit and Explicit are two types of type conversion.
  • Implicit type conversion is an automatic conversion which is done by python without any involvement of user.
  • Explicit type conversion is converting the data type according to the user requirement.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

Shivam Bhatele

Software Developer, Scaler

@shivambhatele80
I am a Software Developer and I loved to share programming knowledge and interact with new people. My hobbies are coding, reading books, and learning new things.
User Popularity
135

Influence

12k

Total Hits

8

Posts