To convert a number from base 10 to base 2 in python, you can use the built-in bin()
function. The bin()
function takes an integer argument and returns its binary representation as a string.
Here's an example:
main.py118 chars4 lines
The '0b'
at the beginning of the string indicates that the number is in binary format.
Note: The bin()
function works for integers only. If you want to convert a float or a string representation of a number to binary, you'll need to convert it to an integer first.
gistlibby LogSnag