The oct
method is used to convert an integer value to octal.
oct(x)
The function takes in one parameter, x
.
x
can be:
__index__()
with an integer return value.This method returns the octal representation of the passed argument.
The returned octal value is prefixed with
0o
.
print("Integer 9 to octal: ", oct(9))print("Binary 0b1111 to octal: ", oct(0b1111))print("Hexadecimal 0xFF to octal: ", oct(0xFF))class Test:val = 10def __index__(self):return self.valdef __int__(self):return self.valprint("The octal value of test object: ", oct(Test()))
In the code above:
We used the oct
method to convert the integer, binary and hexadecimal values to octal values.
We created a class Test
and implemented __index
to return an integer value.
When we call the oct
method with the Test
class object as an argument, we will get the octal value of the integer value returned from the __index__
method.