Python oct() Function

Converts an integer to octal, a base 8 number.

Syntax

Python
oct(number)

Parameters

ParameterDescription
number Required. The value to convert

Example

Python
squawk = 7421
print(oct(squawk))

Output

0o16375

Notes

If the object is not an int, is has to define the __index__() function and return an integer.

Example

Python
class Transponder():
    squawk = 7421

    def __index__(self):
        return self.squawk

print(oct(Transponder()))
Output:
0o16375

Who uses Octal?

Octal is often used in aviation — transponders in aircraft transmit a code, expressed as a four-octal-digit number, when interrogated by ground radar. This code is used to distinguish different aircraft on the radar screen.