1# Python program to convert decimal to binary
2
3# Function to convert Decimal number
4# to Binary number
5def decimalToBinary(n):
6 return bin(n).replace("0b", "")
7
8# Driver code
9if __name__ == '__main__':
10 print(decimalToBinary(8))
11 print(decimalToBinary(18))
12 print(decimalToBinary(7))
13
14Output:
151000
161001
1def convert_to_binary(number:int):
2 if number == None:
3 return "Invalid input"
4 elif type(number) == float:
5 return "Float is not Handled"
6 return format(number, "010b")
7
8print(convert_to_binary(None))
9print(convert_to_binary(100))
10print(convert_to_binary(6.5))
1======= Convert Decimal to Binary in Python ========
2my_int = 10;
3#Method 1: using bin
4n1 = bin(my_int).replace("0b", "") #1010
5or n1 = bin(my_int)[2:]
6
7#Method 2: using format
8n2 = "{0:b}".format(my_int)
9or n2 = format(my_int, 'b') #1010
10