In this python tutorial, you will learn how to Convert Octal Number to Decimal and vice-versa with the built in functions of the python programming language.
How to Convert Octal Number to Decimal 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.
OCTAL TO DECIMAL
RUN CODE SNIPPET#Python program to convert Octal Number to a decimal octal_num = input("Enter an octal number :") decimal_value = int(octal_num,8) print("The decimal value of {} is {}".format(octal_num,decimal_value))
INPUT:
34
OUTPUT:
Enter an octal number : The decimal value of 34 is 28
- Here we give the user the option to enter the values and the input values are scanned using the
input
function and are stored in the variableoctal_num
with the statements/strings("Enter an octal number :")
- In the STDIN section of the code editor the input values are entered.
- We declare the variable
decimal_value
with the  functionint(octal_num,8)
where theint
function is used to get the decimal value of the entered integer. - Now, we display the output value using the
print
and display the statement/string("The decimal value of {} is {}".format(octal_num,decimal_value))
 . - In the above statement/string, the variables
{0}
and{1}
will hold the values ofoctal_num
anddecimal_value
where theformat
function helps in variable substitution and data formatting.
Let’s take a look at the second source code,  here the values are given as input by the user in the code, the if statement and built in functions carry out the function.
DECIMAL TO OCTAL
RUN CODE SNIPPET#Python Program to Convert Decimal Number to Octal decimal_number = int(input("Enter a decimal number :")) octal_number = oct(decimal_number).replace("0o", "") print("\nThe octal value for {0} is {1}".format(decimal_number,octal_number ))
INPUT:
100
OUTPUT:
Enter a decimal number : The octal value for 100 is 144
- Here we give the user the option to enter the values and the input values are scanned using the
input
function and are stored in the variabledecimal_number
with the statements/strings("Enter a decimal number :")
, we use theint
function and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- We declare the variable
octal_number
with the  functionoct(decimal_number)
along with thereplace
function which is used to replace the values("0b","")
with another value. - Now, we display the output value using the
print
and display the statement/string("\nThe octal value for {0} is {1}".format(decimal_number,octal_number ))
 . - In the above statement/string, the variables
{0}
and{1}
will hold the values ofdecimal_number
andoctal_number
where theformat
function helps in variable substitution and data formatting.
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 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 to get the decimal value of the given integer.
- The print() function prints the specified message to the screen, or other standard output device.