Data type conversion

Data type conversion in Python

Python provides predefined functions for Data type conversion in Python. We can use these functions to convert value of one data type to another. Different functions for Data type conversion in Python are:

  • int()
  • float()
  • str()
  • oct()
  • hex()
  • complex()
  • ord()
  • tuple()
  • list()
  • dict()

1. int() function in Python

This function can convert a string floating point or boolean value to integer value.

Example 1:             

>>>var=”15″
>>>var1=int(var)
>>> print(var1)
15

In above example, variable named var contains data value “15”   (within double quotes). So it is actually a string value.

var1=int(var)  statement will convert string value “15” into integer value 15 (without double quotes). We can further perform calculations and manipulations on this integer value.

It should be noted that string variable should not refer to  data value containing characters other than digits.

Example 2:

>>>var=7.54
>>>var1=int(var)
>>> print(var1)
7

In above example, variable named var contains data value  7.54.  It is a floating point value.

var1=int(var)  statement will convert floating point  value 7.54  into integer value 7  after rejecting digits after decimal point.

Example 3:

>>>var=True
>>>var1=int(var)
>>> print(var1)
1

In above example, variable named var contains data value  True  which is a boolean value.

var1=int(var)  statement will convert boolean value True  into integer value 1. For  boolean value False, it will return 0.

2. float() function in Python

This function can covert a string , integer or boolean value to floating point value.

Example 1:

>>>var=”15.75″
>>>var1=float(var)
>>> print(var1)
15.75

In above example, variable named var refers to  data value “15.75″   (within double quotes). So it is actually a string value.

var1=float (var)  statement will convert string value “15.75” into floating point value 15,75 (without double quotes). We can further perform calculations and manipulations on this floating point value.

It should be noted that string variable should not refer to  data value containing characters other than digits and a decimal point.

Example 2:

>>>var=7
>>>var1=float(var)
>>> print(var1)
7.0

In above example, variable named var contains data value  7  which is an integer value.

var1=float(var)  statement will convert integer value 7 into floating  point value 7.0  after suffixing a decimal point followed by 0 .

Example 3:

>>>var=False
>>>var1=float(var)
>>> print(var1)
0.0

In above example, variable named var contains data value  False which is a boolean value.

var1=float(var)  statement will convert boolean value False into floating point  value  0.0.  For  boolean value True, it will return 1.0.

3. str() function in Python

This function can covert a integer,complex or boolean value to string value.

Example 1:

>>>var=15
>>>var1=str(var)
>>> print(var1)
15

In above example, variable named var contains data value  15 which is an integer value.

var1=str(var)  statement will convert integer value 15 into string value “15”.

Example 2:

>>>var=5,75
>>>var1=str(var)
>>> print(var1)
5

In above example, variable named var contains data value  5.75  which is a floating point value.

var1=str(var)  statement will convert floating point value 5.75 into string value “5”.

Example 3:

>>> var=True
>>> var1=str(var)
>>> print(var1)
True

In above example, variable named var contains data value  True  which is a boolean value.

var1=str(var)  statement will convert boolean value True into string value “True”.

Example 4:

>>> var=2+3j
>>> var1=str(var)
>>> print(var1)
(2+3j)

In above example, variable named var contains data value  2+3j  which is a complex value.

var1=str(var)  statement will convert complex value 2+3j  into string value “2+3j”.

4. oct() function in Python

This function can be used to find octal equivalent of  an integer value.

Example:

>>> var=10
>>> var1=oct(var)
>>> print(var1)
0o12

In above example, variable named var contains data value  10 which is an integer value.

var1=oct(var)  statement will find octal equivalent of value 10 i.e. 12 which is represented in Python as 0o12.

5. hex() function in Python

This function can be used to find hexadecimal  equivalent of  an integer value.

Example:

>>> var=10
>>> var1=hex(var)
>>> print(var1)
0xa

In above example, variable named var contains data value  10 which is an integer value.

var1=hex(var)  statement will find hexadecimal equivalent of value 10 i.e. a which is represented in Python  as 0xa. a is the hexadecimal equivalent of value 10.

6. complex() function in Python

This function can be used to convert real numbers to complex value.

Syntax of complex() function is  complex(a,b). where a and b are real values. The result of this function is a+bj.

Example:

>>> var=complex(2.5,3.5)
>>> print(var1)
2.5+3.5j

In above example, variable named var has been assigned function complex() as:

var=complex(2.5,3.5)

Value generated by this function is 2.5+3.5j.

7. ord() function in Python

This function is used to provide unicode of a character value. Character value must not  contain more than one symbol.

**Note: Unicode is the character set implemented by Python. It contain symbols including alphabets, digits, special characters, spaces and symbols from languages used across the globe.

Most commonly used unicode values are:

97 is the unicode of alphabet ‘a’ , 98 is the unicode of alphabet ‘b’ , 99 is the unicode of alphabet ‘c’ and so on.

65 is the unicode of alphabet ‘A’ , 66 is the unicode of alphabet ‘B’ , 67 is the unicode of alphabet ‘C’ and so on.

Example:

>>> var=’a’
>>> var1=ord(var)
>>> print(var1)
97

In above example, variable named var has been assigned alphabet ‘a’.

var1=ord(var)  statement finds unicode for alphabet ‘a’  which is 97.

8. tuple() function in Python

The function tuple() is used to convert a string value into a tuple, Each individual character of  string gets converted into elements of a tuple.

Example:

>>> var = “Lovejot”
>>> var1=tuple(var)
>>> print(var1)
(‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’)

In above example, variable named var is referencing the string value “Lovejot”.

var1=tuple(var)  statement converts the string value “Lovejot” into tuple (‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’). Where ‘L’ becomes first tuple element, ‘o’ becomes second tuple element, ‘v’ becomes third tuple element and so on.

9. list() function in Python

The function list() is used to convert a string value into a list, Each individual element of the string gets  converted into elements of a list .

Example:

>>> var = “Lovejot”
>>> var1=list(var)
>>> print(var1)
[‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’]

In above example, variable named var is referencing the string value “Lovejot”.

var1=list(var)  statement converts the string value “Lovejot” into list [‘L’, ‘o’, ‘v’, ‘e’, ‘j’, ‘o’, ‘t’]. Where ‘L’ becomes first list element, ‘o’ becomes second list element, ‘v’ becomes third list element and so on.

10. dict() function in Python

The function dict() is used to convert a tuple  organized in (key,value)  pair into a dictionary.

Example:

>>>t = ((‘a’, 1) ,(‘e’, 2), (‘i’, 3),(‘o’, 4),(‘u’,5))
>>> d = dict(tup)
>>> print(d)
{‘a’: 1, ‘e’: 2, ‘i’: 3, ‘o’: 4, ‘u’: 5}

In above example, variable tuple t contains values ((‘a’, 1) ,(‘e’, 2), (‘i’, 3),(‘o’, 4),(‘u’,5)) organized into key value pairs as (‘a’, 1) , (‘e’, 2), (‘i’, 3),(‘o’, 4) and (‘u’,5).

d=dict(t)  statement converts the tuple into dictionary {‘a’: 1, ‘e’: 2, ‘i’: 3, ‘o’: 4, ‘u’: 5}.

Leave a Reply

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