HomePythonPython Program to Convert Binary Number to Decimal and vice-versa

Python Program to Convert Binary Number to Decimal and vice-versa

In this python tutorial, you will learn how to Convert Binary Number to Decimal and vice-versa with the if statement and built in functions of the python programming language.

How to Convert Binary Number to Decimal and vice-versa?

Let’s take a look at the first source code, here the values are assigned in the code, the if statement and built in functions carry out the function.

BINARY TO DECIMAL

RUN CODE SNIPPET
#Python Program to Convert Binary Number to Decimal 

def binaryToDecimal(n):
    return int(n,2)

if __name__ == '__main__':
    print(binaryToDecimal('110'))
    print(binaryToDecimal('10000'))
    print(binaryToDecimal('101'))

OUTPUT:

6
16
5
  1. At the start, we use def binaryToDecimal(n): where the def keyword is used to define a function and the binaryToDecimal  is used to call the function to get the value of the variable n.
  2. After that we return the integer value of the variable n  in int(n,2) using the return function .
  3. The code if __name__ == '__main__': is used to execute some code only if the file was run directly, and not imported, with the condition satisfied it moves to the next step.
  4. Using the binaryToDecimal function we find the Decimal form of the binary value assigned in the code using the built in function, we display the final values using the print function.

Let’s take a look at the first second code , here the values are assigned in the code, the if statement and built in functions carry out the function.

DECIMAL TO BINARY

RUN CODE SNIPPET
# Python Program to Convert Decimal Number to Binary 

def decimalToBinary(n):
    return bin(n).replace("0b","")

if __name__ == '__main__':
    print(decimalToBinary(6))
    print(decimalToBinary(16))
    print(decimalToBinary(5))

OUTPUT:

110
10000
101
  1. At the start, we use def decimalToBinary(n): where the def keyword is used to define a function and the decimalToBinary  is used to call the function to get the value of the variable n.
  2. After that we return the Binary value of the variable n using the function return followed by the replace function which is used to replace the values ("0b","") with another value.
  3. The code if __name__ == '__main__': is used to execute some code only if the file was run directly, and not imported, with the condition satisfied it moves to the next step.
  4. Using the decimalToBinary function we find the Binary form of the Decimal value assigned in the code using the built in function, we display the final values using the print function.

NOTE:

  • The replace() function is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring.
  • The if  statement evaluates whether an expression is true. If a condition is true, the “if” statement executes.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The bin() function  is used to convert from a decimal value to its corresponding binary value.
  •  The int() function to convert a binary to its decimal value.
  • The print() function prints the specified message to the screen, or other standard output device.

Leave a Reply

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...