In this python tutorial, you will learn how to Convert Binary Number to Octal and vice-versa with the built in functions of the python programming language.
How to Convert Binary Number to Octal and vice-versa?
Let’s take a look at the first source code,  here the values are given as input by the user in the code, the built in functions carries out the function.
BINARY TO OCTALÂ
RUN CODE SNIPPET#Python Program to Convert Binary number to Octal print("Enter a Binary Number: ", end="") bnum = input() onum = int(bnum, 2) onum = oct(onum) print("\nEquivalent Octal Value = ", onum[2:])
INPUT:
 110011101
OUTPUT:
Enter a Binary Number: Equivalent Octal Value = 635
- We declare a
print
function to display the statement("Enter a Binary Number: ", end="")
after which we declare the variablebnum = input()
, where we give the user the option to enter the values and the input values are scanned using theinput
function - In the STDIN section of the code editor the input values are entered.
- We declare the variable
onum
with the  functionint(bnum, 2)
where theint
function converts the string to an integer type value with base two, that is it converts it into a binary number. - We declare the variable
onum
with the  functionoct(onum)
where theoct
function converts the value of onum to its equivalent octal value. - Now, we display the output value using the
print
and display the statement/string("\nEquivalent Octal Value = ", onum[2:])
. The function[2:]
is added to skip the first two characters of the obtained octal number.
Let’s take a look at the second source code,  here the values are given as input by the user in the code, the built in functions carries out the function.
OCTAL TO BINARY
RUN CODE SNIPPET#Python Program to Convert Octal number to Binary print("Enter Octal Number: ", end="") onum = input() bnum = int(onum, 8) bnum = bin(bnum) print("\nEquivalent Binary Value =", bnum[2:])
INPUT:
635
OUTPUT:
Enter Octal Number: Equivalent Binary Value = 110011101
- We declare a
print
function to display the statement("Enter Octal Number: ", end="")
after which we declare the variableonum = input()
, where we give the user the option to enter the values and the input values are scanned using theinput
function - In the STDIN section of the code editor the input values are entered.
- We declare the variable
bnum
which is equal to the functionint(onum, 8)
, where theint
convert a value passed as its argument to its integer equivalent. - We declare the variable
bnum
which is equal to the functionbin(bnum)
, where thebin
returns binary equivalent of value of he stored value. - Now, we display the output value using the
print
and display the statement/string("\nEquivalent Binary Value =", bnum[2:])
. The function[2:]
is added to skip the first two characters of the obtained binary number.
NOTE:
- The End parameter is used to add any string at the end of the output of the print statement
- 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 oct() function is used to get an octal value of an integer number.
- Â The int() function is used to get the decimal value of the given integer.
- Â The bin() function is used to get the Binary value of the given integer
- The print() function prints the specified message to the screen, or other standard output device.