** 1. int (a, base) **: This function will convert ** any data type to an integer **. Base indicates ** the base the string is in ** if the data type is string.

** 2. float () **: This function is used to convert ** any data type to a floating point number. **,

Output:

After converting to integer base 2: 18 After converting to float: 10010.0

** 3 ... ord (): ** this function is used to convert a ** character to an integer. **

** 4. hex (): ** this function is for converting a ** integer to a hexadecimal string **.

** 5. oct (): ** this function converts a ** integer to an octal string **.

` ` |

` ` |

Exit :

After converting character to integer: 52 After converting 56 to hexadecimal string: 0x38 After converting 56 to octal string: 0o70

** 6. tuple (): ** this function is used to ** convert to tuple **.

** 7. set (): ** this function returns the ** type after conversion to set **.

** 8. list (): ** this function is used to convert ** any data type to a list type **.

` `

` ` |

Output:

After converting string to tuple: (`g`,` e`, `e`,` k`, `s`) After converting string to set: {` k`, `e`,` s`, `g`} After converting string to list: [` g`, `e`,` e`, `k`,` s`]

** 9. dict (): ** this function is used to ** convert an order tuple (key, value) into a dictionary **.

** 10. str (): ** is used to ** convert an integer to a string. **

** 11. complex (real, imag) :: ** this function ** converts real numbers to complex (real, imag) numbers. **

` `

` ` ` # Python code to demonstrate type conversion `

` # using dict (), complex (), str () `

` # initializing integers `

` a `` = `

` 1 `

` ` ` b `

` = `

` 2 `

` # tuple initialization `

` tup `

` = `

` ((`

`` a` `

`, `

` 1 `

` ), (`

` `f` `

`, `

` 2 `

`), (`

` `g` `

`, `

` 3 `

`)) `

` # output an integer to a complex number `

` c `

` = `

` complex `

` (`

` 1 `

`, `

` 2 `

`) `

` print `

` (`

` "After converting integer to complex number:" `

`, end `

` = `

` "") `

` print `

` (c) `

` # output an integer to a string `

` c `

` = `

` str `

` (a) `

` print `

` (`

`" After converting integer to string: "`

`, end `

` = `

` "") `

` print `

` (c) `

` # printing tuple-to-expression dictionary conversion `

` c `

` = `

` dict `

` (tup) `

` print `

` (`

` " After converting tuple to dictionary: "`

`, end `

` = `

`" ") `

` print `

` (c) `

` `

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}

This article courtesy of ** Manjit Singh (S. Nandini) **. If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

X
# Submit new EBook