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
- At the start, we use
def binaryToDecimal(n):where thedefkeyword is used to define a function and thebinaryToDecimalis used to call the function to get the value of the variablen. - After that we return the integer value of the variable
ninint(n,2)using thereturnfunction . - 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. - Using the
binaryToDecimalfunction we find the Decimal form of the binary value assigned in the code using the built in function, we display the final values using theprintfunction.
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
- At the start, we use
def decimalToBinary(n):where thedefkeyword is used to define a function and thedecimalToBinaryis used to call the function to get the value of the variablen. - After that we return the Binary value of the variable
nusing the functionreturnfollowed by thereplacefunction which is used to replace the values("0b","")with another value. - 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. - Using the
decimalToBinaryfunction we find the Binary form of the Decimal value assigned in the code using the built in function, we display the final values using theprintfunction.
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.