❮ Previous Next ❯

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 datatypes

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}


❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved