Python Conversion Between Data Types

We can convert between different data types by using different type conversion functions like int(), float(), str() etc. >>>float(10)
10.0
>>>int(-50.5)
-50



#Conversion to and from string must contain compatible values.
>>>float(10)
10.0
>>>int(-50.5)
-50
>>>float('5.6')
5.6
>>>str(56)
'56'
>>>int('5p')
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
int('5p')
ValueError: invalid literal for int() with base 10: '5p'

#We can even convert one sequence to another.
>>>set([10,20,30])
{10, 20, 30}
>>>tuple({40,50,60})
(40, 50, 60)
>>>list('SILAN')
['S', 'I', 'L', 'A', 'N']

#To convert to dictionary, each element must be a pair
>>>dict([[10,20],[30,40]])
{10: 20, 30: 40}
>>>dict([(10,20),(30,40)])
{10: 20, 30: 40}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext