Wednesday 29 January 2014

Digital conversion using Python

Python can be used to solve a number of mathematical functions. This post will cover some basic in-built Python functions for digital conversion. Let's see some examples.

To convert an integer into a binary string, use the function bin(x)

Eg:

>>> bin(10)
'0b1010'

To convert an integer into a floating number, use the function float(x)

Eg:

>>> float(15)
15.0

To convert an integer into a hexadecimal string, use the function hex(x)

Eg:

>>> hex(51)
'0x33'

To convert a floating point number or decimal number to an integer, use the function int(x)

Eg:

>>> int(8.94943)
8

To convert an integer into an octal string, use the function oct(x)

Eg:

>>> oct(65)
'0101'

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker