A Decimal is a number that contains a point in it. In Python, decimals are also known as floating points as well. While Octal is a different method for counting. Sometimes, you might need to convert decimal to octal in Python without using any built-in methods in Python. In this short article, we will discuss how we can convert decimals to octal in Python using various methods.
How to convert Decimal to octal in Python?
A decimal number is also known as a floating number in Python. Decimal numbers start from 0 and go on. While octal numbers are another ways of counting.
Let us go through various methods to convert the decimal to octal in Python.
Method-1: Using the built-in method
The oct() method in Python is used to convert a decimal number into an octal. Let us first convert the decimal to octal using python by taking an example.
# decimal number a = 10 # decimal to octal in python o = oct(a) # print print(o)
Here is what the above code is doing:
a = 10– assigns the decimal number 10 to the variable
o = oct(a)– calls the
oct()function with the argument
ato convert the decimal number to its octal representation. The result is stored in the variable
print(o)– prints the octal representation of the decimal number 10, which is “0o12” in Python.
Method-2: Using While loop
We can use the while loop to convert decimal to octal in Python. A while loop is a loop that iterates through the object untill the condition is not False. Let us now learn how we can use the while loop to convert the decimal to octal and then we will explain the code.
# variables decimal = 10 octal = 0 ctr = 0 temp = decimal #computing octal using while loop while(temp > 0): octal += ((temp%8)*(10**ctr)) temp = int(temp/8) ctr += 1 print(octal)
Let us now go through the code step by step to understand how it actually works:
decimal = 10: This line initializes a decimal variable with a value of 10. You can replace this with any decimal number you want to convert to octal.
octal = 0: This line initializes an octal variable with a value of 0. This variable will be used to store the octal representation of the decimal number.
ctr = 0: This line initializes a counter variable with a value of 0. This variable will be used to keep track of the position of each digit in the octal number.
temp = decimal: This line initializes a temporary variable with the same value as the decimal variable. This variable will be used to perform the conversion.
while(temp > 0):: This line starts a while loop that will continue as long as the temporary variable is greater than 0.
octal += ((temp%8)*(10**ctr)): This line calculates the value of the current digit in the octal number and adds it to the octal variable. The calculation is done by getting the remainder of the temporary variable divided by 8 (which gives the current octal digit), multiplying it by 10 raised to the power of the counter variable (which gives the position of the digit), and adding it to the current value of the octal variable.
temp = int(temp/8): This line updates the value of the temporary variable by dividing it by 8 and converting the result to an integer (to get the quotient of the division).
ctr += 1: This line updates the value of the counter variable by incrementing it by 1.
print(octal): This line prints the final value of the octal variable, which represents the octal representation of the decimal number.
In this short article, we discussed how we can convert decimal to octal using Python. We covered two methods and explained each method deeply.