This is a quick tutorial on how you can convert an octal number to a decimal in Python. The vice-versa i.e. Decimal To Octal Conversion in Python is also explained.
Octal To Decimal Conversion
You can use the int function in python with base 8 to convert an octal number to its decimal form. The syntax will as defined below.
Note. Make sure you pass the octal number argument as a String in with Quotes Symbol. The example is given below.
Consider the octal number 45. The decimal value here will be 37 which can be calculated using the int function in python as defined by the line of code given below.
decimalNumber = int('45', 8) print(decimalNumber)
Output. Shown In the Following Screenshot.
Decimal To Octal Conversion
If you want to convert a decimal number to its corresponding octal form, then you can simply use the inbuilt function oct with the decimal number to be converted into octal form as its only parameter.
Now as we know the decimal form of the octal number 45 is 37, let’s test here. We’ll provide the decimal value 37 as input here to check if it prints out the correct octal number, i.e. 45.
octalNumber = oct(37) print(octalNumber)
Output. Found Correct as shown in the screenshot given below.