Given a decimal number I need to convert it into binaryGiven a binary number I need to convert it into decimalAfter the conversion I need to perform some operation on it (for example, addition). I also need to print the result with a specified width.Binary-Decimal:n1 = int(input(Enter a binary number: ))n2 = int(input(Enter a binary number: ))# type cast to 'int' is done to provide widthdecimalN1 = int({0:d}.format(n1), 2)decimalN2 = int({0:d}.format(n2), 2)decimalSum = int({0:d}.format(n1 + n2), 2)width = len(str(decimalSum))print(max width = {}.format(width))print ({0:0{3}} + {1:0{3}} = {2:0{3}}.format(decimalN1, decimalN2, decimalSum, width))print ({0} + {1} = {2}.format(type(decimalN1), type(decimalN2), type(decimalSum)))Decimal-Binary:n1 = int(input(Enter a decimal number: ))n2 = int(input(Enter a decimal number: ))# type cast to 'int' is done to provide widthbinaryN1 = int({0:b}.format(n1))binaryN2 = int({0:b}.format(n2))binarySum = int({0:b}.format(n1 + n2))width = (n1 + n2).bit_length()print(max width = {}.format(width))print ({0:0{3}} + {1:0{3}} = {2:0{3}}.format(binaryN1, binaryN2, binarySum, width))print ({0} + {1} = {2}.format(type(binaryN1), type(binaryN2), type(binarySum)))I want to know if there is any other (better) way of doing this. I know the bin() function could be used, but then it returns a string, so I cannot perform (integer) operation on it after the conversion.
Decimal to Binary Conversion and vice-versa in Python
python;beginner;python 3.x;number systems