PYTHON DATA TYPE CONVERSION
TYPE CONVERSION IN PYTHON
• Python defines type conversion functions to directly convert one data type
to another which is useful in day-to-day and competitive programming.
• There are two types of Type Conversion in Python:
1. Python Implicit Type Conversion
2. Python Explicit Type Conversion
TYPE CONVERSION IN PYTHON
• The act of changing an object’s data type is known as type conversion.
• The Python interpreter automatically performs Implicit Type Conversion.
• Python prevents Implicit Type Conversion from losing data.
• The user converts the data types of objects using specified functions in
explicit type conversion, sometimes referred to as type casting.
• When type casting, data loss could happen if the object is forced to conform
to a particular data type.
IMPLICIT TYPE CONVERSION IN
PYTHON
• In Implicit type conversion of data types in Python, the Python interpreter
automatically converts one data type to another without any user
involvement.
• To get a more clear view of the topic see the below examples.
• Example
• As we can see the data type of ‘z’ got automatically changed to the
“float” type while one variable x is of integer type while the other
variable y is of float type.
• The reason for the float value not being converted into an integer
instead is due to type promotion that allows performing operations by
converting data into a wider-sized data type without any loss of
information.
• This is a simple case of Implicit type conversion in Python.
Example :
x = 10
print("x is of type:",type(x))
y = 10.6
print("y is of type:",type(y))
z=x+y
print(z)
print("z is of type:",type(z))
OUTPUT
x is of type: <class 'int'>
y is of type: <class 'float’>
20.6
z is of type: <class 'float'>
EXPLICIT TYPE CONVERSION IN
PYTHON
• In Explicit Type Conversion in Python, the data type is manually changed by
the user as per their requirement.
• With explicit type conversion, there is a risk of data loss since we are
forcing an expression to be changed in some specific data type.
• Various forms of explicit type conversion are explained as follows:
CONVERTING INTEGER TO FLOAT
• int(a, base):
• This function converts any data type to an integer.
• ‘Base’ specifies the base in which the string is if the data type is a
string.
• float():
• This function is used to convert any data type to a floating-
point number.
• Exmaple :
# initializing string
s = "10010"
# printing string converting to int base 2
c = int(s,2)
print ("After converting to integer base 2 : ", end="")
print (c)
# printing string converting to float
e = float(s)
print ("After converting to float : ", end="")
print (e)
• OUTPUT :
• After converting to integer base 2 : 18
• After converting to float : 10010.0
PYTHON TYPE CONVERSION USING
ORD(), HEX(), OCT()
• ord(): This function is used to convert a character to an
integer.
• hex(): This function is to convert an integer to a
hexadecimal string.
• oct(): This function is to convert an integer to an octal string.
• Example :
# initializing integer
s = '4’
# printing character converting to integer
c = ord(s)
print ("After converting character to integer : ",end="")
print (c)
# printing integer converting to hexadecimal string
c = hex(56)
print ("After converting 56 to hexadecimal string : ",end="")
print (c)
# printing integer converting to octal string
c = oct(56)
print ("After converting 56 to octal string : ",end="")
print (c)
• Output:
After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
PYTHON TYPE CONVERSION USING
TUPLE(), SET(), LIST()
• tuple(): This function is used to convert to a tuple.
• set(): This function returns the type after converting to set.
• list(): This function is used to convert any data type to a list type.
• Example :
# initializing string
s = ‘Welcome'
# printing string converting to tuple
c = tuple(s)
print ("After converting string to tuple : ",end="")
print (c)
# printing string converting to set
c = set(s)
print ("After converting string to set : ",end="")
print (c)
# printing string converting to list
c = list(s)
print ("After converting string to list : ",end="")
print (c)
OUPUT
• After converting string to tuple : (‘W', 'e', ‘l', ‘c', ‘o’, ‘m’, ’e’)
• After converting string to set : {'c', 'l', 'W', 'o', 'm', 'e'}
• After converting string to list : [‘W', 'e', ‘l', ‘c', ‘o’, ‘m’, ’e’]
PYTHON CODE TO DEMONSTRATE TYPE
CONVERSION USING DICT(), COMPLEX(), STR()
• dict(): This function is used to convert a tuple of order (key, value) into a
dictionary.
• str(): Used to convert an integer into a string.
• complex(real,imag) : This function converts real numbers to
complex(real,imag) number.
• # initializing integers
• a=1
• b=2
• # initializing tuple
• tup = (('a', 1) ,('f', 2), ('g', 3))
• # printing integer converting to complex number
• c = complex(a,b)
• print ("After converting integer to complex number : ",end="")
• print (c)
• # printing integer converting to string
• c = str(a)
• print ("After converting integer to string : ",end="")
• print (c)
• # printing tuple converting to expression dictionary
• c = dict(tup)
• print ("After converting tuple to dictionary : ",end="")
OUTPUT:
• After converting integer to complex number : (1+2j)
• After converting integer to string : 1
• After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}