Type conversion in Python

In this module, we will learn about Type conversion in Python. Type conversion refers to the various inbuilt functions we could use in Python to directly convert from one data-type to another data-type.

int(a,base) :

Converts the given data type to an integer. base represent its base of number when it is in string/character datatype. We can also use int(a) without number as shown in the example.

a="1010" #string
int(a,2) #return 10
int(a).  #return 1010

int(a,2) is returning 10 because we have provided the function with detail that initially this string in base 2, so this is converted in the decimal format before getting returned.

str() 

Converts the given datatype to string. If integers are converted to a string, then arithmetic operations cannot be performed on them.

a=1054
b=str(a)  #return '1054'
c=b*2    #will give error because arithmetic operations can only be performed in int datatype.

float()

Converts the given datatype to floating-point.

a=53
float(a) #return 53.0
a/1      #return 53.0
  • If non-decimal integers are converted to float it will add .0(point zero) at the end, this will create problems while arithmetic calculations.
  • While performing calculations divide (/) will always return to floating-point.

ord()

Converts character (or string with single-digit) to its Unicode code point when an argument is a unique object.

a='5'  #only character
ord(a) #return 53

hex()

Converts the given integer to its hexadecimal (base 16) number.

a=53
hex(53)  #return 0x35

Learn: Convert float to hex in Python

oct()

Converts the given integer to octal (base 8) number.

a=53
oct(a)    #return 0o65

tuple()

Converts given set to a tuple in Python. Tuples are sequences the same as lists. But the difference between them is that tuple can not be changed whereas lists can.

string = "codespeedy"
tuple(string)     #return ('c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y')
  • Tuple by default is defined/recognized by “( )” brackets. 

set()

Converts to set in python.

string="codespeedy"
set(string)    #return {'c', 'd', 'e', 'o', 'p', 's', 'y'}   In this order is rearranged.
sorted(set(string),key=string.index)     #return ['c', 'o', 'd', 'e', 's', 'p', 'y']    We can use by this way, for no rearranging.
  • Set is defined/recognised by “{ }” (curly) brackets.

list()

 Converts in the list.

list("codespeedy")    #return ['c', 'o', 'd', 'e', 's', 'p', 'e', 'e', 'd', 'y']
  • List is defined/recognized by “[ ]” square brackets.

dict()

Converts tuple of order(key,value) into dictionary.

tup = (('a',2),('b',3),('c',4))
dict(tup)        #return {'a': 2, 'b': 3, 'c': 4}
  • Dictionaries are defined/recognized by “{ }”curly brackets.

complex(real,imaginary)

Converts real numbers to complex format or brings the context in a+ib format when a and b are the given inputs.

complex(2,3)       #return (2+3j)

 

This is how various functions to convert various datatypes works in Python.

Leave a Reply

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