convert a number from base 10 to base 2 in python

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.py
num = 42  # decimal number
binary_num = bin(num)  # binary representation as a string
print(binary_num)  # '0b101010'
118 chars
4 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