Python Code Example: convert decimal to binary number

The code defines a function decToBin(x) which takes a decimal number x as its input and returns the binary equivalent of that number. The function uses the built-in bin() function to convert the decimal number to binary. The bin() function returns a string representation of the binary number, including the prefix ‘0b’.

The function then uses the int() function to convert the string representation of the binary number to an integer, and uses string slicing to remove the ‘0b’ prefix.

The code then calls the decToBin function with the decimal number 22 as the input and prints the result. The output should be “10110”.

def decToBin(x):
    return int(bin(x)[2:])

print(decToBin(22))
Output
10110